Skip to content

Commit

Permalink
fix: Invalid hasCodeMarkBefore check in canInsertSuggestion utili…
Browse files Browse the repository at this point in the history
…ty function (Doist#156)
  • Loading branch information
rfgamaral committed Mar 3, 2023
1 parent 9da3b96 commit 21826c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/utilities/can-insert-node-at.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
18 changes: 10 additions & 8 deletions src/utilities/can-insert-suggestion.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 21826c5

Please sign in to comment.