@tiptap/extensions 3.30.1 → 3.30.3

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.
Files changed (73) hide show
  1. package/dist/character-count/index.cjs +84 -128
  2. package/dist/character-count/index.cjs.map +1 -1
  3. package/dist/character-count/index.d.cts +59 -58
  4. package/dist/character-count/index.d.cts.map +1 -0
  5. package/dist/character-count/index.d.ts +59 -58
  6. package/dist/character-count/index.d.ts.map +1 -0
  7. package/dist/character-count/index.js +82 -100
  8. package/dist/character-count/index.js.map +1 -1
  9. package/dist/drop-cursor/index.cjs +24 -44
  10. package/dist/drop-cursor/index.cjs.map +1 -1
  11. package/dist/drop-cursor/index.d.cts +23 -22
  12. package/dist/drop-cursor/index.d.cts.map +1 -0
  13. package/dist/drop-cursor/index.d.ts +23 -22
  14. package/dist/drop-cursor/index.d.ts.map +1 -0
  15. package/dist/drop-cursor/index.js +22 -16
  16. package/dist/drop-cursor/index.js.map +1 -1
  17. package/dist/focus/index.cjs +45 -92
  18. package/dist/focus/index.cjs.map +1 -1
  19. package/dist/focus/index.d.cts +22 -21
  20. package/dist/focus/index.d.cts.map +1 -0
  21. package/dist/focus/index.d.ts +22 -21
  22. package/dist/focus/index.d.ts.map +1 -0
  23. package/dist/focus/index.js +42 -63
  24. package/dist/focus/index.js.map +1 -1
  25. package/dist/gap-cursor/index.cjs +25 -48
  26. package/dist/gap-cursor/index.cjs.map +1 -1
  27. package/dist/gap-cursor/index.d.cts +16 -15
  28. package/dist/gap-cursor/index.d.cts.map +1 -0
  29. package/dist/gap-cursor/index.d.ts +16 -15
  30. package/dist/gap-cursor/index.d.ts.map +1 -0
  31. package/dist/gap-cursor/index.js +24 -21
  32. package/dist/gap-cursor/index.js.map +1 -1
  33. package/dist/index.cjs +666 -742
  34. package/dist/index.cjs.map +1 -1
  35. package/dist/index.d.cts +231 -220
  36. package/dist/index.d.cts.map +1 -0
  37. package/dist/index.d.ts +231 -220
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +653 -703
  40. package/dist/index.js.map +1 -1
  41. package/dist/placeholder/index.cjs +344 -376
  42. package/dist/placeholder/index.cjs.map +1 -1
  43. package/dist/placeholder/index.d.cts +73 -69
  44. package/dist/placeholder/index.d.cts.map +1 -0
  45. package/dist/placeholder/index.d.ts +73 -69
  46. package/dist/placeholder/index.d.ts.map +1 -0
  47. package/dist/placeholder/index.js +340 -346
  48. package/dist/placeholder/index.js.map +1 -1
  49. package/dist/selection/index.cjs +62 -86
  50. package/dist/selection/index.cjs.map +1 -1
  51. package/dist/selection/index.d.cts +11 -10
  52. package/dist/selection/index.d.cts.map +1 -0
  53. package/dist/selection/index.d.ts +11 -10
  54. package/dist/selection/index.d.ts.map +1 -0
  55. package/dist/selection/index.js +58 -56
  56. package/dist/selection/index.js.map +1 -1
  57. package/dist/trailing-node/index.cjs +59 -89
  58. package/dist/trailing-node/index.cjs.map +1 -1
  59. package/dist/trailing-node/index.d.cts +17 -16
  60. package/dist/trailing-node/index.d.cts.map +1 -0
  61. package/dist/trailing-node/index.d.ts +17 -16
  62. package/dist/trailing-node/index.d.ts.map +1 -0
  63. package/dist/trailing-node/index.js +55 -59
  64. package/dist/trailing-node/index.js.map +1 -1
  65. package/dist/undo-redo/index.cjs +45 -63
  66. package/dist/undo-redo/index.cjs.map +1 -1
  67. package/dist/undo-redo/index.d.cts +31 -30
  68. package/dist/undo-redo/index.d.cts.map +1 -0
  69. package/dist/undo-redo/index.d.ts +31 -30
  70. package/dist/undo-redo/index.d.ts.map +1 -0
  71. package/dist/undo-redo/index.js +43 -35
  72. package/dist/undo-redo/index.js.map +1 -1
  73. package/package.json +6 -6
package/dist/index.js CHANGED
@@ -1,748 +1,698 @@
1
- // src/character-count/character-count.ts
2
- import { Extension } from "@tiptap/core";
1
+ import { Extension, callOrReturn, getChangedRanges, getExtensionField, isNodeEmpty, isNodeSelection } from "@tiptap/core";
3
2
  import { Plugin, PluginKey } from "@tiptap/pm/state";
4
- var CharacterCount = Extension.create({
5
- name: "characterCount",
6
- addOptions() {
7
- return {
8
- limit: null,
9
- autoTrim: true,
10
- mode: "textSize",
11
- textCounter: (text) => text.length,
12
- wordCounter: (text) => text.split(" ").filter((word) => word !== "").length
13
- };
14
- },
15
- addStorage() {
16
- return {
17
- characters: () => 0,
18
- words: () => 0
19
- };
20
- },
21
- onBeforeCreate() {
22
- this.storage.characters = (options) => {
23
- const node = (options == null ? void 0 : options.node) || this.editor.state.doc;
24
- const mode = (options == null ? void 0 : options.mode) || this.options.mode;
25
- if (mode === "textSize") {
26
- const text = node.textBetween(0, node.content.size, void 0, " ");
27
- return this.options.textCounter(text);
28
- }
29
- return node.nodeSize;
30
- };
31
- this.storage.words = (options) => {
32
- const node = (options == null ? void 0 : options.node) || this.editor.state.doc;
33
- const text = node.textBetween(0, node.content.size, " ", " ");
34
- return this.options.wordCounter(text);
35
- };
36
- },
37
- addProseMirrorPlugins() {
38
- let initialEvaluationDone = false;
39
- return [
40
- new Plugin({
41
- key: new PluginKey("characterCount"),
42
- appendTransaction: (transactions, oldState, newState) => {
43
- if (initialEvaluationDone) {
44
- return;
45
- }
46
- const limit = this.options.limit;
47
- const autoTrim = this.options.autoTrim;
48
- if (limit === null || limit === void 0 || limit === 0 || autoTrim === false) {
49
- initialEvaluationDone = true;
50
- return;
51
- }
52
- const initialContentSize = this.storage.characters({ node: newState.doc });
53
- if (initialContentSize > limit) {
54
- const over = initialContentSize - limit;
55
- const from = 0;
56
- const to = over;
57
- console.warn(
58
- `[CharacterCount] Initial content exceeded limit of ${limit} characters. Content was automatically trimmed.`
59
- );
60
- const tr = newState.tr.deleteRange(from, to);
61
- initialEvaluationDone = true;
62
- return tr;
63
- }
64
- initialEvaluationDone = true;
65
- },
66
- filterTransaction: (transaction, state) => {
67
- const limit = this.options.limit;
68
- if (!transaction.docChanged || limit === 0 || limit === null || limit === void 0) {
69
- return true;
70
- }
71
- const oldSize = this.storage.characters({ node: state.doc });
72
- const newSize = this.storage.characters({ node: transaction.doc });
73
- if (newSize <= limit) {
74
- return true;
75
- }
76
- if (oldSize > limit && newSize > limit && newSize <= oldSize) {
77
- return true;
78
- }
79
- if (oldSize > limit && newSize > limit && newSize > oldSize) {
80
- return false;
81
- }
82
- const isPaste = transaction.getMeta("paste");
83
- if (!isPaste) {
84
- return false;
85
- }
86
- const pos = transaction.selection.$head.pos;
87
- const over = newSize - limit;
88
- const from = pos - over;
89
- const to = pos;
90
- transaction.deleteRange(from, to);
91
- const updatedSize = this.storage.characters({ node: transaction.doc });
92
- if (updatedSize > limit) {
93
- return false;
94
- }
95
- return true;
96
- }
97
- })
98
- ];
99
- }
100
- });
101
-
102
- // src/drop-cursor/drop-cursor.ts
103
- import { Extension as Extension2 } from "@tiptap/core";
104
3
  import { dropCursor } from "@tiptap/pm/dropcursor";
