🐛 Sync copies when a copy nested in a main is moved

The component watcher drops a translation commit that moves only shapes
outside mains or whole mains. It found the main of each moved shape with
`get-component-shape`, which stops at the nearest copy head, so a copy
placed in a main, or a shape inside one, looked like a shape outside any
main: moving it with the WASM engine on, whose drags commit as
translations, left the main's copies where they were.

The watcher now walks up through nested copies to the main instance the
shape lies in.

The benchmark covers JVM code; this change is ClojureScript only, so it
is not run.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-14 15:20:47 +01:00
parent 700245059b
commit ac31d67366
2 changed files with 23 additions and 3 deletions

View File

@ -1515,6 +1515,16 @@
(component-sync-events state component-id file-id undo-group)
[(dwu/commit-undo-transaction undo-id)]))))))
(defn- main-ancestor
"The main instance `shape` is or lies inside, through any copies nested in
it, or nil."
[objects shape]
(loop [shape shape]
(cond
(or (nil? shape) (cfh/root? shape)) nil
(ctk/main-instance? shape) shape
:else (recur (get objects (:parent-id shape))))))
(defn- moves-whole-mains?
"Whether the `changes` of a translation commit, applied on the file
`data`, move only shapes outside mains, or whole mains: every shape they
@ -1524,9 +1534,9 @@
(let [objects (dm/get-in data [:pages-index page-id :objects])
ids (into #{} (map :id) changes)]
(every? (fn [id]
(let [root (ctn/get-component-shape objects (get objects id) {:allow-main? true})]
(or (not (ctk/main-instance? root))
(contains? ids (:id root)))))
(let [main (main-ancestor objects (get objects id))]
(or (nil? main)
(contains? ids (:id main)))))
ids)))
(group-by :page-id (filter #(= :mod-obj (:type %)) changes))))

View File

@ -111,6 +111,16 @@
(t/is open?)
(t/is (= red (ftm/fill-of :copy-child)))))))
(t/deftest translation-of-a-copy-nested-in-a-main-syncs-the-copies
;; a copy placed in a main is part of it: moving it is an edit of the main
(t/async done
(ftm/install! (setup/nested-component-with-copy))
(st/emit! (ftm/fill-commit :main2-nested-head red {:translation? true}))
(-> (pwrf/wait-for-layout-update nil 2000)
(.then #(t/is (= red (ftm/fill-of :copy2-child))))
(.catch #(t/is false (str "the wait threw: " %)))
(.finally done))))
(t/deftest derived-commit-leaves-nothing-pending
(t/async done
(run-commit done {:skip-component-sync? true} dropped)))