@portabletext/editor 1.0.19 → 1.1.1

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 (76) hide show
  1. package/lib/index.d.mts +142 -67
  2. package/lib/index.d.ts +142 -67
  3. package/lib/index.esm.js +1130 -371
  4. package/lib/index.esm.js.map +1 -1
  5. package/lib/index.js +1130 -371
  6. package/lib/index.js.map +1 -1
  7. package/lib/index.mjs +1130 -371
  8. package/lib/index.mjs.map +1 -1
  9. package/package.json +4 -18
  10. package/src/editor/Editable.tsx +128 -55
  11. package/src/editor/PortableTextEditor.tsx +66 -32
  12. package/src/editor/__tests__/PortableTextEditor.test.tsx +44 -18
  13. package/src/editor/__tests__/PortableTextEditorTester.tsx +50 -38
  14. package/src/editor/__tests__/RangeDecorations.test.tsx +4 -6
  15. package/src/editor/__tests__/handleClick.test.tsx +28 -9
  16. package/src/editor/__tests__/insert-block.test.tsx +24 -8
  17. package/src/editor/__tests__/pteWarningsSelfSolving.test.tsx +31 -63
  18. package/src/editor/__tests__/utils.ts +10 -4
  19. package/src/editor/components/DraggableBlock.tsx +36 -13
  20. package/src/editor/components/Element.tsx +73 -33
  21. package/src/editor/components/Leaf.tsx +114 -76
  22. package/src/editor/components/SlateContainer.tsx +14 -7
  23. package/src/editor/components/Synchronizer.tsx +8 -5
  24. package/src/editor/hooks/usePortableTextEditor.ts +3 -3
  25. package/src/editor/hooks/usePortableTextEditorSelection.tsx +10 -4
  26. package/src/editor/hooks/useSyncValue.test.tsx +9 -4
  27. package/src/editor/hooks/useSyncValue.ts +198 -133
  28. package/src/editor/nodes/DefaultAnnotation.tsx +6 -4
  29. package/src/editor/nodes/DefaultObject.tsx +1 -1
  30. package/src/editor/plugins/__tests__/createWithInsertData.test.tsx +23 -8
  31. package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +26 -9
  32. package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +15 -5
  33. package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +60 -19
  34. package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +5 -3
  35. package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +4 -2
  36. package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +61 -19
  37. package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +6 -3
  38. package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +30 -13
  39. package/src/editor/plugins/createWithEditableAPI.ts +361 -131
  40. package/src/editor/plugins/createWithHotKeys.ts +46 -130
  41. package/src/editor/plugins/createWithInsertBreak.ts +167 -28
  42. package/src/editor/plugins/createWithInsertData.ts +66 -30
  43. package/src/editor/plugins/createWithMaxBlocks.ts +6 -3
  44. package/src/editor/plugins/createWithObjectKeys.ts +7 -3
  45. package/src/editor/plugins/createWithPatches.ts +66 -24
  46. package/src/editor/plugins/createWithPlaceholderBlock.ts +9 -5
  47. package/src/editor/plugins/createWithPortableTextBlockStyle.ts +17 -7
  48. package/src/editor/plugins/createWithPortableTextLists.ts +21 -9
  49. package/src/editor/plugins/createWithPortableTextMarkModel.ts +217 -52
  50. package/src/editor/plugins/createWithPortableTextSelections.ts +11 -9
  51. package/src/editor/plugins/createWithSchemaTypes.ts +26 -10
  52. package/src/editor/plugins/createWithUndoRedo.ts +106 -27
  53. package/src/editor/plugins/createWithUtils.ts +33 -11
  54. package/src/editor/plugins/index.ts +34 -13
  55. package/src/types/editor.ts +73 -44
  56. package/src/types/options.ts +7 -5
  57. package/src/types/slate.ts +6 -6
  58. package/src/utils/__tests__/dmpToOperations.test.ts +41 -16
  59. package/src/utils/__tests__/operationToPatches.test.ts +4 -3
  60. package/src/utils/__tests__/patchToOperations.test.ts +16 -5
  61. package/src/utils/__tests__/ranges.test.ts +9 -4
  62. package/src/utils/__tests__/valueNormalization.test.tsx +12 -4
  63. package/src/utils/__tests__/values.test.ts +0 -1
  64. package/src/utils/applyPatch.ts +78 -29
  65. package/src/utils/getPortableTextMemberSchemaTypes.ts +38 -23
  66. package/src/utils/operationToPatches.ts +123 -44
  67. package/src/utils/paths.ts +26 -9
  68. package/src/utils/ranges.ts +16 -10
  69. package/src/utils/selection.ts +21 -9
  70. package/src/utils/ucs2Indices.ts +2 -2
  71. package/src/utils/validateValue.ts +118 -45
  72. package/src/utils/values.ts +38 -17
  73. package/src/utils/weakMaps.ts +20 -10
  74. package/src/utils/withChanges.ts +5 -3
  75. package/src/utils/withUndoRedo.ts +1 -1
  76. package/src/utils/withoutPatching.ts +1 -1
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,10 @@ 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
+ () => alert(JSON.stringify(props.annotation)),
712
+ [props.annotation]
713
+ );
638
714
  return /* @__PURE__ */ jsxRuntime.jsx("span", { style: { color: "blue" }, onClick: handleClick, children: props.children });
639
715
  }
640
716
  function getPortableTextMemberSchemaTypes(portableTextType) {
@@ -643,16 +719,24 @@ function getPortableTextMemberSchemaTypes(portableTextType) {
643
719
  const blockType = portableTextType.of?.find(findBlockType);
644
720
  if (!blockType)
645
721
  throw new Error("Block type is not defined in this schema (required)");
646
- const childrenField = blockType.fields?.find((field) => field.name === "children");
722
+ const childrenField = blockType.fields?.find(
723
+ (field) => field.name === "children"
724
+ );
647
725
  if (!childrenField)
648
726
  throw new Error("Children field for block type found in schema (required)");
649
727
  const ofType = childrenField.type.of;
650
728
  if (!ofType)
651
- throw new Error("Valid types for block children not found in schema (required)");
729
+ throw new Error(
730
+ "Valid types for block children not found in schema (required)"
731
+ );
652
732
  const spanType = ofType.find((memberType) => memberType.name === "span");
653
733
  if (!spanType)
654
734
  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) || [];
735
+ const inlineObjectTypes = ofType.filter(
736
+ (memberType) => memberType.name !== "span"
737
+ ) || [], blockObjectTypes = portableTextType.of?.filter(
738
+ (field) => field.name !== blockType.name
739
+ ) || [];
656
740
  return {
657
741
  styles: resolveEnabledStyles(blockType),
658
742
  decorators: resolveEnabledDecorators(spanType),
@@ -666,10 +750,16 @@ function getPortableTextMemberSchemaTypes(portableTextType) {
666
750
  };
667
751
  }
668
752
  function resolveEnabledStyles(blockType) {
669
- const styleField = blockType.fields?.find((btField) => btField.name === "style");
753
+ const styleField = blockType.fields?.find(
754
+ (btField) => btField.name === "style"
755
+ );
670
756
  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);
757
+ throw new Error(
758
+ "A field with name 'style' is not defined in the block type (required)."
759
+ );
760
+ const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter(
761
+ (style) => style.value
762
+ );
673
763
  if (!textStyles || textStyles.length === 0)
674
764
  throw new Error(
675
765
  "The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}."
@@ -680,9 +770,13 @@ function resolveEnabledDecorators(spanType) {
680
770
  return spanType.decorators;
681
771
  }
682
772
  function resolveEnabledListItems(blockType) {
683
- const listField = blockType.fields?.find((btField) => btField.name === "listItem");
773
+ const listField = blockType.fields?.find(
774
+ (btField) => btField.name === "listItem"
775
+ );
684
776
  if (!listField)
685
- throw new Error("A field with name 'listItem' is not defined in the block type (required).");
777
+ throw new Error(
778
+ "A field with name 'listItem' is not defined in the block type (required)."
779
+ );
686
780
  const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
687
781
  if (!listItems)
688
782
  throw new Error("The list field need at least to be an empty array");
@@ -708,7 +802,12 @@ function createOperationToPatches(types2) {
708
802
  const textChild = editor.isTextBlock(block) && editor.isTextSpan(block.children[operation.path[1]]) && block.children[operation.path[1]];
709
803
  if (!textChild)
710
804
  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);
805
+ const path = [
806
+ { _key: block._key },
807
+ "children",
808
+ { _key: textChild._key },
809
+ "text"
810
+ ], 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
811
  return patch.value.length ? [patch] : [];
713
812
  }
714
813
  function removeTextPatch(editor, operation, beforeValue) {
@@ -720,7 +819,12 @@ function createOperationToPatches(types2) {
720
819
  throw new Error("Expected span");
721
820
  if (!textChild)
722
821
  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);
822
+ const path = [
823
+ { _key: block._key },
824
+ "children",
825
+ { _key: textChild._key },
826
+ "text"
827
+ ], 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
828
  return patch.value ? [patch] : [];
725
829
  }
726
830
  function setNodePatch(editor, operation) {
@@ -732,7 +836,9 @@ function createOperationToPatches(types2) {
732
836
  { ...editor.children[operation.path[0]], ...operation.newProperties },
733
837
  isUndefined__default.default
734
838
  );
735
- return [patches.set(fromSlateValue([setNode], textBlockName)[0], [{ _key: block._key }])];
839
+ return [
840
+ patches.set(fromSlateValue([setNode], textBlockName)[0], [{ _key: block._key }])
841
+ ];
736
842
  } else if (operation.path.length === 2) {
737
843
  const block = editor.children[operation.path[0]];
738
844
  if (editor.isTextBlock(block)) {
@@ -743,11 +849,23 @@ function createOperationToPatches(types2) {
743
849
  if (keys.length === 1 && keyName === "_key") {
744
850
  const val = get__default.default(operation.newProperties, keyName);
745
851
  patches$1.push(
746
- patches.set(val, [{ _key: blockKey }, "children", block.children.indexOf(child), keyName])
852
+ patches.set(val, [
853
+ { _key: blockKey },
854
+ "children",
855
+ block.children.indexOf(child),
856
+ keyName
857
+ ])
747
858
  );
748
859
  } else {
749
860
  const val = get__default.default(operation.newProperties, keyName);
750
- patches$1.push(patches.set(val, [{ _key: blockKey }, "children", { _key: childKey }, keyName]));
861
+ patches$1.push(
862
+ patches.set(val, [
863
+ { _key: blockKey },
864
+ "children",
865
+ { _key: childKey },
866
+ keyName
867
+ ])
868
+ );
751
869
  }
752
870
  }), patches$1;
753
871
  }
@@ -755,21 +873,27 @@ function createOperationToPatches(types2) {
755
873
  }
756
874
  throw new Error("Could not find a valid block");
757
875
  } else
758
- throw new Error(`Unexpected path encountered: ${JSON.stringify(operation.path)}`);
876
+ throw new Error(
877
+ `Unexpected path encountered: ${JSON.stringify(operation.path)}`
878
+ );
759
879
  }
760
880
  function insertNodePatch(editor, operation, beforeValue) {
761
881
  const block = beforeValue[operation.path[0]], isTextBlock = editor.isTextBlock(block);
762
882
  if (operation.path.length === 1) {
763
883
  const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block?._key : beforeBlock?._key;
764
884
  return targetKey ? [
765
- patches.insert([fromSlateValue([operation.node], textBlockName)[0]], position, [
766
- { _key: targetKey }
767
- ])
885
+ patches.insert(
886
+ [fromSlateValue([operation.node], textBlockName)[0]],
887
+ position,
888
+ [{ _key: targetKey }]
889
+ )
768
890
  ] : [
769
891
  patches.setIfMissing(beforeValue, []),
770
- patches.insert([fromSlateValue([operation.node], textBlockName)[0]], "before", [
771
- operation.path[0]
772
- ])
892
+ patches.insert(
893
+ [fromSlateValue([operation.node], textBlockName)[0]],
894
+ "before",
895
+ [operation.path[0]]
896
+ )
773
897
  ];
774
898
  } else if (isTextBlock && operation.path.length === 2 && editor.children[operation.path[0]]) {
775
899
  const position = block.children.length === 0 || !block.children[operation.path[1] - 1] ? "before" : "after", node = { ...operation.node };
@@ -792,7 +916,9 @@ function createOperationToPatches(types2) {
792
916
  ])
793
917
  ];
794
918
  }
795
- return debug$k("Something was inserted into a void block. Not producing editor patches."), [];
919
+ return debug$k(
920
+ "Something was inserted into a void block. Not producing editor patches."
921
+ ), [];
796
922
  }
