mirror of
https://github.com/penpot/penpot.git
synced 2026-03-22 10:23:43 +00:00
Merge pull request #4297 from penpot/hiru-fix-advance-shape-ref
Fix references when duplicating nested copies
This commit is contained in:
@@ -154,6 +154,17 @@
|
||||
:else
|
||||
(get-head-shape objects (get objects (:parent-id shape)) options))))
|
||||
|
||||
(defn get-child-heads
|
||||
"Get all recursive childs that are heads (when a head is found, do not
|
||||
continue down looking for subsequent nested heads)."
|
||||
[objects shape-id]
|
||||
(let [shape (get objects shape-id)]
|
||||
(if (nil? shape)
|
||||
[]
|
||||
(if (ctk/instance-head? shape)
|
||||
[shape]
|
||||
(mapcat #(get-child-heads objects %) (:shapes shape))))))
|
||||
|
||||
(defn get-parent-heads
|
||||
"Get all component heads that are ancestors of the shape, in top-down order
|
||||
(include self if it's also a head)."
|
||||
@@ -170,6 +181,20 @@
|
||||
(filter #(and (ctk/instance-head? %) (ctk/in-component-copy? %)))
|
||||
(reverse)))
|
||||
|
||||
(defn get-nesting-level-delta
|
||||
"Get how many levels a shape will 'go up' if moved under the new parent."
|
||||
[objects shape new-parent]
|
||||
(let [orig-heads (->> (get-parent-copy-heads objects shape)
|
||||
(remove #(= (:id %) (:id shape))))
|
||||
dest-heads (get-parent-copy-heads objects new-parent)
|
||||
|
||||
;; Calculate how many parent heads share in common the original
|
||||
;; shape and the new parent.
|
||||
pairs (map vector orig-heads dest-heads)
|
||||
common-count (count (take-while (fn [a b] (= a b)) pairs))]
|
||||
|
||||
(- (count orig-heads) common-count)))
|
||||
|
||||
(defn get-instance-root
|
||||
"Get the parent shape at the top of the component instance (main or copy)."
|
||||
[objects shape]
|
||||
|
||||
@@ -216,14 +216,14 @@
|
||||
|
||||
(some find-ref-shape-in-head (ctn/get-parent-heads (:objects container) shape))))
|
||||
|
||||
(defn find-original-ref-shape
|
||||
"Recursively call to find-ref-shape until find the original shape of the original component"
|
||||
[file container libraries shape & options]
|
||||
(defn advance-shape-ref
|
||||
"Get the shape-ref of the near main of the shape, recursively repeated as many times
|
||||
as the given levels."
|
||||
[file container libraries shape levels & options]
|
||||
(let [ref-shape (find-ref-shape file container libraries shape options)]
|
||||
(if (nil? (:shape-ref ref-shape))
|
||||
ref-shape
|
||||
(find-original-ref-shape file container libraries ref-shape options))))
|
||||
|
||||
(if (or (nil? (:shape-ref ref-shape)) (not (pos? levels)))
|
||||
(:id ref-shape)
|
||||
(advance-shape-ref file container libraries ref-shape (dec levels) options))))
|
||||
|
||||
(defn find-ref-component
|
||||
"Locate the nearest component in the local file or libraries that is referenced by the
|
||||
|
||||
Reference in New Issue
Block a user