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