@portabletext/editor 1.0.10 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -169,8 +169,7 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
169
169
  });
170
170
  }
171
171
  function isEqualToEmptyEditor(children, schemaTypes) {
172
- var _a;
173
- return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && slate.Element.isElement(children[0]) && children[0]._type === schemaTypes.block.name && "style" in children[0] && children[0].style === schemaTypes.styles[0].value && !("listItem" in children[0]) && Array.isArray(children[0].children) && children[0].children.length === 1 && slate.Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !((_a = children[0].children[0].marks) != null && _a.join("")) && children[0].children[0].text === "";
172
+ return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && slate.Element.isElement(children[0]) && children[0]._type === schemaTypes.block.name && "style" in children[0] && children[0].style === schemaTypes.styles[0].value && !("listItem" in children[0]) && Array.isArray(children[0].children) && children[0].children.length === 1 && slate.Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !children[0].children[0].marks?.join("") && children[0].children[0].text === "";
174
173
  }
175
174
  const IS_PROCESSING_REMOTE_CHANGES = /* @__PURE__ */ new WeakMap(), IS_PROCESSING_LOCAL_CHANGES = /* @__PURE__ */ new WeakMap(), IS_DRAGGING = /* @__PURE__ */ new WeakMap(), IS_DRAGGING_BLOCK_ELEMENT = /* @__PURE__ */ new WeakMap(), IS_DRAGGING_ELEMENT_TARGET = /* @__PURE__ */ new WeakMap(), IS_DRAGGING_BLOCK_TARGET_POSITION = /* @__PURE__ */ new WeakMap(), KEY_TO_SLATE_ELEMENT = /* @__PURE__ */ new WeakMap(), KEY_TO_VALUE_ELEMENT = /* @__PURE__ */ new WeakMap(), SLATE_TO_PORTABLE_TEXT_RANGE = /* @__PURE__ */ new WeakMap(), DefaultObject = (props) => /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx("pre", { children: JSON.stringify(props.value, null, 2) }) }), DefaultBlockObject = styledComponents.styled.div`
176
175
  user-select: none;
@@ -639,13 +638,12 @@ function DefaultAnnotation(props) {
639
638
  return /* @__PURE__ */ jsxRuntime.jsx("span", { style: { color: "blue" }, onClick: handleClick, children: props.children });
640
639
  }
641
640
  function getPortableTextMemberSchemaTypes(portableTextType) {
642
- var _a, _b, _c;
643
641
  if (!portableTextType)
644
642
  throw new Error("Parameter 'portabletextType' missing (required)");
645
- const blockType = (_a = portableTextType.of) == null ? void 0 : _a.find(findBlockType);
643
+ const blockType = portableTextType.of?.find(findBlockType);
646
644
  if (!blockType)
647
645
  throw new Error("Block type is not defined in this schema (required)");
648
- const childrenField = (_b = blockType.fields) == null ? void 0 : _b.find((field) => field.name === "children");
646
+ const childrenField = blockType.fields?.find((field) => field.name === "children");
649
647
  if (!childrenField)
650
648
  throw new Error("Children field for block type found in schema (required)");
651
649
  const ofType = childrenField.type.of;
@@ -654,7 +652,7 @@ function getPortableTextMemberSchemaTypes(portableTextType) {
654
652
  const spanType = ofType.find((memberType) => memberType.name === "span");
655
653
  if (!spanType)
656
654
  throw new Error("Span type not found in schema (required)");
657
- const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = ((_c = portableTextType.of) == null ? void 0 : _c.filter((field) => field.name !== blockType.name)) || [];
655
+ const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = portableTextType.of?.filter((field) => field.name !== blockType.name) || [];
658
656
  return {
659
657
  styles: resolveEnabledStyles(blockType),
660
658
  decorators: resolveEnabledDecorators(spanType),
@@ -668,11 +666,10 @@ function getPortableTextMemberSchemaTypes(portableTextType) {
668
666
  };
669
667
  }
670
668
  function resolveEnabledStyles(blockType) {
671
- var _a, _b, _c;
672
- const styleField = (_a = blockType.fields) == null ? void 0 : _a.find((btField) => btField.name === "style");
669
+ const styleField = blockType.fields?.find((btField) => btField.name === "style");
673
670
  if (!styleField)
674
671
  throw new Error("A field with name 'style' is not defined in the block type (required).");
675
- const textStyles = ((_b = styleField.type.options) == null ? void 0 : _b.list) && ((_c = styleField.type.options.list) == null ? void 0 : _c.filter((style) => style.value));
672
+ const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter((style) => style.value);
676
673
  if (!textStyles || textStyles.length === 0)
677
674
  throw new Error(
678
675
  "The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}."
@@ -683,11 +680,10 @@ function resolveEnabledDecorators(spanType) {
683
680
  return spanType.decorators;
684
681
  }
685
682
  function resolveEnabledListItems(blockType) {
686
- var _a, _b;
687
- const listField = (_a = blockType.fields) == null ? void 0 : _a.find((btField) => btField.name === "listItem");
683
+ const listField = blockType.fields?.find((btField) => btField.name === "listItem");
688
684
  if (!listField)
689
685
  throw new Error("A field with name 'listItem' is not defined in the block type (required).");
690
- const listItems = ((_b = listField.type.options) == null ? void 0 : _b.list) && listField.type.options.list.filter((list) => list.value);
686
+ const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
691
687
  if (!listItems)
692
688
  throw new Error("The list field need at least to be an empty array");
693
689
  return listItems;
@@ -765,7 +761,7 @@ function createOperationToPatches(types2) {
765
761
  function insertNodePatch(editor, operation, beforeValue) {
766
762
  const block = beforeValue[operation.path[0]], isTextBlock = editor.isTextBlock(block);
767
763
  if (operation.path.length === 1) {
768
- const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block == null ? void 0 : block._key : beforeBlock == null ? void 0 : beforeBlock._key;
764
+ const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block?._key : beforeBlock?._key;
769
765
  return targetKey ? [
770
766
  patches.insert([fromSlateValue([operation.node], textBlockName)[0]], position, [
771
767
  { _key: targetKey }
@@ -867,7 +863,7 @@ function createOperationToPatches(types2) {
867
863
  function mergeNodePatch(editor, operation, beforeValue) {
868
864
  const patches$1 = [], block = beforeValue[operation.path[0]], targetBlock = editor.children[operation.path[0]];
869
865
  if (operation.path.length === 1)
870
- if (block != null && block._key) {
866
+ if (block?._key) {
871
867
  const newBlock = fromSlateValue([editor.children[operation.path[0] - 1]], textBlockName)[0];
872
868
  patches$1.push(patches.set(newBlock, [{ _key: newBlock._key }])), patches$1.push(patches.unset([{ _key: block._key }]));
873
869
  } else
@@ -965,7 +961,6 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
965
961
  }
966
962
  },
967
963
  insertChild: (type, value) => {
968
- var _a;
969
964
  if (!editor.selection)
970
965
  throw new Error("The editor has no selection");
971
966
  const [focusBlock] = Array.from(
@@ -997,16 +992,13 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
997
992
  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, {
998
993
  select: !0,
999
994
  at: editor.selection
1000
- }), editor.onChange(), ((_a = toPortableTextRange(
995
+ }), editor.onChange(), toPortableTextRange(
1001
996
  fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1002
997
  editor.selection,
1003
998
  types$1
1004
- )) == null ? void 0 : _a.focus.path) || [];
999
+ )?.focus.path || [];
1005
1000
  },
1006
1001
  insertBlock: (type, value) => {
1007
- var _a;
1008
- if (!editor.selection)
1009
- throw new Error("The editor has no selection");
1010
1002
  const block = toSlateValue(
1011
1003
  [
1012
1004
  {
@@ -1016,17 +1008,32 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1016
1008
  }
1017
1009
  ],
1018
1010
  portableTextEditor
1019
- )[0], [focusBlock] = Array.from(
1011
+ )[0];
1012
+ if (!editor.selection) {
1013
+ const lastBlock = Array.from(
1014
+ slate.Editor.nodes(editor, {
1015
+ match: (n) => !slate.Editor.isEditor(n),
1016
+ at: [],
1017
+ reverse: !0
1018
+ })
1019
+ )[0];
1020
+ return slate.Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: lastBlock[1] }), editor.onChange(), toPortableTextRange(
1021
+ fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1022
+ editor.selection,
1023
+ types$1
1024
+ )?.focus.path ?? [];
1025
+ }
1026
+ const focusBlock = Array.from(
1020
1027
  slate.Editor.nodes(editor, {
1021
1028
  at: editor.selection.focus.path.slice(0, 1),
1022
1029
  match: (n) => n._type === types$1.block.name
1023
1030
  })
1024
- )[0] || [void 0];
1025
- return focusBlock && isEqualToEmptyEditor([focusBlock], types$1) && slate.Transforms.removeNodes(editor, { at: editor.selection }), slate.Editor.insertNode(editor, block), editor.onChange(), ((_a = toPortableTextRange(
1031
+ )[0];
1032
+ return slate.Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: focusBlock[1] }), editor.onChange(), toPortableTextRange(
1026
1033
  fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1027
1034
  editor.selection,
1028
1035
  types$1
1029
- )) == null ? void 0 : _a.focus.path) || [];
1036
+ )?.focus.path || [];
1030
1037
  },
1031
1038
  hasBlockStyle: (style) => {
1032
1039
  try {
@@ -1082,7 +1089,6 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1082
1089
  return node;
1083
1090
  },
1084
1091
  activeAnnotations: () => {
1085
- var _a;
1086
1092
  if (!editor.selection || editor.selection.focus.path.length < 2)
1087
1093
  return [];
1088
1094
  try {
@@ -1092,9 +1098,9 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1092
1098
  });
1093
1099
  for (const [span, path] of spans) {
1094
1100
  const [block] = slate.Editor.node(editor, path, { depth: 1 });
1095
- editor.isTextBlock(block) && ((_a = block.markDefs) == null || _a.forEach((def) => {
1101
+ editor.isTextBlock(block) && block.markDefs?.forEach((def) => {
1096
1102
  slate.Text.isText(span) && span.marks && Array.isArray(span.marks) && span.marks.includes(def._key) && activeAnnotations.push(def);
1097
- }));
1103
+ });
1098
1104
  }
1099
1105
  return activeAnnotations;
1100
1106
  } catch {
@@ -1111,28 +1117,17 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1111
1117
  match: (node) => slate.Text.isText(node)
1112
1118
  })
1113
1119
  ];
1114
- if (spans.some(
1115
- ([span]) => {
1116
- var _a;
1117
- return !types.isPortableTextSpan(span) || !span.marks || ((_a = span.marks) == null ? void 0 : _a.length) === 0;
1118
- }
1120
+ if (spans.length === 0 || spans.some(
1121
+ ([span]) => !types.isPortableTextSpan(span) || !span.marks || span.marks?.length === 0
1119
1122
  ))
1120
1123
  return !1;
1121
1124
  const selectionMarkDefs = spans.reduce((accMarkDefs, [, path]) => {
1122
1125
  const [block] = slate.Editor.node(editor, path, { depth: 1 });
1123
1126
  return editor.isTextBlock(block) && block.markDefs ? [...accMarkDefs, ...block.markDefs] : accMarkDefs;
1124
1127
  }, []);
1125
- return spans.every(([span]) => {
1126
- var _a;
1127
- if (!types.isPortableTextSpan(span)) return !1;
1128
- const spanMarkDefs = (_a = span.marks) == null ? void 0 : _a.map(
1129
- (markKey) => {
1130
- var _a2;
1131
- return (_a2 = selectionMarkDefs.find((def) => (def == null ? void 0 : def._key) === markKey)) == null ? void 0 : _a2._type;
1132
- }
1133
- );
1134
- return spanMarkDefs == null ? void 0 : spanMarkDefs.includes(annotationType);
1135
- });
1128
+ return spans.every(([span]) => types.isPortableTextSpan(span) ? span.marks?.map(
1129
+ (markKey) => selectionMarkDefs.find((def) => def?._key === markKey)?._type
1130
+ )?.includes(annotationType) : !1);
1136
1131
  } catch {
1137
1132
  return !1;
1138
1133
  }
@@ -1192,7 +1187,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1192
1187
  if (!(range && range.anchor.path.length > 0 && range.focus.path.length > 0))
1193
1188
  throw new Error("Invalid range");
1194
1189
  if (range) {
1195
- if (!(options != null && options.mode) || (options == null ? void 0 : options.mode) === "selected") {
1190
+ if (!options?.mode || options?.mode === "selected") {
1196
1191
  debug$j("Deleting content in selection"), slate.Transforms.delete(editor, {
1197
1192
  at: range,
1198
1193
  hanging: !0,
@@ -1200,11 +1195,11 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1200
1195
  }), editor.onChange();
1201
1196
  return;
1202
1197
  }
1203
- (options == null ? void 0 : options.mode) === "blocks" && (debug$j("Deleting blocks touched by selection"), slate.Transforms.removeNodes(editor, {
1198
+ options?.mode === "blocks" && (debug$j("Deleting blocks touched by selection"), slate.Transforms.removeNodes(editor, {
1204
1199
  at: range,
1205
1200
  voids: !0,
1206
1201
  match: (node) => editor.isTextBlock(node) || !editor.isTextBlock(node) && slate.Element.isElement(node)
1207
- })), (options == null ? void 0 : options.mode) === "children" && (debug$j("Deleting children touched by selection"), slate.Transforms.removeNodes(editor, {
1202
+ })), options?.mode === "children" && (debug$j("Deleting children touched by selection"), slate.Transforms.removeNodes(editor, {
1208
1203
  at: range,
1209
1204
  voids: !0,
1210
1205
  match: (node) => node._type === types$1.span.name || // Text children
@@ -1303,6 +1298,10 @@ function withPreserveKeys(editor, fn) {
1303
1298
  const prev = isPreservingKeys(editor);
1304
1299
  PRESERVE_KEYS.set(editor, !0), fn(), PRESERVE_KEYS.set(editor, prev);
1305
1300
  }
1301
+ function withoutPreserveKeys(editor, fn) {
1302
+ const prev = isPreservingKeys(editor);
1303
+ PRESERVE_KEYS.set(editor, !1), fn(), PRESERVE_KEYS.set(editor, prev);
1304
+ }
1306
1305
  function isPreservingKeys(editor) {
1307
1306
  return PRESERVE_KEYS.get(editor);
1308
1307
  }
@@ -1313,17 +1312,27 @@ function createWithObjectKeys(schemaTypes, keyGenerator) {
1313
1312
  return editor.apply = (operation) => {
1314
1313
  if (operation.type === "split_node") {
1315
1314
  const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.properties);
1316
- operation.properties = {
1317
- ...operation.properties,
1318
- ...withNewKey ? { _key: keyGenerator() } : {}
1319
- };
1315
+ apply2({
1316
+ ...operation,
1317
+ properties: {
1318
+ ...operation.properties,
1319
+ ...withNewKey ? { _key: keyGenerator() } : {}
1320
+ }
1321
+ });
1322
+ return;
1320
1323
  }
1321
1324
  if (operation.type === "insert_node") {
1322
1325
  const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.node);
1323
- slate.Editor.isEditor(operation.node) || (operation.node = {
1324
- ...operation.node,
1325
- ...withNewKey ? { _key: keyGenerator() } : {}
1326
- });
1326
+ if (!slate.Editor.isEditor(operation.node)) {
1327
+ apply2({
1328
+ ...operation,
1329
+ node: {
1330
+ ...operation.node,
1331
+ ...withNewKey ? { _key: keyGenerator() } : {}
1332
+ }
1333
+ });
1334
+ return;
1335
+ }
1327
1336
  }
1328
1337
  apply2(operation);
1329
1338
  }, editor.normalizeNode = (entry) => {
@@ -2211,7 +2220,7 @@ function findBlockAndChildFromPath(editor, path) {
2211
2220
  const isMatch = isKeyedSegment(path[2]) ? node._key === path[2]._key : index === path[2];
2212
2221
  return isMatch && (childIndex = index), isMatch;
2213
2222
  });
2214
- return child ? { block, child, blockPath, childPath: blockPath == null ? void 0 : blockPath.concat(childIndex) } : { block, blockPath, child: void 0, childPath: void 0 };
2223
+ return child ? { block, child, blockPath, childPath: blockPath?.concat(childIndex) } : { block, blockPath, child: void 0, childPath: void 0 };
2215
2224
  }
2216
2225
  function withRemoteChanges(editor, fn) {
2217
2226
  const prev = isChangingRemotely(editor) || !1;
@@ -2300,11 +2309,12 @@ function createWithUndoRedo(options) {
2300
2309
  )
2301
2310
  );
2302
2311
  });
2312
+ const reversedOperations = transformedOperations.map(slate.Operation.inverse).reverse();
2303
2313
  try {
2304
2314
  slate.Editor.withoutNormalizing(editor, () => {
2305
2315
  withPreserveKeys(editor, () => {
2306
2316
  withoutSaving(editor, () => {
2307
- transformedOperations.map(slate.Operation.inverse).reverse().forEach((op) => {
2317
+ reversedOperations.forEach((op) => {
2308
2318
  editor.apply(op);
2309
2319
  });
2310
2320
  });
@@ -2377,14 +2387,13 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
2377
2387
  if (patch.type === "diffMatchPatch") {
2378
2388
  const operationTargetBlock = findOperationTargetBlock(editor, transformedOperation);
2379
2389
  return !operationTargetBlock || !isEqual__default.default({ _key: operationTargetBlock._key }, patch.path[0]) ? [transformedOperation] : (parse(patch.value).forEach((diffPatch) => {
2380
- var _a, _b, _c, _d;
2381
2390
  let adjustOffsetBy = 0, changedOffset = diffPatch.utf8Start1;
2382
2391
  const { diffs } = diffPatch;
2383
2392
  if (diffs.forEach((diff2, index) => {
2384
2393
  const [diffType, text] = diff2;
2385
2394
  diffType === DIFF_INSERT ? (adjustOffsetBy += text.length, changedOffset += text.length) : diffType === DIFF_DELETE ? (adjustOffsetBy -= text.length, changedOffset -= text.length) : diffType === DIFF_EQUAL && (diffs.slice(index).every(([dType]) => dType === DIFF_EQUAL) || (changedOffset += text.length));
2386
2395
  }), transformedOperation.type === "insert_text" && changedOffset < transformedOperation.offset && (transformedOperation.offset += adjustOffsetBy), transformedOperation.type === "remove_text" && changedOffset <= transformedOperation.offset - transformedOperation.text.length && (transformedOperation.offset += adjustOffsetBy), transformedOperation.type === "set_selection") {
2387
- const currentFocus = (_a = transformedOperation.properties) != null && _a.focus ? { ...transformedOperation.properties.focus } : void 0, currentAnchor = (_b = transformedOperation == null ? void 0 : transformedOperation.properties) != null && _b.anchor ? { ...transformedOperation.properties.anchor } : void 0, newFocus = (_c = transformedOperation == null ? void 0 : transformedOperation.newProperties) != null && _c.focus ? { ...transformedOperation.newProperties.focus } : void 0, newAnchor = (_d = transformedOperation == null ? void 0 : transformedOperation.newProperties) != null && _d.anchor ? { ...transformedOperation.newProperties.anchor } : void 0;
2396
+ const currentFocus = transformedOperation.properties?.focus ? { ...transformedOperation.properties.focus } : void 0, currentAnchor = transformedOperation?.properties?.anchor ? { ...transformedOperation.properties.anchor } : void 0, newFocus = transformedOperation?.newProperties?.focus ? { ...transformedOperation.newProperties.focus } : void 0, newAnchor = transformedOperation?.newProperties?.anchor ? { ...transformedOperation.newProperties.anchor } : void 0;
2388
2397
  (currentFocus && currentAnchor || newFocus && newAnchor) && ([currentFocus, currentAnchor, newFocus, newAnchor].forEach((point) => {
2389
2398
  point && changedOffset < point.offset && (point.offset += adjustOffsetBy);
2390
2399
  }), currentFocus && currentAnchor && (transformedOperation.properties = {
@@ -2400,14 +2409,13 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
2400
2409
  return [transformedOperation];
2401
2410
  }
2402
2411
  function adjustBlockPath(operation, level, blockIndex) {
2403
- var _a, _b, _c, _d;
2404
2412
  const transformedOperation = { ...operation };
2405
2413
  if (blockIndex >= 0 && transformedOperation.type !== "set_selection" && Array.isArray(transformedOperation.path) && transformedOperation.path[0] >= blockIndex + level && transformedOperation.path[0] + level > -1) {
2406
2414
  const newPath = [transformedOperation.path[0] + level, ...transformedOperation.path.slice(1)];
2407
2415
  transformedOperation.path = newPath;
2408
2416
  }
2409
2417
  if (transformedOperation.type === "set_selection") {
2410
- const currentFocus = (_a = transformedOperation.properties) != null && _a.focus ? { ...transformedOperation.properties.focus } : void 0, currentAnchor = (_b = transformedOperation == null ? void 0 : transformedOperation.properties) != null && _b.anchor ? { ...transformedOperation.properties.anchor } : void 0, newFocus = (_c = transformedOperation == null ? void 0 : transformedOperation.newProperties) != null && _c.focus ? { ...transformedOperation.newProperties.focus } : void 0, newAnchor = (_d = transformedOperation == null ? void 0 : transformedOperation.newProperties) != null && _d.anchor ? { ...transformedOperation.newProperties.anchor } : void 0;
2418
+ const currentFocus = transformedOperation.properties?.focus ? { ...transformedOperation.properties.focus } : void 0, currentAnchor = transformedOperation?.properties?.anchor ? { ...transformedOperation.properties.anchor } : void 0, newFocus = transformedOperation?.newProperties?.focus ? { ...transformedOperation.newProperties.focus } : void 0, newAnchor = transformedOperation?.newProperties?.anchor ? { ...transformedOperation.newProperties.anchor } : void 0;
2411
2419
  (currentFocus && currentAnchor || newFocus && newAnchor) && ([currentFocus, currentAnchor, newFocus, newAnchor].forEach((point) => {
2412
2420
  point && point.path[0] >= blockIndex + level && point.path[0] + level > -1 && (point.path = [point.path[0] + level, ...point.path.slice(1)]);
2413
2421
  }), currentFocus && currentAnchor && (transformedOperation.properties = {
@@ -2705,6 +2713,21 @@ function createWithPortableTextLists(types2) {
2705
2713
  }, editor;
2706
2714
  };
2707
2715
  }
2716
+ function isPortableTextSpan(node) {
2717
+ return node._type === "span" && "text" in node && typeof node.text == "string" && (typeof node.marks > "u" || Array.isArray(node.marks) && node.marks.every((mark) => typeof mark == "string"));
2718
+ }
2719
+ function isPortableTextBlock(node) {
2720
+ return (
2721
+ // A block doesn't _have_ to be named 'block' - to differentiate between
2722
+ // allowed child types and marks, one might name them differently
2723
+ typeof node._type == "string" && // Toolkit-types like nested spans are @-prefixed
2724
+ node._type[0] !== "@" && // `markDefs` isn't _required_ per say, but if it's there, it needs to be an array
2725
+ (!("markDefs" in node) || !node.markDefs || Array.isArray(node.markDefs) && // Every mark definition needs to have an `_key` to be mappable in child spans
2726
+ node.markDefs.every((def) => typeof def._key == "string")) && // `children` is required and needs to be an array
2727
+ "children" in node && Array.isArray(node.children) && // All children are objects with `_type` (usually spans, but can contain other stuff)
2728
+ node.children.every((child) => typeof child == "object" && "_type" in child)
2729
+ );
2730
+ }
2708
2731
  const debug$c = debugWithName("plugin:withPortableTextMarkModel");
2709
2732
  function createWithPortableTextMarkModel(types2, change$) {
2710
2733
  return function(editor) {
@@ -2732,10 +2755,7 @@ function createWithPortableTextMarkModel(types2, change$) {
2732
2755
  );
2733
2756
  if (annotationMarks.length > 0) {
2734
2757
  const [block] = slate.Editor.node(editor, slate.Path.parent(path)), orphanedMarks = editor.isTextBlock(block) && annotationMarks.filter(
2735
- (mark) => {
2736
- var _a;
2737
- return !((_a = block.markDefs) != null && _a.find((def) => def._key === mark));
2738
- }
2758
+ (mark) => !block.markDefs?.find((def) => def._key === mark)
2739
2759
  ) || [];
2740
2760
  orphanedMarks.length > 0 && (debug$c("Removing orphaned .marks from span node"), slate.Transforms.setNodes(
2741
2761
  editor,
@@ -2787,10 +2807,9 @@ function createWithPortableTextMarkModel(types2, change$) {
2787
2807
  ), editor.onChange());
2788
2808
  }
2789
2809
  }, editor.apply = (op) => {
2790
- var _a, _b;
2791
2810
  if (op.type === "insert_text") {
2792
2811
  const { selection } = editor;
2793
- if (selection && slate.Range.isCollapsed(selection) && (_b = (_a = slate.Editor.marks(editor)) == null ? void 0 : _a.marks) != null && _b.some((mark) => !decorators.includes(mark))) {
2812
+ if (selection && slate.Range.isCollapsed(selection) && slate.Editor.marks(editor)?.marks?.some((mark) => !decorators.includes(mark))) {
2794
2813
  const [node] = Array.from(
2795
2814
  slate.Editor.nodes(editor, {
2796
2815
  mode: "lowest",
@@ -2816,6 +2835,32 @@ function createWithPortableTextMarkModel(types2, change$) {
2816
2835
  }
2817
2836
  }
2818
2837
  }
2838
+ if (op.type === "remove_text") {
2839
+ const node = Array.from(
2840
+ slate.Editor.nodes(editor, {
2841
+ mode: "lowest",
2842
+ at: { path: op.path, offset: op.offset },
2843
+ match: (n) => n._type === types2.span.name,
2844
+ voids: !1
2845
+ })
2846
+ )[0][0], block = slate.Editor.node(editor, slate.Path.parent(op.path))[0];
2847
+ if (node && isPortableTextSpan(node) && block && isPortableTextBlock(block)) {
2848
+ const markDefs = block.markDefs ?? [], nodeHasAnnotations = (node.marks ?? []).some(
2849
+ (mark) => markDefs.find((markDef) => markDef._key === mark)
2850
+ ), deletingPartOfTheNode = op.offset !== 0, deletingFromTheEnd = op.offset + op.text.length === node.text.length;
2851
+ if (nodeHasAnnotations && deletingPartOfTheNode && deletingFromTheEnd) {
2852
+ slate.Editor.withoutNormalizing(editor, () => {
2853
+ withoutPreserveKeys(editor, () => {
2854
+ slate.Transforms.splitNodes(editor, {
2855
+ match: slate.Text.isText,
2856
+ at: { path: op.path, offset: op.offset }
2857
+ });
2858
+ }), slate.Transforms.removeNodes(editor, { at: slate.Path.next(op.path) });
2859
+ }), editor.onChange();
2860
+ return;
2861
+ }
2862
+ }
2863
+ }
2819
2864
  apply2(op);
2820
2865
  }, editor.addMark = (mark) => {
2821
2866
  if (editor.selection) {
@@ -2824,10 +2869,7 @@ function createWithPortableTextMarkModel(types2, change$) {
2824
2869
  const splitTextNodes = [
2825
2870
  ...slate.Editor.nodes(editor, { at: editor.selection, match: slate.Text.isText })
2826
2871
  ];
2827
- if (splitTextNodes.every((node) => {
2828
- var _a;
2829
- return (_a = node[0].marks) == null ? void 0 : _a.includes(mark);
2830
- }))
2872
+ if (splitTextNodes.every((node) => node[0].marks?.includes(mark)))
2831
2873
  return editor.removeMark(mark), editor;
2832
2874
  slate.Editor.withoutNormalizing(editor, () => {
2833
2875
  splitTextNodes.forEach(([node, path]) => {
@@ -2896,9 +2938,8 @@ function createWithPortableTextMarkModel(types2, change$) {
2896
2938
  slate.Editor.nodes(editor, { match: slate.Text.isText, at: editor.selection })
2897
2939
  );
2898
2940
  return slate.Range.isExpanded(editor.selection) ? selectedNodes.every((n) => {
2899
- var _a;
2900
2941
  const [node] = n;
2901
- return (_a = node.marks) == null ? void 0 : _a.includes(mark);
2942
+ return node.marks?.includes(mark);
2902
2943
  }) : ({
2903
2944
  ...slate.Editor.marks(editor) || {}
2904
2945
  }.marks || []).includes(mark);
@@ -3031,7 +3072,6 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3031
3072
  const reservedHotkeys = ["enter", "tab", "shift", "delete", "end"], activeHotkeys = hotkeysFromOptions || DEFAULT_HOTKEYS;
3032
3073
  return function(editor) {
3033
3074
  return editor.pteWithHotKeys = (event) => {
3034
- var _a, _b, _c, _d, _e, _f;
3035
3075
  Object.keys(activeHotkeys).forEach((cat) => {
3036
3076
  if (cat === "marks")
3037
3077
  for (const hotkey in activeHotkeys[cat]) {
@@ -3078,7 +3118,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3078
3118
  }
3079
3119
  }
3080
3120
  if (isBackspace && editor.selection && editor.selection.focus.path[0] === 0 && slate.Range.isCollapsed(editor.selection)) {
3081
- 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 && ((_b = (_a = focusBlock.children) == null ? void 0 : _a[0]) == null ? void 0 : _b.text) === "";
3121
+ 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 === "";
3082
3122
  if (nextBlock && isTextBlock && isEmptyFocusBlock) {
3083
3123
  event.preventDefault(), event.stopPropagation(), slate.Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), editor.onChange();
3084
3124
  return;
@@ -3088,7 +3128,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3088
3128
  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));
3089
3129
  if (prevBlock && focusBlock && slate.Editor.isVoid(editor, prevBlock) && editor.selection.focus.offset === 0) {
3090
3130
  debug$8("Preventing deleting void block above"), event.preventDefault(), event.stopPropagation();
3091
- const isTextBlock = types.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && ((_d = (_c = focusBlock.children) == null ? void 0 : _c[0]) == null ? void 0 : _d.text) === "";
3131
+ const isTextBlock = types.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && focusBlock.children?.[0]?.text === "";
3092
3132
  if (!isTextBlock || isEmptyFocusBlock) {
3093
3133
  slate.Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), slate.Transforms.select(editor, prevPath), editor.onChange();
3094
3134
  return;
@@ -3104,7 +3144,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3104
3144
  const nextBlock = slate.Node.descendant(
3105
3145
  editor,
3106
3146
  slate.Path.next(editor.selection.focus.path.slice(0, 1))
3107
- ), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath), isEmptyFocusBlock = types.isPortableTextTextBlock(focusBlock) && focusBlock.children.length === 1 && ((_f = (_e = focusBlock.children) == null ? void 0 : _e[0]) == null ? void 0 : _f.text) === "";
3147
+ ), 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 === "";
3108
3148
  if (nextBlock && focusBlock && !slate.Editor.isVoid(editor, focusBlock) && slate.Editor.isVoid(editor, nextBlock) && isEmptyFocusBlock) {
3109
3149
  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();
3110
3150
  return;
@@ -3409,13 +3449,7 @@ function validateValue(value, types$1, keyGenerator) {
3409
3449
  const debug$7 = debugWithName("plugin:withInsertData");
3410
3450
  function createWithInsertData(change$, schemaTypes, keyGenerator) {
3411
3451
  return function(editor) {
3412
- const blockTypeName = schemaTypes.block.name, spanTypeName = schemaTypes.span.name, whitespaceOnPasteMode = schemaTypes.block.options.unstable_whitespaceOnPasteMode, toPlainText = (blocks) => blocks.map((block) => {
3413
- var _a;
3414
- return editor.isTextBlock(block) ? block.children.map((child) => {
3415
- var _a2;
3416
- return child._type === spanTypeName ? child.text : `[${((_a2 = schemaTypes.inlineObjects.find((t) => t.name === child._type)) == null ? void 0 : _a2.title) || "Object"}]`;
3417
- }).join("") : `[${((_a = schemaTypes.blockObjects.find((t) => t.name === block._type)) == null ? void 0 : _a.title) || "Object"}]`;
3418
- }).join(`
3452
+ const blockTypeName = schemaTypes.block.name, spanTypeName = schemaTypes.span.name, whitespaceOnPasteMode = schemaTypes.block.options.unstable_whitespaceOnPasteMode, toPlainText = (blocks) => blocks.map((block) => editor.isTextBlock(block) ? block.children.map((child) => child._type === spanTypeName ? child.text : `[${schemaTypes.inlineObjects.find((t) => t.name === child._type)?.title || "Object"}]`).join("") : `[${schemaTypes.blockObjects.find((t) => t.name === block._type)?.title || "Object"}]`).join(`
3419
3453
 
3420
3454
  `);
