@tiptap/suggestion 2.11.7 → 3.0.0-beta.1

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025, Tiptap GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,14 +1,18 @@
1
1
  # @tiptap/suggestion
2
+
2
3
  [![Version](https://img.shields.io/npm/v/@tiptap/suggestion.svg?label=version)](https://www.npmjs.com/package/@tiptap/suggestion)
3
4
  [![Downloads](https://img.shields.io/npm/dm/@tiptap/suggestion.svg)](https://npmcharts.com/compare/tiptap?minimal=true)
4
5
  [![License](https://img.shields.io/npm/l/@tiptap/suggestion.svg)](https://www.npmjs.com/package/@tiptap/suggestion)
5
6
  [![Sponsor](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub)](https://github.com/sponsors/ueberdosis)
6
7
 
7
8
  ## Introduction
8
- Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as *New York Times*, *The Guardian* or *Atlassian*.
9
+
10
+ Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as _New York Times_, _The Guardian_ or _Atlassian_.
9
11
 
10
12
  ## Official Documentation
13
+
11
14
  Documentation can be found on the [Tiptap website](https://tiptap.dev).
12
15
 
13
16
  ## License
17
+
14
18
  Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
package/dist/index.cjs CHANGED
@@ -1,247 +1,271 @@
1
- 'use strict';
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
2
19
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Suggestion: () => Suggestion,
24
+ SuggestionPluginKey: () => SuggestionPluginKey,
25
+ default: () => index_default,
26
+ findSuggestionMatch: () => findSuggestionMatch
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
4
29
 
5
- var state = require('@tiptap/pm/state');
6
- var view = require('@tiptap/pm/view');
7
- var core = require('@tiptap/core');
30
+ // src/suggestion.ts
31
+ var import_state = require("@tiptap/pm/state");
32
+ var import_view = require("@tiptap/pm/view");
8
33
 
34
+ // src/findSuggestionMatch.ts
35
+ var import_core = require("@tiptap/core");
9
36
  function findSuggestionMatch(config) {
10
- var _a;
11
- const { char, allowSpaces: allowSpacesOption, allowToIncludeChar, allowedPrefixes, startOfLine, $position, } = config;
12
- const allowSpaces = allowSpacesOption && !allowToIncludeChar;
13
- const escapedChar = core.escapeForRegEx(char);
14
- const suffix = new RegExp(`\\s${escapedChar}$`);
15
- const prefix = startOfLine ? '^' : '';
16
- const finalEscapedChar = allowToIncludeChar ? '' : escapedChar;
17
- const regexp = allowSpaces
18
- ? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${finalEscapedChar}|$)`, 'gm')
19
- : new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${finalEscapedChar}]*`, 'gm');
20
- const text = ((_a = $position.nodeBefore) === null || _a === void 0 ? void 0 : _a.isText) && $position.nodeBefore.text;
21
- if (!text) {
22
- return null;
23
- }
24
- const textFrom = $position.pos - text.length;
25
- const match = Array.from(text.matchAll(regexp)).pop();
26
- if (!match || match.input === undefined || match.index === undefined) {
27
- return null;
28
- }
29
- // JavaScript doesn't have lookbehinds. This hacks a check that first character
30
- // is a space or the start of the line
31
- const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index);
32
- const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes === null || allowedPrefixes === void 0 ? void 0 : allowedPrefixes.join('')}\0]?$`).test(matchPrefix);
33
- if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
34
- return null;
35
- }
36
- // The absolute position of the match in the document
37
- const from = textFrom + match.index;
38
- let to = from + match[0].length;
39
- // Edge case handling; if spaces are allowed and we're directly in between
40
- // two triggers
41
- if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {
42
- match[0] += ' ';
43
- to += 1;
44
- }
45
- // If the $position is located within the matched substring, return that range
46
- if (from < $position.pos && to >= $position.pos) {
47
- return {
48
- range: {
49
- from,
50
- to,
51
- },
52
- query: match[0].slice(char.length),
53
- text: match[0],
54
- };
55
- }
37
+ var _a;
38
+ const { char, allowSpaces: allowSpacesOption, allowToIncludeChar, allowedPrefixes, startOfLine, $position } = config;
39
+ const allowSpaces = allowSpacesOption && !allowToIncludeChar;
40
+ const escapedChar = (0, import_core.escapeForRegEx)(char);
41
+ const suffix = new RegExp(`\\s${escapedChar}$`);
42
+ const prefix = startOfLine ? "^" : "";
43
+ const finalEscapedChar = allowToIncludeChar ? "" : escapedChar;
44
+ const regexp = allowSpaces ? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${finalEscapedChar}|$)`, "gm") : new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${finalEscapedChar}]*`, "gm");
45
+ const text = ((_a = $position.nodeBefore) == null ? void 0 : _a.isText) && $position.nodeBefore.text;
46
+ if (!text) {
47
+ return null;
48
+ }
49
+ const textFrom = $position.pos - text.length;
50
+ const match = Array.from(text.matchAll(regexp)).pop();
51
+ if (!match || match.input === void 0 || match.index === void 0) {
56
52
  return null;
53
+ }
54
+ const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index);
55
+ const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes == null ? void 0 : allowedPrefixes.join("")}\0]?$`).test(matchPrefix);
56
+ if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
57
+ return null;
58
+ }
59
+ const from = textFrom + match.index;
60
+ let to = from + match[0].length;
61
+ if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {
62
+ match[0] += " ";
63
+ to += 1;
64
+ }
65
+ if (from < $position.pos && to >= $position.pos) {
66
+ return {
67
+ range: {
68
+ from,
69
+ to
70
+ },
71
+ query: match[0].slice(char.length),
72
+ text: match[0]
73
+ };
74
+ }
75
+ return null;
57
76
  }
