Skip to content

Commit

Permalink
Allow suggestion character to be used inside the suggestion query (#5895
Browse files Browse the repository at this point in the history
)

* 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 <[email protected]>

* added changeset

---------

Signed-off-by: Thomas Citharel <[email protected]>
Co-authored-by: Thomas Citharel <[email protected]>
  • Loading branch information
bdbch and tcitworld authored Nov 30, 2024
1 parent 4b1b423 commit ec8d654
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-turtles-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/suggestion": patch
---

Added a new option ´allowToIncludeChar` that allows the usage of the suggestion char inside the query
10 changes: 7 additions & 3 deletions packages/suggestion/src/findSuggestionMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ResolvedPos } from '@tiptap/pm/model'
export interface Trigger {
char: string
allowSpaces: boolean
allowToIncludeChar: boolean
allowedPrefixes: string[] | null
startOfLine: boolean
$position: ResolvedPos
Expand All @@ -17,15 +18,18 @@ export type SuggestionMatch = {

export function findSuggestionMatch(config: Trigger): SuggestionMatch {
const {
char, allowSpaces, allowedPrefixes, startOfLine, $position,
char, allowSpaces: allowSpacesOption, allowToIncludeChar, allowedPrefixes, startOfLine, $position,
} = config

const allowSpaces = allowSpacesOption && !allowToIncludeChar

const escapedChar = escapeForRegEx(char)
const suffix = new RegExp(`\\s${escapedChar}$`)
const prefix = startOfLine ? '^' : ''
const finalEscapedChar = allowToIncludeChar ? '' : escapedChar
const regexp = allowSpaces
? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${escapedChar}|$)`, 'gm')
: new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${escapedChar}]*`, 'gm')
? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${finalEscapedChar}|$)`, 'gm')
: new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${finalEscapedChar}]*`, 'gm')

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

Expand Down
10 changes: 9 additions & 1 deletion packages/suggestion/src/suggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ export interface SuggestionOptions<I = any, TSelected = any> {
char?: string

/**
* Allow spaces in the suggestion query.
* Allow spaces in the suggestion query. Not compatible with `allowToIncludeChar`. Will be disabled if `allowToIncludeChar` is set to `true`.
* @default false
* @example true
*/
allowSpaces?: boolean

/**
* Allow the character to be included in the suggestion query. Not compatible with `allowSpaces`.
* @default false
*/
allowToIncludeChar?: boolean

/**
* Allow prefixes in the suggestion query.
* @default [' ']
Expand Down Expand Up @@ -167,6 +173,7 @@ export function Suggestion<I = any, TSelected = any>({
editor,
char = '@',
allowSpaces = false,
allowToIncludeChar = false,
allowedPrefixes = [' '],
startOfLine = false,
decorationTag = 'span',
Expand Down Expand Up @@ -323,6 +330,7 @@ export function Suggestion<I = any, TSelected = any>({
const match = findSuggestionMatch({
char,
allowSpaces,
allowToIncludeChar,
allowedPrefixes,
startOfLine,
$position: selection.$from,
Expand Down

0 comments on commit ec8d654

Please sign in to comment.