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

ComposerPromptEditor.tsx

811 lines23.1 KB

1import {
2 LexicalComposer,
3 type InitialConfigType,
4} from "@lexical/react/LexicalComposer";
5import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
6import { ContentEditable } from "@lexical/react/LexicalContentEditable";
7import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary";
8import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
9import { OnChangePlugin } from "@lexical/react/LexicalOnChangePlugin";
10import { PlainTextPlugin } from "@lexical/react/LexicalPlainTextPlugin";
11import {
12 $applyNodeReplacement,
13 $createRangeSelection,
14 $getSelection,
15 $setSelection,
16 $isElementNode,
17 $isLineBreakNode,
18 $isRangeSelection,
19 $isTextNode,
20 $createLineBreakNode,
21 $createParagraphNode,
22 $createTextNode,
23 KEY_ARROW_DOWN_COMMAND,
24 KEY_ARROW_LEFT_COMMAND,
25 KEY_ARROW_RIGHT_COMMAND,
26 KEY_ARROW_UP_COMMAND,
27 KEY_ENTER_COMMAND,
28 KEY_TAB_COMMAND,
29 COMMAND_PRIORITY_HIGH,
30 KEY_BACKSPACE_COMMAND,
31 $getRoot,
32 type ElementNode,
33 type LexicalNode,
34 TextNode,
35 type EditorConfig,
36 type EditorState,
37 type NodeKey,
38 type SerializedTextNode,
39 type Spread,
40} from "lexical";
41import {
42 forwardRef,
43 useCallback,
44 useEffect,
45 useImperativeHandle,
46 useLayoutEffect,
47 useMemo,
48 useRef,
49 type ClipboardEventHandler,
50 type Ref,
51} from "react";
52​
53import { isCollapsedCursorAdjacentToMention } from "~/composer-logic";
54import { splitPromptIntoComposerSegments } from "~/composer-editor-mentions";
55import { cn } from "~/lib/utils";
56import { basenameOfPath, getVscodeIconUrlForEntry } from "~/vscode-icons";
57​
58const COMPOSER_EDITOR_HMR_KEY = `composer-editor-${Math.random().toString(36).slice(2)}`;
59​
60type SerializedComposerMentionNode = Spread<
61 {
62 path: string;
63 type: "composer-mention";
64 version: 1;
65 },
66 SerializedTextNode
67>;
68​
69class ComposerMentionNode extends TextNode {
70 __path: string;
71​
72 static override getType(): string {
73 return "composer-mention";
74 }
75​
76 static override clone(node: ComposerMentionNode): ComposerMentionNode {
77 return new ComposerMentionNode(node.__path, node.__key);
78 }
79​
80 static override importJSON(serializedNode: SerializedComposerMentionNode): ComposerMentionNode {
81 return $createComposerMentionNode(serializedNode.path);
82 }
83​
84 constructor(path: string, key?: NodeKey) {
85 const normalizedPath = path.startsWith("@") ? path.slice(1) : path;
86 super(`@${normalizedPath}`, key);
87 this.__path = normalizedPath;
88 }
89​
90 override exportJSON(): SerializedComposerMentionNode {
91 return {
92 ...super.exportJSON(),
93 path: this.__path,
94 type: "composer-mention",
95 version: 1,
96 };
97 }
98​
99 override createDOM(_config: EditorConfig): HTMLElement {
100 const dom = document.createElement("span");
101 dom.className =
102 "inline-flex select-none items-center gap-1 rounded-md border border-border/70 bg-accent/40 px-1.5 py-px font-medium text-[12px] leading-[1.1] text-foreground align-middle";
103 dom.contentEditable = "false";
104 dom.setAttribute("spellcheck", "false");
105 renderMentionChipDom(dom, this.__path);
106 return dom;
107 }
108​
109 override updateDOM(
110 prevNode: ComposerMentionNode,
111 dom: HTMLElement,
112 _config: EditorConfig,
113 ): boolean {
114 dom.contentEditable = "false";
115 if (prevNode.__text !== this.__text || prevNode.__path !== this.__path) {
116 renderMentionChipDom(dom, this.__path);
117 }
118 return false;
119 }
120​
121 override canInsertTextBefore(): false {
122 return false;
123 }
124​
125 override canInsertTextAfter(): false {
126 return false;
127 }
128​
129 override isTextEntity(): true {
130 return true;
131 }
132​
133 override isToken(): true {
134 return true;
135 }
137​
138function $createComposerMentionNode(path: string): ComposerMentionNode {
139 return $applyNodeReplacement(new ComposerMentionNode(path));
141​
142function inferMentionPathKind(pathValue: string): "file" | "directory" {
143 const base = basenameOfPath(pathValue);
144 if (base.startsWith(".") && !base.slice(1).includes(".")) {
145 return "directory";
146 }
147 if (base.includes(".")) {
148 return "file";
149 }
150 return "directory";
152​
153function resolvedThemeFromDocument(): "light" | "dark" {
154 return document.documentElement.classList.contains("dark") ? "dark" : "light";
156​
157function renderMentionChipDom(container: HTMLElement, pathValue: string): void {
158 container.textContent = "";
159 container.style.setProperty("user-select", "none");
160 container.style.setProperty("-webkit-user-select", "none");
161​
162 const theme = resolvedThemeFromDocument();
163 const icon = document.createElement("img");
164 icon.alt = "";
165 icon.ariaHidden = "true";
166 icon.className = "size-3.5 shrink-0 opacity-85";
167 icon.loading = "lazy";
168 icon.src = getVscodeIconUrlForEntry(pathValue, inferMentionPathKind(pathValue), theme);
169​
170 const label = document.createElement("span");
171 label.className = "truncate select-none leading-tight";
172 label.textContent = basenameOfPath(pathValue);
173​
174 container.append(icon, label);
176​
177function clampCursor(value: string, cursor: number): number {
178 if (!Number.isFinite(cursor)) return value.length;
179 return Math.max(0, Math.min(value.length, Math.floor(cursor)));
181​
182function getComposerNodeTextLength(node: LexicalNode): number {
183 if (node instanceof ComposerMentionNode) {
184 return 1;
185 }
186 if ($isTextNode(node)) {
187 return node.getTextContentSize();
188 }
189 if ($isLineBreakNode(node)) {
190 return 1;
191 }
192 if ($isElementNode(node)) {
193 return node.getChildren().reduce((total, child) => total + getComposerNodeTextLength(child), 0);
194 }
195 return 0;
197​
198function getAbsoluteOffsetForPoint(node: LexicalNode, pointOffset: number): number {
199 let offset = 0;
200 let current: LexicalNode | null = node;
201​
202 while (current) {
203 const nextParent = current.getParent() as LexicalNode | null;
204 if (!nextParent || !$isElementNode(nextParent)) {
205 break;
206 }
207 const siblings = nextParent.getChildren();
208 const index = current.getIndexWithinParent();
209 for (let i = 0; i < index; i += 1) {
210 const sibling = siblings[i];
211 if (!sibling) continue;
212 offset += getComposerNodeTextLength(sibling);
213 }
214 current = nextParent;
215 }
216​
217 if ($isTextNode(node)) {
218 if (node instanceof ComposerMentionNode) {
219 return offset + (pointOffset > 0 ? 1 : 0);
220 }
221 return offset + Math.min(pointOffset, node.getTextContentSize());
222 }
223​
224 if ($isLineBreakNode(node)) {
225 return offset + Math.min(pointOffset, 1);
226 }
227​
228 if ($isElementNode(node)) {
229 const children = node.getChildren();
230 const clampedOffset = Math.max(0, Math.min(pointOffset, children.length));
231 for (let i = 0; i < clampedOffset; i += 1) {
232 const child = children[i];
233 if (!child) continue;
234 offset += getComposerNodeTextLength(child);
235 }
236 return offset;
237 }
238​
239 return offset;
241​
242function findSelectionPointAtOffset(
243 node: LexicalNode,
244 remainingRef: { value: number },
245): { key: string; offset: number; type: "text" | "element" } | null {
246 if (node instanceof ComposerMentionNode) {
247 const parent = node.getParent();
248 if (!parent || !$isElementNode(parent)) return null;
249 const index = node.getIndexWithinParent();
250 if (remainingRef.value === 0) {
251 return {
252 key: parent.getKey(),
253 offset: index,
254 type: "element",
255 };
256 }
257 if (remainingRef.value === 1) {
258 return {
259 key: parent.getKey(),
260 offset: index + 1,
261 type: "element",
262 };
263 }
264 remainingRef.value -= 1;
265 return null;
266 }
267​
268 if ($isTextNode(node)) {
269 const size = node.getTextContentSize();
270 if (remainingRef.value <= size) {
271 return {
272 key: node.getKey(),
273 offset: remainingRef.value,
274 type: "text",
275 };
276 }
277 remainingRef.value -= size;
278 return null;
279 }
280​
281 if ($isLineBreakNode(node)) {
282 const parent = node.getParent();
283 if (!parent) return null;
284 const index = node.getIndexWithinParent();
285 if (remainingRef.value === 0) {
286 return {
287 key: parent.getKey(),
288 offset: index,
289 type: "element",
290 };
291 }
292 if (remainingRef.value === 1) {
293 return {
294 key: parent.getKey(),
295 offset: index + 1,
296 type: "element",
297 };
298 }
299 remainingRef.value -= 1;
300 return null;
301 }
302​
303 if ($isElementNode(node)) {
304 const children = node.getChildren();
305 for (const child of children) {
306 const point = findSelectionPointAtOffset(child, remainingRef);
307 if (point) {
308 return point;
309 }
310 }
311 if (remainingRef.value === 0) {
312 return {
313 key: node.getKey(),
314 offset: children.length,
315 type: "element",
316 };
317 }
318 }
319​
320 return null;
322​
323function $getComposerRootLength(): number {
324 const root = $getRoot();
325 const children = root.getChildren();
326 return children.reduce((sum, child) => sum + getComposerNodeTextLength(child), 0);
328​
329function $setSelectionAtComposerOffset(nextOffset: number): void {
330 const root = $getRoot();
331 const composerLength = $getComposerRootLength();
332 const boundedOffset = Math.max(0, Math.min(nextOffset, composerLength));
333 const remainingRef = { value: boundedOffset };
334 const point =
335 findSelectionPointAtOffset(root, remainingRef) ?? {
336 key: root.getKey(),
337 offset: root.getChildren().length,
338 type: "element" as const,
339 };
340 const selection = $createRangeSelection();
341 selection.anchor.set(point.key, point.offset, point.type);
342 selection.focus.set(point.key, point.offset, point.type);
343 $setSelection(selection);
345​
346function $readSelectionOffsetFromEditorState(fallback: number): number {
347 const selection = $getSelection();
348 if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
349 return fallback;
350 }
351 const anchorNode = selection.anchor.getNode();
352 const offset = getAbsoluteOffsetForPoint(anchorNode, selection.anchor.offset);
353 const composerLength = $getComposerRootLength();
354 return Math.max(0, Math.min(offset, composerLength));
356​
357function $appendTextWithLineBreaks(parent: ElementNode, text: string): void {
358 const lines = text.split("\n");
359 for (let index = 0; index < lines.length; index += 1) {
360 const line = lines[index] ?? "";
361 if (line.length > 0) {
362 parent.append($createTextNode(line));
363 }
364 if (index < lines.length - 1) {
365 parent.append($createLineBreakNode());
366 }
367 }
369​
370function $setComposerEditorPrompt(prompt: string): void {
371 const root = $getRoot();
372 root.clear();
373 const paragraph = $createParagraphNode();
374 root.append(paragraph);
375​
376 const segments = splitPromptIntoComposerSegments(prompt);
377 for (const segment of segments) {
378 if (segment.type === "mention") {
379 paragraph.append($createComposerMentionNode(segment.path));
380 continue;
381 }
382 $appendTextWithLineBreaks(paragraph, segment.text);
383 }
385​
386export interface ComposerPromptEditorHandle {
387 focus: () => void;
388 focusAt: (cursor: number) => void;
389 focusAtEnd: () => void;
390 readSnapshot: () => { value: string; cursor: number };
392​
393interface ComposerPromptEditorProps {
394 value: string;
395 cursor: number;
396 disabled: boolean;
397 placeholder: string;
398 className?: string;
399 onChange: (nextValue: string, nextCursor: number, cursorAdjacentToMention: boolean) => void;
400 onCommandKeyDown?: (
401 key: "ArrowDown" | "ArrowUp" | "Enter" | "Tab",
402 event: KeyboardEvent,
403 ) => boolean;
404 onPaste: ClipboardEventHandler<HTMLElement>;
406​
407interface ComposerPromptEditorInnerProps extends ComposerPromptEditorProps {
408 editorRef: Ref<ComposerPromptEditorHandle>;
410​
411function ComposerCommandKeyPlugin(props: {
412 onCommandKeyDown?: (
413 key: "ArrowDown" | "ArrowUp" | "Enter" | "Tab",
414 event: KeyboardEvent,
415 ) => boolean;
416}) {
417 const [editor] = useLexicalComposerContext();
418​
419 useEffect(() => {
420 const handleCommand = (
421 key: "ArrowDown" | "ArrowUp" | "Enter" | "Tab",
422 event: KeyboardEvent | null,
423 ): boolean => {
424 if (!props.onCommandKeyDown || !event) {
425 return false;
426 }
427 const handled = props.onCommandKeyDown(key, event);
428 if (handled) {
429 event.preventDefault();
430 event.stopPropagation();
431 }
432 return handled;
433 };
434​
435 const unregisterArrowDown = editor.registerCommand(
436 KEY_ARROW_DOWN_COMMAND,
437 (event) => handleCommand("ArrowDown", event),
438 COMMAND_PRIORITY_HIGH,
439 );
440 const unregisterArrowUp = editor.registerCommand(
441 KEY_ARROW_UP_COMMAND,
442 (event) => handleCommand("ArrowUp", event),
443 COMMAND_PRIORITY_HIGH,
444 );
445 const unregisterEnter = editor.registerCommand(
446 KEY_ENTER_COMMAND,
447 (event) => handleCommand("Enter", event),
448 COMMAND_PRIORITY_HIGH,
449 );
450 const unregisterTab = editor.registerCommand(
451 KEY_TAB_COMMAND,
452 (event) => handleCommand("Tab", event),
453 COMMAND_PRIORITY_HIGH,
454 );
455​
456 return () => {
457 unregisterArrowDown();
458 unregisterArrowUp();
459 unregisterEnter();
460 unregisterTab();
461 };
462 }, [editor, props]);
463​
464 return null;
466​
467function ComposerMentionArrowPlugin() {
468 const [editor] = useLexicalComposerContext();
469​
470 useEffect(() => {
471 const unregisterLeft = editor.registerCommand(
472 KEY_ARROW_LEFT_COMMAND,
473 (event) => {
474 let nextOffset: number | null = null;
475 editor.getEditorState().read(() => {
476 const selection = $getSelection();
477 if (!$isRangeSelection(selection) || !selection.isCollapsed()) return;
478 const currentOffset = $readSelectionOffsetFromEditorState(0);
479 if (currentOffset <= 0) return;
480 const promptValue = $getRoot().getTextContent();
481 if (!isCollapsedCursorAdjacentToMention(promptValue, currentOffset, "left")) {
482 return;
483 }
484 nextOffset = currentOffset - 1;
485 });
486 if (nextOffset === null) return false;
487 const selectionOffset = nextOffset;
488 event?.preventDefault();
489 event?.stopPropagation();
490 editor.update(() => {
491 $setSelectionAtComposerOffset(selectionOffset);
492 });
493 return true;
494 },
495 COMMAND_PRIORITY_HIGH,
496 );
497 const unregisterRight = editor.registerCommand(
498 KEY_ARROW_RIGHT_COMMAND,
499 (event) => {
500 let nextOffset: number | null = null;
501 editor.getEditorState().read(() => {
502 const selection = $getSelection();
503 if (!$isRangeSelection(selection) || !selection.isCollapsed()) return;
504 const currentOffset = $readSelectionOffsetFromEditorState(0);
505 const composerLength = $getComposerRootLength();
506 if (currentOffset >= composerLength) return;
507 const promptValue = $getRoot().getTextContent();
508 if (!isCollapsedCursorAdjacentToMention(promptValue, currentOffset, "right")) {
509 return;
510 }
511 nextOffset = currentOffset + 1;
512 });
513 if (nextOffset === null) return false;
514 const selectionOffset = nextOffset;
515 event?.preventDefault();
516 event?.stopPropagation();
517 editor.update(() => {
518 $setSelectionAtComposerOffset(selectionOffset);
519 });
520 return true;
521 },
522 COMMAND_PRIORITY_HIGH,
523 );
524 return () => {
525 unregisterLeft();
526 unregisterRight();
527 };
528 }, [editor]);
529​
530 return null;
532​
533function ComposerMentionSelectionNormalizePlugin() {
534 const [editor] = useLexicalComposerContext();
535​
536 useEffect(() => {
537 return editor.registerUpdateListener(({ editorState }) => {
538 let afterOffset: number | null = null;
539 editorState.read(() => {
540 const selection = $getSelection();
541 if (!$isRangeSelection(selection) || !selection.isCollapsed()) return;
542 const anchorNode = selection.anchor.getNode();
543 if (!(anchorNode instanceof ComposerMentionNode)) return;
544 if (selection.anchor.offset === 0) return;
545 const beforeOffset = getAbsoluteOffsetForPoint(anchorNode, 0);
546 afterOffset = beforeOffset + 1;
547 });
548 if (afterOffset !== null) {
549 queueMicrotask(() => {
550 editor.update(() => {
551 $setSelectionAtComposerOffset(afterOffset!);
552 });
553 });
554 }
555 });
556 }, [editor]);
557​
558 return null;
560​
561function ComposerMentionBackspacePlugin() {
562 const [editor] = useLexicalComposerContext();
563​
564 useEffect(() => {
565 return editor.registerCommand(
566 KEY_BACKSPACE_COMMAND,
567 (event) => {
568 const selection = $getSelection();
569 if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
570 return false;
571 }
572​
573 const anchorNode = selection.anchor.getNode();
574 const removeMentionNode = (candidate: unknown): boolean => {
575 if (!(candidate instanceof ComposerMentionNode)) {
576 return false;
577 }
578 const mentionStart = getAbsoluteOffsetForPoint(candidate, 0);
579 candidate.remove();
580 $setSelectionAtComposerOffset(mentionStart);
581 event?.preventDefault();
582 return true;
583 };
584​
585 if (removeMentionNode(anchorNode)) {
586 return true;
587 }
588​
589 if ($isTextNode(anchorNode)) {
590 if (selection.anchor.offset > 0) {
591 return false;
592 }
593 if (removeMentionNode(anchorNode.getPreviousSibling())) {
594 return true;
595 }
596 const parent = anchorNode.getParent();
597 if ($isElementNode(parent)) {
598 const index = anchorNode.getIndexWithinParent();
599 if (index > 0 && removeMentionNode(parent.getChildAtIndex(index - 1))) {
600 return true;
601 }
602 }
603 return false;
604 }
605​
606 if ($isElementNode(anchorNode)) {
607 const childIndex = selection.anchor.offset - 1;
608 if (childIndex >= 0 && removeMentionNode(anchorNode.getChildAtIndex(childIndex))) {
609 return true;
610 }
611 }
612​
613 return false;
614 },
615 COMMAND_PRIORITY_HIGH,
616 );
617 }, [editor]);
618​
619 return null;
621​
622function ComposerPromptEditorInner({
623 value,
624 cursor,
625 disabled,
626 placeholder,
627 className,
628 onChange,
629 onCommandKeyDown,
630 onPaste,
631 editorRef,
632}: ComposerPromptEditorInnerProps) {
633 const [editor] = useLexicalComposerContext();
634 const onChangeRef = useRef(onChange);
635 const snapshotRef = useRef({ value, cursor: clampCursor(value, cursor) });
636​
637 useEffect(() => {
638 onChangeRef.current = onChange;
639 }, [onChange]);
640​
641 useEffect(() => {
642 editor.setEditable(!disabled);
643 }, [disabled, editor]);
644​
645 useLayoutEffect(() => {
646 const normalizedCursor = clampCursor(value, cursor);
647 const previousSnapshot = snapshotRef.current;
648 if (previousSnapshot.value === value && previousSnapshot.cursor === normalizedCursor) {
649 return;
650 }
651​
652 if (previousSnapshot.value !== value) {
653 editor.update(() => {
654 $setComposerEditorPrompt(value);
655 });
656 }
657​
658 snapshotRef.current = { value, cursor: normalizedCursor };
659​
660 const rootElement = editor.getRootElement();
661 if (!rootElement || document.activeElement !== rootElement) {
662 return;
663 }
664​
665 editor.update(() => {
666 $setSelectionAtComposerOffset(normalizedCursor);
667 });
668 }, [cursor, editor, value]);
669​
670 const focusAt = useCallback(
671 (nextCursor: number) => {
672 const rootElement = editor.getRootElement();
673 if (!rootElement) return;
674 const boundedCursor = clampCursor(snapshotRef.current.value, nextCursor);
675 rootElement.focus();
676 editor.update(() => {
677 $setSelectionAtComposerOffset(boundedCursor);
678 });
679 snapshotRef.current = {
680 value: snapshotRef.current.value,
681 cursor: boundedCursor,
682 };
683 onChangeRef.current(snapshotRef.current.value, boundedCursor, false);
684 },
685 [editor],
686 );
687​
688 const readSnapshot = useCallback((): { value: string; cursor: number } => {
689 let snapshot = snapshotRef.current;
690 editor.getEditorState().read(() => {
691 const nextValue = $getRoot().getTextContent();
692 const fallbackCursor = clampCursor(nextValue, snapshotRef.current.cursor);
693 const nextCursor = clampCursor(
694 nextValue,
695 $readSelectionOffsetFromEditorState(fallbackCursor),
696 );
697 snapshot = {
698 value: nextValue,
699 cursor: nextCursor,
700 };
701 });
702 snapshotRef.current = snapshot;
703 return snapshot;
704 }, [editor]);
705​
706 useImperativeHandle(
707 editorRef,
708 () => ({
709 focus: () => {
710 focusAt(snapshotRef.current.cursor);
711 },
712 focusAt: (nextCursor: number) => {
713 focusAt(nextCursor);
714 },
715 focusAtEnd: () => {
716 focusAt(snapshotRef.current.value.length);
717 },
718 readSnapshot,
719 }),
720 [focusAt, readSnapshot],
721 );
722​
723 const handleEditorChange = useCallback((editorState: EditorState) => {
724 editorState.read(() => {
725 const nextValue = $getRoot().getTextContent();
726 const fallbackCursor = clampCursor(nextValue, snapshotRef.current.cursor);
727 const nextCursor = clampCursor(nextValue, $readSelectionOffsetFromEditorState(fallbackCursor));
728 const previousSnapshot = snapshotRef.current;
729 if (previousSnapshot.value === nextValue && previousSnapshot.cursor === nextCursor) {
730 return;
731 }
732 snapshotRef.current = {
733 value: nextValue,
734 cursor: nextCursor,
735 };
736 const cursorAdjacentToMention =
737 isCollapsedCursorAdjacentToMention(nextValue, nextCursor, "left") ||
738 isCollapsedCursorAdjacentToMention(nextValue, nextCursor, "right");
739 onChangeRef.current(nextValue, nextCursor, cursorAdjacentToMention);
740 });
741 }, []);
742​
743 return (
744 <div className="relative">
745 <PlainTextPlugin
746 contentEditable={
747 <ContentEditable
748 className={cn(
749 "block max-h-[200px] min-h-17.5 w-full overflow-y-auto whitespace-pre-wrap break-words bg-transparent text-[14px] leading-relaxed text-foreground focus:outline-none",
750 className,
751 )}
752 aria-placeholder={placeholder}
753 placeholder={<span />}
754 onPaste={onPaste}
755 />
756 }
757 placeholder={
758 <div className="pointer-events-none absolute inset-0 text-[14px] leading-relaxed text-muted-foreground/35">
759 {placeholder}
760 </div>
761 }
762 ErrorBoundary={LexicalErrorBoundary}
763 />
764 <OnChangePlugin onChange={handleEditorChange} />
765 <ComposerCommandKeyPlugin {...(onCommandKeyDown ? { onCommandKeyDown } : {})} />
766 <ComposerMentionArrowPlugin />
767 <ComposerMentionSelectionNormalizePlugin />
768 <ComposerMentionBackspacePlugin />
769 <HistoryPlugin />
770 </div>
771 );
773​
774export const ComposerPromptEditor = forwardRef<ComposerPromptEditorHandle, ComposerPromptEditorProps>(
775 function ComposerPromptEditor(
776 { value, cursor, disabled, placeholder, className, onChange, onCommandKeyDown, onPaste },
777 ref,
778 ) {
779 const initialValueRef = useRef(value);
780 const initialConfig = useMemo<InitialConfigType>(
781 () => ({
782 namespace: "t3tools-composer-editor",
783 editable: true,
784 nodes: [ComposerMentionNode],
785 editorState: () => {
786 $setComposerEditorPrompt(initialValueRef.current);
787 },
788 onError: (error) => {
789 throw error;
790 },
791 }),
792 [],
793 );
794​
795 return (
796 <LexicalComposer key={COMPOSER_EDITOR_HMR_KEY} initialConfig={initialConfig}>
797 <ComposerPromptEditorInner
798 value={value}
799 cursor={cursor}
800 disabled={disabled}
801 placeholder={placeholder}
802 onChange={onChange}
803 onPaste={onPaste}
804 editorRef={ref}
805 {...(onCommandKeyDown ? { onCommandKeyDown } : {})}
806 {...(className ? { className } : {})}
807 />
808 </LexicalComposer>
809 );
810 },
811);