@tiptap/extension-details 2.24.2 → 3.0.0-beta.11
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/LICENSE.md +21 -0
- package/dist/index.cjs +372 -370
- package/dist/index.cjs.map +1 -1
- package/dist/{details.d.ts → index.d.cts} +5 -3
- package/dist/index.d.ts +35 -4
- package/dist/index.js +347 -367
- package/dist/index.js.map +1 -1
- package/package.json +13 -9
- package/src/details.ts +95 -120
- package/src/helpers/findClosestVisibleNode.ts +14 -8
- package/src/helpers/isNodeVisible.ts +1 -1
- package/src/helpers/setGapCursor.ts +7 -13
- package/dist/details.d.ts.map +0 -1
- package/dist/helpers/findClosestVisibleNode.d.ts +0 -9
- package/dist/helpers/findClosestVisibleNode.d.ts.map +0 -1
- package/dist/helpers/isNodeVisible.d.ts +0 -3
- package/dist/helpers/isNodeVisible.d.ts.map +0 -1
- package/dist/helpers/setGapCursor.d.ts +0 -3
- package/dist/helpers/setGapCursor.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.umd.js +0 -388
- package/dist/index.umd.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,381 +1,361 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
// src/details.ts
|
|
2
|
+
import { defaultBlockAt, findChildren as findChildren2, findParentNode as findParentNode2, isActive, mergeAttributes, Node } from "@tiptap/core";
|
|
3
|
+
import { Plugin, PluginKey, Selection, TextSelection } from "@tiptap/pm/state";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
// src/helpers/isNodeVisible.ts
|
|
6
|
+
var isNodeVisible = (position, editor) => {
|
|
7
|
+
const node = editor.view.domAtPos(position).node;
|
|
8
|
+
const isOpen = node.offsetParent !== null;
|
|
9
|
+
return isOpen;
|
|
9
10
|
};
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
// src/helpers/findClosestVisibleNode.ts
|
|
13
|
+
var findClosestVisibleNode = ($pos, predicate, editor) => {
|
|
14
|
+
for (let i = $pos.depth; i > 0; i -= 1) {
|
|
15
|
+
const node = $pos.node(i);
|
|
16
|
+
const match = predicate(node);
|
|
17
|
+
const isVisible = isNodeVisible($pos.start(i), editor);
|
|
18
|
+
if (match && isVisible) {
|
|
19
|
+
return {
|
|
20
|
+
pos: i > 0 ? $pos.before(i) : 0,
|
|
21
|
+
start: $pos.start(i),
|
|
22
|
+
depth: i,
|
|
23
|
+
node
|
|
24
|
+
};
|
|
24
25
|
}
|
|
26
|
+
}
|
|
25
27
|
};
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
29
|
+
// src/helpers/setGapCursor.ts
|
|
30
|
+
import { findChildren, findParentNode } from "@tiptap/core";
|
|
31
|
+
import { GapCursor } from "@tiptap/pm/gapcursor";
|
|
32
|
+
var setGapCursor = (editor, direction) => {
|
|
33
|
+
const { state, view, extensionManager } = editor;
|
|
34
|
+
const { schema, selection } = state;
|
|
35
|
+
const { empty, $anchor } = selection;
|
|
36
|
+
const hasGapCursorExtension = !!extensionManager.extensions.find((extension) => extension.name === "gapCursor");
|
|
37
|
+
if (!empty || $anchor.parent.type !== schema.nodes.detailsSummary || !hasGapCursorExtension) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if (direction === "right" && $anchor.parentOffset !== $anchor.parent.nodeSize - 2) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
const details = findParentNode((node) => node.type === schema.nodes.details)(selection);
|
|
44
|
+
if (!details) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
const detailsContent = findChildren(details.node, (node) => node.type === schema.nodes.detailsContent);
|
|
48
|
+
if (!detailsContent.length) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
const isOpen = isNodeVisible(details.start + detailsContent[0].pos + 1, editor);
|
|
52
|
+
if (isOpen) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const $position = state.doc.resolve(details.pos + details.node.nodeSize);
|
|
56
|
+
const $validPosition = GapCursor.findFrom($position, 1, false);
|
|
57
|
+
if (!$validPosition) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const { tr } = state;
|
|
61
|
+
const gapCursorSelection = new GapCursor($validPosition);
|
|
62
|
+
tr.setSelection(gapCursorSelection);
|
|
63
|
+
tr.scrollIntoView();
|
|
64
|
+
view.dispatch(tr);
|
|
65
|
+
return true;
|
|
64
66
|
};
|
|
65
67
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
68
|
+
// src/details.ts
|
|
69
|
+
var Details = Node.create({
|
|
70
|
+
name: "details",
|
|
71
|
+
content: "detailsSummary detailsContent",
|
|
72
|
+
group: "block",
|
|
73
|
+
defining: true,
|
|
74
|
+
isolating: true,
|
|
75
|
+
// @ts-ignore: allowGapCursor is not a valid option by default, dts on build doesnt pick this up
|
|
76
|
+
allowGapCursor: false,
|
|
77
|
+
addOptions() {
|
|
78
|
+
return {
|
|
79
|
+
persist: false,
|
|
80
|
+
openClassName: "is-open",
|
|
81
|
+
HTMLAttributes: {}
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
addAttributes() {
|
|
85
|
+
if (!this.options.persist) {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
open: {
|
|
90
|
+
default: false,
|
|
91
|
+
parseHTML: (element) => element.hasAttribute("open"),
|
|
92
|
+
renderHTML: ({ open }) => {
|
|
93
|
+
if (!open) {
|
|
94
|
+
return {};
|
|
95
|
+
}
|
|
96
|
+
return { open: "" };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
parseHTML() {
|
|
102
|
+
return [
|
|
103
|
+
{
|
|
104
|
+
tag: "details"
|
|
105
|
+
}
|
|
106
|
+
];
|
|
107
|
+
},
|
|
108
|
+
renderHTML({ HTMLAttributes }) {
|
|
109
|
+
return ["details", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
|
|
110
|
+
},
|
|
111
|
+
addNodeView() {
|
|
112
|
+
return ({ editor, getPos, node, HTMLAttributes }) => {
|
|
113
|
+
const dom = document.createElement("div");
|
|
114
|
+
const attributes = mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
|
|
115
|
+
"data-type": this.name
|
|
116
|
+
});
|
|
117
|
+
Object.entries(attributes).forEach(([key, value]) => dom.setAttribute(key, value));
|
|
118
|
+
const toggle = document.createElement("button");
|
|
119
|
+
toggle.type = "button";
|
|
120
|
+
dom.append(toggle);
|
|
121
|
+
const content = document.createElement("div");
|
|
122
|
+
dom.append(content);
|
|
123
|
+
const toggleDetailsContent = (setToValue) => {
|
|
124
|
+
if (setToValue !== void 0) {
|
|
125
|
+
if (setToValue) {
|
|
126
|
+
if (dom.classList.contains(this.options.openClassName)) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
dom.classList.add(this.options.openClassName);
|
|
130
|
+
} else {
|
|
131
|
+
if (!dom.classList.contains(this.options.openClassName)) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
dom.classList.remove(this.options.openClassName);
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
dom.classList.toggle(this.options.openClassName);
|
|
138
|
+
}
|
|
139
|
+
const event = new Event("toggleDetailsContent");
|
|
140
|
+
const detailsContent = content.querySelector(':scope > div[data-type="detailsContent"]');
|
|
141
|
+
detailsContent == null ? void 0 : detailsContent.dispatchEvent(event);
|
|
142
|
+
};
|
|
143
|
+
if (node.attrs.open) {
|
|
144
|
+
setTimeout(() => toggleDetailsContent());
|
|
145
|
+
}
|
|
146
|
+
toggle.addEventListener("click", () => {
|
|
147
|
+
toggleDetailsContent();
|
|
81
148
|
if (!this.options.persist) {
|
|
82
|
-
|
|
149
|
+
editor.commands.focus(void 0, { scrollIntoView: false });
|
|
150
|
+
return;
|
|
83
151
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
},
|
|
97
|
-
parseHTML() {
|
|
98
|
-
return [
|
|
99
|
-
{
|
|
100
|
-
tag: 'details',
|
|
101
|
-
},
|
|
102
|
-
];
|
|
103
|
-
},
|
|
104
|
-
renderHTML({ HTMLAttributes }) {
|
|
105
|
-
return [
|
|
106
|
-
'details',
|
|
107
|
-
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
|
|
108
|
-
0,
|
|
109
|
-
];
|
|
110
|
-
},
|
|
111
|
-
addNodeView() {
|
|
112
|
-
return ({ editor, getPos, node, HTMLAttributes, }) => {
|
|
113
|
-
const dom = document.createElement('div');
|
|
114
|
-
const attributes = mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
|
|
115
|
-
'data-type': this.name,
|
|
116
|
-
});
|
|
117
|
-
Object.entries(attributes).forEach(([key, value]) => dom.setAttribute(key, value));
|
|
118
|
-
const toggle = document.createElement('button');
|
|
119
|
-
toggle.type = 'button';
|
|
120
|
-
dom.append(toggle);
|
|
121
|
-
const content = document.createElement('div');
|
|
122
|
-
dom.append(content);
|
|
123
|
-
const toggleDetailsContent = (setToValue) => {
|
|
124
|
-
if (setToValue !== undefined) {
|
|
125
|
-
if (setToValue) {
|
|
126
|
-
if (dom.classList.contains(this.options.openClassName)) {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
dom.classList.add(this.options.openClassName);
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
if (!dom.classList.contains(this.options.openClassName)) {
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
dom.classList.remove(this.options.openClassName);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
dom.classList.toggle(this.options.openClassName);
|
|
140
|
-
}
|
|
141
|
-
const event = new Event('toggleDetailsContent');
|
|
142
|
-
const detailsContent = content.querySelector(':scope > div[data-type="detailsContent"]');
|
|
143
|
-
detailsContent === null || detailsContent === void 0 ? void 0 : detailsContent.dispatchEvent(event);
|
|
144
|
-
};
|
|
145
|
-
if (node.attrs.open) {
|
|
146
|
-
setTimeout(() => toggleDetailsContent());
|
|
152
|
+
if (editor.isEditable && typeof getPos === "function") {
|
|
153
|
+
const { from, to } = editor.state.selection;
|
|
154
|
+
editor.chain().command(({ tr }) => {
|
|
155
|
+
const pos = getPos();
|
|
156
|
+
if (pos === void 0) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
const currentNode = tr.doc.nodeAt(pos);
|
|
160
|
+
if ((currentNode == null ? void 0 : currentNode.type) !== this.type) {
|
|
161
|
+
return false;
|
|
147
162
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (!this.options.persist) {
|
|
151
|
-
editor.commands
|
|
152
|
-
.focus(undefined, { scrollIntoView: false });
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
if (editor.isEditable && typeof getPos === 'function') {
|
|
156
|
-
const { from, to } = editor.state.selection;
|
|
157
|
-
editor
|
|
158
|
-
.chain()
|
|
159
|
-
.command(({ tr }) => {
|
|
160
|
-
const pos = getPos();
|
|
161
|
-
const currentNode = tr.doc.nodeAt(pos);
|
|
162
|
-
if ((currentNode === null || currentNode === void 0 ? void 0 : currentNode.type) !== this.type) {
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
tr.setNodeMarkup(pos, undefined, {
|
|
166
|
-
open: !currentNode.attrs.open,
|
|
167
|
-
});
|
|
168
|
-
return true;
|
|
169
|
-
})
|
|
170
|
-
.setTextSelection({
|
|
171
|
-
from,
|
|
172
|
-
to,
|
|
173
|
-
})
|
|
174
|
-
.focus(undefined, { scrollIntoView: false })
|
|
175
|
-
.run();
|
|
176
|
-
}
|
|
163
|
+
tr.setNodeMarkup(pos, void 0, {
|
|
164
|
+
open: !currentNode.attrs.open
|
|
177
165
|
});
|
|
178
|
-
return
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
const selectionDirection = oldState.selection.from < newState.selection.from
|
|
366
|
-
? 'forward'
|
|
367
|
-
: 'backward';
|
|
368
|
-
const correctedPosition = selectionDirection === 'forward'
|
|
369
|
-
? details.start + detailsSummary.pos
|
|
370
|
-
: details.pos + detailsSummary.pos + detailsSummary.node.nodeSize;
|
|
371
|
-
const selection = TextSelection.create(newState.doc, correctedPosition);
|
|
372
|
-
const transaction = newState.tr.setSelection(selection);
|
|
373
|
-
return transaction;
|
|
374
|
-
},
|
|
375
|
-
}),
|
|
376
|
-
];
|
|
377
|
-
},
|
|
166
|
+
return true;
|
|
167
|
+
}).setTextSelection({
|
|
168
|
+
from,
|
|
169
|
+
to
|
|
170
|
+
}).focus(void 0, { scrollIntoView: false }).run();
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
return {
|
|
174
|
+
dom,
|
|
175
|
+
contentDOM: content,
|
|
176
|
+
ignoreMutation(mutation) {
|
|
177
|
+
if (mutation.type === "selection") {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
return !dom.contains(mutation.target) || dom === mutation.target;
|
|
181
|
+
},
|
|
182
|
+
update: (updatedNode) => {
|
|
183
|
+
if (updatedNode.type !== this.type) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
if (updatedNode.attrs.open !== void 0) {
|
|
187
|
+
toggleDetailsContent(updatedNode.attrs.open);
|
|
188
|
+
}
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
},
|
|
194
|
+
addCommands() {
|
|
195
|
+
return {
|
|
196
|
+
setDetails: () => ({ state, chain }) => {
|
|
197
|
+
var _a;
|
|
198
|
+
const { schema, selection } = state;
|
|
199
|
+
const { $from, $to } = selection;
|
|
200
|
+
const range = $from.blockRange($to);
|
|
201
|
+
if (!range) {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
const slice = state.doc.slice(range.start, range.end);
|
|
205
|
+
const match = schema.nodes.detailsContent.contentMatch.matchFragment(slice.content);
|
|
206
|
+
if (!match) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
const content = ((_a = slice.toJSON()) == null ? void 0 : _a.content) || [];
|
|
210
|
+
return chain().insertContentAt(
|
|
211
|
+
{ from: range.start, to: range.end },
|
|
212
|
+
{
|
|
213
|
+
type: this.name,
|
|
214
|
+
content: [
|
|
215
|
+
{
|
|
216
|
+
type: "detailsSummary"
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
type: "detailsContent",
|
|
220
|
+
content
|
|
221
|
+
}
|
|
222
|
+
]
|
|
223
|
+
}
|
|
224
|
+
).setTextSelection(range.start + 2).run();
|
|
225
|
+
},
|
|
226
|
+
unsetDetails: () => ({ state, chain }) => {
|
|
227
|
+
const { selection, schema } = state;
|
|
228
|
+
const details = findParentNode2((node) => node.type === this.type)(selection);
|
|
229
|
+
if (!details) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
const detailsSummaries = findChildren2(details.node, (node) => node.type === schema.nodes.detailsSummary);
|
|
233
|
+
const detailsContents = findChildren2(details.node, (node) => node.type === schema.nodes.detailsContent);
|
|
234
|
+
if (!detailsSummaries.length || !detailsContents.length) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
const detailsSummary = detailsSummaries[0];
|
|
238
|
+
const detailsContent = detailsContents[0];
|
|
239
|
+
const from = details.pos;
|
|
240
|
+
const $from = state.doc.resolve(from);
|
|
241
|
+
const to = from + details.node.nodeSize;
|
|
242
|
+
const range = { from, to };
|
|
243
|
+
const content = detailsContent.node.content.toJSON() || [];
|
|
244
|
+
const defaultTypeForSummary = $from.parent.type.contentMatch.defaultType;
|
|
245
|
+
const summaryContent = defaultTypeForSummary == null ? void 0 : defaultTypeForSummary.create(null, detailsSummary.node.content).toJSON();
|
|
246
|
+
const mergedContent = [summaryContent, ...content];
|
|
247
|
+
return chain().insertContentAt(range, mergedContent).setTextSelection(from + 1).run();
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
},
|
|
251
|
+
addKeyboardShortcuts() {
|
|
252
|
+
return {
|
|
253
|
+
Backspace: () => {
|
|
254
|
+
const { schema, selection } = this.editor.state;
|
|
255
|
+
const { empty, $anchor } = selection;
|
|
256
|
+
if (!empty || $anchor.parent.type !== schema.nodes.detailsSummary) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
if ($anchor.parentOffset !== 0) {
|
|
260
|
+
return this.editor.commands.command(({ tr }) => {
|
|
261
|
+
const from = $anchor.pos - 1;
|
|
262
|
+
const to = $anchor.pos;
|
|
263
|
+
tr.delete(from, to);
|
|
264
|
+
return true;
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
return this.editor.commands.unsetDetails();
|
|
268
|
+
},
|
|
269
|
+
// Creates a new node below it if it is closed.
|
|
270
|
+
// Otherwise inside `DetailsContent`.
|
|
271
|
+
Enter: ({ editor }) => {
|
|
272
|
+
const { state, view } = editor;
|
|
273
|
+
const { schema, selection } = state;
|
|
274
|
+
const { $head } = selection;
|
|
275
|
+
if ($head.parent.type !== schema.nodes.detailsSummary) {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
const isVisible = isNodeVisible($head.after() + 1, editor);
|
|
279
|
+
const above = isVisible ? state.doc.nodeAt($head.after()) : $head.node(-2);
|
|
280
|
+
if (!above) {
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
const after = isVisible ? 0 : $head.indexAfter(-1);
|
|
284
|
+
const type = defaultBlockAt(above.contentMatchAt(after));
|
|
285
|
+
if (!type || !above.canReplaceWith(after, after, type)) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
const node = type.createAndFill();
|
|
289
|
+
if (!node) {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
const pos = isVisible ? $head.after() + 1 : $head.after(-1);
|
|
293
|
+
const tr = state.tr.replaceWith(pos, pos, node);
|
|
294
|
+
const $pos = tr.doc.resolve(pos);
|
|
295
|
+
const newSelection = Selection.near($pos, 1);
|
|
296
|
+
tr.setSelection(newSelection);
|
|
297
|
+
tr.scrollIntoView();
|
|
298
|
+
view.dispatch(tr);
|
|
299
|
+
return true;
|
|
300
|
+
},
|
|
301
|
+
// The default gapcursor implementation can’t handle hidden content, so we need to fix this.
|
|
302
|
+
ArrowRight: ({ editor }) => {
|
|
303
|
+
return setGapCursor(editor, "right");
|
|
304
|
+
},
|
|
305
|
+
// The default gapcursor implementation can’t handle hidden content, so we need to fix this.
|
|
306
|
+
ArrowDown: ({ editor }) => {
|
|
307
|
+
return setGapCursor(editor, "down");
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
},
|
|
311
|
+
addProseMirrorPlugins() {
|
|
312
|
+
return [
|
|
313
|
+
// This plugin prevents text selections within the hidden content in `DetailsContent`.
|
|
314
|
+
// The cursor is moved to the next visible position.
|
|
315
|
+
new Plugin({
|
|
316
|
+
key: new PluginKey("detailsSelection"),
|
|
317
|
+
appendTransaction: (transactions, oldState, newState) => {
|
|
318
|
+
const { editor, type } = this;
|
|
319
|
+
const selectionSet = transactions.some((transaction2) => transaction2.selectionSet);
|
|
320
|
+
if (!selectionSet || !oldState.selection.empty || !newState.selection.empty) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const detailsIsActive = isActive(newState, type.name);
|
|
324
|
+
if (!detailsIsActive) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const { $from } = newState.selection;
|
|
328
|
+
const isVisible = isNodeVisible($from.pos, editor);
|
|
329
|
+
if (isVisible) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const details = findClosestVisibleNode($from, (node) => node.type === type, editor);
|
|
333
|
+
if (!details) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const detailsSummaries = findChildren2(
|
|
337
|
+
details.node,
|
|
338
|
+
(node) => node.type === newState.schema.nodes.detailsSummary
|
|
339
|
+
);
|
|
340
|
+
if (!detailsSummaries.length) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const detailsSummary = detailsSummaries[0];
|
|
344
|
+
const selectionDirection = oldState.selection.from < newState.selection.from ? "forward" : "backward";
|
|
345
|
+
const correctedPosition = selectionDirection === "forward" ? details.start + detailsSummary.pos : details.pos + detailsSummary.pos + detailsSummary.node.nodeSize;
|
|
346
|
+
const selection = TextSelection.create(newState.doc, correctedPosition);
|
|
347
|
+
const transaction = newState.tr.setSelection(selection);
|
|
348
|
+
return transaction;
|
|
349
|
+
}
|
|
350
|
+
})
|
|
351
|
+
];
|
|
352
|
+
}
|
|
378
353
|
});
|
|
379
354
|
|
|
380
|
-
|
|
381
|
-
|
|
355
|
+
// src/index.ts
|
|
356
|
+
var index_default = Details;
|
|
357
|
+
export {
|
|
358
|
+
Details,
|
|
359
|
+
index_default as default
|
|
360
|
+
};
|
|
361
|
+
//# sourceMappingURL=index.js.map
|