From 1fb421277b5fa54139aa4dc1b1eff8cf3c96bb94 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 2 Nov 2026 11:42:25 +0100 Subject: [PATCH] :bug: Order a copy grid with its own cells by the flow of those cells A copy grid that keeps its own cells held its children in its main's order, which can differ from the order its cells flow in. Updating the main from such a copy gave the main that order with the copy's cells, and a later reflow put the main back in its own flow while its copies kept the old order. A copy grid with its own cells now orders its children by the flow of its cells: sync orders it after taking them, and the grid reorder of an edit includes it, which a copy reorder has to allow. Update-main gives the main the copy's order in one change, as moving each child to its place could not be undone to the main's former order. Benchmark (scripts/bench-components, lower quantile in ms): | world | baseline | now | ratio | |------------------------------------+----------+---------+-------| | deepest-edit-through-three-levels | 35.215 | 18.54 | 0.53 | | edit-through-a-chain-of-dependents | 68.931 | 37.494 | 0.54 | | library-update-into-copies | 83.083 | 46.85 | 0.56 | | main-edit-among-many-shapes | 4.837 | 2.199 | 0.45 | | main-edit-into-10-copies | 1.79 | 0.943 | 0.53 | | main-edit-into-100-copies | 15.415 | 8.927 | 0.58 | | main-edit-into-1000-copies | 153.086 | 91.773 | 0.60 | | variant-switch-into-copies | 32.052 | 20.421 | 0.64 | AI-assisted-by: claude-opus-5-5 --- .../src/app/common/files/changes_builder.cljc | 27 ++++++++----- common/src/app/common/logic/libraries.cljc | 38 ++++++++++++++----- .../common_tests/logic/comp_sync_test.cljc | 24 ++++++++++++ .../logic/composable_gen_test.cljc | 9 +++++ 4 files changed, 79 insertions(+), 19 deletions(-) diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index ff5274563d..15d5f0baa2 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -1236,17 +1236,22 @@ new-shapes (->> (:shapes grid) (filterv #(contains? objects %))) + ;; a copy with its own cells orders its children itself + copy? (ctk/in-component-copy? grid) + redo-change - {:type :reorder-children - :parent-id (:id grid) - :page-id page-id - :shapes new-shapes} + (cond-> {:type :reorder-children + :parent-id (:id grid) + :page-id page-id + :shapes new-shapes} + copy? (assoc :allow-altering-copies true)) undo-change - {:type :reorder-children - :parent-id (:id grid) - :page-id page-id - :shapes old-shapes}] + (cond-> {:type :reorder-children + :parent-id (:id grid) + :page-id page-id + :shapes old-shapes} + copy? (assoc :allow-altering-copies true))] (-> changes (update :redo-changes conj redo-change) (update :undo-changes conj undo-change) @@ -1256,8 +1261,10 @@ (->> ids (map (d/getf objects)) (filter ctl/grid-layout?) - ;; Component sync owns copy child ordering. - (remove ctk/in-component-copy?) + ;; Component sync orders a copy that takes its main's cells; a + ;; copy with its own cells follows their flow. + (remove #(and (ctk/in-component-copy? %) + (not (ctk/touched-group? % :layout-grid-cells)))) (reduce reorder-grid changes))] changes)) diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 36c168ef77..2c781aa426 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -105,6 +105,7 @@ (declare add-shape-to-main) (declare remove-shape) (declare move-shape) +(declare reorder-main-children) (declare set-touched) (declare change-remote-synced) (declare update-attrs) @@ -647,7 +648,8 @@ the `:component` and its `:library`, the roots `:root-inst` and `:root-main`, and `:reset?`. The main shapes of the children of a copy shape that keeps its own geometry are placed like it first, so they get - their expected geometry." + their expected geometry. The children of a grid copy with its own cells + follow the flow of those cells instead of the main's order." [changes {:keys [container main-container component reset?] :as ctx} shape-inst shape-main] (shape-log :debug (:id shape-inst) container :msg "Sync shape direct recursive" @@ -659,6 +661,9 @@ (let [;; a copy shape that keeps its own geometry passes it on to its ;; children, which follow it as they follow their main's shape-before shape-inst + ;; a grid copy with its own cells orders its children as they flow + own-flow? (and (not reset?) (ctsl/grid-layout? shape-inst) + (ctk/touched-group? shape-inst :layout-grid-cells)) placed (cond (and (not reset?) (ctk/touched-group? shape-inst :geometry-group)) (in-main-frame shape-inst (:root-inst ctx) (:root-main ctx)) @@ -695,11 +700,14 @@ :only-main (fn [changes child-main] (add-shape-to-instance changes ctx shape-inst child-main (d/index-of children-main child-main)))}) - (reorder-copy-children shape-inst children-main container)) + (cond-> (not own-flow?) (reorder-copy-children shape-inst children-main container))) (ctsl/grid-layout? shape-inst) (update-grid-copy-attrs shape-before shape-main (not reset?)) + (and own-flow? (= :page (:type container))) + (pcb/reorder-grid-children [(:id shape-inst)]) + ;; cells only carry between grids, so a copy that sync leaves ;; without one drops its own (and (not (ctsl/grid-layout? shape-inst)) (seq (:layout-grid-cells shape-inst))) @@ -862,17 +870,29 @@ (d/index-of children-inst child-inst))) :only-main (fn [changes child-main] (remove-shape changes child-main main-container)) - :moved (fn [changes child-inst child-main] - (move-shape changes - child-main - (d/index-of children-main child-main) - (d/index-of children-inst child-inst) - main-container)) - :inverse? true})) + :inverse? true}) + (reorder-main-children shape-main children-inst @added main-container)) (ctsl/grid-layout? shape-main) (update-grid-main-attrs shape-main shape-inst children-inst @added main-container))))) +(defn- reorder-main-children + "Order the children of `shape-main` as the copy's `children-inst`, each by + the main shape it pairs with or the one `added` for it, in one change that + its undo reverts." + [changes shape-main children-inst added main-container] + (let [order (into [] (keep #(get added (:id %) (:shape-ref %))) children-inst) + reorder (fn [shapes] + {:type :reorder-children + :parent-id (:id shape-main) + :shapes shapes + :allow-altering-copies true})] + (if (= order (vec (:shapes shape-main))) + changes + (pcb/add-changes changes main-container + [(reorder order)] + [(reorder (vec (:shapes shape-main)))])))) + (defn- mark-local-changes "Mark as `:local-change?` the changes of the copy shapes `ids`. The inverse sync may update a component of a remote library, so the frontend commits diff --git a/common/test/common_tests/logic/comp_sync_test.cljc b/common/test/common_tests/logic/comp_sync_test.cljc index f59550cdb6..89499aacba 100644 --- a/common/test/common_tests/logic/comp_sync_test.cljc +++ b/common/test/common_tests/logic/comp_sync_test.cljc @@ -1157,6 +1157,30 @@ (ch/components-changed (:data file) (change {:type :assign :value {:opacity 0.5}})))) (t/is (nil? (ch/components-changed (:data file) (change {:type :assign :value {:interactions []}})))))) +(t/deftest a-copy-grid-with-its-own-cells-orders-its-children-by-their-flow + (let [page-id (thi/new-id! :page) + grid {:id (thi/new-id! :grid) :type :frame :layout :grid :layout-grid-dir :row + :shape-ref (thi/new-id! :main-grid) :shapes [(thi/new-id! :a) (thi/new-id! :b)] + :layout-grid-cells + {(thi/new-id! :cell-a) {:id (thi/id :cell-a) :row 1 :column 1 :row-span 1 :column-span 1 + :position :auto :shapes [(thi/id :a)]} + (thi/new-id! :cell-b) {:id (thi/id :cell-b) :row 1 :column 2 :row-span 1 :column-span 1 + :position :auto :shapes [(thi/id :b)]}}} + objects (fn [grid] + {(:id grid) grid + (thi/id :a) {:id (thi/id :a) :type :rect :parent-id (:id grid) :shape-ref (thi/new-id! :main-a)} + (thi/id :b) {:id (thi/id :b) :type :rect :parent-id (:id grid) :shape-ref (thi/new-id! :main-b)}}) + order (fn [grid] + (->> (-> (pcb/empty-changes nil page-id) + (pcb/with-objects (objects grid)) + (pcb/reorder-grid-children [(:id grid)])) + :redo-changes + (some #(when (= :reorder-children (:type %)) (:shapes %)))))] + (t/testing "a copy grid with its own cells follows their flow" + (t/is (= [(thi/id :b) (thi/id :a)] (order (assoc grid :touched #{:layout-grid-cells}))))) + (t/testing "a copy grid that takes its main's cells keeps the order sync gives" + (t/is (nil? (order grid)))))) + (t/deftest restoring-a-variant-without-its-container-syncs-the-new-main-name (let [initial (tm/apply-to (n/make-variant-container "restore" [["b" "#ffffff"]]) (setup/empty-situation)) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 18621b92a2..d5a72a0063 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -987,3 +987,12 @@ (t/is (map? (cg/run {:world [2 0 {:text true :text-styles true :layout :grid :grid-empty true}] :ops [[:resize 0 0 0 0] [:nest 20 0 0 0] [:style 9 9 1] [:edit 20 1 1 :copy-head] [:reset 20]]})))) + +(t/deftest generated-copy-grid-with-its-own-cells-follows-their-flow + ;; a copy with its own cells, pushed to its main grid flowing by columns, + ;; orders the children of both as those cells flow + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout :grid :variant-diff false + :grid-square true :rich-variants true :layout-children true + :layout-copies true :text true}] + :ops [[:grid-direction 0 0 0 1] [:grid-cell 0 0 0 1] [:grid-direction 0 0 15 0] + [:edit 3 0 1 1] [:update-main 3] [:style 1 11 0 0]]}))))