@portabletext/editor 1.0.19 → 1.1.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.
Files changed (72) hide show
  1. package/lib/index.d.mts +140 -66
  2. package/lib/index.d.ts +140 -66
  3. package/lib/index.esm.js +1125 -362
  4. package/lib/index.esm.js.map +1 -1
  5. package/lib/index.js +1125 -362
  6. package/lib/index.js.map +1 -1
  7. package/lib/index.mjs +1125 -362
  8. package/lib/index.mjs.map +1 -1
  9. package/package.json +2 -2
  10. package/src/editor/Editable.tsx +107 -36
  11. package/src/editor/PortableTextEditor.tsx +47 -12
  12. package/src/editor/__tests__/PortableTextEditor.test.tsx +42 -15
  13. package/src/editor/__tests__/PortableTextEditorTester.tsx +50 -38
  14. package/src/editor/__tests__/RangeDecorations.test.tsx +0 -1
  15. package/src/editor/__tests__/handleClick.test.tsx +28 -9
  16. package/src/editor/__tests__/insert-block.test.tsx +22 -6
  17. package/src/editor/__tests__/pteWarningsSelfSolving.test.tsx +30 -62
  18. package/src/editor/__tests__/utils.ts +10 -3
  19. package/src/editor/components/DraggableBlock.tsx +36 -13
  20. package/src/editor/components/Element.tsx +59 -17
  21. package/src/editor/components/Leaf.tsx +106 -68
  22. package/src/editor/components/SlateContainer.tsx +12 -5
  23. package/src/editor/components/Synchronizer.tsx +5 -2
  24. package/src/editor/hooks/usePortableTextEditor.ts +2 -2
  25. package/src/editor/hooks/usePortableTextEditorSelection.tsx +9 -3
  26. package/src/editor/hooks/useSyncValue.test.tsx +9 -4
  27. package/src/editor/hooks/useSyncValue.ts +199 -130
  28. package/src/editor/nodes/DefaultAnnotation.tsx +6 -3
  29. package/src/editor/plugins/__tests__/createWithInsertData.test.tsx +25 -7
  30. package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +26 -9
  31. package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +15 -5
  32. package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +60 -19
  33. package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +4 -2
  34. package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +4 -2
  35. package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +61 -17
  36. package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +6 -3
  37. package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +30 -13
  38. package/src/editor/plugins/createWithEditableAPI.ts +354 -124
  39. package/src/editor/plugins/createWithHotKeys.ts +41 -121
  40. package/src/editor/plugins/createWithInsertBreak.ts +166 -27
  41. package/src/editor/plugins/createWithInsertData.ts +60 -23
  42. package/src/editor/plugins/createWithMaxBlocks.ts +5 -2
  43. package/src/editor/plugins/createWithObjectKeys.ts +7 -3
  44. package/src/editor/plugins/createWithPatches.ts +60 -16
  45. package/src/editor/plugins/createWithPlaceholderBlock.ts +7 -3
  46. package/src/editor/plugins/createWithPortableTextBlockStyle.ts +17 -7
  47. package/src/editor/plugins/createWithPortableTextLists.ts +21 -8
  48. package/src/editor/plugins/createWithPortableTextMarkModel.ts +213 -46
  49. package/src/editor/plugins/createWithPortableTextSelections.ts +4 -2
  50. package/src/editor/plugins/createWithSchemaTypes.ts +25 -9
  51. package/src/editor/plugins/createWithUndoRedo.ts +107 -24
  52. package/src/editor/plugins/createWithUtils.ts +32 -10
  53. package/src/editor/plugins/index.ts +31 -10
  54. package/src/types/editor.ts +44 -15
  55. package/src/types/options.ts +4 -2
  56. package/src/types/slate.ts +2 -2
  57. package/src/utils/__tests__/dmpToOperations.test.ts +38 -13
  58. package/src/utils/__tests__/operationToPatches.test.ts +3 -2
  59. package/src/utils/__tests__/patchToOperations.test.ts +15 -4
  60. package/src/utils/__tests__/ranges.test.ts +8 -3
  61. package/src/utils/__tests__/valueNormalization.test.tsx +12 -4
  62. package/src/utils/__tests__/values.test.ts +0 -1
  63. package/src/utils/applyPatch.ts +71 -20
  64. package/src/utils/getPortableTextMemberSchemaTypes.ts +30 -15
  65. package/src/utils/operationToPatches.ts +126 -43
  66. package/src/utils/paths.ts +24 -7
  67. package/src/utils/ranges.ts +12 -5
  68. package/src/utils/selection.ts +19 -7
  69. package/src/utils/validateValue.ts +118 -45
  70. package/src/utils/values.ts +31 -9
  71. package/src/utils/weakMaps.ts +18 -8
  72. package/src/utils/withChanges.ts +4 -2
