@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,374 +1,368 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/placeholder/utils/buildPlaceholderDecorations.ts
|
|
46
40
|
function resolveEmptyNodeClass(emptyNodeClass, props) {
|
|
47
|
-
|
|
41
|
+
return typeof emptyNodeClass === "function" ? emptyNodeClass(props) : emptyNodeClass;
|
|
48
42
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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
|
-
|
|
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
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
})
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
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
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
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
|
-
|
|
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
|
-
|
|
305
|
+
return attr.replace(/\s+/g, "-").replace(/[^a-zA-Z0-9-]/g, "").replace(/^[0-9-]+/, "").replace(/^-+/, "").toLowerCase();
|
|
327
306
|
}
|
|
328
|
-
|
|
329
|
-
|
|
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
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
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
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
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
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
Placeholder,
|
|
372
|
-
preparePlaceholderAttribute
|
|
373
|
-
};
|
|
365
|
+
//#endregion
|
|
366
|
+
export { DEFAULT_DATA_ATTRIBUTE, PLUGIN_KEY, Placeholder, preparePlaceholderAttribute };
|
|
367
|
+
|
|
374
368
|
//# sourceMappingURL=index.js.map
|