0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-12 07:41:43 -05:00

💄 Style changes on clone-object function (now clone-shape)

This commit is contained in:
Andrés Moya 2023-12-15 14:36:47 +01:00 committed by Andrey Antukh
parent 2fc6290c8f
commit f49cf0b6ae
3 changed files with 94 additions and 100 deletions

View file

@ -255,7 +255,11 @@
(dissoc :component-root))) (dissoc :component-root)))
[new-root-shape new-shapes updated-shapes] [new-root-shape new-shapes updated-shapes]
(ctst/clone-object shape nil objects update-new-shape update-original-shape) (ctst/clone-shape shape
nil
objects
:update-new-shape update-new-shape
:update-original-shape update-original-shape)
;; If frame-id points to a shape inside the component, remap it to the ;; If frame-id points to a shape inside the component, remap it to the
;; corresponding new frame shape. If not, set it to nil. ;; corresponding new frame shape. If not, set it to nil.
@ -339,15 +343,14 @@
(dissoc :component-root)))) (dissoc :component-root))))
[new-shape new-shapes _] [new-shape new-shapes _]
(ctst/clone-object component-shape (ctst/clone-shape component-shape
frame-id frame-id
(if components-v2 (:objects component-page) (:objects component)) (if components-v2 (:objects component-page) (:objects component))
update-new-shape :update-new-shape update-new-shape
(fn [object _] object) :force-id force-id
force-id :keep-ids? keep-ids?
keep-ids? :frame-id frame-id
frame-id) :dest-objects (:objects container))
;; Fix empty parent-id and remap all grid cells to the new ids. ;; Fix empty parent-id and remap all grid cells to the new ids.
remap-ids remap-ids

View file

