1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-07 23:03:00 +03:00

Lexical: Added auto links on enter/space

This commit is contained in:
Dan Brown
2024-12-14 12:35:13 +00:00
parent a8ef820443
commit 97b201f61f
4 changed files with 171 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
import {initializeUnitTest} from "lexical/__tests__/utils";
import {SerializedLinkNode} from "@lexical/link";
import {
$getRoot,
ParagraphNode,
SerializedParagraphNode,
SerializedTextNode,
TextNode
} from "lexical";
import {registerAutoLinks} from "../auto-links";
describe('Auto-link service tests', () => {
initializeUnitTest((testEnv) => {
test('space after link in text', async () => {
const {editor} = testEnv;
registerAutoLinks(editor);
let pNode!: ParagraphNode;
editor.update(() => {
pNode = new ParagraphNode();
const text = new TextNode('Some https://example.com?test=true text');
pNode.append(text);
$getRoot().append(pNode);
text.select(35, 35);
});
editor.commitUpdates();
const pDomEl = editor.getElementByKey(pNode.getKey());
const event = new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
key: ' ',
keyCode: 62,
});
pDomEl?.dispatchEvent(event);
editor.commitUpdates();
const paragraph = editor!.getEditorState().toJSON().root
.children[0] as SerializedParagraphNode;
expect(paragraph.children[1].type).toBe('link');
const link = paragraph.children[1] as SerializedLinkNode;
expect(link.url).toBe('https://example.com?test=true');
const linkText = link.children[0] as SerializedTextNode;
expect(linkText.text).toBe('https://example.com?test=true');
});
test('enter after link in text', async () => {
const {editor} = testEnv;
registerAutoLinks(editor);
let pNode!: ParagraphNode;
editor.update(() => {
pNode = new ParagraphNode();
const text = new TextNode('Some https://example.com?test=true text');
pNode.append(text);
$getRoot().append(pNode);
text.select(35, 35);
});
editor.commitUpdates();
const pDomEl = editor.getElementByKey(pNode.getKey());
const event = new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
key: 'Enter',
keyCode: 66,
});
pDomEl?.dispatchEvent(event);
editor.commitUpdates();
const paragraph = editor!.getEditorState().toJSON().root
.children[0] as SerializedParagraphNode;
expect(paragraph.children[1].type).toBe('link');
const link = paragraph.children[1] as SerializedLinkNode;
expect(link.url).toBe('https://example.com?test=true');
const linkText = link.children[0] as SerializedTextNode;
expect(linkText.text).toBe('https://example.com?test=true');
});
});
});