From 9a745ea8bcec458ed7ea65e47d04cbbb09589fe0 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Mon, 19 Aug 2024 08:05:19 +0200 Subject: [PATCH] Add active theme toggling --- common/src/app/common/files/changes.cljc | 9 +++++++++ common/src/app/common/files/changes_builder.cljc | 7 +++++++ common/src/app/common/types/file.cljc | 3 +-- common/src/app/common/types/tokens_theme_list.cljc | 4 ++++ frontend/src/app/main/data/tokens.cljs | 10 ++++++++++ .../src/app/main/ui/workspace/tokens/sidebar.cljs | 5 +++-- .../src/app/main/ui/workspace/tokens/token_set.cljs | 13 ++++++++++++- 7 files changed, 46 insertions(+), 5 deletions(-) diff --git a/common/src/app/common/files/changes.cljc b/common/src/app/common/files/changes.cljc index e1023197b..d8c05619a 100644 --- a/common/src/app/common/files/changes.cljc +++ b/common/src/app/common/files/changes.cljc @@ -254,6 +254,11 @@ [: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 ::sm/uuid]]]] + [:delete-temporary-token-theme [:map {:title "DeleteTemporaryTokenThemeChange"} [:type [:= :delete-temporary-token-theme]] @@ -790,6 +795,10 @@ [data {:keys [token-theme]}] (ctotl/add-temporary-token-theme data token-theme)) +(defmethod process-change :update-active-token-themes + [data {:keys [theme-ids]}] + (ctotl/assoc-active-token-themes data theme-ids)) + (defmethod process-change :delete-temporary-token-theme [data {:keys [id]}] (ctotl/delete-temporary-token-theme data id)) diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index 7b7f1fc51..49eb98ade 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -702,6 +702,13 @@ (update :undo-changes conj {:type :delete-temporary-token-theme :id (:id token-theme)}) (apply-changes-local))) +(defn update-active-token-themes + [changes token-active-theme-ids prev-token-active-theme-ids] + (-> changes + (update :redo-changes conj {:type :update-active-token-themes :theme-ids token-active-theme-ids}) + (update :undo-changes conj {:type :update-active-token-themes :theme-ids prev-token-active-theme-ids}) + (apply-changes-local))) + (defn add-token-theme [changes token-theme] (-> changes diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index bd5237af1..593ec5d2d 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -65,8 +65,7 @@ [:map-of {:gen/max 5} :keyword ::ctpg/plugin-data]] [:token-theme-temporary-id {:optional true} ::sm/uuid] - [:token-active-themes {:optional true - :default #{}} + [:token-active-themes {:optional true :default #{}} [:set ::sm/uuid]] [:token-themes {:optional true} [:vector ::sm/uuid]] diff --git a/common/src/app/common/types/tokens_theme_list.cljc b/common/src/app/common/types/tokens_theme_list.cljc index aa26ac207..4cf43d622 100644 --- a/common/src/app/common/types/tokens_theme_list.cljc +++ b/common/src/app/common/types/tokens_theme_list.cljc @@ -14,6 +14,10 @@ [token-set] (assoc token-set :modified-at (dt/now))) +(defn assoc-active-token-themes + [file-data theme-ids] + (assoc file-data :token-active-themes theme-ids)) + (defn add-temporary-token-theme [file-data {:keys [id] :as token-theme}] (-> file-data diff --git a/frontend/src/app/main/data/tokens.cljs b/frontend/src/app/main/data/tokens.cljs index 6c000e4b3..6a2f972bc 100644 --- a/frontend/src/app/main/data/tokens.cljs +++ b/frontend/src/app/main/data/tokens.cljs @@ -118,6 +118,16 @@ theme)) :else changes))) +(defn toggle-token-theme [token-theme-id] + (ptk/reify ::toggle-token-theme + ptk/WatchEvent + (watch [it state _] + (let [themes (wtts/get-active-theme-ids state) + new-themes (wtts/toggle-active-theme-id token-theme-id state) + changes (-> (pcb/empty-changes it) + (pcb/update-active-token-themes new-themes themes))] + (rx/of (dch/commit-changes changes)))))) + (defn create-token-set [token-set] (let [new-token-set (merge {:id (uuid/next) diff --git a/frontend/src/app/main/ui/workspace/tokens/sidebar.cljs b/frontend/src/app/main/ui/workspace/tokens/sidebar.cljs index 7a4a47774..58e2dfda8 100644 --- a/frontend/src/app/main/ui/workspace/tokens/sidebar.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/sidebar.cljs @@ -208,8 +208,9 @@ [:button {:on-click (fn [e] (dom/prevent-default e) - (dom/stop-propagation e))} - (if (some-> active-theme-ids (get id)) "✅" "❎")] + (dom/stop-propagation e) + (st/emit! (wdt/toggle-token-theme id)))} + (if (get active-theme-ids id) "✅" "❎")] [:button {:on-click (fn [e] (dom/prevent-default e) (dom/stop-propagation e) diff --git a/frontend/src/app/main/ui/workspace/tokens/token_set.cljs b/frontend/src/app/main/ui/workspace/tokens/token_set.cljs index 5a9c73764..3c47e0ab5 100644 --- a/frontend/src/app/main/ui/workspace/tokens/token_set.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/token_set.cljs @@ -18,11 +18,22 @@ (group-by :group)))) (defn get-active-theme-ids [state] - (get-in state [:workspace-data :token-active-themes])) + (get-in state [:workspace-data :token-active-themes] #{})) (defn get-temp-theme-id [state] (get-in state [:workspace-data :token-theme-temporary-id])) +(defn toggle-active-theme-id [theme-id state] + (let [temp-theme-id (get-temp-theme-id state) + themes (get-active-theme-ids state) + theme-without-temp (disj themes temp-theme-id) + new-themes (if (get theme-without-temp theme-id) + (disj theme-without-temp theme-id) + (conj theme-without-temp theme-id))] + (if (empty? new-themes) + #{temp-theme-id} + new-themes))) + (defn update-theme-id [state] (let [active-themes (get-active-theme-ids state)