797
923
  function splitNodePatch(editor, operation, beforeValue) {
798
924
  const patches$1 = [], splitBlock = editor.children[operation.path[0]];
@@ -809,7 +935,9 @@ function createOperationToPatches(types2) {
809
935
  [editor.children[operation.path[0] + 1]],
810
936
  textBlockName
811
937
  )[0];
812
- targetValue && (patches$1.push(patches.insert([targetValue], "after", [{ _key: splitBlock._key }])), oldBlock.children.slice(operation.position).forEach((span) => {
938
+ targetValue && (patches$1.push(
939
+ patches.insert([targetValue], "after", [{ _key: splitBlock._key }])
940
+ ), oldBlock.children.slice(operation.position).forEach((span) => {
813
941
  const path = [{ _key: oldBlock._key }, "children", { _key: span._key }];
814
942
  patches$1.push(patches.unset(path));
815
943
  }));
@@ -823,7 +951,10 @@ function createOperationToPatches(types2) {
823
951
  [
824
952
  {
825
953
  ...splitBlock,
826
- children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
954
+ children: splitBlock.children.slice(
955
+ operation.path[1] + 1,
956
+ operation.path[1] + 2
957
+ )
827
958
  }
828
959
  ],
829
960
  textBlockName
@@ -855,10 +986,14 @@ function createOperationToPatches(types2) {
855
986
  throw new Error("Block not found");
856
987
  } else if (editor.isTextBlock(block) && operation.path.length === 2) {
857
988
  const spanToRemove = block.children[operation.path[1]];
858
- return spanToRemove ? block.children.filter((span) => span._key === operation.node._key).length > 1 ? (console.warn(
989
+ return spanToRemove ? block.children.filter(
990
+ (span) => span._key === operation.node._key
991
+ ).length > 1 ? (console.warn(
859
992
  `Multiple spans have \`_key\` ${operation.node._key}. It's ambiguous which one to remove.`,
860
993
  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"), []);
994
+ ), []) : [
995
+ patches.unset([{ _key: block._key }, "children", { _key: spanToRemove._key }])
996
+ ] : (debug$k("Span not found in editor trying to remove node"), []);
862
997
  } else
863
998
  return debug$k("Not creating patch inside object block"), [];
864
999
  }
@@ -866,13 +1001,18 @@ function createOperationToPatches(types2) {
866
1001
  const patches$1 = [], block = beforeValue[operation.path[0]], updatedBlock = editor.children[operation.path[0]];
867
1002
  if (operation.path.length === 1)
868
1003
  if (block?._key) {
869
- const newBlock = fromSlateValue([editor.children[operation.path[0] - 1]], textBlockName)[0];
1004
+ const newBlock = fromSlateValue(
1005
+ [editor.children[operation.path[0] - 1]],
1006
+ textBlockName
1007
+ )[0];
870
1008
  patches$1.push(patches.set(newBlock, [{ _key: newBlock._key }])), patches$1.push(patches.unset([{ _key: block._key }]));
871
1009
  } else
872
1010
  throw new Error("Target key not found!");
873
1011
  else if (editor.isTextBlock(block) && editor.isTextBlock(updatedBlock) && operation.path.length === 2) {
874
1012
  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(
1013
+ updatedSpan && (block.children.filter(
1014
+ (span) => span._key === updatedSpan._key
1015
+ ).length === 1 ? patches$1.push(
876
1016
  patches.set(updatedSpan.text, [
877
1017
  { _key: block._key },
878
1018
  "children",
@@ -882,7 +1022,11 @@ function createOperationToPatches(types2) {
882
1022
  ) : console.warn(
883
1023
  `Multiple spans have \`_key\` ${updatedSpan._key}. It's ambiguous which one to update.`,
884
1024
  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(
1025
+ )), removedSpan && (block.children.filter(
1026
+ (span) => span._key === removedSpan._key
1027
+ ).length === 1 ? patches$1.push(
1028
+ patches.unset([{ _key: block._key }, "children", { _key: removedSpan._key }])
1029
+ ) : console.warn(
886
1030
  `Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`,
887
1031
  JSON.stringify(block, null, 2)
888
1032
  ));
@@ -895,7 +1039,9 @@ function createOperationToPatches(types2) {
895
1039
  if (operation.path.length === 1) {
896
1040
  const position = operation.path[0] > operation.newPath[0] ? "before" : "after";
897
1041
  patches$1.push(patches.unset([{ _key: block._key }])), patches$1.push(
898
- patches.insert([fromSlateValue([block], textBlockName)[0]], position, [{ _key: targetBlock._key }])
1042
+ patches.insert([fromSlateValue([block], textBlockName)[0]], position, [
1043
+ { _key: targetBlock._key }
1044
+ ])
899
1045
  );
900
1046
  } else if (operation.path.length === 2 && editor.isTextBlock(block) && editor.isTextBlock(targetBlock)) {
901
1047
  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 +1103,24 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
957
1103
  },
958
1104
  focusBlock: () => {
959
1105
  if (editor.selection) {
960
- const block = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
1106
+ const block = slate.Node.descendant(
1107
+ editor,
1108
+ editor.selection.focus.path.slice(0, 1)
1109
+ );
961
1110
  if (block)
962
- return fromSlateValue([block], types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0];
1111
+ return fromSlateValue(
1112
+ [block],
1113
+ types$1.block.name,
1114
+ KEY_TO_VALUE_ELEMENT.get(editor)
1115
+ )[0];
963
1116
  }
964
1117
  },
965
1118
  focusChild: () => {
966
1119
  if (editor.selection) {
967
- const block = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
1120
+ const block = slate.Node.descendant(
1121
+ editor,
1122
+ editor.selection.focus.path.slice(0, 1)
1123
+ );
968
1124
  if (block && editor.isTextBlock(block))
969
1125
  return fromSlateValue(
970
1126
  [block],
@@ -985,7 +1141,9 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
985
1141
  if (!focusBlock)
986
1142
  throw new Error("No focused text block");
987
1143
  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");
1144
+ throw new Error(
1145
+ "This type cannot be inserted as a child to a text block"
1146
+ );
989
1147
  const child = toSlateValue(
990
1148
  [
991
1149
  {
@@ -1002,11 +1160,17 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1002
1160
  ],
1003
1161
  portableTextEditor
1004
1162
  )[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, {
1163
+ return isSpanNode && focusNode._type !== types$1.span.name && (debug$j(
1164
+ "Inserting span child next to inline object child, moving selection + 1"
1165
+ ), editor.move({ distance: 1, unit: "character" })), slate.Transforms.insertNodes(editor, child, {
1006
1166
  select: !0,
1007
1167
  at: editor.selection
1008
1168
  }), editor.onChange(), toPortableTextRange(
1009
- fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1169
+ fromSlateValue(
1170
+ editor.children,
1171
+ types$1.block.name,
1172
+ KEY_TO_VALUE_ELEMENT.get(editor)
1173
+ ),
1010
1174
  editor.selection,
1011
1175
  types$1
1012
1176
  )?.focus.path || [];
@@ -1031,7 +1195,11 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1031
1195
  })
1032
1196
  )[0];
1033
1197
  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)),
1198
+ fromSlateValue(
1199
+ editor.children,
1200
+ types$1.block.name,
1201
+ KEY_TO_VALUE_ELEMENT.get(editor)
1202
+ ),
1035
1203
  editor.selection,
1036
1204
  types$1
1037
1205
  )?.focus.path ?? [];
@@ -1043,7 +1211,11 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1043
1211
  })
1044
1212
  )[0];
1045
1213
  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)),
1214
+ fromSlateValue(
1215
+ editor.children,
1216
+ types$1.block.name,
1217
+ KEY_TO_VALUE_ELEMENT.get(editor)
1218
+ ),
1047
1219
  editor.selection,
1048
1220
  types$1
1049
1221
  )?.focus.path || [];
@@ -1069,10 +1241,16 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1069
1241
  editor
1070
1242
  );
1071
1243
  if (slatePath) {
1072
- const [block, blockPath] = slate.Editor.node(editor, slatePath.focus.path.slice(0, 1));
1244
+ const [block, blockPath] = slate.Editor.node(
1245
+ editor,
1246
+ slatePath.focus.path.slice(0, 1)
1247
+ );
1073
1248
  if (block && blockPath && typeof block._key == "string") {
1074
1249
  if (path.length === 1 && slatePath.focus.path.length === 1)
1075
- return [fromSlateValue([block], types$1.block.name)[0], [{ _key: block._key }]];
1250
+ return [
1251
+ fromSlateValue([block], types$1.block.name)[0],
1252
+ [{ _key: block._key }]
1253
+ ];
1076
1254
  const ptBlock = fromSlateValue(
1077
1255
  [block],
1078
1256
  types$1.block.name,
@@ -1081,7 +1259,10 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1081
1259
  if (editor.isTextBlock(ptBlock)) {
1082
1260
  const ptChild = ptBlock.children[slatePath.focus.path[1]];
1083
1261
  if (ptChild)
1084
- return [ptChild, [{ _key: block._key }, "children", { _key: ptChild._key }]];
1262
+ return [
1263
+ ptChild,
1264
+ [{ _key: block._key }, "children", { _key: ptChild._key }]
1265
+ ];
1085
1266
  }
1086
1267
  }
1087
1268
  }
@@ -1148,44 +1329,74 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1148
1329
  addAnnotation: (type, value) => {
1149
1330
  const { selection: originalSelection } = editor;
1150
1331
  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
1332
+ if (originalSelection && (slate.Range.isCollapsed(originalSelection) && (editor.pteExpandToWord(), editor.onChange()), editor.selection)) {
1333
+ let spanPath, markDefPath;
1334
+ const markDefPaths = [];
1335
+ slate.Editor.withoutNormalizing(editor, () => {
1336
+ if (!editor.selection)
1337
+ return;
1338
+ const selectedBlocks = slate.Editor.nodes(editor, {
1339
+ at: editor.selection,
1340
+ match: (node) => editor.isTextBlock(node),
1341
+ reverse: slate.Range.isBackward(editor.selection)
1342
+ });
1343
+ for (const [block, blockPath] of selectedBlocks) {
1344
+ if (block.children.length === 0 || block.children.length === 1 && block.children[0].text === "")
1345
+ continue;
1346
+ const annotationKey = keyGenerator(), markDefs = block.markDefs ?? [];
1347
+ markDefs.find(
1348
+ (markDef) => markDef._type === type.name && markDef._key === annotationKey
1349
+ ) === void 0 && (slate.Transforms.setNodes(
1350
+ editor,
1351
+ {
1352
+ markDefs: [
1353
+ ...markDefs,
1354
+ {
1355
+ _type: type.name,
1356
+ _key: annotationKey,
1357
+ ...value
1358
+ }
1359
+ ]
1360
+ },
1361
+ { at: blockPath }
1362
+ ), markDefPath = [
1363
+ { _key: block._key },
1364
+ "markDefs",
1365
+ { _key: annotationKey }
1366
+ ], slate.Range.isBackward(editor.selection) ? markDefPaths.unshift(markDefPath) : markDefPaths.push(markDefPath)), slate.Transforms.setNodes(
1367
+ editor,
1368
+ {},
1369
+ { match: slate.Text.isText, split: !0 }
1370
+ );
1371
+ const children = slate.Node.children(editor, blockPath);
1372
+ for (const [span, path] of children) {
1373
+ if (!editor.isTextSpan(span) || !slate.Range.includes(editor.selection, path))
1374
+ continue;
1375
+ const marks = span.marks ?? [], existingSameTypeAnnotations = marks.filter(
1376
+ (mark) => markDefs.some(
1377
+ (markDef) => markDef._key === mark && markDef._type === type.name
1378
+ )
1379
+ );
1380
+ slate.Transforms.setNodes(
1381
+ editor,
1382
+ {
1383
+ marks: [
1384
+ ...marks.filter(
1385
+ (mark) => !existingSameTypeAnnotations.includes(mark)
1386
+ ),
1387
+ annotationKey
1388
+ ]
1389
+ },
1390
+ { at: path }
1391
+ ), spanPath = [{ _key: block._key }, "children", { _key: span._key }];
1177
1392
  }
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 }]
1393
+ }
1394
+ markDefPath && spanPath && (returnValue = {
1395
+ markDefPath,
1396
+ markDefPaths,
1397
+ spanPath
1187
1398
  });
1188
- }), slate.Editor.normalize(editor), editor.onChange());
1399
+ }), editor.onChange();
1189
1400
  }
1190
1401
  return returnValue;
1191
1402
  },
@@ -1212,34 +1423,98 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1212
1423
  voids: !0,
1213
1424
  match: (node) => node._type === types$1.span.name || // Text children
1214
1425
  !editor.isTextBlock(node) && slate.Element.isElement(node)
1215
- })), editor.children.length === 0 && (editor.children = [editor.pteCreateEmptyBlock()]), editor.onChange();
1426
+ })), editor.children.length === 0 && (editor.children = [editor.pteCreateTextBlock({ decorators: [] })]), editor.onChange();
1216
1427
  }
1217
1428
  }
1218
1429
  },
1219
1430
  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)
1431
+ debug$j("Removing annotation", type), slate.Editor.withoutNormalizing(editor, () => {
1432
+ if (editor.selection)
1433
+ if (slate.Range.isCollapsed(editor.selection)) {
1434
+ const [block, blockPath] = slate.Editor.node(editor, editor.selection, {
1435
+ depth: 1
1436
+ });
1437
+ if (!editor.isTextBlock(block))
1229
1438
  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
- });
1439
+ const potentialAnnotations = (block.markDefs ?? []).filter(
1440
+ (markDef) => markDef._type === type.name
1441
+ ), [selectedChild, selectedChildPath] = slate.Editor.node(
1442
+ editor,
1443
+ editor.selection,
1444
+ {
1445
+ depth: 2
1446
+ }
1447
+ );
1448
+ if (!editor.isTextSpan(selectedChild))
1449
+ return;
1450
+ const annotationToRemove = selectedChild.marks?.find(
1451
+ (mark) => potentialAnnotations.some((markDef) => markDef._key === mark)
1452
+ );
1453
+ if (!annotationToRemove)
1454
+ return;
1455
+ const previousSpansWithSameAnnotation = [];
1456
+ for (const [child, childPath] of slate.Node.children(editor, blockPath, {
1457
+ reverse: !0
1458
+ }))
1459
+ if (editor.isTextSpan(child) && slate.Path.isBefore(childPath, selectedChildPath))
1460
+ if (child.marks?.includes(annotationToRemove))
1461
+ previousSpansWithSameAnnotation.push([child, childPath]);
1462
+ else
1463
+ break;
1464
+ const nextSpansWithSameAnnotation = [];
1465
+ for (const [child, childPath] of slate.Node.children(editor, blockPath))
1466
+ if (editor.isTextSpan(child) && slate.Path.isAfter(childPath, selectedChildPath))
1467
+ if (child.marks?.includes(annotationToRemove))
1468
+ nextSpansWithSameAnnotation.push([child, childPath]);
1469
+ else
1470
+ break;
1471
+ for (const [child, childPath] of [
1472
+ ...previousSpansWithSameAnnotation,
1473
+ [selectedChild, selectedChildPath],
1474
+ ...nextSpansWithSameAnnotation
1475
+ ])
1476
+ slate.Transforms.setNodes(
1477
+ editor,
1478
+ {
1479
+ marks: child.marks?.filter(
1480
+ (mark) => mark !== annotationToRemove
1481
+ )
1482
+ },
1483
+ { at: childPath }
1484
+ );
1485
+ } else {
1486
+ slate.Transforms.setNodes(
1487
+ editor,
1488
+ {},
1489
+ {
1490
+ match: (node) => editor.isTextSpan(node),
1491
+ split: !0,
1492
+ hanging: !0
1493
+ }
1494
+ );
1495
+ const blocks = slate.Editor.nodes(editor, {
1496
+ at: editor.selection,
1497
+ match: (node) => editor.isTextBlock(node)
1239
1498
  });
1499
+ for (const [block, blockPath] of blocks) {
1500
+ const children = slate.Node.children(editor, blockPath);
1501
+ for (const [child, childPath] of children) {
1502
+ if (!editor.isTextSpan(child) || !slate.Range.includes(editor.selection, childPath))
1503
+ continue;
1504
+ const markDefs = block.markDefs ?? [], marks = child.marks ?? [], marksWithoutAnnotation = marks.filter((mark) => markDefs.find(
1505
+ (markDef2) => markDef2._key === mark
1506
+ )?._type !== type.name);
1507
+ marksWithoutAnnotation.length !== marks.length && slate.Transforms.setNodes(
1508
+ editor,
1509
+ {
1510
+ marks: marksWithoutAnnotation
1511
+ },
1512
+ { at: childPath }
1513
+ );
1514
+ }
1515
+ }
1240
1516
  }
