@tiptap/extension-mention 2.23.0 → 2.24.0
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 +41 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +41 -35
- package/dist/index.js.map +1 -1
- package/dist/index.umd.js +41 -35
- package/dist/index.umd.js.map +1 -1
- package/dist/mention.d.ts +1 -18
- package/dist/mention.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/mention.ts +50 -57
package/dist/index.cjs
CHANGED
|
@@ -63,6 +63,40 @@ function getSuggestionOptions({ editor: tiptapEditor, overrideSuggestionOptions,
|
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Returns the suggestions for the mention extension.
|
|
68
|
+
*
|
|
69
|
+
* @param options The extension options
|
|
70
|
+
* @returns the suggestions
|
|
71
|
+
*/
|
|
72
|
+
function getSuggestions(options) {
|
|
73
|
+
return (options.options.suggestions.length ? options.options.suggestions : [options.options.suggestion]).map(suggestion => getSuggestionOptions({
|
|
74
|
+
// @ts-ignore `editor` can be `undefined` when converting the document to HTML with the HTML utility
|
|
75
|
+
editor: options.editor,
|
|
76
|
+
overrideSuggestionOptions: suggestion,
|
|
77
|
+
extensionName: options.name,
|
|
78
|
+
char: suggestion.char,
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Returns the suggestion options of the mention that has a given character trigger. If not
|
|
83
|
+
* found, it returns the first suggestion.
|
|
84
|
+
*
|
|
85
|
+
* @param options The extension options
|
|
86
|
+
* @param char The character that triggers the mention
|
|
87
|
+
* @returns The suggestion options
|
|
88
|
+
*/
|
|
89
|
+
function getSuggestionFromChar(options, char) {
|
|
90
|
+
const suggestions = getSuggestions(options);
|
|
91
|
+
const suggestion = suggestions.find(s => s.char === char);
|
|
92
|
+
if (suggestion) {
|
|
93
|
+
return suggestion;
|
|
94
|
+
}
|
|
95
|
+
if (suggestions.length) {
|
|
96
|
+
return suggestions[0];
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
66
100
|
/**
|
|
67
101
|
* This extension allows you to insert mentions into the editor.
|
|
68
102
|
* @see https://www.tiptap.dev/api/extensions/mention
|
|
@@ -70,26 +104,20 @@ function getSuggestionOptions({ editor: tiptapEditor, overrideSuggestionOptions,
|
|
|
70
104
|
const Mention = core.Node.create({
|
|
71
105
|
name: 'mention',
|
|
72
106
|
priority: 101,
|
|
73
|
-
addStorage() {
|
|
74
|
-
return {
|
|
75
|
-
suggestions: [],
|
|
76
|
-
getSuggestionFromChar: () => null,
|
|
77
|
-
};
|
|
78
|
-
},
|
|
79
107
|
addOptions() {
|
|
80
108
|
return {
|
|
81
109
|
HTMLAttributes: {},
|
|
82
110
|
renderText({ node, suggestion }) {
|
|
83
|
-
var _a;
|
|
84
|
-
return `${suggestion === null || suggestion === void 0 ? void 0 : suggestion.char}${(
|
|
111
|
+
var _a, _b;
|
|
112
|
+
return `${(_a = suggestion === null || suggestion === void 0 ? void 0 : suggestion.char) !== null && _a !== void 0 ? _a : '@'}${(_b = node.attrs.label) !== null && _b !== void 0 ? _b : node.attrs.id}`;
|
|
85
113
|
},
|
|
86
114
|
deleteTriggerWithBackspace: false,
|
|
87
115
|
renderHTML({ options, node, suggestion }) {
|
|
88
|
-
var _a;
|
|
116
|
+
var _a, _b;
|
|
89
117
|
return [
|
|
90
118
|
'span',
|
|
91
119
|
core.mergeAttributes(this.HTMLAttributes, options.HTMLAttributes),
|
|
92
|
-
`${suggestion === null || suggestion === void 0 ? void 0 : suggestion.char}${(
|
|
120
|
+
`${(_a = suggestion === null || suggestion === void 0 ? void 0 : suggestion.char) !== null && _a !== void 0 ? _a : '@'}${(_b = node.attrs.label) !== null && _b !== void 0 ? _b : node.attrs.id}`,
|
|
93
121
|
];
|
|
94
122
|
},
|
|
95
123
|
suggestions: [],
|
|
@@ -146,10 +174,7 @@ const Mention = core.Node.create({
|
|
|
146
174
|
];
|
|
147
175
|
},
|
|
148
176
|
renderHTML({ node, HTMLAttributes }) {
|
|
149
|
-
|
|
150
|
-
// We cannot use the `this.storage` property here because, when accessed this method,
|
|
151
|
-
// it returns the initial value of the extension storage
|
|
152
|
-
const suggestion = (_c = (_b = (_a = this.editor) === null || _a === void 0 ? void 0 : _a.extensionStorage) === null || _b === void 0 ? void 0 : _b[this.name]) === null || _c === void 0 ? void 0 : _c.getSuggestionFromChar(node.attrs.mentionSuggestionChar);
|
|
177
|
+
const suggestion = getSuggestionFromChar(this, node.attrs.mentionSuggestionChar);
|
|
153
178
|
if (this.options.renderLabel !== undefined) {
|
|
154
179
|
console.warn('renderLabel is deprecated use renderText and renderHTML instead');
|
|
155
180
|
return [
|
|
@@ -179,11 +204,10 @@ const Mention = core.Node.create({
|
|
|
179
204
|
return html;
|
|
180
205
|
},
|
|
181
206
|
renderText({ node }) {
|
|
182
|
-
var _a, _b, _c;
|
|
183
207
|
const args = {
|
|
184
208
|
options: this.options,
|
|
185
209
|
node,
|
|
186
|
-
suggestion: (
|
|
210
|
+
suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar),
|
|
187
211
|
};
|
|
188
212
|
if (this.options.renderLabel !== undefined) {
|
|
189
213
|
console.warn('renderLabel is deprecated use renderText and renderHTML instead');
|
|
@@ -227,25 +251,7 @@ const Mention = core.Node.create({
|
|
|
227
251
|
},
|
|
228
252
|
addProseMirrorPlugins() {
|
|
229
253
|
// Create a plugin for each suggestion configuration
|
|
230
|
-
return this.
|
|
231
|
-
},
|
|
232
|
-
onBeforeCreate() {
|
|
233
|
-
this.storage.suggestions = (this.options.suggestions.length ? this.options.suggestions : [this.options.suggestion]).map(suggestion => getSuggestionOptions({
|
|
234
|
-
editor: this.editor,
|
|
235
|
-
overrideSuggestionOptions: suggestion,
|
|
236
|
-
extensionName: this.name,
|
|
237
|
-
char: suggestion.char,
|
|
238
|
-
}));
|
|
239
|
-
this.storage.getSuggestionFromChar = char => {
|
|
240
|
-
const suggestion = this.storage.suggestions.find(s => s.char === char);
|
|
241
|
-
if (suggestion) {
|
|
242
|
-
return suggestion;
|
|
243
|
-
}
|
|
244
|
-
if (this.storage.suggestions.length) {
|
|
245
|
-
return this.storage.suggestions[0];
|
|
246
|
-
}
|
|
247
|
-
return null;
|
|
248
|
-
};
|
|
254
|
+
return getSuggestions(this).map(Suggestion__default.default);
|
|
249
255
|
},
|
|
250
256
|
});
|
|
251
257
|
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/utils/get-default-suggestion-attributes.ts","../src/mention.ts"],"sourcesContent":["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 { 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\n/**\n * Storage properties or the Mention extension\n */\nexport interface MentionStorage<SuggestionItem = any, Attrs extends Record<string, any> = MentionNodeAttrs> {\n /**\n * The list of suggestions that will trigger the mention.\n */\n suggestions: Array<SuggestionOptions<SuggestionItem, Attrs>>\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 char The character that triggers the mention\n * @returns The suggestion options\n */\n getSuggestionFromChar: (char: string) => SuggestionOptions<SuggestionItem, Attrs> | 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, MentionStorage>({\n name: 'mention',\n\n priority: 101,\n\n addStorage() {\n return {\n suggestions: [],\n getSuggestionFromChar: () => null,\n }\n },\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 // We cannot use the `this.storage` property here because, when accessed this method,\n // it returns the initial value of the extension storage\n const suggestion = (this.editor?.extensionStorage as unknown as Record<string, MentionStorage>)?.[\n this.name\n ]?.getSuggestionFromChar(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 [\n 'span',\n mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),\n html,\n ]\n }\n return html\n },\n\n renderText({ node }) {\n const args = {\n options: this.options,\n node,\n suggestion: (this.editor?.extensionStorage as unknown as Record<string, MentionStorage>)?.[\n this.name\n ]?.getSuggestionFromChar(node.attrs.mentionSuggestionChar),\n }\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: () => 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 state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {\n if (node.type.name === this.name) {\n isMention = true\n tr.insertText(\n this.options.deleteTriggerWithBackspace ? '' : this.options.suggestion.char || '',\n pos,\n pos + node.nodeSize,\n )\n\n return false\n }\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 this.storage.suggestions.map(Suggestion)\n },\n\n onBeforeCreate() {\n this.storage.suggestions = (\n this.options.suggestions.length ? this.options.suggestions : [this.options.suggestion]\n ).map(suggestion => getSuggestionOptions({\n editor: this.editor,\n overrideSuggestionOptions: suggestion,\n extensionName: this.name,\n char: suggestion.char,\n }))\n\n this.storage.getSuggestionFromChar = char => {\n const suggestion = this.storage.suggestions.find(s => s.char === char)\n\n if (suggestion) {\n return suggestion\n }\n if (this.storage.suggestions.length) {\n return this.storage.suggestions[0]\n }\n\n return null\n }\n },\n})\n"],"names":["PluginKey","Node","mergeAttributes","ProseMirrorNode","Suggestion"],"mappings":";;;;;;;;;;;;;AA6BA;;;;;;;;;AASG;AACa,SAAA,oBAAoB,CAAC,EACnC,MAAM,EAAE,YAAY,EACpB,yBAAyB,EACzB,aAAa,EACb,IAAI,GAAG,GAAG,GACkB,EAAA;AAC5B,IAAA,MAAM,SAAS,GAAG,IAAIA,eAAS,EAAE;IAEjC,OAAO;AACL,QAAA,MAAM,EAAE,YAAY;QACpB,IAAI;QACJ,SAAS;QACT,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAA2C,KAAI;;;;AAG7E,YAAA,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS;AAC3D,YAAA,MAAM,aAAa,GAAG,CAAA,EAAA,GAAA,SAAS,aAAT,SAAS,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAT,SAAS,CAAE,IAAI,MAAE,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,UAAU,CAAC,GAAG,CAAC;YAEtD,IAAI,aAAa,EAAE;AACjB,gBAAA,KAAK,CAAC,EAAE,IAAI,CAAC;;YAGf;AACG,iBAAA,KAAK;AACL,iBAAA,KAAK;iBACL,eAAe,CAAC,KAAK,EAAE;AACtB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AAC,CAAC,IAAI,KAAK,IAAI,CAAC;YAEtE,IAAI,UAAU,EAAE;AACd,gBAAA,OAAO,UAAU;;YAEnB,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE;gBACnC,OAAO,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC;;AAGpC,YAAA,OAAO,IAAI;AACb,SAAC;KACF;AACF,CAAA;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/utils/get-default-suggestion-attributes.ts","../src/mention.ts"],"sourcesContent":["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 type { Editor } from '@tiptap/core'\nimport { 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 => 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\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 [\n 'span',\n mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),\n html,\n ]\n }\n return html\n },\n\n renderText({ node }) {\n const args = {\n options: this.options,\n node,\n suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar),\n }\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: () => 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 state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {\n if (node.type.name === this.name) {\n isMention = true\n tr.insertText(\n this.options.deleteTriggerWithBackspace ? '' : this.options.suggestion.char || '',\n pos,\n pos + node.nodeSize,\n )\n\n return false\n }\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"],"names":["PluginKey","Node","mergeAttributes","ProseMirrorNode","Suggestion"],"mappings":";;;;;;;;;;;;;AA6BA;;;;;;;;;AASG;AACa,SAAA,oBAAoB,CAAC,EACnC,MAAM,EAAE,YAAY,EACpB,yBAAyB,EACzB,aAAa,EACb,IAAI,GAAG,GAAG,GACkB,EAAA;AAC5B,IAAA,MAAM,SAAS,GAAG,IAAIA,eAAS,EAAE;IAEjC,OAAO;AACL,QAAA,MAAM,EAAE,YAAY;QACpB,IAAI;QACJ,SAAS;QACT,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAA2C,KAAI;;;;AAG7E,YAAA,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS;AAC3D,YAAA,MAAM,aAAa,GAAG,CAAA,EAAA,GAAA,SAAS,aAAT,SAAS,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAT,SAAS,CAAE,IAAI,MAAE,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,UAAU,CAAC,GAAG,CAAC;YAEtD,IAAI,aAAa,EAAE;AACjB,gBAAA,KAAK,CAAC,EAAE,IAAI,CAAC;;YAGf;AACG,iBAAA,KAAK;AACL,iBAAA,KAAK;iBACL,eAAe,CAAC,KAAK,EAAE;AACtB,gBAAA;AACE,oBAAA,IAAI,EAAE,aAAa;oBACnB,KAAK,EAAE,EAAE,GAAG,KAAK,EAAE,qBAAqB,EAAE,IAAI,EAAE;AACjD,iBAAA;AACD,gBAAA;AACE,oBAAA,IAAI,EAAE,MAAM;AACZ,oBAAA,IAAI,EAAE,GAAG;AACV,iBAAA;aACF;AACA,iBAAA,GAAG,EAAE;;AAGR,YAAA,CAAA,EAAA,G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AU,GAAG,CAAC;AAElB,gBAAA,KAAK,CAAC,GAAG,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,MAAM,EAAE,CAAC,IAAI,EAAE,GAAG,KAAI;oBACvD,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,EAAE;wBAChC,SAAS,GAAG,IAAI;wBAChB,WAAW,GAAG,IAAI;wBAClB,UAAU,GAAG,GAAG;AAChB,wBAAA,OAAO,KAAK;;AAEhB,iBAAC,CAAC;gBAEF,IAAI,SAAS,EAAE;AACb,oBAAA,EAAE,CAAC,UAAU,CACX,IAAI,CAAC,OAAO,CAAC,0BAA0B,GAAG,EAAE,GAAG,WAAW,CAAC,KAAK,CAAC,qBAAqB,EACtF,UAAU,EACV,UAAU,GAAG,WAAW,CAAC,QAAQ,CAClC;;AAGH,gBAAA,OAAO,SAAS;AAClB,aAAC,CAAC;SACH;KACF;IAED,qBAAqB,GAAA;;QAEnB,OAAO,cAAc,CAAC,IAAI,CAAC,CAAC,GAAG,CAACC,2BAAU,CAAC;KAC5C;AACF,CAAA;;;;;"}
|
package/dist/index.js
CHANGED
|
@@ -55,6 +55,40 @@ function getSuggestionOptions({ editor: tiptapEditor, overrideSuggestionOptions,
|
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Returns the suggestions for the mention extension.
|
|
60
|
+
*
|
|
61
|
+
* @param options The extension options
|
|
62
|
+
* @returns the suggestions
|
|
63
|
+
*/
|
|
64
|
+
function getSuggestions(options) {
|
|
65
|
+
return (options.options.suggestions.length ? options.options.suggestions : [options.options.suggestion]).map(suggestion => getSuggestionOptions({
|
|
66
|
+
// @ts-ignore `editor` can be `undefined` when converting the document to HTML with the HTML utility
|
|
67
|
+
editor: options.editor,
|
|
68
|
+
overrideSuggestionOptions: suggestion,
|
|
69
|
+
extensionName: options.name,
|
|
70
|
+
char: suggestion.char,
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns the suggestion options of the mention that has a given character trigger. If not
|
|
75
|
+
* found, it returns the first suggestion.
|
|
76
|
+
*
|
|
77
|
+
* @param options The extension options
|
|
78
|
+
* @param char The character that triggers the mention
|
|
79
|
+
* @returns The suggestion options
|
|
80
|
+
*/
|
|
81
|
+
function getSuggestionFromChar(options, char) {
|
|
82
|
+
const suggestions = getSuggestions(options);
|
|
83
|
+
const suggestion = suggestions.find(s => s.char === char);
|
|
84
|
+
if (suggestion) {
|
|
85
|
+
return suggestion;
|
|
86
|
+
}
|
|
87
|
+
if (suggestions.length) {
|
|
88
|
+
return suggestions[0];
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
58
92
|
/**
|
|
59
93
|
* This extension allows you to insert mentions into the editor.
|
|
60
94
|
* @see https://www.tiptap.dev/api/extensions/mention
|
|
@@ -62,26 +96,20 @@ function getSuggestionOptions({ editor: tiptapEditor, overrideSuggestionOptions,
|
|
|
62
96
|
const Mention = Node.create({
|
|
63
97
|
name: 'mention',
|
|
64
98
|
priority: 101,
|
|
65
|
-
addStorage() {
|
|
66
|
-
return {
|
|
67
|
-
suggestions: [],
|
|
68
|
-
getSuggestionFromChar: () => null,
|
|
69
|
-
};
|
|
70
|
-
},
|
|
71
99
|
addOptions() {
|
|
72
100
|
return {
|
|
73
101
|
HTMLAttributes: {},
|
|
74
102
|
renderText({ node, suggestion }) {
|
|
75
|
-
var _a;
|
|
76
|
-
return `${suggestion === null || suggestion === void 0 ? void 0 : suggestion.char}${(
|
|
103
|
+
var _a, _b;
|
|
104
|
+
return `${(_a = suggestion === null || suggestion === void 0 ? void 0 : suggestion.char) !== null && _a !== void 0 ? _a : '@'}${(_b = node.attrs.label) !== null && _b !== void 0 ? _b : node.attrs.id}`;
|
|
77
105
|
},
|
|
78
106
|
deleteTriggerWithBackspace: false,
|
|
79
107
|
renderHTML({ options, node, suggestion }) {
|
|
80
|
-
var _a;
|
|
108
|
+
var _a, _b;
|
|
81
109
|
return [
|
|
82
110
|
'span',
|
|
83
111
|
mergeAttributes(this.HTMLAttributes, options.HTMLAttributes),
|
|
84
|
-
`${suggestion === null || suggestion === void 0 ? void 0 : suggestion.char}${(
|
|
112
|
+
`${(_a = suggestion === null || suggestion === void 0 ? void 0 : suggestion.char) !== null && _a !== void 0 ? _a : '@'}${(_b = node.attrs.label) !== null && _b !== void 0 ? _b : node.attrs.id}`,
|
|
85
113
|
];
|
|
86
114
|
},
|
|
87
115
|
suggestions: [],
|
|
@@ -138,10 +166,7 @@ const Mention = Node.create({
|
|
|
138
166
|
];
|
|
139
167
|
},
|
|
140
168
|
renderHTML({ node, HTMLAttributes }) {
|
|
141
|
-
|
|
142
|
-
// We cannot use the `this.storage` property here because, when accessed this method,
|
|
143
|
-
// it returns the initial value of the extension storage
|
|
144
|
-
const suggestion = (_c = (_b = (_a = this.editor) === null || _a === void 0 ? void 0 : _a.extensionStorage) === null || _b === void 0 ? void 0 : _b[this.name]) === null || _c === void 0 ? void 0 : _c.getSuggestionFromChar(node.attrs.mentionSuggestionChar);
|
|
169
|
+
const suggestion = getSuggestionFromChar(this, node.attrs.mentionSuggestionChar);
|
|
145
170
|
if (this.options.renderLabel !== undefined) {
|
|
146
171
|
console.warn('renderLabel is deprecated use renderText and renderHTML instead');
|
|
147
172
|
return [
|
|
@@ -171,11 +196,10 @@ const Mention = Node.create({
|
|
|
171
196
|
return html;
|
|
172
197
|
},
|
|
173
198
|
renderText({ node }) {
|
|
174
|
-
var _a, _b, _c;
|
|
175
199
|
const args = {
|
|
176
200
|
options: this.options,
|
|
177
201
|
node,
|
|
178
|
-
suggestion: (
|
|
202
|
+
suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar),
|
|
179
203
|
};
|
|
180
204
|
if (this.options.renderLabel !== undefined) {
|
|
181
205
|
console.warn('renderLabel is deprecated use renderText and renderHTML instead');
|
|
@@ -219,25 +243,7 @@ const Mention = Node.create({
|
|
|
219
243
|
},
|
|
220
244
|
addProseMirrorPlugins() {
|
|
221
245
|
// Create a plugin for each suggestion configuration
|
|
222
|
-
return this.
|
|
223
|
-
},
|
|
224
|
-
onBeforeCreate() {
|
|
225
|
-
this.storage.suggestions = (this.options.suggestions.length ? this.options.suggestions : [this.options.suggestion]).map(suggestion => getSuggestionOptions({
|
|
226
|
-
editor: this.editor,
|
|
227
|
-
overrideSuggestionOptions: suggestion,
|
|
228
|
-
extensionName: this.name,
|
|
229
|
-
char: suggestion.char,
|
|
230
|
-
}));
|
|
231
|
-
this.storage.getSuggestionFromChar = char => {
|
|
232
|
-
const suggestion = this.storage.suggestions.find(s => s.char === char);
|
|
233
|
-
if (suggestion) {
|
|
234
|
-
return suggestion;
|
|
235
|
-
}
|
|
236
|
-
if (this.storage.suggestions.length) {
|
|
237
|
-
return this.storage.suggestions[0];
|
|
238
|
-
}
|
|
239
|
-
return null;
|
|
240
|
-
};
|
|
246
|
+
return getSuggestions(this).map(Suggestion);
|
|
241
247
|
},
|
|
242
248
|
});
|
|
243
249
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/utils/get-default-suggestion-attributes.ts","../src/mention.ts"],"sourcesContent":["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 { 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\n/**\n * Storage properties or the Mention extension\n */\nexport interface MentionStorage<SuggestionItem = any, Attrs extends Record<string, any> = MentionNodeAttrs> {\n /**\n * The list of suggestions that will trigger the mention.\n */\n suggestions: Array<SuggestionOptions<SuggestionItem, Attrs>>\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 char The character that triggers the mention\n * @returns The suggestion options\n */\n getSuggestionFromChar: (char: string) => SuggestionOptions<SuggestionItem, Attrs> | 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, MentionStorage>({\n name: 'mention',\n\n priority: 101,\n\n addStorage() {\n return {\n suggestions: [],\n getSuggestionFromChar: () => null,\n }\n },\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 // We cannot use the `this.storage` property here because, when accessed this method,\n // it returns the initial value of the extension storage\n const suggestion = (this.editor?.extensionStorage as unknown as Record<string, MentionStorage>)?.[\n this.name\n ]?.getSuggestionFromChar(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 [\n 'span',\n mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),\n html,\n ]\n }\n return html\n },\n\n renderText({ node }) {\n const args = {\n options: this.options,\n node,\n suggestion: (this.editor?.extensionStorage as unknown as Record<string, MentionStorage>)?.[\n this.name\n ]?.getSuggestionFromChar(node.attrs.mentionSuggestionChar),\n }\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: () => 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 state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {\n if (node.type.name === this.name) {\n isMention = true\n tr.insertText(\n this.options.deleteTriggerWithBackspace ? '' : this.options.suggestion.char || '',\n pos,\n pos + node.nodeSize,\n )\n\n return false\n }\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 this.storage.suggestions.map(Suggestion)\n },\n\n onBeforeCreate() {\n this.storage.suggestions = (\n this.options.suggestions.length ? this.options.suggestions : [this.options.suggestion]\n ).map(suggestion => getSuggestionOptions({\n editor: this.editor,\n overrideSuggestionOptions: suggestion,\n extensionName: this.name,\n char: suggestion.char,\n }))\n\n this.storage.getSuggestionFromChar = char => {\n const suggestion = this.storage.suggestions.find(s => s.char === char)\n\n if (suggestion) {\n return suggestion\n }\n if (this.storage.suggestions.length) {\n return this.storage.suggestions[0]\n }\n\n return null\n }\n },\n})\n"],"names":["ProseMirrorNode"],"mappings":";;;;;AA6BA;;;;;;;;;AASG;AACa,SAAA,oBAAoB,CAAC,EACnC,MAAM,EAAE,YAAY,EACpB,yBAAyB,EACzB,aAAa,EACb,IAAI,GAAG,GAAG,GACkB,EAAA;AAC5B,IAAA,MAAM,SAAS,GAAG,IAAI,SAAS,EAAE;IAEjC,OAAO;AACL,QAAA,MAAM,EAAE,YAAY;QACpB,IAAI;QACJ,SAAS;QACT,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAA2C,KAAI;;;;AAG7E,YAAA,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS;AAC3D,YAAA,MAAM,aAAa,GAAG,CAAA,EAAA,GAAA,SAAS,aAAT,SAAS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E,IAAI,CAAC,MAAM;AACnB,YAAA,yBAAyB,EAAE,UAAU;YACrC,aAAa,EAAE,IAAI,CAAC,IAAI;YACxB,IAAI,EAAE,UAAU,CAAC,IAAI;AACtB,SAAA,CAAC,CAAC;AAEH,QAAA,IAAI,CAAC,OAAO,CAAC,qBAAqB,GAAG,IAAI,IAAG;YAC1C,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC;YAEtE,IAAI,UAAU,EAAE;AACd,gBAAA,OAAO,UAAU;;YAEnB,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE;gBACnC,OAAO,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC;;AAGpC,YAAA,OAAO,IAAI;AACb,SAAC;KACF;AACF,CAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/utils/get-default-suggestion-attributes.ts","../src/mention.ts"],"sourcesContent":["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 type { Editor } from '@tiptap/core'\nimport { 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 => 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\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 [\n 'span',\n mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),\n html,\n ]\n }\n return html\n },\n\n renderText({ node }) {\n const args = {\n options: this.options,\n node,\n suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar),\n }\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: () => 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 state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {\n if (node.type.name === this.name) {\n isMention = true\n tr.insertText(\n this.options.deleteTriggerWithBackspace ? '' : this.options.suggestion.char || '',\n pos,\n pos + node.nodeSize,\n )\n\n return false\n }\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"],"names":["ProseMirrorNode"],"mappings":";;;;;AA6BA;;;;;;;;;AASG;AACa,SAAA,oBAAoB,CAAC,EACnC,MAAM,EAAE,YAAY,EACpB,yBAAyB,EACzB,aAAa,EACb,IAAI,GAAG,GAAG,GACkB,EAAA;AAC5B,IAAA,MAAM,SAAS,GAAG,IAAI,SAAS,EAAE;IAEjC,OAAO;AACL,QAAA,MAAM,EAAE,YAAY;QACpB,IAAI;QACJ,SAAS;QACT,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAA2C,KAAI;;;;AAG7E,YAAA,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS;AAC3D,YAAA,MAAM,aAAa,GAAG,CAAA,EAAA,GAAA,SAAS,aAAT,SAAS,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAT,SAAS,CAAE,IAAI,MAAE,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,UAAU,CAAC,GAAG,CAAC;YAEtD,IAAI,aAAa,EAAE;AACjB,gBAAA,KAAK,CAAC,EAAE,IAAI,CAAC;;YAGf;AACG,iBAAA,KAAK;AACL,iBAAA,KAAK;iBACL,eAAe,CAAC,KAAK,EAAE;AACtB,gBAA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AA0B,GAAG,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,IAAI,EAAE,EACjF,GAAG,EACH,GAAG,GAAG,IAAI,CAAC,QAAQ,CACpB;AAED,wBAAA,OAAO,KAAK;;AAEhB,iBAAC,CAAC;;AAGF,gBAAA,IAAI,WAAW,GAAG,IAAIA,MAAe,EAAE;gBACvC,IAAI,UAAU,GAAG,CAAC;AAElB,gBAAA,KAAK,CAAC,GAAG,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,MAAM,EAAE,CAAC,IAAI,EAAE,GAAG,KAAI;oBACvD,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,EAAE;wBAChC,SAAS,GAAG,IAAI;wBAChB,WAAW,GAAG,IAAI;wBAClB,UAAU,GAAG,GAAG;AAChB,wBAAA,OAAO,KAAK;;AAEhB,iBAAC,CAAC;gBAEF,IAAI,SAAS,EAAE;AACb,oBAAA,EAAE,CAAC,UAAU,CACX,IAAI,CAAC,OAAO,CAAC,0BAA0B,GAAG,EAAE,GAAG,WAAW,CAAC,KAAK,CAAC,qBAAqB,EACtF,UAAU,EACV,UAAU,GAAG,WAAW,CAAC,QAAQ,CAClC;;AAGH,gBAAA,OAAO,SAAS;AAClB,aAAC,CAAC;SACH;KACF;IAED,qBAAqB,GAAA;;QAEnB,OAAO,cAAc,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC;KAC5C;AACF,CAAA;;;;"}
|
package/dist/index.umd.js
CHANGED
|
@@ -56,6 +56,40 @@
|
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Returns the suggestions for the mention extension.
|
|
61
|
+
*
|
|
62
|
+
* @param options The extension options
|
|
63
|
+
* @returns the suggestions
|
|
64
|
+
*/
|
|
65
|
+
function getSuggestions(options) {
|
|
66
|
+
return (options.options.suggestions.length ? options.options.suggestions : [options.options.suggestion]).map(suggestion => getSuggestionOptions({
|
|
67
|
+
// @ts-ignore `editor` can be `undefined` when converting the document to HTML with the HTML utility
|
|
68
|
+
editor: options.editor,
|
|
69
|
+
overrideSuggestionOptions: suggestion,
|
|
70
|
+
extensionName: options.name,
|
|
71
|
+
char: suggestion.char,
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Returns the suggestion options of the mention that has a given character trigger. If not
|
|
76
|
+
* found, it returns the first suggestion.
|
|
77
|
+
*
|
|
78
|
+
* @param options The extension options
|
|
79
|
+
* @param char The character that triggers the mention
|
|
80
|
+
* @returns The suggestion options
|
|
81
|
+
*/
|
|
82
|
+
function getSuggestionFromChar(options, char) {
|
|
83
|
+
const suggestions = getSuggestions(options);
|
|
84
|
+
const suggestion = suggestions.find(s => s.char === char);
|
|
85
|
+
if (suggestion) {
|
|
86
|
+
return suggestion;
|
|
87
|
+
}
|
|
88
|
+
if (suggestions.length) {
|
|
89
|
+
return suggestions[0];
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
59
93
|
/**
|
|
60
94
|
* This extension allows you to insert mentions into the editor.
|
|
61
95
|
* @see https://www.tiptap.dev/api/extensions/mention
|
|
@@ -63,26 +97,20 @@
|
|
|
63
97
|
const Mention = core.Node.create({
|
|
64
98
|
name: 'mention',
|
|
65
99
|
priority: 101,
|
|
66
|
-
addStorage() {
|
|
67
|
-
return {
|
|
68
|
-
suggestions: [],
|
|
69
|
-
getSuggestionFromChar: () => null,
|
|
70
|
-
};
|
|
71
|
-
},
|
|
72
100
|
addOptions() {
|
|
73
101
|
return {
|
|
74
102
|
HTMLAttributes: {},
|
|
75
103
|
renderText({ node, suggestion }) {
|
|
76
|
-
var _a;
|
|
77
|
-
return `${suggestion === null || suggestion === void 0 ? void 0 : suggestion.char}${(
|
|
104
|
+
var _a, _b;
|
|
105
|
+
return `${(_a = suggestion === null || suggestion === void 0 ? void 0 : suggestion.char) !== null && _a !== void 0 ? _a : '@'}${(_b = node.attrs.label) !== null && _b !== void 0 ? _b : node.attrs.id}`;
|
|
78
106
|
},
|
|
79
107
|
deleteTriggerWithBackspace: false,
|
|
80
108
|
renderHTML({ options, node, suggestion }) {
|
|
81
|
-
var _a;
|
|
109
|
+
var _a, _b;
|
|
82
110
|
return [
|
|
83
111
|
'span',
|
|
84
112
|
core.mergeAttributes(this.HTMLAttributes, options.HTMLAttributes),
|
|
85
|
-
`${suggestion === null || suggestion === void 0 ? void 0 : suggestion.char}${(
|
|
113
|
+
`${(_a = suggestion === null || suggestion === void 0 ? void 0 : suggestion.char) !== null && _a !== void 0 ? _a : '@'}${(_b = node.attrs.label) !== null && _b !== void 0 ? _b : node.attrs.id}`,
|
|
86
114
|
];
|
|
87
115
|
},
|
|
88
116
|
suggestions: [],
|
|
@@ -139,10 +167,7 @@
|
|
|
139
167
|
];
|
|
140
168
|
},
|
|
141
169
|
renderHTML({ node, HTMLAttributes }) {
|
|
142
|
-
|
|
143
|
-
// We cannot use the `this.storage` property here because, when accessed this method,
|
|
144
|
-
// it returns the initial value of the extension storage
|
|
145
|
-
const suggestion = (_c = (_b = (_a = this.editor) === null || _a === void 0 ? void 0 : _a.extensionStorage) === null || _b === void 0 ? void 0 : _b[this.name]) === null || _c === void 0 ? void 0 : _c.getSuggestionFromChar(node.attrs.mentionSuggestionChar);
|
|
170
|
+
const suggestion = getSuggestionFromChar(this, node.attrs.mentionSuggestionChar);
|
|
146
171
|
if (this.options.renderLabel !== undefined) {
|
|
147
172
|
console.warn('renderLabel is deprecated use renderText and renderHTML instead');
|
|
148
173
|
return [
|
|
@@ -172,11 +197,10 @@
|
|
|
172
197
|
return html;
|
|
173
198
|
},
|
|
174
199
|
renderText({ node }) {
|
|
175
|
-
var _a, _b, _c;
|
|
176
200
|
const args = {
|
|
177
201
|
options: this.options,
|
|
178
202
|
node,
|
|
179
|
-
suggestion: (
|
|
203
|
+
suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar),
|
|
180
204
|
};
|
|
181
205
|
if (this.options.renderLabel !== undefined) {
|
|
182
206
|
console.warn('renderLabel is deprecated use renderText and renderHTML instead');
|
|
@@ -220,25 +244,7 @@
|
|
|
220
244
|
},
|
|
221
245
|
addProseMirrorPlugins() {
|
|
222
246
|
// Create a plugin for each suggestion configuration
|
|
223
|
-
return this.
|
|
224
|
-
},
|
|
225
|
-
onBeforeCreate() {
|
|
226
|
-
this.storage.suggestions = (this.options.suggestions.length ? this.options.suggestions : [this.options.suggestion]).map(suggestion => getSuggestionOptions({
|
|
227
|
-
editor: this.editor,
|
|
228
|
-
overrideSuggestionOptions: suggestion,
|
|
229
|
-
extensionName: this.name,
|
|
230
|
-
char: suggestion.char,
|
|
231
|
-
}));
|
|
232
|
-
this.storage.getSuggestionFromChar = char => {
|
|
233
|
-
const suggestion = this.storage.suggestions.find(s => s.char === char);
|
|
234
|
-
if (suggestion) {
|
|
235
|
-
return suggestion;
|
|
236
|
-
}
|
|
237
|
-
if (this.storage.suggestions.length) {
|
|
238
|
-
return this.storage.suggestions[0];
|
|
239
|
-
}
|
|
240
|
-
return null;
|
|
241
|
-
};
|
|
247
|
+
return getSuggestions(this).map(Suggestion);
|
|
242
248
|
},
|
|
243
249
|
});
|
|
244
250
|
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/utils/get-default-suggestion-attributes.ts","../src/mention.ts"],"sourcesContent":["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 { 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\n/**\n * Storage properties or the Mention extension\n */\nexport interface MentionStorage<SuggestionItem = any, Attrs extends Record<string, any> = MentionNodeAttrs> {\n /**\n * The list of suggestions that will trigger the mention.\n */\n suggestions: Array<SuggestionOptions<SuggestionItem, Attrs>>\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 char The character that triggers the mention\n * @returns The suggestion options\n */\n getSuggestionFromChar: (char: string) => SuggestionOptions<SuggestionItem, Attrs> | 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, MentionStorage>({\n name: 'mention',\n\n priority: 101,\n\n addStorage() {\n return {\n suggestions: [],\n getSuggestionFromChar: () => null,\n }\n },\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 // We cannot use the `this.storage` property here because, when accessed this method,\n // it returns the initial value of the extension storage\n const suggestion = (this.editor?.extensionStorage as unknown as Record<string, MentionStorage>)?.[\n this.name\n ]?.getSuggestionFromChar(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 [\n 'span',\n mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),\n html,\n ]\n }\n return html\n },\n\n renderText({ node }) {\n const args = {\n options: this.options,\n node,\n suggestion: (this.editor?.extensionStorage as unknown as Record<string, MentionStorage>)?.[\n this.name\n ]?.getSuggestionFromChar(node.attrs.mentionSuggestionChar),\n }\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: () => 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 state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {\n if (node.type.name === this.name) {\n isMention = true\n tr.insertText(\n this.options.deleteTriggerWithBackspace ? '' : this.options.suggestion.char || '',\n pos,\n pos + node.nodeSize,\n )\n\n return false\n }\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 this.storage.suggestions.map(Suggestion)\n },\n\n onBeforeCreate() {\n this.storage.suggestions = (\n this.options.suggestions.length ? this.options.suggestions : [this.options.suggestion]\n ).map(suggestion => getSuggestionOptions({\n editor: this.editor,\n overrideSuggestionOptions: suggestion,\n extensionName: this.name,\n char: suggestion.char,\n }))\n\n this.storage.getSuggestionFromChar = char => {\n const suggestion = this.storage.suggestions.find(s => s.char === char)\n\n if (suggestion) {\n return suggestion\n }\n if (this.storage.suggestions.length) {\n return this.storage.suggestions[0]\n }\n\n return null\n }\n },\n})\n"],"names":["PluginKey","Node","mergeAttributes","ProseMirrorNode"],"mappings":";;;;;;EA6BA;;;;;;;;;EASG;EACa,SAAA,oBAAoB,CAAC,EACnC,MAAM,EAAE,YAAY,EACpB,yBAAyB,EACzB,aAAa,EACb,IAAI,GAAG,GAAG,GACkB,EAAA;EAC5B,IAAA,MAAM,SAAS,GAAG,IAAIA,eAAS,EAAE;MAEjC,OAAO;EACL,QAAA,MAAM,EAAE,YAAY;UACpB,IAAI;UACJ,SAAS;UACT,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAA2C,KAAI;;;;EAG7E,Y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AC,WAAW,CAAC,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,EACtF,GAAG,CAAC,UAAU,IAAI,oBAAoB,CAAC;cACvC,MAAM,EAAE,IAAI,CAAC,MAAM;EACnB,YAAA,yBAAyB,EAAE,UAAU;cACrC,aAAa,EAAE,IAAI,CAAC,IAAI;cACxB,IAAI,EAAE,UAAU,CAAC,IAAI;EACtB,SAAA,CAAC,CAAC;EAEH,QAAA,IAAI,CAAC,OAAO,CAAC,qBAAqB,GAAG,IAAI,IAAG;cAC1C,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC;cAEtE,IAAI,UAAU,EAAE;EACd,gBAAA,OAAO,UAAU;;cAEnB,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE;kBACnC,OAAO,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC;;EAGpC,YAAA,OAAO,IAAI;EACb,SAAC;OACF;EACF,CAAA;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/utils/get-default-suggestion-attributes.ts","../src/mention.ts"],"sourcesContent":["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 type { Editor } from '@tiptap/core'\nimport { 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 => 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\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 [\n 'span',\n mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),\n html,\n ]\n }\n return html\n },\n\n renderText({ node }) {\n const args = {\n options: this.options,\n node,\n suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar),\n }\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: () => 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 state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {\n if (node.type.name === this.name) {\n isMention = true\n tr.insertText(\n this.options.deleteTriggerWithBackspace ? '' : this.options.suggestion.char || '',\n pos,\n pos + node.nodeSize,\n )\n\n return false\n }\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"],"names":["PluginKey","Node","mergeAttributes","ProseMirrorNode"],"mappings":";;;;;;EA6BA;;;;;;;;;EASG;EACa,SAAA,oBAAoB,CAAC,EACnC,MAAM,EAAE,YAAY,EACpB,yBAAyB,EACzB,aAAa,EACb,IAAI,GAAG,GAAG,GACkB,EAAA;EAC5B,IAAA,MAAM,SAAS,GAAG,IAAIA,eAAS,EAAE;MAEjC,OAAO;EACL,QAAA,MAAM,EAAE,YAAY;UACpB,IAAI;UACJ,SAAS;UACT,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAA2C,KAAI;;;;EAG7E,YAAA,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS;EAC3D,YAAA,MAAM,aAAa,GAAG,CAAA,EAAA,GAAA,SAAS,aAAT,SAAS,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAT,SAAS,CAAE,IAAI,MAAE,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,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s extends Record<strin
|
|
|
84
84
|
*/
|
|
85
85
|
suggestion: Omit<SuggestionOptions<SuggestionItem, Attrs>, 'editor'>;
|
|
86
86
|
}
|
|
87
|
-
/**
|
|
88
|
-
* Storage properties or the Mention extension
|
|
89
|
-
*/
|
|
90
|
-
export interface MentionStorage<SuggestionItem = any, Attrs extends Record<string, any> = MentionNodeAttrs> {
|
|
91
|
-
/**
|
|
92
|
-
* The list of suggestions that will trigger the mention.
|
|
93
|
-
*/
|
|
94
|
-
suggestions: Array<SuggestionOptions<SuggestionItem, Attrs>>;
|
|
95
|
-
/**
|
|
96
|
-
* Returns the suggestion options of the mention that has a given character trigger. If not
|
|
97
|
-
* found, it returns the first suggestion.
|
|
98
|
-
*
|
|
99
|
-
* @param char The character that triggers the mention
|
|
100
|
-
* @returns The suggestion options
|
|
101
|
-
*/
|
|
102
|
-
getSuggestionFromChar: (char: string) => SuggestionOptions<SuggestionItem, Attrs> | null;
|
|
103
|
-
}
|
|
104
87
|
/**
|
|
105
88
|
* This extension allows you to insert mentions into the editor.
|
|
106
89
|
* @see https://www.tiptap.dev/api/extensions/mention
|
|
107
90
|
*/
|
|
108
|
-
export declare const Mention: Node<MentionOptions<any, MentionNodeAttrs>,
|
|
91
|
+
export declare const Mention: Node<MentionOptions<any, MentionNodeAttrs>, any>;
|
|
109
92
|
//# sourceMappingURL=mention.d.ts.map
|
package/dist/mention.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mention.d.ts","sourceRoot":"","sources":["../src/mention.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mention.d.ts","sourceRoot":"","sources":["../src/mention.ts"],"names":[],"mappings":"AACA,OAAO,EAAmB,IAAI,EAAE,MAAM,cAAc,CAAA;AACpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AACrD,OAAO,EAAE,IAAI,IAAI,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAC1D,OAAO,KAA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EAAE,QAAQ,CAAC,CAAC,CAAA;IAE5E;;;;;;OAMG;IACH,UAAU,EAAE,IAAI,CAAC,iBAAiB,CAAC,cAAc,EAAE,KAAK,CAAC,EAAE,QAAQ,CAAC,CAAA;CACrE;AAkDD;;;GAGG;AACH,eAAO,MAAM,OAAO,kDA+LlB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiptap/extension-mention",
|
|
3
3
|
"description": "mention extension for tiptap",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.24.0",
|
|
5
5
|
"homepage": "https://tiptap.dev",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@tiptap/core": "^2.
|
|
33
|
-
"@tiptap/pm": "^2.
|
|
34
|
-
"@tiptap/suggestion": "^2.
|
|
32
|
+
"@tiptap/core": "^2.24.0",
|
|
33
|
+
"@tiptap/pm": "^2.24.0",
|
|
34
|
+
"@tiptap/suggestion": "^2.24.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"@tiptap/core": "^2.7.0",
|
package/src/mention.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Editor } from '@tiptap/core'
|
|
1
2
|
import { mergeAttributes, Node } from '@tiptap/core'
|
|
2
3
|
import type { DOMOutputSpec } from '@tiptap/pm/model'
|
|
3
4
|
import { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
|
@@ -97,53 +98,75 @@ export interface MentionOptions<SuggestionItem = any, Attrs extends Record<strin
|
|
|
97
98
|
suggestion: Omit<SuggestionOptions<SuggestionItem, Attrs>, 'editor'>
|
|
98
99
|
}
|
|
99
100
|
|
|
101
|
+
interface GetSuggestionsOptions {
|
|
102
|
+
editor?: Editor
|
|
103
|
+
options: MentionOptions
|
|
104
|
+
name: string
|
|
105
|
+
}
|
|
106
|
+
|
|
100
107
|
/**
|
|
101
|
-
*
|
|
108
|
+
* Returns the suggestions for the mention extension.
|
|
109
|
+
*
|
|
110
|
+
* @param options The extension options
|
|
111
|
+
* @returns the suggestions
|
|
102
112
|
*/
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
113
|
+
function getSuggestions(options: GetSuggestionsOptions) {
|
|
114
|
+
return (options.options.suggestions.length ? options.options.suggestions : [options.options.suggestion]).map(
|
|
115
|
+
suggestion => getSuggestionOptions({
|
|
116
|
+
// @ts-ignore `editor` can be `undefined` when converting the document to HTML with the HTML utility
|
|
117
|
+
editor: options.editor,
|
|
118
|
+
overrideSuggestionOptions: suggestion,
|
|
119
|
+
extensionName: options.name,
|
|
120
|
+
char: suggestion.char,
|
|
121
|
+
}),
|
|
122
|
+
)
|
|
123
|
+
}
|
|
108
124
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Returns the suggestion options of the mention that has a given character trigger. If not
|
|
127
|
+
* found, it returns the first suggestion.
|
|
128
|
+
*
|
|
129
|
+
* @param options The extension options
|
|
130
|
+
* @param char The character that triggers the mention
|
|
131
|
+
* @returns The suggestion options
|
|
132
|
+
*/
|
|
133
|
+
function getSuggestionFromChar(options: GetSuggestionsOptions, char: string) {
|
|
134
|
+
const suggestions = getSuggestions(options)
|
|
135
|
+
|
|
136
|
+
const suggestion = suggestions.find(s => s.char === char)
|
|
137
|
+
|
|
138
|
+
if (suggestion) {
|
|
139
|
+
return suggestion
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (suggestions.length) {
|
|
143
|
+
return suggestions[0]
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return null
|
|
117
147
|
}
|
|
118
148
|
|
|
119
149
|
/**
|
|
120
150
|
* This extension allows you to insert mentions into the editor.
|
|
121
151
|
* @see https://www.tiptap.dev/api/extensions/mention
|
|
122
152
|
*/
|
|
123
|
-
export const Mention = Node.create<MentionOptions
|
|
153
|
+
export const Mention = Node.create<MentionOptions>({
|
|
124
154
|
name: 'mention',
|
|
125
155
|
|
|
126
156
|
priority: 101,
|
|
127
157
|
|
|
128
|
-
addStorage() {
|
|
129
|
-
return {
|
|
130
|
-
suggestions: [],
|
|
131
|
-
getSuggestionFromChar: () => null,
|
|
132
|
-
}
|
|
133
|
-
},
|
|
134
|
-
|
|
135
158
|
addOptions() {
|
|
136
159
|
return {
|
|
137
160
|
HTMLAttributes: {},
|
|
138
161
|
renderText({ node, suggestion }) {
|
|
139
|
-
return `${suggestion?.char}${node.attrs.label ?? node.attrs.id}`
|
|
162
|
+
return `${suggestion?.char ?? '@'}${node.attrs.label ?? node.attrs.id}`
|
|
140
163
|
},
|
|
141
164
|
deleteTriggerWithBackspace: false,
|
|
142
165
|
renderHTML({ options, node, suggestion }) {
|
|
143
166
|
return [
|
|
144
167
|
'span',
|
|
145
168
|
mergeAttributes(this.HTMLAttributes, options.HTMLAttributes),
|
|
146
|
-
`${suggestion?.char}${node.attrs.label ?? node.attrs.id}`,
|
|
169
|
+
`${suggestion?.char ?? '@'}${node.attrs.label ?? node.attrs.id}`,
|
|
147
170
|
]
|
|
148
171
|
},
|
|
149
172
|
suggestions: [],
|
|
@@ -211,11 +234,7 @@ export const Mention = Node.create<MentionOptions, MentionStorage>({
|
|
|
211
234
|
},
|
|
212
235
|
|
|
213
236
|
renderHTML({ node, HTMLAttributes }) {
|
|
214
|
-
|
|
215
|
-
// it returns the initial value of the extension storage
|
|
216
|
-
const suggestion = (this.editor?.extensionStorage as unknown as Record<string, MentionStorage>)?.[
|
|
217
|
-
this.name
|
|
218
|
-
]?.getSuggestionFromChar(node.attrs.mentionSuggestionChar)
|
|
237
|
+
const suggestion = getSuggestionFromChar(this, node.attrs.mentionSuggestionChar)
|
|
219
238
|
|
|
220
239
|
if (this.options.renderLabel !== undefined) {
|
|
221
240
|
console.warn('renderLabel is deprecated use renderText and renderHTML instead')
|
|
@@ -257,9 +276,7 @@ export const Mention = Node.create<MentionOptions, MentionStorage>({
|
|
|
257
276
|
const args = {
|
|
258
277
|
options: this.options,
|
|
259
278
|
node,
|
|
260
|
-
suggestion: (this
|
|
261
|
-
this.name
|
|
262
|
-
]?.getSuggestionFromChar(node.attrs.mentionSuggestionChar),
|
|
279
|
+
suggestion: getSuggestionFromChar(this, node.attrs.mentionSuggestionChar),
|
|
263
280
|
}
|
|
264
281
|
|
|
265
282
|
if (this.options.renderLabel !== undefined) {
|
|
@@ -322,30 +339,6 @@ export const Mention = Node.create<MentionOptions, MentionStorage>({
|
|
|
322
339
|
|
|
323
340
|
addProseMirrorPlugins() {
|
|
324
341
|
// Create a plugin for each suggestion configuration
|
|
325
|
-
return this.
|
|
326
|
-
},
|
|
327
|
-
|
|
328
|
-
onBeforeCreate() {
|
|
329
|
-
this.storage.suggestions = (
|
|
330
|
-
this.options.suggestions.length ? this.options.suggestions : [this.options.suggestion]
|
|
331
|
-
).map(suggestion => getSuggestionOptions({
|
|
332
|
-
editor: this.editor,
|
|
333
|
-
overrideSuggestionOptions: suggestion,
|
|
334
|
-
extensionName: this.name,
|
|
335
|
-
char: suggestion.char,
|
|
336
|
-
}))
|
|
337
|
-
|
|
338
|
-
this.storage.getSuggestionFromChar = char => {
|
|
339
|
-
const suggestion = this.storage.suggestions.find(s => s.char === char)
|
|
340
|
-
|
|
341
|
-
if (suggestion) {
|
|
342
|
-
return suggestion
|
|
343
|
-
}
|
|
344
|
-
if (this.storage.suggestions.length) {
|
|
345
|
-
return this.storage.suggestions[0]
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
return null
|
|
349
|
-
}
|
|
342
|
+
return getSuggestions(this).map(Suggestion)
|
|
350
343
|
},
|
|
351
344
|
})
|