Skip to content

fix(editor): keep pending new block on fast backspace - #14024

Merged
tiensonqin merged 2 commits into
masterfrom
logseq/fix-pending-backspace-before-landing-a42a
Nov 16, 2026
Merged

tiensonqin merged 2 commits into
masterfrom
logseq/fix-pending-backspace-before-landing-a42a

Conversation

@tiensonqin

@tiensonqin tiensonqin commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes fast Enter/Backspace on an empty block while the first Enter's insert is still pending in the db worker. Backspace was reading caret 0 from the source textarea and marking the in-flight block for deletion, so typed letters were lost and the renderer often crashed.

logseq/db-test#1535

Before

On an empty block, keys sent with no pause (insert still pending, ~40–60 ms on a small graph):

Keys Expected Actual
Enter, Enter, Backspace "", "" ""
Enter, x, y, Backspace "", x ""

The deleted in-flight block also tripped Missing renderer resource entity and showed the error screen:

Before: fast Enter, Enter, Backspace crashes the page after deleting the new block

After

The same sequences keep the new block. Backspace before the editor has moved onto it trims buffered text (or is a no-op when there is nothing to trim). After the editor has actually moved onto the new block, caret 0 still deletes it.

After: Enter, Enter, Backspace leaves two empty blocks

After: Enter, x, y, Backspace leaves an empty block and a block containing x

Change

keydown-backspace-handler used (zero? (util/get-selection-start (.-target e))) as delete-block? for the whole pending window. Until inserted-block-edit-fn focuses the new block, .-target is still the source textarea, and an empty source always reports caret 0.

  • Record the source block uuid when the pending insert starts.
  • Treat caret 0 as a delete only when the current edit block uuid differs from that source (the editor has actually moved). That also covers a queued Tab indent, which still awaits the worker while focus remains on the source.
  • If the buffer is empty and the editor has not moved, Backspace is a no-op. A second Enter while pending is still dropped, matching current master.

Related PRs

Does not depend on #13782 or #14006. #13782 queues a second Enter and lets Backspace undo that queue; the empty-buffer no-op here is compatible with that. #14006 serializes undo/redo around pending inserts and does not change this caret check.

Tests

  • Unit tests for pending-new-block backspace state, including “still on the source uuid during a queued Tab”
  • clj-e2e: fast-enter-enter-backspace-keeps-the-new-empty-block-test, fast-enter-xy-backspace-keeps-typed-text-on-the-new-block-test, and fast-enter-tab-backspace-keeps-the-indented-block-test fire the sequences in one JS turn
  • Existing pending-block e2e tests still pass, including backspace-at-start-removes-pending-block-dom-test and rapid Enter then Tab

To show artifacts inline, enable in settings.

Open in Web Open in Cursor 

Devin Review

Backspace while an Enter insert is still pending used the source
textarea caret. On an empty source that caret is always 0, so the
landing callback deleted the new block and dropped typed text.

Only treat caret 0 as a delete after the editor has moved onto the
new block. Before that, Backspace trims buffered text and is a no-op
when the buffer is empty.

Fixes logseq/db-test#1535

Co-authored-by: Tienson Qin <[email protected]>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Devin Review

