"use client" import { useMemo, useState } from "react" import type { ComponentProps, ComponentType, ReactNode } from "react" import { LexicalComposer } from "@lexical/react/LexicalComposer" import { TextNode } from "lexical" import { I18nProvider, useI18nProvider } from "@workspace/i18n" import { cn } from "@workspace/ui/lib/utils" import { LexicalTextNode } from "./nodes/text-node" import { lexicalTheme } from "./theme" import { LexicalEmbedProvider } from "./embed" import { LexicalHtmlPlugin } from "./plugins/html-value-plugin" import { compileLexicalActions, findLexicalActions, } from "./action-declarations" import { LexicalActionsProvider } from "./actions-context" import { messages as englishMessages } from "./locales/en" const pluginKeys = new WeakMap() let nextPluginKey = 0 function getPluginKey(plugin: ComponentType) { const existingKey = pluginKeys.get(plugin) if (existingKey) return existingKey const key = `${plugin.displayName || plugin.name || "plugin"}:${nextPluginKey}` nextPluginKey += 1 pluginKeys.set(plugin, key) return key } export interface LexicalRootProps extends Omit< ComponentProps<"div">, "onChange" > { namespace?: string onChange: (html: string) => void value?: string } function LexicalI18nBoundary({ children }: { children: ReactNode }) { const hasI18nProvider = useI18nProvider() if (hasI18nProvider) return children return ( {children} ) } export function LexicalRoot({ className, children, namespace = "lexical-editor", onChange, value, ...props }: LexicalRootProps) { const [compiledActions] = useState(() => compileLexicalActions(findLexicalActions(children)) ) const initialConfig = useMemo( () => ({ namespace, nodes: [ LexicalTextNode, { replace: TextNode, with: (node: TextNode) => new LexicalTextNode(node.getTextContent()), withKlass: LexicalTextNode, }, ...compiledActions.nodes, ], onError: (error: Error) => { throw error }, theme: lexicalTheme, }), [compiledActions.nodes, namespace] ) return (
{children}
{compiledActions.plugins.map((Plugin) => ( ))}
) }