1241
- }), slate.Editor.normalize(editor), editor.onChange();
1242
- }
1517
+ }), editor.onChange();
1243
1518
  },
1244
1519
  getSelection: () => {
1245
1520
  let ptRange = null;
@@ -1248,14 +1523,22 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1248
1523
  if (existing)
1249
1524
  return existing;
1250
1525
  ptRange = toPortableTextRange(
1251
- fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1526
+ fromSlateValue(
1527
+ editor.children,
1528
+ types$1.block.name,
1529
+ KEY_TO_VALUE_ELEMENT.get(editor)
1530
+ ),
1252
1531
  editor.selection,
1253
1532
  types$1
1254
1533
  ), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
1255
1534
  }
1256
1535
  return ptRange;
1257
1536
  },
1258
- getValue: () => fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1537
+ getValue: () => fromSlateValue(
1538
+ editor.children,
1539
+ types$1.block.name,
1540
+ KEY_TO_VALUE_ELEMENT.get(editor)
1541
+ ),
1259
1542
  isCollapsedSelection: () => !!editor.selection && slate.Range.isCollapsed(editor.selection),
1260
1543
  isExpandedSelection: () => !!editor.selection && slate.Range.isExpanded(editor.selection),
1261
1544
  insertBreak: () => {
@@ -1269,23 +1552,94 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1269
1552
  }), editor;
1270
1553
  };
1271
1554
  }
