Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2import {
3 $createParagraphNode,
4 $createTextNode,
5 $getRoot,
6 $getSelection,
7 $isRangeSelection,
8 COMMAND_PRIORITY_EDITOR,
9 createEditor,
10 PASTE_COMMAND,
11} from "lexical";
12
13import { registerComposerInlineTokenPaste } from "./composerInlineTokenPaste";
14
15class TestClipboardEvent extends Event {
16 readonly clipboardData: DataTransfer;
17
18 constructor(text: string) {
19 super("paste", { cancelable: true });
20 this.clipboardData = {
21 files: [],
22 getData: (type: string) => (type === "text/plain" ? text : ""),
23 } as unknown as DataTransfer;
24 }
25}
26
27describe("registerComposerInlineTokenPaste", () => {
28 afterEach(() => {
29 vi.unstubAllGlobals();
30 });
31
32 it("handles a copied mention without also running the plain-text paste fallback", () => {
33 vi.stubGlobal("ClipboardEvent", TestClipboardEvent);
34 const editor = createEditor();
35 const mention = "[improve-deploy-error-logging.md](.changeset/improve-deploy-error-logging.md)";
36 const plainTextFallback = vi.fn(() => {
37 const selection = $getSelection();
38 if (!$isRangeSelection(selection)) return false;
39 selection.insertText(mention);
40 return true;
41 });
42
43 editor.update(
44 () => {
45 const paragraph = $createParagraphNode();
46 $getRoot().append(paragraph);
47 paragraph.selectEnd();
48 },
49 { discrete: true },
50 );
51 registerComposerInlineTokenPaste(editor, {
52 createMentionNode: (path) => $createTextNode(`<mention:${path}>`),
53 getExpandedAbsoluteOffsetForPoint: () => 0,
54 });
55 editor.registerCommand(PASTE_COMMAND, plainTextFallback, COMMAND_PRIORITY_EDITOR);
56
57 const event = new TestClipboardEvent(mention);
58 let handled = false;
59 editor.update(
60 () => {
61 handled = editor.dispatchCommand(PASTE_COMMAND, event as ClipboardEvent);
62 },
63 { discrete: true },
64 );
65
66 expect(handled).toBe(true);
67 expect(plainTextFallback).not.toHaveBeenCalled();
68 expect(event.defaultPrevented).toBe(true);
69 expect(editor.getEditorState().read(() => $getRoot().getTextContent())).toBe(
70 "<mention:.changeset/improve-deploy-error-logging.md> ",
71 );
72 });
73
74 it.each([
75 "yarn expo install @expo/ui",
76 "npm install @jane/foo.js",
77 "import '@scope/pkg/sub/path'",
78 ])("leaves scoped package command %s to the plain-text paste fallback", (command) => {
79 vi.stubGlobal("ClipboardEvent", TestClipboardEvent);
80 const editor = createEditor();
81 const plainTextFallback = vi.fn((event: ClipboardEvent) => {
82 const selection = $getSelection();
83 if (!$isRangeSelection(selection)) return false;
84 selection.insertText(event.clipboardData?.getData("text/plain") ?? "");
85 return true;
86 });
87
88 editor.update(
89 () => {
90 const paragraph = $createParagraphNode();
91 $getRoot().append(paragraph);
92 paragraph.selectEnd();
93 },
94 { discrete: true },
95 );
96 registerComposerInlineTokenPaste(editor, {
97 createMentionNode: (path) => $createTextNode(`<mention:${path}>`),
98 getExpandedAbsoluteOffsetForPoint: () => 0,
99 });
100 editor.registerCommand(PASTE_COMMAND, plainTextFallback, COMMAND_PRIORITY_EDITOR);
101
102 const event = new TestClipboardEvent(command);
103 let handled = false;
104 editor.update(
105 () => {
106 handled = editor.dispatchCommand(PASTE_COMMAND, event as ClipboardEvent);
107 },
108 { discrete: true },
109 );
110
111 expect(handled).toBe(true);
112 expect(plainTextFallback).toHaveBeenCalledOnce();
113 expect(editor.getEditorState().read(() => $getRoot().getTextContent())).toBe(command);
114 });
115
116 it("pastes a canonical scoped folder link as a mention", () => {
117 vi.stubGlobal("ClipboardEvent", TestClipboardEvent);
118 const editor = createEditor();
119 const mention = "[sub](@scope/pkg/sub)";
120 const plainTextFallback = vi.fn(() => true);
121
122 editor.update(
123 () => {
124 const paragraph = $createParagraphNode();
125 $getRoot().append(paragraph);
126 paragraph.selectEnd();
127 },
128 { discrete: true },
129 );
130 registerComposerInlineTokenPaste(editor, {
131 createMentionNode: (path) => $createTextNode(`<mention:${path}>`),
132 getExpandedAbsoluteOffsetForPoint: () => 0,
133 });
134 editor.registerCommand(PASTE_COMMAND, plainTextFallback, COMMAND_PRIORITY_EDITOR);
135
136 const event = new TestClipboardEvent(mention);
137 let handled = false;
138 editor.update(
139 () => {
140 handled = editor.dispatchCommand(PASTE_COMMAND, event as ClipboardEvent);
141 },
142 { discrete: true },
143 );
144
145 expect(handled).toBe(true);
146 expect(plainTextFallback).not.toHaveBeenCalled();
147 expect(event.defaultPrevented).toBe(true);
148 expect(editor.getEditorState().read(() => $getRoot().getTextContent())).toBe(
149 "<mention:@scope/pkg/sub> ",
150 );
151 });
152});