Repository navigation
fix(undo): restore moves, deletes, tags and properties - #13444
Conversation
|
Review pass on the undo fixes — the approach looks sound overall. Verified: One real gap and one nit — I couldn't push to the fork branch (no write access), so here's a patch:
diff --git a/deps/outliner/src/logseq/outliner/core.cljs b/deps/outliner/src/logseq/outliner/core.cljs
--- a/deps/outliner/src/logseq/outliner/core.cljs
+++ b/deps/outliner/src/logseq/outliner/core.cljs
@@ -631,21 +631,22 @@
(if (and keep-block-order? (every? :block/order blocks))
- (if (every? at-target? (map :block/order (filter top-level? blocks)))
- (map :block/order blocks)
- ;; The kept orders of the top-level blocks no longer fall next to the
- ;; target, e.g. undo restoring deleted blocks after a sibling moved
- ;; away and back got a new order: order them at the target and keep
- ;; the orders of their children.
- (let [top-level-orders (db-order/gen-n-keys (count (filter top-level? blocks))
- start-order end-order)]
- (first
- (reduce (fn [[orders top-level-orders] block]
- (if (top-level? block)
- [(conj orders (first top-level-orders)) (rest top-level-orders)]
- [(conj orders (:block/order block)) top-level-orders]))
- [[] top-level-orders]
- blocks))))
+ (let [top-level-blocks (filter top-level? blocks)]
+ (if (every? at-target? (map :block/order top-level-blocks))
+ (map :block/order blocks)
+ ;; The kept orders of the top-level blocks no longer fall next to the
+ ;; target, e.g. undo restoring deleted blocks after a sibling moved
+ ;; away and back got a new order: order them at the target and keep
+ ;; the orders of their children.
+ (let [top-level-orders (db-order/gen-n-keys (count top-level-blocks)
+ start-order end-order)]
+ (first
+ (reduce (fn [[orders top-level-orders] block]
+ (if (top-level? block)
+ [(conj orders (first top-level-orders)) (rest top-level-orders)]
+ [(conj orders (:block/order block)) top-level-orders]))
+ [[] top-level-orders]
+ blocks)))))
(db-order/gen-n-keys (count blocks) start-order end-order))))
diff --git a/src/main/frontend/worker/sync/apply_txs.cljs b/src/main/frontend/worker/sync/apply_txs.cljs
--- a/src/main/frontend/worker/sync/apply_txs.cljs
+++ b/src/main/frontend/worker/sync/apply_txs.cljs
@@ -1526,10 +1526,12 @@
page-uuid (:uuid opts)
existing-page (or (when (uuid? page-uuid)
(d/entity @conn [:block/uuid page-uuid]))
- ;; A page and a tag can share a title; only a
- ;; page of the kind being created is this page.
+ ;; A page, a tag or a property can share a title;
+ ;; only an entity of the kind being created is
+ ;; this page.
(let [page (ldb/get-page @conn title)]
- (when (= (boolean (:class? opts)) (boolean (ldb/class? page)))
+ (when (and (not (ldb/property? page))
+ (= (boolean (:class? opts)) (boolean (ldb/class? page))))
page)))]clj-kondo clean on all touched files; both changes are in the patch above if you want to apply them ( |
|
The red-flag case does not reproduce on 478e2f8: with |
Moving up or down a selection whose blocks are not adjacent siblings, such as a top-level block and a nested block picked with Ctrl+click, or 2 siblings with a block between them, and then undoing did not put the blocks back. After a move down of a top-level block and a nested block, for example, the top-level block ended up inside the nested block's parent. The inverse of move-blocks-up-down was a move of the same blocks in the other direction. That only reverses a move of adjacent siblings: moving blocks that are not adjacent siblings gathers them next to each other, so moving them back cannot return each to its own place. For such a selection the inverse is now a move of each block back to its left sibling or parent, the inverse move-blocks already uses. A selection of adjacent siblings keeps the opposite move. Test: undo-move-down-of-blocks-at-different-levels-test, undo-move-up-of-blocks-at-different-levels-test and undo-move-up-of-siblings-that-are-not-adjacent-test. Each fails on master.
Moving a block together with its grandchild picked by Ctrl+click, or 2 blocks picked bottom one first, and then undoing left a block on the destination page: the grandchild, or the upper of the 2 blocks. move-blocks moves each selected block whose parent is not selected (filter-top-level-blocks), so a grandchild leaves its grandparent's subtree, and it moves them top first. The inverse restored only the selected blocks without any selected ancestor, which drops that grandchild, and restored them in selection order. A block restored next to its left sibling before that sibling came back went to the destination page, where the sibling still was. The inverse now restores each selected block whose parent is not selected, in page order, so each restore target is back in place first. Test: undo-move-of-block-and-its-grandchild-restores-both-test and undo-move-of-blocks-selected-bottom-up-restores-both-test. Each fails on master.
Deleting blocks, moving the block that followed them to another page, and then undoing both put the deleted blocks back below that block instead of above it. Undo of a delete inserts the blocks at their old left sibling or parent with their old :block/order values (keep-block-order?). Undo of a move puts a block back at its old left sibling or parent under a new order value. The block that followed the deleted ones came back as the first child with a new order that sorts before the old orders of the deleted blocks, so those blocks, inserted as the first children, sorted after it. insert-blocks now keeps the old order of a top-level block only while that order falls between the target and the block after it. Otherwise it orders the top-level blocks at the target and keeps the orders of their children. Only undo sets keep-block-order?, to put deleted blocks back. Test: undo-delete-after-undoing-move-of-block-left-behind-test. It fails on master.
Renaming a tag to the title of an existing page, deleting the tag, and then undoing both lost the tag for good: both undos were skipped. Deleting a tag retracts it, so undo replays a :create-page op with the tag's uuid and :class? true, then a save of its attributes. The replay of :create-page reuses an existing page with the same title when no entity has the uuid, and ldb/get-page found the ordinary page that owns the title. No tag was created, the save failed on the missing uuid, and both history actions were dropped as invalid. The title fallback now reuses only a page of the kind being created: a tag for a tag, a page that is not a tag otherwise. Test: undo-delete-of-tag-renamed-to-page-title-restores-tag-test. It fails on master.
Setting a property on the property's own page, deleting the property, and then undoing lost the property for good, and the undo history was cleared. Deleting a property retracts it, together with the value blocks on its page. Undo recreates the property, saves its attributes, then inserts its blocks. The attributes include the property's value on its own page, a reference to one of those blocks, so the save failed on an entity that did not exist yet. The failure is not skippable, so undo cleared the whole history. The inverse of deleting a tag or property page now inserts the page's blocks before it saves the page's attributes. Inserting a property value block already sets the owner's value. Test: undo-delete-of-property-valued-on-itself-restores-property-test. It fails on master.
Moving the last children of a block down puts them into the next block, and moving the first children of a block up puts them into the previous block. When that block embeds another block, the children go under the embedded block, and the opposite move left them there. The inverse now uses the opposite move only when a sibling on the side of the move keeps the blocks under their parent. Otherwise build-inverse-move-blocks restores each block to its old left sibling or parent. The 2 new tests without an embed pass before this change too. They cover the case the review raised, where the opposite move does restore the blocks.
Replaying :create-page when the page's uuid is gone looks the page up by title, and ldb/get-page can return a property with that title. The replay then returned the property's uuid and created no page. The title lookup now skips properties, as outliner-page/create! reuses only an entity of the kind it creates. get-block-orders now filters the top-level blocks once. The change is the patch from the review bot's comment on the pull request of 2026-09-26, applied as posted. The comment carries no author header. The test is new.
465488e to
c99732e
Compare
tiensonqin
left a comment
There was a problem hiding this comment.
QAed all cases and they all worked great!
Thanks for this amazing PR 🚢 👍
What changes
Five undo bugs, each with a regression test in
frontend.worker.undo-redo-testthat fails on master. A property-based test found them: random sequences of outliner, page, tag and property operations applied throughoutliner-op/apply-ops!with the worker's undo history recording, each operation undone one at a time and compared with the graph before it, then shrunk to the shortest failing sequence. Review found a sixth, moving blocks up or down into an embed, and a property case of the tag fix.Graph in the steps below: page 1 holds a, b (children b1, b2), c. Page 2 holds d. Selections of blocks that are not neighbors come from Ctrl/Cmd+click, which adds 1 block at a time in click order.
Moving blocks that are not adjacent siblings up or down
Steps: Ctrl+click a, then b1. Press Alt+Shift+Down. Undo. The page reads b (a, b1, b2), c: a stays inside b. The same happens for 2 siblings with a block between them (a and c, moved up: undo gives b, a, c).
Cause: the inverse of
:move-blocks-up-downwas the same move in the other direction (construct.cljc). Moving blocks that are not adjacent siblings gathers them next to each other, so the opposite move cannot put each back.Fix: for such a selection the inverse is
build-inverse-move-blocks, which moves each block back to its old left sibling or parent, as the inverse of:move-blocksalready does. A selection of adjacent siblings keeps the opposite move while it stays under its parent (see the embed case below).Moving blocks up or down into an embed
Steps: paste an embed of d as the block after b. Select b1 and b2 and press Alt+Shift+Down. They go under d on page 2. Undo: b1 and b2 stay under d. The same happens when the embed of d is the block before b and b1, b2 move up.
Cause: moving the last children of a block down puts them into the next block, and moving the first children up puts them into the previous block. When that block is an embed, they go under the embedded block. The opposite move brings them back from the next or previous block, but not from the embedded block.
Fix: the opposite move is the inverse only when a sibling on the side of the move keeps the blocks under their parent. Every other up/down move is undone with
build-inverse-move-blocks.Moving a block together with its grandchild, or a selection made bottom-up
Steps: drag b into a. Ctrl+click a, then b2 (now a's grandchild). Move them to page 2. Undo twice. b2 stays on page 2. The same happens when b2 and b1 are selected bottom-up (b2 first) and moved: after undo, b2 stays on page 2.
Cause:
move-blocksmoves every selected block whose parent is not selected (filter-top-level-blocks), so the grandchild moves on its own. The inverse took its blocks fromselected-block-roots, which drops every block with any selected ancestor, so the grandchild got no restore op. The inverse also restored blocks in selection order: in the bottom-up case b2 was restored next to its old left sibling b1 while b1 was still on page 2.Fix: for moves,
selected-block-rootskeeps the blocksfilter-top-level-blockskeeps (option:direct-parent?), andbuild-inverse-move-blocksrestores them in page order, so a block's left sibling or parent is back before the block. The delete inverse keeps the ancestor rule, since restoring a deleted block restores its subtree.Deleting blocks, then moving the block after them
Steps: select a and b, delete. Move c to page 2. Undo twice. Page 1 reads c, a, b.
Cause: the undo of the delete reinserts a and b with their old
:block/ordervalues (:keep-block-order?), andget-block-orderstook those as they were. The undo of the move had given c a new order fromgen-keyon the then empty page 1, and that key sorts before the old keys of a and b.Fix:
get-block-orderskeeps the old order of a top-level block only while it falls between the target and the next block. Otherwise the top-level blocks get new orders at the target, and their children keep theirs. Only undo sets:keep-block-order?.Deleting a tag renamed to a page's title
Steps: rename the tag "Topic" to "page 1" (a page "page 1" exists; a tag may share a page's title). Delete the tag. Undo twice. The tag is gone for good, and both undos are skipped without a message.
Cause: the undo of the delete replays
:create-page ["page 1" {:uuid <tag uuid> :class? true}]. The replay (apply_txs.cljs) looks the page up by uuid, then by title, finds the ordinary page "page 1", and creates nothing. The following:save-blockof the tag finds no entity and raises "invalid rebase op", which undo skips.Fix: the title fallback reuses only a page of the kind being created, a tag for
:class? trueand a non-tag page otherwise. A property with the title is never reused (patch from the review), since otherwise replaying the create of an ordinary page, whose uuid is gone, over an older property with the same title returned the property's uuid and created no page.Deleting a property that has a value on its own page
Steps: set the number property "rating" on the rating property's own page, delete the property, undo twice. The property is gone for good, and the undo history is cleared.
Cause: a number value is a block whose parent is the page holding the value, here the property's own page. The inverse of the delete saved the page's attributes, which refer to that value block, before it reinserted the page's blocks, so the save threw "Nothing found for entity id". The reason became
:error, and undo cleared the history.Fix: the inverse reinserts the page's blocks before it saves the page's attributes.
Tests
frontend.worker.undo-redo-test: 8 new tests, each failing on master (17 assertions):undo-move-down-of-blocks-at-different-levels-test,undo-move-up-of-blocks-at-different-levels-test,undo-move-up-of-siblings-that-are-not-adjacent-test,undo-move-of-block-and-its-grandchild-restores-both-test,undo-move-of-blocks-selected-bottom-up-restores-both-test,undo-delete-after-undoing-move-of-block-left-behind-test,undo-delete-of-tag-renamed-to-page-title-restores-tag-test,undo-delete-of-property-valued-on-itself-restores-property-test.frontend.worker.undo-redo-test, added after review: 3 tests that fail on 478e2f8 (12 assertions):undo-move-down-of-last-children-into-next-embed-test,undo-move-up-of-first-children-into-previous-embed-test,replay-create-page-titled-like-property-creates-page-test. 2 tests that pass on 478e2f8 and cover the case the review raised, blocks moved into the next or previous block and back (6 assertions):undo-move-down-of-last-children-into-next-block-test,undo-move-up-of-first-children-into-previous-block-test. The namespace: 42 tests, 218 assertions, 0 failures.TZ=Etc/GMT+12andTZ=Pacific/Kiritimati:frontend.worker.undo-redo-test,frontend.undo-redo-test,logseq.outliner.op-construct-testandfrontend.modules.outliner.core-test: 0 failures.TZ=Etc/GMT+12andTZ=Pacific/Kiritimati:frontend.worker.undo-redo-test,frontend.undo-redo-test,frontend.worker.db-sync-test,frontend.worker.db-sync-sim-test,logseq.outliner.op-construct-test,logseq.outliner.move-property-value-undo-test,frontend.modules.outliner.core-testand the other outliner namespaces the commit touches: 0 failures beyond those that fail on master.TZ=Etc/GMT+12and once withTZ=Pacific/Kiritimati: 2484 tests, 12440 assertions each. The only failures are the zone-dependent tests that fix(test): build test dates in local time #13430 fixes (6 assertions at UTC-12, 4 at UTC+14), and at UTC-12 once the CPU-time bound ofsync-search-indice-300-new-blocks-performance-when-semantic-search-enabled, which passed when run again.Allow edits from maintainers.