1272
- function createWithInsertBreak(types2) {
1555
+ function createWithInsertBreak(types2, keyGenerator) {
1273
1556
  return function(editor) {
1274
1557
  const { insertBreak } = editor;
1275
1558
  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
- }
1559
+ if (!editor.selection) {
1560
+ insertBreak();
1561
+ return;
1562
+ }
1563
+ const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
1564
+ if (editor.isTextBlock(focusBlock)) {
1565
+ const [start, end] = slate.Range.edges(editor.selection), isEndAtStartOfBlock = isEqual__default.default(end, {
1566
+ path: [...focusBlockPath, 0],
1567
+ offset: 0
1568
+ });
1569
+ if (isEndAtStartOfBlock && slate.Range.isCollapsed(editor.selection)) {
1570
+ const focusDecorators = editor.isTextSpan(focusBlock.children[0]) ? (focusBlock.children[0].marks ?? []).filter(
1571
+ (mark) => types2.decorators.some((decorator) => decorator.value === mark)
1572
+ ) : [];
1573
+ slate.Editor.insertNode(
1574
+ editor,
1575
+ editor.pteCreateTextBlock({ decorators: focusDecorators })
1576
+ );
1577
+ const [nextBlockPath] = slate.Path.next(focusBlockPath);
1578
+ slate.Transforms.select(editor, {
1579
+ anchor: { path: [nextBlockPath, 0], offset: 0 },
1580
+ focus: { path: [nextBlockPath, 0], offset: 0 }
1581
+ }), editor.onChange();
1582
+ return;
1583
+ }
1584
+ const lastFocusBlockChild = focusBlock.children[focusBlock.children.length - 1], isStartAtEndOfBlock = isEqual__default.default(start, {
1585
+ path: [...focusBlockPath, focusBlock.children.length - 1],
1586
+ offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
1587
+ });
1588
+ if (!isEndAtStartOfBlock && !isStartAtEndOfBlock) {
1589
+ slate.Editor.withoutNormalizing(editor, () => {
1590
+ if (!editor.selection)
1591
+ return;
1592
+ slate.Transforms.splitNodes(editor, {
1593
+ at: editor.selection
1594
+ });
1595
+ const [nextNode, nextNodePath] = slate.Editor.node(
1596
+ editor,
1597
+ slate.Path.next(focusBlockPath),
1598
+ { depth: 1 }
1599
+ );
1600
+ if (slate.Transforms.setSelection(editor, {
1601
+ anchor: { path: [...nextNodePath, 0], offset: 0 },
1602
+ focus: { path: [...nextNodePath, 0], offset: 0 }
1603
+ }), editor.isTextBlock(nextNode) && nextNode.markDefs && nextNode.markDefs.length > 0) {
1604
+ const newMarkDefKeys = /* @__PURE__ */ new Map(), prevNodeSpans = Array.from(
1605
+ slate.Node.children(editor, focusBlockPath)
1606
+ ).map((entry) => entry[0]).filter((node) => editor.isTextSpan(node)), children = slate.Node.children(editor, nextNodePath);
1607
+ for (const [child, childPath] of children) {
1608
+ if (!editor.isTextSpan(child))
1609
+ continue;
1610
+ const marks = child.marks ?? [];
1611
+ for (const mark of marks)
1612
+ types2.decorators.some(
1613
+ (decorator) => decorator.value === mark
1614
+ ) || prevNodeSpans.some(
1615
+ (prevNodeSpan) => prevNodeSpan.marks?.includes(mark)
1616
+ ) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
1617
+ const newMarks = marks.map(
1618
+ (mark) => newMarkDefKeys.get(mark) ?? mark
1619
+ );
1620
+ isEqual__default.default(marks, newMarks) || slate.Transforms.setNodes(
1621
+ editor,
1622
+ { marks: newMarks },
1623
+ {
1624
+ at: childPath
1625
+ }
1626
+ );
1627
+ }
1628
+ const newMarkDefs = nextNode.markDefs.map((markDef) => ({
1629
+ ...markDef,
1630
+ _key: newMarkDefKeys.get(markDef._key) ?? markDef._key
1631
+ }));
1632
+ isEqual__default.default(nextNode.markDefs, newMarkDefs) || slate.Transforms.setNodes(
1633
+ editor,
1634
+ { markDefs: newMarkDefs },
1635
+ {
1636
+ at: nextNodePath,
1637
+ match: (node) => editor.isTextBlock(node)
1638
+ }
1639
+ );
1640
+ }
1641
+ }), editor.onChange();
1642
+ return;
1289
1643
  }
1290
1644
  }
1291
1645
  insertBreak();
@@ -2085,11 +2439,13 @@ function toInt(num) {
2085
2439
  const debug$i = debugWithName("applyPatches"), debugVerbose$4 = debug$i.enabled && !0;
2086
2440
  function createApplyPatch(schemaTypes) {
2087
2441
  let previousPatch;
2088
- return function(editor, patch) {
2442
+ return (editor, patch) => {
2089
2443
  let changed = !1;
2090
- debugVerbose$4 && (debug$i(`
2444
+ debugVerbose$4 && (debug$i(
2445
+ `
2091
2446
 
2092
- NEW PATCH =============================================================`), debug$i(JSON.stringify(patch, null, 2)));
2447
+ NEW PATCH =============================================================`
2448
+ ), debug$i(JSON.stringify(patch, null, 2)));
2093
2449
  try {
2094
2450
  switch (patch.type) {
2095
2451
  case "insert":
@@ -2114,14 +2470,19 @@ NEW PATCH =============================================================`), debug
2114
2470
  };
2115
2471
  }
2116
2472
  function diffMatchPatch(editor, patch) {
2117
- const { block, child, childPath } = findBlockAndChildFromPath(editor, patch.path);
2473
+ const { block, child, childPath } = findBlockAndChildFromPath(
2474
+ editor,
2475
+ patch.path
2476
+ );
2118
2477
  if (!block)
2119
2478
  return debug$i("Block not found"), !1;
2120
2479
  if (!child || !childPath)
2121
2480
  return debug$i("Child not found"), !1;
2122
2481
  if (!(block && editor.isTextBlock(block) && patch.path.length === 4 && patch.path[1] === "children" && patch.path[3] === "text") || !slate.Text.isText(child))
2123
2482
  return !1;
2124
- const patches2 = parse(patch.value), [newValue] = apply(patches2, child.text, { allowExceedingIndices: !0 }), diff$1 = cleanupEfficiency(diff(child.text, newValue), 5);
2483
+ const patches2 = parse(patch.value), [newValue] = apply(patches2, child.text, {
2484
+ allowExceedingIndices: !0
2485
+ }), diff$1 = cleanupEfficiency(diff(child.text, newValue), 5);
2125
2486
  debugState(editor, "before");
2126
2487
  let offset = 0;
2127
2488
  for (const [op, text] of diff$1)
@@ -2155,12 +2516,17 @@ function insertPatch(editor, patch, schemaTypes) {
2155
2516
  { schemaTypes },
2156
2517
  KEY_TO_SLATE_ELEMENT.get(editor)
2157
2518
  ), 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;
2519
+ return debug$i(`Inserting children at path ${childInsertPath}`), debugState(editor, "before"), childrenToInsert && slate.Element.isElement(childrenToInsert[0]) && slate.Transforms.insertNodes(editor, childrenToInsert[0].children, {
2520
+ at: childInsertPath
2521
+ }), debugState(editor, "after"), !0;
2159
2522
  }
2160
2523
  function setPatch(editor, patch) {
2161
2524
  let value = patch.value;
2162
2525
  typeof patch.path[3] == "string" && (value = {}, value[patch.path[3]] = patch.value);
2163
- const { block, blockPath, child, childPath } = findBlockAndChildFromPath(editor, patch.path);
2526
+ const { block, blockPath, child, childPath } = findBlockAndChildFromPath(
2527
+ editor,
2528
+ patch.path
2529
+ );
2164
2530
  if (!block)
2165
2531
  return debug$i("Block not found"), !1;
2166
2532
  const isTextBlock = editor.isTextBlock(block);
@@ -2221,12 +2587,15 @@ function unsetPatch(editor, patch, previousPatch) {
2221
2587
  const previousSelection = editor.selection;
2222
2588
  return slate.Transforms.deselect(editor), editor.children.forEach((c, i) => {
2223
2589
  slate.Transforms.removeNodes(editor, { at: [i] });
2224
- }), slate.Transforms.insertNodes(editor, editor.pteCreateEmptyBlock()), previousSelection && slate.Transforms.select(editor, {
2590
+ }), slate.Transforms.insertNodes(editor, editor.pteCreateTextBlock({ decorators: [] })), previousSelection && slate.Transforms.select(editor, {
2225
2591
  anchor: { path: [0, 0], offset: 0 },
2226
2592
  focus: { path: [0, 0], offset: 0 }
2227
2593
  }), editor.onChange(), debugState(editor, "after"), !0;
2228
2594
  }
2229
- const { block, blockPath, child, childPath } = findBlockAndChildFromPath(editor, patch.path);
2595
+ const { block, blockPath, child, childPath } = findBlockAndChildFromPath(
2596
+ editor,
2597
+ patch.path
2598
+ );
2230
2599
  if (patch.path.length === 1) {
2231
2600
  if (!block || !blockPath)
2232
2601
  return debug$i("Block not found"), !1;
@@ -2258,7 +2627,12 @@ function findBlockAndChildFromPath(editor, path) {
2258
2627
  const isMatch = isKeyedSegment(path[2]) ? node._key === path[2]._key : index === path[2];
2259
2628
  return isMatch && (childIndex = index), isMatch;
2260
2629
  });
2261
- return child ? { block, child, blockPath, childPath: blockPath?.concat(childIndex) } : { block, blockPath, child: void 0, childPath: void 0 };
2630
+ return child ? {
2631
+ block,
2632
+ child,
2633
+ blockPath,
2634
+ childPath: blockPath?.concat(childIndex)
2635
+ } : { block, blockPath, child: void 0, childPath: void 0 };
2262
2636
  }
2263
2637
  const PATCHING = /* @__PURE__ */ new WeakMap();
2264
2638
  function withoutPatching(editor, fn) {
@@ -2287,10 +2661,17 @@ function createWithUndoRedo(options) {
2287
2661
  patches2.forEach((patch) => {
2288
2662
  if (!reset && patch.origin !== "local" && remotePatches) {
2289
2663
  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;
2664
+ debug$h(
2665
+ "Someone else cleared the content, resetting undo/redo history"
2666
+ ), editor.history = { undos: [], redos: [] }, remotePatches.splice(0, remotePatches.length), SAVING.set(editor, !0), reset = !0;
2291
2667
  return;
2292
2668
  }
2293
- remotePatches.push({ patch, time: /* @__PURE__ */ new Date(), snapshot, previousSnapshot });
2669
+ remotePatches.push({
2670
+ patch,
2671
+ time: /* @__PURE__ */ new Date(),
2672
+ snapshot,
2673
+ previousSnapshot
2674
+ });
2294
2675
  }
2295
2676
  }), previousSnapshot = snapshot;
2296
2677
  });
@@ -2311,7 +2692,10 @@ function createWithUndoRedo(options) {
2311
2692
  step.operations.push(op);
2312
2693
  else {
2313
2694
  const newStep = {
2314
- operations: [...editor.selection === null ? [] : [createSelectOperation(editor)], op],
2695
+ operations: [
2696
+ ...editor.selection === null ? [] : [createSelectOperation(editor)],
2697
+ op
2698
+ ],
2315
2699
  timestamp: /* @__PURE__ */ new Date()
2316
2700
  };
2317
2701
  undos.push(newStep), debug$h("Created new undo step", step);
@@ -2328,12 +2712,20 @@ function createWithUndoRedo(options) {
2328
2712
  if (undos.length > 0) {
2329
2713
  const step = undos[undos.length - 1];
2330
2714
  if (debug$h("Undoing", step), step.operations.length > 0) {
2331
- const otherPatches = remotePatches.filter((item) => item.time >= step.timestamp);
2715
+ const otherPatches = remotePatches.filter(
2716
+ (item) => item.time >= step.timestamp
2717
+ );
2332
2718
  let transformedOperations = step.operations;
2333
2719
  otherPatches.forEach((item) => {
2334
2720
  transformedOperations = flatten__default.default(
2335
2721
  transformedOperations.map(
2336
- (op) => transformOperation(editor, item.patch, op, item.snapshot, item.previousSnapshot)
2722
+ (op) => transformOperation(
2723
+ editor,
2724
+ item.patch,
2725
+ op,
2726
+ item.snapshot,
2727
+ item.previousSnapshot
2728
+ )
2337
2729
  )
2338
2730
  );
2339
2731
  });
@@ -2362,12 +2754,20 @@ function createWithUndoRedo(options) {
2362
2754
  if (redos.length > 0) {
2363
2755
  const step = redos[redos.length - 1];
2364
2756
  if (debug$h("Redoing", step), step.operations.length > 0) {
2365
- const otherPatches = remotePatches.filter((item) => item.time >= step.timestamp);
2757
+ const otherPatches = remotePatches.filter(
2758
+ (item) => item.time >= step.timestamp
2759
+ );
2366
2760
  let transformedOperations = step.operations;
2367
2761
  otherPatches.forEach((item) => {
2368
2762
  transformedOperations = flatten__default.default(
2369
2763
  transformedOperations.map(
2370
- (op) => transformOperation(editor, item.patch, op, item.snapshot, item.previousSnapshot)
2764
+ (op) => transformOperation(
2765
+ editor,
2766
+ item.patch,
2767
+ op,
2768
+ item.snapshot,
2769
+ item.previousSnapshot
2770
+ )
2371
2771
  )
2372
2772
  );
2373
2773
  });
@@ -2392,7 +2792,9 @@ function createWithUndoRedo(options) {
2392
2792
  };
2393
2793
  }
2394
2794
  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)}`));
2795
+ debugVerbose$3 && (debug$h(
2796
+ `Adjusting '${operation.type}' operation paths for '${patch.type}' patch`
2797
+ ), debug$h(`Operation ${JSON.stringify(operation)}`), debug$h(`Patch ${JSON.stringify(patch)}`));
2396
2798
  const transformedOperation = { ...operation };
2397
2799
  if (patch.type === "insert" && patch.path.length === 1) {
2398
2800
  const insertBlockIndex = (snapshot || []).findIndex(
@@ -2400,7 +2802,13 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
2400
2802
  );
2401
2803
  return debug$h(
2402
2804
  `Adjusting block path (+${patch.items.length}) for '${transformedOperation.type}' operation and patch '${patch.type}'`
2403
- ), [adjustBlockPath(transformedOperation, patch.items.length, insertBlockIndex)];
2805
+ ), [
2806
+ adjustBlockPath(
2807
+ transformedOperation,
2808
+ patch.items.length,
2809
+ insertBlockIndex
2810
+ )
2811
+ ];
2404
2812
  }
2405
2813
  if (patch.type === "unset" && patch.path.length === 1) {
2406
2814
  const unsetBlockIndex = (previousSnapshot || []).findIndex(
@@ -2411,9 +2819,14 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
2411
2819
  )), [adjustBlockPath(transformedOperation, -1, unsetBlockIndex)]);
2412
2820
  }
2413
2821
  if (patch.type === "unset" && patch.path.length === 0)
2414
- return debug$h(`Adjusting selection for unset everything patch and ${operation.type} operation`), [];
2822
+ return debug$h(
2823
+ `Adjusting selection for unset everything patch and ${operation.type} operation`
2824
+ ), [];
2415
2825
  if (patch.type === "diffMatchPatch") {
2416
- const operationTargetBlock = findOperationTargetBlock(editor, transformedOperation);
2826
+ const operationTargetBlock = findOperationTargetBlock(
2827
+ editor,
2828
+ transformedOperation
2829
+ );
2417
2830
  return !operationTargetBlock || !isEqual__default.default({ _key: operationTargetBlock._key }, patch.path[0]) ? [transformedOperation] : (parse(patch.value).forEach((diffPatch) => {
2418
2831
  let adjustOffsetBy = 0, changedOffset = diffPatch.utf8Start1;
2419
2832
  const { diffs } = diffPatch;
@@ -2439,7 +2852,10 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
2439
2852
  function adjustBlockPath(operation, level, blockIndex) {
2440
2853
  const transformedOperation = { ...operation };
2441
2854
  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)];
2855
+ const newPath = [
2856
+ transformedOperation.path[0] + level,
2857
+ ...transformedOperation.path.slice(1)
2858
+ ];
2443
2859
  transformedOperation.path = newPath;
2444
2860
  }
2445
2861
  if (transformedOperation.type === "set_selection") {
@@ -2527,31 +2943,51 @@ function createWithPatches({
2527
2943
  case "insert_text":
2528
2944
  patches$1 = [
2529
2945
  ...patches$1,
2530
- ...patchFunctions.insertTextPatch(editor, operation, previousChildren)
2946
+ ...patchFunctions.insertTextPatch(
2947
+ editor,
2948
+ operation,
2949
+ previousChildren
2950
+ )
2531
2951
  ];
2532
2952
  break;
2533
2953
  case "remove_text":
2534
2954
  patches$1 = [
2535
2955
  ...patches$1,
2536
- ...patchFunctions.removeTextPatch(editor, operation, previousChildren)
2956
+ ...patchFunctions.removeTextPatch(
2957
+ editor,
2958
+ operation,
2959
+ previousChildren
2960
+ )
2537
2961
  ];
2538
2962
  break;
2539
2963
  case "remove_node":
2540
2964
  patches$1 = [
2541
2965
  ...patches$1,
2542
- ...patchFunctions.removeNodePatch(editor, operation, previousChildren)
2966
+ ...patchFunctions.removeNodePatch(
2967
+ editor,
2968
+ operation,
2969
+ previousChildren
2970
+ )
2543
2971
  ];
2544
2972
  break;
2545
2973
  case "split_node":
2546
2974
  patches$1 = [
2547
2975
  ...patches$1,
2548
- ...patchFunctions.splitNodePatch(editor, operation, previousChildren)
2976
+ ...patchFunctions.splitNodePatch(
2977
+ editor,
2978
+ operation,
2979
+ previousChildren
2980
+ )
2549
2981
  ];
2550
2982
  break;
2551
2983
  case "insert_node":
2552
2984
  patches$1 = [
2553
2985
  ...patches$1,
2554
- ...patchFunctions.insertNodePatch(editor, operation, previousChildren)
2986
+ ...patchFunctions.insertNodePatch(
2987
+ editor,
2988
+ operation,
2989
+ previousChildren
2990
+ )
2555
2991
  ];
2556
2992
  break;
2557
2993
  case "set_node":
@@ -2563,17 +2999,27 @@ function createWithPatches({
2563
2999
  case "merge_node":
2564
3000
  patches$1 = [
2565
3001
  ...patches$1,
2566
- ...patchFunctions.mergeNodePatch(editor, operation, previousChildren)
3002
+ ...patchFunctions.mergeNodePatch(
3003
+ editor,
3004
+ operation,
3005
+ previousChildren
3006
+ )
2567
3007
  ];
2568
3008
  break;
2569
3009
  case "move_node":
2570
3010
  patches$1 = [
2571
3011
  ...patches$1,
2572
- ...patchFunctions.moveNodePatch(editor, operation, previousChildren)
3012
+ ...patchFunctions.moveNodePatch(
3013
+ editor,
3014
+ operation,
3015
+ previousChildren
3016
+ )
2573
3017
  ];
2574
3018
  break;
2575
3019
  }
2576
- return !editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(operation.type) && (patches$1 = [...patches$1, patches.unset([])], change$.next({
3020
+ return !editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(
3021
+ operation.type
3022
+ ) && (patches$1 = [...patches$1, patches.unset([])], change$.next({
2577
3023
  type: "unset",
2578
3024
  previousValue: fromSlateValue(
2579
3025
  previousChildren,
@@ -2606,7 +3052,10 @@ function createWithPlaceholderBlock() {
2606
3052
  const node = op.node;
2607
3053
  if (op.path[0] === 0 && slate.Editor.isVoid(editor, node)) {
2608
3054
  const nextPath = slate.Path.next(op.path);
2609
- editor.children[nextPath[0]] || (debug$f("Adding placeholder block"), slate.Editor.insertNode(editor, editor.pteCreateEmptyBlock()));
3055
+ editor.children[nextPath[0]] || (debug$f("Adding placeholder block"), slate.Editor.insertNode(
3056
+ editor,
3057
+ editor.pteCreateTextBlock({ decorators: [] })
3058
+ ));
2610
3059
  }
2611
3060
  }
2612
3061
  apply2(op);
@@ -2625,7 +3074,11 @@ function createWithPortableTextBlockStyle(types2) {
2625
3074
  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
3075
  const [child] = slate.Editor.node(editor, [op.path[0] + 1, 0]);
2627
3076
  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 });
3077
+ debug$e(`Normalizing split node to ${defaultStyle} style`, op), slate.Transforms.setNodes(
3078
+ editor,
3079
+ { style: defaultStyle },
3080
+ { at: [op.path[0] + 1], voids: !1 }
3081
+ );
2629
3082
  break;
2630
3083
  }
2631
3084
  }
@@ -2641,9 +3094,13 @@ function createWithPortableTextBlockStyle(types2) {
2641
3094
  match: (node) => editor.isTextBlock(node)
2642
3095
  })
2643
3096
  ].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(
3097
+ editor.isTextBlock(node) && node.style === blockStyle ? (debug$e(`Unsetting block style '${blockStyle}'`), slate.Transforms.setNodes(
3098
+ editor,
3099
+ { ...node, style: defaultStyle },
3100
+ {
3101
+ at: path
3102
+ }
3103
+ )) : (blockStyle ? debug$e(`Setting style '${blockStyle}'`) : debug$e("Setting default style", defaultStyle), slate.Transforms.setNodes(
2647
3104
  editor,
2648
3105
  {
2649
3106
  ...node,
@@ -2725,7 +3182,13 @@ function createWithPortableTextLists(types2) {
2725
3182
  return selectedBlocks.length === 0 ? !1 : (selectedBlocks.forEach(([node, path]) => {
2726
3183
  if (editor.isListBlock(node)) {
2727
3184
  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(
3185
+ reverse ? (level--, debug$d(
3186
+ "Decrementing list level",
3187
+ Math.min(MAX_LIST_LEVEL, Math.max(1, level))
3188
+ )) : (level++, debug$d(
3189
+ "Incrementing list level",
3190
+ Math.min(MAX_LIST_LEVEL, Math.max(1, level))
3191
+ )), slate.Transforms.setNodes(
2729
3192
  editor,
2730
3193
  { level: Math.min(MAX_LIST_LEVEL, Math.max(1, level)) },
2731
3194
  { at: path }
@@ -2767,7 +3230,11 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2767
3230
  return function(editor) {
2768
3231
  const { apply: apply2, normalizeNode } = editor, decorators = types2.decorators.map((t) => t.value), forceNewSelection = () => {
2769
3232
  editor.selection && (slate.Transforms.select(editor, { ...editor.selection }), editor.selection = { ...editor.selection });
2770
- const ptRange = toPortableTextRange(editor.children, editor.selection, types2);
3233
+ const ptRange = toPortableTextRange(
3234
+ editor.children,
3235
+ editor.selection,
3236
+ types2
3237
+ );
2771
3238
  change$.next({ type: "selection", selection: ptRange });
2772
3239
  };
2773
3240
  return editor.normalizeNode = (nodeEntry) => {
@@ -2781,17 +3248,26 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2781
3248
  "Merging spans",
2782
3249
  JSON.stringify(child, null, 2),
2783
3250
  JSON.stringify(nextNode, null, 2)
2784
- ), slate.Transforms.mergeNodes(editor, { at: [childPath[0], childPath[1] + 1], voids: !0 });
3251
+ ), slate.Transforms.mergeNodes(editor, {
3252
+ at: [childPath[0], childPath[1] + 1],
3253
+ voids: !0
3254
+ });
2785
3255
  return;
2786
3256
  }
2787
3257
  }
2788
3258
  }
3259
+ if (editor.isTextBlock(node) && !Array.isArray(node.markDefs)) {
3260
+ debug$c("Adding .markDefs to block node"), slate.Transforms.setNodes(editor, { markDefs: [] }, { at: path });
3261
+ return;
3262
+ }
2789
3263
  if (editor.isTextSpan(node) && !Array.isArray(node.marks)) {
2790
3264
  debug$c("Adding .marks to span node"), slate.Transforms.setNodes(editor, { marks: [] }, { at: path });
2791
3265
  return;
2792
3266
  }
2793
3267
  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));
3268
+ const blockPath = slate.Path.parent(path), [block] = slate.Editor.node(editor, blockPath), decorators2 = types2.decorators.map((decorator) => decorator.value), annotations = node.marks?.filter(
3269
+ (mark) => !decorators2.includes(mark)
3270
+ );
2795
3271
  if (editor.isTextBlock(block) && node.text === "" && annotations && annotations.length > 0) {
2796
3272
  debug$c("Removing annotations from empty span node"), slate.Transforms.setNodes(
2797
3273
  editor,
@@ -2809,7 +3285,11 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2809
3285
  if (orphanedAnnotations.length > 0) {
2810
3286
  debug$c("Removing orphaned annotations from span node"), slate.Transforms.setNodes(
2811
3287
  editor,
2812
- { marks: marks.filter((mark) => !orphanedAnnotations.includes(mark)) },
3288
+ {
3289
+ marks: marks.filter(
3290
+ (mark) => !orphanedAnnotations.includes(mark)
3291
+ )
3292
+ },
2813
3293
  { at: childPath }
2814
3294
  );
2815
3295
  return;
@@ -2819,17 +3299,29 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2819
3299
  if (editor.isTextSpan(node)) {
2820
3300
  const blockPath = slate.Path.parent(path), [block] = slate.Editor.node(editor, blockPath);
2821
3301
  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));
3302
+ const decorators2 = types2.decorators.map(
3303
+ (decorator) => decorator.value
3304
+ ), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
2823
3305
  if (orphanedAnnotations.length > 0) {
2824
3306
  debug$c("Removing orphaned annotations from span node"), slate.Transforms.setNodes(
2825
3307
  editor,
2826
- { marks: marks.filter((mark) => !orphanedAnnotations.includes(mark)) },
3308
+ {
3309
+ marks: marks.filter(
3310
+ (mark) => !orphanedAnnotations.includes(mark)
3311
+ )
3312
+ },
2827
3313
  { at: path }
2828
3314
  );
2829
3315
  return;
2830
3316
  }
2831
3317
  }
2832
3318
  }
3319
+ if (editor.isTextBlock(node)) {
3320
+ const markDefs = node.markDefs ?? [], markDefKeys = /* @__PURE__ */ new Set(), newMarkDefs = [];
3321
+ for (const markDef of markDefs)
3322
+ markDefKeys.has(markDef._key) || (markDefKeys.add(markDef._key), newMarkDefs.push(markDef));
3323
+ markDefs.length !== newMarkDefs.length && (debug$c("Removing duplicate markDefs"), slate.Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: path }));
3324
+ }
2833
3325
  if (editor.isTextBlock(node) && !editor.operations.some(
2834
3326
  (op) => op.type === "merge_node" && "markDefs" in op.properties && op.path.length === 1
2835
3327
  )) {
@@ -2857,7 +3349,9 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2857
3349
  }
2858
3350
  if (op.type === "insert_text") {
2859
3351
  const { selection } = editor;
2860
- if (selection && slate.Range.isCollapsed(selection) && slate.Editor.marks(editor)?.marks?.some((mark) => !decorators.includes(mark))) {
3352
+ if (selection && slate.Range.isCollapsed(selection) && slate.Editor.marks(editor)?.marks?.some(
3353
+ (mark) => !decorators.includes(mark)
3354
+ )) {
2861
3355
  const [node] = Array.from(
2862
3356
  slate.Editor.nodes(editor, {
2863
3357
  mode: "lowest",
@@ -2908,7 +3402,11 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2908
3402
  ...slate.Editor.marks(editor) || {}
2909
3403
  }.marks || []).filter((mark) => decorators.includes(mark));
2910
3404
  slate.Editor.withoutNormalizing(editor, () => {
2911
- apply2(op), slate.Transforms.setNodes(editor, { marks: marksWithoutAnnotationMarks }, { at: op.path });
3405
+ apply2(op), slate.Transforms.setNodes(
3406
+ editor,
3407
+ { marks: marksWithoutAnnotationMarks },
3408
+ { at: op.path }
3409
+ );
2912
3410
  }), editor.onChange();
2913
3411
  return;
2914
3412
  }
@@ -2924,7 +3422,11 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2924
3422
  const [targetBlock, targetPath] = slate.Editor.node(editor, [op.path[0] - 1]);
2925
3423
  if (editor.isTextBlock(targetBlock)) {
2926
3424
  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);
3425
+ debug$c("Copying markDefs over to merged block", op), slate.Transforms.setNodes(
3426
+ editor,
3427
+ { markDefs: newMarkDefs },
3428
+ { at: targetPath, voids: !1 }
3429
+ ), apply2(op);
2928
3430
  return;
2929
3431
  }
2930
3432
  }
@@ -2933,8 +3435,17 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2933
3435
  if (editor.selection) {
2934
3436
  if (slate.Range.isExpanded(editor.selection))
2935
3437
  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 })] : [];
3438
+ slate.Transforms.setNodes(
3439
+ editor,
3440
+ {},
3441
+ { match: slate.Text.isText, split: !0, hanging: !0 }
3442
+ );
3443
+ const splitTextNodes = slate.Range.isRange(editor.selection) ? [
3444
+ ...slate.Editor.nodes(editor, {
3445
+ at: editor.selection,
3446
+ match: slate.Text.isText
3447
+ })
3448
+ ] : [];
2938
3449
  splitTextNodes.length > 1 && splitTextNodes.every((node) => node[0].marks?.includes(mark)) ? editor.removeMark(mark) : splitTextNodes.forEach(([node, path]) => {
2939
3450
  const marks = [
2940
3451
  ...(Array.isArray(node.marks) ? node.marks : []).filter(
@@ -2950,13 +3461,32 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2950
3461
  });
2951
3462
  });
2952
3463
  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;
3464
+ const [block, blockPath] = slate.Editor.node(editor, editor.selection, {
3465
+ depth: 1
3466
+ }), lonelyEmptySpan = editor.isTextBlock(block) && block.children.length === 1 && editor.isTextSpan(block.children[0]) && block.children[0].text === "" ? block.children[0] : void 0;
3467
+ if (lonelyEmptySpan) {
3468
+ const existingMarks = lonelyEmptySpan.marks ?? [], existingMarksWithoutDecorator = existingMarks.filter(
3469
+ (existingMark) => existingMark !== mark
3470
+ );
3471
+ slate.Transforms.setNodes(
3472
+ editor,
3473
+ {
3474
+ marks: existingMarks.length === existingMarksWithoutDecorator.length ? [...existingMarks, mark] : existingMarksWithoutDecorator
3475
+ },
3476
+ {
3477
+ at: blockPath,
3478
+ match: (node) => editor.isTextSpan(node)
3479
+ }
3480
+ );
3481
+ } else {
3482
+ const existingMarks = {
3483
+ ...slate.Editor.marks(editor) || {}
3484
+ }.marks || [], marks = {
3485
+ ...slate.Editor.marks(editor) || {},
3486
+ marks: [...existingMarks, mark]
3487
+ };
3488
+ return editor.marks = marks, forceNewSelection(), editor;
3489
+ }
2960
3490
  }
2961
3491
  editor.onChange(), forceNewSelection();
2962
3492
  }
@@ -2966,16 +3496,21 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2966
3496
  if (selection) {
2967
3497
  if (slate.Range.isExpanded(selection))
2968
3498
  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 })
3499
+ slate.Transforms.setNodes(
3500
+ editor,
3501
+ {},
3502
+ { match: slate.Text.isText, split: !0, hanging: !0 }
3503
+ ), editor.selection && [
3504
+ ...slate.Editor.nodes(editor, {
3505
+ at: editor.selection,
3506
+ match: slate.Text.isText
3507
+ })
2971
3508
  ].forEach(([node, path]) => {
2972
3509
  const block = editor.children[path[0]];
2973
3510
  slate.Element.isElement(block) && block.children.includes(node) && slate.Transforms.setNodes(
2974
3511
  editor,
2975
3512
  {
2976
- marks: (Array.isArray(node.marks) ? node.marks : []).filter(
2977
- (eMark) => eMark !== mark
2978
- ),
3513
+ marks: (Array.isArray(node.marks) ? node.marks : []).filter((eMark) => eMark !== mark),
2979
3514
  _type: "span"
2980
3515
  },
2981
3516
  { at: path }
@@ -2983,13 +3518,32 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2983
3518
  });
2984
3519
  }), slate.Editor.normalize(editor);
2985
3520
  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;
3521
+ const [block, blockPath] = slate.Editor.node(editor, selection, {
3522
+ depth: 1
3523
+ }), lonelyEmptySpan = editor.isTextBlock(block) && block.children.length === 1 && editor.isTextSpan(block.children[0]) && block.children[0].text === "" ? block.children[0] : void 0;
3524
+ if (lonelyEmptySpan) {
3525
+ const existingMarksWithoutDecorator = (lonelyEmptySpan.marks ?? []).filter(
3526
+ (existingMark) => existingMark !== mark
3527
+ );
3528
+ slate.Transforms.setNodes(
3529
+ editor,
3530
+ {
3531
+ marks: existingMarksWithoutDecorator
3532
+ },
3533
+ {
3534
+ at: blockPath,
3535
+ match: (node) => editor.isTextSpan(node)
3536
+ }
3537
+ );
3538
+ } else {
3539
+ const existingMarks = {
3540
+ ...slate.Editor.marks(editor) || {}
3541
+ }.marks || [], marks = {
3542
+ ...slate.Editor.marks(editor) || {},
3543
+ marks: existingMarks.filter((eMark) => eMark !== mark)
3544
+ };
3545
+ return editor.marks = { marks: marks.marks, _type: "span" }, forceNewSelection(), editor;
3546
+ }
2993
3547
  }
2994
3548
  editor.onChange(), forceNewSelection();
2995
3549
  }
@@ -3047,14 +3601,18 @@ function createWithSchemaTypes({
3047
3601
  keyGenerator
3048
3602
  }) {
3049
3603
  return function(editor) {
3050
- editor.isTextBlock = (value) => types.isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => types.isPortableTextSpan(value) && value._type == schemaTypes.span.name, editor.isListBlock = (value) => types.isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
3604
+ editor.isTextBlock = (value) => types.isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => types.isPortableTextSpan(value) && value._type === schemaTypes.span.name, editor.isListBlock = (value) => types.isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
3051
3605
  const { normalizeNode } = editor;
3052
3606
  return editor.normalizeNode = (entry) => {
3053
3607
  const [node, path] = entry;
3054
3608
  if (node._type === void 0 && path.length === 2) {
3055
3609
  debug$a("Setting span type on text node without a type");
3056
3610
  const span = node, key = span._key || keyGenerator();
3057
- slate.Transforms.setNodes(editor, { ...span, _type: schemaTypes.span.name, _key: key }, { at: path });
3611
+ slate.Transforms.setNodes(
3612
+ editor,
3613
+ { ...span, _type: schemaTypes.span.name, _key: key },
3614
+ { at: path }
3615
+ );
3058
3616
  }
3059
3617
  if (node._key === void 0 && (path.length === 1 || path.length === 2)) {
3060
3618
  debug$a("Setting missing key on child node without a key");
@@ -3066,7 +3624,11 @@ function createWithSchemaTypes({
3066
3624
  };
3067
3625
  }
3068
3626
  const debug$9 = debugWithName("plugin:withUtils");
3069
- function createWithUtils({ schemaTypes, keyGenerator, portableTextEditor }) {
3627
+ function createWithUtils({
3628
+ schemaTypes,
3629
+ keyGenerator,
3630
+ portableTextEditor
3631
+ }) {
3070
3632
  return function(editor) {
3071
3633
  return editor.pteExpandToWord = () => {
3072
3634
  const { selection } = editor;
@@ -3077,7 +3639,7 @@ function createWithUtils({ schemaTypes, keyGenerator, portableTextEditor }) {
3077
3639
  return;
3078
3640
  }
3079
3641
  const { focus } = selection, focusOffset = focus.offset, charsBefore = textNode.text.slice(0, focusOffset), charsAfter = textNode.text.slice(focusOffset, -1), isEmpty = (str) => str.match(/\s/g), whiteSpaceBeforeIndex = charsBefore.split("").reverse().findIndex((str) => isEmpty(str)), newStartOffset = whiteSpaceBeforeIndex > -1 ? charsBefore.length - whiteSpaceBeforeIndex : 0, whiteSpaceAfterIndex = charsAfter.split("").findIndex((obj) => isEmpty(obj)), newEndOffset = charsBefore.length + (whiteSpaceAfterIndex > -1 ? whiteSpaceAfterIndex : charsAfter.length + 1);
3080
- if (!(newStartOffset === newEndOffset || isNaN(newStartOffset) || isNaN(newEndOffset))) {
3642
+ if (!(newStartOffset === newEndOffset || Number.isNaN(newStartOffset) || Number.isNaN(newEndOffset))) {
3081
3643
  debug$9("pteExpandToWord: Expanding to focused word"), slate.Transforms.setSelection(editor, {
3082
3644
  anchor: { ...selection.anchor, offset: newStartOffset },
3083
3645
  focus: { ...selection.focus, offset: newEndOffset }
@@ -3086,7 +3648,7 @@ function createWithUtils({ schemaTypes, keyGenerator, portableTextEditor }) {
3086
3648
  }
3087
3649
  debug$9("pteExpandToWord: Can't expand to word here");
3088
3650
  }
3089
- }, editor.pteCreateEmptyBlock = () => toSlateValue(
3651
+ }, editor.pteCreateTextBlock = (options) => toSlateValue(
3090
3652
  [
3091
3653
  {
3092
3654
  _type: schemaTypes.block.name,
@@ -3098,7 +3660,9 @@ function createWithUtils({ schemaTypes, keyGenerator, portableTextEditor }) {
3098
3660
  _type: "span",
3099
3661
  _key: keyGenerator(),
3100
3662
  text: "",
3101
- marks: []
3663
+ marks: options.decorators.filter(
3664
+ (decorator) => schemaTypes.decorators.find(({ value }) => value === decorator)
3665
+ )
3102
3666
  }
3103
3667
  ]
3104
3668
  }
@@ -3147,59 +3711,46 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3147
3711
  }
3148
3712
  }
3149
3713
  });
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);
3714
+ 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
3715
  if (isArrowDown && editor.selection) {
3152
- const focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
3716
+ const focusBlock = slate.Node.descendant(
3717
+ editor,
3718
+ editor.selection.focus.path.slice(0, 1)
3719
+ );
3153
3720
  if (focusBlock && slate.Editor.isVoid(editor, focusBlock)) {
3154
3721
  const nextPath = slate.Path.next(editor.selection.focus.path.slice(0, 1));
3155
3722
  if (!slate.Node.has(editor, nextPath)) {
3156
- slate.Transforms.insertNodes(editor, editor.pteCreateEmptyBlock(), { at: nextPath }), editor.onChange();
3723
+ slate.Transforms.insertNodes(
3724
+ editor,
3725
+ editor.pteCreateTextBlock({ decorators: [] }),
3726
+ {
3727
+ at: nextPath
3728
+ }
3729
+ ), editor.onChange();
3157
3730
  return;
3158
3731
  }
3159
3732
  }
3160
3733
  }
3161
3734
  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(
3735
+ const isFirstBlock = editor.selection.focus.path[0] === 0, focusBlock = slate.Node.descendant(
3193
3736
  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();
3737
+ editor.selection.focus.path.slice(0, 1)
3738
+ );
3739
+ if (isFirstBlock && focusBlock && slate.Editor.isVoid(editor, focusBlock)) {
3740
+ slate.Transforms.insertNodes(
3741
+ editor,
3742
+ editor.pteCreateTextBlock({ decorators: [] }),
3743
+ {
3744
+ at: [0]
3745
+ }
3746
+ ), slate.Transforms.select(editor, { path: [0, 0], offset: 0 }), editor.onChange();
3198
3747
  return;
3199
3748
  }
3200
3749
  }
3201
3750
  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(
3751
+ const [focusChild] = slate.Editor.node(editor, editor.selection.focus, {
3752
+ depth: 2
3753
+ }), [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
3754
  (m) => (focusBlock.markDefs || []).map((def) => def._key).includes(m)
3204
3755
  ).length > 0, [start] = slate.Range.edges(editor.selection), atStartOfNode = slate.Editor.isStart(editor, start, start.path);
3205
3756
  focusChild && types.isPortableTextSpan(focusChild) && (!hasAnnotationFocus || atStartOfNode) && editor.pteIncrementBlockLevels(isShiftTab) && event.preventDefault();
@@ -3213,12 +3764,15 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3213
3764
  if (editor.isTextBlock(focusBlock) && focusBlock.style && focusBlock.style !== types$1.styles[0].value) {
3214
3765
  const [, end] = slate.Range.edges(editor.selection);
3215
3766
  if (slate.Editor.isEnd(editor, end, end.path)) {
3216
- slate.Editor.insertNode(editor, editor.pteCreateEmptyBlock()), event.preventDefault(), editor.onChange();
3767
+ slate.Editor.insertNode(
3768
+ editor,
3769
+ editor.pteCreateTextBlock({ decorators: [] })
3770
+ ), event.preventDefault(), editor.onChange();
3217
3771
  return;
3218
3772
  }
3219
3773
  }
3220
3774
  if (focusBlock && slate.Editor.isVoid(editor, focusBlock)) {
3221
- slate.Editor.insertNode(editor, editor.pteCreateEmptyBlock()), event.preventDefault(), editor.onChange();
3775
+ slate.Editor.insertNode(editor, editor.pteCreateTextBlock({ decorators: [] })), event.preventDefault(), editor.onChange();
3222
3776
  return;
3223
3777
  }
3224
3778
  event.preventDefault(), editor.insertBreak(), editor.onChange();
@@ -3238,7 +3792,13 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3238
3792
  }
3239
3793
  function validateValue(value, types$1, keyGenerator) {
3240
3794
  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)];
3795
+ const validChildTypes = [
3796
+ types$1.span.name,
3797
+ ...types$1.inlineObjects.map((t) => t.name)
3798
+ ], validBlockTypes = [
3799
+ types$1.block.name,
3800
+ ...types$1.blockObjects.map((t) => t.name)
3801
+ ];
3242
3802
  return value === void 0 ? { valid: !0, resolution: null, value } : !Array.isArray(value) || value.length === 0 ? {
3243
3803
  valid: !1,
3244
3804
  resolution: {
@@ -3281,7 +3841,9 @@ function validateValue(value, types$1, keyGenerator) {
3281
3841
  if (blk._type === "block") {
3282
3842
  const currentBlockTypeName = types$1.block.name;
3283
3843
  return resolution = {
3284
- patches: [patches.set({ ...blk, _type: currentBlockTypeName }, [{ _key: blk._key }])],
3844
+ patches: [
3845
+ patches.set({ ...blk, _type: currentBlockTypeName }, [{ _key: blk._key }])
3846
+ ],
3285
3847
  description: `Block with _key '${blk._key}' has invalid type name '${blk._type}'. According to the schema, the block type name is '${currentBlockTypeName}'`,
3286
3848
  action: `Use type '${currentBlockTypeName}'`,
3287
3849
  item: blk,
@@ -3293,7 +3855,9 @@ function validateValue(value, types$1, keyGenerator) {
3293
3855
  }, !0;
3294
3856
  }
3295
3857
  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 }])],
3858
+ patches: [
3859
+ patches.set({ ...blk, _type: types$1.block.name }, [{ _key: blk._key }])
3860
+ ],
3297
3861
  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
3862
  action: `Use type '${types$1.block.name}'`,
3299
3863
  item: blk,
@@ -3363,7 +3927,11 @@ function validateValue(value, types$1, keyGenerator) {
3363
3927
  }
3364
3928
  if (blk.markDefs && !Array.isArray(blk.markDefs))
3365
3929
  return resolution = {
3366
- patches: [patches.set({ ...textBlock, markDefs: EMPTY_MARKDEFS }, [{ _key: textBlock._key }])],
3930
+ patches: [
3931
+ patches.set({ ...textBlock, markDefs: EMPTY_MARKDEFS }, [
3932
+ { _key: textBlock._key }
3933
+ ])
3934
+ ],
3367
3935
  description: "Block has invalid required property 'markDefs'.",
3368
3936
  action: "Add empty markDefs array",
3369
3937
  item: textBlock,
@@ -3396,7 +3964,10 @@ function validateValue(value, types$1, keyGenerator) {
3396
3964
  i18n: {
3397
3965
  description: "inputs.portable-text.invalid-value.orphaned-mark-defs.description",
3398
3966
  action: "inputs.portable-text.invalid-value.orphaned-mark-defs.action",
3399
- values: { key: blk._key, unusedMarkDefs: unusedMarkDefs.map((m) => m.toString()) }
3967
+ values: {
3968
+ key: blk._key,
3969
+ unusedMarkDefs: unusedMarkDefs.map((m) => m.toString())
3970
+ }
3400
3971
  }
3401
3972
  }, !0;
3402
3973
  }
@@ -3414,7 +3985,9 @@ function validateValue(value, types$1, keyGenerator) {
3414
3985
  return resolution = {
3415
3986
  autoResolve: !0,
3416
3987
  patches: spanChildren.map((child) => patches.set(
3417
- (child.marks || []).filter((cMrk) => !orphanedMarks.includes(cMrk)),
3988
+ (child.marks || []).filter(
3989
+ (cMrk) => !orphanedMarks.includes(cMrk)
3990
+ ),
3418
3991
  [{ _key: blk._key }, "children", { _key: child._key }, "marks"]
3419
3992
  )),
3420
3993
  description: `Block with _key '${blk._key}' contains marks (${orphaned}) not supported by the current content model.`,
@@ -3423,7 +3996,10 @@ function validateValue(value, types$1, keyGenerator) {
3423
3996
  i18n: {
3424
3997
  description: "inputs.portable-text.invalid-value.orphaned-marks.description",
3425
3998
  action: "inputs.portable-text.invalid-value.orphaned-marks.action",
3426
- values: { key: blk._key, orphanedMarks: orphanedMarks.map((m) => m.toString()) }
3999
+ values: {
4000
+ key: blk._key,
4001
+ orphanedMarks: orphanedMarks.map((m) => m.toString())
4002
+ }
3427
4003
  }
3428
4004
  }, !0;
3429
4005
  }
@@ -3445,7 +4021,9 @@ function validateValue(value, types$1, keyGenerator) {
3445
4021
  const newChild = { ...child, _key: keyGenerator() };
3446
4022
  return resolution = {
3447
4023
  autoResolve: !0,
3448
- patches: [patches.set(newChild, [{ _key: blk._key }, "children", cIndex])],
4024
+ patches: [
4025
+ patches.set(newChild, [{ _key: blk._key }, "children", cIndex])
4026
+ ],
3449
4027
  description: `Child at index ${cIndex} is missing required _key in block with _key ${blk._key}.`,
3450
4028
  action: "Set a new random _key on the object",
3451
4029
  item: blk,
@@ -3458,7 +4036,11 @@ function validateValue(value, types$1, keyGenerator) {
3458
4036
  }
3459
4037
  return child._type ? validChildTypes.includes(child._type) ? child._type === types$1.span.name && typeof child.text != "string" ? (resolution = {
3460
4038
  patches: [
3461
- patches.set({ ...child, text: "" }, [{ _key: blk._key }, "children", { _key: child._key }])
4039
+ patches.set({ ...child, text: "" }, [
4040
+ { _key: blk._key },
4041
+ "children",
4042
+ { _key: child._key }
4043
+ ])
3462
4044
  ],
3463
4045
  description: `Child with _key '${child._key}' in block with key '${blk._key}' has missing or invalid text property!`,
3464
4046
  action: "Write an empty text property to the object",
@@ -3469,17 +4051,25 @@ function validateValue(value, types$1, keyGenerator) {
3469
4051
  values: { key: blk._key, childKey: child._key }
3470
4052
  }
3471
4053
  }, !0) : !1 : (resolution = {
3472
- patches: [patches.unset([{ _key: blk._key }, "children", { _key: child._key }])],
4054
+ patches: [
4055
+ patches.unset([{ _key: blk._key }, "children", { _key: child._key }])
4056
+ ],
3473
4057
  description: `Child with _key '${child._key}' in block with key '${blk._key}' has invalid '_type' property (${child._type}).`,
3474
4058
  action: "Remove the object",
3475
4059
  item: blk,
3476
4060
  i18n: {
3477
4061
  description: "inputs.portable-text.invalid-value.disallowed-child-type.description",
3478
4062
  action: "inputs.portable-text.invalid-value.disallowed-child-type.action",
3479
- values: { key: blk._key, childKey: child._key, childType: child._type }
4063
+ values: {
4064
+ key: blk._key,
4065
+ childKey: child._key,
4066
+ childType: child._type
4067
+ }
3480
4068
  }
3481
4069
  }, !0) : (resolution = {
3482
- patches: [patches.unset([{ _key: blk._key }, "children", { _key: child._key }])],
4070
+ patches: [
4071
+ patches.unset([{ _key: blk._key }, "children", { _key: child._key }])
4072
+ ],
3483
4073
  description: `Child with _key '${child._key}' in block with key '${blk._key}' is missing '_type' property.`,
3484
4074
  action: "Remove the object",
3485
4075
  item: blk,
@@ -3513,11 +4103,13 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3513
4103
  const [voidNode] = endVoid, r = domRange.cloneRange(), domNode = slateReact.ReactEditor.toDOMNode(editor, voidNode);
3514
4104
  r.setEndAfter(domNode), contents = r.cloneContents();
3515
4105
  }
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 ? `
4106
+ Array.from(contents.querySelectorAll("[data-slate-zero-width]")).forEach(
4107
+ (zw) => {
4108
+ const isNewline = zw.getAttribute("data-slate-zero-width") === "n";
4109
+ zw.textContent = isNewline ? `
3519
4110
  ` : "";
3520
- }), Array.from(contents.querySelectorAll("*")).forEach((elm) => {
4111
+ }
4112
+ ), Array.from(contents.querySelectorAll("*")).forEach((elm) => {
3521
4113
  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
4114
  for (const key in elm.attributes)
3523
4115
  elm.hasAttribute(key) && elm.removeAttribute(key);
@@ -3527,7 +4119,10 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3527
4119
  const asHTML = div.innerHTML;
3528
4120
  contents.ownerDocument.body.removeChild(div);
3529
4121
  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);
4122
+ 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(
4123
+ "application/x-portable-text-event-origin",
4124
+ originEvent || "external"
4125
+ ), debug$7("Set fragment data", asJSON, asHTML);
3531
4126
  }, editor.insertPortableTextData = (data) => {
3532
4127
  if (!editor.selection)
3533
4128
  return !1;
@@ -3567,7 +4162,9 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3567
4162
  if (html) {
3568
4163
  if (portableText = blockTools.htmlToBlocks(html, schemaTypes.portableText, {
3569
4164
  unstable_whitespaceOnPasteMode: whitespaceOnPasteMode
3570
- }).map((block) => blockTools.normalizeBlock(block, { blockTypeName })), fragment = toSlateValue(portableText, { schemaTypes }), insertedType = "HTML", portableText.length === 0)
4165
+ }).map(
4166
+ (block) => blockTools.normalizeBlock(block, { blockTypeName })
4167
+ ), fragment = toSlateValue(portableText, { schemaTypes }), insertedType = "HTML", portableText.length === 0)
3571
4168
  return !1;
3572
4169
  } else {
3573
4170
  const textToHtml = `<html><body>${escapeHtml(text).split(/\n{2,}/).map(
@@ -3579,7 +4176,11 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3579
4176
  schemaTypes
3580
4177
  }), insertedType = "text";
3581
4178
  }
3582
- const validation = validateValue(portableText, schemaTypes, keyGenerator);
4179
+ const validation = validateValue(
4180
+ portableText,
4181
+ schemaTypes,
4182
+ keyGenerator
4183
+ );
3583
4184
  if (!validation.valid) {
3584
4185
  const errorDescription = `Could not validate the resulting portable text to insert.
3585
4186
  ${validation.resolution?.description}
@@ -3592,7 +4193,9 @@ Try to insert as plain text (shift-paste) instead.`;
3592
4193
  data: validation
3593
4194
  }), debug$7("Invalid insert result", validation), !1;
3594
4195
  }
3595
- return debug$7(`Inserting ${insertedType} fragment at ${JSON.stringify(editor.selection)}`), _insertFragment(editor, fragment, schemaTypes), change$.next({ type: "loading", isLoading: !1 }), !0;
4196
+ return debug$7(
4197
+ `Inserting ${insertedType} fragment at ${JSON.stringify(editor.selection)}`
4198
+ ), _insertFragment(editor, fragment, schemaTypes), change$.next({ type: "loading", isLoading: !1 }), !0;
3596
4199
  }
3597
4200
  return change$.next({ type: "loading", isLoading: !1 }), !1;
3598
4201
  }, editor.insertData = (data) => {
@@ -3638,10 +4241,7 @@ function _regenerateKeys(editor, fragment, keyGenerator, spanTypeName, editorTyp
3638
4241
  return newNode.children = newNode.children.map(
3639
4242
  (child) => child._type === spanTypeName && editor.isTextSpan(child) ? {
3640
4243
  ...child,
3641
- marks: child.marks && child.marks.includes(oldKey) ? (
3642
- // eslint-disable-next-line max-nested-callbacks
3643
- [...child.marks].filter((mark) => mark !== oldKey).concat(newKey)
3644
- ) : child.marks
4244
+ marks: child.marks && child.marks.includes(oldKey) ? [...child.marks].filter((mark) => mark !== oldKey).concat(newKey) : child.marks
3645
4245
  } : child
3646
4246
  ), { ...def, _key: newKey };
3647
4247
  });
@@ -3657,18 +4257,30 @@ function _insertFragment(editor, fragment, schemaTypes) {
3657
4257
  editor.withoutNormalizing(() => {
3658
4258
  if (!editor.selection)
3659
4259
  return;
3660
- const [focusBlock, focusPath] = slate.Editor.node(editor, editor.selection, { depth: 1 });
4260
+ const [focusBlock, focusPath] = slate.Editor.node(editor, editor.selection, {
4261
+ depth: 1
4262
+ });
3661
4263
  if (editor.isTextBlock(focusBlock) && editor.isTextBlock(fragment[0])) {
3662
4264
  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(
4265
+ debug$7(
4266
+ "Mixing markDefs of focusBlock and fragments[0] block",
4267
+ markDefs,
4268
+ fragment[0].markDefs
4269
+ ), isEqual__default.default(markDefs, fragment[0].markDefs) || slate.Transforms.setNodes(
3664
4270
  editor,
3665
4271
  {
3666
- markDefs: uniq__default.default([...fragment[0].markDefs || [], ...markDefs || []])
4272
+ markDefs: uniq__default.default([
4273
+ ...fragment[0].markDefs || [],
4274
+ ...markDefs || []
4275
+ ])
3667
4276
  },
3668
4277
  { at: focusPath, mode: "lowest", voids: !1 }
3669
4278
  );
3670
4279
  }
3671
- isEqualToEmptyEditor(editor.children, schemaTypes) ? (slate.Transforms.splitNodes(editor, { at: [0, 0] }), editor.insertFragment(fragment), slate.Transforms.removeNodes(editor, { at: [0] })) : editor.insertFragment(fragment);
4280
+ isEqualToEmptyEditor(
4281
+ editor.children,
4282
+ schemaTypes
4283
+ ) ? (slate.Transforms.splitNodes(editor, { at: [0, 0] }), editor.insertFragment(fragment), slate.Transforms.removeNodes(editor, { at: [0] })) : editor.insertFragment(fragment);
3672
4284
  }), editor.onChange();
3673
4285
  }
3674
4286
  const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, options) => {
@@ -3678,7 +4290,11 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
3678
4290
  onChange: e.onChange,
3679
4291
  normalizeNode: e.normalizeNode
3680
4292
  });
3681
- const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(schemaTypes, keyGenerator), withSchemaTypes = createWithSchemaTypes({ schemaTypes, keyGenerator }), withEditableAPI = createWithEditableAPI(portableTextEditor, schemaTypes, keyGenerator), withPatches = createWithPatches({
4293
+ const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(schemaTypes, keyGenerator), withSchemaTypes = createWithSchemaTypes({ schemaTypes, keyGenerator }), withEditableAPI = createWithEditableAPI(
4294
+ portableTextEditor,
4295
+ schemaTypes,
4296
+ keyGenerator
4297
+ ), withPatches = createWithPatches({
3682
4298
  change$,
3683
4299
  keyGenerator,
3684
4300
  patches$,
@@ -3693,7 +4309,14 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
3693
4309
  schemaTypes,
3694
4310
  change$,
3695
4311
  keyGenerator
3696
- ), withPortableTextBlockStyle = createWithPortableTextBlockStyle(schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(schemaTypes), withUtils = createWithUtils({ keyGenerator, schemaTypes, portableTextEditor }), withPortableTextSelections = createWithPortableTextSelections(change$, schemaTypes);
4312
+ ), withPortableTextBlockStyle = createWithPortableTextBlockStyle(schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(schemaTypes, keyGenerator), withUtils = createWithUtils({
4313
+ keyGenerator,
4314
+ schemaTypes,
4315
+ portableTextEditor
4316
+ }), withPortableTextSelections = createWithPortableTextSelections(
4317
+ change$,
4318
+ schemaTypes
4319
+ );
3697
4320
  return e.destroy = () => {
3698
4321
  const originalFunctions = originalFnMap.get(e);
3699
4322
  if (!originalFunctions)
@@ -3707,7 +4330,9 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
3707
4330
  withUtils(
3708
4331
  withPlaceholderBlock(
3709
4332
  withPortableTextLists(
3710
- withPortableTextSelections(withEditableAPI(withInsertBreak(e)))
4333
+ withPortableTextSelections(
4334
+ withEditableAPI(withInsertBreak(e))
4335
+ )
3711
4336
  )
3712
4337
  )
3713
4338
  )
@@ -3726,7 +4351,11 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
3726
4351
  withUtils(
3727
4352
  withMaxBlocks(
3728
4353
  withUndoRedo(
3729
- withPatches(withPortableTextSelections(withEditableAPI(withInsertBreak(e))))
4354
+ withPatches(
4355
+ withPortableTextSelections(
4356
+ withEditableAPI(withInsertBreak(e))
4357
+ )
4358
+ )
3730
4359
  )
3731
4360
  )
3732
4361
  )
@@ -3773,8 +4402,15 @@ function SlateContainer(props) {
3773
4402
  portableTextEditor,
3774
4403
  readOnly
3775
4404
  });
3776
- }, [keyGenerator, portableTextEditor, maxBlocks, readOnly, patches$, slateEditor]);
3777
- const initialValue = react.useMemo(() => [slateEditor.pteCreateEmptyBlock()], [slateEditor]);
4405
+ }, [
4406
+ keyGenerator,
4407
+ portableTextEditor,
4408
+ maxBlocks,
4409
+ readOnly,
4410
+ patches$,
4411
+ slateEditor
4412
+ ]);
4413
+ const initialValue = react.useMemo(() => [slateEditor.pteCreateTextBlock({ decorators: [] })], [slateEditor]);
3778
4414
  return react.useEffect(() => () => {
3779
4415
  debug$6("Destroying Slate editor"), slateEditor.destroy();
3780
4416
  }, [slateEditor]), /* @__PURE__ */ jsxRuntime.jsx(slateReact.Slate, { editor: slateEditor, initialValue, children: props.children });
@@ -3831,7 +4467,11 @@ function useSyncValue(props) {
3831
4467
  slate.Transforms.removeNodes(slateEditor, {
3832
4468
  at: [childrenLength - 1 - index]
3833
4469
  });
3834
- }), slate.Transforms.insertNodes(slateEditor, slateEditor.pteCreateEmptyBlock(), { at: [0] }), hadSelection && slate.Transforms.select(slateEditor, [0, 0]);
4470
+ }), slate.Transforms.insertNodes(
4471
+ slateEditor,
4472
+ slateEditor.pteCreateTextBlock({ decorators: [] }),
4473
+ { at: [0] }
4474
+ ), hadSelection && slate.Transforms.select(slateEditor, [0, 0]);
3835
4475
  });
3836
4476
  });
3837
4477
  }), isChanged = !0), value && value.length > 0) {
@@ -3850,34 +4490,53 @@ function useSyncValue(props) {
3850
4490
  });
