@tiptap/extensions 3.30.2 → 3.30.4

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 +26 -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 +25 -21
  32. package/dist/gap-cursor/index.js.map +1 -1
  33. package/dist/index.cjs +667 -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 +654 -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,699 @@
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
+ const context = {
165
+ name: extension.name,
166
+ options: extension.options,
167
+ storage: extension.storage
168
+ };
169
+ return { allowGapCursor: (_callOrReturn = callOrReturn(getExtensionField(extension, "allowGapCursor", context))) !== null && _callOrReturn !== void 0 ? _callOrReturn : null };
170
+ }
171
+ });
172
+ //#endregion
173
+ //#region src/placeholder/constants.ts
174
+ /** The default data attribute label */
175
+ const DEFAULT_DATA_ATTRIBUTE = "placeholder";
176
+ /** The plugin key used to store and read the placeholder decoration set */
177
+ const PLUGIN_KEY = new PluginKey("tiptap__placeholder");
178
+ //#endregion
179
+ //#region src/placeholder/utils/createPlaceholderDecoration.ts
180
+ /**
181
+ * Creates a ProseMirror node decoration that applies a placeholder
182
+ * CSS class and data attribute to an empty node.
183
+ * @param options.editor - The editor instance
184
+ * @param options.pos - The position of the node in the document
185
+ * @param options.node - The ProseMirror node
186
+ * @param options.isEmptyDoc - Whether the entire document is empty
187
+ * @param options.hasAnchor - Whether the selection anchor is within the node
188
+ * @param options.dataAttribute - The data attribute name (e.g. `data-placeholder`)
189
+ * @param options.classes - CSS classes for empty nodes and the empty editor
190
+ * @param options.placeholder - The placeholder text or a function that returns it
191
+ * @returns A ProseMirror node decoration with placeholder classes and data attribute
192
+ */
223
193
  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
- });
194
+ const { editor, placeholder, dataAttribute, pos, node, isEmptyDoc, hasAnchor, classes: { emptyNode, emptyEditor } } = options;
195
+ const classes = [emptyNode];
196
+ if (isEmptyDoc) classes.push(emptyEditor);
197
+ return Decoration.node(pos, pos + node.nodeSize, {
198
+ class: classes.join(" "),
199
+ [dataAttribute]: typeof placeholder === "function" ? placeholder({
200
+ editor,
201
+ node,
202
+ pos,
203
+ hasAnchor
204
+ }) : placeholder
205
+ });
247
206
  }
