import { $isCodeHighlightNode } from '@lexical/code'; import { $isLinkNode, TOGGLE_LINK_COMMAND } from '@lexical/link'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import { $isAtNodeEnd } from '@lexical/selection'; import { mergeRegister } from '@lexical/utils'; import { $getSelection, $isRangeSelection, $isTextNode, COMMAND_PRIORITY_LOW, ElementNode, FORMAT_TEXT_COMMAND, LexicalEditor, RangeSelection, SELECTION_CHANGE_COMMAND, TextNode, } from 'lexical'; import * as React from 'react'; import { useCallback, useEffect, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import { TFunction } from 'i18next'; import { useTranslation } from 'react-i18next'; import { BlockFormatDropdown } from '../plugins/ToolbarPlugin/components'; function setPopupPosition( editor: HTMLElement, rect: ClientRect, rootElementRect: ClientRect ): void { let top = rect.top - 8 + window.pageYOffset; let left = rect.left + 340 + window.pageXOffset - editor.offsetWidth + rect.width; if ( rect.width >= rootElementRect.width - 20 || left > rootElementRect.width - 150 ) { left = rect.left; top = rect.top - 50 + window.pageYOffset; } if (top < rootElementRect.top) { top = rect.bottom + 20; } editor.style.opacity = '1'; editor.style.top = `${top}px`; editor.style.left = `${left}px`; } function FloatingCharacterStylesEditor({ editor, isLink, isBold, isItalic, isUnderline, isCode, isStrikethrough, isSubscript, isSuperscript, t, }: { editor: LexicalEditor; isBold: boolean; isCode: boolean; isItalic: boolean; isLink: boolean; isStrikethrough: boolean; isSubscript: boolean; isSuperscript: boolean; isUnderline: boolean; t: TFunction; }): JSX.Element { const popupCharStylesEditorRef = useRef(null); const mouseDownRef = useRef(false); const insertLink = useCallback(() => { if (!isLink) { editor.dispatchCommand(TOGGLE_LINK_COMMAND, ''); } else { editor.dispatchCommand(TOGGLE_LINK_COMMAND, null); } }, [editor, isLink]); const updateCharacterStylesEditor = useCallback(() => { const selection = $getSelection(); const popupCharStylesEditorElem = popupCharStylesEditorRef.current; const nativeSelection = window.getSelection(); if (popupCharStylesEditorElem === null) { return; } const rootElement = editor.getRootElement(); if ( selection !== null && nativeSelection !== null && !nativeSelection.isCollapsed && rootElement !== null && rootElement.contains(nativeSelection.anchorNode) ) { const domRange = nativeSelection.getRangeAt(0); const rootElementRect = rootElement.getBoundingClientRect(); let rect; if (nativeSelection.anchorNode === rootElement) { let inner = rootElement; while (inner.firstElementChild != null) { inner = inner.firstElementChild as HTMLElement; } rect = inner.getBoundingClientRect(); } else { rect = domRange.getBoundingClientRect(); } if (!mouseDownRef.current) { setPopupPosition(popupCharStylesEditorElem, rect, rootElementRect); } } }, [editor]); useEffect(() => { const onResize = () => { editor.getEditorState().read(() => { updateCharacterStylesEditor(); }); }; window.addEventListener('resize', onResize); return () => { window.removeEventListener('resize', onResize); }; }, [editor, updateCharacterStylesEditor]); useEffect(() => { editor.getEditorState().read(() => { updateCharacterStylesEditor(); }); return mergeRegister( editor.registerUpdateListener(({ editorState }) => { editorState.read(() => { updateCharacterStylesEditor(); }); }), editor.registerCommand( SELECTION_CHANGE_COMMAND, () => { updateCharacterStylesEditor(); return false; }, COMMAND_PRIORITY_LOW ) ); }, [editor, updateCharacterStylesEditor]); return (
); } function getSelectedNode(selection: RangeSelection): TextNode | ElementNode { const anchor = selection.anchor; const focus = selection.focus; const anchorNode = selection.anchor.getNode(); const focusNode = selection.focus.getNode(); if (anchorNode === focusNode) { return anchorNode; } const isBackward = selection.isBackward(); if (isBackward) { return $isAtNodeEnd(focus) ? anchorNode : focusNode; } else { return $isAtNodeEnd(anchor) ? focusNode : anchorNode; } } function useCharacterStylesPopup(editor: LexicalEditor): JSX.Element | null { const [isText, setIsText] = useState(false); const [isLink, setIsLink] = useState(false); const [isBold, setIsBold] = useState(false); const [isItalic, setIsItalic] = useState(false); const [isUnderline, setIsUnderline] = useState(false); const [isStrikethrough, setIsStrikethrough] = useState(false); const [isSubscript, setIsSubscript] = useState(false); const [isSuperscript, setIsSuperscript] = useState(false); const [isCode, setIsCode] = useState(false); const { t } = useTranslation(['toolbar']); const updatePopup = useCallback(() => { editor.getEditorState().read(() => { const selection = $getSelection(); const nativeSelection = window.getSelection(); const rootElement = editor.getRootElement(); if ( nativeSelection !== null && (!$isRangeSelection(selection) || rootElement === null || !rootElement.contains(nativeSelection.anchorNode)) ) { setIsText(false); return; } if (!$isRangeSelection(selection)) { return; } const node = getSelectedNode(selection); // Update text format setIsBold(selection.hasFormat('bold')); setIsItalic(selection.hasFormat('italic')); setIsUnderline(selection.hasFormat('underline')); setIsStrikethrough(selection.hasFormat('strikethrough')); setIsSubscript(selection.hasFormat('subscript')); setIsSuperscript(selection.hasFormat('superscript')); setIsCode(selection.hasFormat('code')); // Update links const parent = node.getParent(); if ($isLinkNode(parent) || $isLinkNode(node)) { setIsLink(true); } else { setIsLink(false); } if ( !$isCodeHighlightNode(selection.anchor.getNode()) && selection.getTextContent() !== '' ) { setIsText($isTextNode(node)); } else { setIsText(false); } }); }, [editor]); useEffect(() => { document.addEventListener('selectionchange', updatePopup); return () => { document.removeEventListener('selectionchange', updatePopup); }; }, [updatePopup]); useEffect(() => { return editor.registerUpdateListener(() => { updatePopup(); }); }, [editor, updatePopup]); if (!isText || isLink) { return null; } return createPortal( , document.body ); } export default function CharacterStylesPopupPlugin(): JSX.Element | null { const [editor] = useLexicalComposerContext(); if (!editor.isEditable()) return null; return useCharacterStylesPopup(editor); }