From 47f30277533e833e9d46323779a8d9010161d96c Mon Sep 17 00:00:00 2001 From: Petyo Ivanov Date: Mon, 23 Oct 2023 16:13:05 +0300 Subject: [PATCH] fix: link dialog button in FF Fixes #135 --- src/plugins/link-dialog/LinkDialog.tsx | 8 +-- src/plugins/link-dialog/index.ts | 51 ++++++++++--------- src/plugins/toolbar/components/CreateLink.tsx | 7 ++- src/utils/lexicalHelpers.ts | 8 +-- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/plugins/link-dialog/LinkDialog.tsx b/src/plugins/link-dialog/LinkDialog.tsx index 488864d2..b3fd7722 100644 --- a/src/plugins/link-dialog/LinkDialog.tsx +++ b/src/plugins/link-dialog/LinkDialog.tsx @@ -132,10 +132,10 @@ export const LinkDialog: React.FC = () => { data-visible={linkDialogState.type === 'edit'} className={styles.linkDialogAnchor} style={{ - top: theRect?.top, - left: theRect?.left, - width: theRect?.width, - height: theRect?.height + top: `${theRect?.top ?? 0}px`, + left: `${theRect?.left ?? 0}px`, + width: `${theRect?.width ?? 0}px`, + height: `${theRect?.height ?? 0}px` }} /> diff --git a/src/plugins/link-dialog/index.ts b/src/plugins/link-dialog/index.ts index 5674562b..aab0c849 100644 --- a/src/plugins/link-dialog/index.ts +++ b/src/plugins/link-dialog/index.ts @@ -79,30 +79,32 @@ const linkDialogSystem = system( r.o.filter(([, selection]) => $isRangeSelection(selection)) ), ([, selection, editor]) => { - editor?.getEditorState().read(() => { - const node = getLinkNodeInSelection(selection) - const rectangle = getSelectionRectangle(editor)! - if (node) { - r.pub(linkDialogState, { - type: 'edit', - initialUrl: node.getURL(), - initialTitle: node.getTitle() || '', - url: node.getURL(), - title: node.getTitle() || '', - linkNodeKey: node.getKey(), - rectangle - }) - } else { - r.pub(linkDialogState, { - type: 'edit', - initialUrl: '', - initialTitle: '', - title: '', - url: '', - linkNodeKey: '', - rectangle - }) - } + editor?.focus(() => { + editor?.getEditorState().read(() => { + const node = getLinkNodeInSelection(selection) + const rectangle = getSelectionRectangle(editor)! + if (node) { + r.pub(linkDialogState, { + type: 'edit', + initialUrl: node.getURL(), + initialTitle: node.getTitle() || '', + url: node.getURL(), + title: node.getTitle() || '', + linkNodeKey: node.getKey(), + rectangle + }) + } else { + r.pub(linkDialogState, { + type: 'edit', + initialUrl: '', + initialTitle: '', + title: '', + url: '', + linkNodeKey: '', + rectangle + }) + } + }) }) } ) @@ -137,6 +139,7 @@ const linkDialogSystem = system( if ($isRangeSelection(selection) && (getLinkNodeInSelection(selection) || !selection.isCollapsed())) { r.pub(openLinkEditDialog, true) event.stopPropagation() + event.preventDefault() return true } else { return false diff --git a/src/plugins/toolbar/components/CreateLink.tsx b/src/plugins/toolbar/components/CreateLink.tsx index 260b715e..cf2ce025 100644 --- a/src/plugins/toolbar/components/CreateLink.tsx +++ b/src/plugins/toolbar/components/CreateLink.tsx @@ -10,7 +10,12 @@ import { linkDialogPluginHooks } from '../../link-dialog' export const CreateLink = () => { const openLinkDialog = linkDialogPluginHooks.usePublisher('openLinkEditDialog') return ( - openLinkDialog(true)}> + { + openLinkDialog(true) + }} + > ) diff --git a/src/utils/lexicalHelpers.ts b/src/utils/lexicalHelpers.ts index a71b616d..52068f2e 100644 --- a/src/utils/lexicalHelpers.ts +++ b/src/utils/lexicalHelpers.ts @@ -58,10 +58,10 @@ export function getSelectionRectangle(editor: LexicalEditor) { } return { - top: rect.top, - left: rect.left, - width: rect.width, - height: rect.height + top: Math.round(rect.top), + left: Math.round(rect.left), + width: Math.round(rect.width), + height: Math.round(rect.height) } } else if (!activeElement || activeElement.className !== 'link-input') { return null