@ -342,39 +342,26 @@
[frame] [frame]
(not (mth/almost-zero? (:rotation frame 0)))) (not (mth/almost-zero? (:rotation frame 0))))
(defn clone-object (defn clone-shape
"Gets a copy of the object and all its children, with new ids and with "Gets a copy of the shape and all its children, with new ids and with
the parent-children links correctly set. Admits functions to make the parent-children links correctly set. Admits functions to make
more transformations to the cloned objects and the original ones. more transformations to the cloned shapes and the original ones.
Returns the cloned object, the list of all new objects (including Returns the cloned shape, the list of all new shapes (including
the cloned one), and possibly a list of original objects modified. the cloned one), and possibly a list of original shapes modified.
The list of objects are returned in tree traversal order, respecting The list of shapes are returned in tree traversal order, respecting
the order of the children of each parent." the order of the children of each parent."
[shape parent-id objects & {:keys [update-new-shape update-original-shape force-id keep-ids? frame-id dest-objects]
([object parent-id objects] :or {update-new-shape (fn [shape _] shape)
(clone-object object parent-id objects (fn [object _] object) (fn [object _] object) nil false nil objects)) update-original-shape (fn [shape _] shape)
force-id nil
([object parent-id objects update-new-object] keep-ids? false
(clone-object object parent-id objects update-new-object (fn [object _] object) nil false nil objects)) frame-id nil
dest-objects objects}}]
([object parent-id objects update-new-object update-original-object]
(clone-object object parent-id objects update-new-object update-original-object nil false nil objects))
([object parent-id objects update-new-object update-original-object force-id]
(clone-object object parent-id objects update-new-object update-original-object force-id false nil objects))
([object parent-id objects update-new-object update-original-object force-id keep-ids?]
(clone-object object parent-id objects update-new-object update-original-object force-id keep-ids? nil objects))
([object parent-id objects update-new-object update-original-object force-id keep-ids? frame-id]
(clone-object object parent-id objects update-new-object update-original-object force-id keep-ids? frame-id objects))
([object parent-id objects update-new-object update-original-object force-id keep-ids? frame-id dest-objects]
(let [new-id (cond (let [new-id (cond
(some? force-id) force-id (some? force-id) force-id
keep-ids? (:id object) keep-ids? (:id shape)
:else (uuid/next)) :else (uuid/next))
;; Assign the correct frame-id for the given parent. It's the parent-id (if parent is frame) ;; Assign the correct frame-id for the given parent. It's the parent-id (if parent is frame)
@ -390,46 +377,54 @@
:else :else
frame-id)] frame-id)]
(loop [child-ids (seq (:shapes object)) (loop [child-ids (seq (:shapes shape))
new-direct-children [] new-direct-children []
new-children [] new-children []
updated-children []] updated-children []]
(if (empty? child-ids) (if (empty? child-ids)
(let [new-object (cond-> object (let [new-shape (cond-> shape
:always :always
(assoc :id new-id (assoc :id new-id
:parent-id parent-id :parent-id parent-id
:frame-id frame-id) :frame-id frame-id)
(some? (:shapes object)) (some? (:shapes shape))
(assoc :shapes (mapv :id new-direct-children))) (assoc :shapes (mapv :id new-direct-children)))
new-object (update-new-object new-object object) new-shape (update-new-shape new-shape shape)
new-objects (into [new-object] new-children) new-shapes (into [new-shape] new-children)
updated-object (update-original-object object new-object) updated-shape (update-original-shape shape new-shape)
updated-objects (if (identical? object updated-object) updated-shapes (if (identical? shape updated-shape)
updated-children updated-children
(into [updated-object] updated-children))] (into [updated-shape] updated-children))]
[new-object new-objects updated-objects]) [new-shape new-shapes updated-shapes])
(let [child-id (first child-ids) (let [child-id (first child-ids)
child (get objects child-id) child (get objects child-id)
_ (dm/assert! (some? child)) _ (dm/assert! (some? child))
frame-id-child (if (cfh/frame-shape? object) frame-id-child (if (cfh/frame-shape? shape)
new-id new-id
frame-id) frame-id)
[new-child new-child-objects updated-child-objects] [new-child new-child-shapes updated-child-shapes]
(clone-object child new-id objects update-new-object update-original-object nil keep-ids? frame-id-child)] (clone-shape child
new-id
objects
:update-new-shape update-new-shape
:update-original-shape update-original-shape
:force-id nil
:keep-ids? keep-ids?
:frame-id frame-id-child
:dest-objects dest-objects)]
(recur (recur
(next child-ids) (next child-ids)
(into new-direct-children [new-child]) (into new-direct-children [new-child])
(into new-children new-child-objects) (into new-children new-child-shapes)
(into updated-children updated-child-objects)))))))) (into updated-children updated-child-shapes)))))))
(defn generate-shape-grid (defn generate-shape-grid
"Generate a sequence of positions that lays out the list of "Generate a sequence of positions that lays out the list of

View file

@ -98,11 +98,11 @@
(gsh/move delta))) (gsh/move delta)))
[new-instance-shape new-instance-shapes _] [new-instance-shape new-instance-shapes _]
(ctst/clone-object main-instance-shape (ctst/clone-shape main-instance-shape
(:parent-id main-instance-shape) (:parent-id main-instance-shape)
(:objects main-instance-page) (:objects main-instance-page)
update-new-shape :update-new-shape update-new-shape
update-original-shape) :update-original-shape update-original-shape)
remap-frame remap-frame
(fn [shape] (fn [shape]
@ -134,10 +134,9 @@
(let [component-root (d/seek #(nil? (:parent-id %)) (vals (:objects component))) (let [component-root (d/seek #(nil? (:parent-id %)) (vals (:objects component)))
[new-component-shape new-component-shapes _] [new-component-shape new-component-shapes _]
(ctst/clone-object component-root (ctst/clone-shape component-root
nil nil
(get component :objects) (get component :objects))]
identity)]
[new-component-shape new-component-shapes nil nil])))) [new-component-shape new-component-shapes nil nil]))))
@ -976,15 +975,12 @@
original-shape) original-shape)
[_ new-shapes _] [_ new-shapes _]
(ctst/clone-object component-shape (ctst/clone-shape component-shape
(:id parent-shape) (:id parent-shape)
(get component-page :objects) (get component-page :objects)
update-new-shape :update-new-shape update-new-shape
update-original-shape :update-original-shape update-original-shape
nil :dest-objects (get container :objects))
false
nil
(:objects container))
add-obj-change (fn [changes shape'] add-obj-change (fn [changes shape']
(update changes :redo-changes conj (update changes :redo-changes conj
@ -1042,11 +1038,11 @@
original-shape)) original-shape))
[_new-shape new-shapes updated-shapes] [_new-shape new-shapes updated-shapes]
(ctst/clone-object shape (ctst/clone-shape shape
(:id component-parent-shape) (:id component-parent-shape)
(get page :objects) (get page :objects)
update-new-shape :update-new-shape update-new-shape
update-original-shape) :update-original-shape update-original-shape)
add-obj-change (fn [changes shape'] add-obj-change (fn [changes shape']
(update changes :redo-changes conj (update changes :redo-changes conj