105
- var Dropcursor = Extension2.create({
106
- name: "dropCursor",
107
- addOptions() {
108
- return {
109
- color: "currentColor",
110
- width: 1,
111
- class: void 0
112
- };
113
- },
114
- addProseMirrorPlugins() {
115
- return [dropCursor(this.options)];
116
- }
117
- });
118
-
119
- // src/focus/focus.ts
120
- import { Extension as Extension3 } from "@tiptap/core";
121
- import { Plugin as Plugin2, PluginKey as PluginKey2 } from "@tiptap/pm/state";
122
4
  import { Decoration, DecorationSet } from "@tiptap/pm/view";
123
- var Focus = Extension3.create({
124
- name: "focus",
125
- addOptions() {
126
- return {
127
- className: "has-focus",
128
- mode: "all"
129
- };
130
- },
131
- addProseMirrorPlugins() {
132
- return [
133
- new Plugin2({
134
- key: new PluginKey2("focus"),
135
- props: {
136
- decorations: ({ doc, selection }) => {
137
- const { isEditable, isFocused } = this.editor;
138
- const { anchor } = selection;
139
- const decorations = [];
140
- if (!isEditable || !isFocused) {
141
- return DecorationSet.create(doc, []);
142
- }
143
- let maxLevels = 0;
144
- if (this.options.mode === "deepest") {
145
- doc.descendants((node, pos) => {
146
- if (node.isText) {
147
- return;
148
- }
149
- const isCurrent = anchor >= pos && anchor <= pos + node.nodeSize - 1;
150
- if (!isCurrent) {
151
- return false;
152
- }
153
- maxLevels += 1;
154
- });
155
- }
156
- let currentLevel = 0;
157
- doc.descendants((node, pos) => {
158
- if (node.isText) {
159
- return false;
160
- }
161
- const isCurrent = anchor >= pos && anchor <= pos + node.nodeSize - 1;
162
- if (!isCurrent) {
163
- return false;
164
- }
165
- currentLevel += 1;
166
- const outOfScope = this.options.mode === "deepest" && maxLevels - currentLevel > 0 || this.options.mode === "shallowest" && currentLevel > 1;
167
- if (outOfScope) {
168
- return this.options.mode === "deepest";
169
- }
170
- decorations.push(
171
- Decoration.node(pos, pos + node.nodeSize, {
172
- class: this.options.className
173
- })
174
- );
175
- });
176
- return DecorationSet.create(doc, decorations);
177
- }
178
- }
179
- })
180
- ];
181
- }
182
- });
183
-
184
- // src/gap-cursor/gap-cursor.ts
185
- import { callOrReturn, Extension as Extension4, getExtensionField } from "@tiptap/core";
186
5
  import { gapCursor } from "@tiptap/pm/gapcursor";
