@portabletext/editor 1.1.3 → 1.1.5

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 (32) hide show
  1. package/lib/index.d.mts +228 -30
  2. package/lib/index.d.ts +228 -30
  3. package/lib/index.esm.js +169 -112
  4. package/lib/index.esm.js.map +1 -1
  5. package/lib/index.js +169 -112
  6. package/lib/index.js.map +1 -1
  7. package/lib/index.mjs +169 -112
  8. package/lib/index.mjs.map +1 -1
  9. package/package.json +24 -19
  10. package/src/editor/Editable.tsx +6 -11
  11. package/src/editor/PortableTextEditor.tsx +26 -30
  12. package/src/editor/__tests__/self-solving.test.tsx +1 -1
  13. package/src/editor/components/SlateContainer.tsx +2 -13
  14. package/src/editor/components/Synchronizer.tsx +0 -3
  15. package/src/editor/editor-machine.ts +7 -2
  16. package/src/editor/hooks/useSyncValue.ts +3 -5
  17. package/src/editor/key-generator.ts +6 -0
  18. package/src/editor/plugins/createWithEditableAPI.ts +8 -5
  19. package/src/editor/plugins/createWithHotKeys.ts +1 -0
  20. package/src/editor/plugins/createWithInsertBreak.ts +57 -9
  21. package/src/editor/plugins/createWithInsertData.ts +7 -4
  22. package/src/editor/plugins/createWithObjectKeys.ts +12 -5
  23. package/src/editor/plugins/createWithPatches.ts +0 -1
  24. package/src/editor/plugins/createWithPortableTextMarkModel.ts +106 -23
  25. package/src/editor/plugins/createWithSchemaTypes.ts +3 -4
  26. package/src/editor/plugins/createWithUtils.ts +5 -4
  27. package/src/editor/plugins/index.ts +5 -13
  28. package/src/index.ts +2 -2
  29. package/src/types/options.ts +0 -1
  30. package/src/utils/__tests__/operationToPatches.test.ts +0 -2
  31. package/src/utils/__tests__/patchToOperations.test.ts +1 -2
  32. package/src/editor/hooks/usePortableTextEditorKeyGenerator.ts +0 -27
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), types = require("@sanity/types"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), schema = require("@sanity/schema"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), flatten = require("lodash/flatten.js"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), content = require("@sanity/util/content"), debounce = require("lodash/debounce.js");
3
+ var jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), types = require("@sanity/types"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), schema = require("@sanity/schema"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), flatten = require("lodash/flatten.js"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), debounce = require("lodash/debounce.js"), content = require("@sanity/util/content");
4
4
  function _interopDefaultCompat(e) {
5
5
  return e && typeof e == "object" && "default" in e ? e : { default: e };
6
6
  }
@@ -1067,7 +1067,7 @@ function createOperationToPatches(types2) {
1067
1067
  };
1068
1068
  }
1069
1069
  const debug$j = debugWithName("API:editable");
