diff --git a/common/src/app/common/files/changes.cljc b/common/src/app/common/files/changes.cljc index 9bc022f722..2db67597bd 100644 --- a/common/src/app/common/files/changes.cljc +++ b/common/src/app/common/files/changes.cljc @@ -28,6 +28,7 @@ [app.common.types.shape :as cts] [app.common.types.shape-tree :as ctst] [app.common.types.token :as cto] + [app.common.types.token-status :as ctos] [app.common.types.tokens-lib :as ctob] [app.common.types.typographies-list :as ctyl] [app.common.types.typography :as ctt] @@ -397,6 +398,13 @@ [:id ::sm/uuid] [:attrs [:maybe ctob/schema:token-theme-attrs]]]] + [:set-token-theme-status + [:map {:title "SetTokenThemeStatus"} + [:type [:= :set-token-theme-status]] + [:id ::sm/uuid] + [:status :boolean]]] + + ;; TODO deprecate this once everyone uses set-token-theme-status [:set-active-token-themes [:map {:title "SetActiveTokenThemes"} [:type [:= :set-active-token-themes]] @@ -1041,6 +1049,11 @@ (fn [prev-token-theme] (ctob/make-token-theme (merge prev-token-theme attrs))))))))) +(defmethod process-change :set-token-theme-status + [data {:keys [id status]}] + (-> (ctf/ensure-tokens-lib data) + (ctf/update-token-status ctos/set-theme-status id status))) + (defmethod process-change :set-active-token-themes [data {:keys [theme-paths]}] (-> (ctf/ensure-tokens-lib data) diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index c7e8d71288..af07b461ba 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -21,6 +21,7 @@ [app.common.types.path :as path] [app.common.types.shape :as cts] [app.common.types.shape.layout :as ctl] + [app.common.types.token-status :as ctos] [app.common.types.tokens-lib :as ctob] [app.common.uuid :as uuid] [clojure.datafy :refer [datafy]])) @@ -1020,6 +1021,17 @@ :attrs (datafy prev-theme)}) (apply-changes-local)))) +(defn set-token-theme-status + [changes id status] + (assert-library! changes) + (let [library-data (::library-data (meta changes)) + token-status (ctf/get-token-status library-data) + prev-status (ctos/theme-active? token-status id)] + (-> changes + (update :redo-changes conj {:type :set-token-theme-status :id id :status status}) + (update :undo-changes conj {:type :set-token-theme-status :id id :status prev-status}) + (apply-changes-local)))) + (defn set-active-token-themes [changes active-theme-paths] (assert-library! changes) diff --git a/common/src/app/common/logic/tokens.cljc b/common/src/app/common/logic/tokens.cljc index ffec485d54..aa419c2af1 100644 --- a/common/src/app/common/logic/tokens.cljc +++ b/common/src/app/common/logic/tokens.cljc @@ -7,6 +7,7 @@ (ns app.common.logic.tokens (:require [app.common.files.changes-builder :as pcb] + [app.common.types.token-status :as ctos] [app.common.types.tokens-lib :as ctob])) (defn- generate-update-active-sets @@ -40,32 +41,27 @@ [changes tokens-lib set-name] (generate-update-active-sets changes tokens-lib #(ctob/toggle-set % set-name))) -(defn- generate-update-active-token-theme - "Change the active state of a theme in `tokens-lib`. If after the change there is - any active theme other than the hidden one, deactivate the hidden theme." - [changes tokens-lib update-fn] - (let [active-token-themes (some-> tokens-lib - (update-fn) - (ctob/get-active-theme-paths)) - active-token-themes' (if (= active-token-themes #{ctob/hidden-theme-path}) - active-token-themes - (disj active-token-themes ctob/hidden-theme-path))] - (pcb/set-active-token-themes changes active-token-themes'))) +;; (defn- generate-update-theme-status +;; "Change the active status of a theme in `token-status`." +;; [changes token-status update-fn] +;; (let [active-token-themes (some-> tokens-lib +;; (update-fn) +;; (ctob/get-active-theme-paths)) +;; active-token-themes' (if (= active-token-themes #{ctob/hidden-theme-path}) +;; active-token-themes +;; (disj active-token-themes ctob/hidden-theme-path))] +;; (pcb/set-active-token-themes changes active-token-themes'))) -(defn generate-set-active-token-theme - "Activate or deactivate a token theme in `tokens-lib`." - [changes tokens-lib id active?] - (if active? - (generate-update-active-token-theme changes tokens-lib - #(ctob/activate-theme % id)) - (generate-update-active-token-theme changes tokens-lib - #(ctob/deactivate-theme % id)))) +(defn generate-set-token-theme-status + "Activate or deactivate a token theme in `token-status`." + [changes _ id active?] + (pcb/set-token-theme-status changes id (not active?))) (defn generate-toggle-token-theme - "Toggle the active state of a token theme in `tokens-lib`." - [changes tokens-lib id] - (generate-update-active-token-theme changes tokens-lib - #(ctob/toggle-theme-active % id))) + "Toggle the active status of a token theme in `token-status`." + [changes token-status id] + (let [active? (ctos/theme-active? token-status id)] + (pcb/set-token-theme-status changes id (not active?)))) (defn toggle-token-set-group "Toggle a token set group at `group-path` in `tokens-lib` for a `tokens-lib-theme`." diff --git a/common/src/app/common/test_helpers/tokens.cljc b/common/src/app/common/test_helpers/tokens.cljc index edd8220907..ddaf97a1cf 100644 --- a/common/src/app/common/test_helpers/tokens.cljc +++ b/common/src/app/common/test_helpers/tokens.cljc @@ -17,7 +17,11 @@ (defn get-tokens-lib [file] - (:tokens-lib (ctf/file-data file))) + (-> file (ctf/file-data) (ctf/get-tokens-lib))) + +(defn get-token-status + [file] + (-> file (ctf/file-data) (ctf/get-token-status))) (defn add-tokens-lib "Ensure the file has a tokens-lib and a token-statusin its data, creating empty ones if not" @@ -26,11 +30,17 @@ (defn update-tokens-lib [file f] - (ctf/update-file-data file #(update % :tokens-lib f))) + (ctf/update-file-data file #(ctf/update-tokens-lib % f))) (defn update-token-status [file f] - (ctf/update-file-data file #(update % :token-status f))) + (ctf/update-file-data file #(ctf/update-token-status % f))) + +(defn sample-file-with-tokens [tokens-lib-fn token-status-fn] + (-> (thf/sample-file :file1) + (add-tokens-lib) + (update-tokens-lib tokens-lib-fn) + (update-token-status token-status-fn))) (defn get-token [file set-id token-id] diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 79c50e7d3d..777e23e5ea 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -28,8 +28,8 @@ [app.common.types.shape :as cts] [app.common.types.shape-tree :as ctst] [app.common.types.text :as txt] - [app.common.types.tokens-lib :as ctob] [app.common.types.token-status :as ctos] + [app.common.types.tokens-lib :as ctob] [app.common.types.typographies-list :as ctyl] [app.common.types.typography :as cty] [app.common.uuid :as uuid] @@ -199,7 +199,7 @@ (check-file file))) (defn ensure-tokens-lib - "Ensure file-data has a :tokens-lib key, creating one if necessary." + "Ensure file-data has a :tokens-lib and a :token-status keys, creating them if necessary." [file-data] (if (and (some? (:tokens-lib file-data)) (nil? (:token-status file-data))) ;; TODO: remove this when we deprecate old-style files without token-status @@ -317,6 +317,26 @@ (fn [container] (update-objects-tree container f))))) +;; Tokens helpers +(defn get-tokens-lib + [file-data] + (:tokens-lib file-data)) + +(defn get-token-status + [file-data] + (if (and (some? (:tokens-lib file-data)) (nil? (:token-status file-data))) + ;; TODO: remove this when we deprecate old-style files without token-status + (ctos/make-token-status-from-lib (:tokens-lib file-data)) + (:token-status file-data))) + +(defn update-tokens-lib + [file-data f & args] + (d/update-when file-data :tokens-lib #(apply f % args))) + +(defn update-token-status + [file-data f & args] + (d/update-when file-data :token-status #(apply f % (get-tokens-lib file-data) args))) + ;; Asset helpers (defn find-component-file [file libraries component-file] diff --git a/common/src/app/common/types/token_status.cljc b/common/src/app/common/types/token_status.cljc index f3cb9dc8c2..0bc77edd80 100644 --- a/common/src/app/common/types/token_status.cljc +++ b/common/src/app/common/types/token_status.cljc @@ -20,9 +20,9 @@ ;; in a tokens library. (defprotocol ITokenStatus - (activate-theme [_ theme-id] "Add a theme uuid to active themes") - (deactivate-theme [_ theme-id] "Remove a theme uuid from active themes") - (toggle-theme-active [_ theme-id] "Toggle a theme uuid in active themes") + (activate-theme [_ tokens-lib theme-id] "Add a theme uuid to active themes") + (deactivate-theme [_ tokens-lib theme-id] "Remove a theme uuid from active themes") + (set-theme-status [_ tokens-lib theme-id status] "Add or remove a theme uuid to active themes") (theme-active? [_ theme-id] "Check if a theme uuid is active") (active-theme-count [_] "Return the number of active themes") (activate-set [_ set-id] "Add a set uuid to active sets") @@ -43,16 +43,20 @@ (c.json/-write (datafy this) writter options))]) ITokenStatus - (activate-theme [_ theme-id] - (TokenStatus. (conj active-themes theme-id) active-sets)) + (activate-theme [this tokens-lib theme-id] + (if (ctob/get-theme tokens-lib theme-id) + (TokenStatus. (conj active-themes theme-id) active-sets) + this)) - (deactivate-theme [_ theme-id] - (TokenStatus. (disj active-themes theme-id) active-sets)) + (deactivate-theme [this tokens-lib theme-id] + (if (ctob/get-theme tokens-lib theme-id) + (TokenStatus. (disj active-themes theme-id) active-sets) + this)) - (toggle-theme-active [this theme-id] - (if (contains? active-themes theme-id) - (deactivate-theme this theme-id) - (activate-theme this theme-id))) + (set-theme-status [this tokens-lib theme-id status] + (if status + (activate-theme this tokens-lib theme-id) + (deactivate-theme this tokens-lib theme-id))) (theme-active? [_ theme-id] (contains? active-themes theme-id)) @@ -74,7 +78,7 @@ (set-active? [_ set-id] (contains? active-sets set-id)) - + (active-set-count [_] (count active-sets))) diff --git a/frontend/src/app/main/data/helpers.cljs b/frontend/src/app/main/data/helpers.cljs index e3f4c4d538..dd74cf292e 100644 --- a/frontend/src/app/main/data/helpers.cljs +++ b/frontend/src/app/main/data/helpers.cljs @@ -12,6 +12,7 @@ [app.common.geom.matrix :as gmt] [app.common.geom.point :as gpt] [app.common.geom.shapes :as gsh] + [app.common.types.file :as ctf] [app.common.types.path :as path])) (defn lookup-profile @@ -42,12 +43,12 @@ (let [current-file-data (lookup-file-data state) tokens-file-id (or (:tokens-file current-file-data) (:id current-file-data)) tokens-file-data (lookup-file-data state tokens-file-id)] - (:tokens-lib tokens-file-data))) + (ctf/get-tokens-lib tokens-file-data))) (defn lookup-token-status [state] (let [current-file-data (lookup-file-data state)] - (:token-status current-file-data))) + (ctf/get-token-status current-file-data))) (defn get-page [fdata page-id] diff --git a/frontend/src/app/main/data/workspace/tokens/library_edit.cljs b/frontend/src/app/main/data/workspace/tokens/library_edit.cljs index 238e5e3ee6..d6f3170d7f 100644 --- a/frontend/src/app/main/data/workspace/tokens/library_edit.cljs +++ b/frontend/src/app/main/data/workspace/tokens/library_edit.cljs @@ -267,11 +267,11 @@ (ptk/reify ::set-token-theme-active ptk/WatchEvent (watch [_ state _] - (let [data (dsh/lookup-file-data state) - tokens-lib (get-tokens-lib state) - changes (-> (pcb/empty-changes) - (pcb/with-library-data data) - (clt/generate-set-active-token-theme tokens-lib id active?))] + (let [data (dsh/lookup-file-data state) + token-status (dsh/lookup-token-status state) + changes (-> (pcb/empty-changes) + (pcb/with-library-data data) + (clt/generate-set-token-theme-status token-status id active?))] (rx/of (dch/commit-changes changes) (dwtp/propagate-workspace-tokens)))))) @@ -282,10 +282,10 @@ ptk/WatchEvent (watch [it state _] (let [data (dsh/lookup-file-data state) - tokens-lib (get-tokens-lib state) + token-status (dsh/lookup-token-status state) changes (-> (pcb/empty-changes it) (pcb/with-library-data data) - (clt/generate-toggle-token-theme tokens-lib id))] + (clt/generate-toggle-token-theme token-status id))] (rx/of (dch/commit-changes changes) (dwtp/propagate-workspace-tokens))))))