2022-01-13 20:57:33 +08:00
|
|
|
import { Range, escapeForRegEx } 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
|
|
|
|
|
2022-01-13 20:57:33 +08:00
|
|
|
const escapedChar = escapeForRegEx(char)
|
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')
|
|
|
|
|
2022-03-25 03:25:38 +08:00
|
|
|
const text = $position.nodeBefore?.isText && $position.nodeBefore.text
|
2022-03-25 03:32:59 +08:00
|
|
|
|
|
|
|
if (!text) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-03-25 03:25:38 +08:00
|
|
|
const textFrom = $position.pos - text.length
|
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
|
2022-03-25 03:25:38 +08:00
|
|
|
const from = textFrom + match.index
|
2021-01-20 23:52:56 +08:00
|
|
|
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
|
|
|
}
|