fix: can not write second mention bug, see #874

This commit is contained in:
Priestch 2020-12-17 08:35:04 +08:00 committed by Hans Pagel
parent 38ea4e6c66
commit 046babfd3a
2 changed files with 33 additions and 2 deletions

View File

@ -43,7 +43,7 @@ export default class Mention extends Node {
},
group: 'inline',
inline: true,
content: 'inline*',
content: 'text*',
selectable: false,
atom: true,
toDOM: node => [

View File

@ -2,6 +2,37 @@ import { Plugin, PluginKey } from 'prosemirror-state'
import { Decoration, DecorationSet } from 'prosemirror-view'
import { insertText } from 'tiptap-commands'
function getTextBetween(node, from, to, blockSeparator, inlineSeparator, leafText = '\0') {
let text = ''
let blockSeparated = true
let inlineNode = null
node.content.nodesBetween(from, to, (innerNode, pos) => {
if (innerNode.isText) {
if (inlineNode) {
inlineNode = null
return
}
text += innerNode.text.slice(Math.max(from, pos) - pos, to - pos)
blockSeparated = !blockSeparator
} else if (innerNode.isLeaf && leafText) {
text += leafText
blockSeparated = !blockSeparator
} else if (innerNode.isInline && !innerNode.isLeaf) {
text += inlineSeparator
if (innerNode.textContent) {
text += innerNode.textContent
inlineNode = innerNode
}
text += inlineSeparator
blockSeparated = !blockSeparated
} else if (!blockSeparated && innerNode.isBlock) {
text += blockSeparator
blockSeparated = true
}
}, 0)
return text
}
// Create a matcher that matches when a specific character is typed. Useful for @mentions and #tags.
function triggerCharacter({
char = '@',
@ -26,7 +57,7 @@ function triggerCharacter({
// Lookup the boundaries of the current node
const textFrom = $position.before()
const textTo = $position.end()
const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0')
const text = getTextBetween($position.doc, textFrom, textTo, '\0', '\0')
let match = regexp.exec(text)
let position