🎉 Allow to duplicate components with main instance

This commit is contained in:
Andrés Moya 2022-06-17 16:44:32 +02:00
parent ce09ea6eb5
commit 165cdd871f
4 changed files with 49 additions and 15 deletions

View file

@ -223,6 +223,15 @@
(update :undo-changes d/preconj del-change) (update :undo-changes d/preconj del-change)
(apply-changes-local))))) (apply-changes-local)))))
(defn add-objects
([changes objects]
(add-objects changes objects nil))
([changes objects params]
(reduce #(add-object %1 %2 params)
changes
objects)))
(defn change-parent (defn change-parent
([changes parent-id shapes] ([changes parent-id shapes]
(change-parent changes parent-id shapes nil)) (change-parent changes parent-id shapes nil))

View file

@ -355,19 +355,23 @@
unames (into #{} (map :name) all-components) unames (into #{} (map :name) all-components)
new-name (ctst/generate-unique-name unames (:name component)) new-name (ctst/generate-unique-name unames (:name component))
[new-shape new-shapes _updated-shapes main-instance main-instance-page] main-instance-page (wsh/lookup-page state (:main-instance-page component))
(dwlh/duplicate-component component) main-instance-shape (ctn/get-shape main-instance-page (:main-instance-id component))
_ (prn "OJOOOOOOOOOOOOOOO falta calcular main-instance") [new-component-shape new-component-shapes
new-main-instance-shape new-main-instance-shapes]
(dwlh/duplicate-component component main-instance-page main-instance-shape)
changes (-> (pcb/empty-changes it nil) ;; no objects are changed changes (-> (pcb/empty-changes it nil)
(pcb/with-objects nil) ;; in the current page (pcb/with-page main-instance-page)
(pcb/add-component (:id new-shape) (pcb/with-objects (:objects main-instance-page))
(pcb/add-objects new-main-instance-shapes {:ignore-touched true})
(pcb/add-component (:id new-component-shape)
(:path component) (:path component)
new-name new-name
new-shapes new-component-shapes
[] []
(:id main-instance) (:id new-main-instance-shape)
(:id main-instance-page)))] (:id main-instance-page)))]
(rx/of (dch/commit-changes changes)))))) (rx/of (dch/commit-changes changes))))))

View file

@ -130,12 +130,29 @@
(defn duplicate-component (defn duplicate-component
"Clone the root shape of the component and all children. Generate new "Clone the root shape of the component and all children. Generate new
ids from all of them." ids from all of them."
[component] [component main-instance-page main-instance-shape]
(let [component-root (cph/get-component-root component)] (let [position (gpt/add (gpt/point (:x main-instance-shape) (:y main-instance-shape))
(gpt/point (+ (:width main-instance-shape) 50) 0))
component-root (cph/get-component-root component)
[new-component-shape new-component-shapes _]
(ctst/clone-object component-root (ctst/clone-object component-root
nil nil
(get component :objects) (get component :objects)
identity))) identity)
[new-instance-shape new-instance-shapes]
(ctn/instantiate-component main-instance-page
{:id (:id new-component-shape)
:name (:name new-component-shape)
:objects (d/index-by :id new-component-shapes)}
(:component-file main-instance-shape)
position)]
[new-component-shape new-component-shapes
new-instance-shape new-instance-shapes]))
(defn generate-instantiate-component (defn generate-instantiate-component
"Generate changes to create a new instance from a component." "Generate changes to create a new instance from a component."

View file

@ -276,7 +276,11 @@
(dorun (for [component (vals components)] (dorun (for [component (vals components)]
(do (do
(println) (println)
(println (str/format "[%s]" (:name component))) (println (str/format "[%s]" (:name component))
(when show-ids
(str/format " (main: %s/%s)"
(:main-instance-page component)
(:main-instance-id component))))
(show-shape (:id component) 0 (:objects component))))))))) (show-shape (:id component) 0 (:objects component)))))))))
(defn ^:export dump-tree (defn ^:export dump-tree