1070
- function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1070
+ function createWithEditableAPI(editorActor, portableTextEditor, types$1) {
1071
1071
  return function(editor) {
1072
1072
  return portableTextEditor.setEditable({
1073
1073
  focus: () => {
@@ -1147,11 +1147,11 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1147
1147
  const child = toSlateValue(
1148
1148
  [
1149
1149
  {
1150
- _key: keyGenerator(),
1150
+ _key: editorActor.getSnapshot().context.keyGenerator(),
1151
1151
  _type: types$1.block.name,
1152
1152
  children: [
1153
1153
  {
1154
- _key: keyGenerator(),
1154
+ _key: editorActor.getSnapshot().context.keyGenerator(),
1155
1155
  _type: type.name,
1156
1156
  ...value || {}
1157
1157
  }
@@ -1179,7 +1179,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1179
1179
  const block = toSlateValue(
1180
1180
  [
1181
1181
  {
1182
- _key: keyGenerator(),
1182
+ _key: editorActor.getSnapshot().context.keyGenerator(),
1183
1183
  _type: type.name,
1184
1184
  ...value || {}
1185
1185
  }
@@ -1343,7 +1343,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1343
1343
  for (const [block, blockPath] of selectedBlocks) {
1344
1344
  if (block.children.length === 0 || block.children.length === 1 && block.children[0].text === "")
1345
1345
  continue;
1346
- const annotationKey = keyGenerator(), markDefs = block.markDefs ?? [];
1346
+ const annotationKey = editorActor.getSnapshot().context.keyGenerator(), markDefs = block.markDefs ?? [];
1347
1347
  markDefs.find(
1348
1348
  (markDef) => markDef._type === type.name && markDef._key === annotationKey
1349
1349
  ) === void 0 && (slate.Transforms.setNodes(
@@ -1552,7 +1552,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1552
1552
  }), editor;
1553
1553
  };
1554
1554
  }
1555
- function createWithInsertBreak(types2, keyGenerator) {
1555
+ function createWithInsertBreak(editorActor, types2) {
1556
1556
  return function(editor) {
1557
1557
  const { insertBreak } = editor;
1558
1558
  return editor.insertBreak = () => {
@@ -1560,19 +1560,29 @@ function createWithInsertBreak(types2, keyGenerator) {
1560
1560
  insertBreak();
1561
1561
  return;
1562
1562
  }
1563
- const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
1563
+ const [focusSpan] = Array.from(
1564
+ slate.Editor.nodes(editor, {
1565
+ mode: "lowest",
1566
+ at: editor.selection.focus,
1567
+ match: (n) => editor.isTextSpan(n),
1568
+ voids: !1
1569
+ })
1570
+ )[0] ?? [void 0], focusDecorators = focusSpan.marks?.filter(
1571
+ (mark) => types2.decorators.some((decorator) => decorator.value === mark)
1572
+ ) ?? [], focusAnnotations = focusSpan.marks?.filter(
1573
+ (mark) => !types2.decorators.some((decorator) => decorator.value === mark)
1574
+ ) ?? [], focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
1564
1575
  if (editor.isTextBlock(focusBlock)) {
1565
1576
  const [start, end] = slate.Range.edges(editor.selection), isEndAtStartOfBlock = isEqual__default.default(end, {
1566
1577
  path: [...focusBlockPath, 0],
1567
1578
  offset: 0
1568
1579
  });
1569
1580
  if (isEndAtStartOfBlock && slate.Range.isCollapsed(editor.selection)) {
1570
- const focusDecorators = editor.isTextSpan(focusBlock.children[0]) ? (focusBlock.children[0].marks ?? []).filter(
1571
- (mark) => types2.decorators.some((decorator) => decorator.value === mark)
1572
- ) : [];
1573
1581
  slate.Editor.insertNode(
1574
1582
  editor,
1575
- editor.pteCreateTextBlock({ decorators: focusDecorators })
1583
+ editor.pteCreateTextBlock({
1584
+ decorators: focusAnnotations.length === 0 ? focusDecorators : []
1585
+ })
1576
1586
  );
1577
1587
  const [nextBlockPath] = slate.Path.next(focusBlockPath);
1578
1588
  slate.Transforms.select(editor, {
@@ -1585,6 +1595,24 @@ function createWithInsertBreak(types2, keyGenerator) {
1585
1595
  path: [...focusBlockPath, focusBlock.children.length - 1],
1586
1596
  offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
1587
1597
  });
1598
+ if (isStartAtEndOfBlock && slate.Range.isCollapsed(editor.selection) && focusDecorators.length > 0 && focusAnnotations.length > 0) {
1599
+ slate.Editor.withoutNormalizing(editor, () => {
1600
+ if (!editor.selection)
1601
+ return;
1602
+ slate.Editor.insertNode(
1603
+ editor,
1604
+ editor.pteCreateTextBlock({
1605
+ decorators: []
1606
+ })
1607
+ );
1608
+ const [nextBlockPath] = slate.Path.next(focusBlockPath);
1609
+ slate.Transforms.setSelection(editor, {
1610
+ anchor: { path: [nextBlockPath, 0], offset: 0 },
1611
+ focus: { path: [nextBlockPath, 0], offset: 0 }
1612
+ });
1613
+ }), editor.onChange();
1614
+ return;
1615
+ }
1588
1616
  if (!isEndAtStartOfBlock && !isStartAtEndOfBlock) {
1589
1617
  slate.Editor.withoutNormalizing(editor, () => {
1590
1618
  if (!editor.selection)
@@ -1613,7 +1641,10 @@ function createWithInsertBreak(types2, keyGenerator) {
1613
1641
  (decorator) => decorator.value === mark
1614
1642
  ) || prevNodeSpans.some(
1615
1643
  (prevNodeSpan) => prevNodeSpan.marks?.includes(mark)
1616
- ) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
1644
+ ) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(
1645
+ mark,
1646
+ editorActor.getSnapshot().context.keyGenerator()
1647
+ );
1617
1648
  const newMarks = marks.map(
1618
1649
  (mark) => newMarkDefKeys.get(mark) ?? mark
1619
1650
  );
@@ -1694,7 +1725,7 @@ function createWithMaxBlocks(maxBlocks) {
1694
1725
  }, editor;
1695
1726
  };
1696
1727
  }
1697
- function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
1728
+ function createWithObjectKeys(editorActor, schemaTypes) {
1698
1729
  return function(editor) {
1699
1730
  const { apply: apply2, normalizeNode } = editor;
1700
1731
  return editor.apply = (operation) => {
@@ -1711,7 +1742,7 @@ function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
1711
1742
  ...operation,
1712
1743
  properties: {
1713
1744
  ...operation.properties,
1714
- _key: keyGenerator()
1745
+ _key: editorActor.getSnapshot().context.keyGenerator()
1715
1746
  }
1716
1747
  });
1717
1748
  return;
@@ -1721,7 +1752,7 @@ function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
1721
1752
  ...operation,
1722
1753
  node: {
1723
1754
  ...operation.node,
1724
- _key: keyGenerator()
1755
+ _key: editorActor.getSnapshot().context.keyGenerator()
1725
1756
  }
1726
1757
  });
1727
1758
  return;
@@ -1731,12 +1762,20 @@ function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
1731
1762
  const [node, path] = entry;
1732
1763
  if (slate.Element.isElement(node) && node._type === schemaTypes.block.name) {
1733
1764
  if (!node._key) {
1734
- editorActor.send({ type: "normalizing" }), slate.Transforms.setNodes(editor, { _key: keyGenerator() }, { at: path }), editorActor.send({ type: "done normalizing" });
1765
+ editorActor.send({ type: "normalizing" }), slate.Transforms.setNodes(
1766
+ editor,
1767
+ { _key: editorActor.getSnapshot().context.keyGenerator() },
1768
+ { at: path }
1769
+ ), editorActor.send({ type: "done normalizing" });
1735
1770
  return;
1736
1771
  }
1737
1772
  for (const [child, childPath] of slate.Node.children(editor, path))
1738
1773
  if (!child._key) {
1739
- editorActor.send({ type: "normalizing" }), slate.Transforms.setNodes(editor, { _key: keyGenerator() }, { at: childPath }), editorActor.send({ type: "done normalizing" });
1774
+ editorActor.send({ type: "normalizing" }), slate.Transforms.setNodes(
1775
+ editor,
1776
+ { _key: editorActor.getSnapshot().context.keyGenerator() },
1777
+ { at: childPath }
1778
+ ), editorActor.send({ type: "done normalizing" });
1740
1779
  return;
1741
1780
  }
1742
1781
  }
@@ -3228,7 +3267,7 @@ function isPortableTextBlock(node) {
3228
3267
  );
3229
3268
  }
3230
3269
  const debug$c = debugWithName("plugin:withPortableTextMarkModel");
3231
- function createWithPortableTextMarkModel(editorActor, types2, keyGenerator) {
3270
+ function createWithPortableTextMarkModel(editorActor, types2) {
3232
3271
  return function(editor) {
3233
3272
  const { apply: apply2, normalizeNode } = editor, decorators = types2.decorators.map((t) => t.value), forceNewSelection = () => {
3234
3273
  editor.selection && (slate.Transforms.select(editor, { ...editor.selection }), editor.selection = { ...editor.selection });
@@ -3355,24 +3394,65 @@ function createWithPortableTextMarkModel(editorActor, types2, keyGenerator) {
3355
3394
  if (op.type === "insert_text") {
3356
3395
  const { selection } = editor, collapsedSelection = selection ? slate.Range.isCollapsed(selection) : !1;
3357
3396
  if (selection && collapsedSelection) {
3358
- const [span] = Array.from(
3397
+ const [_block, blockPath] = slate.Editor.node(editor, selection, {
3398
+ depth: 1
3399
+ }), [span, spanPath] = Array.from(
3359
3400
  slate.Editor.nodes(editor, {
3360
3401
  mode: "lowest",
3361
3402
  at: selection.focus,
3362
3403
  match: (n) => editor.isTextSpan(n),
3363
3404
  voids: !1
3364
3405
  })
3365
- )[0], marks = span.marks ?? [], marksWithoutAnnotations = marks.filter(
3406
+ )[0] ?? [void 0, void 0], marks = span.marks ?? [], marksWithoutAnnotations = marks.filter(
3366
3407
  (mark) => decorators.includes(mark)
3367
- );
3368
- if (marks.length > marksWithoutAnnotations.length && (selection.anchor.offset === 0 || span.text.length === selection.focus.offset)) {
3369
- slate.Transforms.insertNodes(editor, {
3370
- _type: "span",
3371
- _key: keyGenerator(),
3372
- text: op.text,
3373
- marks: marksWithoutAnnotations
3374
- }), debug$c("Inserting text at end of annotation");
3375
- return;
3408
+ ), spanHasAnnotations = marks.length > marksWithoutAnnotations.length, spanIsEmpty = span.text.length === 0, atTheBeginningOfSpan = selection.anchor.offset === 0, atTheEndOfSpan = selection.anchor.offset === span.text.length;
3409
+ let previousSpan, nextSpan;
3410
+ for (const [child, childPath] of slate.Node.children(editor, blockPath, {
3411
+ reverse: !0
3412
+ }))
3413
+ if (editor.isTextSpan(child) && slate.Path.isBefore(childPath, spanPath)) {
3414
+ previousSpan = child;
3415
+ break;
3416
+ }
3417
+ for (const [child, childPath] of slate.Node.children(editor, blockPath))
3418
+ if (editor.isTextSpan(child) && slate.Path.isAfter(childPath, spanPath)) {
3419
+ nextSpan = child;
3420
+ break;
3421
+ }
3422
+ const previousSpanHasSameAnnotation = previousSpan ? previousSpan.marks?.some(
3423
+ (mark) => !decorators.includes(mark) && marks.includes(mark)
3424
+ ) : !1, previousSpanHasSameMarks = previousSpan ? previousSpan.marks?.every((mark) => marks.includes(mark)) : !1, nextSpanHasSameAnnotation = nextSpan ? nextSpan.marks?.some(
3425
+ (mark) => !decorators.includes(mark) && marks.includes(mark)
3426
+ ) : !1, nextSpanHasSameMarks = nextSpan ? nextSpan.marks?.every((mark) => marks.includes(mark)) : !1;
3427
+ if (spanHasAnnotations && !spanIsEmpty) {
3428
+ if (atTheBeginningOfSpan) {
3429
+ previousSpanHasSameMarks ? slate.Transforms.insertNodes(editor, {
3430
+ _type: "span",
3431
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3432
+ text: op.text,
3433
+ marks: previousSpan?.marks ?? []
3434
+ }) : previousSpanHasSameAnnotation ? apply2(op) : slate.Transforms.insertNodes(editor, {
3435
+ _type: "span",
3436
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3437
+ text: op.text,
3438
+ marks: []
3439
+ });
3440
+ return;
3441
+ }
3442
+ if (atTheEndOfSpan) {
3443
+ nextSpanHasSameMarks ? slate.Transforms.insertNodes(editor, {
3444
+ _type: "span",
3445
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3446
+ text: op.text,
3447
+ marks: nextSpan?.marks ?? []
3448
+ }) : nextSpanHasSameAnnotation ? apply2(op) : slate.Transforms.insertNodes(editor, {
3449
+ _type: "span",
3450
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3451
+ text: op.text,
3452
+ marks: []
3453
+ });
3454
+ return;
3455
+ }
3376
3456
  }
3377
3457
  }
3378
3458
  }
@@ -3600,8 +3680,7 @@ function createWithPortableTextSelections(editorActor, types2) {
3600
3680
  const debug$a = debugWithName("plugin:withSchemaTypes");
3601
3681
  function createWithSchemaTypes({
3602
3682
  editorActor,
3603
- schemaTypes,
3604
- keyGenerator
3683
+ schemaTypes
3605
3684
  }) {
3606
3685
  return function(editor) {
3607
3686
  editor.isTextBlock = (value) => types.isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => types.isPortableTextSpan(value) && value._type === schemaTypes.span.name, editor.isListBlock = (value) => types.isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
@@ -3610,7 +3689,7 @@ function createWithSchemaTypes({
3610
3689
  const [node, path] = entry;
3611
3690
  if (node._type === void 0 && path.length === 2) {
3612
3691
  debug$a("Setting span type on text node without a type");
3613
- const span = node, key = span._key || keyGenerator();
3692
+ const span = node, key = span._key || editorActor.getSnapshot().context.keyGenerator();
3614
3693
  editorActor.send({ type: "normalizing" }), slate.Transforms.setNodes(
3615
3694
  editor,
3616
3695
  { ...span, _type: schemaTypes.span.name, _key: key },
@@ -3620,7 +3699,7 @@ function createWithSchemaTypes({
3620
3699
  }
3621
3700
  if (node._key === void 0 && (path.length === 1 || path.length === 2)) {
3622
3701
  debug$a("Setting missing key on child node without a key");
3623
- const key = keyGenerator();
3702
+ const key = editorActor.getSnapshot().context.keyGenerator();
3624
3703
  editorActor.send({ type: "normalizing" }), slate.Transforms.setNodes(editor, { _key: key }, { at: path }), editorActor.send({ type: "done normalizing" });
3625
3704
  return;
3626
3705
  }
@@ -3630,8 +3709,8 @@ function createWithSchemaTypes({
3630
3709
  }
3631
3710
  const debug$9 = debugWithName("plugin:withUtils");
3632
3711
  function createWithUtils({
3712
+ editorActor,
3633
3713
  schemaTypes,
3634
- keyGenerator,
3635
3714
  portableTextEditor
3636
3715
  }) {
3637
3716
  return function(editor) {
@@ -3657,13 +3736,13 @@ function createWithUtils({
3657
3736
  [
3658
3737
  {
3659
3738
  _type: schemaTypes.block.name,
3660
- _key: keyGenerator(),
3739
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3661
3740
  style: schemaTypes.styles[0].value || "normal",
3662
3741
  markDefs: [],
3663
3742
  children: [
3664
3743
  {
3665
3744
  _type: "span",
3666
- _key: keyGenerator(),
3745
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3667
3746
  text: "",
3668
3747
  marks: options.decorators.filter(
3669
3748
  (decorator) => schemaTypes.decorators.find(({ value }) => value === decorator)
@@ -3731,7 +3810,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
3731
3810
  {
3732
3811
  at: nextPath
3733
3812
  }
3734
- ), editor.onChange();
3813
+ ), slate.Transforms.select(editor, { path: [...nextPath, 0], offset: 0 }), editor.onChange();
3735
3814
  return;
3736
3815
  }
3737
3816
  }
@@ -4074,7 +4153,7 @@ function validateValue(value, types$1, keyGenerator) {
4074
4153
  }) && (valid = !1), { valid, resolution, value });
4075
4154
  }
4076
4155
  const debug$7 = debugWithName("plugin:withInsertData");
4077
- function createWithInsertData(editorActor, schemaTypes, keyGenerator) {
4156
+ function createWithInsertData(editorActor, schemaTypes) {
4078
4157
  return function(editor) {
4079
4158
  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(`
4080
4159
 
@@ -4122,10 +4201,14 @@ function createWithInsertData(editorActor, schemaTypes, keyGenerator) {
4122
4201
  const slateValue = _regenerateKeys(
4123
4202
  editor,
4124
4203
  toSlateValue(parsed, { schemaTypes }),
4125
- keyGenerator,
4204
+ editorActor.getSnapshot().context.keyGenerator,
4126
4205
  spanTypeName,
4127
4206
  schemaTypes
4128
- ), validation = validateValue(parsed, schemaTypes, keyGenerator);
4207
+ ), validation = validateValue(
4208
+ parsed,
4209
+ schemaTypes,
4210
+ editorActor.getSnapshot().context.keyGenerator
4211
+ );
4129
4212
  if (!validation.valid && !validation.resolution?.autoResolve) {
4130
4213
  const errorDescription = `${validation.resolution?.description}`;
4131
4214
  return editorActor.send({
@@ -4166,7 +4249,7 @@ function createWithInsertData(editorActor, schemaTypes, keyGenerator) {
4166
4249
  const validation = validateValue(
4167
4250
  portableText,
4168
4251
  schemaTypes,
4169
- keyGenerator
4252
+ editorActor.getSnapshot().context.keyGenerator
4170
4253
  );
4171
4254
  if (!validation.valid) {
4172
4255
  const errorDescription = `Could not validate the resulting portable text to insert.
@@ -4270,27 +4353,21 @@ function _insertFragment(editor, fragment, schemaTypes) {
4270
4353
  }), editor.onChange();
4271
4354
  }
4272
4355
  const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, options) => {
4273
- const e = editor, { keyGenerator, portableTextEditor, patches$, readOnly, maxBlocks } = options, { editorActor, schemaTypes } = portableTextEditor;
4356
+ const e = editor, { portableTextEditor, patches$, readOnly, maxBlocks } = options, { editorActor, schemaTypes } = portableTextEditor;
4274
4357
  e.subscriptions = [], e.destroy ? e.destroy() : originalFnMap.set(e, {
4275
4358
  apply: e.apply,
4276
4359
  onChange: e.onChange,
4277
4360
  normalizeNode: e.normalizeNode
4278
4361
  });
4279
- const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(
4280
- editorActor,
4281
- schemaTypes,
4282
- keyGenerator
4283
- ), withSchemaTypes = createWithSchemaTypes({
4362
+ const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(editorActor, schemaTypes), withSchemaTypes = createWithSchemaTypes({
4284
4363
  editorActor,
4285
- schemaTypes,
4286
- keyGenerator
4364
+ schemaTypes
4287
4365
  }), withEditableAPI = createWithEditableAPI(
4366
+ editorActor,
4288
4367
  portableTextEditor,
4289
- schemaTypes,
4290
- keyGenerator
4368
+ schemaTypes
4291
4369
  ), withPatches = createWithPatches({
4292
4370
  editorActor,
4293
- keyGenerator,
4294
4371
  patches$,
4295
4372
  patchFunctions: operationToPatches,
4296
4373
  readOnly,
@@ -4301,13 +4378,12 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4301
4378
  blockSchemaType: schemaTypes.block
4302
4379
  }), withPortableTextMarkModel = createWithPortableTextMarkModel(
4303
4380
  editorActor,
4304
- schemaTypes,
4305
- keyGenerator
4381
+ schemaTypes
4306
4382
  ), withPortableTextBlockStyle = createWithPortableTextBlockStyle(
4307
4383
  editorActor,
4308
4384
  schemaTypes
4309
- ), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(schemaTypes, keyGenerator), withUtils = createWithUtils({
4310
- keyGenerator,
4385
+ ), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(editorActor, schemaTypes), withUtils = createWithUtils({
4386
+ editorActor,
4311
4387
  schemaTypes,
4312
4388
  portableTextEditor
4313
4389
  }), withPortableTextSelections = createWithPortableTextSelections(
@@ -4375,10 +4451,9 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4375
4451
  };
4376
4452
  }, debug$6 = debugWithName("component:PortableTextEditor:SlateContainer");
4377
4453
  function SlateContainer(props) {
4378
- const { patches$, portableTextEditor, readOnly, maxBlocks, keyGenerator } = props, [[slateEditor, subscribe]] = react.useState(() => {
4454
+ const { patches$, portableTextEditor, readOnly, maxBlocks } = props, [[slateEditor, subscribe]] = react.useState(() => {
4379
4455
  debug$6("Creating new Slate editor instance");
4380
4456
  const { editor, subscribe: _sub } = withPlugins(slateReact.withReact(slate.createEditor()), {
4381
- keyGenerator,
4382
4457
  maxBlocks,
4383
4458
  patches$,
4384
4459
  portableTextEditor,
@@ -4393,33 +4468,18 @@ function SlateContainer(props) {
4393
4468
  };
4394
4469
  }, [subscribe]), react.useEffect(() => {
4395
4470
  debug$6("Re-initializing plugin chain"), withPlugins(slateEditor, {
4396
- keyGenerator,
4397
4471
  maxBlocks,
4398
4472
  patches$,
4399
4473
  portableTextEditor,
4400
4474
  readOnly
4401
4475
  });
4402
- }, [
4403
- keyGenerator,
4404
- portableTextEditor,
4405
- maxBlocks,
4406
- readOnly,
4407
- patches$,
4408
- slateEditor
4409
- ]);
4476
+ }, [portableTextEditor, maxBlocks, readOnly, patches$, slateEditor]);
4410
4477
  const initialValue = react.useMemo(() => [slateEditor.pteCreateTextBlock({ decorators: [] })], [slateEditor]);
4411
4478
  return react.useEffect(() => () => {
4412
4479
  debug$6("Destroying Slate editor"), slateEditor.destroy();
4413
4480
  }, [slateEditor]), /* @__PURE__ */ jsxRuntime.jsx(slateReact.Slate, { editor: slateEditor, initialValue, children: props.children });
4414
4481
  }
4415
- const defaultKeyGenerator = () => content.randomKey(12), PortableTextEditorKeyGeneratorContext = react.createContext(defaultKeyGenerator), usePortableTextEditorKeyGenerator = () => {
4416
- const keyGenerator = react.useContext(PortableTextEditorKeyGeneratorContext);
4417
- if (keyGenerator === void 0)
4418
- throw new Error(
4419
- "The `usePortableTextEditorKeyGenerator` hook must be used inside the <PortableTextEditor> component's context."
4420
- );
4421
- return keyGenerator;
4422
- }, PortableTextEditorReadOnlyContext = react.createContext(!1), usePortableTextEditorReadOnlyStatus = () => {
4482
+ const PortableTextEditorReadOnlyContext = react.createContext(!1), usePortableTextEditorReadOnlyStatus = () => {
4423
4483
  const readOnly = react.useContext(PortableTextEditorReadOnlyContext);
4424
4484
  if (readOnly === void 0)
4425
4485
  throw new Error(
@@ -4428,7 +4488,7 @@ const defaultKeyGenerator = () => content.randomKey(12), PortableTextEditorKeyGe
4428
4488
  return readOnly;
4429
4489
  }, debug$5 = debugWithName("hook:useSyncValue"), CURRENT_VALUE = /* @__PURE__ */ new WeakMap();
4430
4490
  function useSyncValue(props) {
4431
- const { editorActor, portableTextEditor, readOnly, keyGenerator } = props, { schemaTypes } = portableTextEditor, previousValue = react.useRef(), slateEditor = slateReact.useSlate(), updateValueFunctionRef = react.useRef(), updateFromCurrentValue = react.useCallback(() => {
4491
+ const { editorActor, portableTextEditor, readOnly } = props, { schemaTypes } = portableTextEditor, previousValue = react.useRef(), slateEditor = slateReact.useSlate(), updateValueFunctionRef = react.useRef(), updateFromCurrentValue = react.useCallback(() => {
4432
4492
  const currentValue = CURRENT_VALUE.get(portableTextEditor);
4433
4493
  if (previousValue.current === currentValue) {
4434
4494
  debug$5("Value is the same object as previous, not need to sync");
@@ -4494,7 +4554,7 @@ function useSyncValue(props) {
4494
4554
  const validationValue = [value[currentBlockIndex]], validation = validateValue(
4495
4555
  validationValue,
4496
4556
  schemaTypes,
4497
- keyGenerator
4557
+ editorActor.getSnapshot().context.keyGenerator
4498
4558
  );
4499
4559
  !validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !readOnly && previousValue.current && previousValue.current !== value && (console.warn(
4500
4560
  `${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`
@@ -4519,7 +4579,7 @@ function useSyncValue(props) {
4519
4579
  const validationValue = [value[currentBlockIndex]], validation = validateValue(
4520
4580
  validationValue,
4521
4581
  schemaTypes,
4522
- keyGenerator
4582
+ editorActor.getSnapshot().context.keyGenerator
4523
4583
  );
4524
4584
  debug$5.enabled && debug$5(
4525
4585
  "Validating and inserting new block in the end of the value",
@@ -4566,7 +4626,6 @@ function useSyncValue(props) {
4566
4626
  return updateValueFunctionRef.current = updateFunction, updateFunction;
4567
4627
  }, [
4568
4628
  editorActor,
4569
- keyGenerator,
4570
4629
  portableTextEditor,
4571
4630
  readOnly,
4572
4631
  schemaTypes,
@@ -4634,9 +4693,8 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
4634
4693
  }
4635
4694
  const debug$4 = debugWithName("component:PortableTextEditor:Synchronizer"), debugVerbose$1 = debug$4.enabled && !1, FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3;
4636
4695
  function Synchronizer(props) {
4637
- const portableTextEditor = usePortableTextEditor(), keyGenerator = usePortableTextEditorKeyGenerator(), readOnly = usePortableTextEditorReadOnlyStatus(), { editorActor, getValue, onChange, value } = props, pendingPatches = react.useRef([]), syncValue = useSyncValue({
4696
+ const portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), { editorActor, getValue, onChange, value } = props, pendingPatches = react.useRef([]), syncValue = useSyncValue({
4638
4697
  editorActor,
4639
- keyGenerator,
4640
4698
  portableTextEditor,
4641
4699
  readOnly
4642
4700
  }), slateEditor = slateReact.useSlate();
@@ -4750,7 +4808,8 @@ const networkLogic = xstate.fromCallback(({ sendBack }) => {
4750
4808
  types: {
4751
4809
  context: {},
4752
4810
  events: {},
4753
- emitted: {}
4811
+ emitted: {},
4812
+ input: {}
4754
4813
  },
4755
4814
  actions: {
4756
4815
  "emit patch event": xstate.emit(({ event }) => (xstate.assertEvent(event, "patch"), event)),
@@ -4771,9 +4830,10 @@ const networkLogic = xstate.fromCallback(({ sendBack }) => {
4771
4830
  }
4772
4831
  }).createMachine({
4773
4832
  id: "editor",
4774
- context: {
4833
+ context: ({ input }) => ({
4834
+ keyGenerator: input.keyGenerator,
4775
4835
  pendingEvents: []
4776
- },
4836
+ }),
4777
4837
  invoke: {
4778
4838
  id: "networkLogic",
4779
4839
  src: "networkLogic"
@@ -4861,7 +4921,7 @@ function PortableTextEditorSelectionProvider(props) {
4861
4921
  };
4862
4922
  }, [props.editorActor]), /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorSelectionContext.Provider, { value: selection, children: props.children });
4863
4923
  }
4864
- const debug$2 = debugWithName("component:PortableTextEditor");
4924
+ const defaultKeyGenerator = () => content.randomKey(12), debug$2 = debugWithName("component:PortableTextEditor");
4865
4925
  class PortableTextEditor extends react.Component {
4866
4926
  /**
4867
4927
  * @internal
@@ -4885,7 +4945,11 @@ class PortableTextEditor extends react.Component {
4885
4945
  throw new Error('PortableTextEditor: missing "schemaType" property');
4886
4946
  props.incomingPatches$ && console.warn(
4887
4947
  "The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"
4888
- ), this.editorActor = xstate.createActor(editorMachine), this.editorActor.start(), this.schemaTypes = getPortableTextMemberSchemaTypes(
4948
+ ), this.editorActor = xstate.createActor(editorMachine, {
4949
+ input: {
4950
+ keyGenerator: props.keyGenerator || defaultKeyGenerator
4951
+ }
4952
+ }), this.editorActor.start(), this.schemaTypes = getPortableTextMemberSchemaTypes(
4889
4953
  props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)
4890
4954
  );
4891
4955
  }
@@ -4902,35 +4966,28 @@ class PortableTextEditor extends react.Component {
4902
4966
  return this.editable.getValue();
4903
4967
  };
4904
4968
  render() {
4905
- const { value, children, patches$, incomingPatches$ } = this.props, _patches$ = incomingPatches$ || patches$, maxBlocks = typeof this.props.maxBlocks > "u" ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10) || void 0, readOnly = !!this.props.readOnly, keyGenerator = this.props.keyGenerator || defaultKeyGenerator;
4969
+ const { value, children, patches$, incomingPatches$ } = this.props, _patches$ = incomingPatches$ || patches$, maxBlocks = typeof this.props.maxBlocks > "u" ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10) || void 0, readOnly = !!this.props.readOnly;
4906
4970
  return /* @__PURE__ */ jsxRuntime.jsx(
4907
4971
  SlateContainer,
4908
4972
  {
4909
- keyGenerator,
4910
4973
  maxBlocks,
4911
4974
  patches$: _patches$,
4912
4975
  portableTextEditor: this,
4913
4976
  readOnly,
4914
- children: /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorKeyGeneratorContext.Provider, { value: keyGenerator, children: /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorReadOnlyContext.Provider, { value: readOnly, children: /* @__PURE__ */ jsxRuntime.jsxs(
4915
- PortableTextEditorSelectionProvider,
4916
- {
4917
- editorActor: this.editorActor,
4918
- children: [
4919
- /* @__PURE__ */ jsxRuntime.jsx(
4920
- Synchronizer,
4921
- {
4922
- editorActor: this.editorActor,
4923
- getValue: this.getValue,
4924
- onChange: (change) => {
4925
- this.props.onChange(change), this.change$.next(change);
4926
- },
4927
- value
4928
- }
4929
- ),
4930
- children
4931
- ]
4932
- }
4933
- ) }) }) })
4977
+ children: /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorReadOnlyContext.Provider, { value: readOnly, children: /* @__PURE__ */ jsxRuntime.jsxs(PortableTextEditorSelectionProvider, { editorActor: this.editorActor, children: [
4978
+ /* @__PURE__ */ jsxRuntime.jsx(
4979
+ Synchronizer,
4980
+ {
4981
+ editorActor: this.editorActor,
4982
+ getValue: this.getValue,
4983
+ onChange: (change) => {
4984
+ this.props.onChange(change), this.change$.next(change);
4985
+ },
4986
+ value
4987
+ }
4988
+ ),
4989
+ children
4990
+ ] }) }) })
4934
4991
  }
4935
4992
  );
4936
4993
  }
@@ -5196,7 +5253,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5196
5253
  scrollSelectionIntoView,
5197
5254
  spellCheck,
5198
5255
  ...restProps
5199
- } = props, portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), keyGenerator = usePortableTextEditorKeyGenerator(), ref = react.useRef(null), [editableElement, setEditableElement] = react.useState(
5256
+ } = props, portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), ref = react.useRef(null), [editableElement, setEditableElement] = react.useState(
5200
5257
  null
5201
5258
  ), [hasInvalidValue, setHasInvalidValue] = react.useState(!1), [rangeDecorationState, setRangeDecorationsState] = react.useState([]);
5202
5259
  react.useImperativeHandle(
@@ -5204,8 +5261,8 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5204
5261
  () => ref.current
5205
5262
  );
5206
5263
  const rangeDecorationsRef = react.useRef(rangeDecorations), { editorActor, schemaTypes } = portableTextEditor, slateEditor = slateReact.useSlate(), blockTypeName = schemaTypes.block.name, withInsertData = react.useMemo(
5207
- () => createWithInsertData(editorActor, schemaTypes, keyGenerator),
5208
- [editorActor, keyGenerator, schemaTypes]
5264
+ () => createWithInsertData(editorActor, schemaTypes),
5265
+ [editorActor, schemaTypes]
5209
5266
  ), withHotKeys = react.useMemo(
5210
5267
  () => createWithHotkeys(schemaTypes, portableTextEditor, hotkeys),
5211
5268
  [hotkeys, portableTextEditor, schemaTypes]