3851
4491
  isChanged = !0;
3852
4492
  }
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);
4493
+ slateValueFromProps.forEach(
4494
+ (currentBlock, currentBlockIndex) => {
4495
+ const oldBlock = slateEditor.children[currentBlockIndex];
4496
+ if (oldBlock && !isEqual__default.default(currentBlock, oldBlock) && isValid) {
4497
+ const validationValue = [value[currentBlockIndex]], validation = validateValue(
4498
+ validationValue,
4499
+ schemaTypes,
4500
+ keyGenerator
4501
+ );
4502
+ !validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !readOnly && previousValue.current && previousValue.current !== value && (console.warn(
4503
+ `${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`
4504
+ ), validation.resolution.patches.forEach((patch) => {
4505
+ change$.next({ type: "patch", patch });
4506
+ })), validation.valid || validation.resolution?.autoResolve ? (oldBlock._key === currentBlock._key ? (debug$5.enabled && debug$5("Updating block", oldBlock, currentBlock), _updateBlock(
4507
+ slateEditor,
4508
+ currentBlock,
4509
+ oldBlock,
4510
+ currentBlockIndex
4511
+ )) : (debug$5.enabled && debug$5("Replacing block", oldBlock, currentBlock), _replaceBlock(
4512
+ slateEditor,
4513
+ currentBlock,
4514
+ currentBlockIndex
4515
+ )), isChanged = !0) : (change$.next({
4516
+ type: "invalidValue",
4517
+ resolution: validation.resolution,
4518
+ value
4519
+ }), isValid = !1);
4520
+ }
4521
+ if (!oldBlock && isValid) {
4522
+ const validationValue = [value[currentBlockIndex]], validation = validateValue(
4523
+ validationValue,
4524
+ schemaTypes,
4525
+ keyGenerator
4526
+ );
4527
+ debug$5.enabled && debug$5(
4528
+ "Validating and inserting new block in the end of the value",
4529
+ currentBlock
4530
+ ), validation.valid || validation.resolution?.autoResolve ? slate.Transforms.insertNodes(slateEditor, currentBlock, {
4531
+ at: [currentBlockIndex]
4532
+ }) : (debug$5("Invalid", validation), change$.next({
4533
+ type: "invalidValue",
4534
+ resolution: validation.resolution,
4535
+ value
4536
+ }), isValid = !1);
4537
+ }
3866
4538
  }
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
- });
4539
+ );
3881
4540
  });
