Merge pull request #4297 from penpot/hiru-fix-advance-shape-ref

Fix references when duplicating nested copies
This commit is contained in:
Alejandro
2024-03-22 08:22:20 +01:00
committed by GitHub
4 changed files with 75 additions and 15 deletions

View File

@@ -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]

View File

@@ -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