187
- var Gapcursor = Extension4.create({
188
- name: "gapCursor",
189
- addProseMirrorPlugins() {
190
- return [gapCursor()];
191
- },
192
- extendNodeSchema(extension) {
193
- var _a;
194
- const context = {
195
- name: extension.name,
196
- options: extension.options,
197
- storage: extension.storage
198
- };
199
- return {
200
- allowGapCursor: (_a = callOrReturn(getExtensionField(extension, "allowGapCursor", context))) != null ? _a : null
201
- };
202
- }
6
+ import { history, redo, undo } from "@tiptap/pm/history";
7
+ //#region src/character-count/character-count.ts
8
+ /**
9
+ * This extension allows you to count the characters and words of your document.
10
+ * @see https://tiptap.dev/api/extensions/character-count
11
+ */
12
+ const CharacterCount = Extension.create({
13
+ name: "characterCount",
14
+ addOptions() {
15
+ return {
16
+ limit: null,
17
+ autoTrim: true,
18
+ mode: "textSize",
19
+ textCounter: (text) => text.length,
20
+ wordCounter: (text) => text.split(" ").filter((word) => word !== "").length
21
+ };
22
+ },
23
+ addStorage() {
24
+ return {
25
+ characters: () => 0,
26
+ words: () => 0
27
+ };
28
+ },
29
+ onBeforeCreate() {
30
+ this.storage.characters = (options) => {
31
+ const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc;
32
+ if (((options === null || options === void 0 ? void 0 : options.mode) || this.options.mode) === "textSize") {
33
+ const text = node.textBetween(0, node.content.size, void 0, " ");
34
+ return this.options.textCounter(text);
35
+ }
36
+ return node.nodeSize;
37
+ };
38
+ this.storage.words = (options) => {
39
+ const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc;
40
+ const text = node.textBetween(0, node.content.size, " ", " ");
41
+ return this.options.wordCounter(text);
42
+ };
43
+ },
44
+ addProseMirrorPlugins() {
45
+ let initialEvaluationDone = false;
46
+ return [new Plugin({
47
+ key: new PluginKey("characterCount"),
48
+ appendTransaction: (transactions, oldState, newState) => {
49
+ if (initialEvaluationDone) return;
50
+ const limit = this.options.limit;
51
+ const autoTrim = this.options.autoTrim;
52
+ if (limit === null || limit === void 0 || limit === 0 || autoTrim === false) {
53
+ initialEvaluationDone = true;
54
+ return;
55
+ }
56
+ const initialContentSize = this.storage.characters({ node: newState.doc });
57
+ if (initialContentSize > limit) {
58
+ const over = initialContentSize - limit;
59
+ const from = 0;
60
+ const to = over;
61
+ console.warn(`[CharacterCount] Initial content exceeded limit of ${limit} characters. Content was automatically trimmed.`);
62
+ const tr = newState.tr.deleteRange(from, to);
63
+ initialEvaluationDone = true;
64
+ return tr;
65
+ }
66
+ initialEvaluationDone = true;
67
+ },
68
+ filterTransaction: (transaction, state) => {
69
+ const limit = this.options.limit;
70
+ if (!transaction.docChanged || limit === 0 || limit === null || limit === void 0) return true;
71
+ const oldSize = this.storage.characters({ node: state.doc });
72
+ const newSize = this.storage.characters({ node: transaction.doc });
73
+ if (newSize <= limit) return true;
74
+ if (oldSize > limit && newSize > limit && newSize <= oldSize) return true;
75
+ if (oldSize > limit && newSize > limit && newSize > oldSize) return false;
76
+ if (!transaction.getMeta("paste")) return false;
77
+ const pos = transaction.selection.$head.pos;
78
+ const from = pos - (newSize - limit);
79
+ const to = pos;
80
+ transaction.deleteRange(from, to);
81
+ if (this.storage.characters({ node: transaction.doc }) > limit) return false;
82
+ return true;
83
+ }
84
+ })];
85
+ }
203
86
  });
204
-
205
- // src/placeholder/constants.ts
206
- import { PluginKey as PluginKey3 } from "@tiptap/pm/state";
207
- var DEFAULT_DATA_ATTRIBUTE = "placeholder";
208
- var PLUGIN_KEY = new PluginKey3("tiptap__placeholder");
209
-
210
- // src/placeholder/placeholder.ts
211
- import { Extension as Extension5 } from "@tiptap/core";
212
-
213
- // src/placeholder/plugins/PlaceholderPlugin.ts
214
- import { Plugin as Plugin3 } from "@tiptap/pm/state";
215
- import { DecorationSet as DecorationSet4 } from "@tiptap/pm/view";
216
-
217
- // src/placeholder/utils/buildPlaceholderDecorations.ts
218
- import { isNodeEmpty } from "@tiptap/core";
219
- import { DecorationSet as DecorationSet2 } from "@tiptap/pm/view";
220
-
221
- // src/placeholder/utils/createPlaceholderDecoration.ts
222
- import { Decoration as Decoration2 } from "@tiptap/pm/view";
87
+ //#endregion
88
+ //#region src/drop-cursor/drop-cursor.ts
89
+ /**
90
+ * This extension allows you to add a drop cursor to your editor.
91
+ * A drop cursor is a line that appears when you drag and drop content
92
+ * in-between nodes.
93
+ * @see https://tiptap.dev/api/extensions/dropcursor
94
+ */
95
+ const Dropcursor = Extension.create({
96
+ name: "dropCursor",
97
+ addOptions() {
98
+ return {
99
+ color: "currentColor",
100
+ width: 1,
101
+ class: void 0
102
+ };
103
+ },
104
+ addProseMirrorPlugins() {
105
+ return [dropCursor(this.options)];
106
+ }
107
+ });
108
+ //#endregion
109
+ //#region src/focus/focus.ts
110
+ /**
111
+ * This extension allows you to add a class to the focused node.
112
+ * @see https://www.tiptap.dev/api/extensions/focus
113
+ */
114
+ const Focus = Extension.create({
115
+ name: "focus",
116
+ addOptions() {
117
+ return {
118
+ className: "has-focus",
119
+ mode: "all"
120
+ };
121
+ },
122
+ addProseMirrorPlugins() {
123
+ return [new Plugin({
124
+ key: new PluginKey("focus"),
125
+ props: { decorations: ({ doc, selection }) => {
126
+ const { isEditable, isFocused } = this.editor;
127
+ const { anchor } = selection;
128
+ const decorations = [];
129
+ if (!isEditable || !isFocused) return DecorationSet.create(doc, []);
130
+ let maxLevels = 0;
131
+ if (this.options.mode === "deepest") doc.descendants((node, pos) => {
132
+ if (node.isText) return;
133
+ if (!(anchor >= pos && anchor <= pos + node.nodeSize - 1)) return false;
134
+ maxLevels += 1;
135
+ });
136
+ let currentLevel = 0;
137
+ doc.descendants((node, pos) => {
138
+ if (node.isText) return false;
139
+ if (!(anchor >= pos && anchor <= pos + node.nodeSize - 1)) return false;
140
+ currentLevel += 1;
141
+ if (this.options.mode === "deepest" && maxLevels - currentLevel > 0 || this.options.mode === "shallowest" && currentLevel > 1) return this.options.mode === "deepest";
142
+ decorations.push(Decoration.node(pos, pos + node.nodeSize, { class: this.options.className }));
143
+ });
144
+ return DecorationSet.create(doc, decorations);
145
+ } }
146
+ })];
147
+ }
148
+ });
149
+ //#endregion
150
+ //#region src/gap-cursor/gap-cursor.ts
151
+ /**
152
+ * This extension allows you to add a gap cursor to your editor.
153
+ * A gap cursor is a cursor that appears when you click on a place
154
+ * where no content is present, for example inbetween nodes.
155
+ * @see https://tiptap.dev/api/extensions/gapcursor
156
+ */
157
+ const Gapcursor = Extension.create({
158
+ name: "gapCursor",
159
+ addProseMirrorPlugins() {
160
+ return [gapCursor()];
161
+ },
162
+ extendNodeSchema(extension) {
163
+ var _callOrReturn;
164
+ return { allowGapCursor: (_callOrReturn = callOrReturn(getExtensionField(extension, "allowGapCursor", {
165
+ name: extension.name,
166
+ options: extension.options,
167
+ storage: extension.storage
168
+ }))) !== null && _callOrReturn !== void 0 ? _callOrReturn : null };
169
+ }
170
+ });
171
+ //#endregion
172
+ //#region src/placeholder/constants.ts
173
+ /** The default data attribute label */
174
+ const DEFAULT_DATA_ATTRIBUTE = "placeholder";
175
+ /** The plugin key used to store and read the placeholder decoration set */
176
+ const PLUGIN_KEY = new PluginKey("tiptap__placeholder");
177
+ //#endregion
178
+ //#region src/placeholder/utils/createPlaceholderDecoration.ts
179
+ /**
180
+ * Creates a ProseMirror node decoration that applies a placeholder
181
+ * CSS class and data attribute to an empty node.
182
+ * @param options.editor - The editor instance
183
+ * @param options.pos - The position of the node in the document
184
+ * @param options.node - The ProseMirror node
185
+ * @param options.isEmptyDoc - Whether the entire document is empty
186
+ * @param options.hasAnchor - Whether the selection anchor is within the node
187
+ * @param options.dataAttribute - The data attribute name (e.g. `data-placeholder`)
188
+ * @param options.classes - CSS classes for empty nodes and the empty editor
189
+ * @param options.placeholder - The placeholder text or a function that returns it
190
+ * @returns A ProseMirror node decoration with placeholder classes and data attribute
191
+ */
223
192
  function createPlaceholderDecoration(options) {
224
- const {
225
- editor,
226
- placeholder,
227
- dataAttribute,
228
- pos,
229
- node,
230
- isEmptyDoc,
231
- hasAnchor,
232
- classes: { emptyNode, emptyEditor }
233
- } = options;
234
- const classes = [emptyNode];
235
- if (isEmptyDoc) {
236
- classes.push(emptyEditor);
237
- }
238
- return Decoration2.node(pos, pos + node.nodeSize, {
239
- class: classes.join(" "),
240
- [dataAttribute]: typeof placeholder === "function" ? placeholder({
241
- editor,
242
- node,
243
- pos,
244
- hasAnchor
245
- }) : placeholder
246
- });
193
+ const { editor, placeholder, dataAttribute, pos, node, isEmptyDoc, hasAnchor, classes: { emptyNode, emptyEditor } } = options;
194
+ const classes = [emptyNode];
195
+ if (isEmptyDoc) classes.push(emptyEditor);
196
+ return Decoration.node(pos, pos + node.nodeSize, {
197
+ class: classes.join(" "),
198
+ [dataAttribute]: typeof placeholder === "function" ? placeholder({
199
+ editor,
200
+ node,
201
+ pos,
202
+ hasAnchor
203
+ }) : placeholder
204
+ });
247
205
  }
248
-
249
- // src/placeholder/utils/buildPlaceholderDecorations.ts
206
+ //#endregion
207
+ //#region src/placeholder/utils/buildPlaceholderDecorations.ts
250
208
  function resolveEmptyNodeClass(emptyNodeClass, props) {
251
- return typeof emptyNodeClass === "function" ? emptyNodeClass(props) : emptyNodeClass;
209
+ return typeof emptyNodeClass === "function" ? emptyNodeClass(props) : emptyNodeClass;
252
210
  }
253
- function scanRangeForDecorations({
254
- editor,
255
- options,
256
- dataAttribute,
257
- doc,
258
- selection,
259
- from,
260
- to
261
- }) {
262
- const { anchor } = selection;
263
- const decorations = [];
264
- const isEmptyDoc = editor.isEmpty;
265
- doc.nodesBetween(from, to, (node, pos) => {
266
- const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
267
- const isEmpty = !node.isLeaf && isNodeEmpty(node);
268
- if (!node.type.isTextblock) {
269
- return options.includeChildren;
270
- }
271
- if ((hasAnchor || !options.showOnlyCurrent) && isEmpty) {
272
- decorations.push(
273
- createPlaceholderDecoration({
274
- editor,
275
- isEmptyDoc,
276
- dataAttribute,
277
- hasAnchor,
278
- placeholder: options.placeholder,
279
- classes: {
280
- emptyEditor: options.emptyEditorClass,
281
- emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
282
- editor,
283
- node,
284
- pos,
285
- hasAnchor
286
- })
287
- },
288
- node,
289
- pos
290
- })
291
- );
292
- }
293
- return options.includeChildren;
294
- });
295
- return decorations;
211
+ /**
212
+ * Scans a document range for empty textblocks that should receive placeholder
213
+ * decorations. Used by the slow path and incremental state updates.
214
+ */
215
+ function scanRangeForDecorations({ editor, options, dataAttribute, doc, selection, from, to }) {
216
+ const { anchor } = selection;
217
+ const decorations = [];
218
+ const isEmptyDoc = editor.isEmpty;
219
+ doc.nodesBetween(from, to, (node, pos) => {
220
+ const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
221
+ const isEmpty = !node.isLeaf && isNodeEmpty(node);
222
+ if (!node.type.isTextblock) return options.includeChildren;
223
+ if ((hasAnchor || !options.showOnlyCurrent) && isEmpty) decorations.push(createPlaceholderDecoration({
224
+ editor,
225
+ isEmptyDoc,
226
+ dataAttribute,
227
+ hasAnchor,
228
+ placeholder: options.placeholder,
229
+ classes: {
230
+ emptyEditor: options.emptyEditorClass,
231
+ emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
232
+ editor,
233
+ node,
234
+ pos,
235
+ hasAnchor
236
+ })
237
+ },
238
+ node,
239
+ pos
240
+ }));
241
+ return options.includeChildren;
242
+ });
243
+ return decorations;
296
244
  }
