@tiptap/extension-mention 3.20.3 → 3.20.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +277 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +93 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +250 -0
- package/dist/index.js.map +1 -0
- package/package.json +7 -7
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Mention: () => Mention,
|
|
24
|
+
default: () => index_default
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/mention.ts
|
|
29
|
+
var import_core = require("@tiptap/core");
|
|
30
|
+
var import_model = require("@tiptap/pm/model");
|
|
31
|
+
var import_suggestion = require("@tiptap/suggestion");
|
|
32
|
+
|
|
33
|
+
// src/utils/get-default-suggestion-attributes.ts
|
|
34
|
+
var import_state = require("@tiptap/pm/state");
|
|
35
|
+
function getSuggestionOptions({
|
|
36
|
+
editor: tiptapEditor,
|
|
37
|
+
overrideSuggestionOptions,
|
|
38
|
+
extensionName,
|
|
39
|
+
char = "@"
|
|
40
|
+
}) {
|
|
41
|
+
const pluginKey = new import_state.PluginKey();
|
|
42
|
+
return {
|
|
43
|
+
editor: tiptapEditor,
|
|
44
|
+
char,
|
|
45
|
+
pluginKey,
|
|
46
|
+
command: ({ editor, range, props }) => {
|
|
47
|
+
var _a, _b, _c;
|
|
48
|
+
const nodeAfter = editor.view.state.selection.$to.nodeAfter;
|
|
49
|
+
const overrideSpace = (_a = nodeAfter == null ? void 0 : nodeAfter.text) == null ? void 0 : _a.startsWith(" ");
|
|
50
|
+
if (overrideSpace) {
|
|
51
|
+
range.to += 1;
|
|
52
|
+
}
|
|
53
|
+
editor.chain().focus().insertContentAt(range, [
|
|
54
|
+
{
|
|
55
|
+
type: extensionName,
|
|
56
|
+
attrs: { ...props, mentionSuggestionChar: char }
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: "text",
|
|
60
|
+
text: " "
|
|
61
|
+
}
|
|
62
|
+
]).run();
|
|
63
|
+
(_c = (_b = editor.view.dom.ownerDocument.defaultView) == null ? void 0 : _b.getSelection()) == null ? void 0 : _c.collapseToEnd();
|
|
64
|
+
},
|
|
65
|
+
allow: ({ state, range }) => {
|
|
66
|
+
const $from = state.doc.resolve(range.from);
|
|
67
|
+
const type = state.schema.nodes[extensionName];
|
|
68
|
+
const allow = !!$from.parent.type.contentMatch.matchType(type);
|
|
69
|
+
return allow;
|
|
70
|
+
},
|
|
71
|
+
...overrideSuggestionOptions
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/mention.ts
|
|
76
|
+
function getSuggestions(options) {
|
|
77
|
+
return (options.options.suggestions.length ? options.options.suggestions : [options.options.suggestion]).map(
|
|
78
|
+
(suggestion) => getSuggestionOptions({
|
|
79
|
+
// @ts-ignore `editor` can be `undefined` when converting the document to HTML with the HTML utility
|
|
80
|
+
editor: options.editor,
|
|
81
|
+
overrideSuggestionOptions: suggestion,
|
|
82
|
+
extensionName: options.name,
|
|
83
|
+
char: suggestion.char
|
|
84
|
+
})
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
function getSuggestionFromChar(options, char) {
|
|
88
|
+
const suggestions = getSuggestions(options);
|
|
89
|
+
const suggestion = suggestions.find((s) => s.char === char);
|
|
90
|
+
if (suggestion) {
|
|
91
|
+
return suggestion;
|
|
92
|
+
}
|
|
93
|
+
if (suggestions.length) {
|
|
94
|
+
return suggestions[0];
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
var Mention = import_core.Node.create({
|
|
99
|
+
name: "mention",
|
|
100
|
+
priority: 101,
|
|
101
|
+
addOptions() {
|
|
102
|
+
return {
|
|
103
|
+
HTMLAttributes: {},
|
|
104
|
+
renderText({ node, suggestion }) {
|
|
105
|
+
var _a, _b;
|
|
106
|
+
return `${(_a = suggestion == null ? void 0 : suggestion.char) != null ? _a : "@"}${(_b = node.attrs.label) != null ? _b : node.attrs.id}`;
|
|
107
|
+
},
|
|
108
|
+
deleteTriggerWithBackspace: false,
|
|
109
|
+
renderHTML({ options, node, suggestion }) {
|
|
110
|
+
var _a, _b;
|
|
111
|
+
return [
|
|
112
|
+
"span",
|
|
113
|
+
(0, import_core.mergeAttributes)(this.HTMLAttributes, options.HTMLAttributes),
|
|
114
|
+
`${(_a = suggestion == null ? void 0 : suggestion.char) != null ? _a : "@"}${(_b = node.attrs.label) != null ? _b : node.attrs.id}`
|
|
115
|
+
];
|
|
116
|
+
},
|
|
117
|
+
suggestions: [],
|
|
118
|
+
suggestion: {}
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
group: "inline",
|
|
122
|
+
inline: true,
|
|
123
|
+
selectable: false,
|
|
124
|
+
atom: true,
|
|
125
|
+
addAttributes() {
|
|
126
|
+
return {
|
|
127
|
+
id: {
|
|
128
|
+
default: null,
|
|
129
|
+
parseHTML: (element) => element.getAttribute("data-id"),
|
|
130
|
+
renderHTML: (attributes) => {
|
|
131
|
+
if (!attributes.id) {
|
|
132
|
+
return {};
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
"data-id": attributes.id
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
label: {
|
|
140
|
+
default: null,
|
|
141
|
+
parseHTML: (element) => element.getAttribute("data-label"),
|
|
142
|
+
renderHTML: (attributes) => {
|
|
143
|
+
if (!attributes.label) {
|
|
144
|
+
return {};
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
"data-label": attributes.label
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
// When there are multiple types of mentions, this attribute helps distinguish them
|
|
152
|
+
mentionSuggestionChar: {
|
|
153
|
+
default: "@",
|
|
154
|
+
parseHTML: (element) => element.getAttribute("data-mention-suggestion-char"),
|
|
155
|
+
renderHTML: (attributes) => {
|
|
156
|
+
return {
|
|
157
|
+
"data-mention-suggestion-char": attributes.mentionSuggestionChar
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
},
|
|
163
|
+
parseHTML() {
|
|
164
|
+
return [
|
|
165
|
+
{
|
|
166
|
+
tag: `span[data-type="${this.name}"]`
|
|
167
|
+
}
|
|
168
|
+
];
|
|
169
|
+
},
|
|
170
|
+
renderHTML({ node, HTMLAttributes }) {
|
|
171
|
+
const suggestion = getSuggestionFromChar(this, node.attrs.mentionSuggestionChar);
|
|
172
|
+
if (this.options.renderLabel !== void 0) {
|
|
173
|
+
console.warn("renderLabel is deprecated use renderText and renderHTML instead");
|
|
174
|
+
return [
|
|
175
|
+
"span",
|
|
176
|
+
(0, import_core.mergeAttributes)({ "data-type": this.name }, this.options.HTMLAttributes, HTMLAttributes),
|
|
177
|
+
this.options.renderLabel({
|
|
178
|
+
options: this.options,
|
|
179
|
+
node,
|
|
180
|
+
suggestion
|
|
181
|
+
})
|
|
182
|
+
];
|
|
183
|
+
}
|
|
184
|
+
const mergedOptions = { ...this.options };
|
|
185
|
+
mergedOptions.HTMLAttributes = (0, import_core.mergeAttributes)(
|
|
186
|
+
{ "data-type": this.name },
|
|
187
|
+
this.options.HTMLAttributes,
|
|
188
|
+
HTMLAttributes
|
|
189
|
+
);
|
|
190
|
+
const html = this.options.renderHTML({
|
|
191
|
+
options: mergedOptions,
|
|
192
|
+
node,
|
|
193
|
+
suggestion
|
|
194
|
+
});
|
|
195
|
+
if (typeof html === "string") {
|
|
196
|
+
return ["span", (0, import_core.mergeAttributes)({ "data-type": this.name }, this.options.HTMLAttributes, HTMLAttributes), html];
|
|
197
|
+
}
|
|
198
|
+
return html;
|
|
199
|
+
},
|
|
200
|
+
...(0, import_core.createInlineMarkdownSpec)({
|
|
201
|
+
nodeName: "mention",
|
|
202
|
+
name: "@",
|
|
203
|
+
selfClosing: true,
|
|
204
|
+
allowedAttributes: ["id", "label", { name: "mentionSuggestionChar", skipIfDefault: "@" }],
|
|
205
|
+
parseAttributes: (attrString) => {
|
|
206
|
+
const attrs = {};
|
|
207
|
+
const regex = /(\w+)=(?:"([^"]*)"|'([^']*)')/g;
|
|
208
|
+
let match = regex.exec(attrString);
|
|
209
|
+
while (match !== null) {
|
|
210
|
+
const [, key, doubleQuoted, singleQuoted] = match;
|
|
211
|
+
const value = doubleQuoted != null ? doubleQuoted : singleQuoted;
|
|
212
|
+
attrs[key === "char" ? "mentionSuggestionChar" : key] = value;
|
|
213
|
+
match = regex.exec(attrString);
|
|
214
|
+
}
|
|
215
|
+
return attrs;
|
|
216
|
+
},
|
|
217
|
+
serializeAttributes: (attrs) => {
|
|
218
|
+
return Object.entries(attrs).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => {
|
|
219
|
+
const serializedKey = key === "mentionSuggestionChar" ? "char" : key;
|
|
220
|
+
return `${serializedKey}="${value}"`;
|
|
221
|
+
}).join(" ");
|
|
222
|
+
}
|
|
223
|
+
}),
|
|
224
|
+
renderText({ node }) {
|
|
225
|
+
const args = {
|
|
226
|
+
options: this.options,
|
|
227
|
+
node,
|
|
228
|
+
suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar)
|
|
229
|
+
};
|
|
230
|
+
if (this.options.renderLabel !== void 0) {
|
|
231
|
+
console.warn("renderLabel is deprecated use renderText and renderHTML instead");
|
|
232
|
+
return this.options.renderLabel(args);
|
|
233
|
+
}
|
|
234
|
+
return this.options.renderText(args);
|
|
235
|
+
},
|
|
236
|
+
addKeyboardShortcuts() {
|
|
237
|
+
return {
|
|
238
|
+
Backspace: () => this.editor.commands.command(({ tr, state }) => {
|
|
239
|
+
let isMention = false;
|
|
240
|
+
const { selection } = state;
|
|
241
|
+
const { empty, anchor } = selection;
|
|
242
|
+
if (!empty) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
let mentionNode = new import_model.Node();
|
|
246
|
+
let mentionPos = 0;
|
|
247
|
+
state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
|
|
248
|
+
if (node.type.name === this.name) {
|
|
249
|
+
isMention = true;
|
|
250
|
+
mentionNode = node;
|
|
251
|
+
mentionPos = pos;
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
if (isMention) {
|
|
256
|
+
tr.insertText(
|
|
257
|
+
this.options.deleteTriggerWithBackspace ? "" : mentionNode.attrs.mentionSuggestionChar,
|
|
258
|
+
mentionPos,
|
|
259
|
+
mentionPos + mentionNode.nodeSize
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
return isMention;
|
|
263
|
+
})
|
|
264
|
+
};
|
|
265
|
+
},
|
|
266
|
+
addProseMirrorPlugins() {
|
|
267
|
+
return getSuggestions(this).map(import_suggestion.Suggestion);
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// src/index.ts
|
|
272
|
+
var index_default = Mention;
|
|
273
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
274
|
+
0 && (module.exports = {
|
|
275
|
+
Mention
|
|
276
|
+
});
|
|
277
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/mention.ts","../src/utils/get-default-suggestion-attributes.ts"],"sourcesContent":["import { Mention } from './mention.js'\n\nexport * from './mention.js'\n\nexport default Mention\n","import type { Editor } from '@tiptap/core'\nimport { createInlineMarkdownSpec, mergeAttributes, Node } from '@tiptap/core'\nimport type { DOMOutputSpec } from '@tiptap/pm/model'\nimport { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport type { SuggestionOptions } from '@tiptap/suggestion'\nimport { Suggestion } from '@tiptap/suggestion'\n\nimport { getSuggestionOptions } from './utils/get-default-suggestion-attributes.js'\n\n// See `addAttributes` below\nexport interface MentionNodeAttrs {\n /**\n * The identifier for the selected item that was mentioned, stored as a `data-id`\n * attribute.\n */\n id: string | null\n /**\n * The label to be rendered by the editor as the displayed text for this mentioned\n * item, if provided. Stored as a `data-label` attribute. See `renderLabel`.\n */\n label?: string | null\n /**\n * The character that triggers the suggestion, stored as\n * `data-mention-suggestion-char` attribute.\n */\n mentionSuggestionChar?: string\n}\n\nexport interface MentionOptions<SuggestionItem = any, Attrs extends Record<string, any> = MentionNodeAttrs> {\n /**\n * The HTML attributes for a mention node.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n\n /**\n * A function to render the label of a mention.\n * @deprecated use renderText and renderHTML instead\n * @param props The render props\n * @returns The label\n * @example ({ options, node }) => `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`\n */\n renderLabel?: (props: {\n options: MentionOptions<SuggestionItem, Attrs>\n node: ProseMirrorNode\n suggestion: SuggestionOptions | null\n }) => string\n\n /**\n * A function to render the text of a mention.\n * @param props The render props\n * @returns The text\n * @example ({ options, node }) => `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`\n */\n renderText: (props: {\n options: MentionOptions<SuggestionItem, Attrs>\n node: ProseMirrorNode\n suggestion: SuggestionOptions | null\n }) => string\n\n /**\n * A function to render the HTML of a mention.\n * @param props The render props\n * @returns The HTML as a ProseMirror DOM Output Spec\n * @example ({ options, node }) => ['span', { 'data-type': 'mention' }, `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`]\n */\n renderHTML: (props: {\n options: MentionOptions<SuggestionItem, Attrs>\n node: ProseMirrorNode\n suggestion: SuggestionOptions | null\n }) => DOMOutputSpec\n\n /**\n * Whether to delete the trigger character with backspace.\n * @default false\n */\n deleteTriggerWithBackspace: boolean\n\n /**\n * The suggestion options, when you want to support multiple triggers.\n *\n * With this parameter, you can define multiple types of mention. For example, you can use the `@` character\n * to mention users and the `#` character to mention tags.\n *\n * @default [{ char: '@', pluginKey: MentionPluginKey }]\n * @example [{ char: '@', pluginKey: MentionPluginKey }, { char: '#', pluginKey: new PluginKey('hashtag') }]\n */\n suggestions: Array<Omit<SuggestionOptions<SuggestionItem, Attrs>, 'editor'>>\n\n /**\n * The suggestion options, when you want to support only one trigger. To support multiple triggers, use the\n * `suggestions` parameter instead.\n *\n * @default {}\n * @example { char: '@', pluginKey: MentionPluginKey, command: ({ editor, range, props }) => { ... } }\n */\n suggestion: Omit<SuggestionOptions<SuggestionItem, Attrs>, 'editor'>\n}\n\ninterface GetSuggestionsOptions {\n editor?: Editor\n options: MentionOptions\n name: string\n}\n\n/**\n * Returns the suggestions for the mention extension.\n *\n * @param options The extension options\n * @returns the suggestions\n */\nfunction getSuggestions(options: GetSuggestionsOptions) {\n return (options.options.suggestions.length ? options.options.suggestions : [options.options.suggestion]).map(\n suggestion =>\n getSuggestionOptions({\n // @ts-ignore `editor` can be `undefined` when converting the document to HTML with the HTML utility\n editor: options.editor,\n overrideSuggestionOptions: suggestion,\n extensionName: options.name,\n char: suggestion.char,\n }),\n )\n}\n\n/**\n * Returns the suggestion options of the mention that has a given character trigger. If not\n * found, it returns the first suggestion.\n *\n * @param options The extension options\n * @param char The character that triggers the mention\n * @returns The suggestion options\n */\nfunction getSuggestionFromChar(options: GetSuggestionsOptions, char: string) {\n const suggestions = getSuggestions(options)\n\n const suggestion = suggestions.find(s => s.char === char)\n if (suggestion) {\n return suggestion\n }\n\n if (suggestions.length) {\n return suggestions[0]\n }\n\n return null\n}\n\n/**\n * This extension allows you to insert mentions into the editor.\n * @see https://www.tiptap.dev/api/extensions/mention\n */\nexport const Mention = Node.create<MentionOptions>({\n name: 'mention',\n\n priority: 101,\n\n addOptions() {\n return {\n HTMLAttributes: {},\n renderText({ node, suggestion }) {\n return `${suggestion?.char ?? '@'}${node.attrs.label ?? node.attrs.id}`\n },\n deleteTriggerWithBackspace: false,\n renderHTML({ options, node, suggestion }) {\n return [\n 'span',\n mergeAttributes(this.HTMLAttributes, options.HTMLAttributes),\n `${suggestion?.char ?? '@'}${node.attrs.label ?? node.attrs.id}`,\n ]\n },\n suggestions: [],\n suggestion: {},\n }\n },\n\n group: 'inline',\n\n inline: true,\n\n selectable: false,\n\n atom: true,\n\n addAttributes() {\n return {\n id: {\n default: null,\n parseHTML: element => element.getAttribute('data-id'),\n renderHTML: attributes => {\n if (!attributes.id) {\n return {}\n }\n\n return {\n 'data-id': attributes.id,\n }\n },\n },\n\n label: {\n default: null,\n parseHTML: element => element.getAttribute('data-label'),\n renderHTML: attributes => {\n if (!attributes.label) {\n return {}\n }\n\n return {\n 'data-label': attributes.label,\n }\n },\n },\n\n // When there are multiple types of mentions, this attribute helps distinguish them\n mentionSuggestionChar: {\n default: '@',\n parseHTML: element => element.getAttribute('data-mention-suggestion-char'),\n renderHTML: attributes => {\n return {\n 'data-mention-suggestion-char': attributes.mentionSuggestionChar,\n }\n },\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: `span[data-type=\"${this.name}\"]`,\n },\n ]\n },\n\n renderHTML({ node, HTMLAttributes }) {\n const suggestion = getSuggestionFromChar(this, node.attrs.mentionSuggestionChar)\n\n if (this.options.renderLabel !== undefined) {\n console.warn('renderLabel is deprecated use renderText and renderHTML instead')\n return [\n 'span',\n mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),\n this.options.renderLabel({\n options: this.options,\n node,\n suggestion,\n }),\n ]\n }\n const mergedOptions = { ...this.options }\n\n mergedOptions.HTMLAttributes = mergeAttributes(\n { 'data-type': this.name },\n this.options.HTMLAttributes,\n HTMLAttributes,\n )\n\n const html = this.options.renderHTML({\n options: mergedOptions,\n node,\n suggestion,\n })\n\n if (typeof html === 'string') {\n return ['span', mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes), html]\n }\n return html\n },\n\n ...createInlineMarkdownSpec({\n nodeName: 'mention',\n name: '@',\n selfClosing: true,\n allowedAttributes: ['id', 'label', { name: 'mentionSuggestionChar', skipIfDefault: '@' }],\n parseAttributes: (attrString: string) => {\n const attrs: Record<string, any> = {}\n const regex = /(\\w+)=(?:\"([^\"]*)\"|'([^']*)')/g\n let match = regex.exec(attrString)\n\n while (match !== null) {\n const [, key, doubleQuoted, singleQuoted] = match\n const value = doubleQuoted ?? singleQuoted\n attrs[key === 'char' ? 'mentionSuggestionChar' : key] = value\n match = regex.exec(attrString)\n }\n\n return attrs\n },\n serializeAttributes: (attrs: Record<string, any>) => {\n return Object.entries(attrs)\n .filter(([, value]) => value !== undefined && value !== null)\n .map(([key, value]) => {\n const serializedKey = key === 'mentionSuggestionChar' ? 'char' : key\n return `${serializedKey}=\"${value}\"`\n })\n .join(' ')\n },\n }),\n\n renderText({ node }) {\n const args = {\n options: this.options,\n node,\n suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar),\n }\n if (this.options.renderLabel !== undefined) {\n console.warn('renderLabel is deprecated use renderText and renderHTML instead')\n return this.options.renderLabel(args)\n }\n\n return this.options.renderText(args)\n },\n\n addKeyboardShortcuts() {\n return {\n Backspace: () =>\n this.editor.commands.command(({ tr, state }) => {\n let isMention = false\n const { selection } = state\n const { empty, anchor } = selection\n\n if (!empty) {\n return false\n }\n\n // Store node and position for later use\n let mentionNode = new ProseMirrorNode()\n let mentionPos = 0\n\n state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {\n if (node.type.name === this.name) {\n isMention = true\n mentionNode = node\n mentionPos = pos\n return false\n }\n })\n\n if (isMention) {\n tr.insertText(\n this.options.deleteTriggerWithBackspace ? '' : mentionNode.attrs.mentionSuggestionChar,\n mentionPos,\n mentionPos + mentionNode.nodeSize,\n )\n }\n\n return isMention\n }),\n }\n },\n\n addProseMirrorPlugins() {\n // Create a plugin for each suggestion configuration\n return getSuggestions(this).map(Suggestion)\n },\n})\n","import type { Editor } from '@tiptap/core'\nimport { PluginKey } from '@tiptap/pm/state'\nimport type { SuggestionOptions } from '@tiptap/suggestion'\n\n/**\n * Arguments for the `getSuggestionOptions` function\n * @see getSuggestionOptions\n */\nexport interface GetSuggestionOptionsOptions {\n /**\n * The Tiptap editor instance.\n */\n editor: Editor\n /**\n * The suggestion options configuration provided to the\n * `Mention` extension.\n */\n overrideSuggestionOptions: Omit<SuggestionOptions, 'editor'>\n /**\n * The name of the Mention extension\n */\n extensionName: string\n /**\n * The character that triggers the suggestion.\n * @default '@'\n */\n char?: string\n}\n\n/**\n * Returns the suggestion options for a trigger of the Mention extension. These\n * options are used to create a `Suggestion` ProseMirror plugin. Each plugin lets\n * you define a different trigger that opens the Mention menu. For example,\n * you can define a `@` trigger to mention users and a `#` trigger to mention\n * tags.\n *\n * @param param0 The configured options for the suggestion\n * @returns\n */\nexport function getSuggestionOptions({\n editor: tiptapEditor,\n overrideSuggestionOptions,\n extensionName,\n char = '@',\n}: GetSuggestionOptionsOptions): SuggestionOptions {\n const pluginKey = new PluginKey()\n\n return {\n editor: tiptapEditor,\n char,\n pluginKey,\n command: ({ editor, range, props }: { editor: any; range: any; props: any }) => {\n // increase range.to by one when the next node is of type \"text\"\n // and starts with a space character\n const nodeAfter = editor.view.state.selection.$to.nodeAfter\n const overrideSpace = nodeAfter?.text?.startsWith(' ')\n\n if (overrideSpace) {\n range.to += 1\n }\n\n editor\n .chain()\n .focus()\n .insertContentAt(range, [\n {\n type: extensionName,\n attrs: { ...props, mentionSuggestionChar: char },\n },\n {\n type: 'text',\n text: ' ',\n },\n ])\n .run()\n\n // get reference to `window` object from editor element, to support cross-frame JS usage\n editor.view.dom.ownerDocument.defaultView?.getSelection()?.collapseToEnd()\n },\n allow: ({ state, range }: { state: any; range: any }) => {\n const $from = state.doc.resolve(range.from)\n const type = state.schema.nodes[extensionName]\n const allow = !!$from.parent.type.contentMatch.matchType(type)\n\n return allow\n },\n ...overrideSuggestionOptions,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,kBAAgE;AAEhE,mBAAwC;AAExC,wBAA2B;;;ACJ3B,mBAA0B;AAsCnB,SAAS,qBAAqB;AAAA,EACnC,QAAQ;AAAA,EACR;AAAA,EACA;AAAA,EACA,OAAO;AACT,GAAmD;AACjD,QAAM,YAAY,IAAI,uBAAU;AAEhC,SAAO;AAAA,IACL,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA,SAAS,CAAC,EAAE,QAAQ,OAAO,MAAM,MAA+C;AAnDpF;AAsDM,YAAM,YAAY,OAAO,KAAK,MAAM,UAAU,IAAI;AAClD,YAAM,iBAAgB,4CAAW,SAAX,mBAAiB,WAAW;AAElD,UAAI,eAAe;AACjB,cAAM,MAAM;AAAA,MACd;AAEA,aACG,MAAM,EACN,MAAM,EACN,gBAAgB,OAAO;AAAA,QACtB;AAAA,UACE,MAAM;AAAA,UACN,OAAO,EAAE,GAAG,OAAO,uBAAuB,KAAK;AAAA,QACjD;AAAA,QACA;AAAA,UACE,MAAM;AAAA,UACN,MAAM;AAAA,QACR;AAAA,MACF,CAAC,EACA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;AAAA,MACT,CAAC;AAAA,IACL;AAAA,EACF;AAAA,EAEA,wBAAwB;AAEtB,WAAO,eAAe,IAAI,EAAE,IAAI,4BAAU;AAAA,EAC5C;AACF,CAAC;;;ADhWD,IAAO,gBAAQ;","names":["ProseMirrorNode"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Node } from '@tiptap/core';
|
|
2
|
+
import { Node as Node$1, DOMOutputSpec } from '@tiptap/pm/model';
|
|
3
|
+
import { SuggestionOptions } from '@tiptap/suggestion';
|
|
4
|
+
|
|
5
|
+
interface MentionNodeAttrs {
|
|
6
|
+
/**
|
|
7
|
+
* The identifier for the selected item that was mentioned, stored as a `data-id`
|
|
8
|
+
* attribute.
|
|
9
|
+
*/
|
|
10
|
+
id: string | null;
|
|
11
|
+
/**
|
|
12
|
+
* The label to be rendered by the editor as the displayed text for this mentioned
|
|
13
|
+
* item, if provided. Stored as a `data-label` attribute. See `renderLabel`.
|
|
14
|
+
*/
|
|
15
|
+
label?: string | null;
|
|
16
|
+
/**
|
|
17
|
+
* The character that triggers the suggestion, stored as
|
|
18
|
+
* `data-mention-suggestion-char` attribute.
|
|
19
|
+
*/
|
|
20
|
+
mentionSuggestionChar?: string;
|
|
21
|
+
}
|
|
22
|
+
interface MentionOptions<SuggestionItem = any, Attrs extends Record<string, any> = MentionNodeAttrs> {
|
|
23
|
+
/**
|
|
24
|
+
* The HTML attributes for a mention node.
|
|
25
|
+
* @default {}
|
|
26
|
+
* @example { class: 'foo' }
|
|
27
|
+
*/
|
|
28
|
+
HTMLAttributes: Record<string, any>;
|
|
29
|
+
/**
|
|
30
|
+
* A function to render the label of a mention.
|
|
31
|
+
* @deprecated use renderText and renderHTML instead
|
|
32
|
+
* @param props The render props
|
|
33
|
+
* @returns The label
|
|
34
|
+
* @example ({ options, node }) => `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
|
|
35
|
+
*/
|
|
36
|
+
renderLabel?: (props: {
|
|
37
|
+
options: MentionOptions<SuggestionItem, Attrs>;
|
|
38
|
+
node: Node$1;
|
|
39
|
+
suggestion: SuggestionOptions | null;
|
|
40
|
+
}) => string;
|
|
41
|
+
/**
|
|
42
|
+
* A function to render the text of a mention.
|
|
43
|
+
* @param props The render props
|
|
44
|
+
* @returns The text
|
|
45
|
+
* @example ({ options, node }) => `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
|
|
46
|
+
*/
|
|
47
|
+
renderText: (props: {
|
|
48
|
+
options: MentionOptions<SuggestionItem, Attrs>;
|
|
49
|
+
node: Node$1;
|
|
50
|
+
suggestion: SuggestionOptions | null;
|
|
51
|
+
}) => string;
|
|
52
|
+
/**
|
|
53
|
+
* A function to render the HTML of a mention.
|
|
54
|
+
* @param props The render props
|
|
55
|
+
* @returns The HTML as a ProseMirror DOM Output Spec
|
|
56
|
+
* @example ({ options, node }) => ['span', { 'data-type': 'mention' }, `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`]
|
|
57
|
+
*/
|
|
58
|
+
renderHTML: (props: {
|
|
59
|
+
options: MentionOptions<SuggestionItem, Attrs>;
|
|
60
|
+
node: Node$1;
|
|
61
|
+
suggestion: SuggestionOptions | null;
|
|
62
|
+
}) => DOMOutputSpec;
|
|
63
|
+
/**
|
|
64
|
+
* Whether to delete the trigger character with backspace.
|
|
65
|
+
* @default false
|
|
66
|
+
*/
|
|
67
|
+
deleteTriggerWithBackspace: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* The suggestion options, when you want to support multiple triggers.
|
|
70
|
+
*
|
|
71
|
+
* With this parameter, you can define multiple types of mention. For example, you can use the `@` character
|
|
72
|
+
* to mention users and the `#` character to mention tags.
|
|
73
|
+
*
|
|
74
|
+
* @default [{ char: '@', pluginKey: MentionPluginKey }]
|
|
75
|
+
* @example [{ char: '@', pluginKey: MentionPluginKey }, { char: '#', pluginKey: new PluginKey('hashtag') }]
|
|
76
|
+
*/
|
|
77
|
+
suggestions: Array<Omit<SuggestionOptions<SuggestionItem, Attrs>, 'editor'>>;
|
|
78
|
+
/**
|
|
79
|
+
* The suggestion options, when you want to support only one trigger. To support multiple triggers, use the
|
|
80
|
+
* `suggestions` parameter instead.
|
|
81
|
+
*
|
|
82
|
+
* @default {}
|
|
83
|
+
* @example { char: '@', pluginKey: MentionPluginKey, command: ({ editor, range, props }) => { ... } }
|
|
84
|
+
*/
|
|
85
|
+
suggestion: Omit<SuggestionOptions<SuggestionItem, Attrs>, 'editor'>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* This extension allows you to insert mentions into the editor.
|
|
89
|
+
* @see https://www.tiptap.dev/api/extensions/mention
|
|
90
|
+
*/
|
|
91
|
+
declare const Mention: Node<MentionOptions<any, MentionNodeAttrs>, any>;
|
|
92
|
+
|
|
93
|
+
export { Mention, type MentionNodeAttrs, type MentionOptions, Mention as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Node } from '@tiptap/core';
|
|
2
|
+
import { Node as Node$1, DOMOutputSpec } from '@tiptap/pm/model';
|
|
3
|
+
import { SuggestionOptions } from '@tiptap/suggestion';
|
|
4
|
+
|
|
5
|
+
interface MentionNodeAttrs {
|
|
6
|
+
/**
|
|
7
|
+
* The identifier for the selected item that was mentioned, stored as a `data-id`
|
|
8
|
+
* attribute.
|
|
9
|
+
*/
|
|
10
|
+
id: string | null;
|
|
11
|
+
/**
|
|
12
|
+
* The label to be rendered by the editor as the displayed text for this mentioned
|
|
13
|
+
* item, if provided. Stored as a `data-label` attribute. See `renderLabel`.
|
|
14
|
+
*/
|
|
15
|
+
label?: string | null;
|
|
16
|
+
/**
|
|
17
|
+
* The character that triggers the suggestion, stored as
|
|
18
|
+
* `data-mention-suggestion-char` attribute.
|
|
19
|
+
*/
|
|
20
|
+
mentionSuggestionChar?: string;
|
|
21
|
+
}
|
|
22
|
+
interface MentionOptions<SuggestionItem = any, Attrs extends Record<string, any> = MentionNodeAttrs> {
|
|
23
|
+
/**
|
|
24
|
+
* The HTML attributes for a mention node.
|
|
25
|
+
* @default {}
|
|
26
|
+
* @example { class: 'foo' }
|
|
27
|
+
*/
|
|
28
|
+
HTMLAttributes: Record<string, any>;
|
|
29
|
+
/**
|
|
30
|
+
* A function to render the label of a mention.
|
|
31
|
+
* @deprecated use renderText and renderHTML instead
|
|
32
|
+
* @param props The render props
|
|
33
|
+
* @returns The label
|
|
34
|
+
* @example ({ options, node }) => `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
|
|
35
|
+
*/
|
|
36
|
+
renderLabel?: (props: {
|
|
37
|
+
options: MentionOptions<SuggestionItem, Attrs>;
|
|
38
|
+
node: Node$1;
|
|
39
|
+
suggestion: SuggestionOptions | null;
|
|
40
|
+
}) => string;
|
|
41
|
+
/**
|
|
42
|
+
* A function to render the text of a mention.
|
|
43
|
+
* @param props The render props
|
|
44
|
+
* @returns The text
|
|
45
|
+
* @example ({ options, node }) => `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`
|
|
46
|
+
*/
|
|
47
|
+
renderText: (props: {
|
|
48
|
+
options: MentionOptions<SuggestionItem, Attrs>;
|
|
49
|
+
node: Node$1;
|
|
50
|
+
suggestion: SuggestionOptions | null;
|
|
51
|
+
}) => string;
|
|
52
|
+
/**
|
|
53
|
+
* A function to render the HTML of a mention.
|
|
54
|
+
* @param props The render props
|
|
55
|
+
* @returns The HTML as a ProseMirror DOM Output Spec
|
|
56
|
+
* @example ({ options, node }) => ['span', { 'data-type': 'mention' }, `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`]
|
|
57
|
+
*/
|
|
58
|
+
renderHTML: (props: {
|
|
59
|
+
options: MentionOptions<SuggestionItem, Attrs>;
|
|
60
|
+
node: Node$1;
|
|
61
|
+
suggestion: SuggestionOptions | null;
|
|
62
|
+
}) => DOMOutputSpec;
|
|
63
|
+
/**
|
|
64
|
+
* Whether to delete the trigger character with backspace.
|
|
65
|
+
* @default false
|
|
66
|
+
*/
|
|
67
|
+
deleteTriggerWithBackspace: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* The suggestion options, when you want to support multiple triggers.
|
|
70
|
+
*
|
|
71
|
+
* With this parameter, you can define multiple types of mention. For example, you can use the `@` character
|
|
72
|
+
* to mention users and the `#` character to mention tags.
|
|
73
|
+
*
|
|
74
|
+
* @default [{ char: '@', pluginKey: MentionPluginKey }]
|
|
75
|
+
* @example [{ char: '@', pluginKey: MentionPluginKey }, { char: '#', pluginKey: new PluginKey('hashtag') }]
|
|
76
|
+
*/
|
|
77
|
+
suggestions: Array<Omit<SuggestionOptions<SuggestionItem, Attrs>, 'editor'>>;
|
|
78
|
+
/**
|
|
79
|
+
* The suggestion options, when you want to support only one trigger. To support multiple triggers, use the
|
|
80
|
+
* `suggestions` parameter instead.
|
|
81
|
+
*
|
|
82
|
+
* @default {}
|
|
83
|
+
* @example { char: '@', pluginKey: MentionPluginKey, command: ({ editor, range, props }) => { ... } }
|
|
84
|
+
*/
|
|
85
|
+
suggestion: Omit<SuggestionOptions<SuggestionItem, Attrs>, 'editor'>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* This extension allows you to insert mentions into the editor.
|
|
89
|
+
* @see https://www.tiptap.dev/api/extensions/mention
|
|
90
|
+
*/
|
|
91
|
+
declare const Mention: Node<MentionOptions<any, MentionNodeAttrs>, any>;
|
|
92
|
+
|
|
93
|
+
export { Mention, type MentionNodeAttrs, type MentionOptions, Mention as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// src/mention.ts
|
|
2
|
+
import { createInlineMarkdownSpec, mergeAttributes, Node } from "@tiptap/core";
|
|
3
|
+
import { Node as ProseMirrorNode } from "@tiptap/pm/model";
|
|
4
|
+
import { Suggestion } from "@tiptap/suggestion";
|
|
5
|
+
|
|
6
|
+
// src/utils/get-default-suggestion-attributes.ts
|
|
7
|
+
import { PluginKey } from "@tiptap/pm/state";
|
|
8
|
+
function getSuggestionOptions({
|
|
9
|
+
editor: tiptapEditor,
|
|
10
|
+
overrideSuggestionOptions,
|
|
11
|
+
extensionName,
|
|
12
|
+
char = "@"
|
|
13
|
+
}) {
|
|
14
|
+
const pluginKey = new PluginKey();
|
|
15
|
+
return {
|
|
16
|
+
editor: tiptapEditor,
|
|
17
|
+
char,
|
|
18
|
+
pluginKey,
|
|
19
|
+
command: ({ editor, range, props }) => {
|
|
20
|
+
var _a, _b, _c;
|
|
21
|
+
const nodeAfter = editor.view.state.selection.$to.nodeAfter;
|
|
22
|
+
const overrideSpace = (_a = nodeAfter == null ? void 0 : nodeAfter.text) == null ? void 0 : _a.startsWith(" ");
|
|
23
|
+
if (overrideSpace) {
|
|
24
|
+
range.to += 1;
|
|
25
|
+
}
|
|
26
|
+
editor.chain().focus().insertContentAt(range, [
|
|
27
|
+
{
|
|
28
|
+
type: extensionName,
|
|
29
|
+
attrs: { ...props, mentionSuggestionChar: char }
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
type: "text",
|
|
33
|
+
text: " "
|
|
34
|
+
}
|
|
35
|
+
]).run();
|
|
36
|
+
(_c = (_b = editor.view.dom.ownerDocument.defaultView) == null ? void 0 : _b.getSelection()) == null ? void 0 : _c.collapseToEnd();
|
|
37
|
+
},
|
|
38
|
+
allow: ({ state, range }) => {
|
|
39
|
+
const $from = state.doc.resolve(range.from);
|
|
40
|
+
const type = state.schema.nodes[extensionName];
|
|
41
|
+
const allow = !!$from.parent.type.contentMatch.matchType(type);
|
|
42
|
+
return allow;
|
|
43
|
+
},
|
|
44
|
+
...overrideSuggestionOptions
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/mention.ts
|
|
49
|
+
function getSuggestions(options) {
|
|
50
|
+
return (options.options.suggestions.length ? options.options.suggestions : [options.options.suggestion]).map(
|
|
51
|
+
(suggestion) => getSuggestionOptions({
|
|
52
|
+
// @ts-ignore `editor` can be `undefined` when converting the document to HTML with the HTML utility
|
|
53
|
+
editor: options.editor,
|
|
54
|
+
overrideSuggestionOptions: suggestion,
|
|
55
|
+
extensionName: options.name,
|
|
56
|
+
char: suggestion.char
|
|
57
|
+
})
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
function getSuggestionFromChar(options, char) {
|
|
61
|
+
const suggestions = getSuggestions(options);
|
|
62
|
+
const suggestion = suggestions.find((s) => s.char === char);
|
|
63
|
+
if (suggestion) {
|
|
64
|
+
return suggestion;
|
|
65
|
+
}
|
|
66
|
+
if (suggestions.length) {
|
|
67
|
+
return suggestions[0];
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
var Mention = Node.create({
|
|
72
|
+
name: "mention",
|
|
73
|
+
priority: 101,
|
|
74
|
+
addOptions() {
|
|
75
|
+
return {
|
|
76
|
+
HTMLAttributes: {},
|
|
77
|
+
renderText({ node, suggestion }) {
|
|
78
|
+
var _a, _b;
|
|
79
|
+
return `${(_a = suggestion == null ? void 0 : suggestion.char) != null ? _a : "@"}${(_b = node.attrs.label) != null ? _b : node.attrs.id}`;
|
|
80
|
+
},
|
|
81
|
+
deleteTriggerWithBackspace: false,
|
|
82
|
+
renderHTML({ options, node, suggestion }) {
|
|
83
|
+
var _a, _b;
|
|
84
|
+
return [
|
|
85
|
+
"span",
|
|
86
|
+
mergeAttributes(this.HTMLAttributes, options.HTMLAttributes),
|
|
87
|
+
`${(_a = suggestion == null ? void 0 : suggestion.char) != null ? _a : "@"}${(_b = node.attrs.label) != null ? _b : node.attrs.id}`
|
|
88
|
+
];
|
|
89
|
+
},
|
|
90
|
+
suggestions: [],
|
|
91
|
+
suggestion: {}
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
group: "inline",
|
|
95
|
+
inline: true,
|
|
96
|
+
selectable: false,
|
|
97
|
+
atom: true,
|
|
98
|
+
addAttributes() {
|
|
99
|
+
return {
|
|
100
|
+
id: {
|
|
101
|
+
default: null,
|
|
102
|
+
parseHTML: (element) => element.getAttribute("data-id"),
|
|
103
|
+
renderHTML: (attributes) => {
|
|
104
|
+
if (!attributes.id) {
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
"data-id": attributes.id
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
label: {
|
|
113
|
+
default: null,
|
|
114
|
+
parseHTML: (element) => element.getAttribute("data-label"),
|
|
115
|
+
renderHTML: (attributes) => {
|
|
116
|
+
if (!attributes.label) {
|
|
117
|
+
return {};
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
"data-label": attributes.label
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
// When there are multiple types of mentions, this attribute helps distinguish them
|
|
125
|
+
mentionSuggestionChar: {
|
|
126
|
+
default: "@",
|
|
127
|
+
parseHTML: (element) => element.getAttribute("data-mention-suggestion-char"),
|
|
128
|
+
renderHTML: (attributes) => {
|
|
129
|
+
return {
|
|
130
|
+
"data-mention-suggestion-char": attributes.mentionSuggestionChar
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
parseHTML() {
|
|
137
|
+
return [
|
|
138
|
+
{
|
|
139
|
+
tag: `span[data-type="${this.name}"]`
|
|
140
|
+
}
|
|
141
|
+
];
|
|
142
|
+
},
|
|
143
|
+
renderHTML({ node, HTMLAttributes }) {
|
|
144
|
+
const suggestion = getSuggestionFromChar(this, node.attrs.mentionSuggestionChar);
|
|
145
|
+
if (this.options.renderLabel !== void 0) {
|
|
146
|
+
console.warn("renderLabel is deprecated use renderText and renderHTML instead");
|
|
147
|
+
return [
|
|
148
|
+
"span",
|
|
149
|
+
mergeAttributes({ "data-type": this.name }, this.options.HTMLAttributes, HTMLAttributes),
|
|
150
|
+
this.options.renderLabel({
|
|
151
|
+
options: this.options,
|
|
152
|
+
node,
|
|
153
|
+
suggestion
|
|
154
|
+
})
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
const mergedOptions = { ...this.options };
|
|
158
|
+
mergedOptions.HTMLAttributes = mergeAttributes(
|
|
159
|
+
{ "data-type": this.name },
|
|
160
|
+
this.options.HTMLAttributes,
|
|
161
|
+
HTMLAttributes
|
|
162
|
+
);
|
|
163
|
+
const html = this.options.renderHTML({
|
|
164
|
+
options: mergedOptions,
|
|
165
|
+
node,
|
|
166
|
+
suggestion
|
|
167
|
+
});
|
|
168
|
+
if (typeof html === "string") {
|
|
169
|
+
return ["span", mergeAttributes({ "data-type": this.name }, this.options.HTMLAttributes, HTMLAttributes), html];
|
|
170
|
+
}
|
|
171
|
+
return html;
|
|
172
|
+
},
|
|
173
|
+
...createInlineMarkdownSpec({
|
|
174
|
+
nodeName: "mention",
|
|
175
|
+
name: "@",
|
|
176
|
+
selfClosing: true,
|
|
177
|
+
allowedAttributes: ["id", "label", { name: "mentionSuggestionChar", skipIfDefault: "@" }],
|
|
178
|
+
parseAttributes: (attrString) => {
|
|
179
|
+
const attrs = {};
|
|
180
|
+
const regex = /(\w+)=(?:"([^"]*)"|'([^']*)')/g;
|
|
181
|
+
let match = regex.exec(attrString);
|
|
182
|
+
while (match !== null) {
|
|
183
|
+
const [, key, doubleQuoted, singleQuoted] = match;
|
|
184
|
+
const value = doubleQuoted != null ? doubleQuoted : singleQuoted;
|
|
185
|
+
attrs[key === "char" ? "mentionSuggestionChar" : key] = value;
|
|
186
|
+
match = regex.exec(attrString);
|
|
187
|
+
}
|
|
188
|
+
return attrs;
|
|
189
|
+
},
|
|
190
|
+
serializeAttributes: (attrs) => {
|
|
191
|
+
return Object.entries(attrs).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => {
|
|
192
|
+
const serializedKey = key === "mentionSuggestionChar" ? "char" : key;
|
|
193
|
+
return `${serializedKey}="${value}"`;
|
|
194
|
+
}).join(" ");
|
|
195
|
+
}
|
|
196
|
+
}),
|
|
197
|
+
renderText({ node }) {
|
|
198
|
+
const args = {
|
|
199
|
+
options: this.options,
|
|
200
|
+
node,
|
|
201
|
+
suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar)
|
|
202
|
+
};
|
|
203
|
+
if (this.options.renderLabel !== void 0) {
|
|
204
|
+
console.warn("renderLabel is deprecated use renderText and renderHTML instead");
|
|
205
|
+
return this.options.renderLabel(args);
|
|
206
|
+
}
|
|
207
|
+
return this.options.renderText(args);
|
|
208
|
+
},
|
|
209
|
+
addKeyboardShortcuts() {
|
|
210
|
+
return {
|
|
211
|
+
Backspace: () => this.editor.commands.command(({ tr, state }) => {
|
|
212
|
+
let isMention = false;
|
|
213
|
+
const { selection } = state;
|
|
214
|
+
const { empty, anchor } = selection;
|
|
215
|
+
if (!empty) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
let mentionNode = new ProseMirrorNode();
|
|
219
|
+
let mentionPos = 0;
|
|
220
|
+
state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
|
|
221
|
+
if (node.type.name === this.name) {
|
|
222
|
+
isMention = true;
|
|
223
|
+
mentionNode = node;
|
|
224
|
+
mentionPos = pos;
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
if (isMention) {
|
|
229
|
+
tr.insertText(
|
|
230
|
+
this.options.deleteTriggerWithBackspace ? "" : mentionNode.attrs.mentionSuggestionChar,
|
|
231
|
+
mentionPos,
|
|
232
|
+
mentionPos + mentionNode.nodeSize
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
return isMention;
|
|
236
|
+
})
|
|
237
|
+
};
|
|
238
|
+
},
|
|
239
|
+
addProseMirrorPlugins() {
|
|
240
|
+
return getSuggestions(this).map(Suggestion);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// src/index.ts
|
|
245
|
+
var index_default = Mention;
|
|
246
|
+
export {
|
|
247
|
+
Mention,
|
|
248
|
+
index_default as default
|
|
249
|
+
};
|
|
250
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/mention.ts","../src/utils/get-default-suggestion-attributes.ts","../src/index.ts"],"sourcesContent":["import type { Editor } from '@tiptap/core'\nimport { createInlineMarkdownSpec, mergeAttributes, Node } from '@tiptap/core'\nimport type { DOMOutputSpec } from '@tiptap/pm/model'\nimport { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport type { SuggestionOptions } from '@tiptap/suggestion'\nimport { Suggestion } from '@tiptap/suggestion'\n\nimport { getSuggestionOptions } from './utils/get-default-suggestion-attributes.js'\n\n// See `addAttributes` below\nexport interface MentionNodeAttrs {\n /**\n * The identifier for the selected item that was mentioned, stored as a `data-id`\n * attribute.\n */\n id: string | null\n /**\n * The label to be rendered by the editor as the displayed text for this mentioned\n * item, if provided. Stored as a `data-label` attribute. See `renderLabel`.\n */\n label?: string | null\n /**\n * The character that triggers the suggestion, stored as\n * `data-mention-suggestion-char` attribute.\n */\n mentionSuggestionChar?: string\n}\n\nexport interface MentionOptions<SuggestionItem = any, Attrs extends Record<string, any> = MentionNodeAttrs> {\n /**\n * The HTML attributes for a mention node.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n\n /**\n * A function to render the label of a mention.\n * @deprecated use renderText and renderHTML instead\n * @param props The render props\n * @returns The label\n * @example ({ options, node }) => `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`\n */\n renderLabel?: (props: {\n options: MentionOptions<SuggestionItem, Attrs>\n node: ProseMirrorNode\n suggestion: SuggestionOptions | null\n }) => string\n\n /**\n * A function to render the text of a mention.\n * @param props The render props\n * @returns The text\n * @example ({ options, node }) => `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`\n */\n renderText: (props: {\n options: MentionOptions<SuggestionItem, Attrs>\n node: ProseMirrorNode\n suggestion: SuggestionOptions | null\n }) => string\n\n /**\n * A function to render the HTML of a mention.\n * @param props The render props\n * @returns The HTML as a ProseMirror DOM Output Spec\n * @example ({ options, node }) => ['span', { 'data-type': 'mention' }, `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`]\n */\n renderHTML: (props: {\n options: MentionOptions<SuggestionItem, Attrs>\n node: ProseMirrorNode\n suggestion: SuggestionOptions | null\n }) => DOMOutputSpec\n\n /**\n * Whether to delete the trigger character with backspace.\n * @default false\n */\n deleteTriggerWithBackspace: boolean\n\n /**\n * The suggestion options, when you want to support multiple triggers.\n *\n * With this parameter, you can define multiple types of mention. For example, you can use the `@` character\n * to mention users and the `#` character to mention tags.\n *\n * @default [{ char: '@', pluginKey: MentionPluginKey }]\n * @example [{ char: '@', pluginKey: MentionPluginKey }, { char: '#', pluginKey: new PluginKey('hashtag') }]\n */\n suggestions: Array<Omit<SuggestionOptions<SuggestionItem, Attrs>, 'editor'>>\n\n /**\n * The suggestion options, when you want to support only one trigger. To support multiple triggers, use the\n * `suggestions` parameter instead.\n *\n * @default {}\n * @example { char: '@', pluginKey: MentionPluginKey, command: ({ editor, range, props }) => { ... } }\n */\n suggestion: Omit<SuggestionOptions<SuggestionItem, Attrs>, 'editor'>\n}\n\ninterface GetSuggestionsOptions {\n editor?: Editor\n options: MentionOptions\n name: string\n}\n\n/**\n * Returns the suggestions for the mention extension.\n *\n * @param options The extension options\n * @returns the suggestions\n */\nfunction getSuggestions(options: GetSuggestionsOptions) {\n return (options.options.suggestions.length ? options.options.suggestions : [options.options.suggestion]).map(\n suggestion =>\n getSuggestionOptions({\n // @ts-ignore `editor` can be `undefined` when converting the document to HTML with the HTML utility\n editor: options.editor,\n overrideSuggestionOptions: suggestion,\n extensionName: options.name,\n char: suggestion.char,\n }),\n )\n}\n\n/**\n * Returns the suggestion options of the mention that has a given character trigger. If not\n * found, it returns the first suggestion.\n *\n * @param options The extension options\n * @param char The character that triggers the mention\n * @returns The suggestion options\n */\nfunction getSuggestionFromChar(options: GetSuggestionsOptions, char: string) {\n const suggestions = getSuggestions(options)\n\n const suggestion = suggestions.find(s => s.char === char)\n if (suggestion) {\n return suggestion\n }\n\n if (suggestions.length) {\n return suggestions[0]\n }\n\n return null\n}\n\n/**\n * This extension allows you to insert mentions into the editor.\n * @see https://www.tiptap.dev/api/extensions/mention\n */\nexport const Mention = Node.create<MentionOptions>({\n name: 'mention',\n\n priority: 101,\n\n addOptions() {\n return {\n HTMLAttributes: {},\n renderText({ node, suggestion }) {\n return `${suggestion?.char ?? '@'}${node.attrs.label ?? node.attrs.id}`\n },\n deleteTriggerWithBackspace: false,\n renderHTML({ options, node, suggestion }) {\n return [\n 'span',\n mergeAttributes(this.HTMLAttributes, options.HTMLAttributes),\n `${suggestion?.char ?? '@'}${node.attrs.label ?? node.attrs.id}`,\n ]\n },\n suggestions: [],\n suggestion: {},\n }\n },\n\n group: 'inline',\n\n inline: true,\n\n selectable: false,\n\n atom: true,\n\n addAttributes() {\n return {\n id: {\n default: null,\n parseHTML: element => element.getAttribute('data-id'),\n renderHTML: attributes => {\n if (!attributes.id) {\n return {}\n }\n\n return {\n 'data-id': attributes.id,\n }\n },\n },\n\n label: {\n default: null,\n parseHTML: element => element.getAttribute('data-label'),\n renderHTML: attributes => {\n if (!attributes.label) {\n return {}\n }\n\n return {\n 'data-label': attributes.label,\n }\n },\n },\n\n // When there are multiple types of mentions, this attribute helps distinguish them\n mentionSuggestionChar: {\n default: '@',\n parseHTML: element => element.getAttribute('data-mention-suggestion-char'),\n renderHTML: attributes => {\n return {\n 'data-mention-suggestion-char': attributes.mentionSuggestionChar,\n }\n },\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: `span[data-type=\"${this.name}\"]`,\n },\n ]\n },\n\n renderHTML({ node, HTMLAttributes }) {\n const suggestion = getSuggestionFromChar(this, node.attrs.mentionSuggestionChar)\n\n if (this.options.renderLabel !== undefined) {\n console.warn('renderLabel is deprecated use renderText and renderHTML instead')\n return [\n 'span',\n mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),\n this.options.renderLabel({\n options: this.options,\n node,\n suggestion,\n }),\n ]\n }\n const mergedOptions = { ...this.options }\n\n mergedOptions.HTMLAttributes = mergeAttributes(\n { 'data-type': this.name },\n this.options.HTMLAttributes,\n HTMLAttributes,\n )\n\n const html = this.options.renderHTML({\n options: mergedOptions,\n node,\n suggestion,\n })\n\n if (typeof html === 'string') {\n return ['span', mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes), html]\n }\n return html\n },\n\n ...createInlineMarkdownSpec({\n nodeName: 'mention',\n name: '@',\n selfClosing: true,\n allowedAttributes: ['id', 'label', { name: 'mentionSuggestionChar', skipIfDefault: '@' }],\n parseAttributes: (attrString: string) => {\n const attrs: Record<string, any> = {}\n const regex = /(\\w+)=(?:\"([^\"]*)\"|'([^']*)')/g\n let match = regex.exec(attrString)\n\n while (match !== null) {\n const [, key, doubleQuoted, singleQuoted] = match\n const value = doubleQuoted ?? singleQuoted\n attrs[key === 'char' ? 'mentionSuggestionChar' : key] = value\n match = regex.exec(attrString)\n }\n\n return attrs\n },\n serializeAttributes: (attrs: Record<string, any>) => {\n return Object.entries(attrs)\n .filter(([, value]) => value !== undefined && value !== null)\n .map(([key, value]) => {\n const serializedKey = key === 'mentionSuggestionChar' ? 'char' : key\n return `${serializedKey}=\"${value}\"`\n })\n .join(' ')\n },\n }),\n\n renderText({ node }) {\n const args = {\n options: this.options,\n node,\n suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar),\n }\n if (this.options.renderLabel !== undefined) {\n console.warn('renderLabel is deprecated use renderText and renderHTML instead')\n return this.options.renderLabel(args)\n }\n\n return this.options.renderText(args)\n },\n\n addKeyboardShortcuts() {\n return {\n Backspace: () =>\n this.editor.commands.command(({ tr, state }) => {\n let isMention = false\n const { selection } = state\n const { empty, anchor } = selection\n\n if (!empty) {\n return false\n }\n\n // Store node and position for later use\n let mentionNode = new ProseMirrorNode()\n let mentionPos = 0\n\n state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {\n if (node.type.name === this.name) {\n isMention = true\n mentionNode = node\n mentionPos = pos\n return false\n }\n })\n\n if (isMention) {\n tr.insertText(\n this.options.deleteTriggerWithBackspace ? '' : mentionNode.attrs.mentionSuggestionChar,\n mentionPos,\n mentionPos + mentionNode.nodeSize,\n )\n }\n\n return isMention\n }),\n }\n },\n\n addProseMirrorPlugins() {\n // Create a plugin for each suggestion configuration\n return getSuggestions(this).map(Suggestion)\n },\n})\n","import type { Editor } from '@tiptap/core'\nimport { PluginKey } from '@tiptap/pm/state'\nimport type { SuggestionOptions } from '@tiptap/suggestion'\n\n/**\n * Arguments for the `getSuggestionOptions` function\n * @see getSuggestionOptions\n */\nexport interface GetSuggestionOptionsOptions {\n /**\n * The Tiptap editor instance.\n */\n editor: Editor\n /**\n * The suggestion options configuration provided to the\n * `Mention` extension.\n */\n overrideSuggestionOptions: Omit<SuggestionOptions, 'editor'>\n /**\n * The name of the Mention extension\n */\n extensionName: string\n /**\n * The character that triggers the suggestion.\n * @default '@'\n */\n char?: string\n}\n\n/**\n * Returns the suggestion options for a trigger of the Mention extension. These\n * options are used to create a `Suggestion` ProseMirror plugin. Each plugin lets\n * you define a different trigger that opens the Mention menu. For example,\n * you can define a `@` trigger to mention users and a `#` trigger to mention\n * tags.\n *\n * @param param0 The configured options for the suggestion\n * @returns\n */\nexport function getSuggestionOptions({\n editor: tiptapEditor,\n overrideSuggestionOptions,\n extensionName,\n char = '@',\n}: GetSuggestionOptionsOptions): SuggestionOptions {\n const pluginKey = new PluginKey()\n\n return {\n editor: tiptapEditor,\n char,\n pluginKey,\n command: ({ editor, range, props }: { editor: any; range: any; props: any }) => {\n // increase range.to by one when the next node is of type \"text\"\n // and starts with a space character\n const nodeAfter = editor.view.state.selection.$to.nodeAfter\n const overrideSpace = nodeAfter?.text?.startsWith(' ')\n\n if (overrideSpace) {\n range.to += 1\n }\n\n editor\n .chain()\n .focus()\n .insertContentAt(range, [\n {\n type: extensionName,\n attrs: { ...props, mentionSuggestionChar: char },\n },\n {\n type: 'text',\n text: ' ',\n },\n ])\n .run()\n\n // get reference to `window` object from editor element, to support cross-frame JS usage\n editor.view.dom.ownerDocument.defaultView?.getSelection()?.collapseToEnd()\n },\n allow: ({ state, range }: { state: any; range: any }) => {\n const $from = state.doc.resolve(range.from)\n const type = state.schema.nodes[extensionName]\n const allow = !!$from.parent.type.contentMatch.matchType(type)\n\n return allow\n },\n ...overrideSuggestionOptions,\n }\n}\n","import { Mention } from './mention.js'\n\nexport * from './mention.js'\n\nexport default Mention\n"],"mappings":";AACA,SAAS,0BAA0B,iBAAiB,YAAY;AAEhE,SAAS,QAAQ,uBAAuB;AAExC,SAAS,kBAAkB;;;ACJ3B,SAAS,iBAAiB;AAsCnB,SAAS,qBAAqB;AAAA,EACnC,QAAQ;AAAA,EACR;AAAA,EACA;AAAA,EACA,OAAO;AACT,GAAmD;AACjD,QAAM,YAAY,IAAI,UAAU;AAEhC,SAAO;AAAA,IACL,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA,SAAS,CAAC,EAAE,QAAQ,OAAO,MAAM,MAA+C;AAnDpF;AAsDM,YAAM,YAAY,OAAO,KAAK,MAAM,UAAU,IAAI;AAClD,YAAM,iBAAgB,4CAAW,SAAX,mBAAiB,WAAW;AAElD,UAAI,eAAe;AACjB,cAAM,MAAM;AAAA,MACd;AAEA,aACG,MAAM,EACN,MAAM,EACN,gBAAgB,OAAO;AAAA,QACtB;AAAA,UACE,MAAM;AAAA,UACN,OAAO,EAAE,GAAG,OAAO,uBAAuB,KAAK;AAAA,QACjD;AAAA,QACA;AAAA,UACE,MAAM;AAAA,UACN,MAAM;AAAA,QACR;AAAA,MACF,CAAC,EACA,IAAI;AAGP,yBAAO,KAAK,IAAI,cAAc,gBAA9B,mBAA2C,mBAA3C,mBAA2D;AAAA,IAC7D;AAAA,IACA,OAAO,CAAC,EAAE,OAAO,MAAM,MAAkC;AACvD,YAAM,QAAQ,MAAM,IAAI,QAAQ,MAAM,IAAI;AAC1C,YAAM,OAAO,MAAM,OAAO,MAAM,aAAa;AAC7C,YAAM,QA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|
3
3
|
"description": "mention extension for tiptap",
|
|
4
|
-
"version": "3.20.
|
|
4
|
+
"version": "3.20.5",
|
|
5
5
|
"homepage": "https://tiptap.dev",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -31,14 +31,14 @@
|
|
|
31
31
|
"dist"
|
|
32
32
|
],
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@tiptap/core": "^3.20.
|
|
35
|
-
"@tiptap/pm": "^3.20.
|
|
36
|
-
"@tiptap/suggestion": "^3.20.
|
|
34
|
+
"@tiptap/core": "^3.20.5",
|
|
35
|
+
"@tiptap/pm": "^3.20.5",
|
|
36
|
+
"@tiptap/suggestion": "^3.20.5"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@tiptap/core": "^3.20.
|
|
40
|
-
"@tiptap/pm": "^3.20.
|
|
41
|
-
"@tiptap/suggestion": "^3.20.
|
|
39
|
+
"@tiptap/core": "^3.20.5",
|
|
40
|
+
"@tiptap/pm": "^3.20.5",
|
|
41
|
+
"@tiptap/suggestion": "^3.20.5"
|
|
42
42
|
},
|
|
43
43
|
"repository": {
|
|
44
44
|
"type": "git",
|