package/lib/index.js CHANGED
@@ -38,7 +38,9 @@ function createArrayedPath(point, editor) {
38
38
  return [];
39
39
  if (editor.isVoid(block))
40
40
  return [blockPath[0], 0];
41
- const childPath = [point.path[2]], childIndex = block.children.findIndex((child) => isEqual__default.default([{ _key: child._key }], childPath));
41
+ const childPath = [point.path[2]], childIndex = block.children.findIndex(
42
+ (child) => isEqual__default.default([{ _key: child._key }], childPath)
43
+ );
42
44
  if (childIndex >= 0 && block.children[childIndex]) {
43
45
  const child = block.children[childIndex];
44
46
  return slate.Element.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
@@ -83,7 +85,9 @@ function normalizePoint(point, value) {
83
85
  return null;
84
86
  const newPath = [];
85
87
  let newOffset = point.offset || 0;
86
- const blockKey = typeof point.path[0] == "object" && "_key" in point.path[0] && point.path[0]._key, childKey = typeof point.path[2] == "object" && "_key" in point.path[2] && point.path[2]._key, block = value.find((blk) => blk._key === blockKey);
88
+ const blockKey = typeof point.path[0] == "object" && "_key" in point.path[0] && point.path[0]._key, childKey = typeof point.path[2] == "object" && "_key" in point.path[2] && point.path[2]._key, block = value.find(
89
+ (blk) => blk._key === blockKey
90
+ );
87
91
  if (block)
88
92
  newPath.push({ _key: block._key });
89
93
  else
@@ -113,7 +117,9 @@ function keepObjectEquality(object, keyMap) {
113
117
  }
114
118
  function toSlateValue(value, { schemaTypes }, keyMap = {}) {
115
119
  return value && Array.isArray(value) ? value.map((block) => {
116
- const { _type, _key, ...rest } = block, voidChildren = [{ _key: VOID_CHILD_KEY, _type: "span", text: "", marks: [] }];
120
+ const { _type, _key, ...rest } = block, voidChildren = [
121
+ { _key: VOID_CHILD_KEY, _type: "span", text: "", marks: [] }
122
+ ];
117
123
  if (block && block._type === schemaTypes.block.name) {
118
124
  const textBlock = block;
119
125
  let hasInlines = !1;
@@ -154,12 +160,25 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
154
160
  const { _type: _cType } = child;
155
161
  if ("value" in child && _cType !== "span") {
156
162
  hasInlines = !0;
157
- const { value: v, _key: k, _type: t, __inline: _i, children: _c, ...rest } = child;
158
- return keepObjectEquality({ ...rest, ...v, _key: k, _type: t }, keyMap);
163
+ const {
164
+ value: v,
165
+ _key: k,
166
+ _type: t,
167
+ __inline: _i,
168
+ children: _c,
169
+ ...rest
170
+ } = child;
171
+ return keepObjectEquality(
172
+ { ...rest, ...v, _key: k, _type: t },
173
+ keyMap
174
+ );
159
175
  }
160
176
  return child;
161
177
  });
162
- return hasInlines ? keepObjectEquality({ ...block, children, _key, _type }, keyMap) : block;
178
+ return hasInlines ? keepObjectEquality(
179
+ { ...block, children, _key, _type },
180
+ keyMap
181
+ ) : block;
163
182
  }
164
183
  const blockValue = "value" in block && block.value;
165
184
  return keepObjectEquality(
@@ -331,10 +350,23 @@ function getCounterContentForListLevel(level) {
331
350
  return "counter(listItemNumberNextNextNext) '. '";
332
351
  }
333
352
  }
334
- const debug$l = debugWithName("components:DraggableBlock"), DraggableBlock = ({ children, element, readOnly, blockRef }) => {
335
- const editor = slateReact.useSlateStatic(), dragGhostRef = react.useRef(), [isDragOver, setIsDragOver] = react.useState(!1), isVoid = react.useMemo(() => slate.Editor.isVoid(editor, element), [editor, element]), isInline = react.useMemo(() => slate.Editor.isInline(editor, element), [editor, element]), [blockElement, setBlockElement] = react.useState(null);
353
+ const debug$l = debugWithName("components:DraggableBlock"), DraggableBlock = ({
354
+ children,
355
+ element,
356
+ readOnly,
357
+ blockRef
358
+ }) => {
359
+ const editor = slateReact.useSlateStatic(), dragGhostRef = react.useRef(), [isDragOver, setIsDragOver] = react.useState(!1), isVoid = react.useMemo(
360
+ () => slate.Editor.isVoid(editor, element),
361
+ [editor, element]
362
+ ), isInline = react.useMemo(
363
+ () => slate.Editor.isInline(editor, element),
364
+ [editor, element]
365
+ ), [blockElement, setBlockElement] = react.useState(null);
336
366
  react.useEffect(
337
- () => setBlockElement(blockRef ? blockRef.current : slateReact.ReactEditor.toDOMNode(editor, element)),
367
+ () => setBlockElement(
368
+ blockRef ? blockRef.current : slateReact.ReactEditor.toDOMNode(editor, element)
369
+ ),
338
370
  [editor, element, blockRef]
339
371
  );
340
372
  const handleDragOver = react.useCallback(
@@ -421,7 +453,9 @@ const debug$l = debugWithName("components:DraggableBlock"), DraggableBlock = ({
421
453
  }
422
454
  if (debug$l("Drag start"), IS_DRAGGING.set(editor, !0), event.dataTransfer && (event.dataTransfer.setData("application/portable-text", "something"), event.dataTransfer.effectAllowed = "move"), blockElement && blockElement instanceof HTMLElement) {
423
455
  let dragGhost = blockElement.cloneNode(!0);
424
- const customGhost = dragGhost.querySelector("[data-pt-drag-ghost-element]");
456
+ const customGhost = dragGhost.querySelector(
457
+ "[data-pt-drag-ghost-element]"
458
+ );
425
459
  if (customGhost && (dragGhost = customGhost), dragGhost.setAttribute("data-dragged", ""), document.body) {
426
460
  dragGhostRef.current = dragGhost, dragGhost.style.position = "absolute", dragGhost.style.left = "-99999px", dragGhost.style.boxSizing = "border-box", document.body.appendChild(dragGhost);
427
461
  const rect = blockElement.getBoundingClientRect(), x = event.clientX - rect.left, y = event.clientY - rect.top;
@@ -479,7 +513,11 @@ const EMPTY_ANNOTATIONS = [], inlineBlockStyle = { display: "inline-block" }, El
479
513
  spellCheck
480
514
  }) => {
481
515
  const editor = slateReact.useSlateStatic(), selected = slateReact.useSelected(), blockRef = react.useRef(null), inlineBlockObjectRef = react.useRef(null), focused = selected && editor.selection && slate.Range.isCollapsed(editor.selection) || !1, value = react.useMemo(
482
- () => fromSlateValue([element], schemaTypes.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0],
516
+ () => fromSlateValue(
517
+ [element],
518
+ schemaTypes.block.name,
519
+ KEY_TO_VALUE_ELEMENT.get(editor)
520
+ )[0],
483
521
  [editor, element, schemaTypes.block.name]
484
522
  );
485
523
  let renderedBlock = children, className;
@@ -489,11 +527,17 @@ const EMPTY_ANNOTATIONS = [], inlineBlockStyle = { display: "inline-block" }, El
489
527
  if (typeof element._key != "string")
490
528
  throw new Error("Expected element to have a _key property");
491
529
  if (editor.isInline(element)) {
492
- const path = slateReact.ReactEditor.findPath(editor, element), [block2] = slate.Editor.node(editor, path, { depth: 1 }), schemaType2 = schemaTypes.inlineObjects.find((_type) => _type.name === element._type);
530
+ const path = slateReact.ReactEditor.findPath(editor, element), [block2] = slate.Editor.node(editor, path, { depth: 1 }), schemaType2 = schemaTypes.inlineObjects.find(
531
+ (_type) => _type.name === element._type
532
+ );
493
533
  if (!schemaType2)
494
534
  throw new Error("Could not find type for inline block element");
495
535
  if (slate.Element.isElement(block2)) {
496
- const elmPath = [{ _key: block2._key }, "children", { _key: element._key }];
536
+ const elmPath = [
537
+ { _key: block2._key },
538
+ "children",
539
+ { _key: element._key }
540
+ ];
497
541
  return /* @__PURE__ */ jsxRuntime.jsxs("span", { ...attributes, children: [
498
542
  children,
499
543
  /* @__PURE__ */ jsxRuntime.jsxs(
@@ -531,7 +575,9 @@ const EMPTY_ANNOTATIONS = [], inlineBlockStyle = { display: "inline-block" }, El
531
575
  className = "pt-block pt-text-block";
532
576
  const isListItem = "listItem" in element, style = "style" in element && element.style || "normal";
533
577
  className = `pt-block pt-text-block pt-text-block-style-${style}`;
534
- const blockStyleType = schemaTypes.styles.find((item) => item.value === style);
578
+ const blockStyleType = schemaTypes.styles.find(
579
+ (item) => item.value === style
580
+ );
535
581
  renderStyle && blockStyleType && (renderedBlock = renderStyle({
536
582
  block: element,
537
583
  children,
@@ -544,7 +590,9 @@ const EMPTY_ANNOTATIONS = [], inlineBlockStyle = { display: "inline-block" }, El
544
590
  }));
545
591
  let level;
546
592
  if (isListItem && (typeof element.level == "number" && (level = element.level), className += ` pt-list-item pt-list-item-${element.listItem} pt-list-item-level-${level || 1}`), editor.isListBlock(value) && isListItem && element.listItem) {
547
- const listType = schemaTypes.lists.find((item) => item.value === element.listItem);
593
+ const listType = schemaTypes.lists.find(
594
+ (item) => item.value === element.listItem
595
+ );
548
596
  renderListItem && listType ? renderedBlock = renderListItem({
549
597
  block: value,
550
598
  children: renderedBlock,
@@ -581,15 +629,38 @@ const EMPTY_ANNOTATIONS = [], inlineBlockStyle = { display: "inline-block" }, El
581
629
  {
582
630
  enumerable: !1,
583
631
  get() {
584
- return console.warn("Property 'type' is deprecated, use 'schemaType' instead."), schemaTypes.block;
632
+ return console.warn(
633
+ "Property 'type' is deprecated, use 'schemaType' instead."
634
+ ), schemaTypes.block;
585
635
  }
586
636
  }
587
637
  ), propsOrDefaultRendered = renderBlock ? renderBlock(renderProps) : children;
588
- return /* @__PURE__ */ jsxRuntime.jsx("div", { ...attributes, className, spellCheck, children: /* @__PURE__ */ jsxRuntime.jsx(DraggableBlock, { element, readOnly, blockRef, children: /* @__PURE__ */ jsxRuntime.jsx("div", { ref: blockRef, children: propsOrDefaultRendered }) }) }, element._key);
638
+ return /* @__PURE__ */ jsxRuntime.jsx(
639
+ "div",
640
+ {
641
+ ...attributes,
642
+ className,
643
+ spellCheck,
644
+ children: /* @__PURE__ */ jsxRuntime.jsx(
645
+ DraggableBlock,
646
+ {
647
+ element,
648
+ readOnly,
649
+ blockRef,
650
+ children: /* @__PURE__ */ jsxRuntime.jsx("div", { ref: blockRef, children: propsOrDefaultRendered })
651
+ }
652
+ )
653
+ },
654
+ element._key
655
+ );
589
656
  }
590
- const schemaType = schemaTypes.blockObjects.find((_type) => _type.name === element._type);
657
+ const schemaType = schemaTypes.blockObjects.find(
658
+ (_type) => _type.name === element._type
659
+ );
591
660
  if (!schemaType)
592
- throw new Error(`Could not find schema type for block element of _type ${element._type}`);
661
+ throw new Error(
662
+ `Could not find schema type for block element of _type ${element._type}`
663
+ );
593
664
  className = "pt-block pt-object-block";
594
665
  const block = fromSlateValue(
595
666
  [element],
@@ -612,7 +683,9 @@ const EMPTY_ANNOTATIONS = [], inlineBlockStyle = { display: "inline-block" }, El
612
683
  {
613
684
  enumerable: !1,
614
685
  get() {
615
- return console.warn("Property 'type' is deprecated, use 'schemaType' instead."), schemaType;
686
+ return console.warn(
687
+ "Property 'type' is deprecated, use 'schemaType' instead."
688
+ ), schemaType;
616
689
  }
617
690
  }
618
691
  );
@@ -634,7 +707,11 @@ const EMPTY_ANNOTATIONS = [], inlineBlockStyle = { display: "inline-block" }, El
634
707
  return editor;
635
708
  };
636
709
  function DefaultAnnotation(props) {
637
- const handleClick = react.useCallback(() => alert(JSON.stringify(props.annotation)), [props.annotation]);
710
+ const handleClick = react.useCallback(
711
+ // eslint-disable-next-line no-alert
712
+ () => alert(JSON.stringify(props.annotation)),
713
+ [props.annotation]
714
+ );
638
715
  return /* @__PURE__ */ jsxRuntime.jsx("span", { style: { color: "blue" }, onClick: handleClick, children: props.children });
639
716
  }
640
717
  function getPortableTextMemberSchemaTypes(portableTextType) {
@@ -643,16 +720,24 @@ function getPortableTextMemberSchemaTypes(portableTextType) {
643
720
  const blockType = portableTextType.of?.find(findBlockType);
644
721
  if (!blockType)
645
722
  throw new Error("Block type is not defined in this schema (required)");
646
- const childrenField = blockType.fields?.find((field) => field.name === "children");
723
+ const childrenField = blockType.fields?.find(
724
+ (field) => field.name === "children"
725
+ );
647
726
  if (!childrenField)
648
727
  throw new Error("Children field for block type found in schema (required)");
649
728
  const ofType = childrenField.type.of;
650
729
  if (!ofType)
651
- throw new Error("Valid types for block children not found in schema (required)");
730
+ throw new Error(
731
+ "Valid types for block children not found in schema (required)"
732
+ );
652
733
  const spanType = ofType.find((memberType) => memberType.name === "span");
653
734
  if (!spanType)
654
735
  throw new Error("Span type not found in schema (required)");
655
- const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = portableTextType.of?.filter((field) => field.name !== blockType.name) || [];
736
+ const inlineObjectTypes = ofType.filter(
737
+ (memberType) => memberType.name !== "span"
738
+ ) || [], blockObjectTypes = portableTextType.of?.filter(
739
+ (field) => field.name !== blockType.name
740
+ ) || [];
656
741
  return {
657
742
  styles: resolveEnabledStyles(blockType),
658
743
  decorators: resolveEnabledDecorators(spanType),
@@ -666,10 +751,16 @@ function getPortableTextMemberSchemaTypes(portableTextType) {
666
751
  };
667
752
  }
668
753
  function resolveEnabledStyles(blockType) {
669
- const styleField = blockType.fields?.find((btField) => btField.name === "style");
754
+ const styleField = blockType.fields?.find(
755
+ (btField) => btField.name === "style"
756
+ );
670
757
  if (!styleField)
671
- throw new Error("A field with name 'style' is not defined in the block type (required).");
672
- const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter((style) => style.value);
758
+ throw new Error(
759
+ "A field with name 'style' is not defined in the block type (required)."
760
+ );
761
+ const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter(
762
+ (style) => style.value
763
+ );
673
764
  if (!textStyles || textStyles.length === 0)
674
765
  throw new Error(
675
766
  "The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}."
@@ -680,9 +771,13 @@ function resolveEnabledDecorators(spanType) {
680
771
  return spanType.decorators;
681
772
  }
682
773
  function resolveEnabledListItems(blockType) {
683
- const listField = blockType.fields?.find((btField) => btField.name === "listItem");
774
+ const listField = blockType.fields?.find(
775
+ (btField) => btField.name === "listItem"
776
+ );
684
777
  if (!listField)
685
- throw new Error("A field with name 'listItem' is not defined in the block type (required).");
778
+ throw new Error(
779
+ "A field with name 'listItem' is not defined in the block type (required)."
780
+ );
686
781
  const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
687
782
  if (!listItems)
688
783
  throw new Error("The list field need at least to be an empty array");
@@ -708,7 +803,12 @@ function createOperationToPatches(types2) {
708
803
  const textChild = editor.isTextBlock(block) && editor.isTextSpan(block.children[operation.path[1]]) && block.children[operation.path[1]];
709
804
  if (!textChild)
710
805
  throw new Error("Could not find child");
711
- const path = [{ _key: block._key }, "children", { _key: textChild._key }, "text"], prevBlock = beforeValue[operation.path[0]], prevChild = editor.isTextBlock(prevBlock) && prevBlock.children[operation.path[1]], prevText = editor.isTextSpan(prevChild) ? prevChild.text : "", patch = patches.diffMatchPatch(prevText, textChild.text, path);
806
+ const path = [
807
+ { _key: block._key },
808
+ "children",
809
+ { _key: textChild._key },
810
+ "text"
811
+ ], prevBlock = beforeValue[operation.path[0]], prevChild = editor.isTextBlock(prevBlock) && prevBlock.children[operation.path[1]], prevText = editor.isTextSpan(prevChild) ? prevChild.text : "", patch = patches.diffMatchPatch(prevText, textChild.text, path);
712
812
  return patch.value.length ? [patch] : [];
713
813
  }
714
814
  function removeTextPatch(editor, operation, beforeValue) {
@@ -720,7 +820,12 @@ function createOperationToPatches(types2) {
720
820
  throw new Error("Expected span");
721
821
  if (!textChild)
722
822
  throw new Error("Could not find child");
723
- const path = [{ _key: block._key }, "children", { _key: textChild._key }, "text"], beforeBlock = beforeValue[operation.path[0]], prevTextChild = editor.isTextBlock(beforeBlock) && beforeBlock.children[operation.path[1]], prevText = editor.isTextSpan(prevTextChild) && prevTextChild.text, patch = patches.diffMatchPatch(prevText || "", textChild.text, path);
823
+ const path = [
824
+ { _key: block._key },
825
+ "children",
826
+ { _key: textChild._key },
827
+ "text"
828
+ ], beforeBlock = beforeValue[operation.path[0]], prevTextChild = editor.isTextBlock(beforeBlock) && beforeBlock.children[operation.path[1]], prevText = editor.isTextSpan(prevTextChild) && prevTextChild.text, patch = patches.diffMatchPatch(prevText || "", textChild.text, path);
724
829
  return patch.value ? [patch] : [];
725
830
  }
726
831
  function setNodePatch(editor, operation) {
@@ -732,7 +837,9 @@ function createOperationToPatches(types2) {
732
837
  { ...editor.children[operation.path[0]], ...operation.newProperties },
733
838
  isUndefined__default.default
734
839
  );
735
- return [patches.set(fromSlateValue([setNode], textBlockName)[0], [{ _key: block._key }])];
840
+ return [
841
+ patches.set(fromSlateValue([setNode], textBlockName)[0], [{ _key: block._key }])
842
+ ];
736
843
  } else if (operation.path.length === 2) {
737
844
  const block = editor.children[operation.path[0]];
738
845
  if (editor.isTextBlock(block)) {
@@ -743,11 +850,23 @@ function createOperationToPatches(types2) {
743
850
  if (keys.length === 1 && keyName === "_key") {
744
851
  const val = get__default.default(operation.newProperties, keyName);
745
852
  patches$1.push(
746
- patches.set(val, [{ _key: blockKey }, "children", block.children.indexOf(child), keyName])
853
+ patches.set(val, [
854
+ { _key: blockKey },
855
+ "children",
856
+ block.children.indexOf(child),
857
+ keyName
858
+ ])
747
859
  );
748
860
  } else {
749
861
  const val = get__default.default(operation.newProperties, keyName);
750
- patches$1.push(patches.set(val, [{ _key: blockKey }, "children", { _key: childKey }, keyName]));
862
+ patches$1.push(
863
+ patches.set(val, [
864
+ { _key: blockKey },
865
+ "children",
866
+ { _key: childKey },
867
+ keyName
868
+ ])
869
+ );
751
870
  }
752
871
  }), patches$1;
753
872
  }
@@ -755,21 +874,27 @@ function createOperationToPatches(types2) {
755
874
  }
756
875
  throw new Error("Could not find a valid block");
757
876
  } else
758
- throw new Error(`Unexpected path encountered: ${JSON.stringify(operation.path)}`);
877
+ throw new Error(
878
+ `Unexpected path encountered: ${JSON.stringify(operation.path)}`
879
+ );
759
880
  }
760
881
  function insertNodePatch(editor, operation, beforeValue) {
761
882
  const block = beforeValue[operation.path[0]], isTextBlock = editor.isTextBlock(block);
762
883
  if (operation.path.length === 1) {
763
884
  const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block?._key : beforeBlock?._key;
764
885
  return targetKey ? [
765
- patches.insert([fromSlateValue([operation.node], textBlockName)[0]], position, [
766
- { _key: targetKey }
767
- ])
886
+ patches.insert(
887
+ [fromSlateValue([operation.node], textBlockName)[0]],
888
+ position,
889
+ [{ _key: targetKey }]
890
+ )
768
891
  ] : [
769
892
  patches.setIfMissing(beforeValue, []),
770
- patches.insert([fromSlateValue([operation.node], textBlockName)[0]], "before", [
771
- operation.path[0]
772
- ])
893
+ patches.insert(
894
+ [fromSlateValue([operation.node], textBlockName)[0]],
895
+ "before",
896
+ [operation.path[0]]
897
+ )
773
898
  ];
774
899
  } else if (isTextBlock && operation.path.length === 2 && editor.children[operation.path[0]]) {
775
900
  const position = block.children.length === 0 || !block.children[operation.path[1] - 1] ? "before" : "after", node = { ...operation.node };
@@ -792,7 +917,9 @@ function createOperationToPatches(types2) {
792
917
  ])
793
918
  ];
794
919
  }
795
- return debug$k("Something was inserted into a void block. Not producing editor patches."), [];
920
+ return debug$k(
921
+ "Something was inserted into a void block. Not producing editor patches."
922
+ ), [];
796
923
  }
797
924
  function splitNodePatch(editor, operation, beforeValue) {
798
925
  const patches$1 = [], splitBlock = editor.children[operation.path[0]];
@@ -809,7 +936,9 @@ function createOperationToPatches(types2) {
809
936
  [editor.children[operation.path[0] + 1]],
810
937
  textBlockName
811
938
  )[0];
812
- targetValue && (patches$1.push(patches.insert([targetValue], "after", [{ _key: splitBlock._key }])), oldBlock.children.slice(operation.position).forEach((span) => {
939
+ targetValue && (patches$1.push(
940
+ patches.insert([targetValue], "after", [{ _key: splitBlock._key }])
941
+ ), oldBlock.children.slice(operation.position).forEach((span) => {
813
942
  const path = [{ _key: oldBlock._key }, "children", { _key: span._key }];
814
943
  patches$1.push(patches.unset(path));
815
944
  }));
@@ -823,7 +952,10 @@ function createOperationToPatches(types2) {
823
952
  [
824
953
  {
825
954
  ...splitBlock,
826
- children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
955
+ children: splitBlock.children.slice(
956
+ operation.path[1] + 1,
957
+ operation.path[1] + 2
958
+ )
827
959
  }
828
960
  ],
829
961
  textBlockName
@@ -855,10 +987,14 @@ function createOperationToPatches(types2) {
855
987
  throw new Error("Block not found");
856
988
  } else if (editor.isTextBlock(block) && operation.path.length === 2) {
857
989
  const spanToRemove = block.children[operation.path[1]];
858
- return spanToRemove ? block.children.filter((span) => span._key === operation.node._key).length > 1 ? (console.warn(
990
+ return spanToRemove ? block.children.filter(
991
+ (span) => span._key === operation.node._key
992
+ ).length > 1 ? (console.warn(
859
993
  `Multiple spans have \`_key\` ${operation.node._key}. It's ambiguous which one to remove.`,
860
994
  JSON.stringify(block, null, 2)
861
- ), []) : [patches.unset([{ _key: block._key }, "children", { _key: spanToRemove._key }])] : (debug$k("Span not found in editor trying to remove node"), []);
995
+ ), []) : [
996
+ patches.unset([{ _key: block._key }, "children", { _key: spanToRemove._key }])
997
+ ] : (debug$k("Span not found in editor trying to remove node"), []);
862
998
  } else
863
999
  return debug$k("Not creating patch inside object block"), [];
864
1000
  }
@@ -866,13 +1002,18 @@ function createOperationToPatches(types2) {
866
1002
  const patches$1 = [], block = beforeValue[operation.path[0]], updatedBlock = editor.children[operation.path[0]];
867
1003
  if (operation.path.length === 1)
868
1004
  if (block?._key) {
869
- const newBlock = fromSlateValue([editor.children[operation.path[0] - 1]], textBlockName)[0];
1005
+ const newBlock = fromSlateValue(
1006
+ [editor.children[operation.path[0] - 1]],
1007
+ textBlockName
1008
+ )[0];
870
1009
  patches$1.push(patches.set(newBlock, [{ _key: newBlock._key }])), patches$1.push(patches.unset([{ _key: block._key }]));
871
1010
  } else
872
1011
  throw new Error("Target key not found!");
873
1012
  else if (editor.isTextBlock(block) && editor.isTextBlock(updatedBlock) && operation.path.length === 2) {
874
1013
  const updatedSpan = updatedBlock.children[operation.path[1] - 1] && editor.isTextSpan(updatedBlock.children[operation.path[1] - 1]) ? updatedBlock.children[operation.path[1] - 1] : void 0, removedSpan = block.children[operation.path[1]] && editor.isTextSpan(block.children[operation.path[1]]) ? block.children[operation.path[1]] : void 0;
875
- updatedSpan && (block.children.filter((span) => span._key === updatedSpan._key).length === 1 ? patches$1.push(
1014
+ updatedSpan && (block.children.filter(
1015
+ (span) => span._key === updatedSpan._key
1016
+ ).length === 1 ? patches$1.push(
876
1017
  patches.set(updatedSpan.text, [
877
1018
  { _key: block._key },
878
1019
  "children",
@@ -882,7 +1023,11 @@ function createOperationToPatches(types2) {
882
1023
  ) : console.warn(
883
1024
  `Multiple spans have \`_key\` ${updatedSpan._key}. It's ambiguous which one to update.`,
884
1025
  JSON.stringify(block, null, 2)
885
- )), removedSpan && (block.children.filter((span) => span._key === removedSpan._key).length === 1 ? patches$1.push(patches.unset([{ _key: block._key }, "children", { _key: removedSpan._key }])) : console.warn(
1026
+ )), removedSpan && (block.children.filter(
1027
+ (span) => span._key === removedSpan._key
1028
+ ).length === 1 ? patches$1.push(
1029
+ patches.unset([{ _key: block._key }, "children", { _key: removedSpan._key }])
1030
+ ) : console.warn(
886
1031
  `Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`,
887
1032
  JSON.stringify(block, null, 2)
888
1033
  ));
@@ -895,7 +1040,9 @@ function createOperationToPatches(types2) {
895
1040
  if (operation.path.length === 1) {
896
1041
  const position = operation.path[0] > operation.newPath[0] ? "before" : "after";
897
1042
  patches$1.push(patches.unset([{ _key: block._key }])), patches$1.push(
898
- patches.insert([fromSlateValue([block], textBlockName)[0]], position, [{ _key: targetBlock._key }])
1043
+ patches.insert([fromSlateValue([block], textBlockName)[0]], position, [
1044
+ { _key: targetBlock._key }
1045
+ ])
899
1046
  );
900
1047
  } else if (operation.path.length === 2 && editor.isTextBlock(block) && editor.isTextBlock(targetBlock)) {
901
1048
  const child = block.children[operation.path[1]], targetChild = targetBlock.children[operation.newPath[1]], position = operation.newPath[1] === targetBlock.children.length ? "after" : "before", childToInsert = fromSlateValue([block], textBlockName)[0].children[operation.path[1]];
@@ -957,14 +1104,24 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
957
1104
  },
958
1105
  focusBlock: () => {
959
1106
  if (editor.selection) {
960
- const block = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
1107
+ const block = slate.Node.descendant(
1108
+ editor,
1109
+ editor.selection.focus.path.slice(0, 1)
1110
+ );
961
1111
  if (block)
962
- return fromSlateValue([block], types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0];
1112
+ return fromSlateValue(
1113
+ [block],
1114
+ types$1.block.name,
1115
+ KEY_TO_VALUE_ELEMENT.get(editor)
1116
+ )[0];
963
1117
  }
964
1118
  },
965
1119
  focusChild: () => {
966
1120
  if (editor.selection) {
967
- const block = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
1121
+ const block = slate.Node.descendant(
1122
+ editor,
1123
+ editor.selection.focus.path.slice(0, 1)
1124
+ );
968
1125
  if (block && editor.isTextBlock(block))
969
1126
  return fromSlateValue(
970
1127
  [block],
@@ -985,7 +1142,9 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
985
1142
  if (!focusBlock)
986
1143
  throw new Error("No focused text block");
987
1144
  if (type.name !== types$1.span.name && !types$1.inlineObjects.some((t) => t.name === type.name))
988
- throw new Error("This type cannot be inserted as a child to a text block");
1145
+ throw new Error(
1146
+ "This type cannot be inserted as a child to a text block"
1147
+ );
989
1148
  const child = toSlateValue(
990
1149
  [
991
1150
  {
@@ -1002,11 +1161,17 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1002
1161
  ],
1003
1162
  portableTextEditor
1004
1163
  )[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2), isSpanNode = child._type === types$1.span.name, focusNode = slate.Node.get(editor, focusChildPath);
1005
- return isSpanNode && focusNode._type !== types$1.span.name && (debug$j("Inserting span child next to inline object child, moving selection + 1"), editor.move({ distance: 1, unit: "character" })), slate.Transforms.insertNodes(editor, child, {
1164
+ return isSpanNode && focusNode._type !== types$1.span.name && (debug$j(
1165
+ "Inserting span child next to inline object child, moving selection + 1"
1166
+ ), editor.move({ distance: 1, unit: "character" })), slate.Transforms.insertNodes(editor, child, {
1006
1167
  select: !0,
1007
1168
  at: editor.selection
1008
1169
  }), editor.onChange(), toPortableTextRange(
1009
- fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1170
+ fromSlateValue(
1171
+ editor.children,
1172
+ types$1.block.name,
1173
+ KEY_TO_VALUE_ELEMENT.get(editor)
1174
+ ),
1010
1175
  editor.selection,
1011
1176
  types$1
1012
1177
  )?.focus.path || [];
@@ -1031,7 +1196,11 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1031
1196
  })
1032
1197
  )[0];
1033
1198
  return slate.Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: lastBlock[1] }), editor.onChange(), toPortableTextRange(
1034
- fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1199
+ fromSlateValue(
1200
+ editor.children,
1201
+ types$1.block.name,
1202
+ KEY_TO_VALUE_ELEMENT.get(editor)
1203
+ ),
1035
1204
  editor.selection,
1036
1205
  types$1
1037
1206
  )?.focus.path ?? [];
@@ -1043,7 +1212,11 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1043
1212
  })
1044
1213
  )[0];
1045
1214
  return slate.Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: focusBlock[1] }), editor.onChange(), toPortableTextRange(
1046
- fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1215
+ fromSlateValue(
1216
+ editor.children,
1217
+ types$1.block.name,
1218
+ KEY_TO_VALUE_ELEMENT.get(editor)
1219
+ ),
1047
1220
  editor.selection,
1048
1221
  types$1
1049
1222
  )?.focus.path || [];
@@ -1069,10 +1242,16 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1069
1242
  editor
1070
1243
  );
1071
1244
  if (slatePath) {
1072
- const [block, blockPath] = slate.Editor.node(editor, slatePath.focus.path.slice(0, 1));
1245
+ const [block, blockPath] = slate.Editor.node(
1246
+ editor,
1247
+ slatePath.focus.path.slice(0, 1)
1248
+ );
1073
1249
  if (block && blockPath && typeof block._key == "string") {
1074
1250
  if (path.length === 1 && slatePath.focus.path.length === 1)
1075
- return [fromSlateValue([block], types$1.block.name)[0], [{ _key: block._key }]];
1251
+ return [
1252
+ fromSlateValue([block], types$1.block.name)[0],
1253
+ [{ _key: block._key }]
1254
+ ];
1076
1255
  const ptBlock = fromSlateValue(
1077
1256
  [block],
1078
1257
  types$1.block.name,
@@ -1081,7 +1260,10 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1081
1260
  if (editor.isTextBlock(ptBlock)) {
1082
1261
  const ptChild = ptBlock.children[slatePath.focus.path[1]];
1083
1262
  if (ptChild)
1084
- return [ptChild, [{ _key: block._key }, "children", { _key: ptChild._key }]];
1263
+ return [
1264
+ ptChild,
1265
+ [{ _key: block._key }, "children", { _key: ptChild._key }]
1266
+ ];
1085
1267
  }
1086
1268
  }
1087
1269
  }
@@ -1148,44 +1330,74 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1148
1330
  addAnnotation: (type, value) => {
1149
1331
  const { selection: originalSelection } = editor;
1150
1332
  let returnValue;
1151
- if (originalSelection) {
1152
- const [block] = slate.Editor.node(editor, originalSelection.focus, { depth: 1 });
1153
- if (!editor.isTextBlock(block))
1154
- return;
1155
- const [textNode] = slate.Editor.node(editor, originalSelection.focus, { depth: 2 });
1156
- if (!types.isPortableTextSpan(textNode) || textNode.text === "")
1157
- return;
1158
- slate.Range.isCollapsed(originalSelection) && (editor.pteExpandToWord(), editor.onChange()), editor.selection && (slate.Editor.withoutNormalizing(editor, () => {
1159
- const annotationKey = keyGenerator();
1160
- slate.Transforms.setNodes(
1161
- editor,
1162
- {
1163
- markDefs: [
1164
- ...block.markDefs || [],
1165
- { _type: type.name, _key: annotationKey, ...value }
1166
- ]
1167
- },
1168
- { at: originalSelection.focus }
1169
- ), editor.onChange(), slate.Transforms.setNodes(editor, {}, { match: slate.Text.isText, split: !0 }), editor.onChange(), editor.selection && slate.Text.isText(textNode) && slate.Transforms.setNodes(
1170
- editor,
1171
- {
1172
- marks: [...textNode.marks || [], annotationKey]
1173
- },
1174
- {
1175
- at: editor.selection,
1176
- match: (n) => n._type === types$1.span.name
1333
+ if (originalSelection && (slate.Range.isCollapsed(originalSelection) && (editor.pteExpandToWord(), editor.onChange()), editor.selection)) {
1334
+ let spanPath, markDefPath;
1335
+ const markDefPaths = [];
1336
+ slate.Editor.withoutNormalizing(editor, () => {
1337
+ if (!editor.selection)
1338
+ return;
1339
+ const selectedBlocks = slate.Editor.nodes(editor, {
1340
+ at: editor.selection,
1341
+ match: (node) => editor.isTextBlock(node),
1342
+ reverse: slate.Range.isBackward(editor.selection)
1343
+ });
1344
+ for (const [block, blockPath] of selectedBlocks) {
1345
+ if (block.children.length === 0 || block.children.length === 1 && block.children[0].text === "")
1346
+ continue;
1347
+ const annotationKey = keyGenerator(), markDefs = block.markDefs ?? [];
1348
+ markDefs.find(
1349
+ (markDef) => markDef._type === type.name && markDef._key === annotationKey
1350
+ ) === void 0 && (slate.Transforms.setNodes(
1351
+ editor,
1352
+ {
1353
+ markDefs: [
1354
+ ...markDefs,
1355
+ {
1356
+ _type: type.name,
1357
+ _key: annotationKey,
1358
+ ...value
1359
+ }
1360
+ ]
1361
+ },
1362
+ { at: blockPath }
1363
+ ), markDefPath = [
1364
+ { _key: block._key },
1365
+ "markDefs",
1366
+ { _key: annotationKey }
1367
+ ], slate.Range.isBackward(editor.selection) ? markDefPaths.unshift(markDefPath) : markDefPaths.push(markDefPath)), slate.Transforms.setNodes(
1368
+ editor,
1369
+ {},
1370
+ { match: slate.Text.isText, split: !0 }
1371
+ );
1372
+ const children = slate.Node.children(editor, blockPath);
1373
+ for (const [span, path] of children) {
1374
+ if (!editor.isTextSpan(span) || !slate.Range.includes(editor.selection, path))
1375
+ continue;
1376
+ const marks = span.marks ?? [], existingSameTypeAnnotations = marks.filter(
1377
+ (mark) => markDefs.some(
1378
+ (markDef) => markDef._key === mark && markDef._type === type.name
1379
+ )
1380
+ );
1381
+ slate.Transforms.setNodes(
1382
+ editor,
1383
+ {
1384
+ marks: [
1385
+ ...marks.filter(
1386
+ (mark) => !existingSameTypeAnnotations.includes(mark)
1387
+ ),
1388
+ annotationKey
1389
+ ]
1390
+ },
1391
+ { at: path }
1392
+ ), spanPath = [{ _key: block._key }, "children", { _key: span._key }];
1177
1393
  }
1178
- ), editor.onChange();
1179
- const newPortableTextEditorSelection = toPortableTextRange(
1180
- fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1181
- editor.selection,
1182
- types$1
1183
- );
1184
- newPortableTextEditorSelection && (returnValue = {
1185
- spanPath: newPortableTextEditorSelection.focus.path,
1186
- markDefPath: [{ _key: block._key }, "markDefs", { _key: annotationKey }]
1394
+ }
1395
+ markDefPath && spanPath && (returnValue = {
1396
+ markDefPath,
1397
+ markDefPaths,
1398
+ spanPath
1187
1399
  });
1188
- }), slate.Editor.normalize(editor), editor.onChange());
1400
+ }), editor.onChange();
1189
1401
  }
1190
1402
  return returnValue;
1191
1403
  },
@@ -1212,34 +1424,98 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1212
1424
  voids: !0,
1213
1425
  match: (node) => node._type === types$1.span.name || // Text children
1214
1426
  !editor.isTextBlock(node) && slate.Element.isElement(node)
1215
- })), editor.children.length === 0 && (editor.children = [editor.pteCreateEmptyBlock()]), editor.onChange();
1427
+ })), editor.children.length === 0 && (editor.children = [editor.pteCreateTextBlock({ decorators: [] })]), editor.onChange();
1216
1428
  }
1217
1429
  }
1218
1430
  },
1219
1431
  removeAnnotation: (type) => {
1220
- let { selection } = editor;
1221
- if (debug$j("Removing annotation", type), selection) {
1222
- if (slate.Range.isCollapsed(selection)) {
1223
- const [node, nodePath] = slate.Editor.node(editor, selection, { depth: 2 });
1224
- slate.Text.isText(node) && node.marks && typeof node.text == "string" && (slate.Transforms.select(editor, nodePath), selection = editor.selection);
1225
- }
1226
- slate.Editor.withoutNormalizing(editor, () => {
1227
- if (selection && slate.Range.isExpanded(selection)) {
1228
- if (selection = editor.selection, !selection)
1432
+ debug$j("Removing annotation", type), slate.Editor.withoutNormalizing(editor, () => {
1433
+ if (editor.selection)
1434
+ if (slate.Range.isCollapsed(editor.selection)) {
1435
+ const [block, blockPath] = slate.Editor.node(editor, editor.selection, {
1436
+ depth: 1
1437
+ });
1438
+ if (!editor.isTextBlock(block))
1229
1439
  return;
1230
- [
1231
- ...slate.Editor.nodes(editor, {
1232
- at: selection,
1233
- match: (node) => editor.isTextBlock(node) && Array.isArray(node.markDefs) && node.markDefs.some((def) => def._type === type.name)
1234
- })
1235
- ].forEach(([block]) => {
1236
- editor.isTextBlock(block) && Array.isArray(block.markDefs) && block.markDefs.filter((def) => def._type === type.name).forEach((def) => {
1237
- slate.Editor.removeMark(editor, def._key);
1238
- });
1440
+ const potentialAnnotations = (block.markDefs ?? []).filter(
1441
+ (markDef) => markDef._type === type.name
1442
+ ), [selectedChild, selectedChildPath] = slate.Editor.node(
1443
+ editor,
1444
+ editor.selection,
1445
+ {
1446
+ depth: 2
1447
+ }
1448
+ );
1449
+ if (!editor.isTextSpan(selectedChild))
1450
+ return;
1451
+ const annotationToRemove = selectedChild.marks?.find(
1452
+ (mark) => potentialAnnotations.some((markDef) => markDef._key === mark)
1453
+ );
1454
+ if (!annotationToRemove)
1455
+ return;
1456
+ const previousSpansWithSameAnnotation = [];
1457
+ for (const [child, childPath] of slate.Node.children(editor, blockPath, {
1458
+ reverse: !0
1459
+ }))
1460
+ if (editor.isTextSpan(child) && slate.Path.isBefore(childPath, selectedChildPath))
1461
+ if (child.marks?.includes(annotationToRemove))
1462
+ previousSpansWithSameAnnotation.push([child, childPath]);
1463
+ else
1464
+ break;
1465
+ const nextSpansWithSameAnnotation = [];
1466
+ for (const [child, childPath] of slate.Node.children(editor, blockPath))
1467
+ if (editor.isTextSpan(child) && slate.Path.isAfter(childPath, selectedChildPath))
1468
+ if (child.marks?.includes(annotationToRemove))
1469
+ nextSpansWithSameAnnotation.push([child, childPath]);
1470
+ else
1471
+ break;
1472
+ for (const [child, childPath] of [
1473
+ ...previousSpansWithSameAnnotation,
1474
+ [selectedChild, selectedChildPath],
1475
+ ...nextSpansWithSameAnnotation
1476
+ ])
1477
+ slate.Transforms.setNodes(
1478
+ editor,
1479
+ {
1480
+ marks: child.marks?.filter(
1481
+ (mark) => mark !== annotationToRemove
1482
+ )
1483
+ },
1484
+ { at: childPath }
1485
+ );
1486
+ } else {
1487
+ slate.Transforms.setNodes(
1488
+ editor,
1489
+ {},
1490
+ {
1491
+ match: (node) => editor.isTextSpan(node),
1492
+ split: !0,
1493
+ hanging: !0
1494
+ }
1495
+ );
1496
+ const blocks = slate.Editor.nodes(editor, {
1497
+ at: editor.selection,
1498
+ match: (node) => editor.isTextBlock(node)
1239
1499
  });
1500
+ for (const [block, blockPath] of blocks) {
1501
+ const children = slate.Node.children(editor, blockPath);
1502
+ for (const [child, childPath] of children) {
1503
+ if (!editor.isTextSpan(child) || !slate.Range.includes(editor.selection, childPath))
1504
+ continue;
1505
+ const markDefs = block.markDefs ?? [], marks = child.marks ?? [], marksWithoutAnnotation = marks.filter((mark) => markDefs.find(
1506
+ (markDef2) => markDef2._key === mark
1507
+ )?._type !== type.name);
1508
+ marksWithoutAnnotation.length !== marks.length && slate.Transforms.setNodes(
1509
+ editor,
1510
+ {
1511
+ marks: marksWithoutAnnotation
1512
+ },
1513
+ { at: childPath }
1514
+ );
1515
+ }
1516
+ }
1240
1517
  }
1241
- }), slate.Editor.normalize(editor), editor.onChange();
1242
- }
1518
+ }), editor.onChange();
1243
1519
  },
1244
1520
  getSelection: () => {
1245
1521
  let ptRange = null;
@@ -1248,14 +1524,22 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1248
1524
  if (existing)
1249
1525
  return existing;
1250
1526
  ptRange = toPortableTextRange(
1251
- fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1527
+ fromSlateValue(
1528
+ editor.children,
1529
+ types$1.block.name,
1530
+ KEY_TO_VALUE_ELEMENT.get(editor)
1531
+ ),
1252
1532
  editor.selection,
1253
1533
  types$1
1254
1534
  ), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
1255
1535
  }
1256
1536
  return ptRange;
1257
1537
  },
1258
- getValue: () => fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1538
+ getValue: () => fromSlateValue(
1539
+ editor.children,
1540
+ types$1.block.name,
1541
+ KEY_TO_VALUE_ELEMENT.get(editor)
1542
+ ),
1259
1543
  isCollapsedSelection: () => !!editor.selection && slate.Range.isCollapsed(editor.selection),
1260
1544
  isExpandedSelection: () => !!editor.selection && slate.Range.isExpanded(editor.selection),
1261
1545
  insertBreak: () => {
@@ -1269,23 +1553,94 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1269
1553
  }), editor;
1270
1554
  };
1271
1555
  }
1272
- function createWithInsertBreak(types2) {
1556
+ function createWithInsertBreak(types2, keyGenerator) {
1273
1557
  return function(editor) {
1274
1558
  const { insertBreak } = editor;
1275
1559
  return editor.insertBreak = () => {
1276
- if (editor.selection) {
1277
- const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
1278
- if (editor.isTextBlock(focusBlock)) {
1279
- const [, end] = slate.Range.edges(editor.selection), isEndAtStartOfNode = slate.Editor.isStart(editor, end, end.path), isEmptyTextBlock = focusBlock && isEqualToEmptyEditor([focusBlock], types2);
1280
- if (isEndAtStartOfNode && !isEmptyTextBlock) {
1281
- slate.Editor.insertNode(editor, editor.pteCreateEmptyBlock());
1282
- const [nextBlockPath] = slate.Path.next(focusBlockPath);
1283
- slate.Transforms.select(editor, {
1284
- anchor: { path: [nextBlockPath, 0], offset: 0 },
1285
- focus: { path: [nextBlockPath, 0], offset: 0 }
1286
- }), editor.onChange();
1287
- return;
1288
- }
1560
+ if (!editor.selection) {
1561
+ insertBreak();
1562
+ return;
1563
+ }
1564
+ const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
1565
+ if (editor.isTextBlock(focusBlock)) {
1566
+ const [start, end] = slate.Range.edges(editor.selection), isEndAtStartOfBlock = isEqual__default.default(end, {
1567
+ path: [...focusBlockPath, 0],
1568
+ offset: 0
1569
+ });
1570
+ if (isEndAtStartOfBlock && slate.Range.isCollapsed(editor.selection)) {
1571
+ const focusDecorators = editor.isTextSpan(focusBlock.children[0]) ? (focusBlock.children[0].marks ?? []).filter(
1572
+ (mark) => types2.decorators.some((decorator) => decorator.value === mark)
1573
+ ) : [];
1574
+ slate.Editor.insertNode(
1575
+ editor,
1576
+ editor.pteCreateTextBlock({ decorators: focusDecorators })
1577
+ );
1578
+ const [nextBlockPath] = slate.Path.next(focusBlockPath);
1579
+ slate.Transforms.select(editor, {
1580
+ anchor: { path: [nextBlockPath, 0], offset: 0 },
1581
+ focus: { path: [nextBlockPath, 0], offset: 0 }
1582
+ }), editor.onChange();
1583
+ return;
1584
+ }
1585
+ const lastFocusBlockChild = focusBlock.children[focusBlock.children.length - 1], isStartAtEndOfBlock = isEqual__default.default(start, {
1586
+ path: [...focusBlockPath, focusBlock.children.length - 1],
1587
+ offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
1588
+ });
1589
+ if (!isEndAtStartOfBlock && !isStartAtEndOfBlock) {
1590
+ slate.Editor.withoutNormalizing(editor, () => {
1591
+ if (!editor.selection)
1592
+ return;
1593
+ slate.Transforms.splitNodes(editor, {
1594
+ at: editor.selection
1595
+ });
1596
+ const [nextNode, nextNodePath] = slate.Editor.node(
1597
+ editor,
1598
+ slate.Path.next(focusBlockPath),
1599
+ { depth: 1 }
1600
+ );
1601
+ if (slate.Transforms.setSelection(editor, {
1602
+ anchor: { path: [...nextNodePath, 0], offset: 0 },
1603
+ focus: { path: [...nextNodePath, 0], offset: 0 }
1604
+ }), editor.isTextBlock(nextNode) && nextNode.markDefs && nextNode.markDefs.length > 0) {
1605
+ const newMarkDefKeys = /* @__PURE__ */ new Map(), prevNodeSpans = Array.from(
1606
+ slate.Node.children(editor, focusBlockPath)
1607
+ ).map((entry) => entry[0]).filter((node) => editor.isTextSpan(node)), children = slate.Node.children(editor, nextNodePath);
1608
+ for (const [child, childPath] of children) {
1609
+ if (!editor.isTextSpan(child))
1610
+ continue;
1611
+ const marks = child.marks ?? [];
1612
+ for (const mark of marks)
1613
+ types2.decorators.some(
1614
+ (decorator) => decorator.value === mark
1615
+ ) || prevNodeSpans.some(
1616
+ (prevNodeSpan) => prevNodeSpan.marks?.includes(mark)
1617
+ ) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
1618
+ const newMarks = marks.map(
1619
+ (mark) => newMarkDefKeys.get(mark) ?? mark
1620
+ );
1621
+ isEqual__default.default(marks, newMarks) || slate.Transforms.setNodes(
1622
+ editor,
1623
+ { marks: newMarks },
1624
+ {
1625
+ at: childPath
1626
+ }
1627
+ );
1628
+ }
1629
+ const newMarkDefs = nextNode.markDefs.map((markDef) => ({
1630
+ ...markDef,
1631
+ _key: newMarkDefKeys.get(markDef._key) ?? markDef._key
1632
+ }));
1633
+ isEqual__default.default(nextNode.markDefs, newMarkDefs) || slate.Transforms.setNodes(
1634
+ editor,
1635
+ { markDefs: newMarkDefs },
1636
+ {
1637
+ at: nextNodePath,
1638
+ match: (node) => editor.isTextBlock(node)
1639
+ }
1640
+ );
1641
+ }
1642
+ }), editor.onChange();
1643
+ return;
1289
1644
  }
1290
1645
  }
1291
1646
  insertBreak();
@@ -2087,9 +2442,11 @@ function createApplyPatch(schemaTypes) {
2087
2442
  let previousPatch;
2088
2443
  return function(editor, patch) {
2089
2444
  let changed = !1;
2090
- debugVerbose$4 && (debug$i(`
2445
+ debugVerbose$4 && (debug$i(
2446
+ `
2091
2447
 
2092
- NEW PATCH =============================================================`), debug$i(JSON.stringify(patch, null, 2)));
2448
+ NEW PATCH =============================================================`
2449
+ ), debug$i(JSON.stringify(patch, null, 2)));
2093
2450
  try {
2094
2451
  switch (patch.type) {
2095
2452
  case "insert":
@@ -2114,14 +2471,19 @@ NEW PATCH =============================================================`), debug
2114
2471
  };
2115
2472
  }
2116
2473
  function diffMatchPatch(editor, patch) {
2117
- const { block, child, childPath } = findBlockAndChildFromPath(editor, patch.path);
2474
+ const { block, child, childPath } = findBlockAndChildFromPath(
2475
+ editor,
2476
+ patch.path
2477
+ );
2118
2478
  if (!block)
2119
2479
  return debug$i("Block not found"), !1;
2120
2480
  if (!child || !childPath)
2121
2481
  return debug$i("Child not found"), !1;
2122
2482
  if (!(block && editor.isTextBlock(block) && patch.path.length === 4 && patch.path[1] === "children" && patch.path[3] === "text") || !slate.Text.isText(child))
2123
2483
  return !1;
2124
- const patches2 = parse(patch.value), [newValue] = apply(patches2, child.text, { allowExceedingIndices: !0 }), diff$1 = cleanupEfficiency(diff(child.text, newValue), 5);
2484
+ const patches2 = parse(patch.value), [newValue] = apply(patches2, child.text, {
2485
+ allowExceedingIndices: !0
2486
+ }), diff$1 = cleanupEfficiency(diff(child.text, newValue), 5);
2125
2487
  debugState(editor, "before");
2126
2488
  let offset = 0;
2127
2489
  for (const [op, text] of diff$1)
@@ -2155,12 +2517,17 @@ function insertPatch(editor, patch, schemaTypes) {
2155
2517
  { schemaTypes },
2156
2518
  KEY_TO_SLATE_ELEMENT.get(editor)
2157
2519
  ), targetChildIndex = targetChildPath[1], normalizedIdx = position === "after" ? targetChildIndex + 1 : targetChildIndex, childInsertPath = [targetChildPath[0], normalizedIdx];
2158
- return debug$i(`Inserting children at path ${childInsertPath}`), debugState(editor, "before"), childrenToInsert && slate.Element.isElement(childrenToInsert[0]) && slate.Transforms.insertNodes(editor, childrenToInsert[0].children, { at: childInsertPath }), debugState(editor, "after"), !0;
2520
+ return debug$i(`Inserting children at path ${childInsertPath}`), debugState(editor, "before"), childrenToInsert && slate.Element.isElement(childrenToInsert[0]) && slate.Transforms.insertNodes(editor, childrenToInsert[0].children, {
2521
+ at: childInsertPath
2522
+ }), debugState(editor, "after"), !0;
2159
2523
  }
2160
2524
  function setPatch(editor, patch) {
2161
2525
  let value = patch.value;
2162
2526
  typeof patch.path[3] == "string" && (value = {}, value[patch.path[3]] = patch.value);
2163
- const { block, blockPath, child, childPath } = findBlockAndChildFromPath(editor, patch.path);
2527
+ const { block, blockPath, child, childPath } = findBlockAndChildFromPath(
2528
+ editor,
2529
+ patch.path
2530
+ );
2164
2531
  if (!block)
2165
2532
  return debug$i("Block not found"), !1;
2166
2533
  const isTextBlock = editor.isTextBlock(block);
@@ -2221,12 +2588,15 @@ function unsetPatch(editor, patch, previousPatch) {
2221
2588
  const previousSelection = editor.selection;
2222
2589
  return slate.Transforms.deselect(editor), editor.children.forEach((c, i) => {
2223
2590
  slate.Transforms.removeNodes(editor, { at: [i] });
2224
- }), slate.Transforms.insertNodes(editor, editor.pteCreateEmptyBlock()), previousSelection && slate.Transforms.select(editor, {
2591
+ }), slate.Transforms.insertNodes(editor, editor.pteCreateTextBlock({ decorators: [] })), previousSelection && slate.Transforms.select(editor, {
2225
2592
  anchor: { path: [0, 0], offset: 0 },
2226
2593
  focus: { path: [0, 0], offset: 0 }
2227
2594
  }), editor.onChange(), debugState(editor, "after"), !0;
2228
2595
  }
2229
- const { block, blockPath, child, childPath } = findBlockAndChildFromPath(editor, patch.path);
2596
+ const { block, blockPath, child, childPath } = findBlockAndChildFromPath(
2597
+ editor,
2598
+ patch.path
2599
+ );
2230
2600
  if (patch.path.length === 1) {
2231
2601
  if (!block || !blockPath)
2232
2602
  return debug$i("Block not found"), !1;
@@ -2258,7 +2628,12 @@ function findBlockAndChildFromPath(editor, path) {
2258
2628
  const isMatch = isKeyedSegment(path[2]) ? node._key === path[2]._key : index === path[2];
2259
2629
  return isMatch && (childIndex = index), isMatch;
2260
2630
  });
2261
- return child ? { block, child, blockPath, childPath: blockPath?.concat(childIndex) } : { block, blockPath, child: void 0, childPath: void 0 };
2631
+ return child ? {
2632
+ block,
2633
+ child,
2634
+ blockPath,
2635
+ childPath: blockPath?.concat(childIndex)
2636
+ } : { block, blockPath, child: void 0, childPath: void 0 };
2262
2637
  }
2263
2638
  const PATCHING = /* @__PURE__ */ new WeakMap();
2264
2639
  function withoutPatching(editor, fn) {
@@ -2287,10 +2662,17 @@ function createWithUndoRedo(options) {
2287
2662
  patches2.forEach((patch) => {
2288
2663
  if (!reset && patch.origin !== "local" && remotePatches) {
2289
2664
  if (patch.type === "unset" && patch.path.length === 0) {
2290
- debug$h("Someone else cleared the content, resetting undo/redo history"), editor.history = { undos: [], redos: [] }, remotePatches.splice(0, remotePatches.length), SAVING.set(editor, !0), reset = !0;
2665
+ debug$h(
2666
+ "Someone else cleared the content, resetting undo/redo history"
2667
+ ), editor.history = { undos: [], redos: [] }, remotePatches.splice(0, remotePatches.length), SAVING.set(editor, !0), reset = !0;
2291
2668
  return;
2292
2669
  }
2293
- remotePatches.push({ patch, time: /* @__PURE__ */ new Date(), snapshot, previousSnapshot });
2670
+ remotePatches.push({
2671
+ patch,
2672
+ time: /* @__PURE__ */ new Date(),
2673
+ snapshot,
2674
+ previousSnapshot
2675
+ });
2294
2676
  }
2295
2677
  }), previousSnapshot = snapshot;
2296
2678
  });
@@ -2311,7 +2693,10 @@ function createWithUndoRedo(options) {
2311
2693
  step.operations.push(op);
2312
2694
  else {
2313
2695
  const newStep = {
2314
- operations: [...editor.selection === null ? [] : [createSelectOperation(editor)], op],
2696
+ operations: [
2697
+ ...editor.selection === null ? [] : [createSelectOperation(editor)],
2698
+ op
2699
+ ],
2315
2700
  timestamp: /* @__PURE__ */ new Date()
2316
2701
  };
2317
2702
  undos.push(newStep), debug$h("Created new undo step", step);
@@ -2328,12 +2713,20 @@ function createWithUndoRedo(options) {
2328
2713
  if (undos.length > 0) {
2329
2714
  const step = undos[undos.length - 1];
2330
2715
  if (debug$h("Undoing", step), step.operations.length > 0) {
2331
- const otherPatches = remotePatches.filter((item) => item.time >= step.timestamp);
2716
+ const otherPatches = remotePatches.filter(
2717
+ (item) => item.time >= step.timestamp
2718
+ );
2332
2719
  let transformedOperations = step.operations;
2333
2720
  otherPatches.forEach((item) => {
2334
2721
  transformedOperations = flatten__default.default(
2335
2722
  transformedOperations.map(
2336
- (op) => transformOperation(editor, item.patch, op, item.snapshot, item.previousSnapshot)
2723
+ (op) => transformOperation(
2724
+ editor,
2725
+ item.patch,
2726
+ op,
2727
+ item.snapshot,
2728
+ item.previousSnapshot
2729
+ )
2337
2730
  )
2338
2731
  );
2339
2732
  });
@@ -2362,12 +2755,20 @@ function createWithUndoRedo(options) {
2362
2755
  if (redos.length > 0) {
2363
2756
  const step = redos[redos.length - 1];
2364
2757
  if (debug$h("Redoing", step), step.operations.length > 0) {
2365
- const otherPatches = remotePatches.filter((item) => item.time >= step.timestamp);
2758
+ const otherPatches = remotePatches.filter(
2759
+ (item) => item.time >= step.timestamp
2760
+ );
2366
2761
  let transformedOperations = step.operations;
2367
2762
  otherPatches.forEach((item) => {
2368
2763
  transformedOperations = flatten__default.default(
2369
2764
  transformedOperations.map(
2370
- (op) => transformOperation(editor, item.patch, op, item.snapshot, item.previousSnapshot)
2765
+ (op) => transformOperation(
2766
+ editor,
2767
+ item.patch,
2768
+ op,
2769
+ item.snapshot,
2770
+ item.previousSnapshot
2771
+ )
2371
2772
  )
2372
2773
  );
2373
2774
  });
@@ -2392,7 +2793,9 @@ function createWithUndoRedo(options) {
2392
2793
  };
2393
2794
  }
2394
2795
  function transformOperation(editor, patch, operation, snapshot, previousSnapshot) {
2395
- debugVerbose$3 && (debug$h(`Adjusting '${operation.type}' operation paths for '${patch.type}' patch`), debug$h(`Operation ${JSON.stringify(operation)}`), debug$h(`Patch ${JSON.stringify(patch)}`));
2796
+ debugVerbose$3 && (debug$h(
2797
+ `Adjusting '${operation.type}' operation paths for '${patch.type}' patch`
2798
+ ), debug$h(`Operation ${JSON.stringify(operation)}`), debug$h(`Patch ${JSON.stringify(patch)}`));
2396
2799
  const transformedOperation = { ...operation };
2397
2800
  if (patch.type === "insert" && patch.path.length === 1) {
2398
2801
  const insertBlockIndex = (snapshot || []).findIndex(
@@ -2400,7 +2803,13 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
2400
2803
  );
2401
2804
  return debug$h(
2402
2805
  `Adjusting block path (+${patch.items.length}) for '${transformedOperation.type}' operation and patch '${patch.type}'`
2403
- ), [adjustBlockPath(transformedOperation, patch.items.length, insertBlockIndex)];
2806
+ ), [
2807
+ adjustBlockPath(
2808
+ transformedOperation,
2809
+ patch.items.length,
2810
+ insertBlockIndex
2811
+ )
2812
+ ];
2404
2813
  }
2405
2814
  if (patch.type === "unset" && patch.path.length === 1) {
2406
2815
  const unsetBlockIndex = (previousSnapshot || []).findIndex(
@@ -2411,9 +2820,14 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
2411
2820
  )), [adjustBlockPath(transformedOperation, -1, unsetBlockIndex)]);
2412
2821
  }
2413
2822
  if (patch.type === "unset" && patch.path.length === 0)
2414
- return debug$h(`Adjusting selection for unset everything patch and ${operation.type} operation`), [];
2823
+ return debug$h(
2824
+ `Adjusting selection for unset everything patch and ${operation.type} operation`
2825
+ ), [];
2415
2826
  if (patch.type === "diffMatchPatch") {
2416
- const operationTargetBlock = findOperationTargetBlock(editor, transformedOperation);
2827
+ const operationTargetBlock = findOperationTargetBlock(
2828
+ editor,
2829
+ transformedOperation
2830
+ );
2417
2831
  return !operationTargetBlock || !isEqual__default.default({ _key: operationTargetBlock._key }, patch.path[0]) ? [transformedOperation] : (parse(patch.value).forEach((diffPatch) => {
2418
2832
  let adjustOffsetBy = 0, changedOffset = diffPatch.utf8Start1;
2419
2833
  const { diffs } = diffPatch;
@@ -2439,7 +2853,10 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
2439
2853
  function adjustBlockPath(operation, level, blockIndex) {
2440
2854
  const transformedOperation = { ...operation };
2441
2855
  if (blockIndex >= 0 && transformedOperation.type !== "set_selection" && Array.isArray(transformedOperation.path) && transformedOperation.path[0] >= blockIndex + level && transformedOperation.path[0] + level > -1) {
2442
- const newPath = [transformedOperation.path[0] + level, ...transformedOperation.path.slice(1)];
2856
+ const newPath = [
2857
+ transformedOperation.path[0] + level,
2858
+ ...transformedOperation.path.slice(1)
2859
+ ];
2443
2860
  transformedOperation.path = newPath;
2444
2861
  }
2445
2862
  if (transformedOperation.type === "set_selection") {
@@ -2527,31 +2944,51 @@ function createWithPatches({
2527
2944
  case "insert_text":
2528
2945
  patches$1 = [
2529
2946
  ...patches$1,
2530
- ...patchFunctions.insertTextPatch(editor, operation, previousChildren)
2947
+ ...patchFunctions.insertTextPatch(
2948
+ editor,
2949
+ operation,
2950
+ previousChildren
2951
+ )
2531
2952
  ];
2532
2953
  break;
2533
2954
  case "remove_text":
2534
2955
  patches$1 = [
2535
2956
  ...patches$1,
2536
- ...patchFunctions.removeTextPatch(editor, operation, previousChildren)
2957
+ ...patchFunctions.removeTextPatch(
2958
+ editor,
2959
+ operation,
2960
+ previousChildren
2961
+ )
2537
2962
  ];
2538
2963
  break;
2539
2964
  case "remove_node":
2540
2965
  patches$1 = [
2541
2966
  ...patches$1,
2542
- ...patchFunctions.removeNodePatch(editor, operation, previousChildren)
2967
+ ...patchFunctions.removeNodePatch(
2968
+ editor,
2969
+ operation,
2970
+ previousChildren
2971
+ )
2543
2972
  ];
2544
2973
  break;
2545
2974
  case "split_node":
2546
2975
  patches$1 = [
2547
2976
  ...patches$1,
2548
- ...patchFunctions.splitNodePatch(editor, operation, previousChildren)
2977
+ ...patchFunctions.splitNodePatch(
2978
+ editor,
2979
+ operation,
2980
+ previousChildren
2981
+ )
2549
2982
  ];
2550
2983
  break;
2551
2984
  case "insert_node":
2552
2985
  patches$1 = [
2553
2986
  ...patches$1,
2554
- ...patchFunctions.insertNodePatch(editor, operation, previousChildren)
2987
+ ...patchFunctions.insertNodePatch(
2988
+ editor,
2989
+ operation,
2990
+ previousChildren
2991
+ )
2555
2992
  ];
2556
2993
  break;
2557
2994
  case "set_node":
@@ -2563,17 +3000,27 @@ function createWithPatches({
2563
3000
  case "merge_node":
2564
3001
  patches$1 = [
2565
3002
  ...patches$1,
2566
- ...patchFunctions.mergeNodePatch(editor, operation, previousChildren)
3003
+ ...patchFunctions.mergeNodePatch(
3004
+ editor,
3005
+ operation,
3006
+ previousChildren
3007
+ )
2567
3008
  ];
2568
3009
  break;
2569
3010
  case "move_node":
2570
3011
  patches$1 = [
2571
3012
  ...patches$1,
2572
- ...patchFunctions.moveNodePatch(editor, operation, previousChildren)
3013
+ ...patchFunctions.moveNodePatch(
3014
+ editor,
3015
+ operation,
3016
+ previousChildren
3017
+ )
2573
3018
  ];
2574
3019
  break;
2575
3020
  }
2576
- return !editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(operation.type) && (patches$1 = [...patches$1, patches.unset([])], change$.next({
3021
+ return !editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(
3022
+ operation.type
3023
+ ) && (patches$1 = [...patches$1, patches.unset([])], change$.next({
2577
3024
  type: "unset",
2578
3025
  previousValue: fromSlateValue(
2579
3026
  previousChildren,
@@ -2606,7 +3053,10 @@ function createWithPlaceholderBlock() {
2606
3053
  const node = op.node;
2607
3054
  if (op.path[0] === 0 && slate.Editor.isVoid(editor, node)) {
2608
3055
  const nextPath = slate.Path.next(op.path);
2609
- editor.children[nextPath[0]] || (debug$f("Adding placeholder block"), slate.Editor.insertNode(editor, editor.pteCreateEmptyBlock()));
3056
+ editor.children[nextPath[0]] || (debug$f("Adding placeholder block"), slate.Editor.insertNode(
3057
+ editor,
3058
+ editor.pteCreateTextBlock({ decorators: [] })
3059
+ ));
2610
3060
  }
2611
3061
  }
2612
3062
  apply2(op);
@@ -2625,7 +3075,11 @@ function createWithPortableTextBlockStyle(types2) {
2625
3075
  if (op.type === "split_node" && op.path.length === 1 && editor.isTextBlock(op.properties) && op.properties.style !== defaultStyle && op.path[0] === path[0] && !slate.Path.equals(path, op.path)) {
2626
3076
  const [child] = slate.Editor.node(editor, [op.path[0] + 1, 0]);
2627
3077
  if (slate.Text.isText(child) && child.text === "") {
2628
- debug$e(`Normalizing split node to ${defaultStyle} style`, op), slate.Transforms.setNodes(editor, { style: defaultStyle }, { at: [op.path[0] + 1], voids: !1 });
3078
+ debug$e(`Normalizing split node to ${defaultStyle} style`, op), slate.Transforms.setNodes(
3079
+ editor,
3080
+ { style: defaultStyle },
3081
+ { at: [op.path[0] + 1], voids: !1 }
3082
+ );
2629
3083
  break;
2630
3084
  }
2631
3085
  }
@@ -2641,9 +3095,13 @@ function createWithPortableTextBlockStyle(types2) {
2641
3095
  match: (node) => editor.isTextBlock(node)
2642
3096
  })
2643
3097
  ].forEach(([node, path]) => {
2644
- editor.isTextBlock(node) && node.style === blockStyle ? (debug$e(`Unsetting block style '${blockStyle}'`), slate.Transforms.setNodes(editor, { ...node, style: defaultStyle }, {
2645
- at: path
2646
- })) : (blockStyle ? debug$e(`Setting style '${blockStyle}'`) : debug$e("Setting default style", defaultStyle), slate.Transforms.setNodes(
3098
+ editor.isTextBlock(node) && node.style === blockStyle ? (debug$e(`Unsetting block style '${blockStyle}'`), slate.Transforms.setNodes(
3099
+ editor,
3100
+ { ...node, style: defaultStyle },
3101
+ {
3102
+ at: path
3103
+ }
3104
+ )) : (blockStyle ? debug$e(`Setting style '${blockStyle}'`) : debug$e("Setting default style", defaultStyle), slate.Transforms.setNodes(
2647
3105
  editor,
2648
3106
  {
2649
3107
  ...node,
@@ -2725,7 +3183,13 @@ function createWithPortableTextLists(types2) {
2725
3183
  return selectedBlocks.length === 0 ? !1 : (selectedBlocks.forEach(([node, path]) => {
2726
3184
  if (editor.isListBlock(node)) {
2727
3185
  let level = node.level || 1;
2728
- reverse ? (level--, debug$d("Decrementing list level", Math.min(MAX_LIST_LEVEL, Math.max(1, level)))) : (level++, debug$d("Incrementing list level", Math.min(MAX_LIST_LEVEL, Math.max(1, level)))), slate.Transforms.setNodes(
3186
+ reverse ? (level--, debug$d(
3187
+ "Decrementing list level",
3188
+ Math.min(MAX_LIST_LEVEL, Math.max(1, level))
3189
+ )) : (level++, debug$d(
3190
+ "Incrementing list level",
3191
+ Math.min(MAX_LIST_LEVEL, Math.max(1, level))
3192
+ )), slate.Transforms.setNodes(
2729
3193
  editor,
2730
3194
  { level: Math.min(MAX_LIST_LEVEL, Math.max(1, level)) },
2731
3195
  { at: path }
@@ -2767,7 +3231,11 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2767
3231
  return function(editor) {
2768
3232
  const { apply: apply2, normalizeNode } = editor, decorators = types2.decorators.map((t) => t.value), forceNewSelection = () => {
2769
3233
  editor.selection && (slate.Transforms.select(editor, { ...editor.selection }), editor.selection = { ...editor.selection });
2770
- const ptRange = toPortableTextRange(editor.children, editor.selection, types2);
3234
+ const ptRange = toPortableTextRange(
3235
+ editor.children,
3236
+ editor.selection,
3237
+ types2
3238
+ );
2771
3239
  change$.next({ type: "selection", selection: ptRange });
2772
3240
  };
2773
3241
  return editor.normalizeNode = (nodeEntry) => {
@@ -2781,17 +3249,26 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2781
3249
  "Merging spans",
2782
3250
  JSON.stringify(child, null, 2),
2783
3251
  JSON.stringify(nextNode, null, 2)
2784
- ), slate.Transforms.mergeNodes(editor, { at: [childPath[0], childPath[1] + 1], voids: !0 });
3252
+ ), slate.Transforms.mergeNodes(editor, {
3253
+ at: [childPath[0], childPath[1] + 1],
3254
+ voids: !0
3255
+ });
2785
3256
  return;
2786
3257
  }
2787
3258
  }
2788
3259
  }
3260
+ if (editor.isTextBlock(node) && !Array.isArray(node.markDefs)) {
3261
+ debug$c("Adding .markDefs to block node"), slate.Transforms.setNodes(editor, { markDefs: [] }, { at: path });
3262
+ return;
3263
+ }
2789
3264
  if (editor.isTextSpan(node) && !Array.isArray(node.marks)) {
2790
3265
  debug$c("Adding .marks to span node"), slate.Transforms.setNodes(editor, { marks: [] }, { at: path });
2791
3266
  return;
2792
3267
  }
2793
3268
  if (editor.isTextSpan(node)) {
2794
- const blockPath = slate.Path.parent(path), [block] = slate.Editor.node(editor, blockPath), decorators2 = types2.decorators.map((decorator) => decorator.value), annotations = node.marks?.filter((mark) => !decorators2.includes(mark));
3269
+ const blockPath = slate.Path.parent(path), [block] = slate.Editor.node(editor, blockPath), decorators2 = types2.decorators.map((decorator) => decorator.value), annotations = node.marks?.filter(
3270
+ (mark) => !decorators2.includes(mark)
3271
+ );
2795
3272
  if (editor.isTextBlock(block) && node.text === "" && annotations && annotations.length > 0) {
2796
3273
  debug$c("Removing annotations from empty span node"), slate.Transforms.setNodes(
2797
3274
  editor,
@@ -2809,7 +3286,11 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2809
3286
  if (orphanedAnnotations.length > 0) {
2810
3287
  debug$c("Removing orphaned annotations from span node"), slate.Transforms.setNodes(
2811
3288
  editor,
2812
- { marks: marks.filter((mark) => !orphanedAnnotations.includes(mark)) },
3289
+ {
3290
+ marks: marks.filter(
3291
+ (mark) => !orphanedAnnotations.includes(mark)
3292
+ )
3293
+ },
2813
3294
  { at: childPath }
2814
3295
  );
2815
3296
  return;
@@ -2819,17 +3300,29 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2819
3300
  if (editor.isTextSpan(node)) {
2820
3301
  const blockPath = slate.Path.parent(path), [block] = slate.Editor.node(editor, blockPath);
2821
3302
  if (editor.isTextBlock(block)) {
2822
- const decorators2 = types2.decorators.map((decorator) => decorator.value), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
3303
+ const decorators2 = types2.decorators.map(
3304
+ (decorator) => decorator.value
3305
+ ), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
2823
3306
  if (orphanedAnnotations.length > 0) {
2824
3307
  debug$c("Removing orphaned annotations from span node"), slate.Transforms.setNodes(
2825
3308
  editor,
2826
- { marks: marks.filter((mark) => !orphanedAnnotations.includes(mark)) },
3309
+ {
3310
+ marks: marks.filter(
3311
+ (mark) => !orphanedAnnotations.includes(mark)
3312
+ )
3313
+ },
2827
3314
  { at: path }
2828
3315
  );
2829
3316
  return;
2830
3317
  }
2831
3318
  }
2832
3319
  }
3320
+ if (editor.isTextBlock(node)) {
3321
+ const markDefs = node.markDefs ?? [], markDefKeys = /* @__PURE__ */ new Set(), newMarkDefs = [];
3322
+ for (const markDef of markDefs)
3323
+ markDefKeys.has(markDef._key) || (markDefKeys.add(markDef._key), newMarkDefs.push(markDef));
3324
+ markDefs.length !== newMarkDefs.length && (debug$c("Removing duplicate markDefs"), slate.Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: path }));
3325
+ }
2833
3326
  if (editor.isTextBlock(node) && !editor.operations.some(
2834
3327
  (op) => op.type === "merge_node" && "markDefs" in op.properties && op.path.length === 1
2835
3328
  )) {
@@ -2857,7 +3350,9 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2857
3350
  }
2858
3351
  if (op.type === "insert_text") {
2859
3352
  const { selection } = editor;
2860
- if (selection && slate.Range.isCollapsed(selection) && slate.Editor.marks(editor)?.marks?.some((mark) => !decorators.includes(mark))) {
3353
+ if (selection && slate.Range.isCollapsed(selection) && slate.Editor.marks(editor)?.marks?.some(
3354
+ (mark) => !decorators.includes(mark)
3355
+ )) {
2861
3356
  const [node] = Array.from(
2862
3357
  slate.Editor.nodes(editor, {
2863
3358
  mode: "lowest",
@@ -2908,7 +3403,11 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2908
3403
  ...slate.Editor.marks(editor) || {}
2909
3404
  }.marks || []).filter((mark) => decorators.includes(mark));
2910
3405
  slate.Editor.withoutNormalizing(editor, () => {
2911
- apply2(op), slate.Transforms.setNodes(editor, { marks: marksWithoutAnnotationMarks }, { at: op.path });
3406
+ apply2(op), slate.Transforms.setNodes(
3407
+ editor,
3408
+ { marks: marksWithoutAnnotationMarks },
3409
+ { at: op.path }
3410
+ );
2912
3411
  }), editor.onChange();
2913
3412
  return;
2914
3413
  }
@@ -2924,7 +3423,11 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2924
3423
  const [targetBlock, targetPath] = slate.Editor.node(editor, [op.path[0] - 1]);
2925
3424
  if (editor.isTextBlock(targetBlock)) {
2926
3425
  const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq__default.default([...oldDefs, ...op.properties.markDefs]);
2927
- debug$c("Copying markDefs over to merged block", op), slate.Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: targetPath, voids: !1 }), apply2(op);
3426
+ debug$c("Copying markDefs over to merged block", op), slate.Transforms.setNodes(
3427
+ editor,
3428
+ { markDefs: newMarkDefs },
3429
+ { at: targetPath, voids: !1 }
3430
+ ), apply2(op);
2928
3431
  return;
2929
3432
  }
2930
3433
  }
@@ -2933,8 +3436,17 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2933
3436
  if (editor.selection) {
2934
3437
  if (slate.Range.isExpanded(editor.selection))
2935
3438
  slate.Editor.withoutNormalizing(editor, () => {
2936
- slate.Transforms.setNodes(editor, {}, { match: slate.Text.isText, split: !0 });
2937
- const splitTextNodes = slate.Range.isRange(editor.selection) ? [...slate.Editor.nodes(editor, { at: editor.selection, match: slate.Text.isText })] : [];
3439
+ slate.Transforms.setNodes(
3440
+ editor,
3441
+ {},
3442
+ { match: slate.Text.isText, split: !0, hanging: !0 }
3443
+ );
3444
+ const splitTextNodes = slate.Range.isRange(editor.selection) ? [
3445
+ ...slate.Editor.nodes(editor, {
3446
+ at: editor.selection,
3447
+ match: slate.Text.isText
3448
+ })
3449
+ ] : [];
2938
3450
  splitTextNodes.length > 1 && splitTextNodes.every((node) => node[0].marks?.includes(mark)) ? editor.removeMark(mark) : splitTextNodes.forEach(([node, path]) => {
2939
3451
  const marks = [
2940
3452
  ...(Array.isArray(node.marks) ? node.marks : []).filter(
@@ -2950,13 +3462,32 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2950
3462
  });
2951
3463
  });
2952
3464
  else {
2953
- const existingMarks = {
2954
- ...slate.Editor.marks(editor) || {}
2955
- }.marks || [], marks = {
2956
- ...slate.Editor.marks(editor) || {},
2957
- marks: [...existingMarks, mark]
2958
- };
2959
- return editor.marks = marks, forceNewSelection(), editor;
3465
+ const [block, blockPath] = slate.Editor.node(editor, editor.selection, {
3466
+ depth: 1
3467
+ }), lonelyEmptySpan = editor.isTextBlock(block) && block.children.length === 1 && editor.isTextSpan(block.children[0]) && block.children[0].text === "" ? block.children[0] : void 0;
3468
+ if (lonelyEmptySpan) {
3469
+ const existingMarks = lonelyEmptySpan.marks ?? [], existingMarksWithoutDecorator = existingMarks.filter(
3470
+ (existingMark) => existingMark !== mark
3471
+ );
3472
+ slate.Transforms.setNodes(
3473
+ editor,
3474
+ {
3475
+ marks: existingMarks.length === existingMarksWithoutDecorator.length ? [...existingMarks, mark] : existingMarksWithoutDecorator
3476
+ },
3477
+ {
3478
+ at: blockPath,
3479
+ match: (node) => editor.isTextSpan(node)
3480
+ }
3481
+ );
3482
+ } else {
3483
+ const existingMarks = {
3484
+ ...slate.Editor.marks(editor) || {}
3485
+ }.marks || [], marks = {
3486
+ ...slate.Editor.marks(editor) || {},
3487
+ marks: [...existingMarks, mark]
3488
+ };
3489
+ return editor.marks = marks, forceNewSelection(), editor;
3490
+ }
2960
3491
  }
2961
3492
  editor.onChange(), forceNewSelection();
2962
3493
  }
@@ -2966,16 +3497,21 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2966
3497
  if (selection) {
2967
3498
  if (slate.Range.isExpanded(selection))
2968
3499
  slate.Editor.withoutNormalizing(editor, () => {
2969
- slate.Transforms.setNodes(editor, {}, { match: slate.Text.isText, split: !0 }), editor.selection && [
2970
- ...slate.Editor.nodes(editor, { at: editor.selection, match: slate.Text.isText })
3500
+ slate.Transforms.setNodes(
3501
+ editor,
3502
+ {},
3503
+ { match: slate.Text.isText, split: !0, hanging: !0 }
3504
+ ), editor.selection && [
3505
+ ...slate.Editor.nodes(editor, {
3506
+ at: editor.selection,
3507
+ match: slate.Text.isText
3508
+ })
2971
3509
  ].forEach(([node, path]) => {
2972
3510
  const block = editor.children[path[0]];
2973
3511
  slate.Element.isElement(block) && block.children.includes(node) && slate.Transforms.setNodes(
2974
3512
  editor,
2975
3513
  {
2976
- marks: (Array.isArray(node.marks) ? node.marks : []).filter(
2977
- (eMark) => eMark !== mark
2978
- ),
3514
+ marks: (Array.isArray(node.marks) ? node.marks : []).filter((eMark) => eMark !== mark),
2979
3515
  _type: "span"
2980
3516
  },
2981
3517
  { at: path }
@@ -2983,13 +3519,32 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2983
3519
  });
2984
3520
  }), slate.Editor.normalize(editor);
2985
3521
  else {
2986
- const existingMarks = {
2987
- ...slate.Editor.marks(editor) || {}
2988
- }.marks || [], marks = {
2989
- ...slate.Editor.marks(editor) || {},
2990
- marks: existingMarks.filter((eMark) => eMark !== mark)
2991
- };
2992
- return editor.marks = { marks: marks.marks, _type: "span" }, forceNewSelection(), editor;
3522
+ const [block, blockPath] = slate.Editor.node(editor, selection, {
3523
+ depth: 1
3524
+ }), lonelyEmptySpan = editor.isTextBlock(block) && block.children.length === 1 && editor.isTextSpan(block.children[0]) && block.children[0].text === "" ? block.children[0] : void 0;
3525
+ if (lonelyEmptySpan) {
3526
+ const existingMarksWithoutDecorator = (lonelyEmptySpan.marks ?? []).filter(
3527
+ (existingMark) => existingMark !== mark
3528
+ );
3529
+ slate.Transforms.setNodes(
3530
+ editor,
3531
+ {
3532
+ marks: existingMarksWithoutDecorator
3533
+ },
3534
+ {
3535
+ at: blockPath,
3536
+ match: (node) => editor.isTextSpan(node)
3537
+ }
3538
+ );
3539
+ } else {
3540
+ const existingMarks = {
3541
+ ...slate.Editor.marks(editor) || {}
3542
+ }.marks || [], marks = {
3543
+ ...slate.Editor.marks(editor) || {},
3544
+ marks: existingMarks.filter((eMark) => eMark !== mark)
3545
+ };
3546
+ return editor.marks = { marks: marks.marks, _type: "span" }, forceNewSelection(), editor;
3547
+ }
2993
3548
  }
2994
3549
  editor.onChange(), forceNewSelection();
2995
3550
  }
@@ -3054,7 +3609,11 @@ function createWithSchemaTypes({
3054
3609
  if (node._type === void 0 && path.length === 2) {
3055
3610
  debug$a("Setting span type on text node without a type");
3056
3611
  const span = node, key = span._key || keyGenerator();
3057
- slate.Transforms.setNodes(editor, { ...span, _type: schemaTypes.span.name, _key: key }, { at: path });
3612
+ slate.Transforms.setNodes(
3613
+ editor,
3614
+ { ...span, _type: schemaTypes.span.name, _key: key },
3615
+ { at: path }
3616
+ );
3058
3617
  }
3059
3618
  if (node._key === void 0 && (path.length === 1 || path.length === 2)) {
3060
3619
  debug$a("Setting missing key on child node without a key");
@@ -3066,7 +3625,11 @@ function createWithSchemaTypes({
3066
3625
  };
3067
3626
  }
3068
3627
  const debug$9 = debugWithName("plugin:withUtils");
3069
- function createWithUtils({ schemaTypes, keyGenerator, portableTextEditor }) {
3628
+ function createWithUtils({
3629
+ schemaTypes,
3630
+ keyGenerator,
3631
+ portableTextEditor
3632
+ }) {
3070
3633
  return function(editor) {
3071
3634
  return editor.pteExpandToWord = () => {
3072
3635
  const { selection } = editor;
@@ -3086,7 +3649,7 @@ function createWithUtils({ schemaTypes, keyGenerator, portableTextEditor }) {
3086
3649
  }
3087
3650
  debug$9("pteExpandToWord: Can't expand to word here");
3088
3651
  }
3089
- }, editor.pteCreateEmptyBlock = () => toSlateValue(
3652
+ }, editor.pteCreateTextBlock = (options) => toSlateValue(
3090
3653
  [
3091
3654
  {
3092
3655
  _type: schemaTypes.block.name,
@@ -3098,7 +3661,9 @@ function createWithUtils({ schemaTypes, keyGenerator, portableTextEditor }) {
3098
3661
  _type: "span",
3099
3662
  _key: keyGenerator(),
3100
3663
  text: "",
3101
- marks: []
3664
+ marks: options.decorators.filter(
3665
+ (decorator) => schemaTypes.decorators.find(({ value }) => value === decorator)
3666
+ )
3102
3667
  }
3103
3668
  ]
3104
3669
  }
@@ -3147,59 +3712,46 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3147
3712
  }
3148
3713
  }
3149
3714
  });
3150
- const isEnter = isHotkeyEsm.isHotkey("enter", event.nativeEvent), isTab = isHotkeyEsm.isHotkey("tab", event.nativeEvent), isShiftEnter = isHotkeyEsm.isHotkey("shift+enter", event.nativeEvent), isShiftTab = isHotkeyEsm.isHotkey("shift+tab", event.nativeEvent), isBackspace = isHotkeyEsm.isHotkey("backspace", event.nativeEvent), isDelete = isHotkeyEsm.isHotkey("delete", event.nativeEvent), isArrowDown = isHotkeyEsm.isHotkey("down", event.nativeEvent), isArrowUp = isHotkeyEsm.isHotkey("up", event.nativeEvent);
3715
+ const isEnter = isHotkeyEsm.isHotkey("enter", event.nativeEvent), isTab = isHotkeyEsm.isHotkey("tab", event.nativeEvent), isShiftEnter = isHotkeyEsm.isHotkey("shift+enter", event.nativeEvent), isShiftTab = isHotkeyEsm.isHotkey("shift+tab", event.nativeEvent), isArrowDown = isHotkeyEsm.isHotkey("down", event.nativeEvent), isArrowUp = isHotkeyEsm.isHotkey("up", event.nativeEvent);
3151
3716
  if (isArrowDown && editor.selection) {
3152
- const focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
3717
+ const focusBlock = slate.Node.descendant(
3718
+ editor,
3719
+ editor.selection.focus.path.slice(0, 1)
3720
+ );
3153
3721
  if (focusBlock && slate.Editor.isVoid(editor, focusBlock)) {
3154
3722
  const nextPath = slate.Path.next(editor.selection.focus.path.slice(0, 1));
3155
3723
  if (!slate.Node.has(editor, nextPath)) {
3156
- slate.Transforms.insertNodes(editor, editor.pteCreateEmptyBlock(), { at: nextPath }), editor.onChange();
3724
+ slate.Transforms.insertNodes(
3725
+ editor,
3726
+ editor.pteCreateTextBlock({ decorators: [] }),
3727
+ {
3728
+ at: nextPath
3729
+ }
3730
+ ), editor.onChange();
3157
3731
  return;
3158
3732
  }
3159
3733
  }
3160
3734
  }
3161
3735
  if (isArrowUp && editor.selection) {
3162
- const isFirstBlock = editor.selection.focus.path[0] === 0, focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
3163
- if (isFirstBlock && focusBlock && slate.Editor.isVoid(editor, focusBlock)) {
3164
- slate.Transforms.insertNodes(editor, editor.pteCreateEmptyBlock(), { at: [0] }), slate.Transforms.select(editor, { path: [0, 0], offset: 0 }), editor.onChange();
3165
- return;
3166
- }
3167
- }
3168
- if (isBackspace && editor.selection && editor.selection.focus.path[0] === 0 && slate.Range.isCollapsed(editor.selection)) {
3169
- const focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1)), nextPath = slate.Path.next(editor.selection.focus.path.slice(0, 1)), nextBlock = slate.Node.has(editor, nextPath), isTextBlock = types.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && focusBlock.children?.[0]?.text === "";
3170
- if (nextBlock && isTextBlock && isEmptyFocusBlock) {
3171
- event.preventDefault(), event.stopPropagation(), slate.Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), editor.onChange();
3172
- return;
3173
- }
3174
- }
3175
- if (isBackspace && editor.selection && editor.selection.focus.path[0] > 0 && slate.Range.isCollapsed(editor.selection)) {
3176
- const prevPath = slate.Path.previous(editor.selection.focus.path.slice(0, 1)), prevBlock = slate.Node.descendant(editor, prevPath), focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
3177
- if (prevBlock && focusBlock && slate.Editor.isVoid(editor, prevBlock) && editor.selection.focus.offset === 0) {
3178
- debug$8("Preventing deleting void block above"), event.preventDefault(), event.stopPropagation();
3179
- const isTextBlock = types.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && focusBlock.children?.[0]?.text === "";
3180
- if (!isTextBlock || isEmptyFocusBlock) {
3181
- slate.Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), slate.Transforms.select(editor, prevPath), editor.onChange();
3182
- return;
3183
- }
3184
- if (isTextBlock && !isEmptyFocusBlock) {
3185
- slate.Transforms.select(editor, prevPath), editor.onChange();
3186
- return;
3187
- }
3188
- return;
3189
- }
3190
- }
3191
- if (isDelete && editor.selection && editor.selection.focus.offset === 0 && slate.Range.isCollapsed(editor.selection) && editor.children[editor.selection.focus.path[0] + 1]) {
3192
- const nextBlock = slate.Node.descendant(
3736
+ const isFirstBlock = editor.selection.focus.path[0] === 0, focusBlock = slate.Node.descendant(
3193
3737
  editor,
3194
- slate.Path.next(editor.selection.focus.path.slice(0, 1))
3195
- ), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath), isEmptyFocusBlock = types.isPortableTextTextBlock(focusBlock) && focusBlock.children.length === 1 && focusBlock.children?.[0]?.text === "";
3196
- if (nextBlock && focusBlock && !slate.Editor.isVoid(editor, focusBlock) && slate.Editor.isVoid(editor, nextBlock) && isEmptyFocusBlock) {
3197
- debug$8("Preventing deleting void block below"), event.preventDefault(), event.stopPropagation(), slate.Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), slate.Transforms.select(editor, focusBlockPath), editor.onChange();
3738
+ editor.selection.focus.path.slice(0, 1)
3739
+ );
3740
+ if (isFirstBlock && focusBlock && slate.Editor.isVoid(editor, focusBlock)) {
3741
+ slate.Transforms.insertNodes(
3742
+ editor,
3743
+ editor.pteCreateTextBlock({ decorators: [] }),
3744
+ {
3745
+ at: [0]
3746
+ }
3747
+ ), slate.Transforms.select(editor, { path: [0, 0], offset: 0 }), editor.onChange();
3198
3748
  return;
3199
3749
  }
3200
3750
  }
3201
3751
  if ((isTab || isShiftTab) && editor.selection) {
3202
- const [focusChild] = slate.Editor.node(editor, editor.selection.focus, { depth: 2 }), [focusBlock] = types.isPortableTextSpan(focusChild) ? slate.Editor.node(editor, editor.selection.focus, { depth: 1 }) : [], hasAnnotationFocus = focusChild && types.isPortableTextTextBlock(focusBlock) && types.isPortableTextSpan(focusChild) && (focusChild.marks || []).filter(
3752
+ const [focusChild] = slate.Editor.node(editor, editor.selection.focus, {
3753
+ depth: 2
3754
+ }), [focusBlock] = types.isPortableTextSpan(focusChild) ? slate.Editor.node(editor, editor.selection.focus, { depth: 1 }) : [], hasAnnotationFocus = focusChild && types.isPortableTextTextBlock(focusBlock) && types.isPortableTextSpan(focusChild) && (focusChild.marks || []).filter(
3203
3755
  (m) => (focusBlock.markDefs || []).map((def) => def._key).includes(m)
3204
3756
  ).length > 0, [start] = slate.Range.edges(editor.selection), atStartOfNode = slate.Editor.isStart(editor, start, start.path);
3205
3757
  focusChild && types.isPortableTextSpan(focusChild) && (!hasAnnotationFocus || atStartOfNode) && editor.pteIncrementBlockLevels(isShiftTab) && event.preventDefault();
@@ -3213,12 +3765,15 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3213
3765
  if (editor.isTextBlock(focusBlock) && focusBlock.style && focusBlock.style !== types$1.styles[0].value) {
3214
3766
  const [, end] = slate.Range.edges(editor.selection);
3215
3767
  if (slate.Editor.isEnd(editor, end, end.path)) {
3216
- slate.Editor.insertNode(editor, editor.pteCreateEmptyBlock()), event.preventDefault(), editor.onChange();
3768
+ slate.Editor.insertNode(
3769
+ editor,
3770
+ editor.pteCreateTextBlock({ decorators: [] })
3771
+ ), event.preventDefault(), editor.onChange();
3217
3772
  return;
3218
3773
  }
3219
3774
  }
3220
3775
  if (focusBlock && slate.Editor.isVoid(editor, focusBlock)) {
3221
- slate.Editor.insertNode(editor, editor.pteCreateEmptyBlock()), event.preventDefault(), editor.onChange();
3776
+ slate.Editor.insertNode(editor, editor.pteCreateTextBlock({ decorators: [] })), event.preventDefault(), editor.onChange();
3222
3777
  return;
3223
3778
  }
3224
3779
  event.preventDefault(), editor.insertBreak(), editor.onChange();
@@ -3238,7 +3793,13 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3238
3793
  }
3239
3794
  function validateValue(value, types$1, keyGenerator) {
3240
3795
  let resolution = null, valid = !0;
3241
- const validChildTypes = [types$1.span.name, ...types$1.inlineObjects.map((t) => t.name)], validBlockTypes = [types$1.block.name, ...types$1.blockObjects.map((t) => t.name)];
3796
+ const validChildTypes = [
3797
+ types$1.span.name,
3798
+ ...types$1.inlineObjects.map((t) => t.name)
3799
+ ], validBlockTypes = [
3800
+ types$1.block.name,
3801
+ ...types$1.blockObjects.map((t) => t.name)
3802
+ ];
3242
3803
  return value === void 0 ? { valid: !0, resolution: null, value } : !Array.isArray(value) || value.length === 0 ? {
3243
3804
  valid: !1,
3244
3805
  resolution: {
@@ -3281,7 +3842,9 @@ function validateValue(value, types$1, keyGenerator) {
3281
3842
  if (blk._type === "block") {
3282
3843
  const currentBlockTypeName = types$1.block.name;
3283
3844
  return resolution = {
3284
- patches: [patches.set({ ...blk, _type: currentBlockTypeName }, [{ _key: blk._key }])],
3845
+ patches: [
3846
+ patches.set({ ...blk, _type: currentBlockTypeName }, [{ _key: blk._key }])
3847
+ ],
3285
3848
  description: `Block with _key '${blk._key}' has invalid type name '${blk._type}'. According to the schema, the block type name is '${currentBlockTypeName}'`,
3286
3849
  action: `Use type '${currentBlockTypeName}'`,
3287
3850
  item: blk,
@@ -3293,7 +3856,9 @@ function validateValue(value, types$1, keyGenerator) {
3293
3856
  }, !0;
3294
3857
  }
3295
3858
  return !blk._type && types.isPortableTextTextBlock({ ...blk, _type: types$1.block.name }) ? (resolution = {
3296
- patches: [patches.set({ ...blk, _type: types$1.block.name }, [{ _key: blk._key }])],
3859
+ patches: [
3860
+ patches.set({ ...blk, _type: types$1.block.name }, [{ _key: blk._key }])
3861
+ ],
3297
3862
  description: `Block with _key '${blk._key}' is missing a type name. According to the schema, the block type name is '${types$1.block.name}'`,
3298
3863
  action: `Use type '${types$1.block.name}'`,
3299
3864
  item: blk,
@@ -3363,7 +3928,11 @@ function validateValue(value, types$1, keyGenerator) {
3363
3928
  }
3364
3929
  if (blk.markDefs && !Array.isArray(blk.markDefs))
3365
3930
  return resolution = {
3366
- patches: [patches.set({ ...textBlock, markDefs: EMPTY_MARKDEFS }, [{ _key: textBlock._key }])],
3931
+ patches: [
3932
+ patches.set({ ...textBlock, markDefs: EMPTY_MARKDEFS }, [
3933
+ { _key: textBlock._key }
3934
+ ])
3935
+ ],
3367
3936
  description: "Block has invalid required property 'markDefs'.",
3368
3937
  action: "Add empty markDefs array",
3369
3938
  item: textBlock,
@@ -3396,7 +3965,10 @@ function validateValue(value, types$1, keyGenerator) {
3396
3965
  i18n: {
3397
3966
  description: "inputs.portable-text.invalid-value.orphaned-mark-defs.description",
3398
3967
  action: "inputs.portable-text.invalid-value.orphaned-mark-defs.action",
3399
- values: { key: blk._key, unusedMarkDefs: unusedMarkDefs.map((m) => m.toString()) }
3968
+ values: {
3969
+ key: blk._key,
3970
+ unusedMarkDefs: unusedMarkDefs.map((m) => m.toString())
3971
+ }
3400
3972
  }
3401
3973
  }, !0;
3402
3974
  }
@@ -3414,7 +3986,9 @@ function validateValue(value, types$1, keyGenerator) {
3414
3986
  return resolution = {
3415
3987
  autoResolve: !0,
3416
3988
  patches: spanChildren.map((child) => patches.set(
3417
- (child.marks || []).filter((cMrk) => !orphanedMarks.includes(cMrk)),
3989
+ (child.marks || []).filter(
3990
+ (cMrk) => !orphanedMarks.includes(cMrk)
3991
+ ),
3418
3992
  [{ _key: blk._key }, "children", { _key: child._key }, "marks"]
3419
3993
  )),
3420
3994
  description: `Block with _key '${blk._key}' contains marks (${orphaned}) not supported by the current content model.`,
@@ -3423,7 +3997,10 @@ function validateValue(value, types$1, keyGenerator) {
3423
3997
  i18n: {
3424
3998
  description: "inputs.portable-text.invalid-value.orphaned-marks.description",
3425
3999
  action: "inputs.portable-text.invalid-value.orphaned-marks.action",
3426
- values: { key: blk._key, orphanedMarks: orphanedMarks.map((m) => m.toString()) }
4000
+ values: {
4001
+ key: blk._key,
4002
+ orphanedMarks: orphanedMarks.map((m) => m.toString())
4003
+ }
3427
4004
  }
3428
4005
  }, !0;
3429
4006
  }
@@ -3445,7 +4022,9 @@ function validateValue(value, types$1, keyGenerator) {
3445
4022
  const newChild = { ...child, _key: keyGenerator() };
3446
4023
  return resolution = {
3447
4024
  autoResolve: !0,
3448
- patches: [patches.set(newChild, [{ _key: blk._key }, "children", cIndex])],
4025
+ patches: [
4026
+ patches.set(newChild, [{ _key: blk._key }, "children", cIndex])
4027
+ ],
3449
4028
  description: `Child at index ${cIndex} is missing required _key in block with _key ${blk._key}.`,
3450
4029
  action: "Set a new random _key on the object",
3451
4030
  item: blk,
@@ -3458,7 +4037,11 @@ function validateValue(value, types$1, keyGenerator) {
3458
4037
  }
3459
4038
  return child._type ? validChildTypes.includes(child._type) ? child._type === types$1.span.name && typeof child.text != "string" ? (resolution = {
3460
4039
  patches: [
3461
- patches.set({ ...child, text: "" }, [{ _key: blk._key }, "children", { _key: child._key }])
4040
+ patches.set({ ...child, text: "" }, [
4041
+ { _key: blk._key },
4042
+ "children",
4043
+ { _key: child._key }
4044
+ ])
3462
4045
  ],
3463
4046
  description: `Child with _key '${child._key}' in block with key '${blk._key}' has missing or invalid text property!`,
3464
4047
  action: "Write an empty text property to the object",
@@ -3469,17 +4052,25 @@ function validateValue(value, types$1, keyGenerator) {
3469
4052
  values: { key: blk._key, childKey: child._key }
3470
4053
  }
3471
4054
  }, !0) : !1 : (resolution = {
3472
- patches: [patches.unset([{ _key: blk._key }, "children", { _key: child._key }])],
4055
+ patches: [
4056
+ patches.unset([{ _key: blk._key }, "children", { _key: child._key }])
4057
+ ],
3473
4058
  description: `Child with _key '${child._key}' in block with key '${blk._key}' has invalid '_type' property (${child._type}).`,
3474
4059
  action: "Remove the object",
3475
4060
  item: blk,
3476
4061
  i18n: {
3477
4062
  description: "inputs.portable-text.invalid-value.disallowed-child-type.description",
3478
4063
  action: "inputs.portable-text.invalid-value.disallowed-child-type.action",
3479
- values: { key: blk._key, childKey: child._key, childType: child._type }
4064
+ values: {
4065
+ key: blk._key,
4066
+ childKey: child._key,
4067
+ childType: child._type
4068
+ }
3480
4069
  }
3481
4070
  }, !0) : (resolution = {
3482
- patches: [patches.unset([{ _key: blk._key }, "children", { _key: child._key }])],
4071
+ patches: [
4072
+ patches.unset([{ _key: blk._key }, "children", { _key: child._key }])
4073
+ ],
3483
4074
  description: `Child with _key '${child._key}' in block with key '${blk._key}' is missing '_type' property.`,
3484
4075
  action: "Remove the object",
3485
4076
  item: blk,
@@ -3513,11 +4104,13 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3513
4104
  const [voidNode] = endVoid, r = domRange.cloneRange(), domNode = slateReact.ReactEditor.toDOMNode(editor, voidNode);
3514
4105
  r.setEndAfter(domNode), contents = r.cloneContents();
3515
4106
  }
3516
- Array.from(contents.querySelectorAll("[data-slate-zero-width]")).forEach((zw) => {
3517
- const isNewline = zw.getAttribute("data-slate-zero-width") === "n";
3518
- zw.textContent = isNewline ? `
4107
+ Array.from(contents.querySelectorAll("[data-slate-zero-width]")).forEach(
4108
+ (zw) => {
4109
+ const isNewline = zw.getAttribute("data-slate-zero-width") === "n";
4110
+ zw.textContent = isNewline ? `
3519
4111
  ` : "";
3520
- }), Array.from(contents.querySelectorAll("*")).forEach((elm) => {
4112
+ }
4113
+ ), Array.from(contents.querySelectorAll("*")).forEach((elm) => {
3521
4114
  elm.removeAttribute("contentEditable"), elm.removeAttribute("data-slate-inline"), elm.removeAttribute("data-slate-leaf"), elm.removeAttribute("data-slate-node"), elm.removeAttribute("data-slate-spacer"), elm.removeAttribute("data-slate-string"), elm.removeAttribute("data-slate-zero-width"), elm.removeAttribute("draggable");
3522
4115
  for (const key in elm.attributes)
3523
4116
  elm.hasAttribute(key) && elm.removeAttribute(key);
@@ -3527,7 +4120,10 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3527
4120
  const asHTML = div.innerHTML;
3528
4121
  contents.ownerDocument.body.removeChild(div);
3529
4122
  const fragment = editor.getFragment(), portableText = fromSlateValue(fragment, blockTypeName), asJSON = JSON.stringify(portableText), asPlainText = toPlainText(portableText);
3530
- data.clearData(), data.setData("text/plain", asPlainText), data.setData("text/html", asHTML), data.setData("application/json", asJSON), data.setData("application/x-portable-text", asJSON), debug$7("text", asPlainText), data.setData("application/x-portable-text-event-origin", originEvent || "external"), debug$7("Set fragment data", asJSON, asHTML);
4123
+ data.clearData(), data.setData("text/plain", asPlainText), data.setData("text/html", asHTML), data.setData("application/json", asJSON), data.setData("application/x-portable-text", asJSON), debug$7("text", asPlainText), data.setData(
4124
+ "application/x-portable-text-event-origin",
4125
+ originEvent || "external"
4126
+ ), debug$7("Set fragment data", asJSON, asHTML);
3531
4127
  }, editor.insertPortableTextData = (data) => {
3532
4128
  if (!editor.selection)
3533
4129
  return !1;
@@ -3567,7 +4163,9 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3567
4163
  if (html) {
3568
4164
  if (portableText = blockTools.htmlToBlocks(html, schemaTypes.portableText, {
3569
4165
  unstable_whitespaceOnPasteMode: whitespaceOnPasteMode
3570
- }).map((block) => blockTools.normalizeBlock(block, { blockTypeName })), fragment = toSlateValue(portableText, { schemaTypes }), insertedType = "HTML", portableText.length === 0)
4166
+ }).map(
4167
+ (block) => blockTools.normalizeBlock(block, { blockTypeName })
4168
+ ), fragment = toSlateValue(portableText, { schemaTypes }), insertedType = "HTML", portableText.length === 0)
3571
4169
  return !1;
3572
4170
  } else {
3573
4171
  const textToHtml = `<html><body>${escapeHtml(text).split(/\n{2,}/).map(
@@ -3579,7 +4177,11 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3579
4177
  schemaTypes
3580
4178
  }), insertedType = "text";
3581
4179
  }
3582
- const validation = validateValue(portableText, schemaTypes, keyGenerator);
4180
+ const validation = validateValue(
4181
+ portableText,
4182
+ schemaTypes,
4183
+ keyGenerator
4184
+ );
3583
4185
  if (!validation.valid) {
3584
4186
  const errorDescription = `Could not validate the resulting portable text to insert.
3585
4187
  ${validation.resolution?.description}
@@ -3592,7 +4194,9 @@ Try to insert as plain text (shift-paste) instead.`;
3592
4194
  data: validation
3593
4195
  }), debug$7("Invalid insert result", validation), !1;
3594
4196
  }
3595
- return debug$7(`Inserting ${insertedType} fragment at ${JSON.stringify(editor.selection)}`), _insertFragment(editor, fragment, schemaTypes), change$.next({ type: "loading", isLoading: !1 }), !0;
4197
+ return debug$7(
4198
+ `Inserting ${insertedType} fragment at ${JSON.stringify(editor.selection)}`
4199
+ ), _insertFragment(editor, fragment, schemaTypes), change$.next({ type: "loading", isLoading: !1 }), !0;
3596
4200
  }
3597
4201
  return change$.next({ type: "loading", isLoading: !1 }), !1;
3598
4202
  }, editor.insertData = (data) => {
@@ -3657,18 +4261,30 @@ function _insertFragment(editor, fragment, schemaTypes) {
3657
4261
  editor.withoutNormalizing(() => {
3658
4262
  if (!editor.selection)
3659
4263
  return;
3660
- const [focusBlock, focusPath] = slate.Editor.node(editor, editor.selection, { depth: 1 });
4264
+ const [focusBlock, focusPath] = slate.Editor.node(editor, editor.selection, {
4265
+ depth: 1
4266
+ });
3661
4267
  if (editor.isTextBlock(focusBlock) && editor.isTextBlock(fragment[0])) {
3662
4268
  const { markDefs } = focusBlock;
3663
- debug$7("Mixing markDefs of focusBlock and fragments[0] block", markDefs, fragment[0].markDefs), isEqual__default.default(markDefs, fragment[0].markDefs) || slate.Transforms.setNodes(
4269
+ debug$7(
4270
+ "Mixing markDefs of focusBlock and fragments[0] block",
4271
+ markDefs,
4272
+ fragment[0].markDefs
4273
+ ), isEqual__default.default(markDefs, fragment[0].markDefs) || slate.Transforms.setNodes(
3664
4274
  editor,
3665
4275
  {
3666
- markDefs: uniq__default.default([...fragment[0].markDefs || [], ...markDefs || []])
4276
+ markDefs: uniq__default.default([
4277
+ ...fragment[0].markDefs || [],
4278
+ ...markDefs || []
4279
+ ])
3667
4280
  },
3668
4281
  { at: focusPath, mode: "lowest", voids: !1 }
3669
4282
  );
3670
4283
  }
3671
- isEqualToEmptyEditor(editor.children, schemaTypes) ? (slate.Transforms.splitNodes(editor, { at: [0, 0] }), editor.insertFragment(fragment), slate.Transforms.removeNodes(editor, { at: [0] })) : editor.insertFragment(fragment);
4284
+ isEqualToEmptyEditor(
4285
+ editor.children,
4286
+ schemaTypes
4287
+ ) ? (slate.Transforms.splitNodes(editor, { at: [0, 0] }), editor.insertFragment(fragment), slate.Transforms.removeNodes(editor, { at: [0] })) : editor.insertFragment(fragment);
3672
4288
  }), editor.onChange();
3673
4289
  }
3674
4290
  const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, options) => {
@@ -3678,7 +4294,11 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
3678
4294
  onChange: e.onChange,
3679
4295
  normalizeNode: e.normalizeNode
3680
4296
  });
3681
- const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(schemaTypes, keyGenerator), withSchemaTypes = createWithSchemaTypes({ schemaTypes, keyGenerator }), withEditableAPI = createWithEditableAPI(portableTextEditor, schemaTypes, keyGenerator), withPatches = createWithPatches({
4297
+ const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(schemaTypes, keyGenerator), withSchemaTypes = createWithSchemaTypes({ schemaTypes, keyGenerator }), withEditableAPI = createWithEditableAPI(
4298
+ portableTextEditor,
4299
+ schemaTypes,
4300
+ keyGenerator
4301
+ ), withPatches = createWithPatches({
3682
4302
  change$,
3683
4303
  keyGenerator,
3684
4304
  patches$,
@@ -3693,7 +4313,14 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
3693
4313
  schemaTypes,
3694
4314
  change$,
3695
4315
  keyGenerator
3696
- ), withPortableTextBlockStyle = createWithPortableTextBlockStyle(schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(schemaTypes), withUtils = createWithUtils({ keyGenerator, schemaTypes, portableTextEditor }), withPortableTextSelections = createWithPortableTextSelections(change$, schemaTypes);
4316
+ ), withPortableTextBlockStyle = createWithPortableTextBlockStyle(schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(schemaTypes, keyGenerator), withUtils = createWithUtils({
4317
+ keyGenerator,
4318
+ schemaTypes,
4319
+ portableTextEditor
4320
+ }), withPortableTextSelections = createWithPortableTextSelections(
4321
+ change$,
4322
+ schemaTypes
4323
+ );
3697
4324
  return e.destroy = () => {
3698
4325
  const originalFunctions = originalFnMap.get(e);
3699
4326
  if (!originalFunctions)
@@ -3707,7 +4334,9 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
3707
4334
  withUtils(
3708
4335
  withPlaceholderBlock(
3709
4336
  withPortableTextLists(
3710
- withPortableTextSelections(withEditableAPI(withInsertBreak(e)))
4337
+ withPortableTextSelections(
4338
+ withEditableAPI(withInsertBreak(e))
4339
+ )
3711
4340
  )
3712
4341
  )
3713
4342
  )
@@ -3726,7 +4355,11 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
3726
4355
  withUtils(
3727
4356
  withMaxBlocks(
3728
4357
  withUndoRedo(
3729
- withPatches(withPortableTextSelections(withEditableAPI(withInsertBreak(e))))
4358
+ withPatches(
4359
+ withPortableTextSelections(
4360
+ withEditableAPI(withInsertBreak(e))
4361
+ )
4362
+ )
3730
4363
  )
3731
4364
  )
3732
4365
  )
@@ -3773,8 +4406,15 @@ function SlateContainer(props) {
3773
4406
  portableTextEditor,
3774
4407
  readOnly
3775
4408
  });
3776
- }, [keyGenerator, portableTextEditor, maxBlocks, readOnly, patches$, slateEditor]);
3777
- const initialValue = react.useMemo(() => [slateEditor.pteCreateEmptyBlock()], [slateEditor]);
4409
+ }, [
4410
+ keyGenerator,
4411
+ portableTextEditor,
4412
+ maxBlocks,
4413
+ readOnly,
4414
+ patches$,
4415
+ slateEditor
4416
+ ]);
4417
+ const initialValue = react.useMemo(() => [slateEditor.pteCreateTextBlock({ decorators: [] })], [slateEditor]);
3778
4418
  return react.useEffect(() => () => {
3779
4419
  debug$6("Destroying Slate editor"), slateEditor.destroy();
3780
4420
  }, [slateEditor]), /* @__PURE__ */ jsxRuntime.jsx(slateReact.Slate, { editor: slateEditor, initialValue, children: props.children });
@@ -3831,7 +4471,11 @@ function useSyncValue(props) {
3831
4471
  slate.Transforms.removeNodes(slateEditor, {
3832
4472
  at: [childrenLength - 1 - index]
3833
4473
  });
3834
- }), slate.Transforms.insertNodes(slateEditor, slateEditor.pteCreateEmptyBlock(), { at: [0] }), hadSelection && slate.Transforms.select(slateEditor, [0, 0]);
4474
+ }), slate.Transforms.insertNodes(
4475
+ slateEditor,
4476
+ slateEditor.pteCreateTextBlock({ decorators: [] }),
4477
+ { at: [0] }
4478
+ ), hadSelection && slate.Transforms.select(slateEditor, [0, 0]);
3835
4479
  });
3836
4480
  });
3837
4481
  }), isChanged = !0), value && value.length > 0) {
@@ -3850,34 +4494,53 @@ function useSyncValue(props) {
3850
4494
  });
3851
4495
  isChanged = !0;
3852
4496
  }
3853
- slateValueFromProps.forEach((currentBlock, currentBlockIndex) => {
3854
- const oldBlock = slateEditor.children[currentBlockIndex];
3855
- if (oldBlock && !isEqual__default.default(currentBlock, oldBlock) && isValid) {
3856
- const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, schemaTypes, keyGenerator);
3857
- !validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !readOnly && previousValue.current && previousValue.current !== value && (console.warn(
3858
- `${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`
3859
- ), validation.resolution.patches.forEach((patch) => {
3860
- change$.next({ type: "patch", patch });
3861
- })), validation.valid || validation.resolution?.autoResolve ? (oldBlock._key === currentBlock._key ? (debug$5.enabled && debug$5("Updating block", oldBlock, currentBlock), _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex)) : (debug$5.enabled && debug$5("Replacing block", oldBlock, currentBlock), _replaceBlock(slateEditor, currentBlock, currentBlockIndex)), isChanged = !0) : (change$.next({
3862
- type: "invalidValue",
3863
- resolution: validation.resolution,
3864
- value
3865
- }), isValid = !1);
4497
+ slateValueFromProps.forEach(
4498
+ (currentBlock, currentBlockIndex) => {
4499
+ const oldBlock = slateEditor.children[currentBlockIndex];
4500
+ if (oldBlock && !isEqual__default.default(currentBlock, oldBlock) && isValid) {
4501
+ const validationValue = [value[currentBlockIndex]], validation = validateValue(
4502
+ validationValue,
4503
+ schemaTypes,
4504
+ keyGenerator
4505
+ );
4506
+ !validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !readOnly && previousValue.current && previousValue.current !== value && (console.warn(
4507
+ `${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`
4508
+ ), validation.resolution.patches.forEach((patch) => {
4509
+ change$.next({ type: "patch", patch });
4510
+ })), validation.valid || validation.resolution?.autoResolve ? (oldBlock._key === currentBlock._key ? (debug$5.enabled && debug$5("Updating block", oldBlock, currentBlock), _updateBlock(
4511
+ slateEditor,
4512
+ currentBlock,
4513
+ oldBlock,
4514
+ currentBlockIndex
4515
+ )) : (debug$5.enabled && debug$5("Replacing block", oldBlock, currentBlock), _replaceBlock(
4516
+ slateEditor,
4517
+ currentBlock,
4518
+ currentBlockIndex
4519
+ )), isChanged = !0) : (change$.next({
4520
+ type: "invalidValue",
4521
+ resolution: validation.resolution,
4522
+ value
4523
+ }), isValid = !1);
4524
+ }
4525
+ if (!oldBlock && isValid) {
4526
+ const validationValue = [value[currentBlockIndex]], validation = validateValue(
4527
+ validationValue,
4528
+ schemaTypes,
4529
+ keyGenerator
4530
+ );
4531
+ debug$5.enabled && debug$5(
4532
+ "Validating and inserting new block in the end of the value",
4533
+ currentBlock
4534
+ ), validation.valid || validation.resolution?.autoResolve ? slate.Transforms.insertNodes(slateEditor, currentBlock, {
4535
+ at: [currentBlockIndex]
4536
+ }) : (debug$5("Invalid", validation), change$.next({
4537
+ type: "invalidValue",
4538
+ resolution: validation.resolution,
4539
+ value
4540
+ }), isValid = !1);
4541
+ }
3866
4542
  }
3867
- if (!oldBlock && isValid) {
3868
- const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, schemaTypes, keyGenerator);
3869
- debug$5.enabled && debug$5(
3870
- "Validating and inserting new block in the end of the value",
3871
- currentBlock
3872
- ), validation.valid || validation.resolution?.autoResolve ? slate.Transforms.insertNodes(slateEditor, currentBlock, {
3873
- at: [currentBlockIndex]
3874
- }) : (debug$5("Invalid", validation), change$.next({
3875
- type: "invalidValue",
3876
- resolution: validation.resolution,
3877
- value
3878
- }), isValid = !1);
3879
- }
3880
- });
4543
+ );
3881
4544
  });
3882
4545
  });
3883
4546
  });
@@ -3927,41 +4590,53 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3927
4590
  at: [currentBlockIndex]
3928
4591
  }), slateEditor.isTextBlock(currentBlock) && slateEditor.isTextBlock(oldBlock)) {
3929
4592
  const oldBlockChildrenLength = oldBlock.children.length;
3930
- currentBlock.children.length < oldBlockChildrenLength && Array.from(Array(oldBlockChildrenLength - currentBlock.children.length)).forEach(
3931
- (_, index) => {
3932
- const childIndex = oldBlockChildrenLength - 1 - index;
3933
- childIndex > 0 && (debug$5("Removing child"), slate.Transforms.removeNodes(slateEditor, {
3934
- at: [currentBlockIndex, childIndex]
3935
- }));
4593
+ currentBlock.children.length < oldBlockChildrenLength && Array.from(
4594
+ Array(oldBlockChildrenLength - currentBlock.children.length)
4595
+ ).forEach((_, index) => {
4596
+ const childIndex = oldBlockChildrenLength - 1 - index;
4597
+ childIndex > 0 && (debug$5("Removing child"), slate.Transforms.removeNodes(slateEditor, {
4598
+ at: [currentBlockIndex, childIndex]
4599
+ }));
4600
+ }), currentBlock.children.forEach(
4601
+ (currentBlockChild, currentBlockChildIndex) => {
4602
+ const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual__default.default(currentBlockChild, oldBlockChild), isTextChanged = !isEqual__default.default(
4603
+ currentBlockChild.text,
4604
+ oldBlockChild?.text
4605
+ ), path = [currentBlockIndex, currentBlockChildIndex];
4606
+ if (isChildChanged)
4607
+ if (currentBlockChild._key === oldBlockChild?._key) {
4608
+ debug$5("Updating changed child", currentBlockChild, oldBlockChild), slate.Transforms.setNodes(
4609
+ slateEditor,
4610
+ currentBlockChild,
4611
+ {
4612
+ at: path
4613
+ }
4614
+ );
4615
+ const isSpanNode = slate.Text.isText(currentBlockChild) && currentBlockChild._type === "span" && slate.Text.isText(oldBlockChild) && oldBlockChild._type === "span";
4616
+ isSpanNode && isTextChanged ? (slate.Transforms.delete(slateEditor, {
4617
+ at: {
4618
+ focus: { path, offset: 0 },
4619
+ anchor: { path, offset: oldBlockChild.text.length }
4620
+ }
4621
+ }), slate.Transforms.insertText(slateEditor, currentBlockChild.text, {
4622
+ at: path
4623
+ }), slateEditor.onChange()) : isSpanNode || (debug$5("Updating changed inline object child", currentBlockChild), slate.Transforms.setNodes(
4624
+ slateEditor,
4625
+ { _key: VOID_CHILD_KEY },
4626
+ {
4627
+ at: [...path, 0],
4628
+ voids: !0
4629
+ }
4630
+ ));
4631
+ } else oldBlockChild ? (debug$5("Replacing child", currentBlockChild), slate.Transforms.removeNodes(slateEditor, {
4632
+ at: [currentBlockIndex, currentBlockChildIndex]
4633
+ }), slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
4634
+ at: [currentBlockIndex, currentBlockChildIndex]
4635
+ }), slateEditor.onChange()) : oldBlockChild || (debug$5("Inserting new child", currentBlockChild), slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
4636
+ at: [currentBlockIndex, currentBlockChildIndex]
4637
+ }), slateEditor.onChange());
3936
4638
  }
3937
- ), currentBlock.children.forEach((currentBlockChild, currentBlockChildIndex) => {
3938
- const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual__default.default(currentBlockChild, oldBlockChild), isTextChanged = !isEqual__default.default(currentBlockChild.text, oldBlockChild?.text), path = [currentBlockIndex, currentBlockChildIndex];
3939
- if (isChildChanged)
3940
- if (currentBlockChild._key === oldBlockChild?._key) {
3941
- debug$5("Updating changed child", currentBlockChild, oldBlockChild), slate.Transforms.setNodes(slateEditor, currentBlockChild, {
3942
- at: path
3943
- });
3944
- const isSpanNode = slate.Text.isText(currentBlockChild) && currentBlockChild._type === "span" && slate.Text.isText(oldBlockChild) && oldBlockChild._type === "span";
3945
- isSpanNode && isTextChanged ? (slate.Transforms.delete(slateEditor, {
3946
- at: { focus: { path, offset: 0 }, anchor: { path, offset: oldBlockChild.text.length } }
3947
- }), slate.Transforms.insertText(slateEditor, currentBlockChild.text, {
3948
- at: path
3949
- }), slateEditor.onChange()) : isSpanNode || (debug$5("Updating changed inline object child", currentBlockChild), slate.Transforms.setNodes(
3950
- slateEditor,
3951
- { _key: VOID_CHILD_KEY },
3952
- {
3953
- at: [...path, 0],
3954
- voids: !0
3955
- }
3956
- ));
3957
- } else oldBlockChild ? (debug$5("Replacing child", currentBlockChild), slate.Transforms.removeNodes(slateEditor, {
3958
- at: [currentBlockIndex, currentBlockChildIndex]
3959
- }), slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
3960
- at: [currentBlockIndex, currentBlockChildIndex]
3961
- }), slateEditor.onChange()) : oldBlockChild || (debug$5("Inserting new child", currentBlockChild), slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
3962
- at: [currentBlockIndex, currentBlockChildIndex]
3963
- }), slateEditor.onChange());
3964
- });
4639
+ );
3965
4640
  }
3966
4641
  }
3967
4642
  const debug$4 = debugWithName("component:PortableTextEditor:Synchronizer"), debugVerbose$1 = debug$4.enabled && !1, FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3;
@@ -3980,7 +4655,11 @@ function Synchronizer(props) {
3980
4655
  debug$4("Flushing pending patches"), debugVerbose$1 && debug$4(`Patches:
3981
4656
  ${JSON.stringify(pendingPatches.current, null, 2)}`);
3982
4657
  const snapshot = getValue();
3983
- change$.next({ type: "mutation", patches: pendingPatches.current, snapshot }), pendingPatches.current = [];
4658
+ change$.next({
4659
+ type: "mutation",
4660
+ patches: pendingPatches.current,
4661
+ snapshot
4662
+ }), pendingPatches.current = [];
3984
4663
  }
3985
4664
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !1);
3986
4665
  }, [slateEditor, getValue, change$]), onFlushPendingPatchesThrottled = react.useMemo(() => throttle__default.default(
@@ -4066,7 +4745,9 @@ class PortableTextEditor extends react.Component {
4066
4745
  constructor(props) {
4067
4746
  if (super(props), !props.schemaType)
4068
4747
  throw new Error('PortableTextEditor: missing "schemaType" property');
4069
- props.incomingPatches$ && console.warn("The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"), this.change$.next({ type: "loading", isLoading: !0 }), this.schemaTypes = getPortableTextMemberSchemaTypes(
4748
+ props.incomingPatches$ && console.warn(
4749
+ "The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"
4750
+ ), this.change$.next({ type: "loading", isLoading: !0 }), this.schemaTypes = getPortableTextMemberSchemaTypes(
4070
4751
  props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)
4071
4752
  );
4072
4753
  }
@@ -4158,14 +4839,26 @@ class PortableTextEditor extends react.Component {
4158
4839
  static isSelectionsOverlapping = (editor, selectionA, selectionB) => editor.editable?.isSelectionsOverlapping(selectionA, selectionB);
4159
4840
  }
4160
4841
  const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (props) => {
4161
- const { attributes, children, leaf, schemaTypes, renderChild, renderDecorator, renderAnnotation } = props, spanRef = react.useRef(null), portableTextEditor = usePortableTextEditor(), blockSelected = slateReact.useSelected(), [focused, setFocused] = react.useState(!1), [selected, setSelected] = react.useState(!1), block = children.props.parent, path = react.useMemo(
4842
+ const {
4843
+ attributes,
4844
+ children,
4845
+ leaf,
4846
+ schemaTypes,
4847
+ renderChild,
4848
+ renderDecorator,
4849
+ renderAnnotation
4850
+ } = props, spanRef = react.useRef(null), portableTextEditor = usePortableTextEditor(), blockSelected = slateReact.useSelected(), [focused, setFocused] = react.useState(!1), [selected, setSelected] = react.useState(!1), block = children.props.parent, path = react.useMemo(
4162
4851
  () => block ? [{ _key: block?._key }, "children", { _key: leaf._key }] : [],
4163
4852
  [block, leaf._key]
4164
4853
  ), decoratorValues = react.useMemo(
4165
4854
  () => schemaTypes.decorators.map((dec) => dec.value),
4166
4855
  [schemaTypes.decorators]
4167
4856
  ), marks = react.useMemo(
4168
- () => uniq__default.default((leaf.marks || EMPTY_MARKS).filter((mark) => decoratorValues.includes(mark))),
4857
+ () => uniq__default.default(
4858
+ (leaf.marks || EMPTY_MARKS).filter(
4859
+ (mark) => decoratorValues.includes(mark)
4860
+ )
4861
+ ),
4169
4862
  [decoratorValues, leaf.marks]
4170
4863
  ), annotationMarks = Array.isArray(leaf.marks) ? leaf.marks : EMPTY_MARKS, annotations = react.useMemo(
4171
4864
  () => annotationMarks.map(
@@ -4216,11 +4909,18 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4216
4909
  return () => {
4217
4910
  sub.unsubscribe();
4218
4911
  };
4219
- }, [path, portableTextEditor, setSelectedFromRange, shouldTrackSelectionAndFocus]), react.useEffect(() => setSelectedFromRange(), [setSelectedFromRange]);
4912
+ }, [
4913
+ path,
4914
+ portableTextEditor,
4915
+ setSelectedFromRange,
4916
+ shouldTrackSelectionAndFocus
4917
+ ]), react.useEffect(() => setSelectedFromRange(), [setSelectedFromRange]);
4220
4918
  const content2 = react.useMemo(() => {
4221
4919
  let returnedChildren = children;
4222
4920
  if (slate.Text.isText(leaf) && leaf._type === schemaTypes.span.name && (marks.forEach((mark) => {
4223
- const schemaType = schemaTypes.decorators.find((dec) => dec.value === mark);
4921
+ const schemaType = schemaTypes.decorators.find(
4922
+ (dec) => dec.value === mark
4923
+ );
4224
4924
  if (schemaType && renderDecorator) {
4225
4925
  const _props = Object.defineProperty(
4226
4926
  {
@@ -4236,14 +4936,20 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4236
4936
  {
4237
4937
  enumerable: !1,
4238
4938
  get() {
4239
- return console.warn("Property 'type' is deprecated, use 'schemaType' instead."), schemaType;
4939
+ return console.warn(
4940
+ "Property 'type' is deprecated, use 'schemaType' instead."
4941
+ ), schemaType;
4240
4942
  }
4241
4943
  }
4242
4944
  );
4243
- returnedChildren = renderDecorator(_props);
4945
+ returnedChildren = renderDecorator(
4946
+ _props
4947
+ );
4244
4948
  }
4245
4949
  }), block && annotations.length > 0 && annotations.forEach((annotation) => {
4246
- const schemaType = schemaTypes.annotations.find((t) => t.name === annotation._type);
4950
+ const schemaType = schemaTypes.annotations.find(
4951
+ (t) => t.name === annotation._type
4952
+ );
4247
4953
  if (schemaType)
4248
4954
  if (renderAnnotation) {
4249
4955
  const _props = Object.defineProperty(
@@ -4261,7 +4967,9 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4261
4967
  {
4262
4968
  enumerable: !1,
4263
4969
  get() {
4264
- return console.warn("Property 'type' is deprecated, use 'schemaType' instead."), schemaType;
4970
+ return console.warn(
4971
+ "Property 'type' is deprecated, use 'schemaType' instead."
4972
+ ), schemaType;
4265
4973
  }
4266
4974
  }
4267
4975
  );
@@ -4286,7 +4994,9 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4286
4994
  {
4287
4995
  enumerable: !1,
4288
4996
  get() {
4289
- return console.warn("Property 'type' is deprecated, use 'schemaType' instead."), schemaTypes.span;
4997
+ return console.warn(
4998
+ "Property 'type' is deprecated, use 'schemaType' instead."
4999
+ ), schemaTypes.span;
4290
5000
  }
4291
5001
  }
4292
5002
  );
@@ -4341,8 +5051,13 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4341
5051
  scrollSelectionIntoView,
4342
5052
  spellCheck,
4343
5053
  ...restProps
4344
- } = props, portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), keyGenerator = usePortableTextEditorKeyGenerator(), ref = react.useRef(null), [editableElement, setEditableElement] = react.useState(null), [hasInvalidValue, setHasInvalidValue] = react.useState(!1), [rangeDecorationState, setRangeDecorationsState] = react.useState([]);
4345
- react.useImperativeHandle(forwardedRef, () => ref.current);
5054
+ } = props, portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), keyGenerator = usePortableTextEditorKeyGenerator(), ref = react.useRef(null), [editableElement, setEditableElement] = react.useState(
5055
+ null
5056
+ ), [hasInvalidValue, setHasInvalidValue] = react.useState(!1), [rangeDecorationState, setRangeDecorationsState] = react.useState([]);
5057
+ react.useImperativeHandle(
5058
+ forwardedRef,
5059
+ () => ref.current
5060
+ );
4346
5061
  const rangeDecorationsRef = react.useRef(rangeDecorations), { change$, schemaTypes } = portableTextEditor, slateEditor = slateReact.useSlate(), blockTypeName = schemaTypes.block.name, withInsertData = react.useMemo(
4347
5062
  () => createWithInsertData(change$, schemaTypes, keyGenerator),
4348
5063
  [change$, keyGenerator, schemaTypes]
@@ -4365,7 +5080,15 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4365
5080
  spellCheck
4366
5081
  }
4367
5082
  ),
4368
- [schemaTypes, spellCheck, readOnly, renderBlock, renderChild, renderListItem, renderStyle]
5083
+ [
5084
+ schemaTypes,
5085
+ spellCheck,
5086
+ readOnly,
5087
+ renderBlock,
5088
+ renderChild,
5089
+ renderListItem,
5090
+ renderStyle
5091
+ ]
4369
5092
  ), renderLeaf = react.useCallback(
4370
5093
  (lProps) => {
4371
5094
  if (lProps.leaf._type === "span") {
@@ -4390,7 +5113,14 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4390
5113
  }
4391
5114
  return lProps.children;
4392
5115
  },
4393
- [readOnly, renderAnnotation, renderChild, renderDecorator, renderPlaceholder, schemaTypes]
5116
+ [
5117
+ readOnly,
5118
+ renderAnnotation,
5119
+ renderChild,
5120
+ renderDecorator,
5121
+ renderPlaceholder,
5122
+ schemaTypes
5123
+ ]
4394
5124
  ), restoreSelectionFromProps = react.useCallback(() => {
4395
5125
  if (propsSelection) {
4396
5126
  debug(`Selection from props ${JSON.stringify(propsSelection)}`);
@@ -4399,7 +5129,9 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4399
5129
  fromSlateValue(slateEditor.children, blockTypeName)
4400
5130
  );
4401
5131
  if (normalizedSelection !== null) {
4402
- debug(`Normalized selection from props ${JSON.stringify(normalizedSelection)}`);
5132
+ debug(
5133
+ `Normalized selection from props ${JSON.stringify(normalizedSelection)}`
5134
+ );
4403
5135
  const slateRange = toSlateRange(normalizedSelection, slateEditor);
4404
5136
  slateRange && (slate.Transforms.select(slateEditor, slateRange), slateEditor.operations.some((o) => o.type === "set_selection") || change$.next({ type: "selection", selection: normalizedSelection }), slateEditor.onChange());
4405
5137
  }
@@ -4409,7 +5141,10 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4409
5141
  if (rangeDecorations && rangeDecorations.length > 0) {
4410
5142
  const newSlateRanges = [];
4411
5143
  if (rangeDecorations.forEach((rangeDecorationItem) => {
4412
- const slateRange = toSlateRange(rangeDecorationItem.selection, slateEditor);
5144
+ const slateRange = toSlateRange(
5145
+ rangeDecorationItem.selection,
5146
+ slateEditor
5147
+ );
4413
5148
  if (!slate.Range.isRange(slateRange)) {
4414
5149
  rangeDecorationItem.onMoved && rangeDecorationItem.onMoved({
4415
5150
  newSelection: null,
@@ -4420,14 +5155,21 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4420
5155
  }
4421
5156
  let newRange;
4422
5157
  if (operation && (newRange = moveRangeByOperation(slateRange, operation), newRange && newRange !== slateRange || newRange === null && slateRange)) {
4423
- const value = PortableTextEditor.getValue(portableTextEditor), newRangeSelection = toPortableTextRange(value, newRange, schemaTypes);
5158
+ const value = PortableTextEditor.getValue(portableTextEditor), newRangeSelection = toPortableTextRange(
5159
+ value,
5160
+ newRange,
5161
+ schemaTypes
5162
+ );
4424
5163
  rangeDecorationItem.onMoved && rangeDecorationItem.onMoved({
4425
5164
  newSelection: newRangeSelection,
4426
5165
  rangeDecoration: rangeDecorationItem,
4427
5166
  origin: "local"
4428
5167
  });
4429
5168
  }
4430
- newRange !== null && newSlateRanges.push({ ...newRange || slateRange, rangeDecoration: rangeDecorationItem });
5169
+ newRange !== null && newSlateRanges.push({
5170
+ ...newRange || slateRange,
5171
+ rangeDecoration: rangeDecorationItem
5172
+ });
4431
5173
  }), newSlateRanges.length > 0) {
4432
5174
  setRangeDecorationsState(newSlateRanges);
4433
5175
  return;
@@ -4480,11 +5222,20 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4480
5222
  debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
4481
5223
  return;
4482
5224
  }
4483
- const value = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(value, slateEditor.selection, schemaTypes)?.focus.path || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
5225
+ const value = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(
5226
+ value,
5227
+ slateEditor.selection,
5228
+ schemaTypes
5229
+ )?.focus.path || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
4484
5230
  onPasteResult === void 0 ? (debug("No result from custom paste handler, pasting normally"), slateEditor.insertData(event.clipboardData)) : (change$.next({ type: "loading", isLoading: !0 }), Promise.resolve(onPasteResult).then((result) => {
4485
5231
  debug("Custom paste function from client resolved", result), !result || !result.insert ? (debug("No result from custom paste handler, pasting normally"), slateEditor.insertData(event.clipboardData)) : result.insert ? slateEditor.insertFragment(
4486
- toSlateValue(result.insert, { schemaTypes })
4487
- ) : console.warn("Your onPaste function returned something unexpected:", result);
5232
+ toSlateValue(result.insert, {
5233
+ schemaTypes
5234
+ })
5235
+ ) : console.warn(
5236
+ "Your onPaste function returned something unexpected:",
5237
+ result
5238
+ );
4488
5239
  }).catch((error) => (console.error(error), error)).finally(() => {
4489
5240
  change$.next({ type: "loading", isLoading: !1 });
4490
5241
  }));
@@ -4509,7 +5260,10 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4509
5260
  const [lastBlock, path] = slate.Node.last(slateEditor, []), focusPath = slateEditor.selection.focus.path.slice(0, 1), lastPath = path.slice(0, 1);
4510
5261
  if (slate.Path.equals(focusPath, lastPath)) {
4511
5262
  const node = slate.Node.descendant(slateEditor, path.slice(0, 1));
4512
- lastBlock && slate.Editor.isVoid(slateEditor, node) && (slate.Transforms.insertNodes(slateEditor, slateEditor.pteCreateEmptyBlock()), slateEditor.onChange());
5263
+ lastBlock && slate.Editor.isVoid(slateEditor, node) && (slate.Transforms.insertNodes(
5264
+ slateEditor,
5265
+ slateEditor.pteCreateTextBlock({ decorators: [] })
5266
+ ), slateEditor.onChange());
4513
5267
  }
4514
5268
  }
4515
5269
  },
@@ -4535,7 +5289,10 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4535
5289
  return;
4536
5290
  const existingDOMRange = domSelection.getRangeAt(0);
4537
5291
  try {
4538
- const newDOMRange = slateReact.ReactEditor.toDOMRange(slateEditor, slateEditor.selection);
5292
+ const newDOMRange = slateReact.ReactEditor.toDOMRange(
5293
+ slateEditor,
5294
+ slateEditor.selection
5295
+ );
4539
5296
  (newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
4540
5297
  } catch {
4541
5298
  debug("Could not resolve selection, selecting top document"), slate.Transforms.deselect(slateEditor), slateEditor.children.length > 0 && slate.Transforms.select(slateEditor, [0, 0]), slateEditor.onChange();
@@ -4583,13 +5340,19 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4583
5340
  ];
4584
5341
  if (path.length === 0)
4585
5342
  return [];
4586
- const result = rangeDecorationState.filter((item) => slate.Range.isCollapsed(item) ? path.length !== 2 ? !1 : slate.Path.equals(item.focus.path, path) && slate.Path.equals(item.anchor.path, path) : slate.Range.intersection(item, { anchor: { path, offset: 0 }, focus: { path, offset: 0 } }) || slate.Range.includes(item, path));
5343
+ const result = rangeDecorationState.filter((item) => slate.Range.isCollapsed(item) ? path.length !== 2 ? !1 : slate.Path.equals(item.focus.path, path) && slate.Path.equals(item.anchor.path, path) : slate.Range.intersection(item, {
5344
+ anchor: { path, offset: 0 },
5345
+ focus: { path, offset: 0 }
5346
+ }) || slate.Range.includes(item, path));
4587
5347
  return result.length > 0 ? result : [];
4588
5348
  },
4589
5349
  [slateEditor, schemaTypes, rangeDecorationState]
4590
5350
  );
4591
5351
  return react.useEffect(() => {
4592
- ref.current = slateReact.ReactEditor.toDOMNode(slateEditor, slateEditor), setEditableElement(ref.current);
5352
+ ref.current = slateReact.ReactEditor.toDOMNode(
5353
+ slateEditor,
5354
+ slateEditor
5355
+ ), setEditableElement(ref.current);
4593
5356
  }, [slateEditor, ref]), portableTextEditor ? hasInvalidValue ? null : /* @__PURE__ */ jsxRuntime.jsx(
4594
5357
  slateReact.Editable,
4595
5358
  {