0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-04-03 10:31:38 -05:00

🐛 Prevent token-set overwrite on creation and edition

This commit is contained in:
Andrey Antukh 2025-03-14 15:10:52 +01:00
parent 1965490bee
commit 096b685e2c
6 changed files with 1397 additions and 1369 deletions
common
src/app/common
test/common_tests/types
frontend/src/app/main
data
ui/workspace/tokens

View file

@ -387,7 +387,6 @@
[:set-group-path [:vector :string]]
[:set-group-fname :string]]]
[:move-token-set-before
[:map {:title "MoveTokenSetBefore"}
[:type [:= :move-token-set-before]]

View file

@ -875,9 +875,28 @@
:token nil}))
(apply-changes-local))))
(defn set-token-set [changes set-name group? token-set]
(defn rename-token-set
[changes name new-name]
(assert-library! changes)
(let [library-data (::library-data (meta changes))
(let [library-data (::library-data (meta changes))
prev-token-set (some-> (get library-data :tokens-lib)
(ctob/get-set name))]
(-> changes
(update :redo-changes conj {:type :set-token-set
:set-name name
:token-set (assoc prev-token-set :name new-name)
:group? false})
(update :undo-changes conj {:type :set-token-set
:set-name new-name
:token-set prev-token-set
:group? false})
(apply-changes-local))))
(defn set-token-set
[changes set-name group? token-set]
(assert-library! changes)
(let [library-data (::library-data (meta changes))
prev-token-set (some-> (get library-data :tokens-lib)
(ctob/get-set set-name))]
(-> changes

View file

@ -27,14 +27,17 @@
(def valid-groupable-item?
(sm/validator schema:groupable-item))
(def xf-map-trim
(comp
(map str/trim)
(remove str/empty?)))
(defn split-path
"Decompose a string in the form 'one.two.three' into a vector of strings, removing spaces."
[path separator]
(let [xf (comp (map str/trim)
(remove str/empty?))]
(->> (str/split path separator)
(into [] xf))))
(->> (str/split path separator)
(into [] xf-map-trim)
(not-empty)))
(defn split-path-name [s separator]
(let [[path name] (str/split s (re-pattern (str "\\" separator)) 2)]
@ -61,10 +64,12 @@
(join-path separator))))
(defn get-path
"Get the groups part of the name as a vector. E.g. group.subgroup.name -> ['group' 'subgroup']"
"Get the path of object by specified separator (E.g. with '.'
separator, the 'group.subgroup.name' -> ['group' 'subgroup'])"
[item separator]
(assert (valid-groupable-item? item) "expected groupable item")
(split-path (:name item) separator))
(->> (split-path (:name item) separator)
(not-empty)))
(defn get-groups-str
"Get the groups part of the name. E.g. group.subgroup.name -> group.subgroup"
@ -97,14 +102,9 @@
(def token-separator ".")
(defn get-token-path [path]
(get-path path token-separator))
;; FIXME: misleading name, we are spliting name into path, not
;; spliting path into path
(defn split-token-path
[path]
(split-path path token-separator))
(defn get-token-path
[token]
(get-path token token-separator))
(defrecord Token [name type value description modified-at])
@ -247,28 +247,38 @@
set-name (add-set-prefix (last paths))]
(conj set-path set-name)))
(defn get-token-set-path
[token-set]
(get-path token-set set-separator))
(defn split-token-set-name
[name]
(split-path name set-separator))
;; (defn rename-token-set-preserving-group
;; [token-set name]
;; (let [base (butlast (get-path token-set))
(defn normalize-set-name
[name]
(->> (split-token-set-name name)
(map str/trim)
(str/join set-separator)))
"Normalize a set name.
(defn get-token-set-path [token-set]
(let [path (get-path token-set set-separator)]
(add-token-set-paths-prefix path)))
If `relative-to` is provided, the normalized name will preserve the
same group prefix as reference name"
([name]
(->> (split-token-set-name name)
(str/join set-separator)))
([name relative-to]
(->> (concat (butlast (split-token-set-name relative-to))
(split-token-set-name name))
(str/join set-separator))))
;; FIXME: revisit
(defn get-token-set-final-name
[name]
(-> (split-token-set-name name)
(peek)))
(defn split-token-set-path [token-set-path]
(split-path token-set-path set-separator))
(defn set-name->prefixed-full-path [name-str]
(-> (split-token-set-name name-str)
(set-full-path->set-prefixed-full-path)))
@ -297,7 +307,7 @@
[tokens & {:keys [update-token-fn]
:or {update-token-fn identity}}]
(reduce-kv (fn [acc _ token]
(let [path (split-token-path (:name token))]
(let [path (get-token-path token)]
(assoc-in acc path (update-token-fn token))))
{} tokens))
@ -309,8 +319,8 @@
(reduce
(fn [acc [_ token]]
(let [temp-id (random-uuid)
token (assoc token :temp/id temp-id)
path (split-token-path (:name token))]
token (assoc token :temp/id temp-id)
path (get-token-path token)]
(-> acc
(assoc-in (concat [:tokens-tree] path) token)
(assoc-in [:ids temp-id] token))))
@ -378,6 +388,7 @@
(get-tokens-tree [_]
(tokens-tree tokens))
;; FIXME: looks redundant
(get-dtcg-tokens-tree [_]
(tokens-tree tokens :update-token-fn (fn [token]
(cond-> {"$value" (:value token)
@ -926,7 +937,7 @@ Will return a value that matches this schema:
active-themes)))
(delete-set-group [this set-group-name]
(let [path (split-token-set-path set-group-name)
(let [path (split-token-set-name set-group-name)
prefixed-path (map add-set-group-prefix path)
child-set-names (->> (get-sets-at-path this path)
(map :name)
@ -1268,14 +1279,17 @@ Will return a value that matches this schema:
(tree-seq d/ordered-map? vals themes))
;; Active themes without exposing hidden penpot theme
active-themes-clear (disj active-themes hidden-token-theme-path)
name-set-tuples (->> sets
(tree-seq d/ordered-map? vals)
(filter (partial instance? TokenSet))
(map (fn [token-set]
[(:name token-set) (get-dtcg-tokens-tree token-set)])))
ordered-set-names (map first name-set-tuples)
ordered-set-names (mapv first name-set-tuples)
sets (into {} name-set-tuples)
active-sets (get-active-themes-set-names this)]
(-> sets
(assoc "$themes" themes)
(assoc-in ["$metadata" "tokenSetOrder"] ordered-set-names)

File diff suppressed because it is too large Load diff

View file

@ -127,7 +127,7 @@
(wtu/update-workspace-tokens))))))
(defn create-token-set
[set-name token-set]
[set-name]
(ptk/reify ::create-token-set
ptk/UpdateEvent
(update [_ state]
@ -136,28 +136,16 @@
ptk/WatchEvent
(watch [it state _]
(let [token-set'
;; FIXME: wtf is this?
(update token-set :name #(if (empty? %)
set-name
(ctob/join-set-path [% set-name])))
token-set'
(update token-set' :name ctob/normalize-set-name)
data
(dsh/lookup-file-data state)
token-set-name
(:name token-set')
changes
(-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/set-token-set token-set-name false token-set'))]
(rx/of (set-selected-token-set-name token-set-name)
(dch/commit-changes changes))))))
(let [data (dsh/lookup-file-data state)
tokens-lib (get data :tokens-lib)
set-name (ctob/normalize-set-name set-name)]
(when-not (ctob/get-set tokens-lib set-name)
(let [token-set (ctob/make-token-set :name set-name)
changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/set-token-set set-name false token-set))]
(rx/of (set-selected-token-set-name set-name)
(dch/commit-changes changes))))))))
(defn rename-token-set-group [set-group-path set-group-fname]
(ptk/reify ::rename-token-set-group
@ -168,17 +156,28 @@
(rx/of
(dch/commit-changes changes))))))
(defn update-token-set [set-name token-set]
(defn update-token-set
[token-set name]
(ptk/reify ::update-token-set
ptk/WatchEvent
(watch [it state _]
(let [data (dsh/lookup-file-data state)
changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/set-token-set set-name false token-set))]
(rx/of
(set-selected-token-set-name (:name token-set))
(dch/commit-changes changes))))))
(let [data (dsh/lookup-file-data state)
name (ctob/normalize-set-name name (:name token-set))
tokens-lib (get data :tokens-lib)]
(cond
(= (:name token-set) name)
nil
(ctob/get-set tokens-lib name)
nil
:else
(let [changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/rename-token-set (:name token-set) name))]
(rx/of (set-selected-token-set-name (:name token-set))
(dch/commit-changes changes))))))))
(defn toggle-token-set
[name]

View file

@ -43,17 +43,27 @@
(st/emit! (dt/set-selected-token-set-name name)))
(defn on-update-token-set
[name token-set]
[token-set name]
(st/emit! (dt/clear-token-set-edition)
(dt/update-token-set (:name token-set) (ctob/update-name token-set name))))
(dt/update-token-set token-set name)))
(defn- on-update-token-set-group [path name]
(defn- on-update-token-set-group
[path name]
(st/emit! (dt/clear-token-set-edition)
(dt/rename-token-set-group path name)))
(defn- on-create-token-set [name token-set]
(st/emit! (ptk/data-event ::ev/event {::ev/name "create-token-set" :name name})
(dt/create-token-set name token-set)))
(defn- on-create-token-set
[parent-set name]
(let [;; FIXME: this code should be reusable under helper under
;; common types namespace
name
(if-let [parent-path (ctob/get-token-set-path parent-set)]
(->> (concat parent-path (ctob/split-token-set-name name))
(ctob/join-set-path))
(ctob/normalize-set-name name))]
(st/emit! (ptk/data-event ::ev/event {::ev/name "create-token-set" :name name})
(dt/create-token-set name))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; COMPONENTS
@ -62,13 +72,11 @@
(mf/defc editing-label*
{::mf/private true}
[{:keys [default-value on-cancel on-submit]}]
(let [ref (mf/use-ref)
on-submit-valid
(let [on-submit
(mf/use-fn
(mf/deps on-cancel on-submit default-value)
(fn [event]
(let [value (str/trim (dom/get-target-val event))]
(let [value (dom/get-target-val event)]
(if (or (str/empty? value)
(= value default-value))
(on-cancel)
@ -76,16 +84,15 @@
on-key-down
(mf/use-fn
(mf/deps on-submit-valid on-cancel)
(mf/deps on-submit on-cancel)
(fn [event]
(cond
(kbd/enter? event) (on-submit-valid event)
(kbd/enter? event) (on-submit event)
(kbd/esc? event) (on-cancel))))]
[:input
{:class (stl/css :editing-node)
:type "text"
:ref ref
:on-blur on-submit-valid
:on-blur on-submit
:on-key-down on-key-down
:auto-focus true
:placeholder (tr "workspace.token.set-edit-placeholder")
@ -234,6 +241,7 @@
(mf/defc sets-tree-set*
[{:keys [id set label tree-depth tree-path tree-index is-selected is-active is-draggable is-editing
on-select on-drop on-toggle on-start-edition on-reset-edition on-edit-submit]}]
(let [set-name (get set :name)
can-edit? (mf/use-ctx ctx/can-edit?)
@ -273,7 +281,7 @@
on-edit-submit'
(mf/use-fn
(mf/deps set on-edit-submit)
#(on-edit-submit % set))
#(on-edit-submit set %))
on-drag
(mf/use-fn