-
Notifications
You must be signed in to change notification settings - Fork 66
fix(super-editor): allow Backspace to delete empty paragraphs in suggesting mode #1966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
harbournick
merged 7 commits into
main
from
tadeu/sd-1810-backspace-doesnt-delete-empty-paragraph-in-suggesting-mode
Feb 12, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56f1a38
fix(super-editor): allow Backspace to delete empty paragraphs in sugg…
tupizz f9b5e37
feat(super-editor): add tests for keymap history functionality
tupizz 34fc6bd
test(super-editor): add regression test for paragraph-join tracking i…
harbournick 4bc2750
Fix block node ID generation to prevent duplicate IDs
tupizz 3d37af6
fix(super-editor): handle structural deletions (joins) in suggesting …
tupizz dcd5c9a
chore: remove docs file from branch
tupizz 8f90140
fix(super-editor): address PR review feedback
tupizz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...l-testing/tests/interactions/stories/comments-tcs/backspace-empty-paragraph-suggesting.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { defineStory } from '@superdoc-testing/helpers'; | ||
|
|
||
| const WAIT_MS = 300; | ||
|
|
||
| /** | ||
| * SD-1810: Backspace doesn't delete empty paragraph in suggesting mode | ||
| * | ||
| * Bug: When a user creates a new paragraph (Enter) in suggesting mode and | ||
| * immediately presses Backspace, nothing happens — the empty paragraph stays. | ||
| * | ||
| * Root cause: The track changes system intercepts the ReplaceStep via | ||
| * replaceStep() → markDeletion(). An empty paragraph has no inline content, | ||
| * so markDeletion finds nothing to mark and the step is silently swallowed. | ||
| * | ||
| * This test recreates the exact bug scenario: | ||
| * 1. Type text, press Enter to create an empty paragraph | ||
| * 2. Press Backspace — empty paragraph should be removed | ||
| * 3. Also tests the Enter → Enter → Backspace → Backspace flow (paragraph join) | ||
| */ | ||
| export default defineStory({ | ||
| name: 'backspace-empty-paragraph-suggesting', | ||
| description: 'Test Backspace on empty paragraphs and paragraph joins in suggesting mode', | ||
| tickets: ['SD-1810'], | ||
| startDocument: null, | ||
| layout: true, | ||
| comments: 'off', | ||
| hideCaret: false, | ||
| hideSelection: false, | ||
|
|
||
| async run(_page, helpers): Promise<void> { | ||
| const { type, press, setDocumentMode, waitForStable, milestone } = helpers; | ||
|
|
||
| // Step 1: Type initial content | ||
| await type('Hello World'); | ||
| await waitForStable(WAIT_MS); | ||
| await milestone('initial-text', 'Document with initial text'); | ||
|
|
||
| // Step 2: Switch to suggesting mode | ||
| await setDocumentMode('suggesting'); | ||
| await waitForStable(WAIT_MS); | ||
| await milestone('suggesting-mode', 'Switched to suggesting mode'); | ||
|
|
||
| // Step 3: Press Enter to create an empty paragraph | ||
| await press('Enter'); | ||
| await waitForStable(WAIT_MS); | ||
| await milestone('after-enter', 'New empty paragraph created below'); | ||
|
|
||
| // Step 4: Press Backspace — should delete the empty paragraph | ||
| await press('Backspace'); | ||
| await waitForStable(WAIT_MS); | ||
| await milestone('after-backspace', 'Empty paragraph removed, cursor back at end of "Hello World"'); | ||
|
|
||
| // Step 5: Test Enter → Enter → Backspace → Backspace (join flow) | ||
| await press('Enter'); | ||
| await waitForStable(WAIT_MS); | ||
| await press('Enter'); | ||
| await waitForStable(WAIT_MS); | ||
| await milestone('after-two-enters', 'Two new paragraphs created'); | ||
|
|
||
| // Step 6: First Backspace — removes the second empty paragraph | ||
| await press('Backspace'); | ||
| await waitForStable(WAIT_MS); | ||
| await milestone('after-first-backspace', 'One empty paragraph removed'); | ||
|
|
||
| // Step 7: Second Backspace — joins back with the original paragraph | ||
| await press('Backspace'); | ||
| await waitForStable(WAIT_MS); | ||
| await milestone('after-second-backspace', 'Joined back to original paragraph — cursor at end of "Hello World"'); | ||
| }, | ||
| }); |
202 changes: 202 additions & 0 deletions
202
packages/super-editor/src/core/extensions/keymap-history.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| import { describe, it, expect, afterEach } from 'vitest'; | ||
| import { closeHistory, undoDepth } from 'prosemirror-history'; | ||
| import { initTestEditor } from '@tests/helpers/helpers.js'; | ||
| import { handleEnter, handleBackspace, handleDelete } from './keymap.js'; | ||
|
|
||
| describe('keymap history grouping', () => { | ||
| let editor; | ||
|
|
||
| afterEach(() => { | ||
| editor?.destroy(); | ||
| editor = null; | ||
| }); | ||
|
|
||
| const insertText = (ed, text) => { | ||
| const { from } = ed.state.selection; | ||
| ed.view.dispatch(ed.state.tr.insertText(text, from)); | ||
| }; | ||
|
|
||
| /** Simulate closeHistoryOnly (space / Opt+Backspace handler). */ | ||
| const closeHistoryGroup = (ed) => { | ||
| ed.view.dispatch(closeHistory(ed.view.state.tr)); | ||
| }; | ||
|
|
||
| it('Enter creates a new undo group boundary', () => { | ||
| ({ editor } = initTestEditor({ mode: 'text', content: '<p></p>' })); | ||
|
|
||
| insertText(editor, 'hello'); | ||
| const depthAfterFirstText = undoDepth(editor.state); | ||
|
|
||
| handleEnter(editor); | ||
|
|
||
| insertText(editor, 'world'); | ||
| const depthAfterSecondText = undoDepth(editor.state); | ||
|
|
||
| expect(depthAfterSecondText).toBeGreaterThan(depthAfterFirstText); | ||
| }); | ||
|
|
||
| it('undo after Enter restores text before Enter', () => { | ||
| ({ editor } = initTestEditor({ mode: 'text', content: '<p></p>' })); | ||
|
|
||
| insertText(editor, 'hello'); | ||
| handleEnter(editor); | ||
| insertText(editor, 'world'); | ||
|
|
||
| const textBefore = editor.state.doc.textContent; | ||
| expect(textBefore).toContain('hello'); | ||
| expect(textBefore).toContain('world'); | ||
|
|
||
| editor.commands.undo(); | ||
| const textAfterUndo = editor.state.doc.textContent; | ||
| expect(textAfterUndo).toContain('hello'); | ||
| }); | ||
|
|
||
| it('Enter creates boundary in suggesting mode', () => { | ||
| ({ editor } = initTestEditor({ | ||
| mode: 'text', | ||
| content: '<p></p>', | ||
| user: { name: 'Tester', email: 'test@test.com' }, | ||
| })); | ||
|
|
||
| editor.commands.enableTrackChanges?.(); | ||
|
|
||
| insertText(editor, 'hello'); | ||
| const depthAfterFirstText = undoDepth(editor.state); | ||
|
|
||
| handleEnter(editor); | ||
|
|
||
| insertText(editor, 'world'); | ||
| const depthAfterSecondText = undoDepth(editor.state); | ||
|
|
||
| expect(depthAfterSecondText).toBeGreaterThan(depthAfterFirstText); | ||
| }); | ||
|
|
||
| it('space creates a word-level undo boundary', () => { | ||
| ({ editor } = initTestEditor({ mode: 'text', content: '<p></p>' })); | ||
|
|
||
| insertText(editor, 'hello'); | ||
| const depthAfterFirstWord = undoDepth(editor.state); | ||
|
|
||
| // Simulate space handler: closeHistory then type space | ||
| closeHistoryGroup(editor); | ||
| insertText(editor, ' '); | ||
|
|
||
| insertText(editor, 'world'); | ||
| const depthAfterSecondWord = undoDepth(editor.state); | ||
|
|
||
| expect(depthAfterSecondWord).toBeGreaterThan(depthAfterFirstWord); | ||
| }); | ||
|
|
||
| it('undo after space removes only the last word', () => { | ||
| ({ editor } = initTestEditor({ mode: 'text', content: '<p></p>' })); | ||
|
|
||
| insertText(editor, 'hello'); | ||
| closeHistoryGroup(editor); | ||
| insertText(editor, ' world'); | ||
|
|
||
| expect(editor.state.doc.textContent).toBe('hello world'); | ||
|
|
||
| editor.commands.undo(); | ||
| expect(editor.state.doc.textContent).toBe('hello'); | ||
| }); | ||
|
|
||
| it('closeHistory before deletion creates its own undo step', () => { | ||
| ({ editor } = initTestEditor({ mode: 'text', content: '<p></p>' })); | ||
|
|
||
| insertText(editor, 'hello world'); | ||
| const depthAfterTyping = undoDepth(editor.state); | ||
|
|
||
| // Simulate Opt+Backspace: closeHistory then delete last word | ||
| closeHistoryGroup(editor); | ||
| const { from } = editor.state.selection; | ||
| editor.view.dispatch(editor.state.tr.delete(from - 5, from)); | ||
| const depthAfterDelete = undoDepth(editor.state); | ||
|
|
||
| expect(depthAfterDelete).toBeGreaterThan(depthAfterTyping); | ||
|
|
||
| // Undo should restore the deleted word | ||
| editor.commands.undo(); | ||
| expect(editor.state.doc.textContent).toBe('hello world'); | ||
| }); | ||
|
|
||
| it('Backspace creates a new undo group boundary', () => { | ||
| ({ editor } = initTestEditor({ mode: 'text', content: '<p></p>' })); | ||
|
|
||
| // Create two paragraphs: type, Enter, type | ||
| insertText(editor, 'hello'); | ||
| handleEnter(editor); | ||
| insertText(editor, 'world'); | ||
| const depthBeforeBackspace = undoDepth(editor.state); | ||
|
|
||
| // Move cursor to start of second paragraph so joinBackward succeeds | ||
| let secondParaStart = null; | ||
| editor.state.doc.forEach((_node, offset, index) => { | ||
| if (index === 1) secondParaStart = offset + 1; | ||
| }); | ||
| editor.view.dispatch( | ||
| editor.state.tr.setSelection(editor.state.selection.constructor.create(editor.state.doc, secondParaStart)), | ||
| ); | ||
|
|
||
| // Backspace at start of second paragraph → joins paragraphs | ||
| handleBackspace(editor); | ||
|
|
||
| insertText(editor, ' after'); | ||
| const depthAfterBackspace = undoDepth(editor.state); | ||
|
|
||
| expect(depthAfterBackspace).toBeGreaterThan(depthBeforeBackspace); | ||
| }); | ||
|
|
||
| it('undo after Backspace join restores paragraph break', () => { | ||
| ({ editor } = initTestEditor({ mode: 'text', content: '<p></p>' })); | ||
|
|
||
| insertText(editor, 'hello'); | ||
| handleEnter(editor); | ||
| insertText(editor, 'world'); | ||
|
|
||
| expect(editor.state.doc.childCount).toBe(2); | ||
|
|
||
| // Move cursor to start of second paragraph | ||
| let secondParaStart = null; | ||
| editor.state.doc.forEach((_node, offset, index) => { | ||
| if (index === 1) secondParaStart = offset + 1; | ||
| }); | ||
| editor.view.dispatch( | ||
| editor.state.tr.setSelection(editor.state.selection.constructor.create(editor.state.doc, secondParaStart)), | ||
| ); | ||
|
|
||
| // Backspace joins paragraphs | ||
| handleBackspace(editor); | ||
| expect(editor.state.doc.childCount).toBe(1); | ||
|
|
||
| // Undo should restore the paragraph break | ||
| editor.commands.undo(); | ||
| expect(editor.state.doc.childCount).toBe(2); | ||
| }); | ||
|
|
||
| it('Delete creates a new undo group boundary', () => { | ||
| ({ editor } = initTestEditor({ mode: 'text', content: '<p></p>' })); | ||
|
|
||
| // Create two paragraphs | ||
| insertText(editor, 'hello'); | ||
| handleEnter(editor); | ||
| insertText(editor, 'world'); | ||
| const depthBeforeDelete = undoDepth(editor.state); | ||
|
|
||
| // Move cursor to end of first paragraph so joinForward succeeds | ||
| let firstParaEnd = null; | ||
| editor.state.doc.forEach((node, offset, index) => { | ||
| if (index === 0) firstParaEnd = offset + node.nodeSize - 1; | ||
| }); | ||
| editor.view.dispatch( | ||
| editor.state.tr.setSelection(editor.state.selection.constructor.create(editor.state.doc, firstParaEnd)), | ||
| ); | ||
|
|
||
| // Delete at end of first paragraph → joins paragraphs | ||
| handleDelete(editor); | ||
|
|
||
| insertText(editor, ' after'); | ||
| const depthAfterDelete = undoDepth(editor.state); | ||
|
|
||
| expect(depthAfterDelete).toBeGreaterThan(depthBeforeDelete); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.