@tiptap/extensions 3.30.2 → 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
@@ -1,374 +1,368 @@
1
- // src/placeholder/constants.ts
2
- import { PluginKey } from "@tiptap/pm/state";
3
- var DEFAULT_DATA_ATTRIBUTE = "placeholder";
4
- var PLUGIN_KEY = new PluginKey("tiptap__placeholder");
5
-
6
- // src/placeholder/placeholder.ts
7
- import { Extension } from "@tiptap/core";
8
-
9
- // src/placeholder/plugins/PlaceholderPlugin.ts
10
- import { Plugin } from "@tiptap/pm/state";
11
- import { DecorationSet as DecorationSet3 } from "@tiptap/pm/view";
12
-
13
- // src/placeholder/utils/buildPlaceholderDecorations.ts
14
- import { isNodeEmpty } from "@tiptap/core";
15
- import { DecorationSet } from "@tiptap/pm/view";
16
-
17
- // src/placeholder/utils/createPlaceholderDecoration.ts
18
- import { Decoration } from "@tiptap/pm/view";
1
+ import { Plugin, PluginKey } from "@tiptap/pm/state";
2
+ import { Extension, getChangedRanges, isNodeEmpty } from "@tiptap/core";
3
+ import { Decoration, DecorationSet } from "@tiptap/pm/view";
4
+ //#region src/placeholder/constants.ts
5
+ /** The default data attribute label */
6
+ const DEFAULT_DATA_ATTRIBUTE = "placeholder";
7
+ /** The plugin key used to store and read the placeholder decoration set */
8
+ const PLUGIN_KEY = new PluginKey("tiptap__placeholder");
9
+ //#endregion
10
+ //#region src/placeholder/utils/createPlaceholderDecoration.ts
11
+ /**
12
+ * Creates a ProseMirror node decoration that applies a placeholder
13
+ * CSS class and data attribute to an empty node.
14
+ * @param options.editor - The editor instance
15
+ * @param options.pos - The position of the node in the document
16
+ * @param options.node - The ProseMirror node
17
+ * @param options.isEmptyDoc - Whether the entire document is empty
18
+ * @param options.hasAnchor - Whether the selection anchor is within the node
19
+ * @param options.dataAttribute - The data attribute name (e.g. `data-placeholder`)
20
+ * @param options.classes - CSS classes for empty nodes and the empty editor
21
+ * @param options.placeholder - The placeholder text or a function that returns it
22
+ * @returns A ProseMirror node decoration with placeholder classes and data attribute
23
+ */
19
24
  function createPlaceholderDecoration(options) {
20
- const {
21
- editor,
22
- placeholder,
23
- dataAttribute,
24
- pos,
25
- node,
26
- isEmptyDoc,
27
- hasAnchor,
28
- classes: { emptyNode, emptyEditor }
29
- } = options;
30
- const classes = [emptyNode];
31
- if (isEmptyDoc) {
32
- classes.push(emptyEditor);
33
- }
34
- return Decoration.node(pos, pos + node.nodeSize, {
35
- class: classes.join(" "),
36
- [dataAttribute]: typeof placeholder === "function" ? placeholder({
37
- editor,
38
- node,
39
- pos,
40
- hasAnchor
41
- }) : placeholder
42
- });
25
+ const { editor, placeholder, dataAttribute, pos, node, isEmptyDoc, hasAnchor, classes: { emptyNode, emptyEditor } } = options;
26
+ const classes = [emptyNode];
27
+ if (isEmptyDoc) classes.push(emptyEditor);
28
+ return Decoration.node(pos, pos + node.nodeSize, {
29
+ class: classes.join(" "),
30
+ [dataAttribute]: typeof placeholder === "function" ? placeholder({
31
+ editor,
32
+ node,
33
+ pos,
34
+ hasAnchor
35
+ }) : placeholder
36
+ });
43
37
  }
44
-
45
- // src/placeholder/utils/buildPlaceholderDecorations.ts
38
+ //#endregion
39
+ //#region src/placeholder/utils/buildPlaceholderDecorations.ts
46
40
  function resolveEmptyNodeClass(emptyNodeClass, props) {
47
- return typeof emptyNodeClass === "function" ? emptyNodeClass(props) : emptyNodeClass;
41
+ return typeof emptyNodeClass === "function" ? emptyNodeClass(props) : emptyNodeClass;
48
42
  }
