mirror of
https://github.com/penpot/penpot.git
synced 2025-01-20 05:34:23 -05:00
✨ Rename set groups
This commit is contained in:
parent
2a766a7190
commit
ddc30b7a3c
6 changed files with 131 additions and 19 deletions
|
@ -415,6 +415,12 @@
|
|||
[:type [:= :add-token-sets]]
|
||||
[:token-sets [:sequential ::ctot/token-set]]]]
|
||||
|
||||
[:rename-token-set-group
|
||||
[:map {:title "RenameTokenSetGroup"}
|
||||
[:type [:= :rename-token-set-group]]
|
||||
[:from-path-str :string]
|
||||
[:to-path-str :string]]]
|
||||
|
||||
[:mod-token-set
|
||||
[:map {:title "ModTokenSetChange"}
|
||||
[:type [:= :mod-token-set]]
|
||||
|
@ -1063,6 +1069,13 @@
|
|||
(ctob/ensure-tokens-lib)
|
||||
(ctob/add-sets (map ctob/make-token-set token-sets)))))
|
||||
|
||||
(defmethod process-change :rename-token-set-group
|
||||
[data {:keys [from-path-str to-path-str]}]
|
||||
(update data :tokens-lib (fn [lib]
|
||||
(-> lib
|
||||
(ctob/ensure-tokens-lib)
|
||||
(ctob/rename-set-group from-path-str to-path-str)))))
|
||||
|
||||
(defmethod process-change :mod-token-set
|
||||
[data {:keys [name token-set]}]
|
||||
(update data :tokens-lib (fn [lib]
|
||||
|
|
|
@ -812,6 +812,14 @@
|
|||
(update :undo-changes conj {:type :del-token-set :name (:name token-set)})
|
||||
(apply-changes-local)))
|
||||
|
||||
(defn rename-token-set-group
|
||||
[changes from-path-str to-path-str]
|
||||
(-> changes
|
||||
(update :redo-changes conj {:type :rename-token-set-group :from-path-str from-path-str :to-path-str to-path-str})
|
||||
;; TODO: Figure out undo
|
||||
#_(update :undo-changes conj {:type :rename-token-set-group :name (:name token-set) :token-set (or prev-token-set token-set)})
|
||||
(apply-changes-local)))
|
||||
|
||||
(defn update-token-set
|
||||
[changes token-set prev-token-set]
|
||||
(-> changes
|
||||
|
|
|
@ -183,6 +183,10 @@
|
|||
|
||||
(def set-separator "/")
|
||||
|
||||
(defn join-set-path-str [& args]
|
||||
(->> (filter some? args)
|
||||
(str/join set-separator)))
|
||||
|
||||
(defn join-set-path [path]
|
||||
(join-path path set-separator))
|
||||
|
||||
|
@ -236,12 +240,15 @@
|
|||
(-> (set-name->prefixed-full-path name-str)
|
||||
(join-set-path)))
|
||||
|
||||
(defn prefixed-set-path-string->set-name-string [path-str]
|
||||
(defn prefixed-set-path-string->set-path [path-str]
|
||||
(->> (split-token-set-path path-str)
|
||||
(map (fn [path-part]
|
||||
(or (-> (split-set-str-path-prefix path-part)
|
||||
(second))
|
||||
path-part)))
|
||||
path-part)))))
|
||||
|
||||
(defn prefixed-set-path-string->set-name-string [path-str]
|
||||
(->> (prefixed-set-path-string->set-path path-str)
|
||||
(join-set-path)))
|
||||
|
||||
(defn prefixed-set-path-final-group?
|
||||
|
@ -407,6 +414,8 @@ When `before-set-name` is nil, move set to bottom")
|
|||
(get-in-set-tree [_ path] "get `path` in nested tree of all sets in the library")
|
||||
(get-sets [_] "get an ordered sequence of all sets in the library")
|
||||
(get-sets-at-prefix-path [_ path] "get an ordered sequence of sets at `path` in the library")
|
||||
(get-sets-at-path [_ path] "TODO")
|
||||
(rename-set-group [_ from to] "TODO")
|
||||
(get-ordered-set-names [_] "get an ordered sequence of all sets names in the library")
|
||||
(get-set [_ set-name] "get one set looking for name")
|
||||
(get-neighbor-set-name [_ set-name index-offset] "get neighboring set name offset by `index-offset`"))
|
||||
|
@ -735,6 +744,21 @@ When `before-set-name` is nil, move set to bottom")
|
|||
(tree-seq d/ordered-map? vals)
|
||||
(filter (partial instance? TokenSet))))
|
||||
|
||||
(get-sets-at-path [_ path-str]
|
||||
(some->> (split-token-set-path path-str)
|
||||
(map add-set-path-group-prefix)
|
||||
(get-in sets)
|
||||
(tree-seq d/ordered-map? vals)
|
||||
(filter (partial instance? TokenSet))))
|
||||
|
||||
(rename-set-group [this from-path-str to-path-str]
|
||||
(->> (get-sets-at-path this from-path-str)
|
||||
(reduce
|
||||
(fn [lib set]
|
||||
(update-set lib (:name set) (fn [set']
|
||||
(update set' :name #(str to-path-str (str/strip-prefix % from-path-str))))))
|
||||
this)))
|
||||
|
||||
(get-ordered-set-names [this]
|
||||
(map :name (get-sets this)))
|
||||
|
||||
|
|
|
@ -230,6 +230,24 @@
|
|||
(t/is (= (:name token-set') "updated-name"))
|
||||
(t/is (dt/is-after? (:modified-at token-set') (:modified-at token-set)))))
|
||||
|
||||
(t/deftest rename-token-set-group
|
||||
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
||||
(ctob/add-set (ctob/make-token-set :name "foo/bar/baz"))
|
||||
(ctob/add-set (ctob/make-token-set :name "foo/bar/baz/baz-child-1"))
|
||||
(ctob/add-set (ctob/make-token-set :name "foo/bar/baz/baz-child-2"))
|
||||
(ctob/add-theme (ctob/make-token-theme :name "theme" :sets #{"foo/bar/baz/baz-child-1"})))
|
||||
tokens-lib' (-> tokens-lib
|
||||
(ctob/rename-set-group "foo/bar" "foo/bar-renamed")
|
||||
(ctob/rename-set-group "foo/bar-renamed/baz" "foo/bar-renamed/baz-renamed"))
|
||||
expected-set-names (ctob/get-ordered-set-names tokens-lib')
|
||||
expected-theme-sets (-> (ctob/get-theme tokens-lib' "" "theme")
|
||||
:sets)]
|
||||
(t/is (= expected-set-names
|
||||
'("foo/bar-renamed/baz"
|
||||
"foo/bar-renamed/baz-renamed/baz-child-1"
|
||||
"foo/bar-renamed/baz-renamed/baz-child-2")))
|
||||
(t/is (= expected-theme-sets #{"foo/bar-renamed/baz-renamed/baz-child-1"}))))
|
||||
|
||||
(t/deftest delete-token-set
|
||||
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
||||
(ctob/add-set (ctob/make-token-set :name "test-token-set"))
|
||||
|
|
|
@ -168,6 +168,16 @@
|
|||
(set-selected-token-set-path-from-name (:name new-token-set))
|
||||
(dch/commit-changes changes)))))))
|
||||
|
||||
(defn rename-token-set-group [from-path-str to-path-str]
|
||||
(ptk/reify ::rename-token-set-group
|
||||
ptk/WatchEvent
|
||||
(watch [it _state _]
|
||||
(let [changes (-> (pcb/empty-changes it)
|
||||
(pcb/rename-token-set-group from-path-str to-path-str))]
|
||||
(rx/of
|
||||
(set-selected-token-set-path-from-name to-path-str)
|
||||
(dch/commit-changes changes))))))
|
||||
|
||||
(defn update-token-set [set-name token-set]
|
||||
(ptk/reify ::update-token-set
|
||||
ptk/WatchEvent
|
||||
|
|
|
@ -30,6 +30,15 @@
|
|||
(defn on-update-token-set [set-name token-set]
|
||||
(st/emit! (wdt/update-token-set set-name token-set)))
|
||||
|
||||
(defn on-update-token-set-group [from-prefixed-path-str to-path-str]
|
||||
(st/emit!
|
||||
(wdt/rename-token-set-group
|
||||
(ctob/prefixed-set-path-string->set-name-string from-prefixed-path-str)
|
||||
(-> (ctob/prefixed-set-path-string->set-path from-prefixed-path-str)
|
||||
(butlast)
|
||||
(ctob/join-set-path)
|
||||
(ctob/join-set-path-str to-path-str)))))
|
||||
|
||||
(defn on-create-token-set [_ token-set]
|
||||
(st/emit! (wdt/create-token-set token-set)))
|
||||
|
||||
|
@ -60,7 +69,7 @@
|
|||
:default-value default-value}]))
|
||||
|
||||
(mf/defc sets-tree-set-group
|
||||
[{:keys [label tree-depth tree-path selected? collapsed? editing? on-edit on-edit-reset _on-edit-submit]}]
|
||||
[{:keys [label tree-depth tree-path selected? collapsed? editing? on-edit on-edit-reset on-edit-submit]}]
|
||||
(let [editing?' (editing? tree-path)
|
||||
on-context-menu
|
||||
(mf/use-fn
|
||||
|
@ -73,10 +82,19 @@
|
|||
(wdt/show-token-set-context-menu
|
||||
{:position (dom/get-client-position event)
|
||||
:prefixed-set-path tree-path})))))
|
||||
on-click (mf/use-fn
|
||||
(fn [event]
|
||||
(dom/stop-propagation event)
|
||||
(swap! collapsed? not)))]
|
||||
on-click
|
||||
(mf/use-fn
|
||||
(fn [event]
|
||||
(dom/stop-propagation event)
|
||||
(swap! collapsed? not)))
|
||||
on-double-click
|
||||
(mf/use-fn
|
||||
(mf/deps tree-path)
|
||||
#(on-edit tree-path))
|
||||
on-edit-submit
|
||||
(mf/use-fn
|
||||
(mf/deps tree-path on-edit-submit)
|
||||
#(on-edit-submit tree-path %))]
|
||||
[:div {:role "button"
|
||||
:data-testid "tokens-set-group-item"
|
||||
:style {"--tree-depth" tree-depth}
|
||||
|
@ -95,10 +113,9 @@
|
|||
{:default-value label
|
||||
:on-cancel on-edit-reset
|
||||
:on-create on-edit-reset
|
||||
;; TODO Implement set group renaming
|
||||
:on-submit (constantly nil)}]
|
||||
:on-submit on-edit-submit}]
|
||||
[:div {:class (stl/css :set-name)
|
||||
:on-double-click #(on-edit tree-path)}
|
||||
:on-double-click on-double-click}
|
||||
label])]))
|
||||
|
||||
(mf/defc sets-tree-set
|
||||
|
@ -124,10 +141,16 @@
|
|||
(wdt/show-token-set-context-menu
|
||||
{:position (dom/get-client-position event)
|
||||
:prefixed-set-path tree-path})))))
|
||||
on-double-click (mf/use-fn
|
||||
(mf/deps tree-path)
|
||||
#(on-edit tree-path))
|
||||
on-checkbox-click (mf/use-fn
|
||||
(fn [event]
|
||||
(dom/stop-propagation event)
|
||||
(on-toggle set-name)))]
|
||||
(on-toggle set-name)))
|
||||
on-edit-submit (mf/use-fn
|
||||
(mf/deps set on-edit-submit)
|
||||
#(on-edit-submit set-name (ctob/update-name set %)))]
|
||||
[:div {:role "button"
|
||||
:data-testid "tokens-set-item"
|
||||
:style {"--tree-depth" tree-depth}
|
||||
|
@ -145,10 +168,10 @@
|
|||
{:default-value label
|
||||
:on-cancel on-edit-reset
|
||||
:on-create on-edit-reset
|
||||
:on-submit #(on-edit-submit set-name (ctob/update-name set %))}]
|
||||
:on-submit on-edit-submit}]
|
||||
[:*
|
||||
[:div {:class (stl/css :set-name)
|
||||
:on-double-click #(on-edit tree-path)}
|
||||
:on-double-click on-double-click}
|
||||
label]
|
||||
[:button {:type "button"
|
||||
:on-click on-checkbox-click
|
||||
|
@ -161,7 +184,19 @@
|
|||
:id ic/tick}])]])]))
|
||||
|
||||
(mf/defc sets-tree
|
||||
[{:keys [set-path set-node tree-depth tree-path on-select selected? on-toggle active? editing? on-edit on-edit-reset on-edit-submit]
|
||||
[{:keys [active?
|
||||
editing?
|
||||
on-edit
|
||||
on-edit-reset
|
||||
on-edit-submit-set
|
||||
on-edit-submit-group
|
||||
on-select
|
||||
on-toggle
|
||||
selected?
|
||||
set-node
|
||||
set-path
|
||||
tree-depth
|
||||
tree-path]
|
||||
:or {tree-depth 0}
|
||||
:as props}]
|
||||
(let [[set-path-prefix set-fname] (some-> set-path (ctob/split-set-str-path-prefix))
|
||||
|
@ -188,7 +223,7 @@
|
|||
:on-toggle on-toggle
|
||||
:on-edit on-edit
|
||||
:on-edit-reset on-edit-reset
|
||||
:on-edit-submit on-edit-submit}]
|
||||
:on-edit-submit on-edit-submit-set}]
|
||||
set-group?
|
||||
[:& sets-tree-set-group
|
||||
{:selected? (selected? tree-path)
|
||||
|
@ -200,10 +235,10 @@
|
|||
:editing? editing?
|
||||
:on-edit on-edit
|
||||
:on-edit-reset on-edit-reset
|
||||
:on-edit-submit on-edit-submit}])
|
||||
:on-edit-submit on-edit-submit-group}])
|
||||
(when children?
|
||||
(for [[set-path set-node] set-node
|
||||
:let [tree-path' (str (when tree-path (str tree-path "/")) set-path)]]
|
||||
:let [tree-path' (ctob/join-set-path-str tree-path set-path)]]
|
||||
[:& sets-tree
|
||||
{:key tree-path'
|
||||
:set-path set-path
|
||||
|
@ -217,11 +252,13 @@
|
|||
:editing? editing?
|
||||
:on-edit on-edit
|
||||
:on-edit-reset on-edit-reset
|
||||
:on-edit-submit on-edit-submit}]))]))
|
||||
:on-edit-submit-set on-edit-submit-set
|
||||
:on-edit-submit-group on-update-token-set-group}]))]))
|
||||
|
||||
(mf/defc controlled-sets-list
|
||||
[{:keys [token-sets
|
||||
on-update-token-set
|
||||
on-update-token-set-group
|
||||
token-set-selected?
|
||||
token-set-active?
|
||||
on-create-token-set
|
||||
|
@ -251,7 +288,8 @@
|
|||
:editing? editing?
|
||||
:on-edit on-edit
|
||||
:on-edit-reset on-reset
|
||||
:on-edit-submit on-update-token-set}]
|
||||
:on-edit-submit-set on-update-token-set
|
||||
:on-edit-submit-group on-update-token-set-group}]
|
||||
(when new?
|
||||
[:& sets-tree-set
|
||||
{:set (ctob/make-token-set :name "")
|
||||
|
@ -285,4 +323,5 @@
|
|||
:origin "set-panel"
|
||||
:on-toggle-token-set on-toggle-token-set-click
|
||||
:on-update-token-set on-update-token-set
|
||||
:on-update-token-set-group on-update-token-set-group
|
||||
:on-create-token-set on-create-token-set}]))
|
||||
|
|
Loading…
Add table
Reference in a new issue