Skip to content

Commit 482cb96

Browse files
authored
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
1 parent 53e39d0 commit 482cb96

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

docs/api/utilities/suggestion.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ Allows or disallows spaces in suggested items.
2121

2222
Default: `false`
2323

24+
### allowedPrefixes
25+
The prefix characters that are allowed to trigger a suggestion. Set to `null` to allow any prefix character.
26+
27+
Default: `[' ']`
28+
2429
### startOfLine
2530
Trigger the autocomplete popup at the start of a line only.
2631

packages/suggestion/src/findSuggestionMatch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ResolvedPos } from 'prosemirror-model'
44
export interface Trigger {
55
char: string,
66
allowSpaces: boolean,
7-
prefixSpace: boolean,
7+
allowedPrefixes: string[] | null,
88
startOfLine: boolean,
99
$position: ResolvedPos,
1010
}
@@ -19,7 +19,7 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
1919
const {
2020
char,
2121
allowSpaces,
22-
prefixSpace,
22+
allowedPrefixes,
2323
startOfLine,
2424
$position,
2525
} = config
@@ -47,9 +47,9 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
4747
// JavaScript doesn't have lookbehinds. This hacks a check that first character
4848
// is a space or the start of the line
4949
const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)
50-
const matchPrefixIsSpace = /^[\s\0]?$/.test(matchPrefix)
50+
const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\0]?$`).test(matchPrefix)
5151

52-
if (prefixSpace && !matchPrefixIsSpace) {
52+
if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
5353
return null
5454
}
5555

packages/suggestion/src/suggestion.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export interface SuggestionOptions<I = any> {
99
editor: Editor,
1010
char?: string,
1111
allowSpaces?: boolean,
12+
allowedPrefixes?: string[] | null,
1213
startOfLine?: boolean,
13-
prefixSpace?: boolean,
1414
decorationTag?: string,
1515
decorationClass?: string,
1616
command?: (props: {
@@ -61,7 +61,7 @@ export function Suggestion<I = any>({
6161
editor,
6262
char = '@',
6363
allowSpaces = false,
64-
prefixSpace = true,
64+
allowedPrefixes = [' '],
6565
startOfLine = false,
6666
decorationTag = 'span',
6767
decorationClass = 'suggestion',
@@ -218,7 +218,7 @@ export function Suggestion<I = any>({
218218
const match = findSuggestionMatch({
219219
char,
220220
allowSpaces,
221-
prefixSpace,
221+
allowedPrefixes,
222222
startOfLine,
223223
$position: selection.$from,
224224
})

0 commit comments

Comments
 (0)