49
- function scanRangeForDecorations({
50
- editor,
51
- options,
52
- dataAttribute,
53
- doc,
54
- selection,
55
- from,
56
- to
57
- }) {
58
- const { anchor } = selection;
59
- const decorations = [];
60
- const isEmptyDoc = editor.isEmpty;
61
- doc.nodesBetween(from, to, (node, pos) => {
62
- const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
63
- const isEmpty = !node.isLeaf && isNodeEmpty(node);
64
- if (!node.type.isTextblock) {
65
- return options.includeChildren;
66
- }
67
- if ((hasAnchor || !options.showOnlyCurrent) && isEmpty) {
68
- decorations.push(
69
- createPlaceholderDecoration({
70
- editor,
71
- isEmptyDoc,
72
- dataAttribute,
73
- hasAnchor,
74
- placeholder: options.placeholder,
75
- classes: {
76
- emptyEditor: options.emptyEditorClass,
77
- emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
78
- editor,
79
- node,
80
- pos,
81
- hasAnchor
82
- })
83
- },
84
- node,
85
- pos
86
- })
87
- );
88
- }
89
- return options.includeChildren;
90
- });
91
- return decorations;
43
+ /**
44
+ * Scans a document range for empty textblocks that should receive placeholder
45
+ * decorations. Used by the slow path and incremental state updates.
46
+ */
47
+ function scanRangeForDecorations({ editor, options, dataAttribute, doc, selection, from, to }) {
48
+ const { anchor } = selection;
49
+ const decorations = [];
50
+ const isEmptyDoc = editor.isEmpty;
51
+ doc.nodesBetween(from, to, (node, pos) => {
52
+ const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
53
+ const isEmpty = !node.isLeaf && isNodeEmpty(node);
54
+ if (!node.type.isTextblock) return options.includeChildren;
55
+ if ((hasAnchor || !options.showOnlyCurrent) && isEmpty) decorations.push(createPlaceholderDecoration({
56
+ editor,
57
+ isEmptyDoc,
58
+ dataAttribute,
59
+ hasAnchor,
60
+ placeholder: options.placeholder,
61
+ classes: {
62
+ emptyEditor: options.emptyEditorClass,
63
+ emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
64
+ editor,
65
+ node,
66
+ pos,
67
+ hasAnchor
68
+ })
69
+ },
70
+ node,
71
+ pos
72
+ }));
73
+ return options.includeChildren;
74
+ });
75
+ return decorations;
92
76
  }
