Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally delete full mention chip #3341

Merged
merged 7 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/api/nodes/mention.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ Mention.configure({
})
```

### deleteOnBackspace
Toggle whether the suggestion character(s) should also be deleted on deletion of a mention node. Default is `false`.

```js
Mention.configure({
deleteOnBackspace: 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 = {
options: MentionOptions,
node: ProseMirrorNode,
}) => string,
deleteOnBackspace: boolean,
Tommy-Sun marked this conversation as resolved.
Show resolved Hide resolved
Tommy-Sun marked this conversation as resolved.
Show resolved Hide resolved
suggestion: Omit<SuggestionOptions, 'editor'>,
}

Expand All @@ -23,6 +24,7 @@ export const Mention = Node.create<MentionOptions>({
renderLabel({ options, node }) {
return `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
},
deleteOnBackspace: false,
suggestion: {
char: '@',
pluginKey: MentionPluginKey,
Expand Down Expand Up @@ -144,7 +146,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.deleteOnBackspace ? '' : this.options.suggestion.char || '',
pos,
pos + node.nodeSize,
)

return false
}
Expand Down