♻️ Components refactor: generator for rename-component

This commit is contained in:
Pablo Alba 2024-04-22 13:09:56 +02:00 committed by Andrés Moya
parent 02c455dcba
commit f91d60eeb6
2 changed files with 9 additions and 18 deletions

View file

@ -847,6 +847,7 @@
(defn- generate-rename-component (defn- generate-rename-component
"Generate the changes for rename the component with the given id, in the current file library."
[changes id new-name library-data components-v2] [changes id new-name library-data components-v2]
(let [[path name] (cfh/parse-path-name new-name) (let [[path name] (cfh/parse-path-name new-name)
update-fn update-fn
@ -864,6 +865,9 @@
(pcb/with-library-data library-data) (pcb/with-library-data library-data)
(pcb/update-component id update-fn)))) (pcb/update-component id update-fn))))
(defn generate-sync-shape-inverse (defn generate-sync-shape-inverse
"Generate changes to update the component a shape is linked to, from "Generate changes to update the component a shape is linked to, from
the values in the shape and all its children." the values in the shape and all its children."
@ -1785,3 +1789,5 @@
[:component-root])] [:component-root])]
[root (:id root-shape) changes])) [root (:id root-shape) changes]))

View file

@ -414,25 +414,10 @@
(let [new-name (str/trim new-name)] (let [new-name (str/trim new-name)]
(if (str/empty? new-name) (if (str/empty? new-name)
(rx/empty) (rx/empty)
(let [data (get state :workspace-data) (let [library-data (get state :workspace-data)
[path name] (cfh/parse-path-name new-name)
components-v2 (features/active-feature? state "components/v2") components-v2 (features/active-feature? state "components/v2")
changes (-> (pcb/empty-changes it)
update-fn (cflh/generate-rename-component id new-name library-data components-v2))]
(fn [component]
(cond-> component
:always
(assoc :path path
:name name)
(not components-v2)
(update :objects
;; Give the same name to the root shape
#(assoc-in % [id :name] name))))
changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/update-component id update-fn))]
(rx/of (dch/commit-changes changes)))))))) (rx/of (dch/commit-changes changes))))))))