297
- function buildPlaceholderDecorations({
298
- editor,
299
- options,
300
- dataAttribute,
301
- doc,
302
- selection
303
- }) {
304
- const active = editor.isEditable || !options.showOnlyWhenEditable;
305
- if (!active) {
306
- return null;
307
- }
308
- const { anchor } = selection;
309
- const decorations = [];
310
- const isEmptyDoc = editor.isEmpty;
311
- const useResolvedPath = options.showOnlyCurrent && !options.includeChildren;
312
- if (useResolvedPath) {
313
- const resolved = doc.resolve(anchor);
314
- const node = resolved.depth > 0 ? resolved.node(1) : resolved.nodeAfter;
315
- const nodeStart = resolved.depth > 0 ? resolved.before(1) : anchor;
316
- if (node && node.type.isTextblock && isNodeEmpty(node)) {
317
- const hasAnchor = anchor >= nodeStart && anchor <= nodeStart + node.nodeSize;
318
- decorations.push(
319
- createPlaceholderDecoration({
320
- editor,
321
- isEmptyDoc,
322
- dataAttribute,
323
- hasAnchor,
324
- placeholder: options.placeholder,
325
- classes: {
326
- emptyEditor: options.emptyEditorClass,
327
- emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
328
- editor,
329
- node,
330
- pos: nodeStart,
331
- hasAnchor
332
- })
333
- },
334
- node,
335
- pos: nodeStart
336
- })
337
- );
338
- }
339
- } else {
340
- decorations.push(
341
- ...scanRangeForDecorations({
342
- editor,
343
- options,
344
- dataAttribute,
345
- doc,
346
- selection,
347
- from: 0,
348
- to: doc.content.size
349
- })
350
- );
351
- }
352
- return DecorationSet2.create(doc, decorations);
245
+ /**
246
+ * Builds the placeholder decorations for the current document state.
247
+ * @param options.editor - The editor instance.
248
+ * @param options.options - The resolved placeholder options.
249
+ * @param options.dataAttribute - The prepared `data-*` attribute name.
250
+ * @param options.doc - The current document node.
251
+ * @param options.selection - The current selection.
252
+ * @returns A decoration set, or `null` when no placeholders should be shown.
253
+ */
254
+ function buildPlaceholderDecorations({ editor, options, dataAttribute, doc, selection }) {
255
+ if (!(editor.isEditable || !options.showOnlyWhenEditable)) return null;
256
+ const { anchor } = selection;
257
+ const decorations = [];
258
+ const isEmptyDoc = editor.isEmpty;
259
+ if (options.showOnlyCurrent && !options.includeChildren) {
260
+ const resolved = doc.resolve(anchor);
261
+ const node = resolved.depth > 0 ? resolved.node(1) : resolved.nodeAfter;
262
+ const nodeStart = resolved.depth > 0 ? resolved.before(1) : anchor;
263
+ if (node && node.type.isTextblock && isNodeEmpty(node)) {
264
+ const hasAnchor = anchor >= nodeStart && anchor <= nodeStart + node.nodeSize;
265
+ decorations.push(createPlaceholderDecoration({
266
+ editor,
267
+ isEmptyDoc,
268
+ dataAttribute,
269
+ hasAnchor,
270
+ placeholder: options.placeholder,
271
+ classes: {
272
+ emptyEditor: options.emptyEditorClass,
273
+ emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
274
+ editor,
275
+ node,
276
+ pos: nodeStart,
277
+ hasAnchor
278
+ })
279
+ },
280
+ node,
281
+ pos: nodeStart
282
+ }));
283
+ }
284
+ } else decorations.push(...scanRangeForDecorations({
285
+ editor,
286
+ options,
287
+ dataAttribute,
288
+ doc,
289
+ selection,
290
+ from: 0,
291
+ to: doc.content.size
292
+ }));
293
+ return DecorationSet.create(doc, decorations);
353
294
  }
354
-
355
- // src/placeholder/utils/placeholderStateField.ts
356
- import { getChangedRanges } from "@tiptap/core";
357
- import { DecorationSet as DecorationSet3 } from "@tiptap/pm/view";
358
-
359
- // src/placeholder/utils/resolveTopLevelRange.ts
295
+ //#endregion
296
+ //#region src/placeholder/utils/resolveTopLevelRange.ts
297
+ /**
298
+ * Resolves a document position to the `[from, to)` range of its containing
299
+ * top-level block node in absolute document positions.
300
+ */
360
301
  function resolveTopLevelRange(doc, pos) {
361
- var _a;
362
- const resolved = doc.resolve(pos);
363
- if (resolved.depth === 0) {
364
- const node2 = (_a = resolved.nodeAfter) != null ? _a : resolved.nodeBefore;
365
- if (!node2) {
366
- return { from: pos, to: pos };
367
- }
368
- const nodePos = resolved.nodeAfter ? pos : pos - node2.nodeSize;
369
- return { from: nodePos, to: nodePos + node2.nodeSize };
370
- }
371
- const topLevelPos = resolved.before(1);
372
- const node = resolved.node(1);
373
- return { from: topLevelPos, to: topLevelPos + node.nodeSize };
302
+ const resolved = doc.resolve(pos);
303
+ if (resolved.depth === 0) {
304
+ var _resolved$nodeAfter;
305
+ const node = (_resolved$nodeAfter = resolved.nodeAfter) !== null && _resolved$nodeAfter !== void 0 ? _resolved$nodeAfter : resolved.nodeBefore;
306
+ if (!node) return {
307
+ from: pos,
308
+ to: pos
309
+ };
310
+ const nodePos = resolved.nodeAfter ? pos : pos - node.nodeSize;
311
+ return {
312
+ from: nodePos,
313
+ to: nodePos + node.nodeSize
314
+ };
315
+ }
316
+ const topLevelPos = resolved.before(1);
317
+ return {
318
+ from: topLevelPos,
319
+ to: topLevelPos + resolved.node(1).nodeSize
320
+ };
374
321
  }