3882
4541
  });
3883
4542
  });
@@ -3927,41 +4586,53 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3927
4586
  at: [currentBlockIndex]
3928
4587
  }), slateEditor.isTextBlock(currentBlock) && slateEditor.isTextBlock(oldBlock)) {
3929
4588
  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
- }));
4589
+ currentBlock.children.length < oldBlockChildrenLength && Array.from(
4590
+ Array(oldBlockChildrenLength - currentBlock.children.length)
4591
+ ).forEach((_, index) => {
4592
+ const childIndex = oldBlockChildrenLength - 1 - index;
4593
+ childIndex > 0 && (debug$5("Removing child"), slate.Transforms.removeNodes(slateEditor, {
4594
+ at: [currentBlockIndex, childIndex]
4595
+ }));
4596
+ }), currentBlock.children.forEach(
4597
+ (currentBlockChild, currentBlockChildIndex) => {
4598
+ const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual__default.default(currentBlockChild, oldBlockChild), isTextChanged = !isEqual__default.default(
4599
+ currentBlockChild.text,
4600
+ oldBlockChild?.text
4601
+ ), path = [currentBlockIndex, currentBlockChildIndex];
4602
+ if (isChildChanged)
4603
+ if (currentBlockChild._key === oldBlockChild?._key) {
4604
+ debug$5("Updating changed child", currentBlockChild, oldBlockChild), slate.Transforms.setNodes(
4605
+ slateEditor,
4606
+ currentBlockChild,
4607
+ {
4608
+ at: path
4609
+ }
4610
+ );
4611
+ const isSpanNode = slate.Text.isText(currentBlockChild) && currentBlockChild._type === "span" && slate.Text.isText(oldBlockChild) && oldBlockChild._type === "span";
4612
+ isSpanNode && isTextChanged ? (slate.Transforms.delete(slateEditor, {
4613
+ at: {
4614
+ focus: { path, offset: 0 },
4615
+ anchor: { path, offset: oldBlockChild.text.length }
4616
+ }
4617
+ }), slate.Transforms.insertText(slateEditor, currentBlockChild.text, {
4618
+ at: path
4619
+ }), slateEditor.onChange()) : isSpanNode || (debug$5("Updating changed inline object child", currentBlockChild), slate.Transforms.setNodes(
4620
+ slateEditor,
4621
+ { _key: VOID_CHILD_KEY },
4622
+ {
4623
+ at: [...path, 0],
4624
+ voids: !0
4625
+ }
4626
+ ));
4627
+ } else oldBlockChild ? (debug$5("Replacing child", currentBlockChild), slate.Transforms.removeNodes(slateEditor, {
4628
+ at: [currentBlockIndex, currentBlockChildIndex]
4629
+ }), slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
4630
+ at: [currentBlockIndex, currentBlockChildIndex]
4631
+ }), slateEditor.onChange()) : oldBlockChild || (debug$5("Inserting new child", currentBlockChild), slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
4632
+ at: [currentBlockIndex, currentBlockChildIndex]
4633
+ }), slateEditor.onChange());
3936
4634
  }
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
- });
4635
+ );
3965
4636
  }