93
- function buildPlaceholderDecorations({
94
- editor,
95
- options,
96
- dataAttribute,
97
- doc,
98
- selection
99
- }) {
100
- const active = editor.isEditable || !options.showOnlyWhenEditable;
101
- if (!active) {
102
- return null;
103
- }
104
- const { anchor } = selection;
105
- const decorations = [];
106
- const isEmptyDoc = editor.isEmpty;
107
- const useResolvedPath = options.showOnlyCurrent && !options.includeChildren;
108
- if (useResolvedPath) {
109
- const resolved = doc.resolve(anchor);
110
- const node = resolved.depth > 0 ? resolved.node(1) : resolved.nodeAfter;
111
- const nodeStart = resolved.depth > 0 ? resolved.before(1) : anchor;
112
- if (node && node.type.isTextblock && isNodeEmpty(node)) {
113
- const hasAnchor = anchor >= nodeStart && anchor <= nodeStart + node.nodeSize;
114
- decorations.push(
115
- createPlaceholderDecoration({
116
- editor,
117
- isEmptyDoc,
118
- dataAttribute,
119
- hasAnchor,
120
- placeholder: options.placeholder,
121
- classes: {
122
- emptyEditor: options.emptyEditorClass,
123
- emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
124
- editor,
125
- node,
126
- pos: nodeStart,
127
- hasAnchor
128
- })
129
- },
130
- node,
131
- pos: nodeStart
132
- })
133
- );
134
- }
135
- } else {
136
- decorations.push(
137
- ...scanRangeForDecorations({
138
- editor,
139
- options,
140
- dataAttribute,
141
- doc,
142
- selection,
143
- from: 0,
144
- to: doc.content.size
145
- })
146
- );
147
- }
148
- return DecorationSet.create(doc, decorations);
77
+ /**
78
+ * Builds the placeholder decorations for the current document state.
79
+ * @param options.editor - The editor instance.
80
+ * @param options.options - The resolved placeholder options.
81
+ * @param options.dataAttribute - The prepared `data-*` attribute name.
82
+ * @param options.doc - The current document node.
83
+ * @param options.selection - The current selection.
84
+ * @returns A decoration set, or `null` when no placeholders should be shown.
85
+ */
86
+ function buildPlaceholderDecorations({ editor, options, dataAttribute, doc, selection }) {
87
+ if (!(editor.isEditable || !options.showOnlyWhenEditable)) return null;
88
+ const { anchor } = selection;
89
+ const decorations = [];
90
+ const isEmptyDoc = editor.isEmpty;
91
+ if (options.showOnlyCurrent && !options.includeChildren) {
92
+ const resolved = doc.resolve(anchor);
93
+ const node = resolved.depth > 0 ? resolved.node(1) : resolved.nodeAfter;
94
+ const nodeStart = resolved.depth > 0 ? resolved.before(1) : anchor;
95
+ if (node && node.type.isTextblock && isNodeEmpty(node)) {
96
+ const hasAnchor = anchor >= nodeStart && anchor <= nodeStart + node.nodeSize;
97
+ decorations.push(createPlaceholderDecoration({
98
+ editor,
99
+ isEmptyDoc,
100
+ dataAttribute,
101
+ hasAnchor,
102
+ placeholder: options.placeholder,
103
+ classes: {
104
+ emptyEditor: options.emptyEditorClass,
105
+ emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
106
+ editor,
107
+ node,
108
+ pos: nodeStart,
109
+ hasAnchor
110
+ })
111
+ },
112
+ node,
113
+ pos: nodeStart
114
+ }));
115
+ }
116
+ } else decorations.push(...scanRangeForDecorations({
117
+ editor,
118
+ options,
119
+ dataAttribute,
120
+ doc,
121
+ selection,
122
+ from: 0,
123
+ to: doc.content.size
124
+ }));
125
+ return DecorationSet.create(doc, decorations);
149
126
  }
150
-
151
- // src/placeholder/utils/placeholderStateField.ts
152
- import { getChangedRanges } from "@tiptap/core";
153
- import { DecorationSet as DecorationSet2 } from "@tiptap/pm/view";
154
-
155
- // src/placeholder/utils/resolveTopLevelRange.ts
127
+ //#endregion
128
+ //#region src/placeholder/utils/resolveTopLevelRange.ts
129
+ /**
130
+ * Resolves a document position to the `[from, to)` range of its containing
131
+ * top-level block node in absolute document positions.
132
+ */
156
133
  function resolveTopLevelRange(doc, pos) {
157
- var _a;
158
- const resolved = doc.resolve(pos);
159
- if (resolved.depth === 0) {
160
- const node2 = (_a = resolved.nodeAfter) != null ? _a : resolved.nodeBefore;
161
- if (!node2) {
162
- return { from: pos, to: pos };
163
- }
164
- const nodePos = resolved.nodeAfter ? pos : pos - node2.nodeSize;
165
- return { from: nodePos, to: nodePos + node2.nodeSize };
166
- }
167
- const topLevelPos = resolved.before(1);
168
- const node = resolved.node(1);
169
- return { from: topLevelPos, to: topLevelPos + node.nodeSize };
134
+ const resolved = doc.resolve(pos);
135
+ if (resolved.depth === 0) {
136
+ var _resolved$nodeAfter;
137
+ const node = (_resolved$nodeAfter = resolved.nodeAfter) !== null && _resolved$nodeAfter !== void 0 ? _resolved$nodeAfter : resolved.nodeBefore;
138
+ if (!node) return {
139
+ from: pos,
140
+ to: pos
141
+ };
142
+ const nodePos = resolved.nodeAfter ? pos : pos - node.nodeSize;
143
+ return {
144
+ from: nodePos,
145
+ to: nodePos + node.nodeSize
146
+ };
147
+ }
148
+ const topLevelPos = resolved.before(1);
149
+ return {
150
+ from: topLevelPos,
151
+ to: topLevelPos + resolved.node(1).nodeSize
152
+ };
170
153
  }
