@kerebron/extension-odt 0.0.8

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.
Files changed (49) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +57 -0
  3. package/esm/editor/src/CoreEditor.d.ts +28 -0
  4. package/esm/editor/src/CoreEditor.d.ts.map +1 -0
  5. package/esm/editor/src/CoreEditor.js +170 -0
  6. package/esm/editor/src/Extension.d.ts +26 -0
  7. package/esm/editor/src/Extension.d.ts.map +1 -0
  8. package/esm/editor/src/Extension.js +33 -0
  9. package/esm/editor/src/ExtensionManager.d.ts +32 -0
  10. package/esm/editor/src/ExtensionManager.d.ts.map +1 -0
  11. package/esm/editor/src/ExtensionManager.js +253 -0
  12. package/esm/editor/src/Mark.d.ts +18 -0
  13. package/esm/editor/src/Mark.d.ts.map +1 -0
  14. package/esm/editor/src/Mark.js +34 -0
  15. package/esm/editor/src/Node.d.ts +27 -0
  16. package/esm/editor/src/Node.d.ts.map +1 -0
  17. package/esm/editor/src/Node.js +43 -0
  18. package/esm/editor/src/commands/CommandManager.d.ts +20 -0
  19. package/esm/editor/src/commands/CommandManager.d.ts.map +1 -0
  20. package/esm/editor/src/commands/CommandManager.js +60 -0
  21. package/esm/editor/src/commands/createChainableState.d.ts +3 -0
  22. package/esm/editor/src/commands/createChainableState.d.ts.map +1 -0
  23. package/esm/editor/src/commands/createChainableState.js +29 -0
  24. package/esm/editor/src/commands/mod.d.ts +49 -0
  25. package/esm/editor/src/commands/mod.d.ts.map +1 -0
  26. package/esm/editor/src/commands/mod.js +928 -0
  27. package/esm/editor/src/mod.d.ts +5 -0
  28. package/esm/editor/src/mod.d.ts.map +1 -0
  29. package/esm/editor/src/mod.js +4 -0
  30. package/esm/editor/src/plugins/input-rules/InputRulesPlugin.d.ts +23 -0
  31. package/esm/editor/src/plugins/input-rules/InputRulesPlugin.d.ts.map +1 -0
  32. package/esm/editor/src/plugins/input-rules/InputRulesPlugin.js +163 -0
  33. package/esm/editor/src/types.d.ts +29 -0
  34. package/esm/editor/src/types.d.ts.map +1 -0
  35. package/esm/editor/src/types.js +1 -0
  36. package/esm/editor/src/utilities/createNodeFromContent.d.ts +12 -0
  37. package/esm/editor/src/utilities/createNodeFromContent.d.ts.map +1 -0
  38. package/esm/editor/src/utilities/createNodeFromContent.js +118 -0
  39. package/esm/editor/src/utilities/getHtmlAttributes.d.ts +4 -0
  40. package/esm/editor/src/utilities/getHtmlAttributes.d.ts.map +1 -0
  41. package/esm/editor/src/utilities/getHtmlAttributes.js +47 -0
  42. package/esm/extension-odt/src/ExtensionOdt.d.ts +7 -0
  43. package/esm/extension-odt/src/ExtensionOdt.d.ts.map +1 -0
  44. package/esm/extension-odt/src/ExtensionOdt.js +32 -0
  45. package/esm/extension-odt/src/OdtParser.d.ts +23 -0
  46. package/esm/extension-odt/src/OdtParser.d.ts.map +1 -0
  47. package/esm/extension-odt/src/OdtParser.js +315 -0
  48. package/esm/package.json +3 -0
  49. package/package.json +23 -0
