@prosekit/core 0.7.1 → 0.7.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/_tsup-dts-rollup.d.ts +295 -152
- package/dist/{chunk-YWQGKV6X.js → chunk-UDQXAK7F.js} +379 -195
- package/dist/prosekit-core-test.js +66 -26
- package/dist/prosekit-core.d.ts +18 -1
- package/dist/prosekit-core.js +690 -327
- package/package.json +3 -3
package/dist/prosekit-core.js
CHANGED
@@ -35,7 +35,7 @@ import {
|
|
35
35
|
stateFromJSON,
|
36
36
|
toReversed,
|
37
37
|
union
|
38
|
-
} from "./chunk-
|
38
|
+
} from "./chunk-UDQXAK7F.js";
|
39
39
|
|
40
40
|
// src/commands/add-mark.ts
|
41
41
|
import "@prosekit/pm/model";
|
@@ -43,8 +43,14 @@ import "@prosekit/pm/state";
|
|
43
43
|
function addMark(options) {
|
44
44
|
return (state, dispatch) => {
|
45
45
|
var _a, _b;
|
46
|
-
|
47
|
-
|
46
|
+
const mark = getMarkType(state.schema, options.type).create(options.attrs);
|
47
|
+
const from = (_a = options.from) != null ? _a : state.selection.from;
|
48
|
+
const to = (_b = options.to) != null ? _b : state.selection.to;
|
49
|
+
if (from > to) {
|
50
|
+
return false;
|
51
|
+
}
|
52
|
+
dispatch == null ? void 0 : dispatch(state.tr.addMark(from, to, mark));
|
53
|
+
return true;
|
48
54
|
};
|
49
55
|
}
|
50
56
|
|
@@ -52,26 +58,52 @@ function addMark(options) {
|
|
52
58
|
import { TextSelection } from "@prosekit/pm/state";
|
53
59
|
function expandMark(options) {
|
54
60
|
return (state, dispatch) => {
|
55
|
-
|
56
|
-
|
61
|
+
const markType = getMarkType(state.schema, options.type);
|
62
|
+
const predicate = (mark) => mark.type === markType;
|
63
|
+
const from = expandMarkBefore(state.selection.$from, predicate);
|
64
|
+
const to = expandMarkAfter(state.selection.$to, predicate);
|
65
|
+
if (from === state.selection.from && to === state.selection.to) {
|
66
|
+
return false;
|
67
|
+
}
|
68
|
+
if (dispatch) {
|
69
|
+
dispatch(state.tr.setSelection(TextSelection.create(state.doc, from, to)));
|
70
|
+
}
|
71
|
+
return true;
|
57
72
|
};
|
58
73
|
}
|
59
74
|
function expandMarkBefore($pos, predicate) {
|
60
|
-
|
61
|
-
if (!$pos.marks().some(predicate))
|
75
|
+
const { parent } = $pos;
|
76
|
+
if (!$pos.marks().some(predicate)) {
|
62
77
|
return $pos.pos;
|
63
|
-
|
64
|
-
|
65
|
-
|
78
|
+
}
|
79
|
+
const index = $pos.index();
|
80
|
+
let boundaryIndex = index;
|
81
|
+
for (let i = index; i >= 0; i--) {
|
82
|
+
const node = parent.child(i);
|
83
|
+
if (node.marks.some(predicate)) {
|
84
|
+
boundaryIndex = i;
|
85
|
+
} else {
|
86
|
+
break;
|
87
|
+
}
|
88
|
+
}
|
66
89
|
return $pos.posAtIndex(boundaryIndex);
|
67
90
|
}
|
68
91
|
function expandMarkAfter($pos, predicate) {
|
69
|
-
|
70
|
-
if (!$pos.marks().some(predicate))
|
92
|
+
const { parent } = $pos;
|
93
|
+
if (!$pos.marks().some(predicate)) {
|
71
94
|
return $pos.pos;
|
72
|
-
|
73
|
-
|
74
|
-
|
95
|
+
}
|
96
|
+
const index = Math.max(0, $pos.indexAfter() - 1);
|
97
|
+
const childCount = parent.childCount;
|
98
|
+
let boundaryIndex = index;
|
99
|
+
for (let i = index; i < childCount; i++) {
|
100
|
+
const node = parent.child(i);
|
101
|
+
if (node.marks.some(predicate)) {
|
102
|
+
boundaryIndex = i;
|
103
|
+
} else {
|
104
|
+
break;
|
105
|
+
}
|
106
|
+
}
|
75
107
|
return $pos.posAtIndex(boundaryIndex) + parent.child(boundaryIndex).nodeSize;
|
76
108
|
}
|
77
109
|
|
@@ -82,7 +114,9 @@ import { insertPoint } from "@prosekit/pm/transform";
|
|
82
114
|
// src/utils/set-selection-around.ts
|
83
115
|
import { TextSelection as TextSelection2 } from "@prosekit/pm/state";
|
84
116
|
function setSelectionAround(tr, pos) {
|
85
|
-
|
117
|
+
const docSize = tr.doc.content.size;
|
118
|
+
const $pos = tr.doc.resolve(pos > docSize ? docSize : pos < 0 ? 0 : pos);
|
119
|
+
const selection = TextSelection2.between($pos, $pos);
|
86
120
|
tr.setSelection(selection);
|
87
121
|
}
|
88
122
|
|
@@ -90,19 +124,20 @@ function setSelectionAround(tr, pos) {
|
|
90
124
|
function insertNode(options) {
|
91
125
|
return (state, dispatch) => {
|
92
126
|
var _a;
|
93
|
-
|
127
|
+
const node = options.node ? options.node : options.type ? getNodeType(state.schema, options.type).createAndFill(options.attrs) : null;
|
94
128
|
assert(node, "You must provide either a node or a type");
|
95
|
-
|
129
|
+
const insertPos = insertPoint(
|
96
130
|
state.doc,
|
97
|
-
(_a = options.pos) != null ? _a : state.selection.
|
131
|
+
(_a = options.pos) != null ? _a : state.selection.anchor,
|
98
132
|
node.type
|
99
133
|
);
|
100
|
-
if (insertPos == null) return
|
134
|
+
if (insertPos == null) return false;
|
101
135
|
if (dispatch) {
|
102
|
-
|
103
|
-
setSelectionAround(tr, insertPos + node.nodeSize)
|
136
|
+
const tr = state.tr.insert(insertPos, node);
|
137
|
+
setSelectionAround(tr, insertPos + node.nodeSize);
|
138
|
+
dispatch(tr);
|
104
139
|
}
|
105
|
-
return
|
140
|
+
return true;
|
106
141
|
};
|
107
142
|
}
|
108
143
|
|
@@ -112,21 +147,31 @@ import "@prosekit/pm/state";
|
|
112
147
|
function removeMark(options) {
|
113
148
|
return (state, dispatch) => {
|
114
149
|
var _a, _b;
|
115
|
-
|
116
|
-
|
150
|
+
const markType = getMarkType(state.schema, options.type);
|
151
|
+
const mark = options.attrs ? markType.create(options.attrs) : markType;
|
152
|
+
const from = (_a = options.from) != null ? _a : state.selection.from;
|
153
|
+
const to = (_b = options.to) != null ? _b : state.selection.to;
|
154
|
+
if (from > to) {
|
155
|
+
return false;
|
156
|
+
}
|
157
|
+
dispatch == null ? void 0 : dispatch(state.tr.removeMark(from, to, mark));
|
158
|
+
return true;
|
117
159
|
};
|
118
160
|
}
|
119
161
|
|
120
162
|
// src/utils/find-parent-node.ts
|
121
163
|
function findParentNode(nodeType, $pos) {
|
122
|
-
for (let depth = $pos.depth; depth > 0; depth -= 1)
|
123
|
-
|
124
|
-
|
164
|
+
for (let depth = $pos.depth; depth > 0; depth -= 1) {
|
165
|
+
const node = $pos.node(depth);
|
166
|
+
if (node.type === nodeType) {
|
167
|
+
const from = $pos.before(depth);
|
168
|
+
const to = $pos.after(depth);
|
125
169
|
return {
|
126
170
|
from,
|
127
171
|
to
|
128
172
|
};
|
129
173
|
}
|
174
|
+
}
|
130
175
|
return {
|
131
176
|
from: null,
|
132
177
|
to: null
|
@@ -136,8 +181,14 @@ function findParentNode(nodeType, $pos) {
|
|
136
181
|
// src/commands/remove-node.ts
|
137
182
|
function removeNode(options) {
|
138
183
|
return (state, dispatch) => {
|
139
|
-
|
140
|
-
|
184
|
+
const nodeType = getNodeType(state.schema, options.type);
|
185
|
+
const $pos = typeof options.pos === "number" ? state.doc.resolve(options.pos) : state.selection.$anchor;
|
186
|
+
const { from, to } = findParentNode(nodeType, $pos);
|
187
|
+
if (from == null || to == null || from > to) {
|
188
|
+
return false;
|
189
|
+
}
|
190
|
+
dispatch == null ? void 0 : dispatch(state.tr.delete(from, to));
|
191
|
+
return true;
|
141
192
|
};
|
142
193
|
}
|
143
194
|
|
@@ -147,9 +198,10 @@ import "@prosekit/pm/state";
|
|
147
198
|
// src/utils/get-custom-selection.ts
|
148
199
|
import { TextSelection as TextSelection3 } from "@prosekit/pm/state";
|
149
200
|
function getCustomSelection(state, from, to) {
|
150
|
-
|
201
|
+
const pos = from != null ? from : to;
|
151
202
|
if (pos != null) {
|
152
|
-
|
203
|
+
const $from = state.doc.resolve(from != null ? from : pos);
|
204
|
+
const $to = state.doc.resolve(to != null ? to : pos);
|
153
205
|
return TextSelection3.between($from, $to);
|
154
206
|
}
|
155
207
|
return state.selection;
|
@@ -158,28 +210,31 @@ function getCustomSelection(state, from, to) {
|
|
158
210
|
// src/commands/set-block-type.ts
|
159
211
|
function setBlockType(options) {
|
160
212
|
return (state, dispatch) => {
|
161
|
-
|
213
|
+
const nodeType = getNodeType(state.schema, options.type);
|
214
|
+
const selection = getCustomSelection(state, options.from, options.to);
|
215
|
+
const attrs = options.attrs;
|
216
|
+
let applicable = false;
|
162
217
|
for (let i = 0; i < selection.ranges.length && !applicable; i++) {
|
163
|
-
|
218
|
+
const {
|
164
219
|
$from: { pos: from },
|
165
220
|
$to: { pos: to }
|
166
221
|
} = selection.ranges[i];
|
167
222
|
state.doc.nodesBetween(from, to, (node, pos) => {
|
168
|
-
if (applicable) return
|
169
|
-
if (!
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
223
|
+
if (applicable) return false;
|
224
|
+
if (!node.isTextblock || node.hasMarkup(nodeType, attrs)) return;
|
225
|
+
if (node.type == nodeType) {
|
226
|
+
applicable = true;
|
227
|
+
} else {
|
228
|
+
const $pos = state.doc.resolve(pos), index = $pos.index();
|
229
|
+
applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType);
|
230
|
+
}
|
176
231
|
});
|
177
232
|
}
|
178
|
-
if (!applicable) return
|
233
|
+
if (!applicable) return false;
|
179
234
|
if (dispatch) {
|
180
|
-
|
181
|
-
for (
|
182
|
-
|
235
|
+
const tr = state.tr;
|
236
|
+
for (const range of selection.ranges) {
|
237
|
+
const {
|
183
238
|
$from: { pos: from },
|
184
239
|
$to: { pos: to }
|
185
240
|
} = range;
|
@@ -187,34 +242,48 @@ function setBlockType(options) {
|
|
187
242
|
}
|
188
243
|
dispatch(tr.scrollIntoView());
|
189
244
|
}
|
190
|
-
return
|
245
|
+
return true;
|
191
246
|
};
|
192
247
|
}
|
193
248
|
|
194
249
|
// src/utils/get-node-types.ts
|
195
250
|
import "@prosekit/pm/model";
|
196
251
|
function getNodeTypes(schema, types) {
|
197
|
-
|
252
|
+
if (Array.isArray(types)) {
|
253
|
+
return types.map((type) => getNodeType(schema, type));
|
254
|
+
}
|
255
|
+
return [getNodeType(schema, types)];
|
198
256
|
}
|
199
257
|
|
200
258
|
// src/commands/set-node-attrs.ts
|
201
259
|
function setNodeAttrs(options) {
|
202
260
|
return (state, dispatch) => {
|
203
261
|
var _a, _b;
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
262
|
+
const nodeTypes = getNodeTypes(state.schema, options.type);
|
263
|
+
const from = (_a = options.pos) != null ? _a : state.selection.from;
|
264
|
+
const to = (_b = options.pos) != null ? _b : state.selection.to;
|
265
|
+
const positions = [];
|
266
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
267
|
+
if (nodeTypes.includes(node.type)) {
|
268
|
+
positions.push(pos);
|
269
|
+
}
|
270
|
+
if (!dispatch && positions.length > 0) {
|
271
|
+
return false;
|
272
|
+
}
|
273
|
+
});
|
274
|
+
if (positions.length === 0) {
|
275
|
+
return false;
|
276
|
+
}
|
210
277
|
if (dispatch) {
|
211
|
-
|
212
|
-
for (
|
213
|
-
for (
|
278
|
+
const { tr } = state;
|
279
|
+
for (const pos of positions) {
|
280
|
+
for (const [key, value] of Object.entries(options.attrs)) {
|
214
281
|
tr.setNodeAttribute(pos, key, value);
|
282
|
+
}
|
283
|
+
}
|
215
284
|
dispatch(tr);
|
216
285
|
}
|
217
|
-
return
|
286
|
+
return true;
|
218
287
|
};
|
219
288
|
}
|
220
289
|
|
@@ -222,84 +291,174 @@ function setNodeAttrs(options) {
|
|
222
291
|
import "@prosekit/pm/model";
|
223
292
|
import "@prosekit/pm/state";
|
224
293
|
function markApplies(doc, ranges, type) {
|
225
|
-
for (
|
226
|
-
let can = $from.depth == 0 ? doc.inlineContent && doc.type.allowsMarkType(type) :
|
227
|
-
|
228
|
-
if (can) return
|
294
|
+
for (const { $from, $to } of ranges) {
|
295
|
+
let can = $from.depth == 0 ? doc.inlineContent && doc.type.allowsMarkType(type) : false;
|
296
|
+
doc.nodesBetween($from.pos, $to.pos, (node) => {
|
297
|
+
if (can) return false;
|
229
298
|
can = node.inlineContent && node.type.allowsMarkType(type);
|
230
|
-
})
|
299
|
+
});
|
300
|
+
if (can) return true;
|
231
301
|
}
|
232
|
-
return
|
302
|
+
return false;
|
233
303
|
}
|
234
304
|
function baseToggleMark(markType, attrs = null, options) {
|
235
|
-
|
305
|
+
const removeWhenPresent = (options && options.removeWhenPresent) !== false;
|
236
306
|
return function(state, dispatch) {
|
237
|
-
|
307
|
+
const { empty, $cursor, ranges } = state.selection;
|
238
308
|
if (empty && !$cursor || !markApplies(state.doc, ranges, markType))
|
239
|
-
return
|
240
|
-
if (dispatch)
|
241
|
-
if ($cursor)
|
242
|
-
markType.isInSet(state.storedMarks || $cursor.marks())
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
309
|
+
return false;
|
310
|
+
if (dispatch) {
|
311
|
+
if ($cursor) {
|
312
|
+
if (markType.isInSet(state.storedMarks || $cursor.marks()))
|
313
|
+
dispatch(state.tr.removeStoredMark(markType));
|
314
|
+
else dispatch(state.tr.addStoredMark(markType.create(attrs)));
|
315
|
+
} else {
|
316
|
+
let add;
|
317
|
+
const tr = state.tr;
|
318
|
+
if (removeWhenPresent) {
|
319
|
+
add = !ranges.some(
|
320
|
+
(r) => state.doc.rangeHasMark(r.$from.pos, r.$to.pos, markType)
|
321
|
+
);
|
322
|
+
} else {
|
323
|
+
add = !ranges.every((r) => {
|
324
|
+
let missing = false;
|
325
|
+
tr.doc.nodesBetween(r.$from.pos, r.$to.pos, (node, pos, parent) => {
|
326
|
+
if (missing) return false;
|
327
|
+
missing = !markType.isInSet(node.marks) && !!parent && parent.type.allowsMarkType(markType) && !(node.isText && /^\s*$/.test(
|
328
|
+
node.textBetween(
|
329
|
+
Math.max(0, r.$from.pos - pos),
|
330
|
+
Math.min(node.nodeSize, r.$to.pos - pos)
|
331
|
+
)
|
332
|
+
));
|
333
|
+
});
|
334
|
+
return !missing;
|
335
|
+
});
|
336
|
+
}
|
337
|
+
for (const { $from, $to } of ranges) {
|
338
|
+
if (!add) {
|
261
339
|
tr.removeMark($from.pos, $to.pos, markType);
|
262
|
-
else {
|
263
|
-
let from = $from.pos, to = $to.pos
|
264
|
-
|
340
|
+
} else {
|
341
|
+
let from = $from.pos, to = $to.pos;
|
342
|
+
const start = $from.nodeAfter, end = $to.nodeBefore;
|
343
|
+
const spaceStart = start && start.isText ? /^\s*/.exec(start.text)[0].length : 0;
|
344
|
+
const spaceEnd = end && end.isText ? /\s*$/.exec(end.text)[0].length : 0;
|
345
|
+
if (from + spaceStart < to) {
|
346
|
+
from += spaceStart;
|
347
|
+
to -= spaceEnd;
|
348
|
+
}
|
349
|
+
tr.addMark(from, to, markType.create(attrs));
|
265
350
|
}
|
351
|
+
}
|
266
352
|
dispatch(tr.scrollIntoView());
|
267
353
|
}
|
268
|
-
|
354
|
+
}
|
355
|
+
return true;
|
269
356
|
};
|
270
357
|
}
|
271
|
-
function toggleMark({
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
})(state, dispatch, view);
|
358
|
+
function toggleMark({ type, attrs }) {
|
359
|
+
return (state, dispatch, view) => {
|
360
|
+
return baseToggleMark(getMarkType(state.schema, type), attrs, {
|
361
|
+
removeWhenPresent: false
|
362
|
+
})(state, dispatch, view);
|
363
|
+
};
|
278
364
|
}
|
279
365
|
|
280
366
|
// src/commands/toggle-node.ts
|
281
367
|
import { setBlockType as setBlockType2 } from "@prosekit/pm/commands";
|
282
368
|
import "@prosekit/pm/model";
|
283
369
|
import "@prosekit/pm/state";
|
284
|
-
function toggleNode({
|
285
|
-
type,
|
286
|
-
attrs
|
287
|
-
}) {
|
370
|
+
function toggleNode({ type, attrs }) {
|
288
371
|
return (state, dispatch, view) => {
|
289
372
|
if (isNodeActive(state, type, attrs)) {
|
290
|
-
|
291
|
-
|
373
|
+
const defaultType = state.schema.topNodeType.contentMatch.defaultType;
|
374
|
+
if (!defaultType) {
|
375
|
+
return false;
|
376
|
+
}
|
377
|
+
return setBlockType2(defaultType)(state, dispatch, view);
|
292
378
|
} else {
|
293
|
-
|
379
|
+
const nodeType = getNodeType(state.schema, type);
|
294
380
|
return setBlockType2(nodeType, attrs)(state, dispatch, view);
|
295
381
|
}
|
296
382
|
};
|
297
383
|
}
|
298
384
|
|
385
|
+
// src/commands/unset-block-type.ts
|
386
|
+
import { Fragment, Slice } from "@prosekit/pm/model";
|
387
|
+
import "@prosekit/pm/state";
|
388
|
+
import { ReplaceAroundStep } from "@prosekit/pm/transform";
|
389
|
+
function unsetBlockType(options) {
|
390
|
+
return (state, dispatch) => {
|
391
|
+
var _a, _b;
|
392
|
+
const from = (_a = options == null ? void 0 : options.from) != null ? _a : state.selection.from;
|
393
|
+
const to = (_b = options == null ? void 0 : options.to) != null ? _b : state.selection.to;
|
394
|
+
if (from > to) return false;
|
395
|
+
const tr = state.tr;
|
396
|
+
if (unsetTextBlockType(tr, from, to)) {
|
397
|
+
dispatch == null ? void 0 : dispatch(tr);
|
398
|
+
return true;
|
399
|
+
}
|
400
|
+
return false;
|
401
|
+
};
|
402
|
+
}
|
403
|
+
function unsetTextBlockType(tr, from, to) {
|
404
|
+
const mapFrom = tr.steps.length;
|
405
|
+
tr.doc.nodesBetween(from, to, (node, pos, parent, index) => {
|
406
|
+
if (!parent || !node.isTextblock) return true;
|
407
|
+
const defaultType = parent.contentMatchAt(index).defaultType;
|
408
|
+
if (defaultType && defaultType.isTextblock && node.type !== defaultType && defaultType.validContent(node.content)) {
|
409
|
+
const mapping = tr.mapping.slice(mapFrom);
|
410
|
+
const start = mapping.map(pos, 1);
|
411
|
+
const end = mapping.map(pos + node.nodeSize, 1);
|
412
|
+
const step = new ReplaceAroundStep(
|
413
|
+
start,
|
414
|
+
end,
|
415
|
+
start + 1,
|
416
|
+
end - 1,
|
417
|
+
new Slice(Fragment.from(defaultType.create()), 0, 0),
|
418
|
+
1,
|
419
|
+
true
|
420
|
+
);
|
421
|
+
tr.step(step);
|
422
|
+
}
|
423
|
+
return false;
|
424
|
+
});
|
425
|
+
return tr.steps.length > mapFrom;
|
426
|
+
}
|
427
|
+
|
428
|
+
// src/commands/unset-mark.ts
|
429
|
+
import "@prosekit/pm/state";
|
430
|
+
function unsetMark(options) {
|
431
|
+
return (state, dispatch) => {
|
432
|
+
var _a, _b;
|
433
|
+
const from = (_a = options == null ? void 0 : options.from) != null ? _a : state.selection.from;
|
434
|
+
const to = (_b = options == null ? void 0 : options.to) != null ? _b : state.selection.to;
|
435
|
+
if (from > to) return false;
|
436
|
+
dispatch == null ? void 0 : dispatch(state.tr.removeMark(from, to));
|
437
|
+
return true;
|
438
|
+
};
|
439
|
+
}
|
440
|
+
|
441
|
+
// src/commands/wrap.ts
|
442
|
+
import "@prosekit/pm/model";
|
443
|
+
import "@prosekit/pm/state";
|
444
|
+
import { findWrapping } from "@prosekit/pm/transform";
|
445
|
+
function wrap({ nodeType, attrs }) {
|
446
|
+
return (state, dispatch) => {
|
447
|
+
const { $from, $to } = state.selection;
|
448
|
+
const range = $from.blockRange($to);
|
449
|
+
if (!range) return false;
|
450
|
+
const wrapping = findWrapping(range, nodeType, attrs);
|
451
|
+
if (!wrapping) return false;
|
452
|
+
dispatch == null ? void 0 : dispatch(state.tr.wrap(range, wrapping));
|
453
|
+
return true;
|
454
|
+
};
|
455
|
+
}
|
456
|
+
|
299
457
|
// src/editor/with-priority.ts
|
300
458
|
function withPriority(extension, priority) {
|
301
|
-
|
302
|
-
|
459
|
+
const result = union(extension);
|
460
|
+
result.priority = priority;
|
461
|
+
return result;
|
303
462
|
}
|
304
463
|
|
305
464
|
// src/commands/insert-text.ts
|
@@ -308,41 +467,36 @@ function insertText({
|
|
308
467
|
from,
|
309
468
|
to
|
310
469
|
}) {
|
311
|
-
return (state, dispatch) =>
|
470
|
+
return (state, dispatch) => {
|
471
|
+
if (text) {
|
472
|
+
dispatch == null ? void 0 : dispatch(state.tr.insertText(text, from, to));
|
473
|
+
}
|
474
|
+
return true;
|
475
|
+
};
|
312
476
|
}
|
313
477
|
|
314
478
|
// src/commands/select-all.ts
|
315
479
|
import { AllSelection } from "@prosekit/pm/state";
|
316
480
|
function selectAll() {
|
317
|
-
return (state, dispatch) => (dispatch == null || dispatch(state.tr.setSelection(new AllSelection(state.doc))), !0);
|
318
|
-
}
|
319
|
-
|
320
|
-
// src/commands/wrap.ts
|
321
|
-
import "@prosekit/pm/model";
|
322
|
-
import "@prosekit/pm/state";
|
323
|
-
import { findWrapping } from "@prosekit/pm/transform";
|
324
|
-
function wrap({
|
325
|
-
nodeType,
|
326
|
-
attrs
|
327
|
-
}) {
|
328
481
|
return (state, dispatch) => {
|
329
|
-
|
330
|
-
|
331
|
-
let wrapping = findWrapping(range, nodeType, attrs);
|
332
|
-
return wrapping ? (dispatch == null || dispatch(state.tr.wrap(range, wrapping)), !0) : !1;
|
482
|
+
dispatch == null ? void 0 : dispatch(state.tr.setSelection(new AllSelection(state.doc)));
|
483
|
+
return true;
|
333
484
|
};
|
334
485
|
}
|
335
486
|
|
336
487
|
// src/facets/command.ts
|
337
488
|
var commandFacet = defineFacet({
|
338
|
-
reducer: (inputs) =>
|
489
|
+
reducer: (inputs) => {
|
490
|
+
const commands2 = Object.assign({}, ...inputs);
|
491
|
+
return { commands: commands2 };
|
492
|
+
},
|
339
493
|
parent: rootFacet,
|
340
|
-
singleton:
|
494
|
+
singleton: true
|
341
495
|
});
|
342
496
|
|
343
497
|
// src/extensions/command.ts
|
344
|
-
function defineCommands(
|
345
|
-
return defineFacetPayload(commandFacet, [
|
498
|
+
function defineCommands(commands2) {
|
499
|
+
return defineFacetPayload(commandFacet, [commands2]);
|
346
500
|
}
|
347
501
|
function defineBaseCommands() {
|
348
502
|
return defineCommands({
|
@@ -354,7 +508,9 @@ function defineBaseCommands() {
|
|
354
508
|
setNodeAttrs,
|
355
509
|
selectAll,
|
356
510
|
addMark,
|
357
|
-
removeMark
|
511
|
+
removeMark,
|
512
|
+
unsetBlockType,
|
513
|
+
unsetMark
|
358
514
|
});
|
359
515
|
}
|
360
516
|
|
@@ -367,34 +523,49 @@ import OrderedMap from "orderedmap";
|
|
367
523
|
var schemaSpecFacet = defineFacet({
|
368
524
|
reducer: (specs) => {
|
369
525
|
var _a;
|
370
|
-
let nodes = OrderedMap.from({})
|
371
|
-
|
372
|
-
|
526
|
+
let nodes = OrderedMap.from({});
|
527
|
+
let marks = OrderedMap.from({});
|
528
|
+
let topNode = void 0;
|
529
|
+
for (const spec of specs) {
|
530
|
+
nodes = nodes.append(spec.nodes);
|
531
|
+
marks = marks.append((_a = spec.marks) != null ? _a : {});
|
532
|
+
topNode = topNode != null ? topNode : spec.topNode;
|
533
|
+
}
|
373
534
|
return { nodes, marks, topNode };
|
374
535
|
},
|
375
536
|
parent: schemaFacet,
|
376
|
-
singleton:
|
537
|
+
singleton: true
|
377
538
|
});
|
378
539
|
|
379
540
|
// src/utils/is-element.ts
|
380
|
-
var hasElement = typeof Element
|
541
|
+
var hasElement = typeof Element !== "undefined";
|
381
542
|
function isElement(value) {
|
382
543
|
return hasElement && value instanceof Element;
|
383
544
|
}
|
384
545
|
|
385
546
|
// src/extensions/node-spec.ts
|
386
547
|
function defineNodeSpec(options) {
|
387
|
-
|
548
|
+
const payload = [options, void 0];
|
549
|
+
return defineFacetPayload(nodeSpecFacet, [payload]);
|
388
550
|
}
|
389
551
|
function defineNodeAttr(options) {
|
390
|
-
|
552
|
+
const payload = [void 0, options];
|
553
|
+
return defineFacetPayload(nodeSpecFacet, [payload]);
|
391
554
|
}
|
392
555
|
var nodeSpecFacet = defineFacet({
|
393
556
|
reducer: (payloads) => {
|
394
|
-
let nodes = OrderedMap2.from({})
|
395
|
-
|
396
|
-
|
397
|
-
|
557
|
+
let nodes = OrderedMap2.from({});
|
558
|
+
let topNodeName = void 0;
|
559
|
+
const specPayloads = payloads.map((input) => input[0]).filter(isNotNull);
|
560
|
+
const attrPayloads = payloads.map((input) => input[1]).filter(isNotNull);
|
561
|
+
for (const { name, topNode, ...spec } of specPayloads) {
|
562
|
+
assert(!nodes.get(name), `Node type ${name} can only be defined once`);
|
563
|
+
if (topNode) {
|
564
|
+
topNodeName = name;
|
565
|
+
}
|
566
|
+
nodes = nodes.addToStart(name, spec);
|
567
|
+
}
|
568
|
+
for (const {
|
398
569
|
type,
|
399
570
|
attr,
|
400
571
|
default: defaultValue,
|
@@ -402,57 +573,87 @@ var nodeSpecFacet = defineFacet({
|
|
402
573
|
toDOM,
|
403
574
|
parseDOM
|
404
575
|
} of attrPayloads) {
|
405
|
-
|
406
|
-
|
576
|
+
const spec = nodes.get(type);
|
577
|
+
assert(spec, `Node type ${type} must be defined`);
|
578
|
+
if (!spec.attrs) {
|
579
|
+
spec.attrs = {};
|
580
|
+
}
|
581
|
+
spec.attrs[attr] = {
|
407
582
|
default: defaultValue,
|
408
583
|
splittable
|
409
|
-
}
|
410
|
-
|
584
|
+
};
|
585
|
+
if (toDOM && spec.toDOM) {
|
586
|
+
const existingToDom = spec.toDOM;
|
411
587
|
spec.toDOM = (node) => {
|
412
|
-
|
413
|
-
if (!dom)
|
588
|
+
const dom = existingToDom(node);
|
589
|
+
if (!dom) {
|
414
590
|
return dom;
|
415
|
-
|
416
|
-
|
591
|
+
}
|
592
|
+
const attrDOM = toDOM(node.attrs[attr]);
|
593
|
+
if (!attrDOM) {
|
594
|
+
return dom;
|
595
|
+
}
|
596
|
+
const [key, value] = attrDOM;
|
597
|
+
if (!key) {
|
417
598
|
return dom;
|
418
|
-
|
419
|
-
|
420
|
-
dom[
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
599
|
+
}
|
600
|
+
if (Array.isArray(dom)) {
|
601
|
+
if (typeof dom[1] === "object") {
|
602
|
+
return [
|
603
|
+
dom[0],
|
604
|
+
setObjectAttribute(
|
605
|
+
dom[1],
|
606
|
+
key,
|
607
|
+
value
|
608
|
+
),
|
609
|
+
...dom.slice(2)
|
610
|
+
];
|
611
|
+
} else {
|
612
|
+
return [dom[0], { [key]: value }, ...dom.slice(1)];
|
613
|
+
}
|
614
|
+
} else if (isElement(dom)) {
|
615
|
+
setElementAttribute(dom, key, value);
|
616
|
+
} else if (typeof dom === "object" && "dom" in dom && isElement(dom.dom)) {
|
617
|
+
setElementAttribute(dom.dom, key, value);
|
618
|
+
}
|
619
|
+
return dom;
|
428
620
|
};
|
429
621
|
}
|
430
|
-
if (parseDOM && spec.parseDOM)
|
431
|
-
for (
|
432
|
-
|
622
|
+
if (parseDOM && spec.parseDOM) {
|
623
|
+
for (const rule of spec.parseDOM) {
|
624
|
+
const existingGetAttrs = rule.getAttrs;
|
625
|
+
const existingAttrs = rule.attrs;
|
433
626
|
rule.getAttrs = (dom) => {
|
434
627
|
var _a;
|
435
|
-
|
436
|
-
if (attrs ===
|
628
|
+
const attrs = (_a = existingGetAttrs == null ? void 0 : existingGetAttrs(dom)) != null ? _a : existingAttrs;
|
629
|
+
if (attrs === false || !dom || !isElement(dom)) {
|
437
630
|
return attrs != null ? attrs : null;
|
438
|
-
|
631
|
+
}
|
632
|
+
const value = parseDOM(dom);
|
439
633
|
return {
|
440
634
|
...attrs,
|
441
635
|
[attr]: value
|
442
636
|
};
|
443
637
|
};
|
444
638
|
}
|
639
|
+
}
|
445
640
|
}
|
446
641
|
return { nodes, topNode: topNodeName };
|
447
642
|
},
|
448
643
|
parent: schemaSpecFacet,
|
449
|
-
singleton:
|
644
|
+
singleton: true
|
450
645
|
});
|
451
646
|
function setObjectAttribute(obj, key, value) {
|
452
|
-
|
647
|
+
if (key === "style") {
|
648
|
+
value = `${value}${obj.style || ""}`;
|
649
|
+
}
|
650
|
+
return { ...obj, [key]: value };
|
453
651
|
}
|
454
652
|
function setElementAttribute(element, key, value) {
|
455
|
-
key === "style"
|
653
|
+
if (key === "style") {
|
654
|
+
value = `${value}${element.getAttribute("style") || ""}`;
|
655
|
+
}
|
656
|
+
element.setAttribute(key, value);
|
456
657
|
}
|
457
658
|
|
458
659
|
// src/extensions/doc.ts
|
@@ -460,7 +661,7 @@ function defineDoc() {
|
|
460
661
|
return defineNodeSpec({
|
461
662
|
name: "doc",
|
462
663
|
content: "block+",
|
463
|
-
topNode:
|
664
|
+
topNode: true
|
464
665
|
});
|
465
666
|
}
|
466
667
|
|
@@ -471,27 +672,35 @@ import { PluginKey, ProseMirrorPlugin as ProseMirrorPlugin2 } from "@prosekit/pm
|
|
471
672
|
import "@prosekit/pm/model";
|
472
673
|
import { Plugin } from "@prosekit/pm/state";
|
473
674
|
function definePlugin(plugin) {
|
474
|
-
if (plugin instanceof Plugin)
|
675
|
+
if (plugin instanceof Plugin) {
|
475
676
|
return defineFacetPayload(pluginFacet, [() => [plugin]]);
|
476
|
-
|
677
|
+
}
|
678
|
+
if (Array.isArray(plugin) && plugin.every((p) => p instanceof Plugin)) {
|
477
679
|
return defineFacetPayload(pluginFacet, [() => plugin]);
|
478
|
-
|
680
|
+
}
|
681
|
+
if (typeof plugin === "function") {
|
479
682
|
return defineFacetPayload(pluginFacet, [plugin]);
|
683
|
+
}
|
480
684
|
throw new TypeError("Invalid plugin");
|
481
685
|
}
|
482
686
|
var pluginFacet = defineFacet({
|
483
|
-
reducer: (payloads) =>
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
687
|
+
reducer: (payloads) => {
|
688
|
+
return ({ schema }) => {
|
689
|
+
const plugins = [];
|
690
|
+
for (const payload of payloads) {
|
691
|
+
if (payload instanceof Plugin) {
|
692
|
+
plugins.push(payload);
|
693
|
+
} else if (Array.isArray(payload) && payload.every((p) => p instanceof Plugin)) {
|
694
|
+
plugins.push(...payload);
|
695
|
+
} else if (typeof payload === "function") {
|
696
|
+
plugins.push(...[payload({ schema })].flat());
|
697
|
+
} else {
|
698
|
+
throw new ProseKitError("Invalid plugin");
|
699
|
+
}
|
700
|
+
}
|
701
|
+
plugins.reverse();
|
702
|
+
return { plugins };
|
703
|
+
};
|
495
704
|
},
|
496
705
|
parent: stateFacet
|
497
706
|
});
|
@@ -508,19 +717,28 @@ function defineUnmountHandler(handler) {
|
|
508
717
|
}
|
509
718
|
var pluginViewFacet = defineFacet({
|
510
719
|
reduce: () => {
|
511
|
-
let mountHandlers = []
|
720
|
+
let mountHandlers = [];
|
721
|
+
let updateHandlers = [];
|
722
|
+
let unmountHandlers = [];
|
723
|
+
const plugin = new ProseMirrorPlugin2({
|
512
724
|
key: pluginKey,
|
513
|
-
view: (view) =>
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
725
|
+
view: (view) => {
|
726
|
+
mountHandlers.forEach((fn) => fn(view));
|
727
|
+
return {
|
728
|
+
update: (view2, prevState) => {
|
729
|
+
updateHandlers.forEach((fn) => fn(view2, prevState));
|
730
|
+
},
|
731
|
+
destroy: () => {
|
732
|
+
unmountHandlers.forEach((fn) => fn());
|
733
|
+
}
|
734
|
+
};
|
735
|
+
}
|
736
|
+
});
|
737
|
+
const register = (input) => {
|
738
|
+
mountHandlers = [];
|
739
|
+
updateHandlers = [];
|
740
|
+
unmountHandlers = [];
|
741
|
+
for (const args of input) {
|
524
742
|
switch (args[0]) {
|
525
743
|
case "mount":
|
526
744
|
mountHandlers.push(args[1]);
|
@@ -532,19 +750,24 @@ var pluginViewFacet = defineFacet({
|
|
532
750
|
unmountHandlers.push(args[1]);
|
533
751
|
break;
|
534
752
|
}
|
753
|
+
}
|
535
754
|
};
|
536
|
-
return function(input) {
|
537
|
-
|
755
|
+
return function reducer(input) {
|
756
|
+
register(input);
|
757
|
+
return plugin;
|
538
758
|
};
|
539
759
|
},
|
540
760
|
parent: pluginFacet,
|
541
|
-
singleton:
|
542
|
-
})
|
761
|
+
singleton: true
|
762
|
+
});
|
763
|
+
var pluginKey = new PluginKey("prosekit-plugin-view-handler");
|
543
764
|
|
544
765
|
// src/extensions/events/doc-change.ts
|
545
766
|
function defineDocChangeHandler(handler) {
|
546
767
|
return defineUpdateHandler((view, prevState) => {
|
547
|
-
view.state.doc.eq(prevState.doc)
|
768
|
+
if (!view.state.doc.eq(prevState.doc)) {
|
769
|
+
handler(view, prevState);
|
770
|
+
}
|
548
771
|
});
|
549
772
|
}
|
550
773
|
|
@@ -558,20 +781,26 @@ function combineEventHandlers() {
|
|
558
781
|
_handlers = toReversed(handlers);
|
559
782
|
}
|
560
783
|
function combinedEventHandler(...args) {
|
561
|
-
for (
|
562
|
-
if (handler(...args))
|
563
|
-
return
|
564
|
-
|
784
|
+
for (const handler of _handlers) {
|
785
|
+
if (handler(...args)) {
|
786
|
+
return true;
|
787
|
+
}
|
788
|
+
}
|
789
|
+
return false;
|
565
790
|
}
|
566
791
|
return [setHandlers, combinedEventHandler];
|
567
792
|
}
|
568
793
|
|
569
794
|
// src/utils/group-entries.ts
|
570
795
|
function groupEntries(entries) {
|
571
|
-
|
572
|
-
for (
|
573
|
-
|
574
|
-
|
796
|
+
const map = {};
|
797
|
+
for (const [key, value] of entries) {
|
798
|
+
const values = map[key];
|
799
|
+
if (!values) {
|
800
|
+
map[key] = [value];
|
801
|
+
} else {
|
802
|
+
values.push(value);
|
803
|
+
}
|
575
804
|
}
|
576
805
|
return map;
|
577
806
|
}
|
@@ -584,33 +813,42 @@ function defineDOMEventHandler(event, handler) {
|
|
584
813
|
}
|
585
814
|
var domEventFacet = defineFacet({
|
586
815
|
reduce: () => {
|
587
|
-
|
816
|
+
const setHandlersMap = {};
|
817
|
+
const combinedHandlerMap = {};
|
818
|
+
let plugin;
|
819
|
+
const update = (payloads) => {
|
588
820
|
var _a;
|
589
|
-
let hasNewEvent =
|
590
|
-
for (
|
821
|
+
let hasNewEvent = false;
|
822
|
+
for (const [event] of payloads) {
|
591
823
|
if (!setHandlersMap[event]) {
|
592
|
-
hasNewEvent =
|
593
|
-
|
824
|
+
hasNewEvent = true;
|
825
|
+
const [setHandlers, combinedHandler] = combineEventHandlers();
|
594
826
|
setHandlersMap[event] = setHandlers;
|
595
|
-
|
827
|
+
const e = (view, eventObject) => {
|
828
|
+
return combinedHandler(view, eventObject);
|
829
|
+
};
|
596
830
|
combinedHandlerMap[event] = e;
|
597
831
|
}
|
598
|
-
|
599
|
-
|
600
|
-
|
832
|
+
}
|
833
|
+
const map = groupEntries(payloads);
|
834
|
+
for (const [event, setHandlers] of Object.entries(setHandlersMap)) {
|
835
|
+
const handlers = (_a = map[event]) != null ? _a : [];
|
601
836
|
setHandlers(handlers);
|
602
837
|
}
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
838
|
+
if (hasNewEvent) {
|
839
|
+
plugin = new ProseMirrorPlugin3({
|
840
|
+
key: new PluginKey2("prosekit-dom-event-handler"),
|
841
|
+
props: { handleDOMEvents: combinedHandlerMap }
|
842
|
+
});
|
843
|
+
}
|
607
844
|
};
|
608
|
-
return function(inputs) {
|
609
|
-
|
845
|
+
return function reducer(inputs) {
|
846
|
+
update(inputs);
|
847
|
+
return plugin != null ? plugin : [];
|
610
848
|
};
|
611
849
|
},
|
612
850
|
parent: pluginFacet,
|
613
|
-
singleton:
|
851
|
+
singleton: true
|
614
852
|
});
|
615
853
|
|
616
854
|
// src/extensions/events/editor-event.ts
|
@@ -653,18 +891,45 @@ function defineScrollToSelectionHandler(handler) {
|
|
653
891
|
}
|
654
892
|
var editorEventFacet = defineFacet({
|
655
893
|
reduce: () => {
|
656
|
-
|
657
|
-
return (entries) =>
|
894
|
+
const [update, plugin] = setupEditorEventPlugin();
|
895
|
+
return (entries) => {
|
896
|
+
update(entries);
|
897
|
+
return plugin;
|
898
|
+
};
|
658
899
|
},
|
659
900
|
parent: pluginFacet,
|
660
|
-
singleton:
|
901
|
+
singleton: true
|
661
902
|
});
|
662
903
|
function setupEditorEventPlugin() {
|
663
|
-
|
904
|
+
const [setKeyDownHandlers, handleKeyDown] = combineEventHandlers();
|
905
|
+
const [setKeyPressHandlers, handleKeyPress] = combineEventHandlers();
|
906
|
+
const [setTextInputHandlers, handleTextInput] = combineEventHandlers();
|
907
|
+
const [setClickOnHandlers, handleClickOn] = combineEventHandlers();
|
908
|
+
const [setClickHandlers, handleClick] = combineEventHandlers();
|
909
|
+
const [setDoubleClickOnHandlers, handleDoubleClickOn] = combineEventHandlers();
|
910
|
+
const [setDoubleClickHandlers, handleDoubleClick] = combineEventHandlers();
|
911
|
+
const [setTripleClickOnHandlers, handleTripleClickOn] = combineEventHandlers();
|
912
|
+
const [setTripleClickHandlers, handleTripleClick] = combineEventHandlers();
|
913
|
+
const [setPasteHandlers, handlePaste] = combineEventHandlers();
|
914
|
+
const [setDropHandlers, handleDrop] = combineEventHandlers();
|
915
|
+
const [setScrollToSelectionHandlers, handleScrollToSelection] = combineEventHandlers();
|
916
|
+
const update = (entries) => {
|
664
917
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
665
|
-
|
666
|
-
setKeyDownHandlers((_a = map.keyDown) != null ? _a : [])
|
667
|
-
|
918
|
+
const map = groupEntries(entries);
|
919
|
+
setKeyDownHandlers((_a = map.keyDown) != null ? _a : []);
|
920
|
+
setKeyPressHandlers((_b = map.keyPress) != null ? _b : []);
|
921
|
+
setTextInputHandlers((_c = map.textInput) != null ? _c : []);
|
922
|
+
setClickOnHandlers((_d = map.clickOn) != null ? _d : []);
|
923
|
+
setClickHandlers((_e = map.click) != null ? _e : []);
|
924
|
+
setDoubleClickOnHandlers((_f = map.doubleClickOn) != null ? _f : []);
|
925
|
+
setDoubleClickHandlers((_g = map.doubleClick) != null ? _g : []);
|
926
|
+
setTripleClickOnHandlers((_h = map.tripleClickOn) != null ? _h : []);
|
927
|
+
setTripleClickHandlers((_i = map.tripleClick) != null ? _i : []);
|
928
|
+
setPasteHandlers((_j = map.paste) != null ? _j : []);
|
929
|
+
setDropHandlers((_k = map.drop) != null ? _k : []);
|
930
|
+
setScrollToSelectionHandlers((_l = map.scrollToSelection) != null ? _l : []);
|
931
|
+
};
|
932
|
+
const plugin = new ProseMirrorPlugin4({
|
668
933
|
key: new PluginKey3("prosekit-editor-event"),
|
669
934
|
props: {
|
670
935
|
handleKeyDown,
|
@@ -686,9 +951,11 @@ function setupEditorEventPlugin() {
|
|
686
951
|
|
687
952
|
// src/extensions/events/focus.ts
|
688
953
|
function defineFocusChangeHandler(handler) {
|
954
|
+
const handleFocus = () => handler(true);
|
955
|
+
const handleBlur = () => handler(false);
|
689
956
|
return defineFacetPayload(domEventFacet, [
|
690
|
-
["focus",
|
691
|
-
["blur",
|
957
|
+
["focus", handleFocus],
|
958
|
+
["blur", handleBlur]
|
692
959
|
]);
|
693
960
|
}
|
694
961
|
|
@@ -696,142 +963,202 @@ function defineFocusChangeHandler(handler) {
|
|
696
963
|
import { history, redo, undo } from "@prosekit/pm/history";
|
697
964
|
|
698
965
|
// src/utils/env.ts
|
699
|
-
var isApple = typeof navigator
|
966
|
+
var isApple = typeof navigator !== "undefined" ? /Mac|iP(hone|[ao]d)/.test(navigator.platform) : false;
|
700
967
|
|
701
968
|
// src/extensions/keymap.ts
|
702
|
-
import {
|
703
|
-
baseKeymap,
|
704
|
-
chainCommands,
|
705
|
-
createParagraphNear,
|
706
|
-
liftEmptyBlock,
|
707
|
-
newlineInCode
|
708
|
-
} from "@prosekit/pm/commands";
|
969
|
+
import { chainCommands } from "@prosekit/pm/commands";
|
709
970
|
import { keydownHandler } from "@prosekit/pm/keymap";
|
710
971
|
import { Plugin as Plugin2, PluginKey as PluginKey4 } from "@prosekit/pm/state";
|
711
|
-
|
712
|
-
|
713
|
-
...baseKeymap,
|
714
|
-
Enter: chainCommands(
|
715
|
-
newlineInCode,
|
716
|
-
createParagraphNear,
|
717
|
-
liftEmptyBlock,
|
718
|
-
splitSplittableBlock
|
719
|
-
)
|
720
|
-
};
|
721
|
-
function defineKeymap(keymap) {
|
722
|
-
return defineFacetPayload(keymapFacet, [keymap]);
|
723
|
-
}
|
724
|
-
function defineBaseKeymap(options) {
|
725
|
-
var _a;
|
726
|
-
let priority = (_a = options == null ? void 0 : options.priority) != null ? _a : 1 /* low */;
|
727
|
-
return withPriority(defineKeymap(customBaseKeymap), priority);
|
972
|
+
function defineKeymap(keymap2) {
|
973
|
+
return defineFacetPayload(keymapFacet, [keymap2]);
|
728
974
|
}
|
729
975
|
var keymapFacet = defineFacet({
|
730
976
|
reduce: () => {
|
731
|
-
let handler
|
977
|
+
let handler;
|
978
|
+
const handlerWrapper = (view, event) => {
|
979
|
+
if (handler) return handler(view, event);
|
980
|
+
return false;
|
981
|
+
};
|
982
|
+
const plugin = new Plugin2({
|
732
983
|
key: keymapPluginKey,
|
733
984
|
props: { handleKeyDown: handlerWrapper }
|
734
985
|
});
|
735
|
-
return (keymaps) =>
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
986
|
+
return (keymaps) => {
|
987
|
+
handler = keydownHandler(
|
988
|
+
mergeKeymaps(
|
989
|
+
// The keymap at the end have a higher priority.
|
990
|
+
toReversed(keymaps)
|
991
|
+
)
|
992
|
+
);
|
993
|
+
return plugin;
|
994
|
+
};
|
741
995
|
},
|
742
996
|
parent: pluginFacet,
|
743
|
-
singleton:
|
997
|
+
singleton: true
|
744
998
|
});
|
745
999
|
function mergeKeymaps(keymaps) {
|
746
|
-
|
747
|
-
for (
|
748
|
-
for (
|
749
|
-
|
1000
|
+
const bindings = {};
|
1001
|
+
for (const keymap2 of keymaps) {
|
1002
|
+
for (const [key, command] of Object.entries(keymap2)) {
|
1003
|
+
const commands2 = bindings[key] || (bindings[key] = []);
|
1004
|
+
commands2.push(command);
|
1005
|
+
}
|
1006
|
+
}
|
750
1007
|
return Object.fromEntries(
|
751
|
-
Object.entries(bindings).map(([key,
|
1008
|
+
Object.entries(bindings).map(([key, commands2]) => [
|
752
1009
|
key,
|
753
|
-
chainCommands(...
|
1010
|
+
chainCommands(...commands2)
|
754
1011
|
])
|
755
1012
|
);
|
756
1013
|
}
|
757
1014
|
var keymapPluginKey = new PluginKey4("prosekit-keymap");
|
758
1015
|
|
759
1016
|
// src/extensions/history.ts
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
1017
|
+
var keymap = {
|
1018
|
+
"Mod-z": undo,
|
1019
|
+
"Shift-Mod-z": redo
|
1020
|
+
};
|
1021
|
+
if (!isApple) {
|
1022
|
+
keymap["Mod-y"] = redo;
|
1023
|
+
}
|
1024
|
+
var commands = {
|
1025
|
+
undo: () => undo,
|
1026
|
+
redo: () => redo
|
1027
|
+
};
|
1028
|
+
function defineHistory({
|
1029
|
+
depth = 200,
|
1030
|
+
newGroupDelay = 250
|
1031
|
+
} = {}) {
|
1032
|
+
return union([
|
1033
|
+
definePlugin(history({ depth, newGroupDelay })),
|
767
1034
|
defineKeymap(keymap),
|
768
|
-
defineCommands(
|
769
|
-
undo: () => undo,
|
770
|
-
redo: () => redo
|
771
|
-
})
|
1035
|
+
defineCommands(commands)
|
772
1036
|
]);
|
773
1037
|
}
|
774
1038
|
|
1039
|
+
// src/extensions/keymap-base.ts
|
1040
|
+
import {
|
1041
|
+
baseKeymap,
|
1042
|
+
chainCommands as chainCommands2,
|
1043
|
+
createParagraphNear,
|
1044
|
+
deleteSelection,
|
1045
|
+
joinTextblockBackward,
|
1046
|
+
liftEmptyBlock,
|
1047
|
+
newlineInCode,
|
1048
|
+
selectNodeBackward
|
1049
|
+
} from "@prosekit/pm/commands";
|
1050
|
+
import { splitSplittableBlock } from "prosemirror-splittable";
|
1051
|
+
var customEnter = chainCommands2(
|
1052
|
+
newlineInCode,
|
1053
|
+
createParagraphNear,
|
1054
|
+
liftEmptyBlock,
|
1055
|
+
splitSplittableBlock
|
1056
|
+
);
|
1057
|
+
var customBackspace = chainCommands2(
|
1058
|
+
deleteSelection,
|
1059
|
+
joinTextblockBackward,
|
1060
|
+
selectNodeBackward
|
1061
|
+
);
|
1062
|
+
var customBaseKeymap = {
|
1063
|
+
...baseKeymap,
|
1064
|
+
Enter: customEnter,
|
1065
|
+
Backspace: customBackspace
|
1066
|
+
};
|
1067
|
+
function defineBaseKeymap(options) {
|
1068
|
+
var _a;
|
1069
|
+
const priority = (_a = options == null ? void 0 : options.priority) != null ? _a : 1 /* low */;
|
1070
|
+
return withPriority(defineKeymap(customBaseKeymap), priority);
|
1071
|
+
}
|
1072
|
+
|
775
1073
|
// src/extensions/mark-spec.ts
|
776
1074
|
function defineMarkSpec(options) {
|
777
|
-
|
1075
|
+
const payload = [options, void 0];
|
1076
|
+
return defineFacetPayload(markSpecFacet, [payload]);
|
778
1077
|
}
|
779
1078
|
function defineMarkAttr(options) {
|
780
|
-
|
1079
|
+
const payload = [void 0, options];
|
1080
|
+
return defineFacetPayload(markSpecFacet, [payload]);
|
781
1081
|
}
|
782
1082
|
var markSpecFacet = defineFacet({
|
783
1083
|
reducer: (payloads) => {
|
784
|
-
|
785
|
-
|
786
|
-
|
1084
|
+
const marks = {};
|
1085
|
+
const specPayloads = payloads.map((input) => input[0]).filter(isNotNull);
|
1086
|
+
const attrPayloads = payloads.map((input) => input[1]).filter(isNotNull);
|
1087
|
+
for (const { name, ...spec } of specPayloads) {
|
1088
|
+
if (marks[name]) {
|
787
1089
|
throw new ProseKitError(`Mark type ${name} has already been defined`);
|
1090
|
+
}
|
788
1091
|
marks[name] = spec;
|
789
1092
|
}
|
790
|
-
for (
|
1093
|
+
for (const {
|
791
1094
|
type,
|
792
1095
|
attr,
|
793
1096
|
default: defaultValue,
|
794
1097
|
toDOM,
|
795
1098
|
parseDOM
|
796
1099
|
} of attrPayloads) {
|
797
|
-
|
798
|
-
if (!spec)
|
1100
|
+
const spec = marks[type];
|
1101
|
+
if (!spec) {
|
799
1102
|
throw new ProseKitError(
|
800
1103
|
`Mark type ${type} must be defined before defining attributes`
|
801
1104
|
);
|
802
|
-
|
803
|
-
|
1105
|
+
}
|
1106
|
+
if (!spec.attrs) {
|
1107
|
+
spec.attrs = {};
|
1108
|
+
}
|
1109
|
+
spec.attrs[attr] = { default: defaultValue };
|
1110
|
+
if (toDOM && spec.toDOM) {
|
1111
|
+
const existingToDom = spec.toDOM;
|
804
1112
|
spec.toDOM = (mark, inline) => {
|
805
|
-
|
806
|
-
if (!dom)
|
1113
|
+
const dom = existingToDom(mark, inline);
|
1114
|
+
if (!dom) {
|
807
1115
|
return dom;
|
808
|
-
|
809
|
-
|
1116
|
+
}
|
1117
|
+
const attrDOM = toDOM(mark.attrs[attr]);
|
1118
|
+
if (!attrDOM) {
|
1119
|
+
return dom;
|
1120
|
+
}
|
1121
|
+
const [key, value] = attrDOM;
|
1122
|
+
if (!key) {
|
810
1123
|
return dom;
|
811
|
-
|
812
|
-
|
1124
|
+
}
|
1125
|
+
if (Array.isArray(dom)) {
|
1126
|
+
if (typeof dom[1] === "object") {
|
1127
|
+
return [dom[0], { ...dom[1], [key]: value }, ...dom.slice(2)];
|
1128
|
+
} else {
|
1129
|
+
return [dom[0], { [key]: value }, ...dom.slice(1)];
|
1130
|
+
}
|
1131
|
+
} else if (isElement(dom)) {
|
1132
|
+
dom.setAttribute(key, value);
|
1133
|
+
} else if (typeof dom === "object" && "dom" in dom && isElement(dom.dom)) {
|
1134
|
+
dom.dom.setAttribute(key, value);
|
1135
|
+
}
|
1136
|
+
return dom;
|
813
1137
|
};
|
814
1138
|
}
|
815
|
-
if (parseDOM && spec.parseDOM)
|
816
|
-
for (
|
817
|
-
|
1139
|
+
if (parseDOM && spec.parseDOM) {
|
1140
|
+
for (const rule of spec.parseDOM) {
|
1141
|
+
const existingGetAttrs = rule.getAttrs;
|
1142
|
+
const existingAttrs = rule.attrs;
|
818
1143
|
rule.getAttrs = (dom) => {
|
819
1144
|
var _a;
|
820
|
-
|
821
|
-
if (attrs ===
|
1145
|
+
const attrs = (_a = existingGetAttrs == null ? void 0 : existingGetAttrs(dom)) != null ? _a : existingAttrs;
|
1146
|
+
if (attrs === false || !dom || !isElement(dom)) {
|
822
1147
|
return attrs != null ? attrs : null;
|
823
|
-
|
1148
|
+
}
|
1149
|
+
const value = parseDOM(dom);
|
824
1150
|
return {
|
825
1151
|
...attrs,
|
826
1152
|
[attr]: value
|
827
1153
|
};
|
828
1154
|
};
|
829
1155
|
}
|
1156
|
+
}
|
830
1157
|
}
|
831
1158
|
return { marks, nodes: {} };
|
832
1159
|
},
|
833
1160
|
parent: schemaSpecFacet,
|
834
|
-
singleton:
|
1161
|
+
singleton: true
|
835
1162
|
});
|
836
1163
|
|
837
1164
|
// src/extensions/node-view.ts
|
@@ -842,9 +1169,12 @@ function defineNodeView(options) {
|
|
842
1169
|
}
|
843
1170
|
var nodeViewFacet = defineFacet({
|
844
1171
|
reducer: (inputs) => {
|
845
|
-
|
846
|
-
for (
|
847
|
-
|
1172
|
+
const nodeViews = {};
|
1173
|
+
for (const input of inputs) {
|
1174
|
+
if (!nodeViews[input.name]) {
|
1175
|
+
nodeViews[input.name] = input.constructor;
|
1176
|
+
}
|
1177
|
+
}
|
848
1178
|
return () => [
|
849
1179
|
new ProseMirrorPlugin5({
|
850
1180
|
key: new PluginKey5("prosekit-node-view"),
|
@@ -859,17 +1189,24 @@ var nodeViewFacet = defineFacet({
|
|
859
1189
|
import { PluginKey as PluginKey6, ProseMirrorPlugin as ProseMirrorPlugin6 } from "@prosekit/pm/state";
|
860
1190
|
import "@prosekit/pm/view";
|
861
1191
|
function defineNodeViewFactory(options) {
|
862
|
-
|
1192
|
+
const input = [options, null];
|
1193
|
+
return defineFacetPayload(nodeViewFactoryFacet, [input]);
|
863
1194
|
}
|
864
1195
|
function defineNodeViewComponent(options) {
|
865
|
-
|
1196
|
+
const input = [null, options];
|
1197
|
+
return defineFacetPayload(nodeViewFactoryFacet, [input]);
|
866
1198
|
}
|
1199
|
+
var isServer = typeof window === "undefined";
|
867
1200
|
var nodeViewFactoryFacet = defineFacet({
|
868
1201
|
reducer: (inputs) => {
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
1202
|
+
if (isServer) return [];
|
1203
|
+
const nodeViews = {};
|
1204
|
+
const factories = inputs.map((x) => x[0]).filter(isNotNull);
|
1205
|
+
const options = inputs.map((x) => x[1]).filter(isNotNull);
|
1206
|
+
for (const { group, name, args } of options) {
|
1207
|
+
const factory = factories.find((factory2) => factory2.group === group);
|
1208
|
+
if (!factory) continue;
|
1209
|
+
nodeViews[name] = factory.factory(args);
|
873
1210
|
}
|
874
1211
|
return () => [
|
875
1212
|
new ProseMirrorPlugin6({
|
@@ -907,8 +1244,13 @@ function defineText() {
|
|
907
1244
|
|
908
1245
|
// src/utils/cache.ts
|
909
1246
|
function cache(fn) {
|
910
|
-
let result;
|
911
|
-
return () =>
|
1247
|
+
let result = void 0;
|
1248
|
+
return () => {
|
1249
|
+
if (result === void 0) {
|
1250
|
+
result = fn();
|
1251
|
+
}
|
1252
|
+
return result;
|
1253
|
+
};
|
912
1254
|
}
|
913
1255
|
|
914
1256
|
// src/utils/can-use-regex-lookbehind.ts
|
@@ -916,7 +1258,7 @@ var canUseRegexLookbehind = cache(() => {
|
|
916
1258
|
try {
|
917
1259
|
return "ab".replace(new RegExp("(?<=a)b", "g"), "c") === "ac";
|
918
1260
|
} catch (error) {
|
919
|
-
return
|
1261
|
+
return false;
|
920
1262
|
}
|
921
1263
|
});
|
922
1264
|
|
@@ -927,14 +1269,17 @@ var clsx = clsxLite;
|
|
927
1269
|
// src/utils/collect-nodes.ts
|
928
1270
|
import { ProseMirrorFragment, ProseMirrorNode as ProseMirrorNode2 } from "@prosekit/pm/model";
|
929
1271
|
function collectNodes(content) {
|
930
|
-
if (Array.isArray(content))
|
1272
|
+
if (Array.isArray(content)) {
|
931
1273
|
return content.flatMap(collectNodes);
|
932
|
-
|
1274
|
+
}
|
1275
|
+
if (content instanceof ProseMirrorNode2) {
|
933
1276
|
return [content];
|
1277
|
+
}
|
934
1278
|
if (content instanceof ProseMirrorFragment) {
|
935
|
-
|
936
|
-
for (let i = 0; i < content.childCount; i++)
|
1279
|
+
const nodes = [];
|
1280
|
+
for (let i = 0; i < content.childCount; i++) {
|
937
1281
|
nodes.push(content.child(i));
|
1282
|
+
}
|
938
1283
|
return nodes;
|
939
1284
|
}
|
940
1285
|
throw new ProseKitError(`Invalid node content: ${typeof content}`);
|
@@ -943,7 +1288,7 @@ function collectNodes(content) {
|
|
943
1288
|
// src/utils/default-block-at.ts
|
944
1289
|
function defaultBlockAt(match) {
|
945
1290
|
for (let i = 0; i < match.edgeCount; i++) {
|
946
|
-
|
1291
|
+
const { type } = match.edge(i);
|
947
1292
|
if (type.isTextblock && !type.hasRequiredAttrs()) return type;
|
948
1293
|
}
|
949
1294
|
return null;
|
@@ -952,7 +1297,16 @@ function defaultBlockAt(match) {
|
|
952
1297
|
// src/utils/get-id.ts
|
953
1298
|
var id = 0;
|
954
1299
|
function getId() {
|
955
|
-
|
1300
|
+
id = (id + 1) % Number.MAX_SAFE_INTEGER;
|
1301
|
+
return `id:${id}`;
|
1302
|
+
}
|
1303
|
+
|
1304
|
+
// src/utils/is-at-block-start.ts
|
1305
|
+
function isAtBlockStart(state, view) {
|
1306
|
+
const { $cursor } = state.selection;
|
1307
|
+
if (!$cursor || (view ? !view.endOfTextblock("backward", state) : $cursor.parentOffset > 0))
|
1308
|
+
return null;
|
1309
|
+
return $cursor;
|
956
1310
|
}
|
957
1311
|
|
958
1312
|
// src/utils/is-in-code-block.ts
|
@@ -965,7 +1319,7 @@ function isInCodeBlock(selection) {
|
|
965
1319
|
|
966
1320
|
// src/utils/maybe-run.ts
|
967
1321
|
function maybeRun(value, ...args) {
|
968
|
-
return typeof value
|
1322
|
+
return typeof value === "function" ? value(...args) : value;
|
969
1323
|
}
|
970
1324
|
|
971
1325
|
// src/utils/unicode.ts
|
@@ -973,7 +1327,12 @@ var OBJECT_REPLACEMENT_CHARACTER = "\uFFFC";
|
|
973
1327
|
|
974
1328
|
// src/utils/with-skip-code-block.ts
|
975
1329
|
function withSkipCodeBlock(command) {
|
976
|
-
return (state, dispatch, view) =>
|
1330
|
+
return (state, dispatch, view) => {
|
1331
|
+
if (isInCodeBlock(state.selection)) {
|
1332
|
+
return false;
|
1333
|
+
}
|
1334
|
+
return command(state, dispatch, view);
|
1335
|
+
};
|
977
1336
|
}
|
978
1337
|
export {
|
979
1338
|
Editor,
|
@@ -1036,6 +1395,7 @@ export {
|
|
1036
1395
|
insertNode,
|
1037
1396
|
isAllSelection,
|
1038
1397
|
isApple,
|
1398
|
+
isAtBlockStart,
|
1039
1399
|
isInCodeBlock,
|
1040
1400
|
isMark,
|
1041
1401
|
isMarkAbsent,
|
@@ -1060,6 +1420,9 @@ export {
|
|
1060
1420
|
toggleMark,
|
1061
1421
|
toggleNode,
|
1062
1422
|
union,
|
1423
|
+
unsetBlockType,
|
1424
|
+
unsetMark,
|
1063
1425
|
withPriority,
|
1064
|
-
withSkipCodeBlock
|
1426
|
+
withSkipCodeBlock,
|
1427
|
+
wrap
|
1065
1428
|
};
|