3966
4637
  }
3967
4638
  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 +4651,11 @@ function Synchronizer(props) {
3980
4651
  debug$4("Flushing pending patches"), debugVerbose$1 && debug$4(`Patches:
3981
4652
  ${JSON.stringify(pendingPatches.current, null, 2)}`);
3982
4653
  const snapshot = getValue();
3983
- change$.next({ type: "mutation", patches: pendingPatches.current, snapshot }), pendingPatches.current = [];
4654
+ change$.next({
4655
+ type: "mutation",
4656
+ patches: pendingPatches.current,
4657
+ snapshot
4658
+ }), pendingPatches.current = [];
3984
4659
  }
3985
4660
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !1);
3986
4661
  }, [slateEditor, getValue, change$]), onFlushPendingPatchesThrottled = react.useMemo(() => throttle__default.default(
@@ -4066,7 +4741,9 @@ class PortableTextEditor extends react.Component {
4066
4741
  constructor(props) {
4067
4742
  if (super(props), !props.schemaType)
4068
4743
  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(
4744
+ props.incomingPatches$ && console.warn(
4745
+ "The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"
4746
+ ), this.change$.next({ type: "loading", isLoading: !0 }), this.schemaTypes = getPortableTextMemberSchemaTypes(
4070
4747
  props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)
4071
4748
  );
4072
4749
  }
@@ -4083,7 +4760,7 @@ class PortableTextEditor extends react.Component {
4083
4760
  return this.editable.getValue();
4084
4761
  };
4085
4762
  render() {
4086
- const { onChange, value, children, patches$, incomingPatches$ } = this.props, { change$ } = this, _patches$ = incomingPatches$ || patches$, maxBlocks = typeof this.props.maxBlocks > "u" ? void 0 : parseInt(this.props.maxBlocks.toString(), 10) || void 0, readOnly = !!this.props.readOnly, keyGenerator = this.props.keyGenerator || defaultKeyGenerator;
4763
+ const { onChange, value, children, patches$, incomingPatches$ } = this.props, { change$ } = this, _patches$ = incomingPatches$ || patches$, maxBlocks = typeof this.props.maxBlocks > "u" ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10) || void 0, readOnly = !!this.props.readOnly, keyGenerator = this.props.keyGenerator || defaultKeyGenerator;
4087
4764
  return /* @__PURE__ */ jsxRuntime.jsx(
4088
4765
  SlateContainer,
4089
4766
  {
@@ -4158,14 +4835,26 @@ class PortableTextEditor extends react.Component {
4158
4835
  static isSelectionsOverlapping = (editor, selectionA, selectionB) => editor.editable?.isSelectionsOverlapping(selectionA, selectionB);
4159
4836
  }
4160
4837
  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(
4838
+ const {
4839
+ attributes,
4840
+ children,
4841
+ leaf,
4842
+ schemaTypes,
4843
+ renderChild,
4844
+ renderDecorator,
4845
+ renderAnnotation
4846
+ } = 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
4847
  () => block ? [{ _key: block?._key }, "children", { _key: leaf._key }] : [],
4163
4848
  [block, leaf._key]
4164
4849
  ), decoratorValues = react.useMemo(
4165
4850
  () => schemaTypes.decorators.map((dec) => dec.value),
4166
4851
  [schemaTypes.decorators]
4167
4852
  ), marks = react.useMemo(
4168
- () => uniq__default.default((leaf.marks || EMPTY_MARKS).filter((mark) => decoratorValues.includes(mark))),
4853
+ () => uniq__default.default(
4854
+ (leaf.marks || EMPTY_MARKS).filter(
4855
+ (mark) => decoratorValues.includes(mark)
4856
+ )
4857
+ ),
4169
4858
  [decoratorValues, leaf.marks]
4170
4859
  ), annotationMarks = Array.isArray(leaf.marks) ? leaf.marks : EMPTY_MARKS, annotations = react.useMemo(
4171
4860
  () => annotationMarks.map(
@@ -4216,11 +4905,18 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4216
4905
  return () => {
4217
4906
  sub.unsubscribe();
4218
4907
  };
4219
- }, [path, portableTextEditor, setSelectedFromRange, shouldTrackSelectionAndFocus]), react.useEffect(() => setSelectedFromRange(), [setSelectedFromRange]);
4908
+ }, [
4909
+ path,
4910
+ portableTextEditor,
4911
+ setSelectedFromRange,
4912
+ shouldTrackSelectionAndFocus
4913
+ ]), react.useEffect(() => setSelectedFromRange(), [setSelectedFromRange]);
4220
4914
  const content2 = react.useMemo(() => {
4221
4915
  let returnedChildren = children;
4222
4916
  if (slate.Text.isText(leaf) && leaf._type === schemaTypes.span.name && (marks.forEach((mark) => {
4223
- const schemaType = schemaTypes.decorators.find((dec) => dec.value === mark);
4917
+ const schemaType = schemaTypes.decorators.find(
4918
+ (dec) => dec.value === mark
4919
+ );
4224
4920
  if (schemaType && renderDecorator) {
4225
4921
  const _props = Object.defineProperty(
4226
4922
  {
@@ -4236,14 +4932,20 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4236
4932
  {
4237
4933
  enumerable: !1,
4238
4934
  get() {
4239
- return console.warn("Property 'type' is deprecated, use 'schemaType' instead."), schemaType;
4935
+ return console.warn(
4936
+ "Property 'type' is deprecated, use 'schemaType' instead."
4937
+ ), schemaType;
4240
4938
  }
4241
4939
  }
4242
4940
  );
4243
- returnedChildren = renderDecorator(_props);
4941
+ returnedChildren = renderDecorator(
4942
+ _props
4943
+ );
4244
4944
  }
4245
4945
  }), block && annotations.length > 0 && annotations.forEach((annotation) => {
4246
- const schemaType = schemaTypes.annotations.find((t) => t.name === annotation._type);
4946
+ const schemaType = schemaTypes.annotations.find(
4947
+ (t) => t.name === annotation._type
4948
+ );
4247
4949
  if (schemaType)
4248
4950
  if (renderAnnotation) {
4249
4951
  const _props = Object.defineProperty(
@@ -4261,7 +4963,9 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4261
4963
  {
4262
4964
  enumerable: !1,
4263
4965
  get() {
4264
- return console.warn("Property 'type' is deprecated, use 'schemaType' instead."), schemaType;
4966
+ return console.warn(
4967
+ "Property 'type' is deprecated, use 'schemaType' instead."
4968
+ ), schemaType;
4265
4969
  }
4266
4970
  }
4267
4971
  );
@@ -4286,7 +4990,9 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4286
4990
  {
4287
4991
  enumerable: !1,
4288
4992
  get() {
4289
- return console.warn("Property 'type' is deprecated, use 'schemaType' instead."), schemaTypes.span;
4993
+ return console.warn(
4994
+ "Property 'type' is deprecated, use 'schemaType' instead."
4995
+ ), schemaTypes.span;
4290
4996
  }
4291
4997
  }
4292
4998
  );
@@ -4341,8 +5047,13 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4341
5047
  scrollSelectionIntoView,
4342
5048
  spellCheck,
4343
5049
  ...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);
5050
+ } = props, portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), keyGenerator = usePortableTextEditorKeyGenerator(), ref = react.useRef(null), [editableElement, setEditableElement] = react.useState(
5051
+ null
5052
+ ), [hasInvalidValue, setHasInvalidValue] = react.useState(!1), [rangeDecorationState, setRangeDecorationsState] = react.useState([]);
5053
+ react.useImperativeHandle(
5054
+ forwardedRef,
5055
+ () => ref.current
5056
+ );
4346
5057
  const rangeDecorationsRef = react.useRef(rangeDecorations), { change$, schemaTypes } = portableTextEditor, slateEditor = slateReact.useSlate(), blockTypeName = schemaTypes.block.name, withInsertData = react.useMemo(
4347
5058
  () => createWithInsertData(change$, schemaTypes, keyGenerator),
4348
5059
  [change$, keyGenerator, schemaTypes]
@@ -4365,7 +5076,15 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4365
5076
  spellCheck
4366
5077
  }
4367
5078
  ),
4368
- [schemaTypes, spellCheck, readOnly, renderBlock, renderChild, renderListItem, renderStyle]
5079
+ [
5080
+ schemaTypes,
5081
+ spellCheck,
5082
+ readOnly,
5083
+ renderBlock,
5084
+ renderChild,
5085
+ renderListItem,
5086
+ renderStyle
5087
+ ]
4369
5088
  ), renderLeaf = react.useCallback(
4370
5089
  (lProps) => {
4371
5090
  if (lProps.leaf._type === "span") {
@@ -4390,7 +5109,14 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4390
5109
  }
4391
5110
  return lProps.children;
4392
5111
  },
4393
- [readOnly, renderAnnotation, renderChild, renderDecorator, renderPlaceholder, schemaTypes]
5112
+ [
5113
+ readOnly,
5114
+ renderAnnotation,
5115
+ renderChild,
5116
+ renderDecorator,
5117
+ renderPlaceholder,
5118
+ schemaTypes
5119
+ ]
4394
5120
  ), restoreSelectionFromProps = react.useCallback(() => {
4395
5121
  if (propsSelection) {
4396
5122
  debug(`Selection from props ${JSON.stringify(propsSelection)}`);
@@ -4399,7 +5125,9 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4399
5125
  fromSlateValue(slateEditor.children, blockTypeName)
4400
5126
  );
4401
5127
  if (normalizedSelection !== null) {
4402
- debug(`Normalized selection from props ${JSON.stringify(normalizedSelection)}`);
5128
+ debug(
5129
+ `Normalized selection from props ${JSON.stringify(normalizedSelection)}`
5130
+ );
4403
5131
  const slateRange = toSlateRange(normalizedSelection, slateEditor);
4404
5132
  slateRange && (slate.Transforms.select(slateEditor, slateRange), slateEditor.operations.some((o) => o.type === "set_selection") || change$.next({ type: "selection", selection: normalizedSelection }), slateEditor.onChange());
4405
5133
  }
@@ -4409,7 +5137,10 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4409
5137
  if (rangeDecorations && rangeDecorations.length > 0) {
4410
5138
  const newSlateRanges = [];
4411
5139
  if (rangeDecorations.forEach((rangeDecorationItem) => {
4412
- const slateRange = toSlateRange(rangeDecorationItem.selection, slateEditor);
5140
+ const slateRange = toSlateRange(
5141
+ rangeDecorationItem.selection,
5142
+ slateEditor
5143
+ );
4413
5144
  if (!slate.Range.isRange(slateRange)) {
4414
5145
  rangeDecorationItem.onMoved && rangeDecorationItem.onMoved({
4415
5146
  newSelection: null,
@@ -4420,20 +5151,27 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4420
5151
  }
4421
5152
  let newRange;
4422
5153
  if (operation && (newRange = moveRangeByOperation(slateRange, operation), newRange && newRange !== slateRange || newRange === null && slateRange)) {
4423
- const value = PortableTextEditor.getValue(portableTextEditor), newRangeSelection = toPortableTextRange(value, newRange, schemaTypes);
5154
+ const value = PortableTextEditor.getValue(portableTextEditor), newRangeSelection = toPortableTextRange(
5155
+ value,
5156
+ newRange,
5157
+ schemaTypes
5158
+ );
4424
5159
  rangeDecorationItem.onMoved && rangeDecorationItem.onMoved({
4425
5160
  newSelection: newRangeSelection,
4426
5161
  rangeDecoration: rangeDecorationItem,
4427
5162
  origin: "local"
4428
5163
  });
4429
5164
  }
4430
- newRange !== null && newSlateRanges.push({ ...newRange || slateRange, rangeDecoration: rangeDecorationItem });
5165
+ newRange !== null && newSlateRanges.push({
5166
+ ...newRange || slateRange,
5167
+ rangeDecoration: rangeDecorationItem
5168
+ });
4431
5169
  }), newSlateRanges.length > 0) {
4432
5170
  setRangeDecorationsState(newSlateRanges);
4433
5171
  return;
4434
5172
  }
4435
5173
  }
4436
- setRangeDecorationsState([]);
5174
+ rangeDecorationState.length > 0 && setRangeDecorationsState([]);
4437
5175
  },
4438
5176
  [portableTextEditor, rangeDecorations, schemaTypes, slateEditor]
4439
5177
  );
@@ -4480,11 +5218,20 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4480
5218
  debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
4481
5219
  return;
4482
5220
  }
4483
- const value = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(value, slateEditor.selection, schemaTypes)?.focus.path || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
5221
+ const value = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(
5222
+ value,
5223
+ slateEditor.selection,
5224
+ schemaTypes
5225
+ )?.focus.path || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
4484
5226
  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
5227
  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);
5228
+ toSlateValue(result.insert, {
5229
+ schemaTypes
5230
+ })
5231
+ ) : console.warn(
5232
+ "Your onPaste function returned something unexpected:",
5233
+ result
5234
+ );
4488
5235
  }).catch((error) => (console.error(error), error)).finally(() => {
4489
5236
  change$.next({ type: "loading", isLoading: !1 });
4490
5237
  }));
@@ -4509,7 +5256,10 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4509
5256
  const [lastBlock, path] = slate.Node.last(slateEditor, []), focusPath = slateEditor.selection.focus.path.slice(0, 1), lastPath = path.slice(0, 1);
4510
5257
  if (slate.Path.equals(focusPath, lastPath)) {
4511
5258
  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());
5259
+ lastBlock && slate.Editor.isVoid(slateEditor, node) && (slate.Transforms.insertNodes(
5260
+ slateEditor,
5261
+ slateEditor.pteCreateTextBlock({ decorators: [] })
5262
+ ), slateEditor.onChange());
4513
5263
  }
