🔧 Read and modify token themes by id

This commit is contained in:
Andrés Moya
2025-09-24 11:06:52 +02:00
committed by Andrés Moya
parent 37679b7ec6
commit 4c35571336
12 changed files with 255 additions and 222 deletions

View File

@@ -387,8 +387,7 @@
[:set-token-theme
[:map {:title "SetTokenThemeChange"}
[:type [:= :set-token-theme]]
[:theme-name :string]
[:group :string]
[:id ::sm/uuid]
[:theme [:maybe ctob/schema:token-theme-attrs]]]]
[:set-active-token-themes
@@ -1009,20 +1008,20 @@
(ctob/update-set lib' id (fn [_] (ctob/make-token-set token-set))))))))
(defmethod process-change :set-token-theme
[data {:keys [group theme-name theme]}]
[data {:keys [id theme]}]
(update data :tokens-lib
(fn [lib]
(let [lib' (ctob/ensure-tokens-lib lib)]
(cond
(not theme)
(ctob/delete-theme lib' group theme-name)
(ctob/delete-theme lib' id)
(not (ctob/get-theme lib' group theme-name))
(not (ctob/get-theme lib' id))
(ctob/add-theme lib' (ctob/make-token-theme theme))
:else
(ctob/update-theme lib'
group theme-name
id
(fn [prev-token-theme]
(ctob/make-token-theme (merge prev-token-theme theme)))))))))

View File

@@ -987,30 +987,18 @@
:token-set (datafy prev-token-set)})
(apply-changes-local))))
(defn set-token-theme [changes group theme-name theme]
(defn set-token-theme [changes id theme]
(assert-library! changes)
(let [library-data (::library-data (meta changes))
prev-theme (some-> (get library-data :tokens-lib)
(ctob/get-theme group theme-name))]
(ctob/get-theme id))]
(-> changes
(update :redo-changes conj {:type :set-token-theme
:theme-name theme-name
:group group
:id id
:theme theme})
(update :undo-changes conj (if prev-theme
{:type :set-token-theme
:group group
:theme-name (or
;; Undo of edit
(:name theme)
;; Undo of delete
theme-name)
:theme prev-theme}
;; Undo of create
{:type :set-token-theme
:group group
:theme-name theme-name
:theme nil}))
(update :undo-changes conj {:type :set-token-theme
:id id
:theme prev-theme})
(apply-changes-local))))
(defn set-active-token-themes

View File

@@ -17,7 +17,7 @@
Use this for managing sets active state without having to modify a
user created theme (\"no themes selected\" state in the ui)."
[changes tokens-lib update-theme-fn]
(let [active-token-set-names (ctob/get-active-themes-set-names tokens-lib)
(let [active-token-set-names (ctob/get-active-themes-set-names tokens-lib)
hidden-theme (ctob/get-hidden-theme tokens-lib)
hidden-theme' (-> (some-> hidden-theme
@@ -25,8 +25,7 @@
(update-theme-fn))]
(-> changes
(pcb/set-active-token-themes #{(ctob/get-theme-path hidden-theme')})
(pcb/set-token-theme (:group hidden-theme)
(:name hidden-theme)
(pcb/set-token-theme (ctob/get-id hidden-theme)
hidden-theme'))))
(defn generate-toggle-token-set

View File

@@ -544,6 +544,9 @@
(theme-matches-group-name [_ group name] "if a theme matches the given group & name")
(hidden-theme? [_] "if a theme is the (from the user ui) hidden temporary theme"))
(def hidden-theme-id
uuid/zero)
(def hidden-theme-group
"")
@@ -732,20 +735,21 @@
(defprotocol ITokenThemes
"Collection of themes in groups"
(add-theme [_ token-theme] "add a theme to the library, at the end")
(update-theme [_ group name f] "modify a theme in the ilbrary")
(delete-theme [_ group name] "delete a theme in the library")
(update-theme [_ id f] "modify a theme in the ilbrary")
(delete-theme [_ id] "delete a theme in the library")
(theme-count [_] "get the total number if themes in the library")
(get-theme-tree [_] "get a nested tree of all themes in the library")
(get-themes [_] "get an ordered sequence of all themes in the library")
(get-theme [_ group name] "get one theme looking for name")
(get-theme [_ id] "get one theme looking for id")
(get-theme-by-name [_ group name] "get one theme looking for group and name")
(get-theme-groups [_] "get a sequence of group names by order")
(get-active-theme-paths [_] "get the active theme paths")
(get-active-themes [_] "get an ordered sequence of active themes in the library")
(set-active-themes [_ active-themes] "set active themes in library")
(theme-active? [_ group name] "predicate if token theme is active")
(activate-theme [_ group name] "adds theme from the active-themes")
(deactivate-theme [_ group name] "removes theme from the active-themes")
(toggle-theme-active? [_ group name] "toggles theme in the active-themes")
(theme-active? [_ id] "predicate if token theme is active")
(activate-theme [_ id] "adds theme from the active-themes")
(deactivate-theme [_ id] "removes theme from the active-themes")
(toggle-theme-active? [_ id] "toggles theme in the active-themes")
(get-hidden-theme [_] "get the hidden temporary theme"))
(def schema:token-themes
@@ -896,7 +900,7 @@
(get-token-by-name [_ set-name token-name] "get token in a set searching by set and token names")
(update-token [_ set-id token-id f] "update a token in a set")
(delete-token [_ set-id token-id] "delete a token from a set")
(toggle-set-in-theme [_ group-name theme-name set-name] "toggle a set used / not used in a theme")
(toggle-set-in-theme [_ theme-id set-name] "toggle a set used / not used in a theme")
(get-active-themes-set-names [_] "set of set names that are active in the the active themes")
(sets-at-path-all-active? [_ group-path] "compute active state for child sets at `group-path`.
Will return a value that matches this schema:
@@ -1121,31 +1125,37 @@ Will return a value that matches this schema:
(update themes (:group token-theme) d/oassoc (:name token-theme) token-theme)
active-themes)))
(update-theme [this group name f]
(let [theme (dm/get-in themes [group name])]
(if theme
(let [theme' (-> (make-token-theme (f theme))
(assoc :modified-at (ct/now)))
group' (:group theme')
name' (:name theme')
same-group? (= group group')
same-name? (= name name')
same-path? (and same-group? same-name?)]
(TokensLib. sets
(if same-path?
(update themes group' assoc name' theme')
(-> themes
(d/oassoc-in-before [group name] [group' name'] theme')
(d/dissoc-in [group name])))
(if same-path?
active-themes
(disj active-themes (join-theme-path group name)))))
this)))
(update-theme [this id f]
(if-let [theme (get-theme this id)]
(let [group (:group theme)
name (:name theme)
theme' (-> (make-token-theme (f theme))
(assoc :modified-at (ct/now)))
group' (:group theme')
name' (:name theme')
same-group? (= group group')
same-name? (= name name')
same-path? (and same-group? same-name?)]
(TokensLib. sets
(if same-path?
(update themes group' assoc name' theme')
(-> themes
(d/oassoc-in-before [group name] [group' name'] theme')
(d/dissoc-in [group name])))
(if same-path?
active-themes
(disj active-themes (join-theme-path group name)))))
this))
(delete-theme [_ group name]
(TokensLib. sets
(d/dissoc-in themes [group name])
(disj active-themes (join-theme-path group name))))
(delete-theme [this id]
(let [theme (get-theme this id)
[group name] [(:group theme) (:name theme)]]
(if theme
(TokensLib. sets
(d/dissoc-in themes [group name])
(disj active-themes (join-theme-path group name)))
this)))
(get-theme-tree [_]
themes)
@@ -1163,7 +1173,11 @@ Will return a value that matches this schema:
(theme-count [this]
(count (get-themes this)))
(get-theme [_ group name]
(get-theme [this id]
(->> (get-themes this)
(d/seek #(= (:id %) id))))
(get-theme-by-name [_ group name]
(dm/get-in themes [group name]))
(set-active-themes [_ active-themes]
@@ -1171,9 +1185,10 @@ Will return a value that matches this schema:
themes
active-themes))
(activate-theme [this group name]
(if-let [theme (get-theme this group name)]
(let [group-themes (->> (get themes group)
(activate-theme [this id]
(if-let [theme (get-theme this id)]
(let [group (:group theme)
group-themes (->> (get themes group)
(map (comp get-theme-path val))
(into #{}))
active-themes' (-> (set/difference active-themes group-themes)
@@ -1183,18 +1198,21 @@ Will return a value that matches this schema:
active-themes'))
this))
(deactivate-theme [_ group name]
(TokensLib. sets
themes
(disj active-themes (join-theme-path group name))))
(deactivate-theme [this id]
(if-let [theme (get-theme this id)]
(TokensLib. sets
themes
(disj active-themes (get-theme-path theme)))
this))
(theme-active? [_ group name]
(contains? active-themes (join-theme-path group name)))
(theme-active? [this id]
(when-let [theme (get-theme this id)]
(contains? active-themes (get-theme-path theme))))
(toggle-theme-active? [this group name]
(if (theme-active? this group name)
(deactivate-theme this group name)
(activate-theme this group name)))
(toggle-theme-active? [this id]
(if (theme-active? this id)
(deactivate-theme this id)
(activate-theme this id)))
(get-active-theme-paths [_]
active-themes)
@@ -1204,11 +1222,11 @@ Will return a value that matches this schema:
(list)
(comp
(filter (partial instance? TokenTheme))
(filter #(theme-active? this (:group %) (:name %))))
(filter #(theme-active? this (get-id %))))
(tree-seq d/ordered-map? vals themes)))
(get-hidden-theme [this]
(get-theme this hidden-theme-group hidden-theme-name))
(get-theme this hidden-theme-id))
ITokensLib
(empty-lib? [this]
@@ -1242,10 +1260,10 @@ Will return a value that matches this schema:
(delete-token [this set-id token-id]
(update-set this set-id #(delete-token- % token-id)))
(toggle-set-in-theme [this theme-group theme-name set-name]
(if-let [_theme (get-in themes theme-group theme-name)]
(toggle-set-in-theme [this theme-id set-name]
(if-let [theme (get-theme this theme-id)]
(TokensLib. sets
(d/oupdate-in themes [theme-group theme-name]
(d/oupdate-in themes [(:group theme) (:name theme)]
#(toggle-set % set-name))
active-themes)
this))
@@ -1643,7 +1661,7 @@ Will return a value that matches this schema:
ordered-set-names)
library
(update-theme library hidden-theme-group hidden-theme-name
(update-theme library hidden-theme-id
#(assoc % :sets active-set-names))
library
@@ -1651,8 +1669,9 @@ Will return a value that matches this schema:
library
(reduce (fn [library theme-path]
(let [[group name] (split-theme-path theme-path)]
(activate-theme library group name)))
(let [[group name] (split-theme-path theme-path)
theme (get-theme-by-name library group name)]
(activate-theme library (get-id theme))))
library
active-theme-names)]

View File

@@ -34,6 +34,7 @@
(pcb/with-library-data (:data file))
(clt/generate-toggle-token-set (tht/get-tokens-lib file) "foo/bar"))
_ (prn "changes" changes)
redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo)
undo (thf/apply-undo-changes redo changes)
@@ -84,84 +85,85 @@
(t/deftest set-token-theme-test
(t/testing "delete token theme"
(let [theme-name "foo"
group "main"
(let [theme-id (uuid/next)
file (setup-file #(-> %
(ctob/add-theme (ctob/make-token-theme :name theme-name
:group group))))
(ctob/add-theme (ctob/make-token-theme :id theme-id
:name "foo"
:group "main"))))
changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(pcb/set-token-theme group theme-name nil))
(pcb/set-token-theme theme-id nil))
redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo)
undo (thf/apply-undo-changes redo changes)
undo-lib (tht/get-tokens-lib undo)]
;; Redo
(t/is (nil? (ctob/get-theme redo-lib group theme-name)))
(t/is (nil? (ctob/get-theme redo-lib theme-id)))
;; Undo
(t/is (some? (ctob/get-theme undo-lib group theme-name)))))
(t/is (some? (ctob/get-theme undo-lib theme-id)))))
(t/testing "add token theme"
(let [theme-name "foo"
group "main"
theme (ctob/make-token-theme :name theme-name
:group group)
(let [theme-id (uuid/next)
theme (ctob/make-token-theme :id theme-id
:name "foo"
:group "main")
file (setup-file identity)
changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(pcb/set-token-theme group theme-name theme))
(pcb/set-token-theme theme-id theme))
redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo)
undo (thf/apply-undo-changes redo changes)
undo-lib (tht/get-tokens-lib undo)]
;; Redo
(t/is (some? (ctob/get-theme redo-lib group theme-name)))
(t/is (some? (ctob/get-theme redo-lib theme-id)))
;; Undo
(t/is (nil? (ctob/get-theme undo-lib group theme-name)))))
(t/is (nil? (ctob/get-theme undo-lib theme-id)))))
(t/testing "update token theme"
(let [theme-name "foo"
group "main"
prev-theme (ctob/make-token-theme :name theme-name
:group group)
(let [theme-id (uuid/next)
prev-theme-name "foo"
prev-theme (ctob/make-token-theme :id theme-id
:name prev-theme-name
:group "main")
file (setup-file #(ctob/add-theme % prev-theme))
new-theme-name "foo1"
changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(pcb/set-token-theme group new-theme-name prev-theme))
(pcb/set-token-theme theme-id (ctob/rename prev-theme new-theme-name)))
redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo)
redo-theme (ctob/get-theme redo-lib theme-id)
undo (thf/apply-undo-changes redo changes)
undo-lib (tht/get-tokens-lib undo)]
undo-lib (tht/get-tokens-lib undo)
undo-theme (ctob/get-theme undo-lib theme-id)]
;; Redo
(t/is (some? (ctob/get-theme redo-lib group theme-name)))
(t/is (nil? (ctob/get-theme redo-lib group new-theme-name)))
(t/is (= new-theme-name (ctob/get-name redo-theme)))
;; Undo
(t/is (some? (ctob/get-theme undo-lib group theme-name)))
(t/is (nil? (ctob/get-theme undo-lib group new-theme-name)))))
(t/is (= prev-theme-name (ctob/get-name undo-theme)))))
(t/testing "toggling token theme updates using changes history"
(let [theme-name "foo-theme"
group "main"
(let [theme-id (uuid/next)
theme (ctob/make-token-theme :id theme-id
:name "foo-theme"
:group "main")
set-name "bar-set"
token-set (ctob/make-token-set :name set-name)
theme (ctob/make-token-theme :name theme-name
:group group)
file (setup-file #(-> %
(ctob/add-theme theme)
(ctob/add-set token-set)))
theme' (assoc theme :sets #{set-name})
changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(pcb/set-token-theme group theme-name theme'))
(pcb/set-token-theme theme-id theme'))
changed-file (-> file
(thf/apply-changes changes)
(thf/apply-undo-changes changes)
(thf/apply-changes changes))
changed-lib (tht/get-tokens-lib changed-file)]
(t/is (= #{set-name}
(-> changed-lib (ctob/get-theme group theme-name) :sets))))))
(-> changed-lib (ctob/get-theme theme-id) :sets))))))
(t/deftest set-token-test
(t/testing "delete token"

View File

@@ -146,13 +146,15 @@
(t/is (= ["a/a" "a/b" "b/a" "b/b"] (move ["a" "b"] ["a" "b"] nil true))))))
(t/deftest move-token-set-nested-3
(let [tokens-lib (-> (ctob/make-tokens-lib)
(let [theme-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :name "Foo/Bar/Baz"))
(ctob/add-set (ctob/make-token-set :name "Other"))
(ctob/add-theme (ctob/make-token-theme :name "Theme"
(ctob/add-theme (ctob/make-token-theme :id theme-id
:name "Theme"
:sets #{"Foo/Bar/Baz"}))
(ctob/move-set ["Foo" "Bar" "Baz"] ["Other/Baz"] nil nil))]
(t/is (= #{"Other/Baz"} (:sets (ctob/get-theme tokens-lib "" "Theme"))))))
(t/is (= #{"Other/Baz"} (:sets (ctob/get-theme tokens-lib theme-id))))))
(t/deftest move-token-set-group
(t/testing "reordering"
@@ -170,14 +172,16 @@
(t/is (= ["Bar/Foo" "Foo/A" "Foo/B"] (move ["Bar"] ["Bar"] ["Foo"] true)))))
(t/testing "updates theme set names"
(let [tokens-lib (-> (ctob/make-tokens-lib)
(let [theme-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :name "Foo/A"))
(ctob/add-set (ctob/make-token-set :name "Foo/B"))
(ctob/add-set (ctob/make-token-set :name "Bar/Foo"))
(ctob/add-theme (ctob/make-token-theme :name "Theme"
(ctob/add-theme (ctob/make-token-theme :id theme-id
:name "Theme"
:sets #{"Foo/A" "Bar/Foo"}))
(ctob/move-set-group ["Foo"] ["Bar" "Foo"] nil nil))]
(t/is (= #{"Bar/Foo/A" "Bar/Foo"} (:sets (ctob/get-theme tokens-lib "" "Theme")))))))
(t/is (= #{"Bar/Foo/A" "Bar/Foo"} (:sets (ctob/get-theme tokens-lib theme-id)))))))
(t/deftest tokens-tree
(let [tokens-lib (-> (ctob/make-tokens-lib)
@@ -292,16 +296,17 @@
(t/is (ct/is-after? (ctob/get-modified-at token-set') (ctob/get-modified-at token-set)))))
(t/deftest rename-token-set-group
(let [tokens-lib (-> (ctob/make-tokens-lib)
(let [theme-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :name "foo/bar/baz"))
(ctob/add-set (ctob/make-token-set :name "foo/bar/baz/baz-child-1"))
(ctob/add-set (ctob/make-token-set :name "foo/bar/baz/baz-child-2"))
(ctob/add-theme (ctob/make-token-theme :name "theme" :sets #{"foo/bar/baz/baz-child-1"})))
(ctob/add-theme (ctob/make-token-theme :id theme-id :name "theme" :sets #{"foo/bar/baz/baz-child-1"})))
tokens-lib' (-> tokens-lib
(ctob/rename-set-group ["foo" "bar"] "bar-renamed")
(ctob/rename-set-group ["foo" "bar-renamed" "baz"] "baz-renamed"))
expected-set-names (ctob/get-set-names tokens-lib')
expected-theme-sets (-> (ctob/get-theme tokens-lib' "" "theme")
expected-theme-sets (-> (ctob/get-theme tokens-lib' theme-id)
:sets)]
(t/is (= expected-set-names
'("foo/bar-renamed/baz"
@@ -558,7 +563,7 @@
(ctob/make-token :name "token-4"
:type :border-radius
:value 4000)}))
(ctob/update-theme ctob/hidden-theme-group ctob/hidden-theme-name
(ctob/update-theme ctob/hidden-theme-id
#(ctob/enable-sets % #{"set-a" "set-b"})))
tokens (ctob/get-tokens-in-active-sets tokens-lib)]
@@ -760,33 +765,36 @@
(t/is (= :none expected-invalid-none))))
(t/deftest add-token-theme
(let [tokens-lib (ctob/make-tokens-lib)
token-theme (ctob/make-token-theme :name "test-token-theme")
(let [theme-id (uuid/next)
tokens-lib (ctob/make-tokens-lib)
token-theme (ctob/make-token-theme :id theme-id :name "test-token-theme")
tokens-lib' (ctob/add-theme tokens-lib token-theme)
token-themes' (ctob/get-themes tokens-lib')
token-theme' (ctob/get-theme tokens-lib' "" "test-token-theme")]
token-theme' (ctob/get-theme tokens-lib' theme-id)]
(t/is (= (ctob/theme-count tokens-lib') 2))
(t/is (= (second token-themes') token-theme))
(t/is (= token-theme' token-theme))))
(t/deftest update-token-theme
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme")))
(let [theme-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :id theme-id
:name "test-token-theme")))
tokens-lib' (-> tokens-lib
(ctob/update-theme "" "test-token-theme"
(ctob/update-theme theme-id
(fn [token-theme]
(assoc token-theme
:description "some description")))
(ctob/update-theme "" "not-existing-theme"
(ctob/update-theme (uuid/next)
(fn [token-theme]
(assoc token-theme
:description "no-effect"))))
token-theme (ctob/get-theme tokens-lib "" "test-token-theme")
token-theme' (ctob/get-theme tokens-lib' "" "test-token-theme")]
token-theme (ctob/get-theme tokens-lib theme-id)
token-theme' (ctob/get-theme tokens-lib' theme-id)]
(t/is (= (ctob/theme-count tokens-lib') 2))
(t/is (= (:name token-theme') "test-token-theme"))
@@ -794,49 +802,55 @@
(t/is (ct/is-after? (:modified-at token-theme') (:modified-at token-theme)))))
(t/deftest rename-token-theme
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme")))
(let [theme-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :id theme-id
:name "test-token-theme")))
tokens-lib' (-> tokens-lib
(ctob/update-theme "" "test-token-theme"
(ctob/update-theme theme-id
(fn [token-theme]
(assoc token-theme
:name "updated-name"))))
token-theme (ctob/get-theme tokens-lib "" "test-token-theme")
token-theme' (ctob/get-theme tokens-lib' "" "updated-name")]
token-theme (ctob/get-theme tokens-lib theme-id)
token-theme' (ctob/get-theme tokens-lib' theme-id)]
(t/is (= (ctob/theme-count tokens-lib') 2))
(t/is (= (:name token-theme') "updated-name"))
(t/is (ct/is-after? (:modified-at token-theme') (:modified-at token-theme)))))
(t/deftest delete-token-theme
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme")))
(let [theme-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :id theme-id
:name "test-token-theme")))
tokens-lib' (-> tokens-lib
(ctob/delete-theme theme-id)
(ctob/delete-theme (uuid/next)))
tokens-lib' (-> tokens-lib
(ctob/delete-theme "" "test-token-theme")
(ctob/delete-theme "" "not-existing-theme"))
token-theme' (ctob/get-theme tokens-lib' "" "updated-name")]
token-theme' (ctob/get-theme tokens-lib' theme-id)]
(t/is (= (ctob/theme-count tokens-lib') 1))
(t/is (nil? token-theme'))))
(t/deftest toggle-set-in-theme
(let [tokens-lib (-> (ctob/make-tokens-lib)
(let [theme-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :name "token-set-1"))
(ctob/add-set (ctob/make-token-set :name "token-set-2"))
(ctob/add-set (ctob/make-token-set :name "token-set-3"))
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme")))
(ctob/add-theme (ctob/make-token-theme :id theme-id
:name "test-token-theme"
:sets #{"token-set-1" "token-set-2"})))
tokens-lib' (-> tokens-lib
(ctob/toggle-set-in-theme "" "test-token-theme" "token-set-1")
(ctob/toggle-set-in-theme "" "test-token-theme" "token-set-2")
(ctob/toggle-set-in-theme "" "test-token-theme" "token-set-2"))
(ctob/toggle-set-in-theme theme-id "token-set-1")
(ctob/toggle-set-in-theme theme-id "token-set-3"))
token-theme (ctob/get-theme tokens-lib "" "test-token-theme")
token-theme' (ctob/get-theme tokens-lib' "" "test-token-theme")]
token-theme (ctob/get-theme tokens-lib theme-id)
token-theme' (ctob/get-theme tokens-lib' theme-id)]
(t/is (= (:sets token-theme') #{"token-set-2" "token-set-3"}))
(t/is (ct/is-after? (:modified-at token-theme') (:modified-at token-theme)))))
(t/deftest transit-serialization
@@ -846,8 +860,9 @@
(ctob/make-token :name "test-token"
:type :boolean
:value true))
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme"))
(ctob/toggle-set-in-theme "" "test-token-theme" "test-token-set"))
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :test-token-theme)
:name "test-token-theme"))
(ctob/toggle-set-in-theme (thi/id :test-token-theme) "test-token-set"))
encoded-str (tr/encode-str tokens-lib)
tokens-lib' (tr/decode-str encoded-str)]
@@ -864,8 +879,9 @@
(ctob/make-token :name "test-token"
:type :boolean
:value true))
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme"))
(ctob/toggle-set-in-theme "" "test-token-theme" "test-token-set"))
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :test-token-theme)
:name "test-token-theme"))
(ctob/toggle-set-in-theme (thi/id :test-token-theme) "test-token-set"))
encoded-blob (fres/encode tokens-lib)
tokens-lib' (fres/decode encoded-blob)]
@@ -1180,14 +1196,18 @@
(t/is (nil? token-set'))))
(t/deftest update-token-theme-in-groups
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :group "" :name "token-theme-1"))
(ctob/add-theme (ctob/make-token-theme :group "group1" :name "token-theme-2"))
(ctob/add-theme (ctob/make-token-theme :group "group1" :name "token-theme-3"))
(ctob/add-theme (ctob/make-token-theme :group "group2" :name "token-theme-4")))
(let [theme-1-id (uuid/next)
theme-2-id (uuid/next)
theme-3-id (uuid/next)
theme-4-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :id theme-1-id :group "" :name "token-theme-1"))
(ctob/add-theme (ctob/make-token-theme :id theme-2-id :group "group1" :name "token-theme-2"))
(ctob/add-theme (ctob/make-token-theme :id theme-3-id :group "group1" :name "token-theme-3"))
(ctob/add-theme (ctob/make-token-theme :id theme-4-id :group "group2" :name "token-theme-4")))
tokens-lib' (-> tokens-lib
(ctob/update-theme "group1" "token-theme-2"
(ctob/update-theme theme-2-id
(fn [token-theme]
(assoc token-theme :description "some description"))))
@@ -1215,15 +1235,18 @@
(t/is (= token-groups ["group1" "group2"]))))
(t/deftest rename-token-theme-in-groups
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :group "" :name "token-theme-1"))
(ctob/add-theme (ctob/make-token-theme :group "group1" :name "token-theme-2"))
(ctob/add-theme (ctob/make-token-theme :group "group1" :name "token-theme-3"))
(ctob/add-theme (ctob/make-token-theme :group "group2" :name "token-theme-4")))
(let [theme-1-id (uuid/next)
theme-2-id (uuid/next)
theme-3-id (uuid/next)
theme-4-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :id theme-1-id :group "" :name "token-theme-1"))
(ctob/add-theme (ctob/make-token-theme :id theme-2-id :group "group1" :name "token-theme-2"))
(ctob/add-theme (ctob/make-token-theme :id theme-3-id :group "group1" :name "token-theme-3"))
(ctob/add-theme (ctob/make-token-theme :id theme-4-id :group "group2" :name "token-theme-4")))
tokens-lib' (-> tokens-lib
(ctob/update-theme "group1" "token-theme-2"
(ctob/update-theme theme-2-id
(fn [token-theme]
(assoc token-theme
:name "updated-name"))))
@@ -1243,14 +1266,18 @@
(t/is (ct/is-after? (:modified-at token-theme') (:modified-at token-theme)))))
(t/deftest move-token-theme-of-group
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :group "" :name "token-theme-1"))
(ctob/add-theme (ctob/make-token-theme :group "group1" :name "token-theme-2"))
(ctob/add-theme (ctob/make-token-theme :group "group1" :name "token-theme-3"))
#_(ctob/add-theme (ctob/make-token-theme :group "group2" :name "token-theme-4")))
(let [theme-1-id (uuid/next)
theme-2-id (uuid/next)
theme-3-id (uuid/next)
theme-4-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :id theme-1-id :group "" :name "token-theme-1"))
(ctob/add-theme (ctob/make-token-theme :id theme-2-id :group "group1" :name "token-theme-2"))
(ctob/add-theme (ctob/make-token-theme :id theme-3-id :group "group1" :name "token-theme-3"))
#_(ctob/add-theme (ctob/make-token-theme :in-theme-4-id :group "group2" :name "token-theme-4")))
tokens-lib' (-> tokens-lib
(ctob/update-theme "group1" "token-theme-2"
(ctob/update-theme theme-2-id
(fn [token-theme]
(assoc token-theme
:name "updated-name"
@@ -1273,12 +1300,14 @@
(t/is (ct/is-after? (:modified-at token-theme') (:modified-at token-theme)))))
(t/deftest delete-token-theme-in-group
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :group "" :name "token-theme-1"))
(ctob/add-theme (ctob/make-token-theme :group "group1" :name "token-theme-2")))
(let [theme-1-id (uuid/next)
theme-2-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :id theme-1-id :group "" :name "token-theme-1"))
(ctob/add-theme (ctob/make-token-theme :id theme-2-id :group "group1" :name "token-theme-2")))
tokens-lib' (-> tokens-lib
(ctob/delete-theme "group1" "token-theme-2"))
(ctob/delete-theme theme-2-id))
themes-tree' (ctob/get-theme-tree tokens-lib')
token-theme' (get-in themes-tree' ["group1" "token-theme-2"])]
@@ -1310,7 +1339,7 @@
(let [json (-> (slurp "test/common_tests/types/data/tokens-multi-set-legacy-example.json")
(json/decode {:key-fn identity}))
lib (ctob/parse-decoded-json json "")
token-theme (ctob/get-theme lib "group-1" "theme-1")]
token-theme (ctob/get-theme-by-name lib "group-1" "theme-1")]
(t/is (= '("core" "light" "dark" "theme") (ctob/get-set-names lib)))
(t/testing "set exists in theme"
(t/is (= (:group token-theme) "group-1"))
@@ -1340,7 +1369,7 @@
(let [json (-> (slurp "test/common_tests/types/data/tokens-multi-set-example.json")
(json/decode {:key-fn identity}))
lib (ctob/parse-decoded-json json "")
token-theme (ctob/get-theme lib "group-1" "theme-1")]
token-theme (ctob/get-theme-by-name lib "group-1" "theme-1")]
(t/is (= '("core" "light" "dark" "theme") (ctob/get-set-names lib)))
(t/testing "set exists in theme"
(t/is (= (:group token-theme) "group-1"))
@@ -1525,12 +1554,13 @@
{:name "button.primary.background"
:type :color
:value "{accent.default}"})}))
(ctob/add-theme (ctob/make-token-theme :name "theme-1"
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :theme-1)
:name "theme-1"
:group "group-1"
:external-id "test-id-01"
:modified-at now
:sets #{"core"}))
(ctob/toggle-theme-active? "group-1" "theme-1"))
(ctob/toggle-theme-active? (thi/id :theme-1)))
result (ctob/export-dtcg-json tokens-lib)
expected {"$themes" [{"description" ""
"group" "group-1"
@@ -1578,12 +1608,13 @@
{:name "button.primary.background"
:type :color
:value "{accent.default}"})}))
(ctob/add-theme (ctob/make-token-theme :name "theme-1"
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :theme-1)
:name "theme-1"
:group "group-1"
:external-id "test-id-01"
:modified-at now
:sets #{"some/set"}))
(ctob/toggle-theme-active? "group-1" "theme-1"))
(ctob/toggle-theme-active? (thi/id :theme-1)))
result (ctob/export-dtcg-multi-file tokens-lib)
expected {"$themes.json" [{"description" ""
"group" "group-1"

View File

@@ -73,39 +73,35 @@
(let [data (dsh/lookup-file-data state)
tokens-lib (get data :tokens-lib)]
(if (and tokens-lib (ctob/get-theme tokens-lib (:group token-theme) (:name token-theme)))
(if (and tokens-lib (ctob/get-theme tokens-lib (ctob/get-id token-theme)))
(rx/of (ntf/show {:content (tr "errors.token-theme-already-exists")
:type :toast
:level :error
:timeout 9000}))
(let [changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/set-token-theme (:group new-token-theme)
(:name new-token-theme)
(pcb/set-token-theme (ctob/get-id new-token-theme)
new-token-theme))]
(rx/of (dch/commit-changes changes)))))))))
(defn update-token-theme [[group name] token-theme]
(defn update-token-theme [id token-theme]
(ptk/reify ::update-token-theme
ptk/WatchEvent
(watch [it state _]
(let [data (dsh/lookup-file-data state)
tokens-lib (get data :tokens-lib)]
(if (and (or (not= group (:group token-theme))
(not= name (:name token-theme)))
(ctob/get-theme tokens-lib
(:group token-theme)
(:name token-theme)))
(if (and (not= id (ctob/get-id token-theme))
(ctob/get-theme tokens-lib (ctob/get-id token-theme)))
(rx/of (ntf/show {:content (tr "errors.token-theme-already-exists")
:type :toast
:level :error
:timeout 9000}))
(let [changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/set-token-theme group name token-theme))]
(pcb/set-token-theme (ctob/get-id token-theme) token-theme))]
(rx/of (dch/commit-changes changes))))))))
(defn toggle-token-theme-active? [group name]
(defn toggle-token-theme-active? [id]
(ptk/reify ::toggle-token-theme-active?
ptk/WatchEvent
(watch [it state _]
@@ -113,7 +109,7 @@
tokens-lib (get-tokens-lib state)
active-token-themes (some-> tokens-lib
(ctob/toggle-theme-active? group name)
(ctob/toggle-theme-active? id)
(ctob/get-active-theme-paths))
active-token-themes' (if (= active-token-themes #{ctob/hidden-theme-path})
active-token-themes
@@ -125,14 +121,14 @@
(dch/commit-changes changes)
(dwtp/propagate-workspace-tokens))))))
(defn delete-token-theme [group theme-name]
(defn delete-token-theme [id]
(ptk/reify ::delete-token-theme
ptk/WatchEvent
(watch [it state _]
(let [data (dsh/lookup-file-data state)
changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/set-token-theme group theme-name nil))]
(pcb/set-token-theme id nil))]
(rx/of
(dch/commit-changes changes)
(dwtp/propagate-workspace-tokens))))))
@@ -341,8 +337,7 @@
(pcb/with-library-data data)
(pcb/set-token-set (ctob/get-id token-set) token-set)
(pcb/set-token (ctob/get-id token-set) (:id token) token)
(pcb/set-token-theme (:group hidden-theme)
(:name hidden-theme)
(pcb/set-token-theme (ctob/get-id hidden-theme)
hidden-theme-with-set)
(pcb/set-active-token-themes #{ctob/hidden-theme-path}))]
(rx/of (dch/commit-changes changes)

View File

@@ -437,18 +437,18 @@
(l/derived (d/nilf ctob/get-theme-groups) tokens-lib))
(defn workspace-token-theme
[group name]
[id]
(l/derived
(fn [lib]
(when lib
(ctob/get-theme lib group name)))
(ctob/get-theme lib id)))
tokens-lib))
(def workspace-token-theme-tree-no-hidden
(l/derived (fn [lib]
(or
(some-> lib
(ctob/delete-theme ctob/hidden-theme-group ctob/hidden-theme-name)
(ctob/delete-theme ctob/hidden-theme-id)
(ctob/get-theme-tree))
[]))
tokens-lib))

View File

@@ -9,6 +9,7 @@
(:require
[app.common.data :as d]
[app.common.data.macros :as dm]
[app.common.types.tokens-lib :as ctob]
[app.main.constants :refer [max-input-length]]
[app.main.data.common :as dcm]
[app.main.data.event :as-alias ev]
@@ -159,11 +160,10 @@
(fn [_]
(let [;; We want to create a token on the first set
;; if there are many in this group
complete-name (str (:group set) "/" (first (:sets set)))
path-set (group->paths set)]
(st/emit! (dcm/go-to-workspace :layout :tokens)
(ptk/data-event :expand-token-sets {:paths path-set})
(dwtl/set-selected-token-set-name complete-name)
(dwtl/set-selected-token-set-id (ctob/get-id set))
(dwtl/set-token-type-section-open :color true)
(let [{:keys [modal title]} (get dwta/token-properties :color)
window-size (dom/get-window-size)

View File

@@ -362,7 +362,7 @@
(fn []
(rx/dispose! sub))))
(for [{:keys [token-set id index is-new is-group path parent-path depth] :as node}
(for [{:keys [token-set id index is-new is-group path depth] :as node}
(ctob/sets-tree-seq token-sets
{:skip-children-pred collapsed?
:new-at-path new-path})]

View File

@@ -78,7 +78,7 @@
(mf/defc themes-overview
[{:keys [change-view]}]
(let [active-theme-ids (mf/deref refs/workspace-active-theme-paths)
(let [active-theme-paths (mf/deref refs/workspace-active-theme-paths)
themes-groups (mf/deref refs/workspace-token-theme-tree-no-hidden)
create-theme
@@ -105,20 +105,20 @@
[:> icon* {:icon-id i/group :class (stl/css :group-title-icon)}]
[:> text* {:as "span" :typography "body-medium" :class (stl/css :group-title-name)} group]]])
[:ul {:class (stl/css :theme-group-rows-wrapper)}
(for [[_ {:keys [group name] :as theme}] themes
:let [theme-id (ctob/get-theme-path theme)
selected? (some? (get active-theme-ids theme-id))
(for [[_ {:keys [id name] :as theme}] themes
:let [theme-path (ctob/get-theme-path theme)
selected? (some? (get active-theme-paths theme-path))
delete-theme
(fn [e]
(dom/prevent-default e)
(dom/stop-propagation e)
(st/emit! (dwtl/delete-token-theme group name)))
(st/emit! (dwtl/delete-token-theme id)))
on-edit-theme
(fn [e]
(dom/prevent-default e)
(dom/stop-propagation e)
(change-view :edit-theme {:theme-path [(:id theme) (:group theme) (:name theme)]}))]]
[:li {:key theme-id
(change-view :edit-theme {:theme-info [(:id theme) (:group theme) (:name theme)]}))]]
[:li {:key theme-path
:class (stl/css :theme-row)}
[:div {:class (stl/css :theme-switch-row)}
@@ -126,7 +126,7 @@
[:div {:on-click (fn [e]
(dom/prevent-default e)
(dom/stop-propagation e)
(st/emit! (dwtl/toggle-token-theme-active? group name)))}
(st/emit! (dwtl/toggle-token-theme-active? id)))}
[:& switch {:name (tr "workspace.tokens.theme-name" name)
:on-change (constantly nil)
:selected? selected?}]]]
@@ -238,7 +238,7 @@
(let [tlib (-> (ctob/make-tokens-lib)
(ctob/add-theme theme))
tlib (reduce ctob/add-set tlib sets)]
(ctob/activate-theme tlib (:group theme) (:name theme))))
(ctob/activate-theme tlib (ctob/get-id theme))))
(mf/defc edit-create-theme*
[{:keys [change-view theme on-save is-editing has-prev-view]}]
@@ -285,7 +285,7 @@
(mf/use-fn
(mf/deps current-theme on-back)
(fn []
(st/emit! (dwtl/delete-token-theme (:group current-theme) (:name current-theme)))
(st/emit! (dwtl/delete-token-theme (ctob/get-id current-theme)))
(on-back)))
;; Sets tree handlers
@@ -369,16 +369,16 @@
(mf/defc edit-theme
[{:keys [state change-view]}]
(let [{:keys [theme-path]} state
[_ theme-group theme-name] theme-path
theme (mf/deref (refs/workspace-token-theme theme-group theme-name))
(let [{:keys [theme-info]} state
[theme-id _ _] theme-info
theme (mf/deref (refs/workspace-token-theme theme-id))
has-prev-view (has-prev-view (:prev-type state))
on-save
(mf/use-fn
(mf/deps theme)
(fn [theme']
(st/emit! (dwtl/update-token-theme [(:group theme) (:name theme)] theme'))))]
(st/emit! (dwtl/update-token-theme (ctob/get-id theme) theme'))))]
[:> edit-create-theme*
{:change-view change-view
@@ -413,12 +413,12 @@
state (deref state*)
change-view (mf/use-fn
(fn [type & {:keys [theme-path]}]
(fn [type & {:keys [theme-info]}]
(swap! state* (fn [current-state]
(cond-> current-state
:always (assoc :type type
:prev-type (:type current-state))
:theme-path (assoc :theme-path theme-path))))))
:theme-info (assoc :theme-info theme-info))))))
component (case (:type state)
:empty-themes empty-themes

View File

@@ -26,12 +26,12 @@
[{:keys [themes active-theme-paths on-close grouped?]}]
(when (seq themes)
[:ul {:class (stl/css :theme-options)}
(for [[_ {:keys [group name] :as theme}] themes
(for [[_ {:keys [id name] :as theme}] themes
:let [theme-id (ctob/get-theme-path theme)
selected? (get active-theme-paths theme-id)
select-theme (fn [e]
(dom/stop-propagation e)
(st/emit! (dwtl/toggle-token-theme-active? group name))
(st/emit! (dwtl/toggle-token-theme-active? id))
(on-close))]]
[:li {:key theme-id
:role "option"