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