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

ComposerPromptEditor.tsx

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