Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerInlineTokenPaste.ts

104 lines3.5 KB

1import { collectComposerInlineTokens } from "@t3tools/shared/composerInlineTokens";
2import {
3 $createLineBreakNode,
4 $createTextNode,
5 $getRoot,
6 $getSelection,
7 $isRangeSelection,
8 COMMAND_PRIORITY_HIGH,
9 PASTE_COMMAND,
10 type LexicalEditor,
11 type LexicalNode,
12} from "lexical";
13​
14interface ComposerInlineTokenPasteOptions {
15 createMentionNode: (path: string) => LexicalNode;
16 getExpandedAbsoluteOffsetForPoint: (node: LexicalNode, pointOffset: number) => number;
18​
19export function registerComposerInlineTokenPaste(
20 editor: LexicalEditor,
21 options: ComposerInlineTokenPasteOptions,
22): () => void {
23 return editor.registerCommand(
24 PASTE_COMMAND,
25 (event) => {
26 if (!(event instanceof ClipboardEvent) || event.clipboardData === null) {
27 return false;
28 }
29 if (event.clipboardData.files.length > 0) {
30 return false;
31 }
32 const text = event.clipboardData.getData("text/plain");
33 if (text.length === 0) {
34 return false;
35 }
36 // Token grammar requires trailing whitespace; a virtual newline lets a
37 // mention at the very end of the pasted text still parse.
38 const mentions = collectComposerInlineTokens(`${text}\n`).filter(
39 (token) => token.type === "mention" && token.end <= text.length,
40 );
41 if (mentions.length === 0) {
42 return false;
43 }
44​
45 // Lexical command listeners already run inside an editor update. Starting
46 // a nested update here queues the mention insertion until after this
47 // listener returns, which lets the plain-text paste handler run as well.
48 const selection = $getSelection();
49 if (!$isRangeSelection(selection)) {
50 return false;
51 }
52 const nodes: LexicalNode[] = [];
53 const appendText = (value: string) => {
54 const lines = value.split("\n");
55 for (let index = 0; index < lines.length; index += 1) {
56 const line = lines[index] ?? "";
57 if (line.length > 0) {
58 nodes.push($createTextNode(line));
59 }
60 if (index < lines.length - 1) {
61 nodes.push($createLineBreakNode());
62 }
63 }
64 };
65 const firstMention = mentions[0];
66 if (firstMention && firstMention.start === 0) {
67 const startPoint = selection.isBackward() ? selection.focus : selection.anchor;
68 const insertionOffset = options.getExpandedAbsoluteOffsetForPoint(
69 startPoint.getNode(),
70 startPoint.offset,
71 );
72 const precedingChar = $getRoot()
73 .getTextContent()
74 .slice(insertionOffset - 1, insertionOffset);
75 if (precedingChar.length > 0 && !/\s/.test(precedingChar)) {
76 nodes.push($createTextNode(" "));
77 }
78 }
79 let cursor = 0;
80 for (const mention of mentions) {
81 if (mention.start < cursor) {
82 continue;
83 }
84 if (mention.start > cursor) {
85 appendText(text.slice(cursor, mention.start));
86 }
87 nodes.push(options.createMentionNode(mention.value));
88 cursor = mention.end;
89 }
90 if (cursor < text.length) {
91 appendText(text.slice(cursor));
92 } else {
93 // Keep the serialized prompt valid: mention tokens need trailing
94 // whitespace, so a paste ending in a mention gets the same
95 // trailing space the autocomplete inserts.
96 nodes.push($createTextNode(" "));
97 }
98 selection.insertNodes(nodes);
99 event.preventDefault();
100 return true;
101 },
102 COMMAND_PRIORITY_HIGH,
103 );