-
Notifications
You must be signed in to change notification settings - Fork 132
fix(tracked-changes): fix suggested insertions from paste failures #1969
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
palmer-cl
merged 5 commits into
main
from
colep/sd-1812-bug-pasting-from-external-sources-in-suggestion-mode-does
Feb 10, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3bc231c
fix(tracked-changes): fix suggested insertions from paste failures
palmer-cl ea01f99
Merge remote-tracking branch 'origin/main' into colep/sd-1812-bug-pas…
palmer-cl 8d6d985
refactor: use old tryInsert function style
palmer-cl c76d9a7
refactor: simplify insertion logic
palmer-cl 1acbc53
chore: add comments and tests
palmer-cl 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
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the three new tests all paste at a cursor position (no selection). could we add one where you select some text and paste over it? that would cover the other branch where |
||
| import { EditorState, TextSelection } from 'prosemirror-state'; | ||
| import { DOMParser as PMDOMParser, Slice } from 'prosemirror-model'; | ||
| import { trackedTransaction, documentHelpers } from './index.js'; | ||
| import { TrackInsertMarkName, TrackDeleteMarkName } from '../constants.js'; | ||
| import { TrackChangesBasePluginKey } from '../plugins/trackChangesBasePlugin.js'; | ||
|
|
@@ -207,6 +208,169 @@ describe('trackChangesHelpers replaceStep', () => { | |
| expect(insertedText).toBe('xy'); | ||
| }); | ||
|
|
||
| it('tracks single-paragraph HTML paste insertions', () => { | ||
| const doc = schema.nodes.doc.create( | ||
| {}, | ||
| schema.nodes.paragraph.create({}, schema.nodes.run.create({}, [schema.text('Base')])), | ||
| ); | ||
| let state = createState(doc); | ||
|
|
||
| const basePos = findTextPos(state.doc, 'Base'); | ||
| expect(basePos).toBeTypeOf('number'); | ||
| const insertPos = basePos + 'Base'.length; | ||
| state = state.apply(state.tr.setSelection(TextSelection.create(state.doc, insertPos))); | ||
|
|
||
| const tempDiv = document.createElement('div'); | ||
| tempDiv.innerHTML = '<p>Paste One</p>'; | ||
| const parsedDoc = PMDOMParser.fromSchema(schema).parse(tempDiv); | ||
| const slice = new Slice(parsedDoc.content, 0, 0); | ||
|
|
||
| let tr = state.tr.replaceSelection(slice); | ||
| tr.setMeta('inputType', 'insertFromPaste'); | ||
| const tracked = trackedTransaction({ tr, state, user }); | ||
| const finalState = state.apply(tracked); | ||
|
|
||
| let insertedText = ''; | ||
| finalState.doc.descendants((node) => { | ||
| if (node.isText && node.marks.some((mark) => mark.type.name === TrackInsertMarkName)) { | ||
| insertedText += node.text; | ||
| } | ||
| }); | ||
|
|
||
| expect(insertedText).toContain('Paste One'); | ||
| }); | ||
|
|
||
| it('tracks multi-paragraph HTML paste insertions', () => { | ||
| const doc = schema.nodes.doc.create( | ||
| {}, | ||
| schema.nodes.paragraph.create({}, schema.nodes.run.create({}, [schema.text('Base')])), | ||
| ); | ||
| let state = createState(doc); | ||
|
|
||
| const basePos = findTextPos(state.doc, 'Base'); | ||
| expect(basePos).toBeTypeOf('number'); | ||
| const insertPos = basePos + 'Base'.length; | ||
| state = state.apply(state.tr.setSelection(TextSelection.create(state.doc, insertPos))); | ||
|
|
||
| const tempDiv = document.createElement('div'); | ||
| tempDiv.innerHTML = '<p>Paste One</p><p>Paste Two</p>'; | ||
| const parsedDoc = PMDOMParser.fromSchema(schema).parse(tempDiv); | ||
| const slice = new Slice(parsedDoc.content, 0, 0); | ||
|
|
||
| let tr = state.tr.replaceSelection(slice); | ||
| tr.setMeta('inputType', 'insertFromPaste'); | ||
| const tracked = trackedTransaction({ tr, state, user }); | ||
| const finalState = state.apply(tracked); | ||
|
|
||
| let insertedText = ''; | ||
| finalState.doc.descendants((node) => { | ||
| if (node.isText && node.marks.some((mark) => mark.type.name === TrackInsertMarkName)) { | ||
| insertedText += node.text; | ||
| } | ||
| }); | ||
|
|
||
| expect(insertedText).toContain('Paste One'); | ||
| expect(insertedText).toContain('Paste Two'); | ||
| }); | ||
|
|
||
| it('tracks plain-text paste insertions', () => { | ||
| const doc = schema.nodes.doc.create( | ||
| {}, | ||
| schema.nodes.paragraph.create({}, schema.nodes.run.create({}, [schema.text('Base')])), | ||
| ); | ||
| let state = createState(doc); | ||
|
|
||
| const basePos = findTextPos(state.doc, 'Base'); | ||
| expect(basePos).toBeTypeOf('number'); | ||
| const insertPos = basePos + 'Base'.length; | ||
| state = state.apply(state.tr.setSelection(TextSelection.create(state.doc, insertPos))); | ||
|
|
||
| let tr = state.tr.insertText('Plain Paste', insertPos); | ||
| tr.setMeta('inputType', 'insertFromPaste'); | ||
| const tracked = trackedTransaction({ tr, state, user }); | ||
| const finalState = state.apply(tracked); | ||
|
|
||
| let insertedText = ''; | ||
| finalState.doc.descendants((node) => { | ||
| if (node.isText && node.marks.some((mark) => mark.type.name === TrackInsertMarkName)) { | ||
| insertedText += node.text; | ||
| } | ||
| }); | ||
|
|
||
| expect(insertedText).toContain('Plain Paste'); | ||
| }); | ||
|
|
||
| it('tracks paste replacement over selected existing text', () => { | ||
| const doc = schema.nodes.doc.create( | ||
| {}, | ||
| schema.nodes.paragraph.create({}, schema.nodes.run.create({}, [schema.text('Hello World')])), | ||
| ); | ||
| let state = createState(doc); | ||
|
|
||
| const worldPos = findTextPos(state.doc, 'Hello World'); | ||
| expect(worldPos).toBeTypeOf('number'); | ||
| const from = worldPos + 'Hello '.length; | ||
| const to = from + 'World'.length; | ||
| state = state.apply(state.tr.setSelection(TextSelection.create(state.doc, from, to))); | ||
|
|
||
| const tempDiv = document.createElement('div'); | ||
| tempDiv.innerHTML = '<p>Pasted</p>'; | ||
| const parsedDoc = PMDOMParser.fromSchema(schema).parse(tempDiv); | ||
| const slice = new Slice(parsedDoc.content, 0, 0); | ||
|
|
||
| let tr = state.tr.replaceSelection(slice); | ||
| tr.setMeta('inputType', 'insertFromPaste'); | ||
| const tracked = trackedTransaction({ tr, state, user }); | ||
| const meta = tracked.getMeta(TrackChangesBasePluginKey); | ||
| const finalState = state.apply(tracked); | ||
|
|
||
| let insertedText = ''; | ||
| let deletedText = ''; | ||
| finalState.doc.descendants((node) => { | ||
| if (!node.isText) return; | ||
| if (node.marks.some((mark) => mark.type.name === TrackInsertMarkName)) insertedText += node.text; | ||
| if (node.marks.some((mark) => mark.type.name === TrackDeleteMarkName)) deletedText += node.text; | ||
| }); | ||
|
|
||
| expect(insertedText).toContain('Pasted'); | ||
| expect(deletedText).toContain('World'); | ||
| expect(meta?.insertedMark).toBeDefined(); | ||
| expect(meta?.deletionMark).toBeDefined(); | ||
| expect(meta.insertedMark.attrs.id).toBe(meta.deletionMark.attrs.id); | ||
| }); | ||
|
|
||
| it('prefers original paste slice before maxOpen fallback for collapsed insertions', () => { | ||
| const doc = schema.nodes.doc.create( | ||
| {}, | ||
| schema.nodes.paragraph.create({}, schema.nodes.run.create({}, [schema.text('Base')])), | ||
| ); | ||
| let state = createState(doc); | ||
|
|
||
| const basePos = findTextPos(state.doc, 'Base'); | ||
| expect(basePos).toBeTypeOf('number'); | ||
| const insertPos = basePos + 'Base'.length; | ||
| state = state.apply(state.tr.setSelection(TextSelection.create(state.doc, insertPos))); | ||
|
|
||
| const originalDiv = document.createElement('div'); | ||
| originalDiv.innerHTML = '<p>Paste One</p><p>Paste Two</p>'; | ||
| const originalSlice = new Slice(PMDOMParser.fromSchema(schema).parse(originalDiv).content, 0, 0); | ||
|
|
||
| const fallbackDiv = document.createElement('div'); | ||
| fallbackDiv.innerHTML = '<p>Flattened Fallback</p>'; | ||
| const fallbackSlice = new Slice(PMDOMParser.fromSchema(schema).parse(fallbackDiv).content, 0, 0); | ||
| vi.spyOn(Slice, 'maxOpen').mockReturnValue(fallbackSlice); | ||
|
|
||
| let tr = state.tr.replaceSelection(originalSlice); | ||
| tr.setMeta('inputType', 'insertFromPaste'); | ||
| const tracked = trackedTransaction({ tr, state, user }); | ||
| const finalState = state.apply(tracked); | ||
|
|
||
| const text = finalState.doc.textBetween(0, finalState.doc.content.size, '\n'); | ||
| expect(text).toContain('Paste One'); | ||
| expect(text).toContain('Paste Two'); | ||
| expect(text).not.toContain('Flattened Fallback'); | ||
| }); | ||
|
|
||
| it('tracks replace even when selection contains existing deletions and links', () => { | ||
| const linkMark = schema.marks.link.create({ href: 'https://example.com' }); | ||
| const existingDeletion = schema.marks[TrackDeleteMarkName].create({ | ||
|
|
||
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.
this is the actual fix for the bug but there's no comment explaining what it does.
something like "when pasting into a textblock, try the open slice first so content merges inline instead of creating new paragraphs" would go a long way for anyone reading this later.