From 3611f2d4f637333482a75fc9e7d46fca3684d3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Thu, 19 Mar 2026 12:27:08 +0100 Subject: [PATCH] wip --- .../src/app/common/files/comp_processors.cljc | 24 +++++++++---------- common/src/app/common/files/migrations.cljc | 6 ++--- common/src/app/common/types/file.cljc | 21 ++++++++++++++++ common/src/app/common/types/shape_tree.cljc | 2 -- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/common/src/app/common/files/comp_processors.cljc b/common/src/app/common/files/comp_processors.cljc index 7d9402a280..b37510f2bc 100644 --- a/common/src/app/common/files/comp_processors.cljc +++ b/common/src/app/common/files/comp_processors.cljc @@ -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})))) + diff --git a/common/src/app/common/files/migrations.cljc b/common/src/app/common/files/migrations.cljc index e719452d8d..da98636aa8 100644 --- a/common/src/app/common/files/migrations.cljc +++ b/common/src/app/common/files/migrations.cljc @@ -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] diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 00bd787bd1..0d4af5270e 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -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." diff --git a/common/src/app/common/types/shape_tree.cljc b/common/src/app/common/types/shape_tree.cljc index 92732e18a1..3944d96afb 100644 --- a/common/src/app/common/types/shape_tree.cljc +++ b/common/src/app/common/types/shape_tree.cljc @@ -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."