248
-
249
- // src/placeholder/utils/buildPlaceholderDecorations.ts
207
+ //#endregion
208
+ //#region src/placeholder/utils/buildPlaceholderDecorations.ts
250
209
  function resolveEmptyNodeClass(emptyNodeClass, props) {
251
- return typeof emptyNodeClass === "function" ? emptyNodeClass(props) : emptyNodeClass;
210
+ return typeof emptyNodeClass === "function" ? emptyNodeClass(props) : emptyNodeClass;
252
211
  }
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;
212
+ /**
213
+ * Scans a document range for empty textblocks that should receive placeholder
214
+ * decorations. Used by the slow path and incremental state updates.
215
+ */
216
+ function scanRangeForDecorations({ editor, options, dataAttribute, doc, selection, from, to }) {
217
+ const { anchor } = selection;
218
+ const decorations = [];
219
+ const isEmptyDoc = editor.isEmpty;
220
+ doc.nodesBetween(from, to, (node, pos) => {
221
+ const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
222
+ const isEmpty = !node.isLeaf && isNodeEmpty(node);
223
+ if (!node.type.isTextblock) return options.includeChildren;
224
+ if ((hasAnchor || !options.showOnlyCurrent) && isEmpty) decorations.push(createPlaceholderDecoration({
225
+ editor,
226
+ isEmptyDoc,
227
+ dataAttribute,
228
+ hasAnchor,
229
+ placeholder: options.placeholder,
230
+ classes: {
231
+ emptyEditor: options.emptyEditorClass,
232
+ emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
233
+ editor,
234
+ node,
235
+ pos,
236
+ hasAnchor
237
+ })
238
+ },
239
+ node,
240
+ pos
241
+ }));
242
+ return options.includeChildren;
243
+ });
244
+ return decorations;
296
245
  }
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);
246
+ /**
247
+ * Builds the placeholder decorations for the current document state.
248
+ * @param options.editor - The editor instance.
249
+ * @param options.options - The resolved placeholder options.
250
+ * @param options.dataAttribute - The prepared `data-*` attribute name.
251
+ * @param options.doc - The current document node.
252
+ * @param options.selection - The current selection.
253
+ * @returns A decoration set, or `null` when no placeholders should be shown.
254
+ */
255
+ function buildPlaceholderDecorations({ editor, options, dataAttribute, doc, selection }) {
256
+ if (!(editor.isEditable || !options.showOnlyWhenEditable)) return null;
257
+ const { anchor } = selection;
258
+ const decorations = [];
259
+ const isEmptyDoc = editor.isEmpty;
260
+ if (options.showOnlyCurrent && !options.includeChildren) {
261
+ const resolved = doc.resolve(anchor);
262
+ const node = resolved.depth > 0 ? resolved.node(1) : resolved.nodeAfter;
263
+ const nodeStart = resolved.depth > 0 ? resolved.before(1) : anchor;
264
+ if (node && node.type.isTextblock && isNodeEmpty(node)) {
265
+ const hasAnchor = anchor >= nodeStart && anchor <= nodeStart + node.nodeSize;
266
+ decorations.push(createPlaceholderDecoration({
267
+ editor,
268
+ isEmptyDoc,
269
+ dataAttribute,
270
+ hasAnchor,
271
+ placeholder: options.placeholder,
272
+ classes: {
273
+ emptyEditor: options.emptyEditorClass,
274
+ emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
275
+ editor,
276
+ node,
277
+ pos: nodeStart,
278
+ hasAnchor
279
+ })
280
+ },
281
+ node,
282
+ pos: nodeStart
283
+ }));
284
+ }
285
+ } else decorations.push(...scanRangeForDecorations({
286
+ editor,
287
+ options,
288
+ dataAttribute,
289
+ doc,
290
+ selection,
291
+ from: 0,
292
+ to: doc.content.size
293
+ }));
294
+ return DecorationSet.create(doc, decorations);
353
295
  }
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
296
+ //#endregion
297
+ //#region src/placeholder/utils/resolveTopLevelRange.ts
298
+ /**
299
+ * Resolves a document position to the `[from, to)` range of its containing
300
+ * top-level block node in absolute document positions.
301
+ */
360
302
  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 };
303
+ const resolved = doc.resolve(pos);
304
+ if (resolved.depth === 0) {
305
+ var _resolved$nodeAfter;
306
+ const node = (_resolved$nodeAfter = resolved.nodeAfter) !== null && _resolved$nodeAfter !== void 0 ? _resolved$nodeAfter : resolved.nodeBefore;
307
+ if (!node) return {
308
+ from: pos,
309
+ to: pos
310
+ };
311
+ const nodePos = resolved.nodeAfter ? pos : pos - node.nodeSize;
312
+ return {
313
+ from: nodePos,
314
+ to: nodePos + node.nodeSize
315
+ };
316
+ }
317
+ const topLevelPos = resolved.before(1);
318
+ return {
319
+ from: topLevelPos,
320
+ to: topLevelPos + resolved.node(1).nodeSize
321
+ };
374
322
  }
323
+ /**
324
+ * Converts an absolute document range to content-relative positions used by
325
+ * `Node#nodesBetween` and `Node#forEach` offsets.
326
+ */
375
327
  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
- };
328
+ return {
329
+ from: Math.max(0, range.from - 1),
330
+ to: Math.min(doc.content.size, range.to - 1)
331
+ };
380
332
  }
333
+ /**
334
+ * Returns the top-level block ranges that intersect a document change range.
335
+ * Input `from`/`to` are absolute positions (e.g. from `getChangedRanges`).
336
+ * Returned ranges are content-relative, matching `Node#forEach` offsets.
337
+ */
381
338
  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;
