Add token set changes

This commit is contained in:
Florian Schroedl 2024-08-13 11:57:06 +02:00
parent 9ff3a135a8
commit 547358d579
5 changed files with 78 additions and 11 deletions

View file

@ -24,6 +24,7 @@
[app.common.types.shape :as cts] [app.common.types.shape :as cts]
[app.common.types.shape-tree :as ctst] [app.common.types.shape-tree :as ctst]
[app.common.types.token :as cto] [app.common.types.token :as cto]
[app.common.types.token-theme :as ctot]
[app.common.types.tokens-list :as ctol] [app.common.types.tokens-list :as ctol]
[app.common.types.typographies-list :as ctyl] [app.common.types.typographies-list :as ctyl]
[app.common.types.typography :as ctt] [app.common.types.typography :as ctt]
@ -247,6 +248,21 @@
[:type [:= :del-typography]] [:type [:= :del-typography]]
[:id ::sm/uuid]]] [:id ::sm/uuid]]]
[:add-token-set
[:map {:title "AddTokenSetChange"}
[:type [:= :add-token-set]]
[:token-set ::ctot/token-set]]]
[:mod-token-set
[:map {:title "ModTokenSetChange"}
[:type [:= :mod-token-set]]
[:id ::ctot/token-set]]]
[:del-token-set
[:map {:title "DelTokenSetChange"}
[:type [:= :del-token-set]]
[:id ::sm/uuid]]]
[:add-token [:add-token
[:map {:title "AddTokenChange"} [:map {:title "AddTokenChange"}
[:type [:= :add-token]] [:type [:= :add-token]]

View file

@ -695,6 +695,30 @@
(update :undo-changes conj {:type :add-typography :typography prev-typography}) (update :undo-changes conj {:type :add-typography :typography prev-typography})
(apply-changes-local)))) (apply-changes-local))))
(defn add-token-set
[changes token-set]
(-> changes
(update :redo-changes conj {:type :add-token-set :token-set token-set})
(update :undo-changes conj {:type :del-token-set :id (:id token-set)})
(apply-changes-local)))
(defn update-token-set
[changes token-set prev-token-set]
(-> changes
(update :redo-changes conj {:type :mod-token-set :id (:id token-set) :token-set token-set})
(update :undo-changes conj {:type :mod-token-set :id (:id token-set) :token-set (or prev-token-set token-set)})
(apply-changes-local)))
(defn delete-token-set
[changes token-set-id]
(assert-library! changes)
(let [library-data (::library-data (meta changes))
prev-token-set (get-in library-data [:token-set token-set-id])]
(-> changes
(update :redo-changes conj {:type :del-token-set :id token-set-id})
(update :undo-changes conj {:type :add-token-set :token prev-token-set})
(apply-changes-local))))
(defn add-token (defn add-token
[changes token] [changes token]
(-> changes (-> changes

View file

@ -80,18 +80,45 @@
(let [workspace-data (deref refs/workspace-data)] (let [workspace-data (deref refs/workspace-data)]
(get (:tokens workspace-data) id))) (get (:tokens workspace-data) id)))
(defn get-workspace-sets [state]
(get-in state [:workspace-data :token-sets-index]))
(defn get-token-set [set-id state]
(some-> (get-workspace-sets state)
(get set-id)))
(def default-token-set-name "Global")
(defn create-global-set [])
(defn add-token-to-token-set [token token-set]
(update token-set :items conj (:id token)))
(defn update-create-token (defn update-create-token
[token] [token set-id]
(let [token (update token :id #(or % (uuid/next)))] (let [token (update token :id #(or % (uuid/next)))]
(ptk/reify ::add-token (ptk/reify ::add-token
ptk/WatchEvent ptk/WatchEvent
(watch [it _ _] (watch [it state _]
(let [prev-token (get-token-data-from-token-id (:id token)) (let [prev-token (get-token-data-from-token-id (:id token))
changes (if prev-token create-token? (not prev-token)
(-> (pcb/empty-changes it) token-changes (if create-token?
(pcb/update-token token prev-token)) (-> (pcb/empty-changes it)
(-> (pcb/empty-changes it) (pcb/add-token token))
(pcb/add-token token)))] (-> (pcb/empty-changes it)
(pcb/update-token token prev-token)))
token-set (get-token-set state set-id)
create-set? (not token-set)
changes (cond
create-set? (-> token-changes
(pcb/add-token-set {:id (uuid/next)
:name "Global"
:items [(:id token)]}))
:else (let [updated-token-set (if (contains? token-set (:id token))
token-set
(update token-set :items conj (:id token)))]
(-> token-changes
(pcb/update-token-set token-set updated-token-set))))]
(rx/of (dch/commit-changes changes))))))) (rx/of (dch/commit-changes changes)))))))
(defn delete-token (defn delete-token
@ -107,11 +134,11 @@
(rx/of (dch/commit-changes changes)))))) (rx/of (dch/commit-changes changes))))))
(defn duplicate-token (defn duplicate-token
[id] [id set-id]
(let [new-token (-> (get-token-data-from-token-id id) (let [new-token (-> (get-token-data-from-token-id id)
(dissoc :id) (dissoc :id)
(update :name #(str/concat % "-copy")))] (update :name #(str/concat % "-copy")))]
(update-create-token new-token))) (update-create-token new-token set-id)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TEMP (Move to test) ;; TEMP (Move to test)

View file

@ -207,7 +207,7 @@
[{:title "Delete Token" [{:title "Delete Token"
:action #(st/emit! (dt/delete-token (:id token)))} :action #(st/emit! (dt/delete-token (:id token)))}
{:title "Duplicate Token" {:title "Duplicate Token"
:action #(st/emit! (dt/duplicate-token (:id token)))} :action #(st/emit! (dt/duplicate-token (:id token) nil))}
{:title "Edit Token" {:title "Edit Token"
:action (fn [event] :action (fn [event]
(let [{:keys [key fields]} modal (let [{:keys [key fields]} modal

View file

@ -246,7 +246,7 @@ Token names should only contain letters and digits separated by . characters.")}
:value final-value} :value final-value}
final-description (assoc :description final-description) final-description (assoc :description final-description)
(:id token) (assoc :id (:id token)))] (:id token) (assoc :id (:id token)))]
(st/emit! (dt/update-create-token new-token)) (st/emit! (dt/update-create-token new-token nil))
(st/emit! (wtu/update-workspace-tokens)) (st/emit! (wtu/update-workspace-tokens))
(modal/hide!)))))))))] (modal/hide!)))))))))]
[:form [:form