mirror of
https://github.com/penpot/penpot.git
synced 2025-01-23 23:18:48 -05:00
🎉 Allow to duplicate components with main instance
This commit is contained in:
parent
ce09ea6eb5
commit
165cdd871f
4 changed files with 49 additions and 15 deletions
|
@ -223,6 +223,15 @@
|
|||
(update :undo-changes d/preconj del-change)
|
||||
(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
|
||||
([changes parent-id shapes]
|
||||
(change-parent changes parent-id shapes nil))
|
||||
|
|
|
@ -355,19 +355,23 @@
|
|||
unames (into #{} (map :name) all-components)
|
||||
new-name (ctst/generate-unique-name unames (:name component))
|
||||
|
||||
[new-shape new-shapes _updated-shapes main-instance main-instance-page]
|
||||
(dwlh/duplicate-component component)
|
||||
main-instance-page (wsh/lookup-page state (:main-instance-page 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
|
||||
(pcb/with-objects nil) ;; in the current page
|
||||
(pcb/add-component (:id new-shape)
|
||||
changes (-> (pcb/empty-changes it nil)
|
||||
(pcb/with-page main-instance-page)
|
||||
(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)
|
||||
new-name
|
||||
new-shapes
|
||||
new-component-shapes
|
||||
[]
|
||||
(:id main-instance)
|
||||
(:id new-main-instance-shape)
|
||||
(:id main-instance-page)))]
|
||||
|
||||
(rx/of (dch/commit-changes changes))))))
|
||||
|
|
|
@ -130,12 +130,29 @@
|
|||
(defn duplicate-component
|
||||
"Clone the root shape of the component and all children. Generate new
|
||||
ids from all of them."
|
||||
[component]
|
||||
(let [component-root (cph/get-component-root component)]
|
||||
(ctst/clone-object component-root
|
||||
nil
|
||||
(get component :objects)
|
||||
identity)))
|
||||
[component main-instance-page main-instance-shape]
|
||||
(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
|
||||
nil
|
||||
(get component :objects)
|
||||
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
|
||||
"Generate changes to create a new instance from a component."
|
||||
|
|
|
@ -276,7 +276,11 @@
|
|||
(dorun (for [component (vals components)]
|
||||
(do
|
||||
(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)))))))))
|
||||
|
||||
(defn ^:export dump-tree
|
||||
|
|
Loading…
Add table
Reference in a new issue