diff --git a/frontend/src/app/main/data/workspace/undo.cljs b/frontend/src/app/main/data/workspace/undo.cljs index 9eff6e7260..e461782f8d 100644 --- a/frontend/src/app/main/data/workspace/undo.cljs +++ b/frontend/src/app/main/data/workspace/undo.cljs @@ -199,9 +199,13 @@ (take (inc index) (get-in state [:workspace-undo :items])))))] (cond ;; A delayed derived change still belongs to its primary action, - ;; even after another edit or transaction has started. + ;; even after another edit or transaction has started. An entry + ;; that holds the group only among the groups stacked into it + ;; takes the change too: undo walks entries by their own group. (and (some? group-index) - (or stack? (< group-index index) transaction)) + (or stack? (< group-index index) transaction + (not= (:undo-group entry) + (get-in state [:workspace-undo :items group-index :undo-group])))) (stack-undo-entry state entry group-index) (and (get-in state [:workspace-undo :transaction]) diff --git a/frontend/test/frontend_tests/composable_tests/generated_test.cljs b/frontend/test/frontend_tests/composable_tests/generated_test.cljs index adf5f5eaa4..b998104c80 100644 --- a/frontend/test/frontend_tests/composable_tests/generated_test.cljs +++ b/frontend/test/frontend_tests/composable_tests/generated_test.cljs @@ -1118,6 +1118,12 @@ :ops [[:rotate 16 0 7 0] [:grid-direction 0 0 0 1] [:grid-cell 0 10 0 1] [:add-child 0] [:structure 3 6 15] [:grid-direction 0 0 0 1] [:grid-cell 0 4 2 1]]}))) +(t/deftest ^:async generated-an-ungroup-in-a-grid-main-undoes-in-one-step + ;; the copies' repositioning, synced from the layout update after the + ;; ungroup, undoes with the ungroup + (await (replay {:world [2 2 {:variants true :text true :layout :grid :layout-copies true}] + :ops [[:detach 6 0 0 0] [:structure 0 2 0]]}))) + (t/deftest ^:async generated-a-detach-under-an-unaccepted-child-removal-leaves-it-plain ;; the loaded library no longer has the nested copy's child main, so the ;; detach leaves that child plain until the consumer accepts the library diff --git a/frontend/test/frontend_tests/data/workspace_undo_test.cljs b/frontend/test/frontend_tests/data/workspace_undo_test.cljs index d9bf224d7a..87bcfe763e 100644 --- a/frontend/test/frontend_tests/data/workspace_undo_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_undo_test.cljs @@ -159,6 +159,33 @@ (t/is (= 1 (count (get-in state [:workspace-undo :items])))) (t/is (= #{g1 g2} (get-in state [:workspace-undo :items 0 :undo-groups]))))) +;; Scenario: an ungroup commits (g1), the layout update after it stacks onto +;; its entry with a group of its own (g2), and the component sync that update +;; causes commits later under g2 without stacking. Proves: the sync joins the +;; same entry, so one Ctrl+Z undoes all three; a separate entry under g2 +;; would be undone alone, as undo walks entries by their own group. +(t/deftest a-delayed-commit-joins-the-entry-its-group-was-stacked-into + (let [page-id (uuid/next) + [g1 g2] (repeatedly 2 uuid/next) + state (reduce #(ptk/update %2 %1) + {} + [(dwu/append-undo (entry page-id "A" g1) false) + (dwu/append-undo (entry page-id "B" g2) true) + (dwu/append-undo (entry page-id "C" g2) false)])] + (t/is (= 1 (count (get-in state [:workspace-undo :items])))) + (t/is (= ["A" "B" "C"] (mapv :name (get-in state [:workspace-undo :items 0 :redo-changes])))))) + +;; Scenario: two commits of one group in a row, without stacking. Proves: +;; they keep separate entries, which undo walks together by their group. +(t/deftest adjacent-commits-of-one-group-keep-their-entries + (let [page-id (uuid/next) + g1 (uuid/next) + state (reduce #(ptk/update %2 %1) + {} + [(dwu/append-undo (entry page-id "A" g1) false) + (dwu/append-undo (entry page-id "B" g1) false)])] + (t/is (= 2 (count (get-in state [:workspace-undo :items])))))) + ;; Scenario: the user presses Ctrl+Z on a page rename. Proves: the commit it ;; makes can be undone (its undo changes redo the rename) and names the index ;; the history moved from, so a rollback can put both back.