From 73e6a598e95190db3024393fc8e7cfa12655682f Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Sun, 12 May 2024 21:25:05 +0530 Subject: [PATCH] chore: super note link popup changes [skip e2e] --- .../Plugins/ToolbarPlugin/LinkViewer.tsx | 13 +++++++++++-- .../Plugins/ToolbarPlugin/ToolbarPlugin.tsx | 6 ++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/web/src/javascripts/Components/SuperEditor/Plugins/ToolbarPlugin/LinkViewer.tsx b/packages/web/src/javascripts/Components/SuperEditor/Plugins/ToolbarPlugin/LinkViewer.tsx index 9f298182e..d0628e5d1 100644 --- a/packages/web/src/javascripts/Components/SuperEditor/Plugins/ToolbarPlugin/LinkViewer.tsx +++ b/packages/web/src/javascripts/Components/SuperEditor/Plugins/ToolbarPlugin/LinkViewer.tsx @@ -32,6 +32,10 @@ const LinkViewer = ({ isMobile, editor, linkNode, setIsEditingLink }: Props) => return [linkUrl, isAutoLink] }, [editor, linkNode]) + const linkNodeDOM = useMemo(() => { + return editor.getElementByKey(linkNode.getKey()) + }, [editor, linkNode]) + const rangeRect = useRef() const updateLinkEditorPosition = useCallback(() => { if (isMobile) { @@ -58,13 +62,18 @@ const LinkViewer = ({ isMobile, editor, linkNode, setIsEditingLink }: Props) => return } + if (!linkNodeDOM) { + return + } + const linkEditorRect = linkEditorElement.getBoundingClientRect() const rootElementRect = rootElement.getBoundingClientRect() + const linkNodeRect = linkNodeDOM.getBoundingClientRect() const calculatedStyles = getPositionedPopoverStyles({ align: 'center', side: 'top', - anchorRect: rangeRect.current, + anchorRect: linkNodeRect, popoverRect: linkEditorRect, documentRect: rootElementRect, offset: 12, @@ -77,7 +86,7 @@ const LinkViewer = ({ isMobile, editor, linkNode, setIsEditingLink }: Props) => }) linkEditorElement.style.opacity = '1' } - }, [editor, isMobile]) + }, [editor, isMobile, linkNodeDOM]) useElementResize(linkViewerRef.current, updateLinkEditorPosition) diff --git a/packages/web/src/javascripts/Components/SuperEditor/Plugins/ToolbarPlugin/ToolbarPlugin.tsx b/packages/web/src/javascripts/Components/SuperEditor/Plugins/ToolbarPlugin/ToolbarPlugin.tsx index cb3092d8e..69a86fcea 100644 --- a/packages/web/src/javascripts/Components/SuperEditor/Plugins/ToolbarPlugin/ToolbarPlugin.tsx +++ b/packages/web/src/javascripts/Components/SuperEditor/Plugins/ToolbarPlugin/ToolbarPlugin.tsx @@ -313,6 +313,8 @@ const ToolbarPlugin = () => { } const anchorNode = selection.anchor.getNode() + const focusNode = selection.focus.getNode() + const isAnchorSameAsFocus = anchorNode === focusNode let element = anchorNode.getKey() === 'root' ? anchorNode @@ -342,9 +344,9 @@ const ToolbarPlugin = () => { const node = getSelectedNode(selection) const parent = node.getParent() setIsEditingLink(false) - if ($isLinkNode(node)) { + if ($isLinkNode(node) && isAnchorSameAsFocus) { setLinkNode(node) - } else if ($isLinkNode(parent)) { + } else if ($isLinkNode(parent) && isAnchorSameAsFocus) { setLinkNode(parent) } else { setLinkNode(null)