3421
3455
  return editor.setFragmentData = (data, originEvent) => {
@@ -3447,7 +3481,6 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3447
3481
  const fragment = editor.getFragment(), portableText = fromSlateValue(fragment, blockTypeName), asJSON = JSON.stringify(portableText), asPlainText = toPlainText(portableText);
3448
3482
  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);
3449
3483
  }, editor.insertPortableTextData = (data) => {
3450
- var _a, _b;
3451
3484
  if (!editor.selection)
3452
3485
  return !1;
3453
3486
  const pText = data.getData("application/x-portable-text"), origin = data.getData("application/x-portable-text-event-origin");
@@ -3461,8 +3494,8 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3461
3494
  spanTypeName,
3462
3495
  schemaTypes
3463
3496
  ), validation = validateValue(parsed, schemaTypes, keyGenerator);
3464
- if (!validation.valid && !((_a = validation.resolution) != null && _a.autoResolve)) {
3465
- const errorDescription = `${(_b = validation.resolution) == null ? void 0 : _b.description}`;
3497
+ if (!validation.valid && !validation.resolution?.autoResolve) {
3498
+ const errorDescription = `${validation.resolution?.description}`;
3466
3499
  return change$.next({
3467
3500
  type: "error",
3468
3501
  level: "warning",
@@ -3476,7 +3509,6 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3476
3509
  }
3477
3510
  return !1;
3478
3511
  }, editor.insertTextOrHTMLData = (data) => {
3479
- var _a;
3480
3512
  if (!editor.selection)
3481
3513
  return debug$7("No selection, not inserting"), !1;
3482
3514
  change$.next({ type: "loading", isLoading: !0 });
@@ -3502,7 +3534,7 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3502
3534
  const validation = validateValue(portableText, schemaTypes, keyGenerator);
3503
3535
  if (!validation.valid) {
3504
3536
  const errorDescription = `Could not validate the resulting portable text to insert.
3505
- ${(_a = validation.resolution) == null ? void 0 : _a.description}
3537
+ ${validation.resolution?.description}
3506
3538
  Try to insert as plain text (shift-paste) instead.`;
3507
3539
  return change$.next({
3508
3540
  type: "error",
@@ -3767,15 +3799,14 @@ function useSyncValue(props) {
3767
3799
  isChanged = !0;
3768
3800
  }
3769
3801
  slateValueFromProps.forEach((currentBlock, currentBlockIndex) => {
3770
- var _a, _b, _c, _d, _e;
3771
3802
  const oldBlock = slateEditor.children[currentBlockIndex];
3772
3803
  if (oldBlock && !isEqual__default.default(currentBlock, oldBlock) && isValid) {
3773
3804
  const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, schemaTypes, keyGenerator);
3774
- !validation.valid && (_a = validation.resolution) != null && _a.autoResolve && ((_b = validation.resolution) == null ? void 0 : _b.patches.length) > 0 && !readOnly && previousValue.current && previousValue.current !== value && (console.warn(
3775
- `${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${(_c = validation.resolution) == null ? void 0 : _c.description}`
3805
+ !validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !readOnly && previousValue.current && previousValue.current !== value && (console.warn(
3806
+ `${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`
3776
3807
  ), validation.resolution.patches.forEach((patch) => {
3777
3808
  change$.next({ type: "patch", patch });
3778
- })), validation.valid || (_d = validation.resolution) != null && _d.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({
3809
+ })), 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({
3779
3810
  type: "invalidValue",
3780
3811
  resolution: validation.resolution,
3781
3812
  value
@@ -3786,7 +3817,7 @@ function useSyncValue(props) {
3786
3817
  debug$5.enabled && debug$5(
3787
3818
  "Validating and inserting new block in the end of the value",
3788
3819
  currentBlock
3789
- ), validation.valid || (_e = validation.resolution) != null && _e.autoResolve ? withPreserveKeys(slateEditor, () => {
3820
+ ), validation.valid || validation.resolution?.autoResolve ? withPreserveKeys(slateEditor, () => {
3790
3821
  slate.Transforms.insertNodes(slateEditor, currentBlock, {
3791
3822
  at: [currentBlockIndex]
3792
3823
  });
@@ -3856,9 +3887,9 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3856
3887
  }));
3857
3888
  }
3858
3889
  ), currentBlock.children.forEach((currentBlockChild, currentBlockChildIndex) => {
3859
- const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual__default.default(currentBlockChild, oldBlockChild), isTextChanged = !isEqual__default.default(currentBlockChild.text, oldBlockChild == null ? void 0 : oldBlockChild.text), path = [currentBlockIndex, currentBlockChildIndex];
3890
+ const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual__default.default(currentBlockChild, oldBlockChild), isTextChanged = !isEqual__default.default(currentBlockChild.text, oldBlockChild?.text), path = [currentBlockIndex, currentBlockChildIndex];
3860
3891
  if (isChildChanged)
3861
- if (currentBlockChild._key === (oldBlockChild == null ? void 0 : oldBlockChild._key)) {
3892
+ if (currentBlockChild._key === oldBlockChild?._key) {
3862
3893
  debug$5("Updating changed child", currentBlockChild, oldBlockChild), slate.Transforms.setNodes(slateEditor, currentBlockChild, {
3863
3894
  at: path
3864
3895
  });
@@ -3974,16 +4005,22 @@ function PortableTextEditorSelectionProvider(props) {
3974
4005
  };
3975
4006
  }, [change$]), /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorSelectionContext.Provider, { value: selection, children: props.children });
3976
4007
  }
3977
- var __defProp = Object.defineProperty, __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __publicField = (obj, key, value) => __defNormalProp(obj, typeof key != "symbol" ? key + "" : key, value);
3978
4008
  const debug$2 = debugWithName("component:PortableTextEditor");
3979
4009
  class PortableTextEditor extends react.Component {
4010
+ /**
4011
+ * An observable of all the editor changes.
4012
+ */
4013
+ change$ = new rxjs.Subject();
4014
+ /**
4015
+ * A lookup table for all the relevant schema types for this portable text type.
4016
+ */
4017
+ schemaTypes;
4018
+ /**
4019
+ * The editor API (currently implemented with Slate).
4020
+ */
4021
+ editable;
3980
4022
  constructor(props) {
3981
- if (super(props), __publicField(this, "change$", new rxjs.Subject()), __publicField(this, "schemaTypes"), __publicField(this, "editable"), __publicField(this, "setEditable", (editable) => {
3982
- this.editable = { ...this.editable, ...editable };
3983
- }), __publicField(this, "getValue", () => {
3984
- if (this.editable)
3985
- return this.editable.getValue();
3986
- }), !props.schemaType)
4023
+ if (super(props), !props.schemaType)
3987
4024
  throw new Error('PortableTextEditor: missing "schemaType" property');
3988
4025
  props.incomingPatches$ && console.warn("The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"), this.change$.next({ type: "loading", isLoading: !0 }), this.schemaTypes = getPortableTextMemberSchemaTypes(
3989
4026
  props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)
@@ -3994,6 +4031,13 @@ class PortableTextEditor extends react.Component {
3994
4031
  this.props.schemaType.hasOwnProperty("jsonType") ? this.props.schemaType : compileType(this.props.schemaType)
3995
4032
  )), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this);
3996
4033
  }
4034
+ setEditable = (editable) => {
4035
+ this.editable = { ...this.editable, ...editable };
4036
+ };
4037
+ getValue = () => {
4038
+ if (this.editable)
4039
+ return this.editable.getValue();
4040
+ };
3997
4041
  render() {
3998
4042
  const { onChange, value, children, patches$, incomingPatches$ } = this.props, { change$ } = this, _patches$ = incomingPatches$ || patches$, maxBlocks = typeof this.props.maxBlocks > "u" ? void 0 : parseInt(this.props.maxBlocks.toString(), 10) || void 0, readOnly = !!this.props.readOnly, keyGenerator = this.props.keyGenerator || defaultKeyGenerator;
3999
4043
  return /* @__PURE__ */ jsxRuntime.jsx(
@@ -4019,99 +4063,59 @@ class PortableTextEditor extends react.Component {
4019
4063
  }
4020
4064
  );
4021
4065
  }
4066
+ // Static API methods
4067
+ static activeAnnotations = (editor) => editor && editor.editable ? editor.editable.activeAnnotations() : [];
4068
+ static isAnnotationActive = (editor, annotationType) => editor && editor.editable ? editor.editable.isAnnotationActive(annotationType) : !1;
4069
+ static addAnnotation = (editor, type, value) => editor.editable?.addAnnotation(type, value);
4070
+ static blur = (editor) => {
4071
+ debug$2("Host blurred"), editor.editable?.blur();
4072
+ };
4073
+ static delete = (editor, selection, options) => editor.editable?.delete(selection, options);
4074
+ static findDOMNode = (editor, element) => editor.editable?.findDOMNode(element);
4075
+ static findByPath = (editor, path) => editor.editable?.findByPath(path) || [];
4076
+ static focus = (editor) => {
4077
+ debug$2("Host requesting focus"), editor.editable?.focus();
4078
+ };
4079
+ static focusBlock = (editor) => editor.editable?.focusBlock();
4080
+ static focusChild = (editor) => editor.editable?.focusChild();
4081
+ static getSelection = (editor) => editor.editable ? editor.editable.getSelection() : null;
4082
+ static getValue = (editor) => editor.editable?.getValue();
4083
+ static hasBlockStyle = (editor, blockStyle) => editor.editable?.hasBlockStyle(blockStyle);
4084
+ static hasListStyle = (editor, listStyle) => editor.editable?.hasListStyle(listStyle);
4085
+ static isCollapsedSelection = (editor) => editor.editable?.isCollapsedSelection();
4086
+ static isExpandedSelection = (editor) => editor.editable?.isExpandedSelection();
4087
+ static isMarkActive = (editor, mark) => editor.editable?.isMarkActive(mark);
4088
+ static insertChild = (editor, type, value) => (debug$2("Host inserting child"), editor.editable?.insertChild(type, value));
4089
+ static insertBlock = (editor, type, value) => editor.editable?.insertBlock(type, value);
4090
+ static insertBreak = (editor) => editor.editable?.insertBreak();
4091
+ static isVoid = (editor, element) => editor.editable?.isVoid(element);
4092
+ static isObjectPath = (editor, path) => {
4093
+ if (!path || !Array.isArray(path)) return !1;
4094
+ const isChildObjectEditPath = path.length > 3 && path[1] === "children";
4095
+ return path.length > 1 && path[1] !== "children" || isChildObjectEditPath;
4096
+ };
4097
+ static marks = (editor) => editor.editable?.marks();
4098
+ static select = (editor, selection) => {
4099
+ debug$2("Host setting selection", selection), editor.editable?.select(selection);
4100
+ };
4101
+ static removeAnnotation = (editor, type) => editor.editable?.removeAnnotation(type);
4102
+ static toggleBlockStyle = (editor, blockStyle) => (debug$2("Host is toggling block style"), editor.editable?.toggleBlockStyle(blockStyle));
4103
+ static toggleList = (editor, listStyle) => editor.editable?.toggleList(listStyle);
4104
+ static toggleMark = (editor, mark) => {
4105
+ debug$2("Host toggling mark", mark), editor.editable?.toggleMark(mark);
4106
+ };
4107
+ static getFragment = (editor) => (debug$2("Host getting fragment"), editor.editable?.getFragment());
4108
+ static undo = (editor) => {
4109
+ debug$2("Host undoing"), editor.editable?.undo();
4110
+ };
4111
+ static redo = (editor) => {
4112
+ debug$2("Host redoing"), editor.editable?.redo();
4113
+ };
4114
+ static isSelectionsOverlapping = (editor, selectionA, selectionB) => editor.editable?.isSelectionsOverlapping(selectionA, selectionB);
4022
4115
  }
4023
- __publicField(PortableTextEditor, "activeAnnotations", (editor) => editor && editor.editable ? editor.editable.activeAnnotations() : []), __publicField(PortableTextEditor, "isAnnotationActive", (editor, annotationType) => editor && editor.editable ? editor.editable.isAnnotationActive(annotationType) : !1), __publicField(PortableTextEditor, "addAnnotation", (editor, type, value) => {
4024
- var _a;
4025
- return (_a = editor.editable) == null ? void 0 : _a.addAnnotation(type, value);
4026
- }), __publicField(PortableTextEditor, "blur", (editor) => {
4027
- var _a;
4028
- debug$2("Host blurred"), (_a = editor.editable) == null || _a.blur();
4029
- }), __publicField(PortableTextEditor, "delete", (editor, selection, options) => {
4030
- var _a;
4031
- return (_a = editor.editable) == null ? void 0 : _a.delete(selection, options);
4032
- }), __publicField(PortableTextEditor, "findDOMNode", (editor, element) => {
4033
- var _a;
4034
- return (_a = editor.editable) == null ? void 0 : _a.findDOMNode(element);
4035
- }), __publicField(PortableTextEditor, "findByPath", (editor, path) => {
4036
- var _a;
4037
- return ((_a = editor.editable) == null ? void 0 : _a.findByPath(path)) || [];
4038
- }), __publicField(PortableTextEditor, "focus", (editor) => {
4039
- var _a;
4040
- debug$2("Host requesting focus"), (_a = editor.editable) == null || _a.focus();
4041
- }), __publicField(PortableTextEditor, "focusBlock", (editor) => {
4042
- var _a;
4043
- return (_a = editor.editable) == null ? void 0 : _a.focusBlock();
4044
- }), __publicField(PortableTextEditor, "focusChild", (editor) => {
4045
- var _a;
4046
- return (_a = editor.editable) == null ? void 0 : _a.focusChild();
4047
- }), __publicField(PortableTextEditor, "getSelection", (editor) => editor.editable ? editor.editable.getSelection() : null), __publicField(PortableTextEditor, "getValue", (editor) => {
4048
- var _a;
4049
- return (_a = editor.editable) == null ? void 0 : _a.getValue();
4050
- }), __publicField(PortableTextEditor, "hasBlockStyle", (editor, blockStyle) => {
4051
- var _a;
4052
- return (_a = editor.editable) == null ? void 0 : _a.hasBlockStyle(blockStyle);
4053
- }), __publicField(PortableTextEditor, "hasListStyle", (editor, listStyle) => {
4054
- var _a;
4055
- return (_a = editor.editable) == null ? void 0 : _a.hasListStyle(listStyle);
4056
- }), __publicField(PortableTextEditor, "isCollapsedSelection", (editor) => {
4057
- var _a;
4058
- return (_a = editor.editable) == null ? void 0 : _a.isCollapsedSelection();
4059
- }), __publicField(PortableTextEditor, "isExpandedSelection", (editor) => {
4060
- var _a;
4061
- return (_a = editor.editable) == null ? void 0 : _a.isExpandedSelection();
4062
- }), __publicField(PortableTextEditor, "isMarkActive", (editor, mark) => {
4063
- var _a;
4064
- return (_a = editor.editable) == null ? void 0 : _a.isMarkActive(mark);
4065
- }), __publicField(PortableTextEditor, "insertChild", (editor, type, value) => {
4066
- var _a;
4067
- return debug$2("Host inserting child"), (_a = editor.editable) == null ? void 0 : _a.insertChild(type, value);
4068
- }), __publicField(PortableTextEditor, "insertBlock", (editor, type, value) => {
4069
- var _a;
4070
- return (_a = editor.editable) == null ? void 0 : _a.insertBlock(type, value);
4071
- }), __publicField(PortableTextEditor, "insertBreak", (editor) => {
4072
- var _a;
4073
- return (_a = editor.editable) == null ? void 0 : _a.insertBreak();
4074
- }), __publicField(PortableTextEditor, "isVoid", (editor, element) => {
4075
- var _a;
4076
- return (_a = editor.editable) == null ? void 0 : _a.isVoid(element);
4077
- }), __publicField(PortableTextEditor, "isObjectPath", (editor, path) => {
4078
- if (!path || !Array.isArray(path)) return !1;
4079
- const isChildObjectEditPath = path.length > 3 && path[1] === "children";
4080
- return path.length > 1 && path[1] !== "children" || isChildObjectEditPath;
4081
- }), __publicField(PortableTextEditor, "marks", (editor) => {
4082
- var _a;
4083
- return (_a = editor.editable) == null ? void 0 : _a.marks();
4084
- }), __publicField(PortableTextEditor, "select", (editor, selection) => {
4085
- var _a;
4086
- debug$2("Host setting selection", selection), (_a = editor.editable) == null || _a.select(selection);
4087
- }), __publicField(PortableTextEditor, "removeAnnotation", (editor, type) => {
4088
- var _a;
4089
- return (_a = editor.editable) == null ? void 0 : _a.removeAnnotation(type);
4090
- }), __publicField(PortableTextEditor, "toggleBlockStyle", (editor, blockStyle) => {
4091
- var _a;
4092
- return debug$2("Host is toggling block style"), (_a = editor.editable) == null ? void 0 : _a.toggleBlockStyle(blockStyle);
4093
- }), __publicField(PortableTextEditor, "toggleList", (editor, listStyle) => {
4094
- var _a;
4095
- return (_a = editor.editable) == null ? void 0 : _a.toggleList(listStyle);
4096
- }), __publicField(PortableTextEditor, "toggleMark", (editor, mark) => {
4097
- var _a;
4098
- debug$2("Host toggling mark", mark), (_a = editor.editable) == null || _a.toggleMark(mark);
4099
- }), __publicField(PortableTextEditor, "getFragment", (editor) => {
4100
- var _a;
4101
- return debug$2("Host getting fragment"), (_a = editor.editable) == null ? void 0 : _a.getFragment();
4102
- }), __publicField(PortableTextEditor, "undo", (editor) => {
4103
- var _a;
4104
- debug$2("Host undoing"), (_a = editor.editable) == null || _a.undo();
4105
- }), __publicField(PortableTextEditor, "redo", (editor) => {
4106
- var _a;
4107
- debug$2("Host redoing"), (_a = editor.editable) == null || _a.redo();
4108
- }), __publicField(PortableTextEditor, "isSelectionsOverlapping", (editor, selectionA, selectionB) => {
4109
- var _a;
4110
- return (_a = editor.editable) == null ? void 0 : _a.isSelectionsOverlapping(selectionA, selectionB);
4111
- });
4112
4116
  const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (props) => {
4113
4117
  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(
4114
- () => block ? [{ _key: block == null ? void 0 : block._key }, "children", { _key: leaf._key }] : [],
4118
+ () => block ? [{ _key: block?._key }, "children", { _key: leaf._key }] : [],
4115
4119
  [block, leaf._key]
4116
4120
  ), decoratorValues = react.useMemo(
4117
4121
  () => schemaTypes.decorators.map((dec) => dec.value),
@@ -4121,10 +4125,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4121
4125
  [decoratorValues, leaf.marks]
4122
4126
  ), annotationMarks = Array.isArray(leaf.marks) ? leaf.marks : EMPTY_MARKS, annotations = react.useMemo(
4123
4127
  () => annotationMarks.map(
4124
- (mark) => {
4125
- var _a;
4126
- return !decoratorValues.includes(mark) && ((_a = block == null ? void 0 : block.markDefs) == null ? void 0 : _a.find((def) => def._key === mark));
4127
- }
4128
+ (mark) => !decoratorValues.includes(mark) && block?.markDefs?.find((def) => def._key === mark)
4128
4129
  ).filter(Boolean),
4129
4130
  [annotationMarks, block, decoratorValues]
4130
4131
  ), shouldTrackSelectionAndFocus = annotations.length > 0 && blockSelected;
@@ -4435,7 +4436,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4435
4436
  debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
4436
4437
  return;
4437
4438
  }
4438
- const value = PortableTextEditor.getValue(portableTextEditor), ptRange = toPortableTextRange(value, slateEditor.selection, schemaTypes), path = (ptRange == null ? void 0 : ptRange.focus.path) || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
4439
+ const value = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(value, slateEditor.selection, schemaTypes)?.focus.path || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
4439
4440
  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) => {
4440
4441
  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(
4441
4442
  toSlateValue(result.insert, { schemaTypes })
@@ -4491,7 +4492,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4491
4492
  const existingDOMRange = domSelection.getRangeAt(0);
4492
4493
  try {
4493
4494
  const newDOMRange = slateReact.ReactEditor.toDOMRange(slateEditor, slateEditor.selection);
4494
- (newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug("DOM range out of sync, validating selection"), domSelection == null || domSelection.removeAllRanges(), domSelection.addRange(newDOMRange));
4495
+ (newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
4495
4496
  } catch {
4496
4497
  debug("Could not resolve selection, selecting top document"), slate.Transforms.deselect(slateEditor), slateEditor.children.length > 0 && slate.Transforms.select(slateEditor, [0, 0]), slateEditor.onChange();
4497
4498
  }