This commit is contained in:
Andrés Moya
2026-03-19 12:27:08 +01:00
parent de9aef04c9
commit 3611f2d4f6
4 changed files with 35 additions and 18 deletions

View File

@@ -10,22 +10,20 @@
[app.common.types.file :as ctf]))
(defn fix-missing-swap-slots
"Locate shapes that have been swapped (i.e. their shape-ref does not point to the near match) but
they don't have a swap slot. In this case, add one pointing to the near match."
[file libraries]
(ctf/update-all-shapes
file
(fn [shape]
(if (and (ctk/instance-head? shape) (ctk/in-component-copy? shape))
(let [{:keys [container]}
(meta shape)
ref-shape
(ctf/find-ref-shape file container libraries shape :include-deleted? true :with-context? true)]
(println "comparing" (:name shape) "with ref" (some-> ref-shape :name))
(if ref-shape
(if (and (not= (:shape-ref shape) (:id ref-shape))
(nil? (ctk/get-swap-slot shape)))
(let [updated-shape (ctk/set-swap-slot shape (:id ref-shape))]
{:result :update :updated-shape updated-shape})
{:result :keep})
(if (ctk/subcopy-head? shape)
(let [container (:container (meta shape))
near-match (ctf/find-near-match file container libraries shape :include-deleted? true :with-context? false)]
(if (and (some? near-match)
(not= (:shape-ref shape) (:id near-match))
(nil? (ctk/get-swap-slot shape)))
(let [updated-shape (ctk/set-swap-slot shape (:id near-match))]
{:result :update :updated-shape updated-shape})
{:result :keep}))
{:result :keep}))))

View File

@@ -1847,8 +1847,8 @@
(letfn [(update-shape-recursive
[container shape-id root?]
#_(when root?
(prn "Checking container:" (:id container)))
(when root?
(prn "Checking container:" (:id container)))
(if (:objects container)
(let [shape (ctsht/get-shape container shape-id)]
(if (and (ctk/instance-head? shape) (ctk/in-component-copy? shape))
@@ -1861,7 +1861,7 @@
(update-shape-recursive container child-id false))
container
(:shapes shape))))
container))
container))
(compare-slots
[container-copy container-main shape-copy shape-main]

View File

@@ -408,6 +408,27 @@
(get-ref-shape (:data component-file) component shape :with-context? with-context?))))]
(some find-ref-shape-in-head (ctn/get-parent-heads (:objects container) shape))))
(defn find-near-match
"Locate the shape that occupies the same position in the near main component.
This will be the ref-shape except if the shape is a copy subhead that has been
swapped. In this case, the near match will be the ref-shape that was before
the swap."
[file container libraries shape & {:keys [with-context?] :or {with-context? false}}]
(let [parent-shape (ctst/get-shape container (:parent-id shape))
parent-ref-shape (when parent-shape
(find-ref-shape file container libraries parent-shape :include-deleted? true :with-context? true))
ref-container (when parent-ref-shape
(:container (meta parent-ref-shape)))
shape-index (when parent-shape
(d/index-of (:shapes parent-shape) (:id shape)))
near-match-id (when (and parent-ref-shape shape-index)
(get (:shapes parent-ref-shape) shape-index))
near-match (when near-match-id
(cond-> (ctst/get-shape ref-container near-match-id)
with-context?
(with-meta (meta parent-ref-shape))))]
near-match))
(defn advance-shape-ref
"Get the shape-ref of the near main of the shape, recursively repeated as many times
as the given levels."

View File

@@ -16,8 +16,6 @@
[app.common.types.shape.layout :as ctl]
[app.common.uuid :as uuid]))
;; FIXME: the order of arguments seems arbitrary, container should be a first artgument
(defn add-shape
"Insert a shape in the tree, at the given index below the given parent or frame.
Update the parent as needed."