@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.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isPortableTextTextBlock, isPortableTextSpan as isPortableTextSpan$1,
|
|
1
|
+
import { isKeySegment, isPortableTextTextBlock, isPortableTextSpan as isPortableTextSpan$1, defineType, defineField, isPortableTextListBlock } from "@sanity/types";
|
|
2
2
|
import { Schema } from "@sanity/schema";
|
|
3
3
|
import startCase from "lodash.startcase";
|
|
4
4
|
import { jsx, Fragment, jsxs } from "react/jsx-runtime";
|
|
@@ -138,7 +138,13 @@ function getNextBlock(context) {
|
|
|
138
138
|
return nextBlock;
|
|
139
139
|
}
|
|
140
140
|
function isEmptyTextBlock(block) {
|
|
141
|
-
|
|
141
|
+
if (!isPortableTextTextBlock(block))
|
|
142
|
+
return !1;
|
|
143
|
+
const onlyText = block.children.every(isPortableTextSpan$1), blockText = getTextBlockText(block);
|
|
144
|
+
return onlyText && blockText === "";
|
|
145
|
+
}
|
|
146
|
+
function getTextBlockText(block) {
|
|
147
|
+
return block.children.map((child) => child.text ?? "").join("");
|
|
142
148
|
}
|
|
143
149
|
const breakingBlockObject = {
|
|
144
150
|
on: "insert break",
|
|
@@ -147,7 +153,7 @@ const breakingBlockObject = {
|
|
|
147
153
|
}) => !!getFocusBlockObject(context),
|
|
148
154
|
actions: [() => [{
|
|
149
155
|
type: "insert text block",
|
|
150
|
-
|
|
156
|
+
placement: "after"
|
|
151
157
|
}]]
|
|
152
158
|
}, deletingEmptyTextBlockAfterBlockObject = {
|
|
153
159
|
on: "delete backward",
|
|
@@ -164,17 +170,8 @@ const breakingBlockObject = {
|
|
|
164
170
|
focusTextBlock,
|
|
165
171
|
previousBlock
|
|
166
172
|
}) => [{
|
|
167
|
-
type: "delete",
|
|
168
|
-
|
|
169
|
-
anchor: {
|
|
170
|
-
path: focusTextBlock.path,
|
|
171
|
-
offset: 0
|
|
172
|
-
},
|
|
173
|
-
focus: {
|
|
174
|
-
path: focusTextBlock.path,
|
|
175
|
-
offset: 0
|
|
176
|
-
}
|
|
177
|
-
}
|
|
173
|
+
type: "delete block",
|
|
174
|
+
blockPath: focusTextBlock.path
|
|
178
175
|
}, {
|
|
179
176
|
type: "select",
|
|
180
177
|
selection: {
|
|
@@ -203,17 +200,8 @@ const breakingBlockObject = {
|
|
|
203
200
|
focusTextBlock,
|
|
204
201
|
nextBlock
|
|
205
202
|
}) => [{
|
|
206
|
-
type: "delete",
|
|
207
|
-
|
|
208
|
-
anchor: {
|
|
209
|
-
path: focusTextBlock.path,
|
|
210
|
-
offset: 0
|
|
211
|
-
},
|
|
212
|
-
focus: {
|
|
213
|
-
path: focusTextBlock.path,
|
|
214
|
-
offset: 0
|
|
215
|
-
}
|
|
216
|
-
}
|
|
203
|
+
type: "delete block",
|
|
204
|
+
blockPath: focusTextBlock.path
|
|
217
205
|
}, {
|
|
218
206
|
type: "select",
|
|
219
207
|
selection: {
|
|
@@ -391,6 +379,58 @@ function isPortableTextBlock(node) {
|
|
|
391
379
|
node.children.every((child) => typeof child == "object" && "_type" in child)
|
|
392
380
|
);
|
|
393
381
|
}
|
|
382
|
+
function blockOffsetToSpanSelectionPoint({
|
|
383
|
+
value,
|
|
384
|
+
blockOffset
|
|
385
|
+
}) {
|
|
386
|
+
let offsetLeft = blockOffset.offset, selectionPoint;
|
|
387
|
+
for (const block of value)
|
|
388
|
+
if (block._key === blockOffset.path[0]._key && isPortableTextTextBlock(block)) {
|
|
389
|
+
for (const child of block.children)
|
|
390
|
+
if (isPortableTextSpan$1(child)) {
|
|
391
|
+
if (offsetLeft === 0) {
|
|
392
|
+
selectionPoint = {
|
|
393
|
+
path: [...blockOffset.path, "children", {
|
|
394
|
+
_key: child._key
|
|
395
|
+
}],
|
|
396
|
+
offset: 0
|
|
397
|
+
};
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
if (offsetLeft <= child.text.length) {
|
|
401
|
+
selectionPoint = {
|
|
402
|
+
path: [...blockOffset.path, "children", {
|
|
403
|
+
_key: child._key
|
|
404
|
+
}],
|
|
405
|
+
offset: offsetLeft
|
|
406
|
+
};
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
offsetLeft -= child.text.length;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return selectionPoint;
|
|
413
|
+
}
|
|
414
|
+
function spanSelectionPointToBlockOffset({
|
|
415
|
+
value,
|
|
416
|
+
selectionPoint
|
|
417
|
+
}) {
|
|
418
|
+
let offset = 0;
|
|
419
|
+
for (const block of value)
|
|
420
|
+
if (block._key === selectionPoint.path[0]._key && isPortableTextTextBlock(block)) {
|
|
421
|
+
for (const child of block.children)
|
|
422
|
+
if (isPortableTextSpan$1(child)) {
|
|
423
|
+
if (child._key === selectionPoint.path[2]._key)
|
|
424
|
+
return {
|
|
425
|
+
path: [{
|
|
426
|
+
_key: block._key
|
|
427
|
+
}],
|
|
428
|
+
offset: offset + selectionPoint.offset
|
|
429
|
+
};
|
|
430
|
+
offset += child.text.length;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
394
434
|
function createMarkdownBehaviors(config) {
|
|
395
435
|
const automaticBlockquoteOnSpace = {
|
|
396
436
|
on: "insert text",
|
|
@@ -403,12 +443,24 @@ function createMarkdownBehaviors(config) {
|
|
|
403
443
|
const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
|
|
404
444
|
if (!selectionCollapsed || !focusTextBlock || !focusSpan)
|
|
405
445
|
return !1;
|
|
406
|
-
const
|
|
446
|
+
const blockOffset = spanSelectionPointToBlockOffset({
|
|
447
|
+
value: context.value,
|
|
448
|
+
selectionPoint: {
|
|
449
|
+
path: [{
|
|
450
|
+
_key: focusTextBlock.node._key
|
|
451
|
+
}, "children", {
|
|
452
|
+
_key: focusSpan.node._key
|
|
453
|
+
}],
|
|
454
|
+
offset: context.selection.focus.offset
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
if (!blockOffset)
|
|
458
|
+
return !1;
|
|
459
|
+
const blockText = getTextBlockText(focusTextBlock.node), caretAtTheEndOfQuote = blockOffset.offset === 1, looksLikeMarkdownQuote = /^>/.test(blockText), blockquoteStyle = config.blockquoteStyle?.({
|
|
407
460
|
schema: context.schema
|
|
408
461
|
});
|
|
409
462
|
return caretAtTheEndOfQuote && looksLikeMarkdownQuote && blockquoteStyle !== void 0 ? {
|
|
410
463
|
focusTextBlock,
|
|
411
|
-
focusSpan,
|
|
412
464
|
style: blockquoteStyle
|
|
413
465
|
} : !1;
|
|
414
466
|
},
|
|
@@ -417,7 +469,6 @@ function createMarkdownBehaviors(config) {
|
|
|
417
469
|
text: " "
|
|
418
470
|
}], (_, {
|
|
419
471
|
focusTextBlock,
|
|
420
|
-
focusSpan,
|
|
421
472
|
style
|
|
422
473
|
}) => [{
|
|
423
474
|
type: "unset block",
|
|
@@ -428,19 +479,17 @@ function createMarkdownBehaviors(config) {
|
|
|
428
479
|
style,
|
|
429
480
|
paths: [focusTextBlock.path]
|
|
430
481
|
}, {
|
|
431
|
-
type: "delete",
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
offset: 2
|
|
440
|
-
}
|
|
482
|
+
type: "delete text",
|
|
483
|
+
anchor: {
|
|
484
|
+
path: focusTextBlock.path,
|
|
485
|
+
offset: 0
|
|
486
|
+
},
|
|
487
|
+
focus: {
|
|
488
|
+
path: focusTextBlock.path,
|
|
489
|
+
offset: 2
|
|
441
490
|
}
|
|
442
491
|
}]]
|
|
443
|
-
},
|
|
492
|
+
}, automaticHr = {
|
|
444
493
|
on: "insert text",
|
|
445
494
|
guard: ({
|
|
446
495
|
context,
|
|
@@ -449,14 +498,14 @@ function createMarkdownBehaviors(config) {
|
|
|
449
498
|
const hrCharacter = event.text === "-" ? "-" : event.text === "*" ? "*" : event.text === "_" ? "_" : void 0;
|
|
450
499
|
if (hrCharacter === void 0)
|
|
451
500
|
return !1;
|
|
452
|
-
const
|
|
501
|
+
const hrObject = config.horizontalRuleObject?.({
|
|
453
502
|
schema: context.schema
|
|
454
503
|
}), focusBlock = getFocusTextBlock(context), selectionCollapsed = selectionIsCollapsed(context);
|
|
455
|
-
if (!
|
|
504
|
+
if (!hrObject || !focusBlock || !selectionCollapsed)
|
|
456
505
|
return !1;
|
|
457
506
|
const onlyText = focusBlock.node.children.every(isPortableTextSpan), blockText = focusBlock.node.children.map((child) => child.text ?? "").join("");
|
|
458
507
|
return onlyText && blockText === `${hrCharacter}${hrCharacter}` ? {
|
|
459
|
-
|
|
508
|
+
hrObject,
|
|
460
509
|
focusBlock,
|
|
461
510
|
hrCharacter
|
|
462
511
|
} : !1;
|
|
@@ -467,26 +516,59 @@ function createMarkdownBehaviors(config) {
|
|
|
467
516
|
type: "insert text",
|
|
468
517
|
text: hrCharacter
|
|
469
518
|
}], (_, {
|
|
470
|
-
|
|
519
|
+
hrObject,
|
|
471
520
|
focusBlock
|
|
472
521
|
}) => [{
|
|
473
522
|
type: "insert block object",
|
|
474
|
-
|
|
523
|
+
placement: "after",
|
|
524
|
+
blockObject: hrObject
|
|
475
525
|
}, {
|
|
476
|
-
type: "delete",
|
|
477
|
-
|
|
478
|
-
anchor: {
|
|
479
|
-
path: focusBlock.path,
|
|
480
|
-
offset: 0
|
|
481
|
-
},
|
|
482
|
-
focus: {
|
|
483
|
-
path: focusBlock.path,
|
|
484
|
-
offset: 0
|
|
485
|
-
}
|
|
486
|
-
}
|
|
526
|
+
type: "delete block",
|
|
527
|
+
blockPath: focusBlock.path
|
|
487
528
|
}, {
|
|
488
529
|
type: "insert text block",
|
|
489
|
-
|
|
530
|
+
placement: "after"
|
|
531
|
+
}]]
|
|
532
|
+
}, automaticHrOnPaste = {
|
|
533
|
+
on: "paste",
|
|
534
|
+
guard: ({
|
|
535
|
+
context,
|
|
536
|
+
event
|
|
537
|
+
}) => {
|
|
538
|
+
const text = event.clipboardData.getData("text/plain"), hrRegExp = /^(---)$|(___)$|(\*\*\*)$/gm, hrCharacters = text.match(hrRegExp)?.[0], hrObject = config.horizontalRuleObject?.({
|
|
539
|
+
schema: context.schema
|
|
540
|
+
}), focusBlock = getFocusBlock(context);
|
|
541
|
+
return !hrCharacters || !hrObject || !focusBlock ? !1 : {
|
|
542
|
+
hrCharacters,
|
|
543
|
+
hrObject,
|
|
544
|
+
focusBlock
|
|
545
|
+
};
|
|
546
|
+
},
|
|
547
|
+
actions: [(_, {
|
|
548
|
+
hrCharacters
|
|
549
|
+
}) => [{
|
|
550
|
+
type: "insert text",
|
|
551
|
+
text: hrCharacters
|
|
552
|
+
}], (_, {
|
|
553
|
+
hrObject,
|
|
554
|
+
focusBlock
|
|
555
|
+
}) => isPortableTextTextBlock(focusBlock.node) ? [{
|
|
556
|
+
type: "insert text block",
|
|
557
|
+
textBlock: {
|
|
558
|
+
children: focusBlock.node.children
|
|
559
|
+
},
|
|
560
|
+
placement: "after"
|
|
561
|
+
}, {
|
|
562
|
+
type: "insert block object",
|
|
563
|
+
blockObject: hrObject,
|
|
564
|
+
placement: "after"
|
|
565
|
+
}, {
|
|
566
|
+
type: "delete block",
|
|
567
|
+
blockPath: focusBlock.path
|
|
568
|
+
}] : [{
|
|
569
|
+
type: "insert block object",
|
|
570
|
+
blockObject: hrObject,
|
|
571
|
+
placement: "after"
|
|
490
572
|
}]]
|
|
491
573
|
}, automaticHeadingOnSpace = {
|
|
492
574
|
on: "insert text",
|
|
@@ -499,8 +581,21 @@ function createMarkdownBehaviors(config) {
|
|
|
499
581
|
const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
|
|
500
582
|
if (!selectionCollapsed || !focusTextBlock || !focusSpan)
|
|
501
583
|
return !1;
|
|
502
|
-
const
|
|
503
|
-
|
|
584
|
+
const blockOffset = spanSelectionPointToBlockOffset({
|
|
585
|
+
value: context.value,
|
|
586
|
+
selectionPoint: {
|
|
587
|
+
path: [{
|
|
588
|
+
_key: focusTextBlock.node._key
|
|
589
|
+
}, "children", {
|
|
590
|
+
_key: focusSpan.node._key
|
|
591
|
+
}],
|
|
592
|
+
offset: context.selection.focus.offset
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
if (!blockOffset)
|
|
596
|
+
return !1;
|
|
597
|
+
const blockText = getTextBlockText(focusTextBlock.node), markdownHeadingSearch = /^#+/.exec(blockText), level = markdownHeadingSearch ? markdownHeadingSearch[0].length : void 0;
|
|
598
|
+
if (blockOffset.offset !== level)
|
|
504
599
|
return !1;
|
|
505
600
|
const style = level !== void 0 ? config.headingStyle?.({
|
|
506
601
|
schema: context.schema,
|
|
@@ -508,7 +603,6 @@ function createMarkdownBehaviors(config) {
|
|
|
508
603
|
}) : void 0;
|
|
509
604
|
return level !== void 0 && style !== void 0 ? {
|
|
510
605
|
focusTextBlock,
|
|
511
|
-
focusSpan,
|
|
512
606
|
style,
|
|
513
607
|
level
|
|
514
608
|
} : !1;
|
|
@@ -518,7 +612,6 @@ function createMarkdownBehaviors(config) {
|
|
|
518
612
|
text: " "
|
|
519
613
|
}], (_, {
|
|
520
614
|
focusTextBlock,
|
|
521
|
-
focusSpan,
|
|
522
615
|
style,
|
|
523
616
|
level
|
|
524
617
|
}) => [{
|
|
@@ -530,16 +623,14 @@ function createMarkdownBehaviors(config) {
|
|
|
530
623
|
style,
|
|
531
624
|
paths: [focusTextBlock.path]
|
|
532
625
|
}, {
|
|
533
|
-
type: "delete",
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
offset: level + 1
|
|
542
|
-
}
|
|
626
|
+
type: "delete text",
|
|
627
|
+
anchor: {
|
|
628
|
+
path: focusTextBlock.path,
|
|
629
|
+
offset: 0
|
|
630
|
+
},
|
|
631
|
+
focus: {
|
|
632
|
+
path: focusTextBlock.path,
|
|
633
|
+
offset: level + 1
|
|
543
634
|
}
|
|
544
635
|
}]]
|
|
545
636
|
}, clearStyleOnBackspace = {
|
|
@@ -577,15 +668,27 @@ function createMarkdownBehaviors(config) {
|
|
|
577
668
|
const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
|
|
578
669
|
if (!selectionCollapsed || !focusTextBlock || !focusSpan)
|
|
579
670
|
return !1;
|
|
580
|
-
const
|
|
671
|
+
const blockOffset = spanSelectionPointToBlockOffset({
|
|
672
|
+
value: context.value,
|
|
673
|
+
selectionPoint: {
|
|
674
|
+
path: [{
|
|
675
|
+
_key: focusTextBlock.node._key
|
|
676
|
+
}, "children", {
|
|
677
|
+
_key: focusSpan.node._key
|
|
678
|
+
}],
|
|
679
|
+
offset: context.selection.focus.offset
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
if (!blockOffset)
|
|
683
|
+
return !1;
|
|
684
|
+
const blockText = getTextBlockText(focusTextBlock.node), defaultStyle = config.defaultStyle?.({
|
|
581
685
|
schema: context.schema
|
|
582
|
-
}), looksLikeUnorderedList = /^(-|\*)/.test(
|
|
686
|
+
}), looksLikeUnorderedList = /^(-|\*)/.test(blockText), unorderedListStyle = config.unorderedListStyle?.({
|
|
583
687
|
schema: context.schema
|
|
584
|
-
}), caretAtTheEndOfUnorderedList =
|
|
688
|
+
}), caretAtTheEndOfUnorderedList = blockOffset.offset === 1;
|
|
585
689
|
if (defaultStyle && caretAtTheEndOfUnorderedList && looksLikeUnorderedList && unorderedListStyle !== void 0)
|
|
586
690
|
return {
|
|
587
691
|
focusTextBlock,
|
|
588
|
-
focusSpan,
|
|
589
692
|
listItem: unorderedListStyle,
|
|
590
693
|
listItemLength: 1,
|
|
591
694
|
style: defaultStyle
|
|
@@ -595,7 +698,6 @@ function createMarkdownBehaviors(config) {
|
|
|
595
698
|
}), caretAtTheEndOfOrderedList = context.selection.focus.offset === 2;
|
|
596
699
|
return defaultStyle && caretAtTheEndOfOrderedList && looksLikeOrderedList && orderedListStyle !== void 0 ? {
|
|
597
700
|
focusTextBlock,
|
|
598
|
-
focusSpan,
|
|
599
701
|
listItem: orderedListStyle,
|
|
600
702
|
listItemLength: 2,
|
|
601
703
|
style: defaultStyle
|
|
@@ -606,7 +708,6 @@ function createMarkdownBehaviors(config) {
|
|
|
606
708
|
text: " "
|
|
607
709
|
}], (_, {
|
|
608
710
|
focusTextBlock,
|
|
609
|
-
focusSpan,
|
|
610
711
|
style,
|
|
611
712
|
listItem,
|
|
612
713
|
listItemLength
|
|
@@ -617,20 +718,18 @@ function createMarkdownBehaviors(config) {
|
|
|
617
718
|
style,
|
|
618
719
|
paths: [focusTextBlock.path]
|
|
619
720
|
}, {
|
|
620
|
-
type: "delete",
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
offset: listItemLength + 1
|
|
629
|
-
}
|
|
721
|
+
type: "delete text",
|
|
722
|
+
anchor: {
|
|
723
|
+
path: focusTextBlock.path,
|
|
724
|
+
offset: 0
|
|
725
|
+
},
|
|
726
|
+
focus: {
|
|
727
|
+
path: focusTextBlock.path,
|
|
728
|
+
offset: listItemLength + 1
|
|
630
729
|
}
|
|
631
730
|
}]]
|
|
632
731
|
};
|
|
633
|
-
return [automaticBlockquoteOnSpace,
|
|
732
|
+
return [automaticBlockquoteOnSpace, automaticHeadingOnSpace, automaticHr, automaticHrOnPaste, clearStyleOnBackspace, automaticListOnSpace];
|
|
634
733
|
}
|
|
635
734
|
function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
636
735
|
if (!portableTextType)
|
|
@@ -4728,7 +4827,65 @@ function createSlateEditor(config) {
|
|
|
4728
4827
|
};
|
|
4729
4828
|
return slateEditors.set(config.editorActor, slateEditor), slateEditor;
|
|
4730
4829
|
}
|
|
4731
|
-
|
|
4830
|
+
function insertBlock({
|
|
4831
|
+
block,
|
|
4832
|
+
placement,
|
|
4833
|
+
editor,
|
|
4834
|
+
schema
|
|
4835
|
+
}) {
|
|
4836
|
+
if (editor.selection) {
|
|
4837
|
+
const [focusBlock, focusBlockPath] = Array.from(Editor.nodes(editor, {
|
|
4838
|
+
at: editor.selection.focus.path.slice(0, 1),
|
|
4839
|
+
match: (n) => !Editor.isEditor(n)
|
|
4840
|
+
}))[0] ?? [void 0, void 0];
|
|
4841
|
+
if (placement === "after") {
|
|
4842
|
+
const nextPath = [focusBlockPath[0] + 1];
|
|
4843
|
+
Transforms.insertNodes(editor, block, {
|
|
4844
|
+
at: nextPath
|
|
4845
|
+
}), Transforms.select(editor, {
|
|
4846
|
+
anchor: {
|
|
4847
|
+
path: [nextPath[0], 0],
|
|
4848
|
+
offset: 0
|
|
4849
|
+
},
|
|
4850
|
+
focus: {
|
|
4851
|
+
path: [nextPath[0], 0],
|
|
4852
|
+
offset: 0
|
|
4853
|
+
}
|
|
4854
|
+
});
|
|
4855
|
+
} else
|
|
4856
|
+
Editor.insertNode(editor, block);
|
|
4857
|
+
focusBlock && isEqualToEmptyEditor([focusBlock], schema) && Transforms.removeNodes(editor, {
|
|
4858
|
+
at: focusBlockPath
|
|
4859
|
+
});
|
|
4860
|
+
} else {
|
|
4861
|
+
const lastBlock = Array.from(Editor.nodes(editor, {
|
|
4862
|
+
match: (n) => !Editor.isEditor(n),
|
|
4863
|
+
at: [],
|
|
4864
|
+
reverse: !0
|
|
4865
|
+
}))[0];
|
|
4866
|
+
Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], schema) && Transforms.removeNodes(editor, {
|
|
4867
|
+
at: lastBlock[1]
|
|
4868
|
+
});
|
|
4869
|
+
}
|
|
4870
|
+
}
|
|
4871
|
+
const insertBlockObjectActionImplementation = ({
|
|
4872
|
+
context,
|
|
4873
|
+
action
|
|
4874
|
+
}) => {
|
|
4875
|
+
const block = toSlateValue([{
|
|
4876
|
+
_key: context.keyGenerator(),
|
|
4877
|
+
_type: action.blockObject.name,
|
|
4878
|
+
...action.blockObject.value ? action.blockObject.value : {}
|
|
4879
|
+
}], {
|
|
4880
|
+
schemaTypes: context.schema
|
|
4881
|
+
})[0];
|
|
4882
|
+
insertBlock({
|
|
4883
|
+
block,
|
|
4884
|
+
placement: action.placement,
|
|
4885
|
+
editor: action.editor,
|
|
4886
|
+
schema: context.schema
|
|
4887
|
+
});
|
|
4888
|
+
}, debug$5 = debugWithName("API:editable");
|
|
4732
4889
|
function createEditableAPI(editor, editorActor) {
|
|
4733
4890
|
const types = editorActor.getSnapshot().context.schema;
|
|
4734
4891
|
return {
|
|
@@ -4817,18 +4974,21 @@ function createEditableAPI(editor, editorActor) {
|
|
|
4817
4974
|
at: editor.selection
|
|
4818
4975
|
}), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types)?.focus.path || [];
|
|
4819
4976
|
},
|
|
4820
|
-
insertBlock: (type, value) => insertBlockObjectActionImplementation({
|
|
4977
|
+
insertBlock: (type, value) => (insertBlockObjectActionImplementation({
|
|
4821
4978
|
context: {
|
|
4822
4979
|
keyGenerator: editorActor.getSnapshot().context.keyGenerator,
|
|
4823
4980
|
schema: types
|
|
4824
4981
|
},
|
|
4825
4982
|
action: {
|
|
4826
4983
|
type: "insert block object",
|
|
4827
|
-
|
|
4828
|
-
|
|
4984
|
+
blockObject: {
|
|
4985
|
+
name: type.name,
|
|
4986
|
+
value
|
|
4987
|
+
},
|
|
4988
|
+
placement: "auto",
|
|
4829
4989
|
editor
|
|
4830
4990
|
}
|
|
4831
|
-
}),
|
|
4991
|
+
}), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types)?.focus.path ?? []),
|
|
4832
4992
|
hasBlockStyle: (style) => {
|
|
4833
4993
|
try {
|
|
4834
4994
|
return editor.pteHasBlockStyle(style);
|
|
@@ -4998,35 +5158,6 @@ function createEditableAPI(editor, editorActor) {
|
|
|
4998
5158
|
}
|
|
4999
5159
|
};
|
|
5000
5160
|
}
|
|
5001
|
-
const insertBlockObjectActionImplementation = ({
|
|
5002
|
-
context,
|
|
5003
|
-
action
|
|
5004
|
-
}) => {
|
|
5005
|
-
const editor = action.editor, types = context.schema, block = toSlateValue([{
|
|
5006
|
-
_key: context.keyGenerator(),
|
|
5007
|
-
_type: action.name,
|
|
5008
|
-
...action.value ? action.value : {}
|
|
5009
|
-
}], {
|
|
5010
|
-
schemaTypes: context.schema
|
|
5011
|
-
})[0];
|
|
5012
|
-
if (!editor.selection) {
|
|
5013
|
-
const lastBlock = Array.from(Editor.nodes(editor, {
|
|
5014
|
-
match: (n) => !Editor.isEditor(n),
|
|
5015
|
-
at: [],
|
|
5016
|
-
reverse: !0
|
|
5017
|
-
}))[0];
|
|
5018
|
-
return Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types) && Transforms.removeNodes(editor, {
|
|
5019
|
-
at: lastBlock[1]
|
|
5020
|
-
}), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types)?.focus.path ?? [];
|
|
5021
|
-
}
|
|
5022
|
-
const focusBlock = Array.from(Editor.nodes(editor, {
|
|
5023
|
-
at: editor.selection.focus.path.slice(0, 1),
|
|
5024
|
-
match: (n) => n._type === types.block.name
|
|
5025
|
-
}))[0];
|
|
5026
|
-
return Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types) && Transforms.removeNodes(editor, {
|
|
5027
|
-
at: focusBlock[1]
|
|
5028
|
-
}), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types)?.focus.path || [];
|
|
5029
|
-
};
|
|
5030
5161
|
function isAnnotationActive({
|
|
5031
5162
|
editor,
|
|
5032
5163
|
annotation
|
|
@@ -5422,18 +5553,52 @@ const addAnnotationActionImplementation = ({
|
|
|
5422
5553
|
}) => {
|
|
5423
5554
|
deleteForward(action.editor, action.unit);
|
|
5424
5555
|
},
|
|
5425
|
-
delete: ({
|
|
5556
|
+
"delete block": ({
|
|
5557
|
+
action
|
|
5558
|
+
}) => {
|
|
5559
|
+
const range = toSlateRange({
|
|
5560
|
+
anchor: {
|
|
5561
|
+
path: action.blockPath,
|
|
5562
|
+
offset: 0
|
|
5563
|
+
},
|
|
5564
|
+
focus: {
|
|
5565
|
+
path: action.blockPath,
|
|
5566
|
+
offset: 0
|
|
5567
|
+
}
|
|
5568
|
+
}, action.editor);
|
|
5569
|
+
if (!range) {
|
|
5570
|
+
console.error("Unable to find Slate range from selection points");
|
|
5571
|
+
return;
|
|
5572
|
+
}
|
|
5573
|
+
Transforms.removeNodes(action.editor, {
|
|
5574
|
+
at: range
|
|
5575
|
+
});
|
|
5576
|
+
},
|
|
5577
|
+
"delete text": ({
|
|
5578
|
+
context,
|
|
5426
5579
|
action
|
|
5427
5580
|
}) => {
|
|
5428
|
-
const
|
|
5429
|
-
|
|
5430
|
-
|
|
5581
|
+
const value = fromSlateValue(action.editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(action.editor)), anchor = blockOffsetToSpanSelectionPoint({
|
|
5582
|
+
value,
|
|
5583
|
+
blockOffset: action.anchor
|
|
5584
|
+
}), focus = blockOffsetToSpanSelectionPoint({
|
|
5585
|
+
value,
|
|
5586
|
+
blockOffset: action.focus
|
|
5587
|
+
});
|
|
5588
|
+
if (!anchor || !focus) {
|
|
5589
|
+
console.error("Unable to find anchor or focus selection point");
|
|
5431
5590
|
return;
|
|
5432
5591
|
}
|
|
5433
|
-
|
|
5434
|
-
|
|
5435
|
-
|
|
5436
|
-
|
|
5592
|
+
const range = toSlateRange({
|
|
5593
|
+
anchor,
|
|
5594
|
+
focus
|
|
5595
|
+
}, action.editor);
|
|
5596
|
+
if (!range) {
|
|
5597
|
+
console.error("Unable to find Slate range from selection points");
|
|
5598
|
+
return;
|
|
5599
|
+
}
|
|
5600
|
+
Transforms.delete(action.editor, {
|
|
5601
|
+
at: range
|
|
5437
5602
|
});
|
|
5438
5603
|
},
|
|
5439
5604
|
"insert block object": insertBlockObjectActionImplementation,
|
|
@@ -5449,16 +5614,27 @@ const addAnnotationActionImplementation = ({
|
|
|
5449
5614
|
context,
|
|
5450
5615
|
action
|
|
5451
5616
|
}) => {
|
|
5452
|
-
|
|
5617
|
+
const block = toSlateValue([{
|
|
5453
5618
|
_key: context.keyGenerator(),
|
|
5454
5619
|
_type: context.schema.block.name,
|
|
5455
5620
|
style: context.schema.styles[0].value ?? "normal",
|
|
5456
5621
|
markDefs: [],
|
|
5457
|
-
children:
|
|
5622
|
+
children: action.textBlock?.children?.map((child) => ({
|
|
5623
|
+
...child,
|
|
5624
|
+
_key: context.keyGenerator()
|
|
5625
|
+
})) ?? [{
|
|
5626
|
+
_type: context.schema.span.name,
|
|
5458
5627
|
_key: context.keyGenerator(),
|
|
5459
|
-
_type: "span",
|
|
5460
5628
|
text: ""
|
|
5461
5629
|
}]
|
|
5630
|
+
}], {
|
|
5631
|
+
schemaTypes: context.schema
|
|
5632
|
+
})[0];
|
|
5633
|
+
insertBlock({
|
|
5634
|
+
block,
|
|
5635
|
+
editor: action.editor,
|
|
5636
|
+
schema: context.schema,
|
|
5637
|
+
placement: action.placement
|
|
5462
5638
|
});
|
|
5463
5639
|
},
|
|
5464
5640
|
effect: ({
|
|
@@ -5493,8 +5669,15 @@ function performAction({
|
|
|
5493
5669
|
action
|
|
5494
5670
|
}) {
|
|
5495
5671
|
switch (action.type) {
|
|
5496
|
-
case "delete": {
|
|
5497
|
-
behaviorActionImplementations
|
|
5672
|
+
case "delete block": {
|
|
5673
|
+
behaviorActionImplementations["delete block"]({
|
|
5674
|
+
context,
|
|
5675
|
+
action
|
|
5676
|
+
});
|
|
5677
|
+
break;
|
|
5678
|
+
}
|
|
5679
|
+
case "delete text": {
|
|
5680
|
+
behaviorActionImplementations["delete text"]({
|
|
5498
5681
|
context,
|
|
5499
5682
|
action
|
|
5500
5683
|
});
|