import type { ReactNode, RefObject } from "react"; import { useEffect, useImperativeHandle, useRef, useState } from "react"; import clsx from "clsx"; import type { LexicalEditor, SerializedEditorState } from "lexical"; import { $getRoot } from "lexical"; import { InitialConfigType, LexicalComposer } from "@lexical/react/LexicalComposer"; import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin"; import { ContentEditable } from "@lexical/react/LexicalContentEditable"; import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary"; import { EditorRefPlugin } from "@lexical/react/LexicalEditorRefPlugin"; import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin"; import { ListPlugin } from "@lexical/react/LexicalListPlugin"; import { HorizontalRulePlugin } from "@lexical/react/LexicalHorizontalRulePlugin"; import { ToolbarPlugin } from "./plugins/ToolbarPlugin"; import { LazyOnChangePlugin } from "./plugins/LazyOnChangePlugin"; import { CustomAutoLinkPlugin } from "./plugins/AutoLinkPlugin"; import { FloatingLinkEditorPlugin } from "./plugins/FloatingLinkEditorPlugin"; import { CustomLinkPlugin } from "./plugins/LinkPlugin"; import { editorConfig } from "./config/editor"; import { $loadFromHtml } from "./config/html"; import type { ToolbarVariantProps } from "./style"; import { contentEditableStyles } from "./style"; import { ToolbarContext } from "./ToolbarContext"; import { WysiwygValue } from "./types"; interface Props { id?: string; extendedToolbar?: boolean; floatingPosition?: "top" | "bottom"; defaultValue?: WysiwygValue; disabled?: boolean; readOnly?: boolean; proseCompact?: boolean; contentEditableBaseStyle?: "normal" | "withoutToolbarOnMobile"; /** * If you want to define custom height */ contentEditableClassName?: string; containerClassName?: string; /** * by default sticky is only available for mobile device with a top-0 if * you want to change this provide your own className */ toolbarSticky?: ToolbarVariantProps["sticky"]; toolbarVisible?: ToolbarVariantProps["visible"]; ref?: RefObject; debounceChange?: number | false; onChange?: (value: WysiwygValue) => void; children?: ReactNode; } function readValue(editor: LexicalEditor): WysiwygValue { return { state: editor.getEditorState().toJSON(), }; } export interface WysiwygRef { getValue: () => WysiwygValue; setHtml: (html: string, fireEvent?: boolean) => void; getState: () => SerializedEditorState; setState: (state: SerializedEditorState, fireEvent?: boolean) => void; clear: (fireEvent?: boolean) => void; } export function Wysiwyg({ id, floatingPosition = "bottom", extendedToolbar = true, proseCompact = false, defaultValue, disabled = false, readOnly = false, contentEditableBaseStyle = "normal", contentEditableClassName, containerClassName, ref, toolbarSticky, toolbarVisible, debounceChange = false, onChange, children, }: Props) { const [fullInitialConfig] = useState(() => ({ ...editorConfig, editable: !disabled && !readOnly, ...(defaultValue ? { editorState: JSON.stringify(defaultValue.state) } : {}), })); const editorRef = useRef(null); useEffect(() => { editorRef.current?.setEditable(!disabled && !readOnly); }, [disabled, readOnly]); const [floatingAnchorElem, setFloatingAnchorElem] = useState(null); const [isLinkEditMode, setIsLinkEditMode] = useState(false); const onRef = (floatingAnchorElem: HTMLDivElement) => { if (floatingAnchorElem !== null) { setFloatingAnchorElem(floatingAnchorElem); } }; function getEditor(): LexicalEditor { const editor = editorRef.current; if (!editor) { throw new Error("Wysiwyg ref called before the editor was mounted"); } return editor; } function notifyChange(editor: LexicalEditor) { onChange?.(readValue(editor)); } useImperativeHandle(ref, () => { return { getValue: () => { const editor = getEditor(); return readValue(editor); }, setHtml: (nextHtml: string, fireEvent = false) => { const editor = getEditor(); editor.update(() => { $loadFromHtml(editor, nextHtml); }); if (fireEvent) { notifyChange(editor); } }, getState: () => { return getEditor().getEditorState().toJSON(); }, setState: (editorState: SerializedEditorState, fireEvent = false) => { const editor = getEditor(); const parsedEditorState = editor.parseEditorState(editorState); editor.setEditorState(parsedEditorState); if (fireEvent) { notifyChange(editor); } }, clear: (fireEvent = false) => { const editor = getEditor(); editor.update(() => { const root = $getRoot(); root.getChildren().forEach((node) => { node.remove(); }); }); if (fireEvent) { notifyChange(editor); } }, }; }); return (
{debounceChange && }
{floatingAnchorElem && ( )} {children} } ErrorBoundary={LexicalErrorBoundary} />
); }