339
+ const ranges = [];
340
+ doc.forEach((node, offset) => {
341
+ const nodeStart = offset;
342
+ const nodeEnd = nodeStart + node.nodeSize;
343
+ const absNodeStart = nodeStart + 1;
344
+ const absNodeEnd = nodeEnd + 1;
345
+ if (absNodeStart < to && absNodeEnd > from) ranges.push({
346
+ from: nodeStart,
347
+ to: nodeEnd
348
+ });
349
+ });
350
+ return ranges;
393
351
  }
352
+ /**
353
+ * Sorts ranges by start position and merges overlapping or adjacent ranges.
354
+ */
394
355
  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;
356
+ if (ranges.length === 0) return [];
357
+ const sorted = [...ranges].sort((a, b) => a.from - b.from);
358
+ const merged = [{ ...sorted[0] }];
359
+ for (let i = 1; i < sorted.length; i += 1) {
360
+ const last = merged[merged.length - 1];
361
+ const current = sorted[i];
362
+ if (current.from <= last.to) last.to = Math.max(last.to, current.to);
363
+ else merged.push({ ...current });
364
+ }
365
+ return merged;
410
366
  }
411
-
412
- // src/placeholder/utils/placeholderStateField.ts
367
+ //#endregion
368
+ //#region src/placeholder/utils/placeholderStateField.ts
369
+ /**
370
+ * Expands a single changed range to the top-level blocks it touches.
371
+ * Also resolves blocks at range boundaries so split/merge edits update
372
+ * adjacent empty nodes (e.g. a new paragraph after Enter).
373
+ */
413
374
  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;
375
+ const ranges = getTopLevelBlocksInRange(doc, change.from, change.to);
376
+ ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, change.from)));
377
+ if (change.to > change.from) ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, Math.min(change.to, doc.content.size + 1) - 1)));
378
+ else if (change.from < doc.content.size + 1) ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, Math.min(change.from + 1, doc.content.size))));
379
+ return ranges;
432
380
  }
381
+ /**
382
+ * Collects content-relative top-level block ranges that need placeholder
383
+ * decorations recomputed after a transaction.
384
+ */
433
385
  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);
386
+ const ranges = [];
387
+ if (tr.docChanged) {
388
+ const changes = getChangedRanges(tr);
389
+ for (const change of changes) ranges.push(...collectBlocksForChange(newState.doc, change.newRange));
390
+ }
391
+ if (tr.selectionSet) {
392
+ ranges.push(toContentRelativeRange(newState.doc, resolveTopLevelRange(newState.doc, tr.mapping.map(oldState.selection.anchor))));
393
+ ranges.push(toContentRelativeRange(newState.doc, resolveTopLevelRange(newState.doc, newState.selection.anchor)));
394
+ }
395
+ return mergeRanges(ranges);
456
396
  }
397
+ /** Clamps a content-relative range to `[0, doc.content.size]`. */
457
398
  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 };
