diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 8730d94018..ab89c011c3 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -1318,22 +1318,63 @@ (map set-link updated-shapes) (map #(set-link (ctn/get-shape container (:id %))) updated-shapes))))) +(defn- position-in-batch + "The position of the shape `id` of `container` among the children of its + parent, as the changes in `changes` so far leave them." + [changes container id] + (let [objects (:objects container) + parent-id (:parent-id (get objects id)) + page? (cfh/page? container) + ;; a change on the objects of `container` + in-container? (fn [change] + (= (:id container) (if page? (:page-id change) (:component-id change)))) + insert (fn [shapes index ids] + (if (some? index) + (d/insert-at-index shapes index ids) + (into (filterv (complement (set ids)) shapes) ids))) + children (reduce (fn [shapes change] + (if-not (in-container? change) + shapes + (case (:type change) + :del-obj (filterv #(not= % (:id change)) shapes) + :add-obj (cond-> shapes + (= parent-id (:parent-id change)) + (insert (:index change) [(:id change)])) + :mov-objects (let [moved (:shapes change)] + (if (= parent-id (:parent-id change)) + (insert shapes (:index change) moved) + (filterv (complement (set moved)) shapes))) + :reorder-children (cond-> shapes + (= parent-id (:parent-id change)) + (-> (:shapes change) (vec))) + shapes))) + (vec (get-in objects [parent-id :shapes])) + (:redo-changes changes))] + (d/index-of children id))) + (defn- remove-shape + "Delete `shape` of `container` with its children. Each undo restores a + shape at the position it had when the changes so far deleted it, so + siblings that one batch deletes come back in their order." [changes shape container] (shape-log :info (:id shape) container :msg (str "REMOVE-SHAPE " (shape-label shape container))) (let [objects (get container :objects) parents (cfh/get-parent-ids objects (:id shape)) ;; from the bottom up - ids (reverse (into [(:id shape)] (cfh/get-children-ids objects (:id shape))))] + ids (reverse (into [(:id shape)] (cfh/get-children-ids objects (:id shape)))) + ;; the builder's objects, when it applies its changes as it goes + local (pcb/get-objects changes)] (pcb/add-changes changes container (conj (mapv del-obj-change ids) {:type :reg-objects :shapes (vec parents)}) ;; the parents are recalculated after the shapes are restored (cons {:type :reg-objects :shapes (vec parents)} (map #(add-obj-change (get objects %) - (or (cfh/get-position-on-parent (pcb/get-objects changes) %) - (cfh/get-position-on-parent objects %))) + (or (cfh/get-position-on-parent local %) + (if (= % (:id shape)) + (position-in-batch changes container %) + (cfh/get-position-on-parent objects %)))) ids))))) (defn- longest-increasing diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 399a74bc9b..2b4fdd14bb 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1998,6 +1998,13 @@ :ops [[:grid-direction 11 0 0 1] [:grid-cell 11 0 0 1] [:swap 16 0 0 0] [:make-variant 4] [:switch 10] [:add-variant 4] [:switch 9]]})))) +(t/deftest undoing-an-update-main-restores-the-main-children-it-replaced-in-order + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:duplicate 0 0 0 0] [:duplicate 0 0 0 0] [:variant-property 0 0 0 0] [:switch 12] + [:duplicate 16 0 0 0] [:make-variant 0] [:switch 12] [:update-main 0 0 0 0]]})))) + (t/deftest turning-a-flipped-copy-turns-a-child-stored-as-the-other-mirror-turn (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false :layout-item-margins false :grid-square false :rich-variants true diff --git a/common/test/common_tests/logic/swap_group_test.cljc b/common/test/common_tests/logic/swap_group_test.cljc index 6fdb8fada7..2dcbf65a88 100644 --- a/common/test/common_tests/logic/swap_group_test.cljc +++ b/common/test/common_tests/logic/swap_group_test.cljc @@ -512,6 +512,36 @@ (t/is (= (:id (thc/get-component file :heart)) (:component-id other))) (t/is (= "#222222" (tho/bottom-fill-color file :copy2))))) +(t/deftest undoing-an-update-main-that-replaces-two-children-keeps-their-order + ;; {:row-main} [:name Frame1] # [Component :row] + ;; :row-s1..:row-s4 [:name Frame1] @--> [Component :star] :star-main + ;; :copy [:name Frame1] #--> [Component :row] :row-main + ;; :copy-s1, :copy-s2 @--> [Component :heart] (swapped) + ;; :copy-s3, :copy-s4 @--> [Component :star] + (let [file (-> (thf/sample-file :file1) + (tho/add-simple-component :star :star-main :star-rect) + (tho/add-simple-component :heart :heart-main :heart-rect) + (tho/add-frame :row-main) + (thc/instantiate-component :star :row-s1 :parent-label :row-main) + (thc/instantiate-component :star :row-s2 :parent-label :row-main) + (thc/instantiate-component :star :row-s3 :parent-label :row-main) + (thc/instantiate-component :star :row-s4 :parent-label :row-main) + (thc/make-component :row :row-main) + (thc/instantiate-component :row :copy + :children-labels [:copy-s1 :copy-s2 :copy-s3 :copy-s4]) + (tho/swap-component-in-shape :copy-s1 :heart) + (tho/swap-component-in-shape :copy-s2 :heart)) + page (thf/current-page file) + container (ctn/make-container page :page) + changes (-> (pcb/empty-changes) + (pcb/with-container container) + (cll/generate-sync-shape-inverse {(:id file) file} container (thi/id :copy))) + updated (thf/apply-changes file changes :validate? false) + undone (thf/apply-undo-changes updated changes)] + (t/is (= 4 (count (:shapes (ths/get-shape updated :row-main))))) + (t/is (= (:shapes (ths/get-shape file :row-main)) + (:shapes (ths/get-shape undone :row-main)))))) + (t/deftest update-main-keeps-a-deeper-swap-as-a-swap (let [file (-> (setup-two-levels) (update-main :outer-copy :page))