154
+ /**
155
+ * Converts an absolute document range to content-relative positions used by
156
+ * `Node#nodesBetween` and `Node#forEach` offsets.
157
+ */
171
158
  function toContentRelativeRange(doc, range) {
172
- return {
173
- from: Math.max(0, range.from - 1),
174
- to: Math.min(doc.content.size, range.to - 1)
175
- };
159
+ return {
160
+ from: Math.max(0, range.from - 1),
161
+ to: Math.min(doc.content.size, range.to - 1)
162
+ };
176
163
  }
164
+ /**
165
+ * Returns the top-level block ranges that intersect a document change range.
166
+ * Input `from`/`to` are absolute positions (e.g. from `getChangedRanges`).
167
+ * Returned ranges are content-relative, matching `Node#forEach` offsets.
168
+ */
177
169
  function getTopLevelBlocksInRange(doc, from, to) {
178
- const ranges = [];
179
- doc.forEach((node, offset) => {
180
- const nodeStart = offset;
181
- const nodeEnd = nodeStart + node.nodeSize;
182
- const absNodeStart = nodeStart + 1;
183
- const absNodeEnd = nodeEnd + 1;
184
- if (absNodeStart < to && absNodeEnd > from) {
185
- ranges.push({ from: nodeStart, to: nodeEnd });
186
- }
187
- });
188
- return ranges;
170
+ const ranges = [];
171
+ doc.forEach((node, offset) => {
172
+ const nodeStart = offset;
173
+ const nodeEnd = nodeStart + node.nodeSize;
174
+ const absNodeStart = nodeStart + 1;
175
+ const absNodeEnd = nodeEnd + 1;
176
+ if (absNodeStart < to && absNodeEnd > from) ranges.push({
177
+ from: nodeStart,
178
+ to: nodeEnd
179
+ });
180
+ });
181
+ return ranges;
189
182
  }
183
+ /**
184
+ * Sorts ranges by start position and merges overlapping or adjacent ranges.
185
+ */
190
186
  function mergeRanges(ranges) {
191
- if (ranges.length === 0) {
192
- return [];
193
- }
194
- const sorted = [...ranges].sort((a, b) => a.from - b.from);
195
- const merged = [{ ...sorted[0] }];
196
- for (let i = 1; i < sorted.length; i += 1) {
197
- const last = merged[merged.length - 1];
198
- const current = sorted[i];
199
- if (current.from <= last.to) {
200
- last.to = Math.max(last.to, current.to);
201
- } else {
202
- merged.push({ ...current });
203
- }
204
- }
205
- return merged;
187
+ if (ranges.length === 0) return [];
188
+ const sorted = [...ranges].sort((a, b) => a.from - b.from);
189
+ const merged = [{ ...sorted[0] }];
190
+ for (let i = 1; i < sorted.length; i += 1) {
191
+ const last = merged[merged.length - 1];
192
+ const current = sorted[i];
193
+ if (current.from <= last.to) last.to = Math.max(last.to, current.to);
194
+ else merged.push({ ...current });
195
+ }
196
+ return merged;
206
197
  }
207
-
208
- // src/placeholder/utils/placeholderStateField.ts
198
+ //#endregion
199
+ //#region src/placeholder/utils/placeholderStateField.ts
200
+ /**
201
+ * Expands a single changed range to the top-level blocks it touches.
202
+ * Also resolves blocks at range boundaries so split/merge edits update
203
+ * adjacent empty nodes (e.g. a new paragraph after Enter).
204
+ */
209
205
  function collectBlocksForChange(doc, change) {
210
- const ranges = getTopLevelBlocksInRange(doc, change.from, change.to);
211
- ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, change.from)));
212
- if (change.to > change.from) {
213
- ranges.push(
214
- toContentRelativeRange(
215
- doc,
216
- resolveTopLevelRange(doc, Math.min(change.to, doc.content.size + 1) - 1)
217
- )
218
- );
219
- } else if (change.from < doc.content.size + 1) {
220
- ranges.push(
221
- toContentRelativeRange(
222
- doc,
223
- resolveTopLevelRange(doc, Math.min(change.from + 1, doc.content.size))
224
- )
225
- );
226
- }
227
- return ranges;
206
+ const ranges = getTopLevelBlocksInRange(doc, change.from, change.to);
207
+ ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, change.from)));
208
+ if (change.to > change.from) ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, Math.min(change.to, doc.content.size + 1) - 1)));
209
+ else if (change.from < doc.content.size + 1) ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, Math.min(change.from + 1, doc.content.size))));
210
+ return ranges;
228
211
  }
