-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support pasting a url onto text to create a link (#7766)
- Loading branch information
Showing
3 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'@keystone-6/fields-document': patch | ||
--- | ||
|
||
Adds support for pasting a url onto text to create a link |
This file contains 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
132 changes: 132 additions & 0 deletions
132
packages/fields-document/src/DocumentEditor/pasting/links.test.tsx
This file contains 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,132 @@ | ||
/** @jest-environment jsdom */ | ||
/** @jsxRuntime classic */ | ||
/** @jsx jsx */ | ||
import { Editor } from 'slate'; | ||
import { makeEditor, jsx } from '../tests/utils'; | ||
import { MyDataTransfer } from './data-transfer'; | ||
|
||
function pasteText(editor: Editor, text: string) { | ||
const data = new MyDataTransfer(); | ||
data.setData('text/plain', text); | ||
editor.insertData(data); | ||
} | ||
|
||
test('pasting a url on some text wraps the text with a link', () => { | ||
const editor = makeEditor( | ||
<editor> | ||
<paragraph> | ||
<text> | ||
blah <anchor /> | ||
blah | ||
<focus /> blah | ||
</text> | ||
</paragraph> | ||
</editor> | ||
); | ||
pasteText(editor, 'https://keystonejs.com'); | ||
expect(editor).toMatchInlineSnapshot(` | ||
<editor> | ||
<paragraph> | ||
<text> | ||
blah | ||
</text> | ||
<link | ||
@@isInline={true} | ||
href="https://keystonejs.com" | ||
> | ||
<text> | ||
<anchor /> | ||
blah | ||
<focus /> | ||
</text> | ||
</link> | ||
<text> | ||
blah | ||
</text> | ||
</paragraph> | ||
</editor> | ||
`); | ||
}); | ||
|
||
test('pasting a url on a selection spanning multiple blocks replaces the selection with the url', () => { | ||
const editor = makeEditor( | ||
<editor> | ||
<paragraph> | ||
<text> | ||
start should still exist <anchor /> | ||
blah blah | ||
</text> | ||
</paragraph> | ||
<paragraph> | ||
<text> | ||
blah blah | ||
<focus /> end should still exist | ||
</text> | ||
</paragraph> | ||
</editor> | ||
); | ||
pasteText(editor, 'https://keystonejs.com'); | ||
expect(editor).toMatchInlineSnapshot(` | ||
<editor> | ||
<paragraph> | ||
<text> | ||
start should still exist | ||
</text> | ||
<link | ||
@@isInline={true} | ||
href="https://keystonejs.com" | ||
> | ||
<text> | ||
https://keystonejs.com | ||
<cursor /> | ||
</text> | ||
</link> | ||
<text> | ||
end should still exist | ||
</text> | ||
</paragraph> | ||
</editor> | ||
`); | ||
}); | ||
|
||
test('pasting a url on a selection with a link inside replaces the selection with the url', () => { | ||
const editor = makeEditor( | ||
<editor> | ||
<paragraph> | ||
<text> | ||
start should still exist <anchor /> | ||
should{' '} | ||
</text> | ||
<link href="https://keystonejs.com/docs"> | ||
<text>be</text> | ||
</link> | ||
<text> | ||
replaced | ||
<focus /> end should still exist | ||
</text> | ||
</paragraph> | ||
</editor> | ||
); | ||
pasteText(editor, 'https://keystonejs.com'); | ||
expect(editor).toMatchInlineSnapshot(` | ||
<editor> | ||
<paragraph> | ||
<text> | ||
start should still exist | ||
</text> | ||
<link | ||
@@isInline={true} | ||
href="https://keystonejs.com" | ||
> | ||
<text> | ||
https://keystonejs.com | ||
<cursor /> | ||
</text> | ||
</link> | ||
<text> | ||
end should still exist | ||
</text> | ||
</paragraph> | ||
</editor> | ||
`); | ||
}); |