From e729e85c42c2bcf442e1ae9d22547b7bf2d5ca3c Mon Sep 17 00:00:00 2001 From: Eva Marco Date: Fri, 7 Mar 2025 13:17:36 +0100 Subject: [PATCH] :recycle: Create hidden theme on token lib creation (#5991) * :recycle: Create hidden theme on token lib creation * :paperclip: Fix tests * :tada: Add migration * :tada: Remove comment * :recycle: Remove unused changes * :paperclip: Remove unused fn --- common/src/app/common/files/changes.cljc | 23 --------- .../src/app/common/files/changes_builder.cljc | 7 --- common/src/app/common/files/migrations.cljc | 15 +++++- common/src/app/common/logic/tokens.cljc | 17 +++---- common/src/app/common/types/tokens_lib.cljc | 33 ++++++------- .../test/common_tests/logic/token_test.cljc | 9 +--- .../common_tests/types/tokens_lib_test.cljc | 48 ++++++++++--------- frontend/src/app/main/data/tokens.cljs | 11 +++-- 8 files changed, 70 insertions(+), 93 deletions(-) diff --git a/common/src/app/common/files/changes.cljc b/common/src/app/common/files/changes.cljc index 77b609698..da2037063 100644 --- a/common/src/app/common/files/changes.cljc +++ b/common/src/app/common/files/changes.cljc @@ -371,22 +371,11 @@ [:type [:= :del-typography]] [:id ::sm/uuid]]] - [:add-temporary-token-theme - [:map {:title "AddTemporaryTokenThemeChange"} - [:type [:= :add-temporary-token-theme]] - [:token-theme ::ctot/token-theme]]] - [:update-active-token-themes [:map {:title "UpdateActiveTokenThemes"} [:type [:= :update-active-token-themes]] [:theme-ids [:set :string]]]] - [:delete-temporary-token-theme - [:map {:title "DeleteTemporaryTokenThemeChange"} - [:type [:= :delete-temporary-token-theme]] - [:id ::sm/uuid] - [:name :string]]] - [:add-token-theme [:map {:title "AddTokenThemeChange"} [:type [:= :add-token-theme]] @@ -1044,23 +1033,11 @@ (ctob/update-token-in-set lib' set-name token-name (fn [prev-token] (ctob/make-token (merge prev-token token))))))))) -(defmethod process-change :add-temporary-token-theme - [data {:keys [token-theme]}] - (update data :tokens-lib #(-> % - (ctob/ensure-tokens-lib) - (ctob/add-theme (ctob/make-token-theme token-theme))))) - (defmethod process-change :update-active-token-themes [data {:keys [theme-ids]}] (update data :tokens-lib #(-> % (ctob/ensure-tokens-lib) (ctob/set-active-themes theme-ids)))) -(defmethod process-change :delete-temporary-token-theme - [data {:keys [group name]}] - (update data :tokens-lib #(-> % - (ctob/ensure-tokens-lib) - (ctob/delete-theme group name)))) - (defmethod process-change :add-token-theme [data {:keys [token-theme]}] (update data :tokens-lib #(-> % diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index a280cb954..8701288ff 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -767,13 +767,6 @@ (update :undo-changes conj {:type :add-typography :typography prev-typography}) (apply-changes-local)))) -(defn add-temporary-token-theme - [changes token-theme] - (-> changes - (update :redo-changes conj {:type :add-temporary-token-theme :token-theme token-theme}) - (update :undo-changes conj {:type :delete-temporary-token-theme :id (:id token-theme) :name (:name token-theme)}) - (apply-changes-local))) - (defn update-active-token-themes [changes token-active-theme-ids prev-token-active-theme-ids] (-> changes diff --git a/common/src/app/common/files/migrations.cljc b/common/src/app/common/files/migrations.cljc index a0c2a8494..f75d2b905 100644 --- a/common/src/app/common/files/migrations.cljc +++ b/common/src/app/common/files/migrations.cljc @@ -29,6 +29,7 @@ [app.common.types.file :as ctf] [app.common.types.shape :as cts] [app.common.types.shape.shadow :as ctss] + [app.common.types.tokens-lib :as ctob] [app.common.uuid :as uuid] [clojure.set :as set] [cuerdas.core :as str])) @@ -1225,6 +1226,17 @@ (update :pages-index update-vals update-container) (update :components update-vals update-container)))) +(defmethod migrate-data "Add hidden theme" + [data _] + (letfn [(update-tokens-lib [tokens-lib] + (let [hidden-theme (ctob/get-hidden-theme tokens-lib)] + (if (nil? hidden-theme) + (ctob/add-theme tokens-lib (ctob/make-hidden-token-theme)) + tokens-lib)))] + (if (contains? data :tokensLib) + (update data :tokens-lib update-tokens-lib) + data))) + (def available-migrations (into (d/ordered-set) ["legacy-2" @@ -1278,4 +1290,5 @@ "legacy-62" "legacy-65" "legacy-66" - "legacy-67"])) + "legacy-67" + "Add hidden theme"])) diff --git a/common/src/app/common/logic/tokens.cljc b/common/src/app/common/logic/tokens.cljc index 705e352b2..5b2548e2d 100644 --- a/common/src/app/common/logic/tokens.cljc +++ b/common/src/app/common/logic/tokens.cljc @@ -4,24 +4,21 @@ [app.common.types.tokens-lib :as ctob])) (defn generate-update-active-sets - "Copy the active sets from the currently active themes and move them to the hidden token theme and update the theme with `update-hidden-theme-fn`. - + "Copy the active sets from the currently active themes and move them to the hidden token theme and update the theme with `update-theme-fn`. 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-hidden-theme-fn] + [changes tokens-lib update-theme-fn] (let [prev-active-token-themes (ctob/get-active-theme-paths tokens-lib) active-token-set-names (ctob/get-active-themes-set-names tokens-lib) - prev-hidden-token-theme (ctob/get-hidden-theme tokens-lib) - hidden-token-theme (-> (or (some-> prev-hidden-token-theme (ctob/set-sets active-token-set-names)) - (ctob/make-hidden-token-theme :sets active-token-set-names)) - (update-hidden-theme-fn)) + prev-hidden-token-theme (ctob/get-hidden-theme tokens-lib) + + hidden-token-theme (-> (some-> prev-hidden-token-theme (ctob/set-sets active-token-set-names)) + (update-theme-fn)) changes (-> changes (pcb/update-active-token-themes #{ctob/hidden-token-theme-path} prev-active-token-themes)) - changes (if prev-hidden-token-theme - (pcb/update-token-theme changes hidden-token-theme prev-hidden-token-theme) - (pcb/add-token-theme changes hidden-token-theme))] + changes (pcb/update-token-theme changes hidden-token-theme prev-hidden-token-theme)] changes)) (defn generate-toggle-token-set diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index 71e7f66c1..cde6d9fa6 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -582,7 +582,6 @@ (defn make-token-theme [& {:as attrs}] (-> attrs - (dissoc :id) (update :group d/nilv top-level-theme-group-name) (update :is-source d/nilv false) (update :modified-at #(or % (dt/now))) @@ -736,7 +735,6 @@ (cons item (mapcat #(walk % (assoc opts :parent path :depth (inc depth))) v'))))))))))] (walk (or nodes (d/ordered-map)) nil))) - (defn sets-tree-seq "Get tokens sets tree as a flat list @@ -1250,28 +1248,24 @@ Will return a value that matches this schema: (update "sets" keys))))) active-sets (get metadata "activeSets") active-themes (get metadata "activeThemes") - themes-data (if (and (seq active-sets) - (empty? active-themes)) - (conj themes-data {"name" hidden-token-theme-name - "group" "" - "description" nil - "is-source" true - "modified-at" (dt/now) - "sets" active-sets}) - themes-data) active-themes (if (empty? active-themes) #{hidden-token-theme-path} active-themes) + set-order (get metadata "tokenSetOrder") name->pos (into {} (map-indexed (fn [idx itm] [itm idx]) set-order)) sets-data' (sort-by (comp name->pos first) sets-data) lib (make-tokens-lib) - lib' (reduce - (fn [lib [set-name tokens]] - (add-set lib (make-token-set - :name set-name - :tokens (flatten-nested-tokens-json tokens "")))) - lib sets-data')] + lib' (->> sets-data' + (reduce (fn [lib [set-name tokens]] + (add-set lib (make-token-set + :name set-name + :tokens (flatten-nested-tokens-json tokens "")))) + lib)) + lib' (cond-> lib' + (and (seq active-sets) (empty? active-themes)) + (update-theme hidden-token-theme-group hidden-token-theme-name + #(assoc % :sets active-sets)))] (if-let [themes-data (seq themes-data)] (as-> lib' $ (reduce @@ -1352,7 +1346,10 @@ Will return a value that matches this schema: :active-themes #{})) ([& {:keys [sets themes active-themes]}] - (let [active-themes (d/nilv active-themes #{})] + (let [active-themes (d/nilv active-themes #{}) + themes (if (empty? themes) + (update themes hidden-token-theme-group d/oassoc hidden-token-theme-name (make-hidden-token-theme)) + themes)] (TokensLib. (check-token-sets sets) (check-token-themes themes) diff --git a/common/test/common_tests/logic/token_test.cljc b/common/test/common_tests/logic/token_test.cljc index aa84adf82..25d508a65 100644 --- a/common/test/common_tests/logic/token_test.cljc +++ b/common/test/common_tests/logic/token_test.cljc @@ -32,7 +32,6 @@ (t/is (= #{} (:sets (ctob/get-hidden-theme redo-lib)))) ;; Undo - (t/is (nil? (ctob/get-hidden-theme undo-lib))) (t/is (= #{"/theme"} (ctob/get-active-theme-paths undo-lib))))) (t/testing "toggling an inactive set will switch to hidden theme without user sets" @@ -51,7 +50,6 @@ (t/is (= #{} (:sets (ctob/get-hidden-theme redo-lib)))) ;; Undo - (t/is (nil? (ctob/get-hidden-theme undo-lib))) (t/is (= #{"/theme"} (ctob/get-active-theme-paths undo-lib))))) (t/testing "toggling an set with hidden theme already active will toggle set in hidden theme" @@ -68,10 +66,7 @@ undo-lib (tht/get-tokens-lib undo)] (t/is (= (ctob/get-active-theme-paths redo-lib) (ctob/get-active-theme-paths undo-lib))) - (t/is (= #{"foo/bar"} (:sets (ctob/get-hidden-theme redo-lib)))) - - ;; Undo - (t/is (some? (ctob/get-hidden-theme undo-lib)))))) + (t/is (= #{"foo/bar"} (:sets (ctob/get-hidden-theme redo-lib))))))) (t/deftest set-token-test (t/testing "delete token" @@ -155,7 +150,6 @@ (t/is (= #{"foo/bar/baz" "foo/bar/baz/baz-child"} (:sets (ctob/get-hidden-theme redo-lib)))) ;; Undo - (t/is (nil? (ctob/get-hidden-theme undo-lib))) (t/is (= #{"/theme"} (ctob/get-active-theme-paths undo-lib))))) (t/testing "toggling set group with partially active sets inside will deactivate all child sets" @@ -177,7 +171,6 @@ (t/is (= #{ctob/hidden-token-theme-path} (ctob/get-active-theme-paths redo-lib))) ;; Undo - (t/is (nil? (ctob/get-hidden-theme undo-lib))) (t/is (= #{"/theme"} (ctob/get-active-theme-paths undo-lib)))))) (t/deftest generate-move-token-set-test diff --git a/common/test/common_tests/types/tokens_lib_test.cljc b/common/test/common_tests/types/tokens_lib_test.cljc index e9fbf032c..44f76ac6c 100644 --- a/common/test/common_tests/types/tokens_lib_test.cljc +++ b/common/test/common_tests/types/tokens_lib_test.cljc @@ -76,7 +76,6 @@ (t/is (= (:description token-set1) "")) (t/is (some? (:modified-at token-set1))) (t/is (empty? (:tokens token-set1))) - (t/is (= (:name token-set2) "test-token-set-2")) (t/is (= (:description token-set2) "test description")) (t/is (= (:modified-at token-set2) now)) @@ -503,8 +502,8 @@ token-themes' (ctob/get-themes tokens-lib') token-theme' (ctob/get-theme tokens-lib' "" "test-token-theme")] - (t/is (= (ctob/theme-count tokens-lib') 1)) - (t/is (= (first token-themes') token-theme)) + (t/is (= (ctob/theme-count tokens-lib') 2)) + (t/is (= (second token-themes') token-theme)) (t/is (= token-theme' token-theme)))) (t/testing "update-token-theme" @@ -524,7 +523,7 @@ token-theme (ctob/get-theme tokens-lib "" "test-token-theme") token-theme' (ctob/get-theme tokens-lib' "" "test-token-theme")] - (t/is (= (ctob/theme-count tokens-lib') 1)) + (t/is (= (ctob/theme-count tokens-lib') 2)) (t/is (= (:name token-theme') "test-token-theme")) (t/is (= (:description token-theme') "some description")) (t/is (dt/is-after? (:modified-at token-theme') (:modified-at token-theme))))) @@ -542,7 +541,7 @@ token-theme (ctob/get-theme tokens-lib "" "test-token-theme") token-theme' (ctob/get-theme tokens-lib' "" "updated-name")] - (t/is (= (ctob/theme-count tokens-lib') 1)) + (t/is (= (ctob/theme-count tokens-lib') 2)) (t/is (= (:name token-theme') "updated-name")) (t/is (dt/is-after? (:modified-at token-theme') (:modified-at token-theme))))) @@ -556,7 +555,7 @@ token-theme' (ctob/get-theme tokens-lib' "" "updated-name")] - (t/is (= (ctob/theme-count tokens-lib') 0)) + (t/is (= (ctob/theme-count tokens-lib') 1)) (t/is (nil? token-theme')))) (t/testing "toggle-set-in-theme" @@ -590,7 +589,7 @@ (t/is (ctob/valid-tokens-lib? tokens-lib')) (t/is (= (ctob/set-count tokens-lib') 1)) - (t/is (= (ctob/theme-count tokens-lib') 1)))) + (t/is (= (ctob/theme-count tokens-lib') 2)))) #?(:clj (t/testing "fressian-serialization" @@ -606,7 +605,7 @@ (t/is (ctob/valid-tokens-lib? tokens-lib')) (t/is (= (ctob/set-count tokens-lib') 1)) - (t/is (= (ctob/theme-count tokens-lib') 1)))))) + (t/is (= (ctob/theme-count tokens-lib') 2)))))) (t/deftest grouping (t/testing "split-and-join" @@ -990,7 +989,7 @@ [node-group0 node-group1 node-group2] (ctob/get-children themes-tree) - [node-theme1] + [hidden-theme node-theme1] (ctob/get-children (second node-group0)) [node-theme2 node-theme3] @@ -999,19 +998,24 @@ [node-theme4] (ctob/get-children (second node-group2))] - (t/is (= (count themes-list) 4)) - (t/is (= (:name (nth themes-list 0)) "token-theme-1")) - (t/is (= (:name (nth themes-list 1)) "token-theme-2")) - (t/is (= (:name (nth themes-list 2)) "token-theme-3")) - (t/is (= (:name (nth themes-list 3)) "token-theme-4")) - (t/is (= (:group (nth themes-list 0)) "")) - (t/is (= (:group (nth themes-list 1)) "group1")) + (t/is (= (count themes-list) 5)) + (t/is (= (:name (nth themes-list 0)) "__PENPOT__HIDDEN__TOKEN__THEME__")) + (t/is (= (:name (nth themes-list 1)) "token-theme-1")) + (t/is (= (:name (nth themes-list 2)) "token-theme-2")) + (t/is (= (:name (nth themes-list 3)) "token-theme-3")) + (t/is (= (:name (nth themes-list 4)) "token-theme-4")) + (t/is (= (:group (nth themes-list 1)) "")) (t/is (= (:group (nth themes-list 2)) "group1")) - (t/is (= (:group (nth themes-list 3)) "group2")) + (t/is (= (:group (nth themes-list 3)) "group1")) + (t/is (= (:group (nth themes-list 4)) "group2")) (t/is (= (first node-group0) "")) (t/is (= (ctob/group? (second node-group0)) true)) - (t/is (= (count (second node-group0)) 1)) + (t/is (= (count (second node-group0)) 2)) + + (t/is (= (first hidden-theme) "__PENPOT__HIDDEN__TOKEN__THEME__")) + (t/is (= (ctob/group? (second hidden-theme)) false)) + (t/is (= (:name (second hidden-theme)) "__PENPOT__HIDDEN__TOKEN__THEME__")) (t/is (= (first node-theme1) "token-theme-1")) (t/is (= (ctob/group? (second node-theme1)) false)) @@ -1051,7 +1055,7 @@ token-theme (get-in themes-tree ["group1" "token-theme-2"]) token-theme' (get-in themes-tree' ["group1" "token-theme-2"])] - (t/is (= (ctob/theme-count tokens-lib') 4)) + (t/is (= (ctob/theme-count tokens-lib') 5)) (t/is (= (count group1') 2)) (t/is (= (d/index-of (keys group1') "token-theme-2") 0)) (t/is (= (:name token-theme') "token-theme-2")) @@ -1088,7 +1092,7 @@ token-theme (get-in themes-tree ["group1" "token-theme-2"]) token-theme' (get-in themes-tree' ["group1" "updated-name"])] - (t/is (= (ctob/theme-count tokens-lib') 4)) + (t/is (= (ctob/theme-count tokens-lib') 5)) (t/is (= (count group1') 2)) (t/is (= (d/index-of (keys group1') "updated-name") 0)) (t/is (= (:name token-theme') "updated-name")) @@ -1117,7 +1121,7 @@ token-theme (get-in themes-tree ["group1" "token-theme-2"]) token-theme' (get-in themes-tree' ["group2" "updated-name"])] - (t/is (= (ctob/theme-count tokens-lib') 3)) + (t/is (= (ctob/theme-count tokens-lib') 4)) (t/is (= (count group1') 1)) (t/is (= (count group2') 1)) (t/is (= (d/index-of (keys group2') "updated-name") 0)) @@ -1137,7 +1141,7 @@ themes-tree' (ctob/get-theme-tree tokens-lib') token-theme' (get-in themes-tree' ["group1" "token-theme-2"])] - (t/is (= (ctob/theme-count tokens-lib') 1)) + (t/is (= (ctob/theme-count tokens-lib') 2)) (t/is (= (count themes-tree') 1)) (t/is (nil? token-theme')))))) diff --git a/frontend/src/app/main/data/tokens.cljs b/frontend/src/app/main/data/tokens.cljs index 9d78af382..41feb80a1 100644 --- a/frontend/src/app/main/data/tokens.cljs +++ b/frontend/src/app/main/data/tokens.cljs @@ -253,18 +253,21 @@ (ctob/add-token token)) hidden-theme - (ctob/make-hidden-token-theme :sets [set-name]) + (ctob/make-hidden-token-theme) + + hidden-theme-with-set + (ctob/enable-set hidden-theme set-name) changes (pcb/add-token-set (pcb/empty-changes) token-set) changes (-> changes - (pcb/add-token-theme hidden-theme) + (pcb/update-token-theme hidden-theme-with-set hidden-theme) (pcb/update-active-token-themes #{ctob/hidden-token-theme-path} #{}))] - (rx/of (set-selected-token-set-name set-name) - (dch/commit-changes changes)))))) + (rx/of (dch/commit-changes changes) + (set-selected-token-set-name set-name)))))) (defn create-token [params]