Skip to content

Commit ec8d654

Browse files
bdbchtcitworld
andauthoredNov 30, 2024··
Allow suggestion character to be used inside the suggestion query (#5895)
* feat(suggestion): add option to allow triggering chars to also be in suggestions It's currently not possible to mention emails or federated usernames for instance, because the triggering char (often `@`) cannot be included in the mention suggestion itself. This option allows to have some, so that the mention will only end with a space. That makes it incompatible with the `allowSpaces` option. Closes #4447 Signed-off-by: Thomas Citharel <tcit@tcit.fr> * added changeset --------- Signed-off-by: Thomas Citharel <tcit@tcit.fr> Co-authored-by: Thomas Citharel <tcit@tcit.fr>
1 parent 4b1b423 commit ec8d654

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed
 

‎.changeset/nasty-turtles-rule.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tiptap/suggestion": patch
3+
---
4+
5+
Added a new option ´allowToIncludeChar` that allows the usage of the suggestion char inside the query

‎packages/suggestion/src/findSuggestionMatch.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ResolvedPos } from '@tiptap/pm/model'
44
export interface Trigger {
55
char: string
66
allowSpaces: boolean
7+
allowToIncludeChar: boolean
78
allowedPrefixes: string[] | null
89
startOfLine: boolean
910
$position: ResolvedPos
@@ -17,15 +18,18 @@ export type SuggestionMatch = {
1718

1819
export function findSuggestionMatch(config: Trigger): SuggestionMatch {
1920
const {
20-
char, allowSpaces, allowedPrefixes, startOfLine, $position,
21+
char, allowSpaces: allowSpacesOption, allowToIncludeChar, allowedPrefixes, startOfLine, $position,
2122
} = config
2223

24+
const allowSpaces = allowSpacesOption && !allowToIncludeChar
25+
2326
const escapedChar = escapeForRegEx(char)
2427
const suffix = new RegExp(`\\s${escapedChar}$`)
2528
const prefix = startOfLine ? '^' : ''
29+
const finalEscapedChar = allowToIncludeChar ? '' : escapedChar
2630
const regexp = allowSpaces
27-
? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${escapedChar}|$)`, 'gm')
28-
: new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${escapedChar}]*`, 'gm')
31+
? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${finalEscapedChar}|$)`, 'gm')
32+
: new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${finalEscapedChar}]*`, 'gm')
2933

3034
const text = $position.nodeBefore?.isText && $position.nodeBefore.text
3135

‎packages/suggestion/src/suggestion.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ export interface SuggestionOptions<I = any, TSelected = any> {
2626
char?: string
2727

2828
/**
29-
* Allow spaces in the suggestion query.
29+
* Allow spaces in the suggestion query. Not compatible with `allowToIncludeChar`. Will be disabled if `allowToIncludeChar` is set to `true`.
3030
* @default false
3131
* @example true
3232
*/
3333
allowSpaces?: boolean
3434

35+
/**
36+
* Allow the character to be included in the suggestion query. Not compatible with `allowSpaces`.
37+
* @default false
38+
*/
39+
allowToIncludeChar?: boolean
40+
3541
/**
3642
* Allow prefixes in the suggestion query.
3743
* @default [' ']
@@ -167,6 +173,7 @@ export function Suggestion<I = any, TSelected = any>({
167173
editor,
168174
char = '@',
169175
allowSpaces = false,
176+
allowToIncludeChar = false,
170177
allowedPrefixes = [' '],
171178
startOfLine = false,
172179
decorationTag = 'span',
@@ -323,6 +330,7 @@ export function Suggestion<I = any, TSelected = any>({
323330
const match = findSuggestionMatch({
324331
char,
325332
allowSpaces,
333+
allowToIncludeChar,
326334
allowedPrefixes,
327335
startOfLine,
328336
$position: selection.$from,

0 commit comments

Comments
 (0)
Please sign in to comment.