399
+ const clampedFrom = Math.max(0, Math.min(from, doc.content.size));
400
+ return {
401
+ from: clampedFrom,
402
+ to: Math.max(clampedFrom, Math.min(to, doc.content.size))
403
+ };
461
404
  }
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;
405
+ /**
406
+ * Removes and rebuilds placeholder decorations within the given ranges.
407
+ * Only drops decorations fully contained in a range so mapped decorations
408
+ * on neighbouring blocks (e.g. at a block boundary) are kept intact.
409
+ */
410
+ function updateDecorationsInRanges({ decorations, ranges, editor, options, dataAttribute, doc, selection }) {
411
+ let next = decorations;
412
+ for (const range of ranges) {
413
+ const { from, to } = clampRange(range.from, range.to, doc);
414
+ const existing = next.find(from, to).filter((decoration) => decoration.from >= from && decoration.to <= to);
415
+ if (existing.length) next = next.remove(existing);
416
+ const newDecos = scanRangeForDecorations({
417
+ editor,
418
+ options,
419
+ dataAttribute,
420
+ doc,
421
+ selection,
422
+ from,
423
+ to
424
+ });
425
+ if (newDecos.length) next = next.add(doc, newDecos);
426
+ }
427
+ return next;
492
428
  }
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
- };
429
+ /**
430
+ * Creates the incremental `StateField<DecorationSet>` used by the slow path
431
+ * (`showOnlyCurrent: false` or `includeChildren: true`).
432
+ *
433
+ * Decorations are mapped through each transaction and only recomputed for
434
+ * top-level blocks touched by document or selection changes.
435
+ * @param options.editor - The editor instance.
436
+ * @param options.options - The resolved placeholder options.
437
+ * @param options.dataAttribute - The prepared `data-*` attribute name.
438
+ * @returns A ProseMirror state field storing the placeholder decoration set.
439
+ */
440
+ function createPlaceholderStateField({ editor, options, dataAttribute }) {
441
+ return {
442
+ init(_config, state) {
443
+ const decorations = buildPlaceholderDecorations({
444
+ editor,
445
+ options,
446
+ dataAttribute,
447
+ doc: state.doc,
448
+ selection: state.selection
449
+ });
450
+ return decorations !== null && decorations !== void 0 ? decorations : DecorationSet.empty;
451
+ },
452
+ apply(tr, prev, oldState, newState) {
453
+ if (!tr.docChanged && !tr.selectionSet) return prev;
454
+ return updateDecorationsInRanges({
455
+ decorations: prev.map(tr.mapping, tr.doc),
456
+ ranges: collectRescanRanges(tr, oldState, newState),
457
+ editor,
458
+ options,
459
+ dataAttribute,
460
+ doc: newState.doc,
461
+ selection: newState.selection
462
+ });
463
+ }
464
+ };
526
465
  }
527
-
528
- // src/placeholder/utils/preparePlaceholderAttribute.ts
466
+ //#endregion
467
+ //#region src/placeholder/utils/preparePlaceholderAttribute.ts
468
+ /**
469
+ * Prepares the placeholder attribute by ensuring it is properly formatted.
470
+ * @param attr - The placeholder attribute string.
471
+ * @returns The prepared placeholder attribute string.
472
+ */
529
473
  function preparePlaceholderAttribute(attr) {
530
- return attr.replace(/\s+/g, "-").replace(/[^a-zA-Z0-9-]/g, "").replace(/^[0-9-]+/, "").replace(/^-+/, "").toLowerCase();
474
+ return attr.replace(/\s+/g, "-").replace(/[^a-zA-Z0-9-]/g, "").replace(/^[0-9-]+/, "").replace(/^-+/, "").toLowerCase();
531
475
  }
532
-
533
- // src/placeholder/plugins/PlaceholderPlugin.ts
476
+ //#endregion
477
+ //#region src/placeholder/plugins/PlaceholderPlugin.ts
478
+ /**
479
+ * Creates the ProseMirror plugin that renders placeholder decorations.
480
+ * @param options.editor - The editor instance.
481
+ * @param options.options - The resolved placeholder options.
482
+ * @returns The configured placeholder plugin.
483
+ */
534
484
  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
- });
485
+ const dataAttribute = options.dataAttribute ? `data-${preparePlaceholderAttribute(options.dataAttribute)}` : `data-${DEFAULT_DATA_ATTRIBUTE}`;
486
+ const useResolvedPath = options.showOnlyCurrent && !options.includeChildren;
487
+ return new Plugin({
488
+ key: PLUGIN_KEY,
489
+ ...useResolvedPath ? {} : { state: createPlaceholderStateField({
490
+ editor,
491
+ options,
492
+ dataAttribute
493
+ }) },
494
+ props: { decorations: useResolvedPath ? ({ doc, selection }) => buildPlaceholderDecorations({
495
+ editor,
496
+ options,
497
+ dataAttribute,
498
+ doc,
499
+ selection
500
+ }) : (state) => {
501
+ var _PLUGIN_KEY$getState;
502
+ if (options.showOnlyWhenEditable && !editor.isEditable) return DecorationSet.empty;
503
+ return (_PLUGIN_KEY$getState = PLUGIN_KEY.getState(state)) !== null && _PLUGIN_KEY$getState !== void 0 ? _PLUGIN_KEY$getState : DecorationSet.empty;
504
+ } }
505
+ });
552
506
  }
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
- }
507
+ //#endregion
508
+ //#region src/placeholder/placeholder.ts
509
+ /**
510
+ * This extension allows you to add a placeholder to your editor.
511
+ * A placeholder is a text that appears when the editor or a node is empty.
512
+ * @see https://www.tiptap.dev/api/extensions/placeholder
513
+ */
514
+ const Placeholder = Extension.create({
515
+ name: "placeholder",
516
+ addOptions() {
517
+ return {
518
+ emptyEditorClass: "is-editor-empty",
519
+ emptyNodeClass: "is-empty",
520
+ dataAttribute: DEFAULT_DATA_ATTRIBUTE,
521
+ placeholder: "Write something …",
522
+ showOnlyWhenEditable: true,
523
+ showOnlyCurrent: true,
524
+ includeChildren: false
525
+ };
526
+ },
527
+ addProseMirrorPlugins() {
528
+ return [createPlaceholderPlugin({
529
+ editor: this.editor,
530
+ options: this.options
531
+ })];
532
+ }
571
533
  });
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";
534
+ //#endregion
535
+ //#region src/selection/selection.ts
536
+ /**
537
+ * Whether the native browser selection should be cleared on blur and restored on focus.
538
+ * Only applies to non-empty text selections in an editable editor.
539
+ */
577
540
  function shouldSyncDomSelection(state, editor) {
578
- return !state.selection.empty && !isNodeSelection(state.selection) && editor.isEditable;
541
+ return !state.selection.empty && !isNodeSelection(state.selection) && editor.isEditable;
579
542
  }
