From e8bbb75008a01bf923b4a35c783ba83ef6fafb30 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Mon, 19 Aug 2024 08:49:03 +0200 Subject: [PATCH] Implement group theme switching --- .../main/ui/workspace/tokens/token_set.cljs | 51 ++++++++++++++++--- 1 file changed, 43 insertions(+), 8 deletions(-) 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 3c47e0ab5..bf1b16574 100644 --- a/frontend/src/app/main/ui/workspace/tokens/token_set.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/token_set.cljs @@ -1,4 +1,6 @@ -(ns app.main.ui.workspace.tokens.token-set) +(ns app.main.ui.workspace.tokens.token-set + (:require + [clojure.set :as set])) ;; Themes ---------------------------------------------------------------------- @@ -23,17 +25,50 @@ (defn get-temp-theme-id [state] (get-in state [:workspace-data :token-theme-temporary-id])) +(defn theme-ids-with-group [theme-id state] + (let [themes (get-workspace-themes-index state) + theme-group (get-in themes [theme-id :group]) + same-group-theme-ids (->> themes + (eduction + (map val) + (filter #(= (:group %) theme-group)) + (map :id)) + (into #{}))] + same-group-theme-ids)) + (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))] + (let [temp-theme-id-set (some->> (get-temp-theme-id state) (conj #{})) + active-theme-ids (get-active-theme-ids state) + add? (not (get active-theme-ids theme-id)) + ;; Deactivate themes with the same group when activating a theme + same-group-ids (when add? (theme-ids-with-group theme-id state)) + theme-ids-without-same-group (set/difference active-theme-ids + same-group-ids + temp-theme-id-set) + new-themes (if add? + (conj theme-ids-without-same-group theme-id) + (disj theme-ids-without-same-group theme-id))] (if (empty? new-themes) - #{temp-theme-id} + (or temp-theme-id-set #{}) new-themes))) +(comment + (let [state {:workspace-data {:token-themes-index {1 {:id 1}}}}] + (toggle-active-theme-id 1 state)) + + (let [state {:workspace-data {:token-active-themes #{1} + :token-themes-index {1 {:id 1}}}}] + (toggle-active-theme-id 1 state)) + + (let [state {:workspace-data {:token-active-themes #{2 3 4} + :token-themes-index {1 {:id 1} + 2 {:id 2} + 3 {:id 3} + 4 {:id 4 :group :different}}}}] + (toggle-active-theme-id 1 state)) + nil) + + (defn update-theme-id [state] (let [active-themes (get-active-theme-ids state)