-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(editor): prevent deletion of globalContent node on select-all + delete #3347
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
Open
danilowoz
wants to merge
2
commits into
canary
Choose a base branch
from
cursor/protect-global-content-node-6816
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,237 @@ | ||
| import type { JSONContent } from '@tiptap/core'; | ||
| import { Editor } from '@tiptap/core'; | ||
| import { TextSelection } from '@tiptap/pm/state'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { StarterKit } from './index'; | ||
|
|
||
| vi.mock('@/actions/ai', () => ({ | ||
| uploadImageViaAI: vi.fn(), | ||
| })); | ||
|
|
||
| describe('GlobalContent Node', () => { | ||
| let editor: Editor | null = null; | ||
|
|
||
| afterEach(() => { | ||
| editor?.destroy(); | ||
| editor = null; | ||
| }); | ||
|
|
||
| function createEditor(content: JSONContent) { | ||
| editor = new Editor({ | ||
| content, | ||
| extensions: [StarterKit], | ||
| }); | ||
| editor.view.dispatch(editor.state.tr); | ||
| return editor; | ||
| } | ||
|
|
||
| it('preserves globalContent when selecting all and deleting', () => { | ||
| const themeData = { theme: 'basic', css: 'body { color: red; }' }; | ||
|
|
||
| const ed = createEditor({ | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'globalContent', | ||
| attrs: { data: themeData }, | ||
| }, | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'Hello world' }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const { state } = ed; | ||
| const allSelection = TextSelection.create( | ||
| state.doc, | ||
| 0, | ||
| state.doc.content.size, | ||
| ); | ||
| ed.view.dispatch(state.tr.setSelection(allSelection).deleteSelection()); | ||
|
|
||
| const json = ed.getJSON(); | ||
| const globalContentNodes = json.content!.filter( | ||
| (n) => n.type === 'globalContent', | ||
| ); | ||
|
|
||
| expect(globalContentNodes).toHaveLength(1); | ||
| expect(globalContentNodes[0].attrs!.data).toEqual(themeData); | ||
| }); | ||
|
|
||
| it('preserves globalContent data after multiple select-all + delete cycles', () => { | ||
| const themeData = { theme: 'minimal', styles: [{ id: 'test' }] }; | ||
|
|
||
| const ed = createEditor({ | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'globalContent', | ||
| attrs: { data: themeData }, | ||
| }, | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'First content' }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| for (let i = 0; i < 3; i++) { | ||
| const { state } = ed; | ||
| const allSelection = TextSelection.create( | ||
| state.doc, | ||
| 0, | ||
| state.doc.content.size, | ||
| ); | ||
| ed.view.dispatch(state.tr.setSelection(allSelection).deleteSelection()); | ||
| } | ||
|
|
||
| const json = ed.getJSON(); | ||
| const globalContentNodes = json.content!.filter( | ||
| (n) => n.type === 'globalContent', | ||
| ); | ||
|
|
||
| expect(globalContentNodes).toHaveLength(1); | ||
| expect(globalContentNodes[0].attrs!.data).toEqual(themeData); | ||
| }); | ||
|
|
||
| it('does not duplicate globalContent when it already exists', () => { | ||
| const ed = createEditor({ | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'globalContent', | ||
| attrs: { data: { theme: 'basic' } }, | ||
| }, | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'Hello' }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| ed.commands.insertContent({ | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'New paragraph' }], | ||
| }); | ||
|
|
||
| const json = ed.getJSON(); | ||
| const globalContentNodes = json.content!.filter( | ||
| (n) => n.type === 'globalContent', | ||
| ); | ||
|
|
||
| expect(globalContentNodes).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('restores globalContent at position 0 when deleted via replaceWith', () => { | ||
| const themeData = { theme: 'basic', css: '.test {}' }; | ||
|
|
||
| const ed = createEditor({ | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'globalContent', | ||
| attrs: { data: themeData }, | ||
| }, | ||
| { | ||
| type: 'container', | ||
| content: [ | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'Content' }], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const { state } = ed; | ||
| const { schema } = state; | ||
| const newParagraph = schema.nodes.paragraph.create( | ||
| null, | ||
| schema.text('Replaced content'), | ||
| ); | ||
| const tr = state.tr.replaceWith(0, state.doc.content.size, newParagraph); | ||
| ed.view.dispatch(tr); | ||
|
|
||
| const json = ed.getJSON(); | ||
| const globalContentNodes = json.content!.filter( | ||
| (n) => n.type === 'globalContent', | ||
| ); | ||
|
|
||
| expect(globalContentNodes).toHaveLength(1); | ||
| expect(globalContentNodes[0].attrs!.data).toEqual(themeData); | ||
| }); | ||
|
|
||
| it('does not restore globalContent if it was never present', () => { | ||
| const ed = createEditor({ | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'No global content here' }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| ed.commands.clearContent(); | ||
|
|
||
| const json = ed.getJSON(); | ||
| const globalContentNodes = (json.content ?? []).filter( | ||
| (n) => n.type === 'globalContent', | ||
| ); | ||
|
|
||
| expect(globalContentNodes).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('setGlobalContent command creates globalContent if not present', () => { | ||
| const ed = createEditor({ | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'Hello' }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| ed.commands.setGlobalContent('theme', 'basic'); | ||
|
|
||
| const json = ed.getJSON(); | ||
| const globalContentNodes = json.content!.filter( | ||
| (n) => n.type === 'globalContent', | ||
| ); | ||
|
|
||
| expect(globalContentNodes).toHaveLength(1); | ||
| expect(globalContentNodes[0].attrs!.data.theme).toBe('basic'); | ||
| }); | ||
|
|
||
| it('setGlobalContent command updates existing globalContent data', () => { | ||
| const ed = createEditor({ | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'globalContent', | ||
| attrs: { data: { theme: 'basic' } }, | ||
| }, | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'Hello' }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| ed.commands.setGlobalContent('css', 'body { color: blue; }'); | ||
|
|
||
| const json = ed.getJSON(); | ||
| const globalContentNodes = json.content!.filter( | ||
| (n) => n.type === 'globalContent', | ||
| ); | ||
|
|
||
| expect(globalContentNodes).toHaveLength(1); | ||
| expect(globalContentNodes[0].attrs!.data).toEqual({ | ||
| theme: 'basic', | ||
| css: 'body { color: blue; }', | ||
| }); | ||
| }); | ||
| }); | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: Use the real select-all path here;
TextSelectioncan miss the leaf-block edge case this bug targets.Prompt for AI agents