212
+ /**
213
+ * Collects content-relative top-level block ranges that need placeholder
214
+ * decorations recomputed after a transaction.
215
+ */
229
216
  function collectRescanRanges(tr, oldState, newState) {
230
- const ranges = [];
231
- if (tr.docChanged) {
232
- const changes = getChangedRanges(tr);
233
- for (const change of changes) {
234
- ranges.push(...collectBlocksForChange(newState.doc, change.newRange));
235
- }
236
- }
237
- if (tr.selectionSet) {
238
- ranges.push(
239
- toContentRelativeRange(
240
- newState.doc,
241
- resolveTopLevelRange(newState.doc, tr.mapping.map(oldState.selection.anchor))
242
- )
243
- );
244
- ranges.push(
245
- toContentRelativeRange(
246
- newState.doc,
247
- resolveTopLevelRange(newState.doc, newState.selection.anchor)
248
- )
249
- );
250
- }
251
- return mergeRanges(ranges);
217
+ const ranges = [];
218
+ if (tr.docChanged) {
219
+ const changes = getChangedRanges(tr);
220
+ for (const change of changes) ranges.push(...collectBlocksForChange(newState.doc, change.newRange));
221
+ }
222
+ if (tr.selectionSet) {
223
+ ranges.push(toContentRelativeRange(newState.doc, resolveTopLevelRange(newState.doc, tr.mapping.map(oldState.selection.anchor))));
224
+ ranges.push(toContentRelativeRange(newState.doc, resolveTopLevelRange(newState.doc, newState.selection.anchor)));
225
+ }
226
+ return mergeRanges(ranges);
252
227
  }
228
+ /** Clamps a content-relative range to `[0, doc.content.size]`. */
253
229
  function clampRange(from, to, doc) {
254
- const clampedFrom = Math.max(0, Math.min(from, doc.content.size));
255
- const clampedTo = Math.max(clampedFrom, Math.min(to, doc.content.size));
256
- return { from: clampedFrom, to: clampedTo };
230
+ const clampedFrom = Math.max(0, Math.min(from, doc.content.size));
231
+ return {
232
+ from: clampedFrom,
233
+ to: Math.max(clampedFrom, Math.min(to, doc.content.size))
234
+ };
257
235
  }
