Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow multiple prefix characters to trigger a suggestion #2896

Merged
merged 2 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/api/utilities/suggestion.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Allows or disallows spaces in suggested items.

Default: `false`

### allowedPrefixes
bdbch marked this conversation as resolved.
Show resolved Hide resolved
The prefix characters that are allowed to trigger a suggestion. Set to `null` to allow any prefix character.

Default: `[' ']`

### startOfLine
Trigger the autocomplete popup at the start of a line only.

Expand Down
8 changes: 4 additions & 4 deletions packages/suggestion/src/findSuggestionMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ResolvedPos } from 'prosemirror-model'
export interface Trigger {
char: string,
allowSpaces: boolean,
prefixSpace: boolean,
allowedPrefixes: string[] | null,
startOfLine: boolean,
$position: ResolvedPos,
}
Expand All @@ -19,7 +19,7 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
const {
char,
allowSpaces,
prefixSpace,
allowedPrefixes,
startOfLine,
$position,
} = config
Expand Down Expand Up @@ -47,9 +47,9 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
// JavaScript doesn't have lookbehinds. This hacks a check that first character
// is a space or the start of the line
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
}

Expand Down
6 changes: 3 additions & 3 deletions packages/suggestion/src/suggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface SuggestionOptions<I = any> {
editor: Editor,
char?: string,
allowSpaces?: boolean,
allowedPrefixes?: string[] | null,
startOfLine?: boolean,
prefixSpace?: boolean,
decorationTag?: string,
decorationClass?: string,
command?: (props: {
Expand Down Expand Up @@ -61,7 +61,7 @@ export function Suggestion<I = any>({
editor,
char = '@',
allowSpaces = false,
prefixSpace = true,
allowedPrefixes = [' '],
startOfLine = false,
decorationTag = 'span',
decorationClass = 'suggestion',
Expand Down Expand Up @@ -218,7 +218,7 @@ export function Suggestion<I = any>({
const match = findSuggestionMatch({
char,
allowSpaces,
prefixSpace,
allowedPrefixes,
startOfLine,
$position: selection.$from,
})
Expand Down