58
77
 
59
- const SuggestionPluginKey = new state.PluginKey('suggestion');
60
- /**
61
- * This utility allows you to create suggestions.
62
- * @see https://tiptap.dev/api/utilities/suggestion
63
- */
64
- function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allowSpaces = false, allowToIncludeChar = false, allowedPrefixes = [' '], startOfLine = false, decorationTag = 'span', decorationClass = 'suggestion', command = () => null, items = () => [], render = () => ({}), allow = () => true, findSuggestionMatch: findSuggestionMatch$1 = findSuggestionMatch, }) {
65
- let props;
66
- const renderer = render === null || render === void 0 ? void 0 : render();
67
- const plugin = new state.Plugin({
68
- key: pluginKey,
69
- view() {
70
- return {
71
- update: async (view, prevState) => {
72
- var _a, _b, _c, _d, _e, _f, _g;
73
- const prev = (_a = this.key) === null || _a === void 0 ? void 0 : _a.getState(prevState);
74
- const next = (_b = this.key) === null || _b === void 0 ? void 0 : _b.getState(view.state);
75
- // See how the state changed
76
- const moved = prev.active && next.active && prev.range.from !== next.range.from;
77
- const started = !prev.active && next.active;
78
- const stopped = prev.active && !next.active;
79
- const changed = !started && !stopped && prev.query !== next.query;
80
- const handleStart = started || (moved && changed);
81
- const handleChange = changed || moved;
82
- const handleExit = stopped || (moved && changed);
83
- // Cancel when suggestion isn't active
84
- if (!handleStart && !handleChange && !handleExit) {
85
- return;
86
- }
87
- const state = handleExit && !handleStart ? prev : next;
88
- const decorationNode = view.dom.querySelector(`[data-decoration-id="${state.decorationId}"]`);
89
- props = {
90
- editor,
91
- range: state.range,
92
- query: state.query,
93
- text: state.text,
94
- items: [],
95
- command: commandProps => {
96
- return command({
97
- editor,
98
- range: state.range,
99
- props: commandProps,
100
- });
101
- },
102
- decorationNode,
103
- // virtual node for popper.js or tippy.js
104
- // this can be used for building popups without a DOM node
105
- clientRect: decorationNode
106
- ? () => {
107
- var _a;
108
- // because of `items` can be asynchrounous we’ll search for the current decoration node
109
- const { decorationId } = (_a = this.key) === null || _a === void 0 ? void 0 : _a.getState(editor.state); // eslint-disable-line
110
- const currentDecorationNode = view.dom.querySelector(`[data-decoration-id="${decorationId}"]`);
111
- return (currentDecorationNode === null || currentDecorationNode === void 0 ? void 0 : currentDecorationNode.getBoundingClientRect()) || null;
112
- }
113
- : null,
114
- };
115
- if (handleStart) {
116
- (_c = renderer === null || renderer === void 0 ? void 0 : renderer.onBeforeStart) === null || _c === void 0 ? void 0 : _c.call(renderer, props);
117
- }
118
- if (handleChange) {
119
- (_d = renderer === null || renderer === void 0 ? void 0 : renderer.onBeforeUpdate) === null || _d === void 0 ? void 0 : _d.call(renderer, props);
120
- }
121
- if (handleChange || handleStart) {
122
- props.items = await items({
123
- editor,
124
- query: state.query,
125
- });
126
- }
127
- if (handleExit) {
128
- (_e = renderer === null || renderer === void 0 ? void 0 : renderer.onExit) === null || _e === void 0 ? void 0 : _e.call(renderer, props);
129
- }
130
- if (handleChange) {
131
- (_f = renderer === null || renderer === void 0 ? void 0 : renderer.onUpdate) === null || _f === void 0 ? void 0 : _f.call(renderer, props);
132
- }
133
- if (handleStart) {
134
- (_g = renderer === null || renderer === void 0 ? void 0 : renderer.onStart) === null || _g === void 0 ? void 0 : _g.call(renderer, props);
135
- }
136
- },
137
- destroy: () => {
138
- var _a;
139
- if (!props) {
140
- return;
141
- }
142
- (_a = renderer === null || renderer === void 0 ? void 0 : renderer.onExit) === null || _a === void 0 ? void 0 : _a.call(renderer, props);
143
- },
144
- };
145
- },
146
- state: {
147
- // Initialize the plugin's internal state.
148
- init() {
149
- const state = {
150
- active: false,
151
- range: {
152
- from: 0,
153
- to: 0,
154
- },
155
- query: null,
156
- text: null,
157
- composing: false,
158
- };
159
- return state;
160
- },
161
- // Apply changes to the plugin state from a view transaction.
162
- apply(transaction, prev, _oldState, state) {
163
- const { isEditable } = editor;
164
- const { composing } = editor.view;
165
- const { selection } = transaction;
166
- const { empty, from } = selection;
167
- const next = { ...prev };
168
- next.composing = composing;
169
- // We can only be suggesting if the view is editable, and:
170
- // * there is no selection, or
171
- // * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)
172
- if (isEditable && (empty || editor.view.composing)) {
173
- // Reset active state if we just left the previous suggestion range
174
- if ((from < prev.range.from || from > prev.range.to) && !composing && !prev.composing) {
175
- next.active = false;
176
- }
177
- // Try to match against where our cursor currently is
178
- const match = findSuggestionMatch$1({
179
- char,
180
- allowSpaces,
181
- allowToIncludeChar,
182
- allowedPrefixes,
183
- startOfLine,
184
- $position: selection.$from,
185
- });
186
- const decorationId = `id_${Math.floor(Math.random() * 0xffffffff)}`;
187
- // If we found a match, update the current state to show it
188
- if (match && allow({
189
- editor, state, range: match.range, isActive: prev.active,
190
- })) {
191
- next.active = true;
192
- next.decorationId = prev.decorationId ? prev.decorationId : decorationId;
193
- next.range = match.range;
194
- next.query = match.query;
195
- next.text = match.text;
196
- }
197
- else {
198
- next.active = false;
199
- }
200
- }
201
- else {
202
- next.active = false;
203
- }
204
- // Make sure to empty the range if suggestion is inactive
205
- if (!next.active) {
206
- next.decorationId = null;
207
- next.range = { from: 0, to: 0 };
208
- next.query = null;
209
- next.text = null;
210
- }
211
- return next;
212
- },
213
- },
214
- props: {
215
- // Call the keydown hook if suggestion is active.
216
- handleKeyDown(view, event) {
217
- var _a;
218
- const { active, range } = plugin.getState(view.state);
219
- if (!active) {
220
- return false;
221
- }
222
- return ((_a = renderer === null || renderer === void 0 ? void 0 : renderer.onKeyDown) === null || _a === void 0 ? void 0 : _a.call(renderer, { view, event, range })) || false;
223
- },
224
- // Setup decorator on the currently active suggestion.
225
- decorations(state) {
226
- const { active, range, decorationId } = plugin.getState(state);
227
- if (!active) {
228
- return null;
229
- }
230
- return view.DecorationSet.create(state.doc, [
231
- view.Decoration.inline(range.from, range.to, {
232
- nodeName: decorationTag,
233
- class: decorationClass,
234
- 'data-decoration-id': decorationId,
235
- }),
236
- ]);
78
+ // src/suggestion.ts
79
+ var SuggestionPluginKey = new import_state.PluginKey("suggestion");
80
+ function Suggestion({
81
+ pluginKey = SuggestionPluginKey,
82
+ editor,
83
+ char = "@",
84
+ allowSpaces = false,
85
+ allowToIncludeChar = false,
86
+ allowedPrefixes = [" "],
87
+ startOfLine = false,
88
+ decorationTag = "span",
89
+ decorationClass = "suggestion",
90
+ command = () => null,
91
+ items = () => [],
92
+ render = () => ({}),
93
+ allow = () => true,
94
+ findSuggestionMatch: findSuggestionMatch2 = findSuggestionMatch
95
+ }) {
96
+ let props;
97
+ const renderer = render == null ? void 0 : render();
98
+ const plugin = new import_state.Plugin({
99
+ key: pluginKey,
100
+ view() {
101
+ return {
102
+ update: async (view, prevState) => {
103
+ var _a, _b, _c, _d, _e, _f, _g;
104
+ const prev = (_a = this.key) == null ? void 0 : _a.getState(prevState);
105
+ const next = (_b = this.key) == null ? void 0 : _b.getState(view.state);
106
+ const moved = prev.active && next.active && prev.range.from !== next.range.from;
107
+ const started = !prev.active && next.active;
108
+ const stopped = prev.active && !next.active;
109
+ const changed = !started && !stopped && prev.query !== next.query;
110
+ const handleStart = started || moved && changed;
111
+ const handleChange = changed || moved;
112
+ const handleExit = stopped || moved && changed;
113
+ if (!handleStart && !handleChange && !handleExit) {
114
+ return;
115
+ }
116
+ const state = handleExit && !handleStart ? prev : next;
117
+ const decorationNode = view.dom.querySelector(`[data-decoration-id="${state.decorationId}"]`);
118
+ props = {
119
+ editor,
120
+ range: state.range,
121
+ query: state.query,
122
+ text: state.text,
123
+ items: [],
124
+ command: (commandProps) => {
125
+ return command({
126
+ editor,
127
+ range: state.range,
128
+ props: commandProps
129
+ });
237
130
  },
131
+ decorationNode,
132
+ // virtual node for positioning
133
+ // this can be used for building popups without a DOM node
134
+ clientRect: decorationNode ? () => {
135
+ var _a2;
136
+ const { decorationId } = (_a2 = this.key) == null ? void 0 : _a2.getState(editor.state);
137
+ const currentDecorationNode = view.dom.querySelector(`[data-decoration-id="${decorationId}"]`);
138
+ return (currentDecorationNode == null ? void 0 : currentDecorationNode.getBoundingClientRect()) || null;
139
+ } : null
140
+ };
141
+ if (handleStart) {
142
+ (_c = renderer == null ? void 0 : renderer.onBeforeStart) == null ? void 0 : _c.call(renderer, props);
143
+ }
144
+ if (handleChange) {
145
+ (_d = renderer == null ? void 0 : renderer.onBeforeUpdate) == null ? void 0 : _d.call(renderer, props);
146
+ }
147
+ if (handleChange || handleStart) {
148
+ props.items = await items({
149
+ editor,
150
+ query: state.query
151
+ });
152
+ }
153
+ if (handleExit) {
154
+ (_e = renderer == null ? void 0 : renderer.onExit) == null ? void 0 : _e.call(renderer, props);
155
+ }
156
+ if (handleChange) {
157
+ (_f = renderer == null ? void 0 : renderer.onUpdate) == null ? void 0 : _f.call(renderer, props);
158
+ }
159
+ if (handleStart) {
160
+ (_g = renderer == null ? void 0 : renderer.onStart) == null ? void 0 : _g.call(renderer, props);
161
+ }
238
162
  },
239
- });
240
- return plugin;
163
+ destroy: () => {
164
+ var _a;
165
+ if (!props) {
166
+ return;
167
+ }
168
+ (_a = renderer == null ? void 0 : renderer.onExit) == null ? void 0 : _a.call(renderer, props);
169
+ }
170
+ };
171
+ },
172
+ state: {
173
+ // Initialize the plugin's internal state.
174
+ init() {
175
+ const state = {
176
+ active: false,
177
+ range: {
178
+ from: 0,
179
+ to: 0
180
+ },
181
+ query: null,
182
+ text: null,
183
+ composing: false
184
+ };
185
+ return state;
186
+ },
187
+ // Apply changes to the plugin state from a view transaction.
188
+ apply(transaction, prev, _oldState, state) {
189
+ const { isEditable } = editor;
190
+ const { composing } = editor.view;
191
+ const { selection } = transaction;
192
+ const { empty, from } = selection;
193
+ const next = { ...prev };
194
+ next.composing = composing;
195
+ if (isEditable && (empty || editor.view.composing)) {
196
+ if ((from < prev.range.from || from > prev.range.to) && !composing && !prev.composing) {
197
+ next.active = false;
198
+ }
199
+ const match = findSuggestionMatch2({
200
+ char,
201
+ allowSpaces,
202
+ allowToIncludeChar,
203
+ allowedPrefixes,
204
+ startOfLine,
205
+ $position: selection.$from
206
+ });
207
+ const decorationId = `id_${Math.floor(Math.random() * 4294967295)}`;
208
+ if (match && allow({
209
+ editor,
210
+ state,
211
+ range: match.range,
212
+ isActive: prev.active
213
+ })) {
214
+ next.active = true;
215
+ next.decorationId = prev.decorationId ? prev.decorationId : decorationId;
216
+ next.range = match.range;
217
+ next.query = match.query;
218
+ next.text = match.text;
219
+ } else {
220
+ next.active = false;
221
+ }
222
+ } else {
223
+ next.active = false;
224
+ }
225
+ if (!next.active) {
226
+ next.decorationId = null;
227
+ next.range = { from: 0, to: 0 };
228
+ next.query = null;
229
+ next.text = null;
230
+ }
231
+ return next;
232
+ }
233
+ },
234
+ props: {
235
+ // Call the keydown hook if suggestion is active.
236
+ handleKeyDown(view, event) {
237
+ var _a;
238
+ const { active, range } = plugin.getState(view.state);
239
+ if (!active) {
240
+ return false;
241
+ }
242
+ return ((_a = renderer == null ? void 0 : renderer.onKeyDown) == null ? void 0 : _a.call(renderer, { view, event, range })) || false;
243
+ },
244
+ // Setup decorator on the currently active suggestion.
245
+ decorations(state) {
246
+ const { active, range, decorationId } = plugin.getState(state);
247
+ if (!active) {
248
+ return null;
249
+ }
250
+ return import_view.DecorationSet.create(state.doc, [
251
+ import_view.Decoration.inline(range.from, range.to, {
252
+ nodeName: decorationTag,
253
+ class: decorationClass,
254
+ "data-decoration-id": decorationId
255
+ })
256
+ ]);
257
+ }
258
+ }
259
+ });
260
+ return plugin;
241
261
  }
242
262
 
243
- exports.Suggestion = Suggestion;
244
- exports.SuggestionPluginKey = SuggestionPluginKey;
245
- exports.default = Suggestion;
246
- exports.findSuggestionMatch = findSuggestionMatch;
247
- //# sourceMappingURL=index.cjs.map
263
+ // src/index.ts
264
+ var index_default = Suggestion;
265
+ // Annotate the CommonJS export names for ESM import in node:
266
+ 0 && (module.exports = {
267
+ Suggestion,
268
+ SuggestionPluginKey,
269
+ findSuggestionMatch
270
+ });
271
+ //# sourceMappingURL=index.cjs.map