Comment thread src/main/frontend/handler/editor.cljs Outdated
(defn- inserted-block-edit-fn
[source-block block container-id]
(fn [rows]
(mark-pending-new-block-editor-landed!)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Queued indentation deletes the pending block

After Enter and Tab, :editor-landed? becomes true before queued indentation finishes. Backspace then reads the source textarea's zero caret and deletes the new block.

Learn more

The insertion callback marks :editor-landed? before entering its promise chain. When Tab was queued, indent-outdent-blocks! starts a separate worker mutation that the callback awaits before calling edit-block!. During that wait the source textarea still receives keys. The new predicate treats its caret at zero as a request to delete the inserted block, undoing the protection for fast Backspace.

Example: Start on an empty block. Press Enter, Tab, then Backspace while indentation is pending. The source editor has caret 0, so Backspace sets :delete?; when indentation returns, the insertion callback switches back and deletes the new block instead of retaining it.

Recommended fix: Set the landed flag only when the new textarea actually receives focus, after any queued indent/outdent operation. Alternatively, verify that the keydown target belongs to the inserted block before interpreting caret zero as a deletion request.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

An :editor-landed? flag set at the start of the insert callback was
true while a queued Tab indent still awaited the worker, so the
source textarea's caret 0 could be treated as a delete.

Record the source block uuid at Enter and delete on Backspace only
when the editor has actually moved onto another block with caret 0.

Co-authored-by: Tienson Qin <[email protected]>
@tiensonqin
tiensonqin merged commit ec3e95b into master Nov 16, 2026
16 checks passed
@tiensonqin
tiensonqin deleted the logseq/fix-pending-backspace-before-landing-a42a branch November 16, 2026 09:54
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 16, 2026
…d, as master pins

Reverts 1f2f44f ("an Enter pressed while the previous Enter's insert runs
is queued, not dropped"). Master's logseq#14024 (logseq/db-test#1535) pins the
opposite in enter-while-insert-is-pending-is-dropped-test: a second Enter
during a pending insert starts no insert and is not queued, so Backspace
cannot undo it. Master's behaviour wins; the keys typed during a pending
Enter still land on the new block, and master's source-block check decides
when Backspace deletes it.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 16, 2026
…d, as master pins

Reverts 1f2f44f ("an Enter pressed while the previous Enter's insert runs
is queued, not dropped"). Master's logseq#14024 (logseq/db-test#1535) pins the
opposite in enter-while-insert-is-pending-is-dropped-test: a second Enter
during a pending insert starts no insert and is not queued, so Backspace
cannot undo it. Master's behaviour wins; the keys typed during a pending
Enter still land on the new block, and master's source-block check decides
when Backspace deletes it.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 17, 2026
…d, as master pins

Reverts 1f2f44f ("an Enter pressed while the previous Enter's insert runs
is queued, not dropped"). Master's logseq#14024 (logseq/db-test#1535) pins the
opposite in enter-while-insert-is-pending-is-dropped-test: a second Enter
during a pending insert starts no insert and is not queued, so Backspace
cannot undo it. Master's behaviour wins; the keys typed during a pending
Enter still land on the new block, and master's source-block check decides
when Backspace deletes it.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 17, 2026
…d, as master pins

Reverts 1f2f44f ("an Enter pressed while the previous Enter's insert runs
is queued, not dropped"). Master's logseq#14024 (logseq/db-test#1535) pins the
opposite in enter-while-insert-is-pending-is-dropped-test: a second Enter
during a pending insert starts no insert and is not queued, so Backspace
cannot undo it. Master's behaviour wins; the keys typed during a pending
Enter still land on the new block, and master's source-block check decides
when Backspace deletes it.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 18, 2026
…d, as master pins

Reverts 1f2f44f ("an Enter pressed while the previous Enter's insert runs
is queued, not dropped"). Master's logseq#14024 (logseq/db-test#1535) pins the
opposite in enter-while-insert-is-pending-is-dropped-test: a second Enter
during a pending insert starts no insert and is not queued, so Backspace
cannot undo it. Master's behaviour wins; the keys typed during a pending
Enter still land on the new block, and master's source-block check decides
when Backspace deletes it.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 18, 2026
…d, as master pins

Reverts 1f2f44f ("an Enter pressed while the previous Enter's insert runs
is queued, not dropped"). Master's logseq#14024 (logseq/db-test#1535) pins the
opposite in enter-while-insert-is-pending-is-dropped-test: a second Enter
during a pending insert starts no insert and is not queued, so Backspace
cannot undo it. Master's behaviour wins; the keys typed during a pending
Enter still land on the new block, and master's source-block check decides
when Backspace deletes it.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 18, 2026
…d, as master pins

Reverts 1f2f44f ("an Enter pressed while the previous Enter's insert runs
is queued, not dropped"). Master's logseq#14024 (logseq/db-test#1535) pins the
opposite in enter-while-insert-is-pending-is-dropped-test: a second Enter
during a pending insert starts no insert and is not queued, so Backspace
cannot undo it. Master's behaviour wins; the keys typed during a pending
Enter still land on the new block, and master's source-block check decides
when Backspace deletes it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants