/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import 'katex/dist/katex.css'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import { $wrapNodeInElement } from '@lexical/utils'; import { $createParagraphNode, $insertNodes, $isRootOrShadowRoot, COMMAND_PRIORITY_EDITOR, createCommand, LexicalCommand, LexicalEditor, } from 'lexical'; import { useCallback, useEffect } from 'react'; import * as React from 'react'; import KatexEquationAlterer from '../ui/KatexEquationAlterer'; import { $createEquationNode, EquationNode } from '../nodes/EquationNode'; type CommandPayload = { equation: string; inline: boolean; }; export const INSERT_EQUATION_COMMAND: LexicalCommand = createCommand('INSERT_EQUATION_COMMAND'); export function InsertEquationDialog({ activeEditor, onClose, }: { activeEditor: LexicalEditor; onClose: () => void; }): JSX.Element { const onEquationConfirm = useCallback( (equation: string, inline: boolean) => { activeEditor.dispatchCommand(INSERT_EQUATION_COMMAND, { equation, inline, }); onClose(); }, [activeEditor, onClose] ); return ; } export default function EquationsPlugin(): JSX.Element | null { const [editor] = useLexicalComposerContext(); useEffect(() => { if (!editor.hasNodes([EquationNode])) { throw new Error( 'EquationsPlugins: EquationsNode not registered on editor' ); } return editor.registerCommand( INSERT_EQUATION_COMMAND, (payload) => { const { equation, inline } = payload; const equationNode = $createEquationNode(equation, inline); $insertNodes([equationNode]); if ($isRootOrShadowRoot(equationNode.getParentOrThrow())) { $wrapNodeInElement(equationNode, $createParagraphNode).selectEnd(); } return true; }, COMMAND_PRIORITY_EDITOR ); }, [editor]); return null; }