@portabletext/editor 1.0.18 → 1.1.0

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