diff --git a/common/src/app/common/files/changes.cljc b/common/src/app/common/files/changes.cljc index 50411a014..2adf6b559 100644 --- a/common/src/app/common/files/changes.cljc +++ b/common/src/app/common/files/changes.cljc @@ -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] diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index d68aec93c..a1580b124 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -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 diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index 484d9c81d..f59b6106e 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -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? @@ -406,7 +413,9 @@ When `before-set-name` is nil, move set to bottom") (get-set-tree [_] "get a nested tree of all sets in the library") (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-prefix-path [_ prefixed-path] "get an ordered sequence of sets at `prefixed-path` in the library") + (get-sets-at-path [_ path-str] "get an ordered sequence of sets at `path` in the library") + (rename-set-group [_ from-path-str to-path-str] "renames set groups and all child set names from `from-path-str` to `to-path-str`") (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,11 +744,26 @@ Will return a value that matches this schema: (->> (tree-seq d/ordered-map? vals sets) (filter (partial instance? TokenSet)))) - (get-sets-at-prefix-path [_ path] - (some->> (get-in sets (split-token-set-path path)) + (get-sets-at-prefix-path [_ prefixed-path] + (some->> (get-in sets (split-token-set-path prefixed-path)) (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))) diff --git a/common/test/common_tests/types/tokens_lib_test.cljc b/common/test/common_tests/types/tokens_lib_test.cljc index f11829d98..3129e93fb 100644 --- a/common/test/common_tests/types/tokens_lib_test.cljc +++ b/common/test/common_tests/types/tokens_lib_test.cljc @@ -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")) diff --git a/frontend/src/app/main/data/tokens.cljs b/frontend/src/app/main/data/tokens.cljs index cc94f3e1a..d0bbc890b 100644 --- a/frontend/src/app/main/data/tokens.cljs +++ b/frontend/src/app/main/data/tokens.cljs @@ -133,6 +133,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 diff --git a/frontend/src/app/main/ui/workspace/tokens/sets.cljs b/frontend/src/app/main/ui/workspace/tokens/sets.cljs index 1f75103a3..6d67e8232 100644 --- a/frontend/src/app/main/ui/workspace/tokens/sets.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/sets.cljs @@ -31,6 +31,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))) @@ -79,7 +88,7 @@ :id (if mixed? ic/remove ic/tick)}])])) (mf/defc sets-tree-set-group - [{:keys [label tree-depth tree-path active? selected? collapsed? editing? on-edit on-edit-reset _on-edit-submit]}] + [{:keys [label tree-depth tree-path active? selected? collapsed? editing? on-edit on-edit-reset on-edit-submit]}] (let [editing?' (editing? tree-path) active?' (active? tree-path) on-context-menu @@ -93,10 +102,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} @@ -115,11 +133,10 @@ {: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] [:& checkbox {:checked (case active?' @@ -151,10 +168,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} @@ -172,10 +195,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] [:& checkbox {:on-click on-checkbox-click @@ -188,7 +211,8 @@ editing? on-edit on-edit-reset - on-edit-submit + on-edit-submit-set + on-edit-submit-group on-select on-toggle selected? @@ -222,7 +246,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) @@ -235,10 +259,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 @@ -253,11 +277,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? token-set-group-active? @@ -288,7 +314,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 "") @@ -326,4 +353,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}]))