2021-01-19 17:09:32 +08:00
|
|
|
import { Range } from '@tiptap/core'
|
2021-01-15 21:49:28 +08:00
|
|
|
import { ResolvedPos } from 'prosemirror-model'
|
|
|
|
|
|
|
|
export interface Trigger {
|
|
|
|
char: string,
|
|
|
|
allowSpaces: boolean,
|
2021-09-07 04:25:41 +08:00
|
|
|
prefixSpace: boolean,
|
2021-01-15 21:49:28 +08:00
|
|
|
startOfLine: boolean,
|
|
|
|
$position: ResolvedPos,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type SuggestionMatch = {
|
2021-01-19 17:09:32 +08:00
|
|
|
range: Range,
|
2021-01-15 21:49:28 +08:00
|
|
|
query: string,
|
|
|
|
text: string,
|
|
|
|
} | null
|
|
|
|
|
|
|
|
export function findSuggestionMatch(config: Trigger): SuggestionMatch {
|
|
|
|
const {
|
2021-01-20 03:27:51 +08:00
|
|
|
char,
|
|
|
|
allowSpaces,
|
2021-09-07 04:25:41 +08:00
|
|
|
prefixSpace,
|
2021-01-20 03:27:51 +08:00
|
|
|
startOfLine,
|
|
|
|
$position,
|
2021-01-15 21:49:28 +08:00
|
|
|
} = config
|
|
|
|
|
|
|
|
// Matching expressions used for later
|
2021-07-26 21:39:08 +08:00
|
|
|
const escapedChar = char
|
|
|
|
.split('')
|
|
|
|
.map(c => `\\${c}`)
|
|
|
|
.join('')
|
2021-01-15 21:49:28 +08:00
|
|
|
const suffix = new RegExp(`\\s${escapedChar}$`)
|
|
|
|
const prefix = startOfLine ? '^' : ''
|
|
|
|
const regexp = allowSpaces
|
|
|
|
? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${escapedChar}|$)`, 'gm')
|
|
|
|
: new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${escapedChar}]*`, 'gm')
|
|
|
|
|
2021-05-19 01:25:20 +08:00
|
|
|
const isTopLevelNode = $position.depth <= 0
|
|
|
|
const textFrom = isTopLevelNode
|
|
|
|
? 0
|
|
|
|
: $position.before()
|
2021-01-15 21:49:28 +08:00
|
|
|
const textTo = $position.pos
|
|
|
|
const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0')
|
2021-02-08 03:00:13 +08:00
|
|
|
const match = Array.from(text.matchAll(regexp)).pop()
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-02-08 03:00:13 +08:00
|
|
|
if (!match || match.input === undefined || match.index === undefined) {
|
2021-01-20 23:52:56 +08:00
|
|
|
return null
|
|
|
|
}
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-09-07 04:33:16 +08:00
|
|
|
// JavaScript doesn't have lookbehinds. This hacks a check that first character
|
|
|
|
// is a space or the start of the line
|
2021-01-20 23:52:56 +08:00
|
|
|
const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)
|
2021-09-07 04:33:16 +08:00
|
|
|
const matchPrefixIsSpace = /^[\s\0]?$/.test(matchPrefix)
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-09-07 04:33:16 +08:00
|
|
|
if (prefixSpace && !matchPrefixIsSpace) {
|
2021-01-20 23:52:56 +08:00
|
|
|
return null
|
|
|
|
}
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-01-20 23:52:56 +08:00
|
|
|
// The absolute position of the match in the document
|
|
|
|
const from = match.index + $position.start()
|
|
|
|
let to = from + match[0].length
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-01-20 23:52:56 +08:00
|
|
|
// Edge case handling; if spaces are allowed and we're directly in between
|
|
|
|
// two triggers
|
|
|
|
if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {
|
|
|
|
match[0] += ' '
|
|
|
|
to += 1
|
|
|
|
}
|
2021-01-15 21:49:28 +08:00
|
|
|
|
2021-01-20 23:52:56 +08:00
|
|
|
// If the $position is located within the matched substring, return that range
|
|
|
|
if (from < $position.pos && to >= $position.pos) {
|
|
|
|
return {
|
|
|
|
range: {
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
},
|
|
|
|
query: match[0].slice(char.length),
|
|
|
|
text: match[0],
|
|
|
|
}
|
2021-01-15 21:49:28 +08:00
|
|
|
}
|
|
|
|
|
2021-01-20 23:52:56 +08:00
|
|
|
return null
|
2021-01-15 21:49:28 +08:00
|
|
|
}
|