From 21826c58f763d020d3810fe373ab2524e0f0448e Mon Sep 17 00:00:00 2001 From: Ricardo Amaral Date: Fri, 3 Mar 2023 16:50:28 +0000 Subject: [PATCH] fix: Invalid `hasCodeMarkBefore` check in `canInsertSuggestion` utility function (#156) --- src/utilities/can-insert-node-at.ts | 2 +- src/utilities/can-insert-suggestion.ts | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/utilities/can-insert-node-at.ts b/src/utilities/can-insert-node-at.ts index 851df575..d6edec51 100644 --- a/src/utilities/can-insert-node-at.ts +++ b/src/utilities/can-insert-node-at.ts @@ -1,4 +1,4 @@ -import { Editor, Range } from '@tiptap/core' +import type { Editor, Range } from '@tiptap/core' /** * Check if a node of a specific type can be inserted at a specific position in the editor. diff --git a/src/utilities/can-insert-suggestion.ts b/src/utilities/can-insert-suggestion.ts index e9a39288..b7da363b 100644 --- a/src/utilities/can-insert-suggestion.ts +++ b/src/utilities/can-insert-suggestion.ts @@ -1,5 +1,5 @@ -import { Editor } from '@tiptap/core' -import { EditorState } from '@tiptap/pm/state' +import type { Editor } from '@tiptap/core' +import type { EditorState } from '@tiptap/pm/state' /** * Check if a suggestion can be inserted within the current editor selection. @@ -13,13 +13,15 @@ function canInsertSuggestion({ editor, state }: { editor: Editor; state: EditorS const isInsideCodeBlockNode = selection.$from.parent.type.name === 'codeBlock' - const hasCodeMarkBefore = state.doc - .nodeAt(selection.$from.parentOffset - 1) - ?.marks.some((mark) => mark.type.name === 'code') + const wordsBeforeSelection = (selection.$from.nodeBefore?.text ?? '').split(' ') - const isComposingInlineCode = selection.$from.nodeBefore?.text - ?.split(' ') - .some((word) => word.startsWith('`')) + const hasCodeMarkBefore = + (selection.$from.parent.cut( + selection.$from.parentOffset - wordsBeforeSelection.slice(-1)[0].length - 1, + selection.$from.parentOffset - 1, + ).content.firstChild?.marks.length ?? 0) > 0 + + const isComposingInlineCode = wordsBeforeSelection.some((word) => word.startsWith('`')) return ( !isInsideCodeMark && !isInsideCodeBlockNode && !hasCodeMarkBefore && !isComposingInlineCode