import { $isLinkNode, TOGGLE_LINK_COMMAND } from '@lexical/link'; import { $isAtNodeEnd } from '@lexical/selection'; import { mergeRegister } from '@lexical/utils'; import { $getSelection, $isRangeSelection, COMMAND_PRIORITY_LOW, ElementNode, LexicalEditor, RangeSelection, SELECTION_CHANGE_COMMAND, TextNode, } from 'lexical'; import React from 'react'; import { useCallback, useEffect, useRef, useState } from 'react'; import LinkPreview from '../../../ui/LinkPreview'; import { getSelectedNode } from '../../../utils/node.util'; function positionEditorElement(editor, rect) { if (rect === null) { editor.style.opacity = '0'; editor.style.top = '-1000px'; editor.style.left = '-1000px'; } else { editor.style.opacity = '1'; editor.style.top = `${rect.top + rect.height + window.pageYOffset + 10}px`; editor.style.left = `${ rect.left + window.pageXOffset - editor.offsetWidth / 2 + rect.width / 2 }px`; } } function FloatingLinkEditor({ editor, }: { editor: LexicalEditor; }): JSX.Element { const editorRef = useRef(null); const inputRef = useRef(null); const [linkUrl, setLinkUrl] = useState(''); const [isEditMode, setEditMode] = useState(true); const [lastSelection, setLastSelection] = useState(null); const updateLinkEditor = useCallback(() => { const selection = $getSelection(); if ($isRangeSelection(selection)) { const node = getSelectedNode(selection); const parent = node.getParent(); if ($isLinkNode(parent)) { setLinkUrl(parent.getURL()); } else if ($isLinkNode(node)) { setLinkUrl(node.getURL()); } else { setLinkUrl(''); } } const editorElem = editorRef.current; const nativeSelection = window.getSelection(); const activeElement = document.activeElement; if (editorElem === null) { return; } const rootElement = editor.getRootElement(); if ( selection !== null && !nativeSelection.isCollapsed && rootElement !== null && rootElement.contains(nativeSelection.anchorNode) ) { const domRange = nativeSelection.getRangeAt(0); 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(); } positionEditorElement(editorElem, rect); setLastSelection(selection); } else if (!activeElement || activeElement.className !== 'link-input') { positionEditorElement(editorElem, null); setLastSelection(null); setEditMode(false); setLinkUrl(''); } return true; }, [editor]); useEffect(() => { const onResize = () => { editor.getEditorState().read(() => { updateLinkEditor(); }); }; window.addEventListener('resize', onResize); return () => { window.removeEventListener('resize', onResize); }; }, [editor, updateLinkEditor]); useEffect(() => { return mergeRegister( editor.registerUpdateListener(({ editorState }) => { editorState.read(() => { updateLinkEditor(); }); }), editor.registerCommand( SELECTION_CHANGE_COMMAND, () => { updateLinkEditor(); return true; }, COMMAND_PRIORITY_LOW ) ); }, [editor, updateLinkEditor]); useEffect(() => { editor.getEditorState().read(() => { updateLinkEditor(); }); }, [editor, updateLinkEditor]); useEffect(() => { if (isEditMode && inputRef.current) { inputRef.current.focus(); } }, [isEditMode]); return (
{isEditMode ? ( { setLinkUrl(event.target.value); }} placeholder="https://" onKeyDown={(event) => { if (event.key === 'Enter') { event.preventDefault(); if (lastSelection !== null) { if (linkUrl !== '') { editor.dispatchCommand(TOGGLE_LINK_COMMAND, linkUrl); } setEditMode(false); } } else if (event.key === 'Escape') { event.preventDefault(); setEditMode(false); } }} /> ) : ( <>
{ setEditMode(true); }} > event.preventDefault()} > {linkUrl} 
event.preventDefault()} />
)}
); } export default FloatingLinkEditor;