Skip to content

Commit

Permalink
Optionally delete full mention chip (#3341)
Browse files Browse the repository at this point in the history
* Optionally delete full mention chip

* Added documentation for deleteOnBackspace

* rename all references of deleteOnBackspace to deleteTriggerWithBackspace
  • Loading branch information
Tommy-Sun authored Apr 5, 2024
1 parent 51c5e5f commit ee645c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/api/nodes/mention.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion packages/extension-mention/src/mention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SuggestionOptions, 'editor'>
}

Expand All @@ -23,6 +24,7 @@ export const Mention = Node.create<MentionOptions>({
renderText({ options, node }) {
return `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
},
deleteTriggerWithBackspace: false,
renderHTML({ options, node }) {
return [
'span',
Expand Down Expand Up @@ -177,7 +179,11 @@ export const Mention = Node.create<MentionOptions>({
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
}
Expand Down

0 comments on commit ee645c1

Please sign in to comment.