-
Notifications
You must be signed in to change notification settings - Fork 132
fix(super-editor): restore marks correctly after clear format + undo (SD-1771) #1967
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 3 commits into
main
from
tadeu/sd-1771-formatting-undo-clear-formatting-leaves-mixed-formatting
Feb 9, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
59 changes: 59 additions & 0 deletions
59
devtools/visual-testing/tests/interactions/stories/formatting/clear-format-undo.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,59 @@ | ||
| import { defineStory } from '@superdoc-testing/helpers'; | ||
|
|
||
| const WAIT_MS = 300; | ||
| const FONT_NAME = 'Courier New'; | ||
|
|
||
| export default defineStory({ | ||
| name: 'clear-format-undo', | ||
| description: 'Clear formatting on styled text, then undo to verify marks are fully restored.', | ||
| tickets: ['SD-1771'], | ||
| startDocument: null, | ||
| layout: true, | ||
| toolbar: 'full', | ||
| waitForFonts: true, | ||
|
|
||
| async run(_page, helpers): Promise<void> { | ||
| const { step, type, newLine, bold, italic, selectAll, undo, focus, executeCommand, waitForStable, milestone } = | ||
| helpers; | ||
|
|
||
| await step('Type and format text', async () => { | ||
| await focus(); | ||
|
|
||
| // Type three lines with different formatting | ||
| await bold(); | ||
| await type('Bold text here.'); | ||
| await bold(); | ||
| await newLine(); | ||
|
|
||
| await italic(); | ||
| await type('Italic text here.'); | ||
| await italic(); | ||
| await newLine(); | ||
|
|
||
| await type('Custom font text.'); | ||
| await waitForStable(WAIT_MS); | ||
|
|
||
| // Apply a custom font to the last line | ||
| await selectAll(); | ||
| await executeCommand('setFontFamily', FONT_NAME); | ||
| await waitForStable(WAIT_MS); | ||
|
|
||
| await milestone('formatted', 'Text with bold, italic, and Courier New applied.'); | ||
| }); | ||
|
|
||
| await step('Clear formatting', async () => { | ||
| await selectAll(); | ||
| await executeCommand('clearFormat'); | ||
| await waitForStable(WAIT_MS); | ||
|
|
||
| await milestone('cleared', 'All formatting cleared from text.'); | ||
| }); | ||
|
|
||
| await step('Undo clear format', async () => { | ||
| await undo(); | ||
| await waitForStable(WAIT_MS); | ||
|
|
||
| await milestone('after-undo', 'Undo restored formatting — bold, italic, and Courier New should reappear.'); | ||
| }); | ||
| }, | ||
| }); |
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
127 changes: 127 additions & 0 deletions
127
packages/super-editor/src/extensions/format-commands/clear-format-undo.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,127 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { TextSelection } from 'prosemirror-state'; | ||
| import { initTestEditor, loadTestDataForEditorTests } from '@tests/helpers/helpers.js'; | ||
|
|
||
| /** | ||
| * SD-1771: Formatting - Undo clear formatting leaves mixed formatting | ||
| * | ||
| * Root cause: run nodes (non-atom inline containers) can carry a textStyle | ||
| * mark with all-null attrs. ProseMirror's `tr.removeMark(from, to)` creates | ||
| * RemoveMarkStep for marks on ALL inline nodes (including the run's | ||
| * null-attrs textStyle). On undo, AddMarkStep only applies to atom/text | ||
| * nodes, so the run's null-attrs textStyle overwrites the text's correct | ||
| * textStyle mark. | ||
| * | ||
| * Fix: unsetAllMarks now collects marks only from leaf/atom nodes and removes | ||
| * each explicitly, avoiding RemoveMarkSteps for container-node marks. | ||
| */ | ||
| describe('SD-1771: Clear format + undo mark restoration', () => { | ||
| it('should restore textStyle mark attrs after clear format + undo (simple doc)', () => { | ||
| const { editor } = initTestEditor({ | ||
| loadFromSchema: true, | ||
| content: { | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'paragraph', | ||
| content: [ | ||
| { | ||
| type: 'run', | ||
| attrs: { | ||
| runProperties: { | ||
| bold: true, | ||
| fontFamily: { ascii: 'Roboto', hAnsi: 'Roboto', cs: 'Roboto' }, | ||
| fontSize: 44, | ||
| color: { val: '000000' }, | ||
| }, | ||
| }, | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: 'Hello World', | ||
| marks: [ | ||
| { type: 'bold' }, | ||
| { | ||
| type: 'textStyle', | ||
| attrs: { color: '#000000', fontFamily: 'Roboto, sans-serif', fontSize: '22pt' }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| const from = 2; | ||
| const to = 2 + 'Hello World'.length; | ||
| editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, from, to))); | ||
| editor.commands.clearFormat(); | ||
| editor.commands.undo(); | ||
|
|
||
| const marks = []; | ||
| editor.state.doc.descendants((node) => { | ||
| if (node.isText) marks.push(...node.marks.map((m) => ({ type: m.type.name, attrs: { ...m.attrs } }))); | ||
| }); | ||
| const textStyle = marks.find((m) => m.type === 'textStyle'); | ||
| expect(textStyle).toBeDefined(); | ||
| expect(textStyle.attrs.color).toBe('#000000'); | ||
| expect(textStyle.attrs.fontFamily).toBe('Roboto, sans-serif'); | ||
| expect(textStyle.attrs.fontSize).toBe('22pt'); | ||
| }); | ||
|
|
||
| it('should restore textStyle marks correctly after clear format + undo (real DOCX)', async () => { | ||
| const { docx, media, mediaFiles, fonts } = await loadTestDataForEditorTests('sdpr.docx'); | ||
| const { editor } = initTestEditor({ content: docx, media, mediaFiles, fonts }); | ||
|
|
||
| // Find first paragraph with styled text (textStyle marks with real values) | ||
| let targetFrom = null; | ||
| let targetTo = null; | ||
| let expectedColor = null; | ||
| let expectedFontFamily = null; | ||
| let expectedFontSize = null; | ||
|
|
||
| editor.state.doc.descendants((node, pos) => { | ||
| if (targetFrom !== null) return false; | ||
| if (node.type.name === 'paragraph' && node.textContent.length > 5) { | ||
| node.descendants((child) => { | ||
| if (targetFrom !== null) return false; | ||
| if (child.isText) { | ||
| const ts = child.marks.find((m) => m.type.name === 'textStyle'); | ||
| if (ts && (ts.attrs.fontFamily || ts.attrs.fontSize || ts.attrs.color)) { | ||
| targetFrom = pos + 1; | ||
| targetTo = pos + node.nodeSize - 1; | ||
| expectedColor = ts.attrs.color; | ||
| expectedFontFamily = ts.attrs.fontFamily; | ||
| expectedFontSize = ts.attrs.fontSize; | ||
| return false; | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| expect(targetFrom).not.toBeNull(); | ||
|
|
||
| editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, targetFrom, targetTo))); | ||
|
|
||
| // Clear formatting then undo | ||
| editor.commands.clearFormat(); | ||
| editor.commands.undo(); | ||
|
|
||
| // After undo, the first text node's textStyle should have the original attrs | ||
| let textStyleAfterUndo = null; | ||
| editor.state.doc.nodesBetween(targetFrom, Math.min(targetTo, editor.state.doc.content.size), (node) => { | ||
| if (!textStyleAfterUndo && node.isText) { | ||
| textStyleAfterUndo = node.marks.find((m) => m.type.name === 'textStyle'); | ||
| } | ||
| }); | ||
|
tupizz marked this conversation as resolved.
|
||
|
|
||
| expect(textStyleAfterUndo).toBeDefined(); | ||
| expect(textStyleAfterUndo.attrs.color).toBe(expectedColor); | ||
| expect(textStyleAfterUndo.attrs.fontFamily).toBe(expectedFontFamily); | ||
| expect(textStyleAfterUndo.attrs.fontSize).toBe(expectedFontSize); | ||
| }); | ||
| }); | ||
Binary file not shown.
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.