"use client"; import { useEffect, useRef, useState } from "react"; import { $createParagraphNode, $getRoot, type EditorThemeClasses } from "lexical"; import { HorizontalRuleNode } from "@lexical/extension"; import { AutoLinkNode, LinkNode } from "@lexical/link"; import { HeadingNode, QuoteNode } from "@lexical/rich-text"; import { LexicalComposer } from "@lexical/react/LexicalComposer"; import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; import { ContentEditable } from "@lexical/react/LexicalContentEditable"; import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary"; import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin"; import { HorizontalRulePlugin } from "@lexical/react/LexicalHorizontalRulePlugin"; import { CheckListPlugin } from "@lexical/react/LexicalCheckListPlugin"; import { ListPlugin } from "@lexical/react/LexicalListPlugin"; import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin"; import { ListItemNode, ListNode } from "@lexical/list"; import { cn } from "@/lib/utils"; import { LexicalRichTextToolbar } from "./LexicalRichTextToolbar"; export type LexicalJson = Record; const EXTERNAL_STATE_TAG = "polaris-external-state"; const EDITOR_NODES = [ HeadingNode, QuoteNode, ListNode, ListItemNode, HorizontalRuleNode, LinkNode, AutoLinkNode, ] as const; const EDITOR_THEME: EditorThemeClasses = { heading: { h2: "mt-4 mb-2 text-lg font-semibold", h3: "mt-3 mb-2 text-base font-semibold", h4: "mt-2 mb-1 text-sm font-semibold", }, hr: "my-4 border-border", link: "text-primary underline underline-offset-2", list: { checklist: "pl-6", listitem: "mb-1 pl-1", listitemChecked: "text-muted-foreground line-through", listitemUnchecked: "", nested: { list: "pl-6", listitem: "pl-1" }, ol: "mb-4 list-decimal pl-6", ul: "mb-4 list-disc pl-6", }, paragraph: "mb-4 last:mb-0", quote: "mb-4 border-l-2 border-border pl-3 italic text-muted-foreground", text: { bold: "font-semibold", code: "rounded-sm bg-muted px-1 font-mono text-xs", italic: "italic", strikethrough: "line-through", underline: "underline", underlineStrikethrough: "underline line-through", }, }; function serializeValue(value: LexicalJson | null): string | null { return value === null ? null : (JSON.stringify(value) ?? null); } function EditablePlugin({ disabled }: { disabled: boolean }) { const [editor] = useLexicalComposerContext(); useEffect(() => { editor.setEditable(!disabled); }, [disabled, editor]); return null; } function ExternalStatePlugin({ lastEmittedJsonRef, value, }: { lastEmittedJsonRef: { current: string | null }; value: LexicalJson | null; }) { const [editor] = useLexicalComposerContext(); const previousValueRef = useRef(value); useEffect(() => { if (previousValueRef.current === value) return; previousValueRef.current = value; const serializedValue = serializeValue(value); if (serializedValue !== null && serializedValue === lastEmittedJsonRef.current) { lastEmittedJsonRef.current = null; return; } lastEmittedJsonRef.current = null; if (serializedValue === null) { editor.update( () => { const root = $getRoot(); root.clear(); root.append($createParagraphNode()); }, { tag: EXTERNAL_STATE_TAG }, ); return; } editor.setEditorState(editor.parseEditorState(serializedValue), { tag: EXTERNAL_STATE_TAG }); }, [editor, lastEmittedJsonRef, value]); return null; } function SerializeOnChangePlugin({ lastEmittedJsonRef, onChange, }: { lastEmittedJsonRef: { current: string | null }; onChange: (json: LexicalJson) => void; }) { const [editor] = useLexicalComposerContext(); const onChangeRef = useRef(onChange); useEffect(() => { onChangeRef.current = onChange; }, [onChange]); useEffect( () => editor.registerUpdateListener(({ dirtyElements, dirtyLeaves, editorState, tags }) => { if (tags.has(EXTERNAL_STATE_TAG) || (dirtyElements.size === 0 && dirtyLeaves.size === 0)) { return; } const json: LexicalJson = { ...editorState.toJSON() }; lastEmittedJsonRef.current = JSON.stringify(json); onChangeRef.current(json); }), [editor, lastEmittedJsonRef], ); return null; } export interface LexicalRichTextEditorProps { value: LexicalJson | null; onChange: (json: LexicalJson) => void; placeholder?: string; disabled?: boolean; /** Briefings only: adds the color palette used by the SQF exporter. */ showTextColor?: boolean; } export function LexicalRichTextEditor({ disabled = false, onChange, placeholder = "Write the operation details...", showTextColor = false, value, }: LexicalRichTextEditorProps) { const lastEmittedJsonRef = useRef(null); const [initialConfig] = useState(() => ({ namespace: "PolarisSideOpNarrative", nodes: EDITOR_NODES, onError: (error: Error) => console.error("Lexical editor error.", error), editorState: serializeValue(value), theme: EDITOR_THEME, editable: !disabled, })); return (
{placeholder}} /> } placeholder={
{placeholder}
} ErrorBoundary={LexicalErrorBoundary} />
); }