feat: Allow multiple prefix characters to trigger a suggestion (#2896)
* feat: Allow multiple prefix characters to trigger a suggestion * review: Turn `allowedPrefixes` into an array instead
This commit is contained in:
@@ -21,6 +21,11 @@ Allows or disallows spaces in suggested items.
|
|||||||
|
|
||||||
Default: `false`
|
Default: `false`
|
||||||
|
|
||||||
|
### allowedPrefixes
|
||||||
|
The prefix characters that are allowed to trigger a suggestion. Set to `null` to allow any prefix character.
|
||||||
|
|
||||||
|
Default: `[' ']`
|
||||||
|
|
||||||
### startOfLine
|
### startOfLine
|
||||||
Trigger the autocomplete popup at the start of a line only.
|
Trigger the autocomplete popup at the start of a line only.
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { ResolvedPos } from 'prosemirror-model'
|
|||||||
export interface Trigger {
|
export interface Trigger {
|
||||||
char: string,
|
char: string,
|
||||||
allowSpaces: boolean,
|
allowSpaces: boolean,
|
||||||
prefixSpace: boolean,
|
allowedPrefixes: string[] | null,
|
||||||
startOfLine: boolean,
|
startOfLine: boolean,
|
||||||
$position: ResolvedPos,
|
$position: ResolvedPos,
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
|
|||||||
const {
|
const {
|
||||||
char,
|
char,
|
||||||
allowSpaces,
|
allowSpaces,
|
||||||
prefixSpace,
|
allowedPrefixes,
|
||||||
startOfLine,
|
startOfLine,
|
||||||
$position,
|
$position,
|
||||||
} = config
|
} = config
|
||||||
@@ -47,9 +47,9 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
|
|||||||
// JavaScript doesn't have lookbehinds. This hacks a check that first character
|
// JavaScript doesn't have lookbehinds. This hacks a check that first character
|
||||||
// is a space or the start of the line
|
// is a space or the start of the line
|
||||||
const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)
|
const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)
|
||||||
const matchPrefixIsSpace = /^[\s\0]?$/.test(matchPrefix)
|
const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\0]?$`).test(matchPrefix)
|
||||||
|
|
||||||
if (prefixSpace && !matchPrefixIsSpace) {
|
if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ export interface SuggestionOptions<I = any> {
|
|||||||
editor: Editor,
|
editor: Editor,
|
||||||
char?: string,
|
char?: string,
|
||||||
allowSpaces?: boolean,
|
allowSpaces?: boolean,
|
||||||
|
allowedPrefixes?: string[] | null,
|
||||||
startOfLine?: boolean,
|
startOfLine?: boolean,
|
||||||
prefixSpace?: boolean,
|
|
||||||
decorationTag?: string,
|
decorationTag?: string,
|
||||||
decorationClass?: string,
|
decorationClass?: string,
|
||||||
command?: (props: {
|
command?: (props: {
|
||||||
@@ -61,7 +61,7 @@ export function Suggestion<I = any>({
|
|||||||
editor,
|
editor,
|
||||||
char = '@',
|
char = '@',
|
||||||
allowSpaces = false,
|
allowSpaces = false,
|
||||||
prefixSpace = true,
|
allowedPrefixes = [' '],
|
||||||
startOfLine = false,
|
startOfLine = false,
|
||||||
decorationTag = 'span',
|
decorationTag = 'span',
|
||||||
decorationClass = 'suggestion',
|
decorationClass = 'suggestion',
|
||||||
@@ -218,7 +218,7 @@ export function Suggestion<I = any>({
|
|||||||
const match = findSuggestionMatch({
|
const match = findSuggestionMatch({
|
||||||
char,
|
char,
|
||||||
allowSpaces,
|
allowSpaces,
|
||||||
prefixSpace,
|
allowedPrefixes,
|
||||||
startOfLine,
|
startOfLine,
|
||||||
$position: selection.$from,
|
$position: selection.$from,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user