@portabletext/editor 1.11.2 → 1.12.0
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/README.md +11 -0
- package/lib/index.d.mts +26 -7
- package/lib/index.d.ts +26 -7
- package/lib/index.esm.js +317 -134
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +316 -133
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +317 -134
- package/lib/index.mjs.map +1 -1
- package/package.json +6 -6
- package/src/editor/behavior/behavior.action-utils.insert-block.ts +61 -0
- package/src/editor/behavior/behavior.action.insert-block-object.ts +25 -0
- package/src/editor/behavior/behavior.actions.ts +88 -32
- package/src/editor/behavior/behavior.core.block-objects.ts +5 -11
- package/src/editor/behavior/behavior.markdown.ts +149 -62
- package/src/editor/behavior/behavior.types.ts +22 -6
- package/src/editor/behavior/behavior.utils.block-offset.test.ts +143 -0
- package/src/editor/behavior/behavior.utils.block-offset.ts +101 -0
- package/src/editor/behavior/behavior.utils.ts +13 -2
- package/src/editor/plugins/createWithEditableAPI.ts +22 -87
- package/src/index.ts +1 -0
package/lib/index.js
CHANGED
|
@@ -117,7 +117,13 @@ function getNextBlock(context) {
|
|
|
117
117
|
return nextBlock;
|
|
118
118
|
}
|
|
119
119
|
function isEmptyTextBlock(block) {
|
|
120
|
-
|
|
120
|
+
if (!types.isPortableTextTextBlock(block))
|
|
121
|
+
return !1;
|
|
122
|
+
const onlyText = block.children.every(types.isPortableTextSpan), blockText = getTextBlockText(block);
|
|
123
|
+
return onlyText && blockText === "";
|
|
124
|
+
}
|
|
125
|
+
function getTextBlockText(block) {
|
|
126
|
+
return block.children.map((child) => child.text ?? "").join("");
|
|
121
127
|
}
|
|
122
128
|
const breakingBlockObject = {
|
|
123
129
|
on: "insert break",
|
|
@@ -126,7 +132,7 @@ const breakingBlockObject = {
|
|
|
126
132
|
}) => !!getFocusBlockObject(context),
|
|
127
133
|
actions: [() => [{
|
|
128
134
|
type: "insert text block",
|
|
129
|
-
|
|
135
|
+
placement: "after"
|
|
130
136
|
}]]
|
|
131
137
|
}, deletingEmptyTextBlockAfterBlockObject = {
|
|
132
138
|
on: "delete backward",
|
|
@@ -143,17 +149,8 @@ const breakingBlockObject = {
|
|
|
143
149
|
focusTextBlock,
|
|
144
150
|
previousBlock
|
|
145
151
|
}) => [{
|
|
146
|
-
type: "delete",
|
|
147
|
-
|
|
148
|
-
anchor: {
|
|
149
|
-
path: focusTextBlock.path,
|
|
150
|
-
offset: 0
|
|
151
|
-
},
|
|
152
|
-
focus: {
|
|
153
|
-
path: focusTextBlock.path,
|
|
154
|
-
offset: 0
|
|
155
|
-
}
|
|
156
|
-
}
|
|
152
|
+
type: "delete block",
|
|
153
|
+
blockPath: focusTextBlock.path
|
|
157
154
|
}, {
|
|
158
155
|
type: "select",
|
|
159
156
|
selection: {
|
|
@@ -182,17 +179,8 @@ const breakingBlockObject = {
|
|
|
182
179
|
focusTextBlock,
|
|
183
180
|
nextBlock
|
|
184
181
|
}) => [{
|
|
185
|
-
type: "delete",
|
|
186
|
-
|
|
187
|
-
anchor: {
|
|
188
|
-
path: focusTextBlock.path,
|
|
189
|
-
offset: 0
|
|
190
|
-
},
|
|
191
|
-
focus: {
|
|
192
|
-
path: focusTextBlock.path,
|
|
193
|
-
offset: 0
|
|
194
|
-
}
|
|
195
|
-
}
|
|
182
|
+
type: "delete block",
|
|
183
|
+
blockPath: focusTextBlock.path
|
|
196
184
|
}, {
|
|
197
185
|
type: "select",
|
|
198
186
|
selection: {
|
|
@@ -370,6 +358,58 @@ function isPortableTextBlock(node) {
|
|
|
370
358
|
node.children.every((child) => typeof child == "object" && "_type" in child)
|
|
371
359
|
);
|
|
372
360
|
}
|
|
361
|
+
function blockOffsetToSpanSelectionPoint({
|
|
362
|
+
value,
|
|
363
|
+
blockOffset
|
|
364
|
+
}) {
|
|
365
|
+
let offsetLeft = blockOffset.offset, selectionPoint;
|
|
366
|
+
for (const block of value)
|
|
367
|
+
if (block._key === blockOffset.path[0]._key && types.isPortableTextTextBlock(block)) {
|
|
368
|
+
for (const child of block.children)
|
|
369
|
+
if (types.isPortableTextSpan(child)) {
|
|
370
|
+
if (offsetLeft === 0) {
|
|
371
|
+
selectionPoint = {
|
|
372
|
+
path: [...blockOffset.path, "children", {
|
|
373
|
+
_key: child._key
|
|
374
|
+
}],
|
|
375
|
+
offset: 0
|
|
376
|
+
};
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
if (offsetLeft <= child.text.length) {
|
|
380
|
+
selectionPoint = {
|
|
381
|
+
path: [...blockOffset.path, "children", {
|
|
382
|
+
_key: child._key
|
|
383
|
+
}],
|
|
384
|
+
offset: offsetLeft
|
|
385
|
+
};
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
offsetLeft -= child.text.length;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return selectionPoint;
|
|
392
|
+
}
|
|
393
|
+
function spanSelectionPointToBlockOffset({
|
|
394
|
+
value,
|
|
395
|
+
selectionPoint
|
|
396
|
+
}) {
|
|
397
|
+
let offset = 0;
|
|
398
|
+
for (const block of value)
|
|
399
|
+
if (block._key === selectionPoint.path[0]._key && types.isPortableTextTextBlock(block)) {
|
|
400
|
+
for (const child of block.children)
|
|
401
|
+
if (types.isPortableTextSpan(child)) {
|
|
402
|
+
if (child._key === selectionPoint.path[2]._key)
|
|
403
|
+
return {
|
|
404
|
+
path: [{
|
|
405
|
+
_key: block._key
|
|
406
|
+
}],
|
|
407
|
+
offset: offset + selectionPoint.offset
|
|
408
|
+
};
|
|
409
|
+
offset += child.text.length;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
373
413
|
function createMarkdownBehaviors(config) {
|
|
374
414
|
const automaticBlockquoteOnSpace = {
|
|
375
415
|
on: "insert text",
|
|
@@ -382,12 +422,24 @@ function createMarkdownBehaviors(config) {
|
|
|
382
422
|
const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
|
|
383
423
|
if (!selectionCollapsed || !focusTextBlock || !focusSpan)
|
|
384
424
|
return !1;
|
|
385
|
-
const
|
|
425
|
+
const blockOffset = spanSelectionPointToBlockOffset({
|
|
426
|
+
value: context.value,
|
|
427
|
+
selectionPoint: {
|
|
428
|
+
path: [{
|
|
429
|
+
_key: focusTextBlock.node._key
|
|
430
|
+
}, "children", {
|
|
431
|
+
_key: focusSpan.node._key
|
|
432
|
+
}],
|
|
433
|
+
offset: context.selection.focus.offset
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
if (!blockOffset)
|
|
437
|
+
return !1;
|
|
438
|
+
const blockText = getTextBlockText(focusTextBlock.node), caretAtTheEndOfQuote = blockOffset.offset === 1, looksLikeMarkdownQuote = /^>/.test(blockText), blockquoteStyle = config.blockquoteStyle?.({
|
|
386
439
|
schema: context.schema
|
|
387
440
|
});
|
|
388
441
|
return caretAtTheEndOfQuote && looksLikeMarkdownQuote && blockquoteStyle !== void 0 ? {
|
|
389
442
|
focusTextBlock,
|
|
390
|
-
focusSpan,
|
|
391
443
|
style: blockquoteStyle
|
|
392
444
|
} : !1;
|
|
393
445
|
},
|
|
@@ -396,7 +448,6 @@ function createMarkdownBehaviors(config) {
|
|
|
396
448
|
text: " "
|
|
397
449
|
}], (_, {
|
|
398
450
|
focusTextBlock,
|
|
399
|
-
focusSpan,
|
|
400
451
|
style
|
|
401
452
|
}) => [{
|
|
402
453
|
type: "unset block",
|
|
@@ -407,19 +458,17 @@ function createMarkdownBehaviors(config) {
|
|
|
407
458
|
style,
|
|
408
459
|
paths: [focusTextBlock.path]
|
|
409
460
|
}, {
|
|
410
|
-
type: "delete",
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
offset: 2
|
|
419
|
-
}
|
|
461
|
+
type: "delete text",
|
|
462
|
+
anchor: {
|
|
463
|
+
path: focusTextBlock.path,
|
|
464
|
+
offset: 0
|
|
465
|
+
},
|
|
466
|
+
focus: {
|
|
467
|
+
path: focusTextBlock.path,
|
|
468
|
+
offset: 2
|
|
420
469
|
}
|
|
421
470
|
}]]
|
|
422
|
-
},
|
|
471
|
+
}, automaticHr = {
|
|
423
472
|
on: "insert text",
|
|
424
473
|
guard: ({
|
|
425
474
|
context,
|
|
@@ -428,14 +477,14 @@ function createMarkdownBehaviors(config) {
|
|
|
428
477
|
const hrCharacter = event.text === "-" ? "-" : event.text === "*" ? "*" : event.text === "_" ? "_" : void 0;
|
|
429
478
|
if (hrCharacter === void 0)
|
|
430
479
|
return !1;
|
|
431
|
-
const
|
|
480
|
+
const hrObject = config.horizontalRuleObject?.({
|
|
432
481
|
schema: context.schema
|
|
433
482
|
}), focusBlock = getFocusTextBlock(context), selectionCollapsed = selectionIsCollapsed(context);
|
|
434
|
-
if (!
|
|
483
|
+
if (!hrObject || !focusBlock || !selectionCollapsed)
|
|
435
484
|
return !1;
|
|
436
485
|
const onlyText = focusBlock.node.children.every(isPortableTextSpan), blockText = focusBlock.node.children.map((child) => child.text ?? "").join("");
|
|
437
486
|
return onlyText && blockText === `${hrCharacter}${hrCharacter}` ? {
|
|
438
|
-
|
|
487
|
+
hrObject,
|
|
439
488
|
focusBlock,
|
|
440
489
|
hrCharacter
|
|
441
490
|
} : !1;
|
|
@@ -446,26 +495,59 @@ function createMarkdownBehaviors(config) {
|
|
|
446
495
|
type: "insert text",
|
|
447
496
|
text: hrCharacter
|
|
448
497
|
}], (_, {
|
|
449
|
-
|
|
498
|
+
hrObject,
|
|
450
499
|
focusBlock
|
|
451
500
|
}) => [{
|
|
452
501
|
type: "insert block object",
|
|
453
|
-
|
|
502
|
+
placement: "after",
|
|
503
|
+
blockObject: hrObject
|
|
454
504
|
}, {
|
|
455
|
-
type: "delete",
|
|
456
|
-
|
|
457
|
-
anchor: {
|
|
458
|
-
path: focusBlock.path,
|
|
459
|
-
offset: 0
|
|
460
|
-
},
|
|
461
|
-
focus: {
|
|
462
|
-
path: focusBlock.path,
|
|
463
|
-
offset: 0
|
|
464
|
-
}
|
|
465
|
-
}
|
|
505
|
+
type: "delete block",
|
|
506
|
+
blockPath: focusBlock.path
|
|
466
507
|
}, {
|
|
467
508
|
type: "insert text block",
|
|
468
|
-
|
|
509
|
+
placement: "after"
|
|
510
|
+
}]]
|
|
511
|
+
}, automaticHrOnPaste = {
|
|
512
|
+
on: "paste",
|
|
513
|
+
guard: ({
|
|
514
|
+
context,
|
|
515
|
+
event
|
|
516
|
+
}) => {
|
|
517
|
+
const text = event.clipboardData.getData("text/plain"), hrRegExp = /^(---)$|(___)$|(\*\*\*)$/gm, hrCharacters = text.match(hrRegExp)?.[0], hrObject = config.horizontalRuleObject?.({
|
|
518
|
+
schema: context.schema
|
|
519
|
+
}), focusBlock = getFocusBlock(context);
|
|
520
|
+
return !hrCharacters || !hrObject || !focusBlock ? !1 : {
|
|
521
|
+
hrCharacters,
|
|
522
|
+
hrObject,
|
|
523
|
+
focusBlock
|
|
524
|
+
};
|
|
525
|
+
},
|
|
526
|
+
actions: [(_, {
|
|
527
|
+
hrCharacters
|
|
528
|
+
}) => [{
|
|
529
|
+
type: "insert text",
|
|
530
|
+
text: hrCharacters
|
|
531
|
+
}], (_, {
|
|
532
|
+
hrObject,
|
|
533
|
+
focusBlock
|
|
534
|
+
}) => types.isPortableTextTextBlock(focusBlock.node) ? [{
|
|
535
|
+
type: "insert text block",
|
|
536
|
+
textBlock: {
|
|
537
|
+
children: focusBlock.node.children
|
|
538
|
+
},
|
|
539
|
+
placement: "after"
|
|
540
|
+
}, {
|
|
541
|
+
type: "insert block object",
|
|
542
|
+
blockObject: hrObject,
|
|
543
|
+
placement: "after"
|
|
544
|
+
}, {
|
|
545
|
+
type: "delete block",
|
|
546
|
+
blockPath: focusBlock.path
|
|
547
|
+
}] : [{
|
|
548
|
+
type: "insert block object",
|
|
549
|
+
blockObject: hrObject,
|
|
550
|
+
placement: "after"
|
|
469
551
|
}]]
|
|
470
552
|
}, automaticHeadingOnSpace = {
|
|
471
553
|
on: "insert text",
|
|
@@ -478,8 +560,21 @@ function createMarkdownBehaviors(config) {
|
|
|
478
560
|
const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
|
|
479
561
|
if (!selectionCollapsed || !focusTextBlock || !focusSpan)
|
|
480
562
|
return !1;
|
|
481
|
-
const
|
|
482
|
-
|
|
563
|
+
const blockOffset = spanSelectionPointToBlockOffset({
|
|
564
|
+
value: context.value,
|
|
565
|
+
selectionPoint: {
|
|
566
|
+
path: [{
|
|
567
|
+
_key: focusTextBlock.node._key
|
|
568
|
+
}, "children", {
|
|
569
|
+
_key: focusSpan.node._key
|
|
570
|
+
}],
|
|
571
|
+
offset: context.selection.focus.offset
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
if (!blockOffset)
|
|
575
|
+
return !1;
|
|
576
|
+
const blockText = getTextBlockText(focusTextBlock.node), markdownHeadingSearch = /^#+/.exec(blockText), level = markdownHeadingSearch ? markdownHeadingSearch[0].length : void 0;
|
|
577
|
+
if (blockOffset.offset !== level)
|
|
483
578
|
return !1;
|
|
484
579
|
const style = level !== void 0 ? config.headingStyle?.({
|
|
485
580
|
schema: context.schema,
|
|
@@ -487,7 +582,6 @@ function createMarkdownBehaviors(config) {
|
|
|
487
582
|
}) : void 0;
|
|
488
583
|
return level !== void 0 && style !== void 0 ? {
|
|
489
584
|
focusTextBlock,
|
|
490
|
-
focusSpan,
|
|
491
585
|
style,
|
|
492
586
|
level
|
|
493
587
|
} : !1;
|
|
@@ -497,7 +591,6 @@ function createMarkdownBehaviors(config) {
|
|
|
497
591
|
text: " "
|
|
498
592
|
}], (_, {
|
|
499
593
|
focusTextBlock,
|
|
500
|
-
focusSpan,
|
|
501
594
|
style,
|
|
502
595
|
level
|
|
503
596
|
}) => [{
|
|
@@ -509,16 +602,14 @@ function createMarkdownBehaviors(config) {
|
|
|
509
602
|
style,
|
|
510
603
|
paths: [focusTextBlock.path]
|
|
511
604
|
}, {
|
|
512
|
-
type: "delete",
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
offset: level + 1
|
|
521
|
-
}
|
|
605
|
+
type: "delete text",
|
|
606
|
+
anchor: {
|
|
607
|
+
path: focusTextBlock.path,
|
|
608
|
+
offset: 0
|
|
609
|
+
},
|
|
610
|
+
focus: {
|
|
611
|
+
path: focusTextBlock.path,
|
|
612
|
+
offset: level + 1
|
|
522
613
|
}
|
|
523
614
|
}]]
|
|
524
615
|
}, clearStyleOnBackspace = {
|
|
@@ -556,15 +647,27 @@ function createMarkdownBehaviors(config) {
|
|
|
556
647
|
const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
|
|
557
648
|
if (!selectionCollapsed || !focusTextBlock || !focusSpan)
|
|
558
649
|
return !1;
|
|
559
|
-
const
|
|
650
|
+
const blockOffset = spanSelectionPointToBlockOffset({
|
|
651
|
+
value: context.value,
|
|
652
|
+
selectionPoint: {
|
|
653
|
+
path: [{
|
|
654
|
+
_key: focusTextBlock.node._key
|
|
655
|
+
}, "children", {
|
|
656
|
+
_key: focusSpan.node._key
|
|
657
|
+
}],
|
|
658
|
+
offset: context.selection.focus.offset
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
if (!blockOffset)
|
|
662
|
+
return !1;
|
|
663
|
+
const blockText = getTextBlockText(focusTextBlock.node), defaultStyle = config.defaultStyle?.({
|
|
560
664
|
schema: context.schema
|
|
561
|
-
}), looksLikeUnorderedList = /^(-|\*)/.test(
|
|
665
|
+
}), looksLikeUnorderedList = /^(-|\*)/.test(blockText), unorderedListStyle = config.unorderedListStyle?.({
|
|
562
666
|
schema: context.schema
|
|
563
|
-
}), caretAtTheEndOfUnorderedList =
|
|
667
|
+
}), caretAtTheEndOfUnorderedList = blockOffset.offset === 1;
|
|
564
668
|
if (defaultStyle && caretAtTheEndOfUnorderedList && looksLikeUnorderedList && unorderedListStyle !== void 0)
|
|
565
669
|
return {
|
|
566
670
|
focusTextBlock,
|
|
567
|
-
focusSpan,
|
|
568
671
|
listItem: unorderedListStyle,
|
|
569
672
|
listItemLength: 1,
|
|
570
673
|
style: defaultStyle
|
|
@@ -574,7 +677,6 @@ function createMarkdownBehaviors(config) {
|
|
|
574
677
|
}), caretAtTheEndOfOrderedList = context.selection.focus.offset === 2;
|
|
575
678
|
return defaultStyle && caretAtTheEndOfOrderedList && looksLikeOrderedList && orderedListStyle !== void 0 ? {
|
|
576
679
|
focusTextBlock,
|
|
577
|
-
focusSpan,
|
|
578
680
|
listItem: orderedListStyle,
|
|
579
681
|
listItemLength: 2,
|
|
580
682
|
style: defaultStyle
|
|
@@ -585,7 +687,6 @@ function createMarkdownBehaviors(config) {
|
|
|
585
687
|
text: " "
|
|
586
688
|
}], (_, {
|
|
587
689
|
focusTextBlock,
|
|
588
|
-
focusSpan,
|
|
589
690
|
style,
|
|
590
691
|
listItem,
|
|
591
692
|
listItemLength
|
|
@@ -596,20 +697,18 @@ function createMarkdownBehaviors(config) {
|
|
|
596
697
|
style,
|
|
597
698
|
paths: [focusTextBlock.path]
|
|
598
699
|
}, {
|
|
599
|
-
type: "delete",
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
offset: listItemLength + 1
|
|
608
|
-
}
|
|
700
|
+
type: "delete text",
|
|
701
|
+
anchor: {
|
|
702
|
+
path: focusTextBlock.path,
|
|
703
|
+
offset: 0
|
|
704
|
+
},
|
|
705
|
+
focus: {
|
|
706
|
+
path: focusTextBlock.path,
|
|
707
|
+
offset: listItemLength + 1
|
|
609
708
|
}
|
|
610
709
|
}]]
|
|
611
710
|
};
|
|
612
|
-
return [automaticBlockquoteOnSpace,
|
|
711
|
+
return [automaticBlockquoteOnSpace, automaticHeadingOnSpace, automaticHr, automaticHrOnPaste, clearStyleOnBackspace, automaticListOnSpace];
|
|
613
712
|
}
|
|
614
713
|
function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
615
714
|
if (!portableTextType)
|
|
@@ -4707,7 +4806,65 @@ function createSlateEditor(config) {
|
|
|
4707
4806
|
};
|
|
4708
4807
|
return slateEditors.set(config.editorActor, slateEditor), slateEditor;
|
|
4709
4808
|
}
|
|
4710
|
-
|
|
4809
|
+
function insertBlock({
|
|
4810
|
+
block,
|
|
4811
|
+
placement,
|
|
4812
|
+
editor,
|
|
4813
|
+
schema: schema2
|
|
4814
|
+
}) {
|
|
4815
|
+
if (editor.selection) {
|
|
4816
|
+
const [focusBlock, focusBlockPath] = Array.from(slate.Editor.nodes(editor, {
|
|
4817
|
+
at: editor.selection.focus.path.slice(0, 1),
|
|
4818
|
+
match: (n) => !slate.Editor.isEditor(n)
|
|
4819
|
+
}))[0] ?? [void 0, void 0];
|
|
4820
|
+
if (placement === "after") {
|
|
4821
|
+
const nextPath = [focusBlockPath[0] + 1];
|
|
4822
|
+
slate.Transforms.insertNodes(editor, block, {
|
|
4823
|
+
at: nextPath
|
|
4824
|
+
}), slate.Transforms.select(editor, {
|
|
4825
|
+
anchor: {
|
|
4826
|
+
path: [nextPath[0], 0],
|
|
4827
|
+
offset: 0
|
|
4828
|
+
},
|
|
4829
|
+
focus: {
|
|
4830
|
+
path: [nextPath[0], 0],
|
|
4831
|
+
offset: 0
|
|
4832
|
+
}
|
|
4833
|
+
});
|
|
4834
|
+
} else
|
|
4835
|
+
slate.Editor.insertNode(editor, block);
|
|
4836
|
+
focusBlock && isEqualToEmptyEditor([focusBlock], schema2) && slate.Transforms.removeNodes(editor, {
|
|
4837
|
+
at: focusBlockPath
|
|
4838
|
+
});
|
|
4839
|
+
} else {
|
|
4840
|
+
const lastBlock = Array.from(slate.Editor.nodes(editor, {
|
|
4841
|
+
match: (n) => !slate.Editor.isEditor(n),
|
|
4842
|
+
at: [],
|
|
4843
|
+
reverse: !0
|
|
4844
|
+
}))[0];
|
|
4845
|
+
slate.Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], schema2) && slate.Transforms.removeNodes(editor, {
|
|
4846
|
+
at: lastBlock[1]
|
|
4847
|
+
});
|
|
4848
|
+
}
|
|
4849
|
+
}
|
|
4850
|
+
const insertBlockObjectActionImplementation = ({
|
|
4851
|
+
context,
|
|
4852
|
+
action
|
|
4853
|
+
}) => {
|
|
4854
|
+
const block = toSlateValue([{
|
|
4855
|
+
_key: context.keyGenerator(),
|
|
4856
|
+
_type: action.blockObject.name,
|
|
4857
|
+
...action.blockObject.value ? action.blockObject.value : {}
|
|
4858
|
+
}], {
|
|
4859
|
+
schemaTypes: context.schema
|
|
4860
|
+
})[0];
|
|
4861
|
+
insertBlock({
|
|
4862
|
+
block,
|
|
4863
|
+
placement: action.placement,
|
|
4864
|
+
editor: action.editor,
|
|
4865
|
+
schema: context.schema
|
|
4866
|
+
});
|
|
4867
|
+
}, debug$5 = debugWithName("API:editable");
|
|
4711
4868
|
function createEditableAPI(editor, editorActor) {
|
|
4712
4869
|
const types2 = editorActor.getSnapshot().context.schema;
|
|
4713
4870
|
return {
|
|
@@ -4796,18 +4953,21 @@ function createEditableAPI(editor, editorActor) {
|
|
|
4796
4953
|
at: editor.selection
|
|
4797
4954
|
}), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path || [];
|
|
4798
4955
|
},
|
|
4799
|
-
insertBlock: (type, value) => insertBlockObjectActionImplementation({
|
|
4956
|
+
insertBlock: (type, value) => (insertBlockObjectActionImplementation({
|
|
4800
4957
|
context: {
|
|
4801
4958
|
keyGenerator: editorActor.getSnapshot().context.keyGenerator,
|
|
4802
4959
|
schema: types2
|
|
4803
4960
|
},
|
|
4804
4961
|
action: {
|
|
4805
4962
|
type: "insert block object",
|
|
4806
|
-
|
|
4807
|
-
|
|
4963
|
+
blockObject: {
|
|
4964
|
+
name: type.name,
|
|
4965
|
+
value
|
|
4966
|
+
},
|
|
4967
|
+
placement: "auto",
|
|
4808
4968
|
editor
|
|
4809
4969
|
}
|
|
4810
|
-
}),
|
|
4970
|
+
}), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path ?? []),
|
|
4811
4971
|
hasBlockStyle: (style) => {
|
|
4812
4972
|
try {
|
|
4813
4973
|
return editor.pteHasBlockStyle(style);
|
|
@@ -4977,35 +5137,6 @@ function createEditableAPI(editor, editorActor) {
|
|
|
4977
5137
|
}
|
|
4978
5138
|
};
|
|
4979
5139
|
}
|
|
4980
|
-
const insertBlockObjectActionImplementation = ({
|
|
4981
|
-
context,
|
|
4982
|
-
action
|
|
4983
|
-
}) => {
|
|
4984
|
-
const editor = action.editor, types2 = context.schema, block = toSlateValue([{
|
|
4985
|
-
_key: context.keyGenerator(),
|
|
4986
|
-
_type: action.name,
|
|
4987
|
-
...action.value ? action.value : {}
|
|
4988
|
-
}], {
|
|
4989
|
-
schemaTypes: context.schema
|
|
4990
|
-
})[0];
|
|
4991
|
-
if (!editor.selection) {
|
|
4992
|
-
const lastBlock = Array.from(slate.Editor.nodes(editor, {
|
|
4993
|
-
match: (n) => !slate.Editor.isEditor(n),
|
|
4994
|
-
at: [],
|
|
4995
|
-
reverse: !0
|
|
4996
|
-
}))[0];
|
|
4997
|
-
return slate.Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types2) && slate.Transforms.removeNodes(editor, {
|
|
4998
|
-
at: lastBlock[1]
|
|
4999
|
-
}), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path ?? [];
|
|
5000
|
-
}
|
|
5001
|
-
const focusBlock = Array.from(slate.Editor.nodes(editor, {
|
|
5002
|
-
at: editor.selection.focus.path.slice(0, 1),
|
|
5003
|
-
match: (n) => n._type === types2.block.name
|
|
5004
|
-
}))[0];
|
|
5005
|
-
return slate.Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types2) && slate.Transforms.removeNodes(editor, {
|
|
5006
|
-
at: focusBlock[1]
|
|
5007
|
-
}), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path || [];
|
|
5008
|
-
};
|
|
5009
5140
|
function isAnnotationActive({
|
|
5010
5141
|
editor,
|
|
5011
5142
|
annotation
|
|
@@ -5401,18 +5532,52 @@ const addAnnotationActionImplementation = ({
|
|
|
5401
5532
|
}) => {
|
|
5402
5533
|
slate.deleteForward(action.editor, action.unit);
|
|
5403
5534
|
},
|
|
5404
|
-
delete: ({
|
|
5535
|
+
"delete block": ({
|
|
5536
|
+
action
|
|
5537
|
+
}) => {
|
|
5538
|
+
const range = toSlateRange({
|
|
5539
|
+
anchor: {
|
|
5540
|
+
path: action.blockPath,
|
|
5541
|
+
offset: 0
|
|
5542
|
+
},
|
|
5543
|
+
focus: {
|
|
5544
|
+
path: action.blockPath,
|
|
5545
|
+
offset: 0
|
|
5546
|
+
}
|
|
5547
|
+
}, action.editor);
|
|
5548
|
+
if (!range) {
|
|
5549
|
+
console.error("Unable to find Slate range from selection points");
|
|
5550
|
+
return;
|
|
5551
|
+
}
|
|
5552
|
+
slate.Transforms.removeNodes(action.editor, {
|
|
5553
|
+
at: range
|
|
5554
|
+
});
|
|
5555
|
+
},
|
|
5556
|
+
"delete text": ({
|
|
5557
|
+
context,
|
|
5405
5558
|
action
|
|
5406
5559
|
}) => {
|
|
5407
|
-
const
|
|
5408
|
-
|
|
5409
|
-
|
|
5560
|
+
const value = fromSlateValue(action.editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(action.editor)), anchor = blockOffsetToSpanSelectionPoint({
|
|
5561
|
+
value,
|
|
5562
|
+
blockOffset: action.anchor
|
|
5563
|
+
}), focus = blockOffsetToSpanSelectionPoint({
|
|
5564
|
+
value,
|
|
5565
|
+
blockOffset: action.focus
|
|
5566
|
+
});
|
|
5567
|
+
if (!anchor || !focus) {
|
|
5568
|
+
console.error("Unable to find anchor or focus selection point");
|
|
5410
5569
|
return;
|
|
5411
5570
|
}
|
|
5412
|
-
|
|
5413
|
-
|
|
5414
|
-
|
|
5415
|
-
|
|
5571
|
+
const range = toSlateRange({
|
|
5572
|
+
anchor,
|
|
5573
|
+
focus
|
|
5574
|
+
}, action.editor);
|
|
5575
|
+
if (!range) {
|
|
5576
|
+
console.error("Unable to find Slate range from selection points");
|
|
5577
|
+
return;
|
|
5578
|
+
}
|
|
5579
|
+
slate.Transforms.delete(action.editor, {
|
|
5580
|
+
at: range
|
|
5416
5581
|
});
|
|
5417
5582
|
},
|
|
5418
5583
|
"insert block object": insertBlockObjectActionImplementation,
|
|
@@ -5428,16 +5593,27 @@ const addAnnotationActionImplementation = ({
|
|
|
5428
5593
|
context,
|
|
5429
5594
|
action
|
|
5430
5595
|
}) => {
|
|
5431
|
-
|
|
5596
|
+
const block = toSlateValue([{
|
|
5432
5597
|
_key: context.keyGenerator(),
|
|
5433
5598
|
_type: context.schema.block.name,
|
|
5434
5599
|
style: context.schema.styles[0].value ?? "normal",
|
|
5435
5600
|
markDefs: [],
|
|
5436
|
-
children:
|
|
5601
|
+
children: action.textBlock?.children?.map((child) => ({
|
|
5602
|
+
...child,
|
|
5603
|
+
_key: context.keyGenerator()
|
|
5604
|
+
})) ?? [{
|
|
5605
|
+
_type: context.schema.span.name,
|
|
5437
5606
|
_key: context.keyGenerator(),
|
|
5438
|
-
_type: "span",
|
|
5439
5607
|
text: ""
|
|
5440
5608
|
}]
|
|
5609
|
+
}], {
|
|
5610
|
+
schemaTypes: context.schema
|
|
5611
|
+
})[0];
|
|
5612
|
+
insertBlock({
|
|
5613
|
+
block,
|
|
5614
|
+
editor: action.editor,
|
|
5615
|
+
schema: context.schema,
|
|
5616
|
+
placement: action.placement
|
|
5441
5617
|
});
|
|
5442
5618
|
},
|
|
5443
5619
|
effect: ({
|
|
@@ -5472,8 +5648,15 @@ function performAction({
|
|
|
5472
5648
|
action
|
|
5473
5649
|
}) {
|
|
5474
5650
|
switch (action.type) {
|
|
5475
|
-
case "delete": {
|
|
5476
|
-
behaviorActionImplementations
|
|
5651
|
+
case "delete block": {
|
|
5652
|
+
behaviorActionImplementations["delete block"]({
|
|
5653
|
+
context,
|
|
5654
|
+
action
|
|
5655
|
+
});
|
|
5656
|
+
break;
|
|
5657
|
+
}
|
|
5658
|
+
case "delete text": {
|
|
5659
|
+
behaviorActionImplementations["delete text"]({
|
|
5477
5660
|
context,
|
|
5478
5661
|
action
|
|
5479
5662
|
});
|