322
+ /**
323
+ * Converts an absolute document range to content-relative positions used by
324
+ * `Node#nodesBetween` and `Node#forEach` offsets.
325
+ */
375
326
  function toContentRelativeRange(doc, range) {
376
- return {
377
- from: Math.max(0, range.from - 1),
378
- to: Math.min(doc.content.size, range.to - 1)
379
- };
327
+ return {
328
+ from: Math.max(0, range.from - 1),
329
+ to: Math.min(doc.content.size, range.to - 1)
330
+ };
380
331
  }
332
+ /**
333
+ * Returns the top-level block ranges that intersect a document change range.
334
+ * Input `from`/`to` are absolute positions (e.g. from `getChangedRanges`).
335
+ * Returned ranges are content-relative, matching `Node#forEach` offsets.
336
+ */
381
337
  function getTopLevelBlocksInRange(doc, from, to) {
382
- const ranges = [];
383
- doc.forEach((node, offset) => {
384
- const nodeStart = offset;
385
- const nodeEnd = nodeStart + node.nodeSize;
386
- const absNodeStart = nodeStart + 1;
387
- const absNodeEnd = nodeEnd + 1;
388
- if (absNodeStart < to && absNodeEnd > from) {
389
- ranges.push({ from: nodeStart, to: nodeEnd });
390
- }
391
- });
392
- return ranges;
338
+ const ranges = [];
339
+ doc.forEach((node, offset) => {
340
+ const nodeStart = offset;
341
+ const nodeEnd = nodeStart + node.nodeSize;
342
+ const absNodeStart = nodeStart + 1;
343
+ const absNodeEnd = nodeEnd + 1;
344
+ if (absNodeStart < to && absNodeEnd > from) ranges.push({
345
+ from: nodeStart,
346
+ to: nodeEnd
347
+ });
348
+ });
349
+ return ranges;
393
350
  }
351
+ /**
352
+ * Sorts ranges by start position and merges overlapping or adjacent ranges.
353
+ */
394
354
  function mergeRanges(ranges) {
395
- if (ranges.length === 0) {
396
- return [];
397
- }
398
- const sorted = [...ranges].sort((a, b) => a.from - b.from);
399
- const merged = [{ ...sorted[0] }];
400
- for (let i = 1; i < sorted.length; i += 1) {
401
- const last = merged[merged.length - 1];
402
- const current = sorted[i];
403
- if (current.from <= last.to) {
404
- last.to = Math.max(last.to, current.to);
405
- } else {
406
- merged.push({ ...current });
407
- }
408
- }
409
- return merged;
355
+ if (ranges.length === 0) return [];
356
+ const sorted = [...ranges].sort((a, b) => a.from - b.from);
357
+ const merged = [{ ...sorted[0] }];
358
+ for (let i = 1; i < sorted.length; i += 1) {
359
+ const last = merged[merged.length - 1];
360
+ const current = sorted[i];
361
+ if (current.from <= last.to) last.to = Math.max(last.to, current.to);
362
+ else merged.push({ ...current });
363
+ }
364
+ return merged;
410
365
  }
411
-
412
- // src/placeholder/utils/placeholderStateField.ts
366
+ //#endregion
367
+ //#region src/placeholder/utils/placeholderStateField.ts
368
+ /**
369
+ * Expands a single changed range to the top-level blocks it touches.
370
+ * Also resolves blocks at range boundaries so split/merge edits update
371
+ * adjacent empty nodes (e.g. a new paragraph after Enter).
372
+ */
413
373
  function collectBlocksForChange(doc, change) {
414
- const ranges = getTopLevelBlocksInRange(doc, change.from, change.to);
415
- ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, change.from)));
416
- if (change.to > change.from) {
417
- ranges.push(
418
- toContentRelativeRange(
419
- doc,
420
- resolveTopLevelRange(doc, Math.min(change.to, doc.content.size + 1) - 1)
421
- )
422
- );
423
- } else if (change.from < doc.content.size + 1) {
424
- ranges.push(
425
- toContentRelativeRange(
426
- doc,
427
- resolveTopLevelRange(doc, Math.min(change.from + 1, doc.content.size))
428
- )
429
- );
430
- }
431
- return ranges;
374
+ const ranges = getTopLevelBlocksInRange(doc, change.from, change.to);
375
+ ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, change.from)));
376
+ if (change.to > change.from) ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, Math.min(change.to, doc.content.size + 1) - 1)));
377
+ else if (change.from < doc.content.size + 1) ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, Math.min(change.from + 1, doc.content.size))));
378
+ return ranges;
432
379
  }
380
+ /**
381
+ * Collects content-relative top-level block ranges that need placeholder
382
+ * decorations recomputed after a transaction.
383
+ */
433
384
  function collectRescanRanges(tr, oldState, newState) {
434
- const ranges = [];
435
- if (tr.docChanged) {
436
- const changes = getChangedRanges(tr);
437
- for (const change of changes) {
438
- ranges.push(...collectBlocksForChange(newState.doc, change.newRange));
439
- }
440
- }
441
- if (tr.selectionSet) {
442
- ranges.push(
443
- toContentRelativeRange(
444
- newState.doc,
445
- resolveTopLevelRange(newState.doc, tr.mapping.map(oldState.selection.anchor))
446
- )
447
- );
448
- ranges.push(
449
- toContentRelativeRange(
450
- newState.doc,
451
- resolveTopLevelRange(newState.doc, newState.selection.anchor)
452
- )
453
- );
454
- }
455
- return mergeRanges(ranges);
385
+ const ranges = [];
386
+ if (tr.docChanged) {
387
+ const changes = getChangedRanges(tr);
388
+ for (const change of changes) ranges.push(...collectBlocksForChange(newState.doc, change.newRange));
389
+ }
390
+ if (tr.selectionSet) {
391
+ ranges.push(toContentRelativeRange(newState.doc, resolveTopLevelRange(newState.doc, tr.mapping.map(oldState.selection.anchor))));
392
+ ranges.push(toContentRelativeRange(newState.doc, resolveTopLevelRange(newState.doc, newState.selection.anchor)));
393
+ }
394
+ return mergeRanges(ranges);
456
395
  }
396
+ /** Clamps a content-relative range to `[0, doc.content.size]`. */
457
397
  function clampRange(from, to, doc) {
458
- const clampedFrom = Math.max(0, Math.min(from, doc.content.size));
459
- const clampedTo = Math.max(clampedFrom, Math.min(to, doc.content.size));
460
- return { from: clampedFrom, to: clampedTo };
398
+ const clampedFrom = Math.max(0, Math.min(from, doc.content.size));
399
+ return {
400
+ from: clampedFrom,
401
+ to: Math.max(clampedFrom, Math.min(to, doc.content.size))
402
+ };
461
403
  }
