import type { LexicalCommand } from 'lexical'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import { COMMAND_PRIORITY_EDITOR, createCommand } from 'lexical'; import { useEffect } from 'react'; import { $insertNodeToNearestRoot } from '@lexical/utils'; import { $createYouTubeNode, YouTubeNode } from '../nodes/YouTubeNode'; export const INSERT_YOUTUBE_COMMAND: LexicalCommand = createCommand(); export default function YouTubePlugin(): JSX.Element { const [editor] = useLexicalComposerContext(); useEffect(() => { if (!editor.hasNodes([YouTubeNode])) { throw new Error('YouTubePlugin: YouTubeNode not registered on editor'); } return editor.registerCommand( INSERT_YOUTUBE_COMMAND, (payload) => { const youTubeNode = $createYouTubeNode(payload); $insertNodeToNearestRoot(youTubeNode); return true; }, COMMAND_PRIORITY_EDITOR ); }, [editor]); return null; }