From f0a9444ab602be1c213661c4fa8d5cd77b63f7a1 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Tue, 17 Sep 2024 10:48:23 +0200 Subject: [PATCH] tokens-lib refactor: Get collection of theme groups --- common/src/app/common/types/tokens_lib.cljc | 18 ++++++++++++++++-- .../common_tests/types/tokens_lib_test.cljc | 9 +++++++++ frontend/src/app/main/refs.cljs | 14 ++++++++++---- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index 0a8b8d7d9..476bff524 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -283,11 +283,18 @@ (def check-token-theme! (sm/check-fn ::token-theme)) +(def top-level-theme-group-name + "Top level theme groups have an empty string as the theme group." + "") + +(defn top-level-theme-group? [group] + (= group top-level-theme-group-name)) + (defn make-token-theme [& {:keys [] :as params}] (let [params (-> params (dissoc :id) - (update :group #(or % "")) + (update :group #(or % top-level-theme-group-name)) (update :is-source #(or % false)) (update :modified-at #(or % (dt/now))) (update :sets #(into (d/ordered-set) %))) @@ -308,7 +315,8 @@ (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 [_ group name] "get one theme looking for name") + (get-theme-groups [_] "get a sequence of group names by order")) (def schema:token-themes [:and @@ -431,6 +439,12 @@ (get-theme-tree [_] themes) + (get-theme-groups [_] + (into [] (comp + (map key) + (remove top-level-theme-group?)) + themes)) + (get-themes [_] (->> (tree-seq d/ordered-map? vals themes) (filter (partial instance? TokenTheme)))) diff --git a/common/test/common_tests/types/tokens_lib_test.cljc b/common/test/common_tests/types/tokens_lib_test.cljc index fd342c95c..8efed001b 100644 --- a/common/test/common_tests/types/tokens_lib_test.cljc +++ b/common/test/common_tests/types/tokens_lib_test.cljc @@ -872,6 +872,15 @@ (t/is (= (:description token-theme') "some description")) (t/is (dt/is-after? (:modified-at token-theme') (:modified-at token-theme))))) + (t/deftest get-theme-groups + (let [token-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"))) + token-groups (ctob/get-theme-groups token-lib)] + (t/is (= token-groups ["group1" "group2"])))) + (t/deftest rename-theme-in-groups (let [tokens-lib (-> (ctob/make-tokens-lib) (ctob/add-theme (ctob/make-token-theme :group "" :name "token-theme-1")) diff --git a/frontend/src/app/main/refs.cljs b/frontend/src/app/main/refs.cljs index af5207ffb..3e8994e36 100644 --- a/frontend/src/app/main/refs.cljs +++ b/frontend/src/app/main/refs.cljs @@ -15,7 +15,8 @@ [app.main.data.workspace.state-helpers :as wsh] [app.main.store :as st] [app.main.ui.workspace.tokens.token-set :as wtts] - [okulary.core :as l])) + [okulary.core :as l] + [app.common.types.tokens-lib :as ctob])) ;; ---- Global refs @@ -240,13 +241,18 @@ st/state =)) +;; ---- Tokens + +(def tokens-lib + (l/derived :tokens-lib workspace-data)) + +(def workspace-token-theme-groups + (l/derived #(some-> % ctob/get-theme-groups) tokens-lib)) + (defn workspace-token-theme [id] (l/derived #(wtts/get-workspace-theme id %) st/state)) -(def workspace-token-theme-groups - (l/derived wtts/get-workspace-theme-groups st/state)) - (def workspace-active-theme-ids (l/derived wtts/get-active-theme-ids st/state))