543
+ /**
544
+ * Whether the selection decoration should be rendered to keep the selection
545
+ * visible while the editor is blurred (and not dragging).
546
+ */
580
547
  function shouldPreserveSelection(state, editor) {
581
- return shouldSyncDomSelection(state, editor) && !editor.isFocused && !editor.view.dragging;
548
+ return shouldSyncDomSelection(state, editor) && !editor.isFocused && !editor.view.dragging;
582
549
  }
583
550
  function clearDomSelection() {
584
- var _a;
585
- (_a = window.getSelection()) == null ? void 0 : _a.removeAllRanges();
551
+ var _window$getSelection;
552
+ (_window$getSelection = window.getSelection()) === null || _window$getSelection === void 0 || _window$getSelection.removeAllRanges();
586
553
  }
554
+ /**
555
+ * Sync the native selection from the editor state.
556
+ * @see https://prosemirror.net/docs/ref/#view.EditorView.focus
557
+ */
587
558
  function restoreDomSelection(view) {
588
- view.focus();
559
+ view.focus();
589
560
  }
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
- }
561
+ /**
562
+ * This extension allows you to add a class to the selected text when the editor is blurred.
563
+ * It clears the native browser selection on blur (so `::selection` styles do not overlap the
564
+ * decoration) and restores it when the editor is focused again.
565
+ * @see https://www.tiptap.dev/api/extensions/selection
566
+ */
567
+ const Selection = Extension.create({
568
+ name: "selection",
569
+ addOptions() {
570
+ return { className: "selection" };
571
+ },
572
+ addProseMirrorPlugins() {
573
+ const { editor, options } = this;
574
+ return [new Plugin({
575
+ key: new PluginKey("selection"),
576
+ props: {
577
+ decorations(state) {
578
+ if (!shouldPreserveSelection(state, editor)) return null;
579
+ return DecorationSet.create(state.doc, [Decoration.inline(state.selection.from, state.selection.to, { class: options.className })]);
580
+ },
581
+ handleDOMEvents: {
582
+ blur(view) {
583
+ if (!shouldSyncDomSelection(view.state, editor)) return false;
584
+ clearDomSelection();
585
+ return false;
586
+ },
587
+ focus(view) {
588
+ if (!shouldSyncDomSelection(view.state, editor)) return false;
589
+ requestAnimationFrame(() => {
590
+ if (!editor.isDestroyed && view.hasFocus()) restoreDomSelection(view);
591
+ });
592
+ return false;
593
+ }
594
+ }
595
+ }
596
+ })];
597
+ }
637
598
  });
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;
599
+ //#endregion
600
+ //#region src/trailing-node/trailing-node.ts
601
+ const skipTrailingNodeMeta = "skipTrailingNode";
602
+ function nodeEqualsType({ types, node }) {
603
+ return node && Array.isArray(types) && types.includes(node.type) || (node === null || node === void 0 ? void 0 : node.type) === types;
648
604
  }
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
- }
605
+ /**
606
+ * This extension allows you to add an extra node at the end of the document.
607
+ * @see https://www.tiptap.dev/api/extensions/trailing-node
608
+ */
609
+ const TrailingNode = Extension.create({
610
+ name: "trailingNode",
611
+ addOptions() {
612
+ return {
613
+ node: void 0,
614
+ notAfter: []
615
+ };
616
+ },
617
+ addProseMirrorPlugins() {
618
+ var _this$editor$schema$t;
619
+ const plugin = new PluginKey(this.name);
620
+ 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";
621
+ const disabledNodes = Object.entries(this.editor.schema.nodes).map(([, value]) => value).filter((node) => (this.options.notAfter || []).concat(defaultNode).includes(node.name));
622
+ return [new Plugin({
623
+ key: plugin,
624
+ appendTransaction: (transactions, __, state) => {
625
+ const { doc, tr, schema } = state;
626
+ const shouldInsertNodeAtEnd = plugin.getState(state);
627
+ const endPosition = doc.content.size;
628
+ const type = schema.nodes[defaultNode];
629
+ if (transactions.some((transaction) => transaction.getMeta("skipTrailingNode"))) return;
630
+ if (!shouldInsertNodeAtEnd) return;
631
+ return tr.insert(endPosition, type.create());
632
+ },
633
+ state: {
634
+ init: (_, state) => {
635
+ const lastNode = state.tr.doc.lastChild;
636
+ return !nodeEqualsType({
637
+ node: lastNode,
638
+ types: disabledNodes
639
+ });
640
+ },
641
+ apply: (tr, value) => {
642
+ if (!tr.docChanged) return value;
643
+ if (tr.getMeta("__uniqueIDTransaction")) return value;
644
+ const lastNode = tr.doc.lastChild;
645
+ return !nodeEqualsType({
646
+ node: lastNode,
647
+ types: disabledNodes
648
+ });
649
+ }
650
+ }
651
+ })];
652
+ }
697
653
  });
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
- }
654
+ //#endregion
655
+ //#region src/undo-redo/undo-redo.ts
656
+ /**
657
+ * This extension allows you to undo and redo recent changes.
658
+ * @see https://www.tiptap.dev/api/extensions/undo-redo
659
+ *
660
+ * **Important**: If the `@tiptap/extension-collaboration` package is used, make sure to remove
661
+ * the `undo-redo` extension, as it is not compatible with the `collaboration` extension.
662
+ *
663
+ * `@tiptap/extension-collaboration` uses its own history implementation.
664
+ */
665
+ const UndoRedo = Extension.create({
666
+ name: "undoRedo",
667
+ addOptions() {
668
+ return {
669
+ depth: 100,
670
+ newGroupDelay: 500
671
+ };
672
+ },
673
+ addCommands() {
674
+ return {
675
+ undo: () => ({ state, dispatch }) => {
676
+ return undo(state, dispatch);
677
+ },
678
+ redo: () => ({ state, dispatch }) => {
679
+ return redo(state, dispatch);
680
+ }
681
+ };
682
+ },
683
+ addProseMirrorPlugins() {
684
+ return [history(this.options)];
685
+ },
686
+ addKeyboardShortcuts() {
687
+ return {
688
+ "Mod-z": () => this.editor.commands.undo(),
689
+ "Shift-Mod-z": () => this.editor.commands.redo(),
690
+ "Mod-y": () => this.editor.commands.redo(),
691
+ "Mod-я": () => this.editor.commands.undo(),
692
+ "Shift-Mod-я": () => this.editor.commands.redo()
693
+ };
694
+ }
733
695
  });
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
- };
696
+ //#endregion
697
+ export { CharacterCount, DEFAULT_DATA_ATTRIBUTE, Dropcursor, Focus, Gapcursor, PLUGIN_KEY, Placeholder, Selection, TrailingNode, UndoRedo, preparePlaceholderAttribute, skipTrailingNodeMeta };
698
+
748
699
  //# sourceMappingURL=index.js.map