@@ -0,0 +1,928 @@
1
+ import { canJoin, canSplit, findWrapping, joinPoint, liftTarget, ReplaceAroundStep, ReplaceStep, replaceStep, } from 'prosemirror-transform';
2
+ import { Fragment, NodeRange, Slice, } from 'prosemirror-model';
3
+ import { AllSelection, NodeSelection, Selection, SelectionRange, TextSelection, } from 'prosemirror-state';
4
+ /// Returns a command function that wraps the selection in a list with
5
+ /// the given type an attributes. If `dispatch` is null, only return a
6
+ /// value to indicate whether this is possible, but don't actually
7
+ /// perform the change.
8
+ export function wrapInList(listType, attrs = null) {
9
+ return function (state, dispatch) {
10
+ let { $from, $to } = state.selection;
11
+ let range = $from.blockRange($to);
12
+ if (!range)
13
+ return false;
14
+ let tr = dispatch ? state.tr : null;
15
+ if (!wrapRangeInList(tr, range, listType, attrs))
16
+ return false;
17
+ if (dispatch)
18
+ dispatch(tr.scrollIntoView());
19
+ return true;
20
+ };
21
+ }
22
+ /// Try to wrap the given node range in a list of the given type.
23
+ /// Return `true` when this is possible, `false` otherwise. When `tr`
24
+ /// is non-null, the wrapping is added to that transaction. When it is
25
+ /// `null`, the function only queries whether the wrapping is
26
+ /// possible.
27
+ export function wrapRangeInList(tr, range, listType, attrs = null) {
28
+ let doJoin = false, outerRange = range, doc = range.$from.doc;
29
+ // This is at the top of an existing list item
30
+ if (range.depth >= 2 &&
31
+ range.$from.node(range.depth - 1).type.compatibleContent(listType) &&
32
+ range.startIndex == 0) {
33
+ // Don't do anything if this is the top of the list
34
+ if (range.$from.index(range.depth - 1) == 0)
35
+ return false;
36
+ let $insert = doc.resolve(range.start - 2);
37
+ outerRange = new NodeRange($insert, $insert, range.depth);
38
+ if (range.endIndex < range.parent.childCount) {
39
+ range = new NodeRange(range.$from, doc.resolve(range.$to.end(range.depth)), range.depth);
40
+ }
41
+ doJoin = true;
42
+ }
43
+ let wrap = findWrapping(outerRange, listType, attrs, range);
44
+ if (!wrap)
45
+ return false;
46
+ if (tr)
47
+ doWrapInList(tr, range, wrap, doJoin, listType);
48
+ return true;
49
+ }
50
+ function doWrapInList(tr, range, wrappers, joinBefore, listType) {
51
+ let content = Fragment.empty;
52
+ for (let i = wrappers.length - 1; i >= 0; i--) {
53
+ content = Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content));
54
+ }
55
+ tr.step(new ReplaceAroundStep(range.start - (joinBefore ? 2 : 0), range.end, range.start, range.end, new Slice(content, 0, 0), wrappers.length, true));
56
+ let found = 0;
57
+ for (let i = 0; i < wrappers.length; i++) {
58
+ if (wrappers[i].type == listType)
59
+ found = i + 1;
60
+ }
61
+ let splitDepth = wrappers.length - found;
62
+ let splitPos = range.start + wrappers.length - (joinBefore ? 2 : 0), parent = range.parent;
63
+ for (let i = range.startIndex, e = range.endIndex, first = true; i < e; i++, first = false) {
64
+ if (!first && canSplit(tr.doc, splitPos, splitDepth)) {
65
+ tr.split(splitPos, splitDepth);
66
+ splitPos += 2 * splitDepth;
67
+ }
68
+ splitPos += parent.child(i).nodeSize;
69
+ }
70
+ return tr;
71
+ }
72
+ /// Delete the selection, if there is one.
73
+ export const deleteSelection = (state, dispatch) => {
74
+ if (state.selection.empty)
75
+ return false;
76
+ if (dispatch)
77
+ dispatch(state.tr.deleteSelection().scrollIntoView());
78
+ return true;
79
+ };
80
+ function atBlockStart(state, view) {
81
+ let { $cursor } = state.selection;
82
+ if (!$cursor ||
83
+ (view ? !view.endOfTextblock('backward', state) : $cursor.parentOffset > 0)) {
84
+ return null;
85
+ }
86
+ return $cursor;
87
+ }
88
+ /// If the selection is empty and at the start of a textblock, try to
89
+ /// reduce the distance between that block and the one before it—if
90
+ /// there's a block directly before it that can be joined, join them.
91
+ /// If not, try to move the selected block closer to the next one in
92
+ /// the document structure by lifting it out of its parent or moving it
93
+ /// into a parent of the previous block. Will use the view for accurate
94
+ /// (bidi-aware) start-of-textblock detection if given.
95
+ export const joinBackward = (state, dispatch, view) => {
96
+ let $cursor = atBlockStart(state, view);
97
+ if (!$cursor)
98
+ return false;
99
+ let $cut = findCutBefore($cursor);
100
+ // If there is no node before this, try to lift
101
+ if (!$cut) {
102
+ let range = $cursor.blockRange(), target = range && liftTarget(range);
103
+ if (target == null)
104
+ return false;
105
+ if (dispatch)
106
+ dispatch(state.tr.lift(range, target).scrollIntoView());
107
+ return true;
108
+ }
109
+ let before = $cut.nodeBefore;
110
+ // Apply the joining algorithm
111
+ if (deleteBarrier(state, $cut, dispatch, -1))
112
+ return true;
113
+ // If the node below has no content and the node above is
114
+ // selectable, delete the node below and select the one above.
115
+ if ($cursor.parent.content.size == 0 &&
116
+ (textblockAt(before, 'end') || NodeSelection.isSelectable(before))) {
117
+ for (let depth = $cursor.depth;; depth--) {
118
+ let delStep = replaceStep(state.doc, $cursor.before(depth), $cursor.after(depth), Slice.empty);
119
+ if (delStep &&
120
+ delStep.slice.size <
121
+ delStep.to - delStep.from) {
122
+ if (dispatch) {
123
+ let tr = state.tr.step(delStep);
124
+ tr.setSelection(textblockAt(before, 'end')
125
+ ? Selection.findFrom(tr.doc.resolve(tr.mapping.map($cut.pos, -1)), -1)
126
+ : NodeSelection.create(tr.doc, $cut.pos - before.nodeSize));
127
+ dispatch(tr.scrollIntoView());
128
+ }
129
+ return true;
130
+ }
131
+ if (depth == 1 || $cursor.node(depth - 1).childCount > 1)
132
+ break;
133
+ }
134
+ }
135
+ // If the node before is an atom, delete it
136
+ if (before.isAtom && $cut.depth == $cursor.depth - 1) {
137
+ if (dispatch) {
138
+ dispatch(state.tr.delete($cut.pos - before.nodeSize, $cut.pos).scrollIntoView());
139
+ }
140
+ return true;
141
+ }
142
+ return false;
143
+ };
144
+ /// A more limited form of [`joinBackward`](#commands.joinBackward)
145
+ /// that only tries to join the current textblock to the one before
146
+ /// it, if the cursor is at the start of a textblock.
147
+ export const joinTextblockBackward = (state, dispatch, view) => {
148
+ let $cursor = atBlockStart(state, view);
149
+ if (!$cursor)
150
+ return false;
151
+ let $cut = findCutBefore($cursor);
152
+ return $cut ? joinTextblocksAround(state, $cut, dispatch) : false;
153
+ };
154
+ /// A more limited form of [`joinForward`](#commands.joinForward)
155
+ /// that only tries to join the current textblock to the one after
156
+ /// it, if the cursor is at the end of a textblock.
157
+ export const joinTextblockForward = (state, dispatch, view) => {
158
+ let $cursor = atBlockEnd(state, view);
159
+ if (!$cursor)
160
+ return false;
161
+ let $cut = findCutAfter($cursor);
162
+ return $cut ? joinTextblocksAround(state, $cut, dispatch) : false;
163
+ };
164
+ function joinTextblocksAround(state, $cut, dispatch) {
165
+ let before = $cut.nodeBefore, beforeText = before, beforePos = $cut.pos - 1;
166
+ for (; !beforeText.isTextblock; beforePos--) {
167
+ if (beforeText.type.spec.isolating)
168
+ return false;
169
+ let child = beforeText.lastChild;
170
+ if (!child)
171
+ return false;
172
+ beforeText = child;
173
+ }
174
+ let after = $cut.nodeAfter, afterText = after, afterPos = $cut.pos + 1;
175
+ for (; !afterText.isTextblock; afterPos++) {
176
+ if (afterText.type.spec.isolating)
177
+ return false;
178
+ let child = afterText.firstChild;
179
+ if (!child)
180
+ return false;
181
+ afterText = child;
182
+ }
183
+ let step = replaceStep(state.doc, beforePos, afterPos, Slice.empty);
184
+ if (!step || step.from != beforePos ||
185
+ step instanceof ReplaceStep && step.slice.size >= afterPos - beforePos)
186
+ return false;
187
+ if (dispatch) {
188
+ let tr = state.tr.step(step);
189
+ tr.setSelection(TextSelection.create(tr.doc, beforePos));
190
+ dispatch(tr.scrollIntoView());
191
+ }
192
+ return true;
193
+ }
194
+ function textblockAt(node, side, only = false) {
195
+ for (let scan = node; scan; scan = side == 'start' ? scan.firstChild : scan.lastChild) {
196
+ if (scan.isTextblock)
197
+ return true;
198
+ if (only && scan.childCount != 1)
199
+ return false;
200
+ }
201
+ return false;
202
+ }
203
+ /// When the selection is empty and at the start of a textblock, select
204
+ /// the node before that textblock, if possible. This is intended to be
205
+ /// bound to keys like backspace, after
206
+ /// [`joinBackward`](#commands.joinBackward) or other deleting
207
+ /// commands, as a fall-back behavior when the schema doesn't allow
208
+ /// deletion at the selected point.
209
+ export const selectNodeBackward = (state, dispatch, view) => {
210
+ let { $head, empty } = state.selection, $cut = $head;
211
+ if (!empty)
212
+ return false;
213
+ if ($head.parent.isTextblock) {
214
+ if (view ? !view.endOfTextblock('backward', state) : $head.parentOffset > 0)
215
+ return false;
216
+ $cut = findCutBefore($head);
217
+ }
218
+ let node = $cut && $cut.nodeBefore;
219
+ if (!node || !NodeSelection.isSelectable(node))
220
+ return false;
221
+ if (dispatch) {
222
+ dispatch(state.tr.setSelection(NodeSelection.create(state.doc, $cut.pos - node.nodeSize)).scrollIntoView());
223
+ }
224
+ return true;
225
+ };
226
+ function findCutBefore($pos) {
227
+ if (!$pos.parent.type.spec.isolating) {
228
+ for (let i = $pos.depth - 1; i >= 0; i--) {
229
+ if ($pos.index(i) > 0) {
230
+ return $pos.doc.resolve($pos.before(i + 1));
231
+ }
232
+ if ($pos.node(i).type.spec.isolating)
233
+ break;
234
+ }
235
+ }
236
+ return null;
237
+ }
238
+ function atBlockEnd(state, view) {
239
+ let { $cursor } = state.selection;
240
+ if (!$cursor ||
241
+ (view
242
+ ? !view.endOfTextblock('forward', state)
243
+ : $cursor.parentOffset < $cursor.parent.content.size)) {
244
+ return null;
245
+ }
246
+ return $cursor;
247
+ }
248
+ /// If the selection is empty and the cursor is at the end of a
249
+ /// textblock, try to reduce or remove the boundary between that block
250
+ /// and the one after it, either by joining them or by moving the other
251
+ /// block closer to this one in the tree structure. Will use the view
252
+ /// for accurate start-of-textblock detection if given.
253
+ export const joinForward = (state, dispatch, view) => {
254
+ let $cursor = atBlockEnd(state, view);
255
+ if (!$cursor)
256
+ return false;
257
+ let $cut = findCutAfter($cursor);
258
+ // If there is no node after this, there's nothing to do
259
+ if (!$cut)
260
+ return false;
261
+ let after = $cut.nodeAfter;
262
+ // Try the joining algorithm
263
+ if (deleteBarrier(state, $cut, dispatch, 1))
264
+ return true;
265
+ // If the node above has no content and the node below is
266
+ // selectable, delete the node above and select the one below.
267
+ if ($cursor.parent.content.size == 0 &&
268
+ (textblockAt(after, 'start') || NodeSelection.isSelectable(after))) {
269
+ let delStep = replaceStep(state.doc, $cursor.before(), $cursor.after(), Slice.empty);
270
+ if (delStep &&
271
+ delStep.slice.size <
272
+ delStep.to - delStep.from) {
273
+ if (dispatch) {
274
+ let tr = state.tr.step(delStep);
275
+ tr.setSelection(textblockAt(after, 'start')
276
+ ? Selection.findFrom(tr.doc.resolve(tr.mapping.map($cut.pos)), 1)
277
+ : NodeSelection.create(tr.doc, tr.mapping.map($cut.pos)));
278
+ dispatch(tr.scrollIntoView());
279
+ }
280
+ return true;
281
+ }
282
+ }
283
+ // If the next node is an atom, delete it
284
+ if (after.isAtom && $cut.depth == $cursor.depth - 1) {
285
+ if (dispatch) {
286
+ dispatch(state.tr.delete($cut.pos, $cut.pos + after.nodeSize).scrollIntoView());
287
+ }
288
+ return true;
289
+ }
290
+ return false;
291
+ };
292
+ /// When the selection is empty and at the end of a textblock, select
293
+ /// the node coming after that textblock, if possible. This is intended
294
+ /// to be bound to keys like delete, after
295
+ /// [`joinForward`](#commands.joinForward) and similar deleting
296
+ /// commands, to provide a fall-back behavior when the schema doesn't
297
+ /// allow deletion at the selected point.
298
+ export const selectNodeForward = (state, dispatch, view) => {
299
+ let { $head, empty } = state.selection, $cut = $head;
300
+ if (!empty)
301
+ return false;
302
+ if ($head.parent.isTextblock) {
303
+ if (view
304
+ ? !view.endOfTextblock('forward', state)
305
+ : $head.parentOffset < $head.parent.content.size) {
306
+ return false;
307
+ }
308
+ $cut = findCutAfter($head);
309
+ }
310
+ let node = $cut && $cut.nodeAfter;
311
+ if (!node || !NodeSelection.isSelectable(node))
312
+ return false;
313
+ if (dispatch) {
314
+ dispatch(state.tr.setSelection(NodeSelection.create(state.doc, $cut.pos))
315
+ .scrollIntoView());
316
+ }
317
+ return true;
318
+ };
319
+ function findCutAfter($pos) {
320
+ if (!$pos.parent.type.spec.isolating) {
321
+ for (let i = $pos.depth - 1; i >= 0; i--) {
322
+ let parent = $pos.node(i);
323
+ if ($pos.index(i) + 1 < parent.childCount) {
324
+ return $pos.doc.resolve($pos.after(i + 1));
325
+ }
326
+ if (parent.type.spec.isolating) {
327
+ break;
328
+ }
329
+ }
330
+ }
331
+ return null;
332
+ }
333
+ /// Join the selected block or, if there is a text selection, the
334
+ /// closest ancestor block of the selection that can be joined, with
335
+ /// the sibling above it.
336
+ export const joinUp = (state, dispatch) => {
337
+ let sel = state.selection, nodeSel = sel instanceof NodeSelection, point;
338
+ if (nodeSel) {
339
+ if (sel.node.isTextblock || !canJoin(state.doc, sel.from))
340
+ return false;
341
+ point = sel.from;
342
+ }
343
+ else {
344
+ point = joinPoint(state.doc, sel.from, -1);
345
+ if (point == null)
346
+ return false;
347
+ }
348
+ if (dispatch) {
349
+ let tr = state.tr.join(point);
350
+ if (nodeSel) {
351
+ tr.setSelection(NodeSelection.create(tr.doc, point - state.doc.resolve(point).nodeBefore.nodeSize));
352
+ }
353
+ dispatch(tr.scrollIntoView());
354
+ }
355
+ return true;
356
+ };
357
+ /// Join the selected block, or the closest ancestor of the selection
358
+ /// that can be joined, with the sibling after it.
359
+ export const joinDown = (state, dispatch) => {
360
+ let sel = state.selection, point;
361
+ if (sel instanceof NodeSelection) {
362
+ if (sel.node.isTextblock || !canJoin(state.doc, sel.to))
363
+ return false;
364
+ point = sel.to;
365
+ }
366
+ else {
367
+ point = joinPoint(state.doc, sel.to, 1);
368
+ if (point == null)
369
+ return false;
370
+ }
371
+ if (dispatch) {
372
+ dispatch(state.tr.join(point).scrollIntoView());
373
+ }
374
+ return true;
375
+ };
376
+ /// Lift the selected block, or the closest ancestor block of the
377
+ /// selection that can be lifted, out of its parent node.
378
+ export const lift = (state, dispatch) => {
379
+ let { $from, $to } = state.selection;
380
+ let range = $from.blockRange($to), target = range && liftTarget(range);
381
+ if (target == null)
382
+ return false;
383
+ if (dispatch)
384
+ dispatch(state.tr.lift(range, target).scrollIntoView());
385
+ return true;
386
+ };
387
+ /// If the selection is in a node whose type has a truthy
388
+ /// [`code`](#model.NodeSpec.code) property in its spec, replace the
389
+ /// selection with a newline character.
390
+ export const newlineInCode = (state, dispatch) => {
391
+ let { $head, $anchor } = state.selection;
392
+ if (!$head.parent.type.spec.code || !$head.sameParent($anchor))
393
+ return false;
394
+ if (dispatch)
395
+ dispatch(state.tr.insertText('\n').scrollIntoView());
396
+ return true;
397
+ };
398
+ function defaultBlockAt(match) {
399
+ for (let i = 0; i < match.edgeCount; i++) {
400
+ let { type } = match.edge(i);
401
+ if (type.isTextblock && !type.hasRequiredAttrs())
402
+ return type;
403
+ }
404
+ return null;
405
+ }
406
+ /// When the selection is in a node with a truthy
407
+ /// [`code`](#model.NodeSpec.code) property in its spec, create a
408
+ /// default block after the code block, and move the cursor there.
409
+ export const exitCode = (state, dispatch) => {
410
+ let { $head, $anchor } = state.selection;
411
+ if (!$head.parent.type.spec.code || !$head.sameParent($anchor))
412
+ return false;
413
+ let above = $head.node(-1), after = $head.indexAfter(-1), type = defaultBlockAt(above.contentMatchAt(after));
414
+ if (!type || !above.canReplaceWith(after, after, type))
415
+ return false;
416
+ if (dispatch) {
417
+ let pos = $head.after(), tr = state.tr.replaceWith(pos, pos, type.createAndFill());
418
+ tr.setSelection(Selection.near(tr.doc.resolve(pos), 1));
419
+ dispatch(tr.scrollIntoView());
420
+ }
421
+ return true;
422
+ };
423
+ /// If a block node is selected, create an empty paragraph before (if
424
+ /// it is its parent's first child) or after it.
425
+ export const createParagraphNear = (state, dispatch) => {
426
+ let sel = state.selection, { $from, $to } = sel;
427
+ if (sel instanceof AllSelection || $from.parent.inlineContent ||
428
+ $to.parent.inlineContent)
429
+ return false;
430
+ let type = defaultBlockAt($to.parent.contentMatchAt($to.indexAfter()));
431
+ if (!type || !type.isTextblock)
432
+ return false;
433
+ if (dispatch) {
434
+ let side = (!$from.parentOffset && $to.index() < $to.parent.childCount ? $from : $to)
435
+ .pos;
436
+ let tr = state.tr.insert(side, type.createAndFill());
437
+ tr.setSelection(TextSelection.create(tr.doc, side + 1));
438
+ dispatch(tr.scrollIntoView());
439
+ }
440
+ return true;
441
+ };
442
+ /// If the cursor is in an empty textblock that can be lifted, lift the
443
+ /// block.
444
+ export const liftEmptyBlock = (state, dispatch) => {
445
+ let { $cursor } = state.selection;
446
+ if (!$cursor || $cursor.parent.content.size)
447
+ return false;
448
+ if ($cursor.depth > 1 && $cursor.after() != $cursor.end(-1)) {
449
+ let before = $cursor.before();
450
+ if (canSplit(state.doc, before)) {
451
+ if (dispatch)
452
+ dispatch(state.tr.split(before).scrollIntoView());
453
+ return true;
454
+ }
455
+ }
456
+ let range = $cursor.blockRange(), target = range && liftTarget(range);
457
+ if (target == null)
458
+ return false;
459
+ if (dispatch)
460
+ dispatch(state.tr.lift(range, target).scrollIntoView());
461
+ return true;
462
+ };
463
+ /// Create a variant of [`splitBlock`](#commands.splitBlock) that uses
464
+ /// a custom function to determine the type of the newly split off block.
465
+ export function splitBlockAs(splitNode) {
466
+ return (state, dispatch) => {
467
+ let { $from, $to } = state.selection;
468
+ if (state.selection instanceof NodeSelection && state.selection.node.isBlock) {
469
+ if (!$from.parentOffset || !canSplit(state.doc, $from.pos))
470
+ return false;
471
+ if (dispatch)
472
+ dispatch(state.tr.split($from.pos).scrollIntoView());
473
+ return true;
474
+ }
475
+ if (!$from.depth)
476
+ return false;
477
+ let types = [];
478
+ let splitDepth, deflt, atEnd = false, atStart = false;
479
+ for (let d = $from.depth;; d--) {
480
+ let node = $from.node(d);
481
+ if (node.isBlock) {
482
+ atEnd = $from.end(d) == $from.pos + ($from.depth - d);
483
+ atStart = $from.start(d) == $from.pos - ($from.depth - d);
484
+ deflt = defaultBlockAt($from.node(d - 1).contentMatchAt($from.indexAfter(d - 1)));
485
+ let splitType = splitNode && splitNode($to.parent, atEnd, $from);
486
+ types.unshift(splitType || (atEnd && deflt ? { type: deflt } : null));
487
+ splitDepth = d;
488
+ break;
489
+ }
490
+ else {
491
+ if (d == 1)
492
+ return false;
493
+ types.unshift(null);
494
+ }
495
+ }
496
+ let tr = state.tr;
497
+ if (state.selection instanceof TextSelection ||
498
+ state.selection instanceof AllSelection)
499
+ tr.deleteSelection();
500
+ let splitPos = tr.mapping.map($from.pos);
501
+ let can = canSplit(tr.doc, splitPos, types.length, types);
502
+ if (!can) {
503
+ types[0] = deflt ? { type: deflt } : null;
504
+ can = canSplit(tr.doc, splitPos, types.length, types);
505
+ }
506
+ tr.split(splitPos, types.length, types);
507
+ if (!atEnd && atStart && $from.node(splitDepth).type != deflt) {
508
+ let first = tr.mapping.map($from.before(splitDepth)), $first = tr.doc.resolve(first);
509
+ if (deflt &&
510
+ $from.node(splitDepth - 1).canReplaceWith($first.index(), $first.index() + 1, deflt)) {
511
+ tr.setNodeMarkup(tr.mapping.map($from.before(splitDepth)), deflt);
512
+ }
513
+ }
514
+ if (dispatch)
515
+ dispatch(tr.scrollIntoView());
516
+ return true;
517
+ };
518
+ }
519
+ /// Split the parent block of the selection. If the selection is a text
520
+ /// selection, also delete its content.
521
+ export const splitBlock = splitBlockAs();
522
+ /// Acts like [`splitBlock`](#commands.splitBlock), but without
523
+ /// resetting the set of active marks at the cursor.
524
+ export const splitBlockKeepMarks = (state, dispatch) => {
525
+ return splitBlock(state, dispatch && ((tr) => {
526
+ let marks = state.storedMarks ||
527
+ (state.selection.$to.parentOffset && state.selection.$from.marks());
528
+ if (marks)
529
+ tr.ensureMarks(marks);
530
+ dispatch(tr);
531
+ }));
532
+ };
533
+ /// Move the selection to the node wrapping the current selection, if
534
+ /// any. (Will not select the document node.)
535
+ export const selectParentNode = (state, dispatch) => {
536
+ let { $from, to } = state.selection, pos;
537
+ let same = $from.sharedDepth(to);
538
+ if (same == 0)
539
+ return false;
540
+ pos = $from.before(same);
541
+ if (dispatch) {
542
+ dispatch(state.tr.setSelection(NodeSelection.create(state.doc, pos)));
543
+ }
544
+ return true;
545
+ };
546
+ /// Select the whole document.
547
+ export const selectAll = (state, dispatch) => {
548
+ if (dispatch)
549
+ dispatch(state.tr.setSelection(new AllSelection(state.doc)));
550
+ return true;
551
+ };
552
+ function joinMaybeClear(state, $pos, dispatch) {
553
+ let before = $pos.nodeBefore, after = $pos.nodeAfter, index = $pos.index();
554
+ if (!before || !after || !before.type.compatibleContent(after.type)) {
555
+ return false;
556
+ }
557
+ if (!before.content.size && $pos.parent.canReplace(index - 1, index)) {
558
+ if (dispatch) {
559
+ dispatch(state.tr.delete($pos.pos - before.nodeSize, $pos.pos).scrollIntoView());
560
+ }
561
+ return true;
562
+ }
563
+ if (!$pos.parent.canReplace(index, index + 1) ||
564
+ !(after.isTextblock || canJoin(state.doc, $pos.pos))) {
565
+ return false;
566
+ }
567
+ if (dispatch) {
568
+ dispatch(state.tr.join($pos.pos).scrollIntoView());
569
+ }
570
+ return true;
571
+ }
572
+ function deleteBarrier(state, $cut, dispatch, dir) {
573
+ let before = $cut.nodeBefore, after = $cut.nodeAfter, conn, match;
574
+ let isolated = before.type.spec.isolating || after.type.spec.isolating;
575
+ if (!isolated && joinMaybeClear(state, $cut, dispatch))
576
+ return true;
577
+ let canDelAfter = !isolated &&
578
+ $cut.parent.canReplace($cut.index(), $cut.index() + 1);
579
+ if (canDelAfter &&
580
+ (conn = (match = before.contentMatchAt(before.childCount)).findWrapping(after.type)) &&
581
+ match.matchType(conn[0] || after.type).validEnd) {
582
+ if (dispatch) {
583
+ let end = $cut.pos + after.nodeSize, wrap = Fragment.empty;
584
+ for (let i = conn.length - 1; i >= 0; i--) {
585
+ wrap = Fragment.from(conn[i].create(null, wrap));
586
+ }
587
+ wrap = Fragment.from(before.copy(wrap));
588
+ let tr = state.tr.step(new ReplaceAroundStep($cut.pos - 1, end, $cut.pos, end, new Slice(wrap, 1, 0), conn.length, true));
589
+ let $joinAt = tr.doc.resolve(end + 2 * conn.length);
590
+ if ($joinAt.nodeAfter && $joinAt.nodeAfter.type == before.type &&
591
+ canJoin(tr.doc, $joinAt.pos))
592
+ tr.join($joinAt.pos);
593
+ dispatch(tr.scrollIntoView());
594
+ }
595
+ return true;
596
+ }
597
+ let selAfter = after.type.spec.isolating || (dir > 0 && isolated)
598
+ ? null
599
+ : Selection.findFrom($cut, 1);
600
+ let range = selAfter && selAfter.$from.blockRange(selAfter.$to), target = range && liftTarget(range);
601
+ if (target != null && target >= $cut.depth) {
602
+ if (dispatch)
603
+ dispatch(state.tr.lift(range, target).scrollIntoView());
604
+ return true;
605
+ }
606
+ if (canDelAfter && textblockAt(after, 'start', true) &&
607
+ textblockAt(before, 'end')) {
608
+ let at = before, wrap = [];
609
+ for (;;) {
610
+ wrap.push(at);
611
+ if (at.isTextblock)
612
+ break;
613
+ at = at.lastChild;
614
+ }
615
+ let afterText = after, afterDepth = 1;
616
+ for (; !afterText.isTextblock; afterText = afterText.firstChild) {
617
+ afterDepth++;
618
+ }
619
+ if (at.canReplace(at.childCount, at.childCount, afterText.content)) {
620
+ if (dispatch) {
621
+ let end = Fragment.empty;
622
+ for (let i = wrap.length - 1; i >= 0; i--) {
623
+ end = Fragment.from(wrap[i].copy(end));
624
+ }
625
+ let tr = state.tr.step(new ReplaceAroundStep($cut.pos - wrap.length, $cut.pos + after.nodeSize, $cut.pos + afterDepth, $cut.pos + after.nodeSize - afterDepth, new Slice(end, wrap.length, 0), 0, true));
626
+ dispatch(tr.scrollIntoView());
627
+ }
628
+ return true;
629
+ }
630
+ }
631
+ return false;
632
+ }
633
+ function selectTextblockSide(side) {
634
+ return function (state, dispatch) {
635
+ let sel = state.selection, $pos = side < 0 ? sel.$from : sel.$to;
636
+ let depth = $pos.depth;
637
+ while ($pos.node(depth).isInline) {
638
+ if (!depth)
639
+ return false;
640
+ depth--;
641
+ }
642
+ if (!$pos.node(depth).isTextblock)
643
+ return false;
644
+ if (dispatch) {
645
+ dispatch(state.tr.setSelection(TextSelection.create(state.doc, side < 0 ? $pos.start(depth) : $pos.end(depth))));
646
+ }
647
+ return true;
648
+ };
649
+ }
650
+ /// Moves the cursor to the start of current text block.
651
+ export const selectTextblockStart = selectTextblockSide(-1);
652
+ /// Moves the cursor to the end of current text block.
653
+ export const selectTextblockEnd = selectTextblockSide(1);
654
+ // Parameterized commands
655
+ /// Wrap the selection in a node of the given type with the given
656
+ /// attributes.
657
+ export function wrapIn(nodeType, attrs = null) {
658
+ return function (state, dispatch) {
659
+ let { $from, $to } = state.selection;
660
+ let range = $from.blockRange($to), wrapping = range && findWrapping(range, nodeType, attrs);
661
+ if (!wrapping)
662
+ return false;
663
+ if (dispatch)
664
+ dispatch(state.tr.wrap(range, wrapping).scrollIntoView());
665
+ return true;
666
+ };
667
+ }
668
+ /// Returns a command that tries to set the selected textblocks to the
669
+ /// given node type with the given attributes.
670
+ export function setBlockType(nodeType, attrs = null) {
671
+ return function (state, dispatch) {
672
+ let applicable = false;
673
+ for (let i = 0; i < state.selection.ranges.length && !applicable; i++) {
674
+ let { $from: { pos: from }, $to: { pos: to } } = state.selection.ranges[i];
675
+ state.doc.nodesBetween(from, to, (node, pos) => {
676
+ if (applicable)
677
+ return false;
678
+ if (!node.isTextblock || node.hasMarkup(nodeType, attrs))
679
+ return;
680
+ if (node.type == nodeType) {
681
+ applicable = true;
682
+ }
683
+ else {
684
+ let $pos = state.doc.resolve(pos), index = $pos.index();
685
+ applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType);
686
+ }
687
+ });
688
+ }
689
+ if (!applicable)
690
+ return false;
691
+ if (dispatch) {
692
+ let tr = state.tr;
693
+ for (let i = 0; i < state.selection.ranges.length; i++) {
694
+ let { $from: { pos: from }, $to: { pos: to } } = state.selection.ranges[i];
695
+ tr.setBlockType(from, to, nodeType, attrs);
696
+ }
697
+ dispatch(tr.scrollIntoView());
698
+ }
699
+ return true;
700
+ };
701
+ }
702
+ function markApplies(doc, ranges, type, enterAtoms) {
703
+ for (let i = 0; i < ranges.length; i++) {
704
+ let { $from, $to } = ranges[i];
705
+ let can = $from.depth == 0
706
+ ? doc.inlineContent && doc.type.allowsMarkType(type)
707
+ : false;
708
+ doc.nodesBetween($from.pos, $to.pos, (node, pos) => {
709
+ if (can ||
710
+ !enterAtoms && node.isAtom && node.isInline && pos >= $from.pos &&
711
+ pos + node.nodeSize <= $to.pos) {
712
+ return false;
713
+ }
714
+ can = node.inlineContent && node.type.allowsMarkType(type);
715
+ });
716
+ if (can)
717
+ return true;
718
+ }
719
+ return false;
720
+ }
721
+ function removeInlineAtoms(ranges) {
722
+ let result = [];
723
+ for (let i = 0; i < ranges.length; i++) {
724
+ let { $from, $to } = ranges[i];
725
+ $from.doc.nodesBetween($from.pos, $to.pos, (node, pos) => {
726
+ if (node.isAtom && node.content.size && node.isInline && pos >= $from.pos &&
727
+ pos + node.nodeSize <= $to.pos) {
728
+ if (pos + 1 > $from.pos) {
729
+ result.push(new SelectionRange($from, $from.doc.resolve(pos + 1)));
730
+ }
731
+ $from = $from.doc.resolve(pos + 1 + node.content.size);
732
+ return false;
733
+ }
734
+ });
735
+ if ($from.pos < $to.pos)
736
+ result.push(new SelectionRange($from, $to));
737
+ }
738
+ return result;
739
+ }
740
+ /// Create a command function that toggles the given mark with the
741
+ /// given attributes. Will return `false` when the current selection
742
+ /// doesn't support that mark. This will remove the mark if any marks
743
+ /// of that type exist in the selection, or add it otherwise. If the
744
+ /// selection is empty, this applies to the [stored
745
+ /// marks](#state.EditorState.storedMarks) instead of a range of the
746
+ /// document.
747
+ export function toggleMark(markType, attrs = null, options) {
748
+ let removeWhenPresent = (options && options.removeWhenPresent) !== false;
749
+ let enterAtoms = (options && options.enterInlineAtoms) !== false;
750
+ let dropSpace = !(options && options.includeWhitespace);
751
+ return function (state, dispatch) {
752
+ let { empty, $cursor, ranges } = state.selection;
753
+ if ((empty && !$cursor) ||
754
+ !markApplies(state.doc, ranges, markType, enterAtoms))
755
+ return false;
756
+ if (dispatch) {
757
+ if ($cursor) {
758
+ if (markType.isInSet(state.storedMarks || $cursor.marks())) {
759
+ dispatch(state.tr.removeStoredMark(markType));
760
+ }
761
+ else {
762
+ dispatch(state.tr.addStoredMark(markType.create(attrs)));
763
+ }
764
+ }
765
+ else {
766
+ let add, tr = state.tr;
767
+ if (!enterAtoms)
768
+ ranges = removeInlineAtoms(ranges);
769
+ if (removeWhenPresent) {
770
+ add = !ranges.some((r) => state.doc.rangeHasMark(r.$from.pos, r.$to.pos, markType));
771
+ }
772
+ else {
773
+ add = !ranges.every((r) => {
774
+ let missing = false;
775
+ tr.doc.nodesBetween(r.$from.pos, r.$to.pos, (node, pos, parent) => {
776
+ if (missing)
777
+ return false;
778
+ missing = !markType.isInSet(node.marks) && !!parent &&
779
+ parent.type.allowsMarkType(markType) &&
780
+ !(node.isText &&
781
+ /^\s*$/.test(node.textBetween(Math.max(0, r.$from.pos - pos), Math.min(node.nodeSize, r.$to.pos - pos))));
782
+ });
783
+ return !missing;
784
+ });
785
+ }
786
+ for (let i = 0; i < ranges.length; i++) {
787
+ let { $from, $to } = ranges[i];
788
+ if (!add) {
789
+ tr.removeMark($from.pos, $to.pos, markType);
790
+ }
791
+ else {
792
+ let from = $from.pos, to = $to.pos, start = $from.nodeAfter, end = $to.nodeBefore;
793
+ let spaceStart = dropSpace && start && start.isText
794
+ ? /^\s*/.exec(start.text)[0].length
795
+ : 0;
796
+ let spaceEnd = dropSpace && end && end.isText
797
+ ? /\s*$/.exec(end.text)[0].length
798
+ : 0;
799
+ if (from + spaceStart < to) {
800
+ from += spaceStart;
801
+ to -= spaceEnd;
802
+ }
803
+ tr.addMark(from, to, markType.create(attrs));
804
+ }
805
+ }
806
+ dispatch(tr.scrollIntoView());
807
+ }
808
+ }
809
+ return true;
810
+ };
811
+ }
812
+ function wrapDispatchForJoin(dispatch, isJoinable) {
813
+ return (tr) => {
814
+ if (!tr.isGeneric)
815
+ return dispatch(tr);
816
+ let ranges = [];
817
+ for (let i = 0; i < tr.mapping.maps.length; i++) {
818
+ let map = tr.mapping.maps[i];
819
+ for (let j = 0; j < ranges.length; j++) {
820
+ ranges[j] = map.map(ranges[j]);
821
+ }
822
+ map.forEach((_s, _e, from, to) => ranges.push(from, to));
823
+ }
824
+ // Figure out which joinable points exist inside those ranges,
825
+ // by checking all node boundaries in their parent nodes.
826
+ let joinable = [];
827
+ for (let i = 0; i < ranges.length; i += 2) {
828
+ let from = ranges[i], to = ranges[i + 1];
829
+ let $from = tr.doc.resolve(from), depth = $from.sharedDepth(to), parent = $from.node(depth);
830
+ for (let index = $from.indexAfter(depth), pos = $from.after(depth + 1); pos <= to; ++index) {
831
+ let after = parent.maybeChild(index);
832
+ if (!after)
833
+ break;
834
+ if (index && joinable.indexOf(pos) == -1) {
835
+ let before = parent.child(index - 1);
836
+ if (before.type == after.type && isJoinable(before, after)) {
837
+ joinable.push(pos);
838
+ }
839
+ }
840
+ pos += after.nodeSize;
841
+ }
842
+ }
843
+ // Join the joinable points
844
+ joinable.sort((a, b) => a - b);
845
+ for (let i = joinable.length - 1; i >= 0; i--) {
846
+ if (canJoin(tr.doc, joinable[i]))
847
+ tr.join(joinable[i]);
848
+ }
849
+ dispatch(tr);
850
+ };
851
+ }
852
+ /// Wrap a command so that, when it produces a transform that causes
853
+ /// two joinable nodes to end up next to each other, those are joined.
854
+ /// Nodes are considered joinable when they are of the same type and
855
+ /// when the `isJoinable` predicate returns true for them or, if an
856
+ /// array of strings was passed, if their node type name is in that
857
+ /// array.
858
+ export function autoJoin(command, isJoinable) {
859
+ let canJoin = Array.isArray(isJoinable)
860
+ ? (node) => isJoinable.indexOf(node.type.name) > -1
861
+ : isJoinable;
862
+ return (state, dispatch, view) => command(state, dispatch && wrapDispatchForJoin(dispatch, canJoin), view);
863
+ }
864
+ /// Combine a number of command functions into a single function (which
865
+ /// calls them one by one until one returns true).
866
+ export function chainCommands(...commands) {
867
+ return function (state, dispatch, view) {
868
+ for (let i = 0; i < commands.length; i++) {
869
+ if (commands[i](state, dispatch, view))
870
+ return true;
871
+ }
872
+ return false;
873
+ };
874
+ }
875
+ export function alternativeCommands(...commands) {
876
+ return function (state, dispatch, view) {
877
+ for (let i = 0; i < commands.length; i++) {
878
+ if (commands[i](state, dispatch, view))
879
+ return true;
880
+ }
881
+ return false;
882
+ };
883
+ }
884
+ let backspace = chainCommands(deleteSelection, joinBackward, selectNodeBackward);
885
+ let del = chainCommands(deleteSelection, joinForward, selectNodeForward);
886
+ /// A basic keymap containing bindings not specific to any schema.
887
+ /// Binds the following keys (when multiple commands are listed, they
888
+ /// are chained with [`chainCommands`](#commands.chainCommands)):
889
+ ///
890
+ /// * **Enter** to `newlineInCode`, `createParagraphNear`, `liftEmptyBlock`, `splitBlock`
891
+ /// * **Mod-Enter** to `exitCode`
892
+ /// * **Backspace** and **Mod-Backspace** to `deleteSelection`, `joinBackward`, `selectNodeBackward`
893
+ /// * **Delete** and **Mod-Delete** to `deleteSelection`, `joinForward`, `selectNodeForward`
894
+ /// * **Mod-Delete** to `deleteSelection`, `joinForward`, `selectNodeForward`
895
+ /// * **Mod-a** to `selectAll`
896
+ const pcBaseKeymap = {
897
+ 'Enter': chainCommands(newlineInCode, createParagraphNear, liftEmptyBlock, splitBlock),
898
+ 'Mod-Enter': exitCode,
899
+ 'Backspace': backspace,
900
+ 'Mod-Backspace': backspace,
901
+ 'Shift-Backspace': backspace,
902
+ 'Delete': del,
903
+ 'Mod-Delete': del,
904
+ 'Mod-a': selectAll,
905
+ };
906
+ /// A copy of `pcBaseKeymap` that also binds **Ctrl-h** like Backspace,
907
+ /// **Ctrl-d** like Delete, **Alt-Backspace** like Ctrl-Backspace, and
908
+ /// **Ctrl-Alt-Backspace**, **Alt-Delete**, and **Alt-d** like
909
+ /// Ctrl-Delete.
910
+ const macBaseKeymap = {
911
+ 'Ctrl-h': pcBaseKeymap['Backspace'],
912
+ 'Alt-Backspace': pcBaseKeymap['Mod-Backspace'],
913
+ 'Ctrl-d': pcBaseKeymap['Delete'],
914
+ 'Ctrl-Alt-Backspace': pcBaseKeymap['Mod-Delete'],
915
+ 'Alt-Delete': pcBaseKeymap['Mod-Delete'],
916
+ 'Alt-d': pcBaseKeymap['Mod-Delete'],
917
+ 'Ctrl-a': selectTextblockStart,
918
+ 'Ctrl-e': selectTextblockEnd,
919
+ };
920
+ for (let key in pcBaseKeymap)
921
+ macBaseKeymap[key] = pcBaseKeymap[key];
922
+ const mac = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
923
+ /// Depending on the detected platform, this will hold
924
+ /// [`pcBasekeymap`](#commands.pcBaseKeymap) or
925
+ /// [`macBaseKeymap`](#commands.macBaseKeymap).
926
+ export const baseKeymap = mac
927
+ ? macBaseKeymap
928
+ : pcBaseKeymap;