From d3105919f274e1528d00e0af859d42b33c317fc8 Mon Sep 17 00:00:00 2001 From: matthewlipski Date: Wed, 11 Dec 2024 10:19:39 +0100 Subject: [PATCH] Fixed `getSelection` behaviour and build --- .../src/api/blockManipulation/selections/selection.ts | 10 +++++----- .../DefaultButtons/CreateLinkButton.tsx | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/core/src/api/blockManipulation/selections/selection.ts b/packages/core/src/api/blockManipulation/selections/selection.ts index 1eccdd4279..2017581e41 100644 --- a/packages/core/src/api/blockManipulation/selections/selection.ts +++ b/packages/core/src/api/blockManipulation/selections/selection.ts @@ -23,6 +23,11 @@ export function getSelection< ): Selection | undefined { const state = editor._tiptapEditor.state; + // Return undefined if the selection is collapsed or a node is selected. + if (state.selection.empty || "node" in state.selection) { + return undefined; + } + const $startBlockBeforePos = state.doc.resolve( getNearestBlockPos(state.doc, state.selection.from).posBeforeNode ); @@ -30,11 +35,6 @@ export function getSelection< getNearestBlockPos(state.doc, state.selection.to).posBeforeNode ); - // Return undefined if anchor and head are in the same block. - if ($startBlockBeforePos.pos === $endBlockBeforePos.pos) { - return undefined; - } - // Converts the node at the given index and depth around `$startBlockBeforePos` // to a block. Used to get blocks at given indices at the shared depth and // at the depth of `$startBlockBeforePos`. diff --git a/packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.tsx b/packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.tsx index 889eb27c7a..3e3b966729 100644 --- a/packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.tsx +++ b/packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.tsx @@ -66,12 +66,12 @@ export const CreateLinkButton = () => { } }; - editor.prosemirrorView.dom.addEventListener("keydown", callback); + editor.prosemirrorView?.dom.addEventListener("keydown", callback); return () => { - editor.prosemirrorView.dom.removeEventListener("keydown", callback); + editor.prosemirrorView?.dom.removeEventListener("keydown", callback); }; - }, [editor.prosemirrorView.dom]); + }, [editor.prosemirrorView?.dom]); const update = useCallback( (url: string, text: string) => {