258
- function updateDecorationsInRanges({
259
- decorations,
260
- ranges,
261
- editor,
262
- options,
263
- dataAttribute,
264
- doc,
265
- selection
266
- }) {
267
- let next = decorations;
268
- for (const range of ranges) {
269
- const { from, to } = clampRange(range.from, range.to, doc);
270
- const existing = next.find(from, to).filter((decoration) => decoration.from >= from && decoration.to <= to);
271
- if (existing.length) {
272
- next = next.remove(existing);
273
- }
274
- const newDecos = scanRangeForDecorations({
275
- editor,
276
- options,
277
- dataAttribute,
278
- doc,
279
- selection,
280
- from,
281
- to
282
- });
283
- if (newDecos.length) {
284
- next = next.add(doc, newDecos);
285
- }
286
- }
287
- return next;
236
+ /**
237
+ * Removes and rebuilds placeholder decorations within the given ranges.
238
+ * Only drops decorations fully contained in a range so mapped decorations
239
+ * on neighbouring blocks (e.g. at a block boundary) are kept intact.
240
+ */
241
+ function updateDecorationsInRanges({ decorations, ranges, editor, options, dataAttribute, doc, selection }) {
242
+ let next = decorations;
243
+ for (const range of ranges) {
244
+ const { from, to } = clampRange(range.from, range.to, doc);
245
+ const existing = next.find(from, to).filter((decoration) => decoration.from >= from && decoration.to <= to);
246
+ if (existing.length) next = next.remove(existing);
247
+ const newDecos = scanRangeForDecorations({
248
+ editor,
249
+ options,
250
+ dataAttribute,
251
+ doc,
252
+ selection,
253
+ from,
254
+ to
255
+ });
256
+ if (newDecos.length) next = next.add(doc, newDecos);
257
+ }
258
+ return next;
288
259
  }
289
- function createPlaceholderStateField({
290
- editor,
291
- options,
292
- dataAttribute
293
- }) {
294
- return {
295
- init(_config, state) {
296
- const decorations = buildPlaceholderDecorations({
297
- editor,
298
- options,
299
- dataAttribute,
300
- doc: state.doc,
301
- selection: state.selection
302
- });
303
- return decorations != null ? decorations : DecorationSet2.empty;
304
- },
305
- apply(tr, prev, oldState, newState) {
306
- if (!tr.docChanged && !tr.selectionSet) {
307
- return prev;
308
- }
309
- const mapped = prev.map(tr.mapping, tr.doc);
310
- const ranges = collectRescanRanges(tr, oldState, newState);
311
- return updateDecorationsInRanges({
312
- decorations: mapped,
313
- ranges,
314
- editor,
315
- options,
316
- dataAttribute,
317
- doc: newState.doc,
318
- selection: newState.selection
319
- });
320
- }
321
- };
260
+ /**
261
+ * Creates the incremental `StateField<DecorationSet>` used by the slow path
262
+ * (`showOnlyCurrent: false` or `includeChildren: true`).
263
+ *
264
+ * Decorations are mapped through each transaction and only recomputed for
265
+ * top-level blocks touched by document or selection changes.
266
+ * @param options.editor - The editor instance.
267
+ * @param options.options - The resolved placeholder options.
268
+ * @param options.dataAttribute - The prepared `data-*` attribute name.
269
+ * @returns A ProseMirror state field storing the placeholder decoration set.
270
+ */
271
+ function createPlaceholderStateField({ editor, options, dataAttribute }) {
272
+ return {
273
+ init(_config, state) {
274
+ const decorations = buildPlaceholderDecorations({
275
+ editor,
276
+ options,
277
+ dataAttribute,
278
+ doc: state.doc,
279
+ selection: state.selection
280
+ });
281
+ return decorations !== null && decorations !== void 0 ? decorations : DecorationSet.empty;
282
+ },
283
+ apply(tr, prev, oldState, newState) {
284
+ if (!tr.docChanged && !tr.selectionSet) return prev;
285
+ return updateDecorationsInRanges({
286
+ decorations: prev.map(tr.mapping, tr.doc),
287
+ ranges: collectRescanRanges(tr, oldState, newState),
288
+ editor,
289
+ options,
290
+ dataAttribute,
291
+ doc: newState.doc,
292
+ selection: newState.selection
293
+ });
294
+ }
295
+ };
322
296
  }
323
-
324
- // src/placeholder/utils/preparePlaceholderAttribute.ts
297
+ //#endregion
298
+ //#region src/placeholder/utils/preparePlaceholderAttribute.ts
299
+ /**
300
+ * Prepares the placeholder attribute by ensuring it is properly formatted.
301
+ * @param attr - The placeholder attribute string.
302
+ * @returns The prepared placeholder attribute string.
303
+ */
325
304
  function preparePlaceholderAttribute(attr) {
326
- return attr.replace(/\s+/g, "-").replace(/[^a-zA-Z0-9-]/g, "").replace(/^[0-9-]+/, "").replace(/^-+/, "").toLowerCase();
305
+ return attr.replace(/\s+/g, "-").replace(/[^a-zA-Z0-9-]/g, "").replace(/^[0-9-]+/, "").replace(/^-+/, "").toLowerCase();
327
306
  }
328
-
329
- // src/placeholder/plugins/PlaceholderPlugin.ts
307
+ //#endregion
308
+ //#region src/placeholder/plugins/PlaceholderPlugin.ts
309
+ /**
310
+ * Creates the ProseMirror plugin that renders placeholder decorations.
311
+ * @param options.editor - The editor instance.
312
+ * @param options.options - The resolved placeholder options.
313
+ * @returns The configured placeholder plugin.
314
+ */
330
315
  function createPlaceholderPlugin({ editor, options }) {
331
- const dataAttribute = options.dataAttribute ? `data-${preparePlaceholderAttribute(options.dataAttribute)}` : `data-${DEFAULT_DATA_ATTRIBUTE}`;
332
- const useResolvedPath = options.showOnlyCurrent && !options.includeChildren;
333
- return new Plugin({
334
- key: PLUGIN_KEY,
335
- ...useResolvedPath ? {} : {
336
- state: createPlaceholderStateField({ editor, options, dataAttribute })
337
- },
338
- props: {
339
- decorations: useResolvedPath ? ({ doc, selection }) => buildPlaceholderDecorations({ editor, options, dataAttribute, doc, selection }) : (state) => {
340
- var _a;
341
- if (options.showOnlyWhenEditable && !editor.isEditable) {
342
- return DecorationSet3.empty;
343
- }
344
- return (_a = PLUGIN_KEY.getState(state)) != null ? _a : DecorationSet3.empty;
345
- }
346
- }
347
- });
316
+ const dataAttribute = options.dataAttribute ? `data-${preparePlaceholderAttribute(options.dataAttribute)}` : `data-${DEFAULT_DATA_ATTRIBUTE}`;
317
+ const useResolvedPath = options.showOnlyCurrent && !options.includeChildren;
318
+ return new Plugin({
319
+ key: PLUGIN_KEY,
320
+ ...useResolvedPath ? {} : { state: createPlaceholderStateField({
321
+ editor,
322
+ options,
323
+ dataAttribute
324
+ }) },
325
+ props: { decorations: useResolvedPath ? ({ doc, selection }) => buildPlaceholderDecorations({
326
+ editor,
327
+ options,
328
+ dataAttribute,
329
+ doc,
330
+ selection
331
+ }) : (state) => {
332
+ var _PLUGIN_KEY$getState;
333
+ if (options.showOnlyWhenEditable && !editor.isEditable) return DecorationSet.empty;
334
+ return (_PLUGIN_KEY$getState = PLUGIN_KEY.getState(state)) !== null && _PLUGIN_KEY$getState !== void 0 ? _PLUGIN_KEY$getState : DecorationSet.empty;
335
+ } }
336
+ });
348
337
  }
349
-
350
- // src/placeholder/placeholder.ts
351
- var Placeholder = Extension.create({
352
- name: "placeholder",
353
- addOptions() {
354
- return {
355
- emptyEditorClass: "is-editor-empty",
356
- emptyNodeClass: "is-empty",
357
- dataAttribute: DEFAULT_DATA_ATTRIBUTE,
358
- placeholder: "Write something \u2026",
359
- showOnlyWhenEditable: true,
360
- showOnlyCurrent: true,
361
- includeChildren: false
362
- };
363
- },
364
- addProseMirrorPlugins() {
365
- return [createPlaceholderPlugin({ editor: this.editor, options: this.options })];
366
- }
338
+ //#endregion
339
+ //#region src/placeholder/placeholder.ts
340
+ /**
341
+ * This extension allows you to add a placeholder to your editor.
342
+ * A placeholder is a text that appears when the editor or a node is empty.
343
+ * @see https://www.tiptap.dev/api/extensions/placeholder
344
+ */
345
+ const Placeholder = Extension.create({
346
+ name: "placeholder",
347
+ addOptions() {
348
+ return {
349
+ emptyEditorClass: "is-editor-empty",
350
+ emptyNodeClass: "is-empty",
351
+ dataAttribute: DEFAULT_DATA_ATTRIBUTE,
352
+ placeholder: "Write something …",
353
+ showOnlyWhenEditable: true,
354
+ showOnlyCurrent: true,
355
+ includeChildren: false
356
+ };
357
+ },
358
+ addProseMirrorPlugins() {
359
+ return [createPlaceholderPlugin({
360
+ editor: this.editor,
361
+ options: this.options
362
+ })];
363
+ }
367
364
  });
368
- export {
369
- DEFAULT_DATA_ATTRIBUTE,
370
- PLUGIN_KEY,
371
- Placeholder,
372
- preparePlaceholderAttribute
373
- };
365
+ //#endregion
366
+ export { DEFAULT_DATA_ATTRIBUTE, PLUGIN_KEY, Placeholder, preparePlaceholderAttribute };
367
+
374
368
  //# sourceMappingURL=index.js.map