Repository navigation
fix(editor): keep pending new block on fast backspace - #14024
Conversation
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]>
| (defn- inserted-block-edit-fn | ||
| [source-block block container-id] | ||
| (fn [rows] | ||
| (mark-pending-new-block-editor-landed!) |
There was a problem hiding this comment.
🔴 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.
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]>
…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.
…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.
…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.
…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.
…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.
…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.
…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.
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):
"","""""",x""The deleted in-flight block also tripped
Missing renderer resource entityand 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-handlerused(zero? (util/get-selection-start (.-target e)))asdelete-block?for the whole pending window. Untilinserted-block-edit-fnfocuses the new block,.-targetis still the source textarea, and an empty source always reports caret 0.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
fast-enter-enter-backspace-keeps-the-new-empty-block-test,fast-enter-xy-backspace-keeps-typed-text-on-the-new-block-test, andfast-enter-tab-backspace-keeps-the-indented-block-testfire the sequences in one JS turnbackspace-at-start-removes-pending-block-dom-testand rapid Enter then TabTo show artifacts inline, enable in settings.