From 45a06096277b5b175204414e81d2940023be6682 Mon Sep 17 00:00:00 2001 From: Ricardo Amaral Date: Fri, 17 Jun 2022 12:57:25 +0100 Subject: [PATCH 1/2] feat: Allow multiple prefix characters to trigger a suggestion --- docs/api/utilities/suggestion.md | 5 +++++ packages/suggestion/src/findSuggestionMatch.ts | 8 ++++---- packages/suggestion/src/suggestion.ts | 6 +++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/docs/api/utilities/suggestion.md b/docs/api/utilities/suggestion.md index 3e0cac1bea..98a6efed75 100644 --- a/docs/api/utilities/suggestion.md +++ b/docs/api/utilities/suggestion.md @@ -21,6 +21,11 @@ Allows or disallows spaces in suggested items. Default: `false` +### allowedPrefixes +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. diff --git a/packages/suggestion/src/findSuggestionMatch.ts b/packages/suggestion/src/findSuggestionMatch.ts index 7e272239f2..9590ffa73e 100644 --- a/packages/suggestion/src/findSuggestionMatch.ts +++ b/packages/suggestion/src/findSuggestionMatch.ts @@ -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, } @@ -19,7 +19,7 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch { const { char, allowSpaces, - prefixSpace, + allowedPrefixes, startOfLine, $position, } = config @@ -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}\0]?$`).test(matchPrefix) - if (prefixSpace && !matchPrefixIsSpace) { + if (allowedPrefixes !== null && !matchPrefixIsAllowed) { return null } diff --git a/packages/suggestion/src/suggestion.ts b/packages/suggestion/src/suggestion.ts index c6f2c3baae..3620c69602 100644 --- a/packages/suggestion/src/suggestion.ts +++ b/packages/suggestion/src/suggestion.ts @@ -9,8 +9,8 @@ export interface SuggestionOptions { editor: Editor, char?: string, allowSpaces?: boolean, + allowedPrefixes?: string | null, startOfLine?: boolean, - prefixSpace?: boolean, decorationTag?: string, decorationClass?: string, command?: (props: { @@ -61,7 +61,7 @@ export function Suggestion({ editor, char = '@', allowSpaces = false, - prefixSpace = true, + allowedPrefixes = ' ', startOfLine = false, decorationTag = 'span', decorationClass = 'suggestion', @@ -218,7 +218,7 @@ export function Suggestion({ const match = findSuggestionMatch({ char, allowSpaces, - prefixSpace, + allowedPrefixes, startOfLine, $position: selection.$from, }) From 9d7a8d75dee7c1f758cbdb0bc52b38f1a37c27cb Mon Sep 17 00:00:00 2001 From: Ricardo Amaral Date: Tue, 21 Jun 2022 09:58:55 +0100 Subject: [PATCH 2/2] review: Turn `allowedPrefixes` into an array instead --- docs/api/utilities/suggestion.md | 2 +- packages/suggestion/src/findSuggestionMatch.ts | 4 ++-- packages/suggestion/src/suggestion.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/api/utilities/suggestion.md b/docs/api/utilities/suggestion.md index 98a6efed75..4ff8892b0b 100644 --- a/docs/api/utilities/suggestion.md +++ b/docs/api/utilities/suggestion.md @@ -24,7 +24,7 @@ Default: `false` ### allowedPrefixes The prefix characters that are allowed to trigger a suggestion. Set to `null` to allow any prefix character. -Default: ` ` +Default: `[' ']` ### startOfLine Trigger the autocomplete popup at the start of a line only. diff --git a/packages/suggestion/src/findSuggestionMatch.ts b/packages/suggestion/src/findSuggestionMatch.ts index 9590ffa73e..d245775253 100644 --- a/packages/suggestion/src/findSuggestionMatch.ts +++ b/packages/suggestion/src/findSuggestionMatch.ts @@ -4,7 +4,7 @@ import { ResolvedPos } from 'prosemirror-model' export interface Trigger { char: string, allowSpaces: boolean, - allowedPrefixes: string | null, + allowedPrefixes: string[] | null, startOfLine: boolean, $position: ResolvedPos, } @@ -47,7 +47,7 @@ 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 matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes}\0]?$`).test(matchPrefix) + const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\0]?$`).test(matchPrefix) if (allowedPrefixes !== null && !matchPrefixIsAllowed) { return null diff --git a/packages/suggestion/src/suggestion.ts b/packages/suggestion/src/suggestion.ts index 3620c69602..6b6895cc4b 100644 --- a/packages/suggestion/src/suggestion.ts +++ b/packages/suggestion/src/suggestion.ts @@ -9,7 +9,7 @@ export interface SuggestionOptions { editor: Editor, char?: string, allowSpaces?: boolean, - allowedPrefixes?: string | null, + allowedPrefixes?: string[] | null, startOfLine?: boolean, decorationTag?: string, decorationClass?: string, @@ -61,7 +61,7 @@ export function Suggestion({ editor, char = '@', allowSpaces = false, - allowedPrefixes = ' ', + allowedPrefixes = [' '], startOfLine = false, decorationTag = 'span', decorationClass = 'suggestion',