0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 06:02:32 -05:00

Fix token create

This commit is contained in:
Florian Schroedl 2024-09-27 15:36:07 +02:00
parent b7cedf219b
commit cce4014fbe
5 changed files with 29 additions and 49 deletions

View file

@ -303,16 +303,13 @@
[:add-token
[:map {:title "AddTokenChange"}
[:type [:= :add-token]]
[:set-id ::sm/uuid]
[:set-name :string]
[:token ::cto/token]]]
[:mod-token
[:map {:title "ModTokenChange"}
[:type [:= :mod-token]]
[:set-id ::sm/uuid]
[:set-name :string]
[:id ::sm/uuid]
[:name :string]
[:token ::cto/token]]]
@ -320,7 +317,6 @@
[:map {:title "DelTokenChange"}
[:type [:= :del-token]]
[:set-name :string]
[:id ::sm/uuid]
[:name :string]]]]])
(sm/register! ::changes

View file

@ -765,17 +765,17 @@
(apply-changes-local))))
(defn add-token
[changes set-id set-name token]
[changes set-name token]
(-> changes
(update :redo-changes conj {:type :add-token :set-id set-id :set-name set-name :token token})
(update :undo-changes conj {:type :del-token :set-name set-name :id (:id token) :name (:name token)})
(update :redo-changes conj {:type :add-token :set-name set-name :token token})
(update :undo-changes conj {:type :del-token :set-name set-name :name (:name token)})
(apply-changes-local)))
(defn update-token
[changes set-id set-name {:keys [id name] :as token} {prev-name :name :as prev-token}]
[changes set-name token prev-token]
(-> changes
(update :redo-changes conj {:type :mod-token :set-id set-id :set-name set-name :id id :name prev-name :token token})
(update :undo-changes conj {:type :mod-token :set-id set-id :set-name set-name :id id :name name :token (or prev-token token)})
(update :redo-changes conj {:type :mod-token :set-name set-name :name (:name prev-token) :token token})
(update :undo-changes conj {:type :mod-token :set-name set-name :name (:name token) :token (or prev-token token)})
(apply-changes-local)))
(defn delete-token
@ -784,8 +784,8 @@
(let [library-data (::library-data (meta changes))
prev-token (get-in library-data [:tokens token-id])]
(-> changes
(update :redo-changes conj {:type :del-token :set-name set-name :id token-id :name token-name})
(update :undo-changes conj {:type :add-token :set-id uuid/zero :set-name set-name :token prev-token})
(update :redo-changes conj {:type :del-token :set-name set-name :name token-name})
(update :undo-changes conj {:type :add-token :set-name set-name :token prev-token})
(apply-changes-local))))
(defn add-component

View file

@ -60,7 +60,6 @@
(sm/register! ::token
[:map {:title "Token"}
[:id ::sm/uuid]
[:name token-name-ref]
[:type [::sm/one-of token-types]]
[:value :any]

View file

@ -138,6 +138,7 @@
(add-token [_ token] "add a token at the end of the list")
(update-token [_ token-name f] "update a token in the list")
(delete-token [_ token-name] "delete a token from the list")
(get-token [_ token-name] "return an ordered sequence of all tokens in the set")
(get-tokens [_] "return an ordered sequence of all tokens in the set"))
(defrecord TokenSet [name description modified-at tokens]
@ -170,6 +171,9 @@
(dt/now)
(dissoc tokens token-name)))
(get-token [_ token-name]
(get tokens token-name))
(get-tokens [_]
(vals tokens)))

View file

@ -203,14 +203,6 @@
theme)
(pcb/update-active-token-themes #{target-theme-id} (wtts/get-active-theme-ids state)))]
(comment
(-> (ctob/make-token-theme
:group ""
:name "bar")
(ctob/toggle-set "foo"))
nil)
(defn toggle-token-set [{:keys [token-set-name]}]
(ptk/reify ::toggle-token-set
ptk/WatchEvent
@ -249,34 +241,23 @@
(defn update-create-token
[token]
(let [token (update token :id #(or % (uuid/next)))]
(ptk/reify ::update-create-token
ptk/WatchEvent
(watch [it state _]
(let [token-set (wtts/get-selected-token-set state)
create-set? (not token-set)
token-set (or token-set
{:id (uuid/next)
:name "Global"
:tokens []})
changes (cond-> (pcb/empty-changes it)
create-set?
(pcb/add-token-set token-set))
prev-token-id (d/seek #(= % (:id token)) (:tokens token-set))
prev-token (get-token-data-from-token-id prev-token-id)
create-token? (not prev-token)
changes (if create-token?
(pcb/add-token changes (:id token-set) (:name token-set) token)
(pcb/update-token changes (:id token-set) (:name token-set) token prev-token))
changes (-> changes
(ensure-token-theme-changes state {:new-set? create-set?
:id (:id token-set)}))]
(rx/of
(set-selected-token-set-id (:name token-set))
(dch/commit-changes changes)))))))
(ptk/reify ::update-create-token
ptk/WatchEvent
(watch [_ state _]
(let [tlib (get-tokens-lib state)
token-set (wtts/get-selected-token-set state)
changes (if (not token-set)
;; No set created add a global set
(->> (ctob/make-token-set tlib :name "Global")
(ctob/add-token token)
(pcb/add-token-set (pcb/empty-changes)))
;; Either update or add token to existing set
(if-let [prev-token (ctob/get-token token-set (:name token))]
(pcb/update-token (pcb/empty-changes) (:name token-set) token prev-token)
(pcb/add-token (pcb/empty-changes) (:name token-set) token)))]
(rx/of
(set-selected-token-set-id (:name token-set))
(dch/commit-changes changes))))))
(defn delete-token
[set-name id name]