462
- function updateDecorationsInRanges({
463
- decorations,
464
- ranges,
465
- editor,
466
- options,
467
- dataAttribute,
468
- doc,
469
- selection
470
- }) {
471
- let next = decorations;
472
- for (const range of ranges) {
473
- const { from, to } = clampRange(range.from, range.to, doc);
474
- const existing = next.find(from, to).filter((decoration) => decoration.from >= from && decoration.to <= to);
475
- if (existing.length) {
476
- next = next.remove(existing);
477
- }
478
- const newDecos = scanRangeForDecorations({
479
- editor,
480
- options,
481
- dataAttribute,
482
- doc,
483
- selection,
484
- from,
485
- to
486
- });
487
- if (newDecos.length) {
488
- next = next.add(doc, newDecos);
489
- }
490
- }
491
- return next;
404
+ /**
405
+ * Removes and rebuilds placeholder decorations within the given ranges.
406
+ * Only drops decorations fully contained in a range so mapped decorations
407
+ * on neighbouring blocks (e.g. at a block boundary) are kept intact.
408
+ */
409
+ function updateDecorationsInRanges({ decorations, ranges, editor, options, dataAttribute, doc, selection }) {
410
+ let next = decorations;
411
+ for (const range of ranges) {
412
+ const { from, to } = clampRange(range.from, range.to, doc);
413
+ const existing = next.find(from, to).filter((decoration) => decoration.from >= from && decoration.to <= to);
414
+ if (existing.length) next = next.remove(existing);
415
+ const newDecos = scanRangeForDecorations({
416
+ editor,
417
+ options,
418
+ dataAttribute,
419
+ doc,
420
+ selection,
421
+ from,
422
+ to
423
+ });
424
+ if (newDecos.length) next = next.add(doc, newDecos);
425
+ }
426
+ return next;
492
427
  }
493
- function createPlaceholderStateField({
494
- editor,
495
- options,
496
- dataAttribute
497
- }) {
498
- return {
499
- init(_config, state) {
500
- const decorations = buildPlaceholderDecorations({
501
- editor,
502
- options,
503
- dataAttribute,
504
- doc: state.doc,
505
- selection: state.selection
506
- });
507
- return decorations != null ? decorations : DecorationSet3.empty;
508
- },
509
- apply(tr, prev, oldState, newState) {
510
- if (!tr.docChanged && !tr.selectionSet) {
511
- return prev;
512
- }
513
- const mapped = prev.map(tr.mapping, tr.doc);
514
- const ranges = collectRescanRanges(tr, oldState, newState);
515
- return updateDecorationsInRanges({
516
- decorations: mapped,
517
- ranges,
518
- editor,
519
- options,
520
- dataAttribute,
521
- doc: newState.doc,
522
- selection: newState.selection
523
- });
524
- }
525
- };
428
+ /**
429
+ * Creates the incremental `StateField<DecorationSet>` used by the slow path
430
+ * (`showOnlyCurrent: false` or `includeChildren: true`).
431
+ *
432
+ * Decorations are mapped through each transaction and only recomputed for
433
+ * top-level blocks touched by document or selection changes.
434
+ * @param options.editor - The editor instance.
435
+ * @param options.options - The resolved placeholder options.
436
+ * @param options.dataAttribute - The prepared `data-*` attribute name.
437
+ * @returns A ProseMirror state field storing the placeholder decoration set.
438
+ */
439
+ function createPlaceholderStateField({ editor, options, dataAttribute }) {
440
+ return {
441
+ init(_config, state) {
442
+ const decorations = buildPlaceholderDecorations({
443
+ editor,
444
+ options,
445
+ dataAttribute,
446
+ doc: state.doc,
447
+ selection: state.selection
448
+ });
449
+ return decorations !== null && decorations !== void 0 ? decorations : DecorationSet.empty;
450
+ },
451
+ apply(tr, prev, oldState, newState) {
452
+ if (!tr.docChanged && !tr.selectionSet) return prev;
453
+ return updateDecorationsInRanges({
454
+ decorations: prev.map(tr.mapping, tr.doc),
455
+ ranges: collectRescanRanges(tr, oldState, newState),
456
+ editor,
457
+ options,
458
+ dataAttribute,
459
+ doc: newState.doc,
460
+ selection: newState.selection
461
+ });
462
+ }
463
+ };
526
464
  }
527
-
528
- // src/placeholder/utils/preparePlaceholderAttribute.ts
465
+ //#endregion
466
+ //#region src/placeholder/utils/preparePlaceholderAttribute.ts
467
+ /**
468
+ * Prepares the placeholder attribute by ensuring it is properly formatted.
469
+ * @param attr - The placeholder attribute string.
470
+ * @returns The prepared placeholder attribute string.
471
+ */
529
472
  function preparePlaceholderAttribute(attr) {
530
- return attr.replace(/\s+/g, "-").replace(/[^a-zA-Z0-9-]/g, "").replace(/^[0-9-]+/, "").replace(/^-+/, "").toLowerCase();
473
+ return attr.replace(/\s+/g, "-").replace(/[^a-zA-Z0-9-]/g, "").replace(/^[0-9-]+/, "").replace(/^-+/, "").toLowerCase();
531
474
  }
532
-
533
- // src/placeholder/plugins/PlaceholderPlugin.ts
475
+ //#endregion
476
+ //#region src/placeholder/plugins/PlaceholderPlugin.ts
477
+ /**
478
+ * Creates the ProseMirror plugin that renders placeholder decorations.
479
+ * @param options.editor - The editor instance.
480
+ * @param options.options - The resolved placeholder options.
481
+ * @returns The configured placeholder plugin.
482
+ */
534
483
  function createPlaceholderPlugin({ editor, options }) {
535
- const dataAttribute = options.dataAttribute ? `data-${preparePlaceholderAttribute(options.dataAttribute)}` : `data-${DEFAULT_DATA_ATTRIBUTE}`;
536
- const useResolvedPath = options.showOnlyCurrent && !options.includeChildren;
537
- return new Plugin3({
538
- key: PLUGIN_KEY,
539
- ...useResolvedPath ? {} : {
540
- state: createPlaceholderStateField({ editor, options, dataAttribute })
541
- },
542
- props: {
543
- decorations: useResolvedPath ? ({ doc, selection }) => buildPlaceholderDecorations({ editor, options, dataAttribute, doc, selection }) : (state) => {
544
- var _a;
545
- if (options.showOnlyWhenEditable && !editor.isEditable) {
546
- return DecorationSet4.empty;
547
- }
548
- return (_a = PLUGIN_KEY.getState(state)) != null ? _a : DecorationSet4.empty;
549
- }
550
- }
551
- });
484
+ const dataAttribute = options.dataAttribute ? `data-${preparePlaceholderAttribute(options.dataAttribute)}` : `data-${DEFAULT_DATA_ATTRIBUTE}`;
485
+ const useResolvedPath = options.showOnlyCurrent && !options.includeChildren;
486
+ return new Plugin({
487
+ key: PLUGIN_KEY,
488
+ ...useResolvedPath ? {} : { state: createPlaceholderStateField({
489
+ editor,
490
+ options,
491
+ dataAttribute
492
+ }) },
493
+ props: { decorations: useResolvedPath ? ({ doc, selection }) => buildPlaceholderDecorations({
494
+ editor,
495
+ options,
496
+ dataAttribute,
497
+ doc,
498
+ selection
499
+ }) : (state) => {
500
+ var _PLUGIN_KEY$getState;
501
+ if (options.showOnlyWhenEditable && !editor.isEditable) return DecorationSet.empty;
502
+ return (_PLUGIN_KEY$getState = PLUGIN_KEY.getState(state)) !== null && _PLUGIN_KEY$getState !== void 0 ? _PLUGIN_KEY$getState : DecorationSet.empty;
503
+ } }
504
+ });
552
505
  }
553
-
554
- // src/placeholder/placeholder.ts
555
- var Placeholder = Extension5.create({
556
- name: "placeholder",
557
- addOptions() {
558
- return {
559
- emptyEditorClass: "is-editor-empty",
560
- emptyNodeClass: "is-empty",
561
- dataAttribute: DEFAULT_DATA_ATTRIBUTE,
562
- placeholder: "Write something \u2026",
563
- showOnlyWhenEditable: true,
564
- showOnlyCurrent: true,
565
- includeChildren: false
566
- };
567
- },
568
- addProseMirrorPlugins() {
569
- return [createPlaceholderPlugin({ editor: this.editor, options: this.options })];
570
- }
506
+ //#endregion
507
+ //#region src/placeholder/placeholder.ts
508
+ /**
509
+ * This extension allows you to add a placeholder to your editor.
510
+ * A placeholder is a text that appears when the editor or a node is empty.
511
+ * @see https://www.tiptap.dev/api/extensions/placeholder
512
+ */
513
+ const Placeholder = Extension.create({
514
+ name: "placeholder",
515
+ addOptions() {
516
+ return {
517
+ emptyEditorClass: "is-editor-empty",
518
+ emptyNodeClass: "is-empty",
519
+ dataAttribute: DEFAULT_DATA_ATTRIBUTE,
520
+ placeholder: "Write something …",
521
+ showOnlyWhenEditable: true,
522
+ showOnlyCurrent: true,
523
+ includeChildren: false
524
+ };
525
+ },
526
+ addProseMirrorPlugins() {
527
+ return [createPlaceholderPlugin({
528
+ editor: this.editor,
529
+ options: this.options
530
+ })];
531
+ }
571
532
  });
572
-
573
- // src/selection/selection.ts
574
- import { Extension as Extension6, isNodeSelection } from "@tiptap/core";
575
- import { Plugin as Plugin4, PluginKey as PluginKey4 } from "@tiptap/pm/state";
576
- import { Decoration as Decoration3, DecorationSet as DecorationSet5 } from "@tiptap/pm/view";
533
+ //#endregion
534
+ //#region src/selection/selection.ts
535
+ /**
536
+ * Whether the native browser selection should be cleared on blur and restored on focus.
537
+ * Only applies to non-empty text selections in an editable editor.
538
+ */
577
539
  function shouldSyncDomSelection(state, editor) {
578
- return !state.selection.empty && !isNodeSelection(state.selection) && editor.isEditable;
540
+ return !state.selection.empty && !isNodeSelection(state.selection) && editor.isEditable;
579
541
  }
542
+ /**
543
+ * Whether the selection decoration should be rendered to keep the selection
544
+ * visible while the editor is blurred (and not dragging).
545
+ */
580
546
  function shouldPreserveSelection(state, editor) {
581
- return shouldSyncDomSelection(state, editor) && !editor.isFocused && !editor.view.dragging;
547
+ return shouldSyncDomSelection(state, editor) && !editor.isFocused && !editor.view.dragging;
582
548
  }
583
549
  function clearDomSelection() {
584
- var _a;
585
- (_a = window.getSelection()) == null ? void 0 : _a.removeAllRanges();
550
+ var _window$getSelection;
551
+ (_window$getSelection = window.getSelection()) === null || _window$getSelection === void 0 || _window$getSelection.removeAllRanges();
586
552
  }
553
+ /**
554
+ * Sync the native selection from the editor state.
555
+ * @see https://prosemirror.net/docs/ref/#view.EditorView.focus
556
+ */
587
557
  function restoreDomSelection(view) {
588
- view.focus();
558
+ view.focus();
589
559
  }
590
- var Selection = Extension6.create({
591
- name: "selection",
592
- addOptions() {
593
- return {
594
- className: "selection"
595
- };
596
- },
597
- addProseMirrorPlugins() {
598
- const { editor, options } = this;
599
- return [
600
- new Plugin4({
601
- key: new PluginKey4("selection"),
602
- props: {
603
- decorations(state) {
604
- if (!shouldPreserveSelection(state, editor)) {
605
- return null;
606
- }
607
- return DecorationSet5.create(state.doc, [
608
- Decoration3.inline(state.selection.from, state.selection.to, {
609
- class: options.className
610
- })
611
- ]);
612
- },
613
- handleDOMEvents: {
614
- blur(view) {
615
- if (!shouldSyncDomSelection(view.state, editor)) {
616
- return false;
617
- }
618
- clearDomSelection();
619
- return false;
620
- },
621
- focus(view) {
622
- if (!shouldSyncDomSelection(view.state, editor)) {
623
- return false;
624
- }
625
- requestAnimationFrame(() => {
626
- if (!editor.isDestroyed && view.hasFocus()) {
627
- restoreDomSelection(view);
628
- }
629
- });
630
- return false;
631
- }
632
- }
633
- }
634
- })
635
- ];
636
- }
560
+ /**
561
+ * This extension allows you to add a class to the selected text when the editor is blurred.
562
+ * It clears the native browser selection on blur (so `::selection` styles do not overlap the
563
+ * decoration) and restores it when the editor is focused again.
564
+ * @see https://www.tiptap.dev/api/extensions/selection
565
+ */
566
+ const Selection = Extension.create({
567
+ name: "selection",
568
+ addOptions() {
569
+ return { className: "selection" };
570
+ },
571
+ addProseMirrorPlugins() {
572
+ const { editor, options } = this;
573
+ return [new Plugin({
574
+ key: new PluginKey("selection"),
575
+ props: {
576
+ decorations(state) {
577
+ if (!shouldPreserveSelection(state, editor)) return null;
578
+ return DecorationSet.create(state.doc, [Decoration.inline(state.selection.from, state.selection.to, { class: options.className })]);
579
+ },
580
+ handleDOMEvents: {
581
+ blur(view) {
582
+ if (!shouldSyncDomSelection(view.state, editor)) return false;
583
+ clearDomSelection();
584
+ return false;
585
+ },
586
+ focus(view) {
587
+ if (!shouldSyncDomSelection(view.state, editor)) return false;
588
+ requestAnimationFrame(() => {
589
+ if (!editor.isDestroyed && view.hasFocus()) restoreDomSelection(view);
590
+ });
591
+ return false;
592
+ }
593
+ }
594
+ }
595
+ })];
596
+ }
637
597
  });
638
-
639
- // src/trailing-node/trailing-node.ts
640
- import { Extension as Extension7 } from "@tiptap/core";
641
- import { Plugin as Plugin5, PluginKey as PluginKey5 } from "@tiptap/pm/state";
642
- var skipTrailingNodeMeta = "skipTrailingNode";
643
- function nodeEqualsType({
644
- types,
645
- node
646
- }) {
647
- return node && Array.isArray(types) && types.includes(node.type) || (node == null ? void 0 : node.type) === types;
598
+ //#endregion
599
+ //#region src/trailing-node/trailing-node.ts
600
+ const skipTrailingNodeMeta = "skipTrailingNode";
601
+ function nodeEqualsType({ types, node }) {
602
+ return node && Array.isArray(types) && types.includes(node.type) || (node === null || node === void 0 ? void 0 : node.type) === types;
648
603
  }
649
- var TrailingNode = Extension7.create({
650
- name: "trailingNode",
651
- addOptions() {
652
- return {
653
- node: void 0,
654
- notAfter: []
655
- };
656
- },
657
- addProseMirrorPlugins() {
658
- var _a;
659
- const plugin = new PluginKey5(this.name);
660
- const defaultNode = this.options.node || ((_a = this.editor.schema.topNodeType.contentMatch.defaultType) == null ? void 0 : _a.name) || "paragraph";
661
- const disabledNodes = Object.entries(this.editor.schema.nodes).map(([, value]) => value).filter((node) => (this.options.notAfter || []).concat(defaultNode).includes(node.name));
662
- return [
663
- new Plugin5({
664
- key: plugin,
665
- appendTransaction: (transactions, __, state) => {
666
- const { doc, tr, schema } = state;
667
- const shouldInsertNodeAtEnd = plugin.getState(state);
668
- const endPosition = doc.content.size;
669
- const type = schema.nodes[defaultNode];
670
- if (transactions.some((transaction) => transaction.getMeta(skipTrailingNodeMeta))) {
671
- return;
672
- }
673
- if (!shouldInsertNodeAtEnd) {
674
- return;
675
- }
676
- return tr.insert(endPosition, type.create());
677
- },
678
- state: {
679
- init: (_, state) => {
680
- const lastNode = state.tr.doc.lastChild;
681
- return !nodeEqualsType({ node: lastNode, types: disabledNodes });
682
- },
683
- apply: (tr, value) => {
684
- if (!tr.docChanged) {
685
- return value;
686
- }
687
- if (tr.getMeta("__uniqueIDTransaction")) {
688
- return value;
689
- }
690
- const lastNode = tr.doc.lastChild;
691
- return !nodeEqualsType({ node: lastNode, types: disabledNodes });
692
- }
693
- }
694
- })
695
- ];
696
- }
604
+ /**
605
+ * This extension allows you to add an extra node at the end of the document.
606
+ * @see https://www.tiptap.dev/api/extensions/trailing-node
607
+ */
608
+ const TrailingNode = Extension.create({
609
+ name: "trailingNode",
610
+ addOptions() {
611
+ return {
612
+ node: void 0,
613
+ notAfter: []
614
+ };
615
+ },
616
+ addProseMirrorPlugins() {
617
+ var _this$editor$schema$t;
618
+ const plugin = new PluginKey(this.name);
619
+ const defaultNode = this.options.node || ((_this$editor$schema$t = this.editor.schema.topNodeType.contentMatch.defaultType) === null || _this$editor$schema$t === void 0 ? void 0 : _this$editor$schema$t.name) || "paragraph";
620
+ const disabledNodes = Object.entries(this.editor.schema.nodes).map(([, value]) => value).filter((node) => (this.options.notAfter || []).concat(defaultNode).includes(node.name));
621
+ return [new Plugin({
622
+ key: plugin,
623
+ appendTransaction: (transactions, __, state) => {
624
+ const { doc, tr, schema } = state;
625
+ const shouldInsertNodeAtEnd = plugin.getState(state);
626
+ const endPosition = doc.content.size;
627
+ const type = schema.nodes[defaultNode];
628
+ if (transactions.some((transaction) => transaction.getMeta("skipTrailingNode"))) return;
629
+ if (!shouldInsertNodeAtEnd) return;
630
+ return tr.insert(endPosition, type.create());
631
+ },
632
+ state: {
633
+ init: (_, state) => {
634
+ const lastNode = state.tr.doc.lastChild;
635
+ return !nodeEqualsType({
636
+ node: lastNode,
637
+ types: disabledNodes
638
+ });
639
+ },
640
+ apply: (tr, value) => {
641
+ if (!tr.docChanged) return value;
642
+ if (tr.getMeta("__uniqueIDTransaction")) return value;
643
+ const lastNode = tr.doc.lastChild;
644
+ return !nodeEqualsType({
645
+ node: lastNode,
646
+ types: disabledNodes
647
+ });
648
+ }
649
+ }
650
+ })];
651
+ }
697
652
  });
698
-
699
- // src/undo-redo/undo-redo.ts
700
- import { Extension as Extension8 } from "@tiptap/core";
701
- import { history, redo, undo } from "@tiptap/pm/history";
702
- var UndoRedo = Extension8.create({
703
- name: "undoRedo",
704
- addOptions() {
705
- return {
706
- depth: 100,
707
- newGroupDelay: 500
708
- };
709
- },
710
- addCommands() {
711
- return {
712
- undo: () => ({ state, dispatch }) => {
713
- return undo(state, dispatch);
714
- },
715
- redo: () => ({ state, dispatch }) => {
716
- return redo(state, dispatch);
717
- }
718
- };
719
- },
720
- addProseMirrorPlugins() {
721
- return [history(this.options)];
722
- },
723
- addKeyboardShortcuts() {
724
- return {
725
- "Mod-z": () => this.editor.commands.undo(),
726
- "Shift-Mod-z": () => this.editor.commands.redo(),
727
- "Mod-y": () => this.editor.commands.redo(),
728
- // Russian keyboard layouts
729
- "Mod-\u044F": () => this.editor.commands.undo(),
730
- "Shift-Mod-\u044F": () => this.editor.commands.redo()
731
- };
732
- }
653
+ //#endregion
654
+ //#region src/undo-redo/undo-redo.ts
655
+ /**
656
+ * This extension allows you to undo and redo recent changes.
657
+ * @see https://www.tiptap.dev/api/extensions/undo-redo
658
+ *
659
+ * **Important**: If the `@tiptap/extension-collaboration` package is used, make sure to remove
660
+ * the `undo-redo` extension, as it is not compatible with the `collaboration` extension.
661
+ *
662
+ * `@tiptap/extension-collaboration` uses its own history implementation.
663
+ */
664
+ const UndoRedo = Extension.create({
665
+ name: "undoRedo",
666
+ addOptions() {
667
+ return {
668
+ depth: 100,
669
+ newGroupDelay: 500
670
+ };
671
+ },
672
+ addCommands() {
673
+ return {
674
+ undo: () => ({ state, dispatch }) => {
675
+ return undo(state, dispatch);
676
+ },
677
+ redo: () => ({ state, dispatch }) => {
678
+ return redo(state, dispatch);
679
+ }
680
+ };
681
+ },
682
+ addProseMirrorPlugins() {
683
+ return [history(this.options)];
684
+ },
685
+ addKeyboardShortcuts() {
686
+ return {
687
+ "Mod-z": () => this.editor.commands.undo(),
688
+ "Shift-Mod-z": () => this.editor.commands.redo(),
689
+ "Mod-y": () => this.editor.commands.redo(),
690
+ "Mod-я": () => this.editor.commands.undo(),
691
+ "Shift-Mod-я": () => this.editor.commands.redo()
692
+ };
693
+ }
733
694
  });
734
- export {
735
- CharacterCount,
736
- DEFAULT_DATA_ATTRIBUTE,
737
- Dropcursor,
738
- Focus,
739
- Gapcursor,
740
- PLUGIN_KEY,
741
- Placeholder,
742
- Selection,
743
- TrailingNode,
744
- UndoRedo,
745
- preparePlaceholderAttribute,
746
- skipTrailingNodeMeta
747
- };
695
+ //#endregion
696
+ export { CharacterCount, DEFAULT_DATA_ATTRIBUTE, Dropcursor, Focus, Gapcursor, PLUGIN_KEY, Placeholder, Selection, TrailingNode, UndoRedo, preparePlaceholderAttribute, skipTrailingNodeMeta };
697
+
748
698
  //# sourceMappingURL=index.js.map