4514
5264
  }
4515
5265
  },
@@ -4535,7 +5285,10 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4535
5285
  return;
4536
5286
  const existingDOMRange = domSelection.getRangeAt(0);
4537
5287
  try {
4538
- const newDOMRange = slateReact.ReactEditor.toDOMRange(slateEditor, slateEditor.selection);
5288
+ const newDOMRange = slateReact.ReactEditor.toDOMRange(
5289
+ slateEditor,
5290
+ slateEditor.selection
5291
+ );
4539
5292
  (newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
4540
5293
  } catch {
4541
5294
  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 +5336,19 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4583
5336
  ];
4584
5337
  if (path.length === 0)
4585
5338
  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));
5339
+ 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, {
5340
+ anchor: { path, offset: 0 },
5341
+ focus: { path, offset: 0 }
5342
+ }) || slate.Range.includes(item, path));
4587
5343
  return result.length > 0 ? result : [];
4588
5344
  },
4589
5345
  [slateEditor, schemaTypes, rangeDecorationState]
4590
5346
  );
4591
5347
  return react.useEffect(() => {
4592
- ref.current = slateReact.ReactEditor.toDOMNode(slateEditor, slateEditor), setEditableElement(ref.current);
5348
+ ref.current = slateReact.ReactEditor.toDOMNode(
5349
+ slateEditor,
5350
+ slateEditor
5351
+ ), setEditableElement(ref.current);
4593
5352
  }, [slateEditor, ref]), portableTextEditor ? hasInvalidValue ? null : /* @__PURE__ */ jsxRuntime.jsx(
4594
5353
  slateReact.Editable,
4595
5354
  {