diff --git a/common/src/app/common/files/changes.cljc b/common/src/app/common/files/changes.cljc index a486fc950..0f90faac4 100644 --- a/common/src/app/common/files/changes.cljc +++ b/common/src/app/common/files/changes.cljc @@ -249,6 +249,22 @@ [:type [:= :del-typography]] [:id ::sm/uuid]]] + [:add-token-theme + [:map {:title "AddTokenThemeChange"} + [:type [:= :add-token-theme]] + [:token-theme ::ctot/token-theme]]] + + [:mod-token-theme + [:map {:title "ModTokenThemeChange"} + [:type [:= :mod-token-theme]] + [:id ::sm/uuid] + [:token-theme ::ctot/token-theme]]] + + [:del-token-theme + [:map {:title "DelTokenThemeChange"} + [:type [:= :del-token-theme]] + [:id ::sm/uuid]]] + [:add-token-set [:map {:title "AddTokenSetChange"} [:type [:= :add-token-set]] @@ -760,6 +776,18 @@ [data {:keys [id]}] (ctol/delete-token data id)) +(defmethod process-change :add-token-theme + [data {:keys [token-theme]}] + (ctotl/add-token-theme data token-theme)) + +(defmethod process-change :mod-token-theme + [data {:keys [id token-theme]}] + (ctotl/update-token-theme data id merge token-theme)) + +(defmethod process-change :del-token-theme + [data {:keys [id]}] + (ctotl/delete-token-theme data id)) + (defmethod process-change :add-token-set [data {:keys [token-set]}] (ctotl/add-token-set data token-set)) diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index b4486a6d4..9d1e90a50 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -695,6 +695,30 @@ (update :undo-changes conj {:type :add-typography :typography prev-typography}) (apply-changes-local)))) +(defn add-token-theme + [changes token-theme] + (-> changes + (update :redo-changes conj {:type :add-token-theme :token-theme token-theme}) + (update :undo-changes conj {:type :del-token-theme :id (:id token-theme)}) + (apply-changes-local))) + +(defn update-token-theme + [changes token-theme prev-token-theme] + (-> changes + (update :redo-changes conj {:type :mod-token-theme :id (:id token-theme) :token-theme token-theme}) + (update :undo-changes conj {:type :mod-token-theme :id (:id token-theme) :token-theme (or prev-token-theme token-theme)}) + (apply-changes-local))) + +(defn delete-token-theme + [changes token-theme-id] + (assert-library! changes) + (let [library-data (::library-data (meta changes)) + prev-token-theme (get-in library-data [:token-theme token-theme-id])] + (-> changes + (update :redo-changes conj {:type :del-token-theme :id token-theme-id}) + (update :undo-changes conj {:type :add-token-theme :token-theme prev-token-theme}) + (apply-changes-local)))) + (defn add-token-set [changes token-set] (-> changes diff --git a/common/src/app/common/types/tokens_theme_list.cljc b/common/src/app/common/types/tokens_theme_list.cljc index f2e6058e2..bcd15bf5a 100644 --- a/common/src/app/common/types/tokens_theme_list.cljc +++ b/common/src/app/common/types/tokens_theme_list.cljc @@ -14,6 +14,26 @@ [token-set] (assoc token-set :modified-at (dt/now))) +(defn add-token-theme + [file-data {:keys [index id] :as token-theme}] + (-> file-data + (update :token-themes + (fn [token-themes] + (let [exists? (some (partial = id) token-themes)] + (cond + exists? token-themes + (nil? index) (conj (or token-themes []) id) + :else (d/insert-at-index token-themes index [id]))))) + (update :token-themes-index assoc id token-theme))) + +(defn update-token-theme + [file-data token-theme-id f & args] + (d/update-in-when file-data [:token-themes-index token-theme-id] #(-> (apply f % args) (touch)))) + +(defn delete-token-theme + [file-data token-id] + file-data) + (defn add-token-set [file-data {:keys [index id] :as token-set}] (-> file-data diff --git a/frontend/src/app/main/data/tokens.cljs b/frontend/src/app/main/data/tokens.cljs index fffd64c65..a3608e934 100644 --- a/frontend/src/app/main/data/tokens.cljs +++ b/frontend/src/app/main/data/tokens.cljs @@ -88,6 +88,20 @@ (update [_ state] (wtts/assoc-selected-token-set-id state id)))) +(defn create-token-theme [token-theme] + (let [new-token-theme (merge + {:id (uuid/next) + :sets #{} + :selected :enabled} + token-theme)] + (ptk/reify ::create-token-theme + ptk/WatchEvent + (watch [it _ _] + (let [changes (-> (pcb/empty-changes it) + (pcb/add-token-theme new-token-theme))] + (rx/of + (dch/commit-changes changes))))))) + (defn create-token-set [token-set] (let [new-token-set (merge {:id (uuid/next)