Repository navigation
fix(link): handle single-character and whitespace selections #1321
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
ReFFaT
wants to merge
6
commits into
main
Choose a base branch
from
fix/link-placeholder-selection
base: main
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
197644d
fix(link): handle single-character and whitespace selections
ReFFaT 2e898e2
fix(link): preserve inline objects in range selections
ReFFaT beb4d72
Merge branch 'main' into fix/link-placeholder-selection
ReFFaT 3b922de
fix(link): simplify selection handling and stabilize regression tests
ReFFaT 148349c
test(link): wait for editor selection before opening link popup
ReFFaT 7f893d4
fix(link): anchor selections before trailing line breaks
ReFFaT 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
150 changes: 150 additions & 0 deletions
150
packages/editor/src/extensions/markdown/Link/actions/linkEnhanceActions.test.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,150 @@ | ||
| import {EditorState, TextSelection} from 'prosemirror-state'; | ||
| import {builders} from 'prosemirror-test-builder'; | ||
| import {DecorationSet, EditorView} from 'prosemirror-view'; | ||
| import {describe, expect, it, vi} from 'vitest'; | ||
|
|
||
| import {ExtensionsManager} from '../../../../core'; | ||
| import {BaseNode, BaseSchemaSpecs} from '../../../base/specs'; | ||
| import {BreakNodeName, BreaksSpecs} from '../../Breaks/BreaksSpecs'; | ||
| import {ImageSpecs} from '../../Image/ImageSpecs'; | ||
| import {LinkAttr, LinkSpecs, linkMarkName} from '../LinkSpecs'; | ||
| import {linkTooltipPlugin} from '../plugins/LinkTooltipPlugin'; | ||
|
|
||
| import {addEmptyLink, linkActionSpec2} from './linkEnhanceActions'; | ||
|
|
||
| const deps = new ExtensionsManager({ | ||
| extensions: (builder) => | ||
| builder.use(BaseSchemaSpecs, {}).use(LinkSpecs).use(ImageSpecs).use(BreaksSpecs, {}), | ||
| }).buildDeps(); | ||
| const {schema} = deps; | ||
| const {doc, p, link, image, br} = builders<'doc' | 'p' | 'link' | 'image' | 'br'>(schema, { | ||
| image: {nodeType: 'image', src: 'image.png'}, | ||
| br: {nodeType: BreakNodeName.HardBreak}, | ||
| doc: {nodeType: BaseNode.Doc}, | ||
| p: {nodeType: BaseNode.Paragraph}, | ||
| link: {markType: linkMarkName, [LinkAttr.Href]: 'https://example.com'}, | ||
| }); | ||
| const tooltip = linkTooltipPlugin(deps); | ||
|
|
||
| function decorations(state: EditorState) { | ||
| const result = tooltip.props.decorations?.call(tooltip, state); | ||
| return result instanceof DecorationSet ? result.find() : []; | ||
| } | ||
|
|
||
| describe('addEmptyLink', () => { | ||
| it.each([ | ||
| ['two inline images', () => doc(p('before <from>', image(), image(), '<to> after'))], | ||
| [ | ||
| 'an image surrounded by whitespace', | ||
| () => doc(p('before <from> ', image(), ' <to> after')), | ||
| ], | ||
| ] as const)('should keep link creation enabled for %s', (_name, createDocument) => { | ||
| const document = createDocument(); | ||
| let state = EditorState.create({ | ||
| schema, | ||
| doc: document, | ||
| selection: TextSelection.create(document, document.tag.from, document.tag.to), | ||
| }); | ||
|
|
||
| expect(addEmptyLink(state)).toBe(true); | ||
| expect( | ||
| addEmptyLink(state, (tr) => { | ||
| state = state.apply(tr); | ||
| }), | ||
| ).toBe(true); | ||
|
|
||
| const objects: {pos: number; size: number}[] = []; | ||
| state.doc.nodesBetween(document.tag.from, document.tag.to, (node, pos) => { | ||
| if (node.isInline && !node.isText) { | ||
| expect(schema.marks[linkMarkName].isInSet(node.marks)?.attrs).toMatchObject({ | ||
| [LinkAttr.Href]: '', | ||
| [LinkAttr.IsPlaceholder]: true, | ||
| }); | ||
| objects.push({pos, size: node.nodeSize}); | ||
| } | ||
| }); | ||
| expect(objects.length).toBeGreaterThan(0); | ||
| const last = objects[objects.length - 1]; | ||
| expect(decorations(state)).toEqual([ | ||
| expect.objectContaining({from: last.pos, to: last.pos + last.size}), | ||
| ]); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['a space', () => doc(p('before<from> <to>after'))], | ||
| ['whitespace and a line break', () => doc(p('before<from> ', br(), ' \u00a0<to>after'))], | ||
| ] as const)('should reject %s without dispatching', (_name, createDocument) => { | ||
| const document = createDocument(); | ||
| const state = EditorState.create({ | ||
| schema, | ||
| doc: document, | ||
| selection: TextSelection.create(document, document.tag.from, document.tag.to), | ||
| }); | ||
| const dispatch = vi.fn(); | ||
|
|
||
| expect(addEmptyLink(state)).toBe(false); | ||
| expect(addEmptyLink(state, dispatch)).toBe(false); | ||
| expect(dispatch).not.toHaveBeenCalled(); | ||
| const view = new EditorView(null, {state}); | ||
| try { | ||
| expect(linkActionSpec2(deps).isEnable(state, undefined, view)).toBe(false); | ||
| } finally { | ||
| view.destroy(); | ||
| } | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['a', 8, 9], | ||
| [' a ', 9, 10], | ||
| ['word', 8, 12], | ||
| [' word ', 9, 13], | ||
| ] as const)('should provide a tooltip anchor for selected text %j', (text, from, to) => { | ||
| const document = doc(p('before <from>', text, '<to> after')); | ||
| let state = EditorState.create({ | ||
| schema, | ||
| doc: document, | ||
| selection: TextSelection.create(document, document.tag.from, document.tag.to), | ||
| }); | ||
|
|
||
| expect( | ||
| addEmptyLink(state, (tr) => { | ||
| state = state.apply(tr); | ||
| }), | ||
| ).toBe(true); | ||
| expect(decorations(state)).toEqual([ | ||
| expect.objectContaining({ | ||
| from, | ||
| to, | ||
| }), | ||
| ]); | ||
| }); | ||
|
|
||
| it('should anchor a new single-character link next to an existing link', () => { | ||
| const document = doc(p(link('existing'), '<from>a<to> after')); | ||
| let state = EditorState.create({ | ||
| schema, | ||
| doc: document, | ||
| selection: TextSelection.create(document, document.tag.from, document.tag.to), | ||
| }); | ||
|
|
||
| expect( | ||
| addEmptyLink(state, (tr) => { | ||
| state = state.apply(tr); | ||
| }), | ||
| ).toBe(true); | ||
| expect(decorations(state)).toEqual([ | ||
| expect.objectContaining({from: document.tag.from, to: document.tag.to}), | ||
| ]); | ||
| }); | ||
|
|
||
| it('should keep a saved link tooltip hidden at its start', () => { | ||
| const document = doc(p('before ', link('<cursor>a'), ' after')); | ||
| const state = EditorState.create({ | ||
| schema, | ||
| doc: document, | ||
| selection: TextSelection.create(document, document.tag.cursor), | ||
| }); | ||
|
|
||
| expect(decorations(state)).toEqual([]); | ||
| }); | ||
| }); | ||
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.
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.