diff --git a/docs/api/nodes/mention.md b/docs/api/nodes/mention.md index b9911dbd71a..975ecce5ae2 100644 --- a/docs/api/nodes/mention.md +++ b/docs/api/nodes/mention.md @@ -67,6 +67,16 @@ Mention.configure({ } }) ``` + +### deleteTriggerWithBackspace +Toggle whether the suggestion character(s) should also be deleted on deletion of a mention node. Default is `false`. + +```js +Mention.configure({ + deleteTriggerWithBackspace: true +}) +``` + ### suggestion [Read more](/api/utilities/suggestion) diff --git a/packages/extension-mention/src/mention.ts b/packages/extension-mention/src/mention.ts index 2938cfb0274..40212eba6ac 100644 --- a/packages/extension-mention/src/mention.ts +++ b/packages/extension-mention/src/mention.ts @@ -9,6 +9,7 @@ export type MentionOptions = { renderLabel?: (props: { options: MentionOptions; node: ProseMirrorNode }) => string renderText: (props: { options: MentionOptions; node: ProseMirrorNode }) => string renderHTML: (props: { options: MentionOptions; node: ProseMirrorNode }) => DOMOutputSpec + deleteTriggerWithBackspace: boolean suggestion: Omit } @@ -23,6 +24,7 @@ export const Mention = Node.create({ renderText({ options, node }) { return `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}` }, + deleteTriggerWithBackspace: false, renderHTML({ options, node }) { return [ 'span', @@ -174,7 +176,11 @@ export const Mention = Node.create({ state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => { if (node.type.name === this.name) { isMention = true - tr.insertText(this.options.suggestion.char || '', pos, pos + node.nodeSize) + tr.insertText( + this.options.deleteTriggerWithBackspace ? '' : this.options.suggestion.char || '', + pos, + pos + node.nodeSize, + ) return false }