@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.mjs CHANGED
@@ -20,8 +20,8 @@ import { isHotkey } from "is-hotkey-esm";
20
20
  import { htmlToBlocks, normalizeBlock } from "@sanity/block-tools";
21
21
  import isPlainObject from "lodash/isPlainObject.js";
22
22
  import throttle from "lodash/throttle.js";
23
- import { randomKey } from "@sanity/util/content";
24
23
  import debounce from "lodash/debounce.js";
24
+ import { randomKey } from "@sanity/util/content";
25
25
  const rootName = "sanity-pte:";
26
26
  debug$m(rootName);
27
27
  function debugWithName(name) {
@@ -1084,7 +1084,7 @@ function createOperationToPatches(types) {
1084
1084
  };
1085
1085
  }
1086
1086
  const debug$j = debugWithName("API:editable");
1087
- function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
1087
+ function createWithEditableAPI(editorActor, portableTextEditor, types) {
1088
1088
  return function(editor) {
1089
1089
  return portableTextEditor.setEditable({
1090
1090
  focus: () => {
@@ -1164,11 +1164,11 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
1164
1164
  const child = toSlateValue(
1165
1165
  [
1166
1166
  {
1167
- _key: keyGenerator(),
1167
+ _key: editorActor.getSnapshot().context.keyGenerator(),
1168
1168
  _type: types.block.name,
1169
1169
  children: [
1170
1170
  {
1171
- _key: keyGenerator(),
1171
+ _key: editorActor.getSnapshot().context.keyGenerator(),
1172
1172
  _type: type.name,
1173
1173
  ...value || {}
1174
1174
  }
@@ -1196,7 +1196,7 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
1196
1196
  const block = toSlateValue(
1197
1197
  [
1198
1198
  {
1199
- _key: keyGenerator(),
1199
+ _key: editorActor.getSnapshot().context.keyGenerator(),
1200
1200
  _type: type.name,
1201
1201
  ...value || {}
1202
1202
  }
@@ -1360,7 +1360,7 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
1360
1360
  for (const [block, blockPath] of selectedBlocks) {
1361
1361
  if (block.children.length === 0 || block.children.length === 1 && block.children[0].text === "")
1362
1362
  continue;
1363
- const annotationKey = keyGenerator(), markDefs = block.markDefs ?? [];
1363
+ const annotationKey = editorActor.getSnapshot().context.keyGenerator(), markDefs = block.markDefs ?? [];
1364
1364
  markDefs.find(
1365
1365
  (markDef) => markDef._type === type.name && markDef._key === annotationKey
1366
1366
  ) === void 0 && (Transforms.setNodes(
@@ -1569,7 +1569,7 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
1569
1569
  }), editor;
1570
1570
  };
1571
1571
  }
1572
- function createWithInsertBreak(types, keyGenerator) {
1572
+ function createWithInsertBreak(editorActor, types) {
1573
1573
  return function(editor) {
1574
1574
  const { insertBreak } = editor;
1575
1575
  return editor.insertBreak = () => {
@@ -1577,19 +1577,29 @@ function createWithInsertBreak(types, keyGenerator) {
1577
1577
  insertBreak();
1578
1578
  return;
1579
1579
  }
1580
- const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);
1580
+ const [focusSpan] = Array.from(
1581
+ Editor.nodes(editor, {
1582
+ mode: "lowest",
1583
+ at: editor.selection.focus,
1584
+ match: (n) => editor.isTextSpan(n),
1585
+ voids: !1
1586
+ })
1587
+ )[0] ?? [void 0], focusDecorators = focusSpan.marks?.filter(
1588
+ (mark) => types.decorators.some((decorator) => decorator.value === mark)
1589
+ ) ?? [], focusAnnotations = focusSpan.marks?.filter(
1590
+ (mark) => !types.decorators.some((decorator) => decorator.value === mark)
1591
+ ) ?? [], focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);
1581
1592
  if (editor.isTextBlock(focusBlock)) {
1582
1593
  const [start, end] = Range.edges(editor.selection), isEndAtStartOfBlock = isEqual(end, {
1583
1594
  path: [...focusBlockPath, 0],
1584
1595
  offset: 0
1585
1596
  });
1586
1597
  if (isEndAtStartOfBlock && Range.isCollapsed(editor.selection)) {
1587
- const focusDecorators = editor.isTextSpan(focusBlock.children[0]) ? (focusBlock.children[0].marks ?? []).filter(
1588
- (mark) => types.decorators.some((decorator) => decorator.value === mark)
1589
- ) : [];
1590
1598
  Editor.insertNode(
1591
1599
  editor,
1592
- editor.pteCreateTextBlock({ decorators: focusDecorators })
1600
+ editor.pteCreateTextBlock({
1601
+ decorators: focusAnnotations.length === 0 ? focusDecorators : []
1602
+ })
1593
1603
  );
1594
1604
  const [nextBlockPath] = Path.next(focusBlockPath);
1595
1605
  Transforms.select(editor, {
@@ -1602,6 +1612,24 @@ function createWithInsertBreak(types, keyGenerator) {
1602
1612
  path: [...focusBlockPath, focusBlock.children.length - 1],
1603
1613
  offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
1604
1614
  });
1615
+ if (isStartAtEndOfBlock && Range.isCollapsed(editor.selection) && focusDecorators.length > 0 && focusAnnotations.length > 0) {
1616
+ Editor.withoutNormalizing(editor, () => {
1617
+ if (!editor.selection)
1618
+ return;
1619
+ Editor.insertNode(
1620
+ editor,
1621
+ editor.pteCreateTextBlock({
1622
+ decorators: []
1623
+ })
1624
+ );
1625
+ const [nextBlockPath] = Path.next(focusBlockPath);
1626
+ Transforms.setSelection(editor, {
1627
+ anchor: { path: [nextBlockPath, 0], offset: 0 },
1628
+ focus: { path: [nextBlockPath, 0], offset: 0 }
1629
+ });
1630
+ }), editor.onChange();
1631
+ return;
1632
+ }
1605
1633
  if (!isEndAtStartOfBlock && !isStartAtEndOfBlock) {
1606
1634
  Editor.withoutNormalizing(editor, () => {
1607
1635
  if (!editor.selection)
@@ -1630,7 +1658,10 @@ function createWithInsertBreak(types, keyGenerator) {
1630
1658
  (decorator) => decorator.value === mark
1631
1659
  ) || prevNodeSpans.some(
1632
1660
  (prevNodeSpan) => prevNodeSpan.marks?.includes(mark)
1633
- ) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
1661
+ ) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(
1662
+ mark,
1663
+ editorActor.getSnapshot().context.keyGenerator()
1664
+ );
1634
1665
  const newMarks = marks.map(
1635
1666
  (mark) => newMarkDefKeys.get(mark) ?? mark
1636
1667
  );
@@ -1711,7 +1742,7 @@ function createWithMaxBlocks(maxBlocks) {
1711
1742
  }, editor;
1712
1743
  };
1713
1744
  }
1714
- function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
1745
+ function createWithObjectKeys(editorActor, schemaTypes) {
1715
1746
  return function(editor) {
1716
1747
  const { apply: apply2, normalizeNode } = editor;
1717
1748
  return editor.apply = (operation) => {
@@ -1728,7 +1759,7 @@ function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
1728
1759
  ...operation,
1729
1760
  properties: {
1730
1761
  ...operation.properties,
1731
- _key: keyGenerator()
1762
+ _key: editorActor.getSnapshot().context.keyGenerator()
1732
1763
  }
1733
1764
  });
1734
1765
  return;
@@ -1738,7 +1769,7 @@ function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
1738
1769
  ...operation,
1739
1770
  node: {
1740
1771
  ...operation.node,
1741
- _key: keyGenerator()
1772
+ _key: editorActor.getSnapshot().context.keyGenerator()
1742
1773
  }
1743
1774
  });
1744
1775
  return;
@@ -1748,12 +1779,20 @@ function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
1748
1779
  const [node, path] = entry;
1749
1780
  if (Element$1.isElement(node) && node._type === schemaTypes.block.name) {
1750
1781
  if (!node._key) {
1751
- editorActor.send({ type: "normalizing" }), Transforms.setNodes(editor, { _key: keyGenerator() }, { at: path }), editorActor.send({ type: "done normalizing" });
1782
+ editorActor.send({ type: "normalizing" }), Transforms.setNodes(
1783
+ editor,
1784
+ { _key: editorActor.getSnapshot().context.keyGenerator() },
1785
+ { at: path }
1786
+ ), editorActor.send({ type: "done normalizing" });
1752
1787
  return;
1753
1788
  }
1754
1789
  for (const [child, childPath] of Node.children(editor, path))
1755
1790
  if (!child._key) {
1756
- editorActor.send({ type: "normalizing" }), Transforms.setNodes(editor, { _key: keyGenerator() }, { at: childPath }), editorActor.send({ type: "done normalizing" });
1791
+ editorActor.send({ type: "normalizing" }), Transforms.setNodes(
1792
+ editor,
1793
+ { _key: editorActor.getSnapshot().context.keyGenerator() },
1794
+ { at: childPath }
1795
+ ), editorActor.send({ type: "done normalizing" });
1757
1796
  return;
1758
1797
  }
1759
1798
  }
@@ -3245,7 +3284,7 @@ function isPortableTextBlock(node) {
3245
3284
  );
3246
3285
  }
3247
3286
  const debug$c = debugWithName("plugin:withPortableTextMarkModel");
3248
- function createWithPortableTextMarkModel(editorActor, types, keyGenerator) {
3287
+ function createWithPortableTextMarkModel(editorActor, types) {
3249
3288
  return function(editor) {
3250
3289
  const { apply: apply2, normalizeNode } = editor, decorators = types.decorators.map((t) => t.value), forceNewSelection = () => {
3251
3290
  editor.selection && (Transforms.select(editor, { ...editor.selection }), editor.selection = { ...editor.selection });
@@ -3372,24 +3411,65 @@ function createWithPortableTextMarkModel(editorActor, types, keyGenerator) {
3372
3411
  if (op.type === "insert_text") {
3373
3412
  const { selection } = editor, collapsedSelection = selection ? Range.isCollapsed(selection) : !1;
3374
3413
  if (selection && collapsedSelection) {
3375
- const [span] = Array.from(
3414
+ const [_block, blockPath] = Editor.node(editor, selection, {
3415
+ depth: 1
3416
+ }), [span, spanPath] = Array.from(
3376
3417
  Editor.nodes(editor, {
3377
3418
  mode: "lowest",
3378
3419
  at: selection.focus,
3379
3420
  match: (n) => editor.isTextSpan(n),
3380
3421
  voids: !1
3381
3422
  })
3382
- )[0], marks = span.marks ?? [], marksWithoutAnnotations = marks.filter(
3423
+ )[0] ?? [void 0, void 0], marks = span.marks ?? [], marksWithoutAnnotations = marks.filter(
3383
3424
  (mark) => decorators.includes(mark)
3384
- );
3385
- if (marks.length > marksWithoutAnnotations.length && (selection.anchor.offset === 0 || span.text.length === selection.focus.offset)) {
3386
- Transforms.insertNodes(editor, {
3387
- _type: "span",
3388
- _key: keyGenerator(),
3389
- text: op.text,
3390
- marks: marksWithoutAnnotations
3391
- }), debug$c("Inserting text at end of annotation");
3392
- return;
3425
+ ), spanHasAnnotations = marks.length > marksWithoutAnnotations.length, spanIsEmpty = span.text.length === 0, atTheBeginningOfSpan = selection.anchor.offset === 0, atTheEndOfSpan = selection.anchor.offset === span.text.length;
3426
+ let previousSpan, nextSpan;
3427
+ for (const [child, childPath] of Node.children(editor, blockPath, {
3428
+ reverse: !0
3429
+ }))
3430
+ if (editor.isTextSpan(child) && Path.isBefore(childPath, spanPath)) {
3431
+ previousSpan = child;
3432
+ break;
3433
+ }
3434
+ for (const [child, childPath] of Node.children(editor, blockPath))
3435
+ if (editor.isTextSpan(child) && Path.isAfter(childPath, spanPath)) {
3436
+ nextSpan = child;
3437
+ break;
3438
+ }
3439
+ const previousSpanHasSameAnnotation = previousSpan ? previousSpan.marks?.some(
3440
+ (mark) => !decorators.includes(mark) && marks.includes(mark)
3441
+ ) : !1, previousSpanHasSameMarks = previousSpan ? previousSpan.marks?.every((mark) => marks.includes(mark)) : !1, nextSpanHasSameAnnotation = nextSpan ? nextSpan.marks?.some(
3442
+ (mark) => !decorators.includes(mark) && marks.includes(mark)
3443
+ ) : !1, nextSpanHasSameMarks = nextSpan ? nextSpan.marks?.every((mark) => marks.includes(mark)) : !1;
3444
+ if (spanHasAnnotations && !spanIsEmpty) {
3445
+ if (atTheBeginningOfSpan) {
3446
+ previousSpanHasSameMarks ? Transforms.insertNodes(editor, {
3447
+ _type: "span",
3448
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3449
+ text: op.text,
3450
+ marks: previousSpan?.marks ?? []
3451
+ }) : previousSpanHasSameAnnotation ? apply2(op) : Transforms.insertNodes(editor, {
3452
+ _type: "span",
3453
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3454
+ text: op.text,
3455
+ marks: []
3456
+ });
3457
+ return;
3458
+ }
3459
+ if (atTheEndOfSpan) {
3460
+ nextSpanHasSameMarks ? Transforms.insertNodes(editor, {
3461
+ _type: "span",
3462
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3463
+ text: op.text,
3464
+ marks: nextSpan?.marks ?? []
3465
+ }) : nextSpanHasSameAnnotation ? apply2(op) : Transforms.insertNodes(editor, {
3466
+ _type: "span",
3467
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3468
+ text: op.text,
3469
+ marks: []
3470
+ });
3471
+ return;
3472
+ }
3393
3473
  }
3394
3474
  }
3395
3475
  }
@@ -3617,8 +3697,7 @@ function createWithPortableTextSelections(editorActor, types) {
3617
3697
  const debug$a = debugWithName("plugin:withSchemaTypes");
3618
3698
  function createWithSchemaTypes({
3619
3699
  editorActor,
3620
- schemaTypes,
3621
- keyGenerator
3700
+ schemaTypes
3622
3701
  }) {
3623
3702
  return function(editor) {
3624
3703
  editor.isTextBlock = (value) => isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => isPortableTextSpan$1(value) && value._type === schemaTypes.span.name, editor.isListBlock = (value) => 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;
@@ -3627,7 +3706,7 @@ function createWithSchemaTypes({
3627
3706
  const [node, path] = entry;
3628
3707
  if (node._type === void 0 && path.length === 2) {
3629
3708
  debug$a("Setting span type on text node without a type");
3630
- const span = node, key = span._key || keyGenerator();
3709
+ const span = node, key = span._key || editorActor.getSnapshot().context.keyGenerator();
3631
3710
  editorActor.send({ type: "normalizing" }), Transforms.setNodes(
3632
3711
  editor,
3633
3712
  { ...span, _type: schemaTypes.span.name, _key: key },
@@ -3637,7 +3716,7 @@ function createWithSchemaTypes({
3637
3716
  }
3638
3717
  if (node._key === void 0 && (path.length === 1 || path.length === 2)) {
3639
3718
  debug$a("Setting missing key on child node without a key");
3640
- const key = keyGenerator();
3719
+ const key = editorActor.getSnapshot().context.keyGenerator();
3641
3720
  editorActor.send({ type: "normalizing" }), Transforms.setNodes(editor, { _key: key }, { at: path }), editorActor.send({ type: "done normalizing" });
3642
3721
  return;
3643
3722
  }
@@ -3647,8 +3726,8 @@ function createWithSchemaTypes({
3647
3726
  }
3648
3727
  const debug$9 = debugWithName("plugin:withUtils");
3649
3728
  function createWithUtils({
3729
+ editorActor,
3650
3730
  schemaTypes,
3651
- keyGenerator,
3652
3731
  portableTextEditor
3653
3732
  }) {
3654
3733
  return function(editor) {
@@ -3674,13 +3753,13 @@ function createWithUtils({
3674
3753
  [
3675
3754
  {
3676
3755
  _type: schemaTypes.block.name,
3677
- _key: keyGenerator(),
3756
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3678
3757
  style: schemaTypes.styles[0].value || "normal",
3679
3758
  markDefs: [],
3680
3759
  children: [
3681
3760
  {
3682
3761
  _type: "span",
3683
- _key: keyGenerator(),
3762
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3684
3763
  text: "",
3685
3764
  marks: options.decorators.filter(
3686
3765
  (decorator) => schemaTypes.decorators.find(({ value }) => value === decorator)
@@ -3748,7 +3827,7 @@ function createWithHotkeys(types, portableTextEditor, hotkeysFromOptions) {
3748
3827
  {
3749
3828
  at: nextPath
3750
3829
  }
3751
- ), editor.onChange();
3830
+ ), Transforms.select(editor, { path: [...nextPath, 0], offset: 0 }), editor.onChange();
3752
3831
  return;
3753
3832
  }
3754
3833
  }
@@ -4091,7 +4170,7 @@ function validateValue(value, types, keyGenerator) {
4091
4170
  }) && (valid = !1), { valid, resolution, value });
4092
4171
  }
4093
4172
  const debug$7 = debugWithName("plugin:withInsertData");
4094
- function createWithInsertData(editorActor, schemaTypes, keyGenerator) {
4173
+ function createWithInsertData(editorActor, schemaTypes) {
4095
4174
  return function(editor) {
4096
4175
  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(`
4097
4176
 
@@ -4139,10 +4218,14 @@ function createWithInsertData(editorActor, schemaTypes, keyGenerator) {
4139
4218
  const slateValue = _regenerateKeys(
4140
4219
  editor,
4141
4220
  toSlateValue(parsed, { schemaTypes }),
4142
- keyGenerator,
4221
+ editorActor.getSnapshot().context.keyGenerator,
4143
4222
  spanTypeName,
4144
4223
  schemaTypes
4145
- ), validation = validateValue(parsed, schemaTypes, keyGenerator);
4224
+ ), validation = validateValue(
4225
+ parsed,
4226
+ schemaTypes,
4227
+ editorActor.getSnapshot().context.keyGenerator
4228
+ );
4146
4229
  if (!validation.valid && !validation.resolution?.autoResolve) {
4147
4230
  const errorDescription = `${validation.resolution?.description}`;
4148
4231
  return editorActor.send({
@@ -4183,7 +4266,7 @@ function createWithInsertData(editorActor, schemaTypes, keyGenerator) {
4183
4266
  const validation = validateValue(
4184
4267
  portableText,
4185
4268
  schemaTypes,
4186
- keyGenerator
4269
+ editorActor.getSnapshot().context.keyGenerator
4187
4270
  );
4188
4271
  if (!validation.valid) {
4189
4272
  const errorDescription = `Could not validate the resulting portable text to insert.
@@ -4287,27 +4370,21 @@ function _insertFragment(editor, fragment, schemaTypes) {
4287
4370
  }), editor.onChange();
4288
4371
  }
4289
4372
  const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, options) => {
4290
- const e = editor, { keyGenerator, portableTextEditor, patches$, readOnly, maxBlocks } = options, { editorActor, schemaTypes } = portableTextEditor;
4373
+ const e = editor, { portableTextEditor, patches$, readOnly, maxBlocks } = options, { editorActor, schemaTypes } = portableTextEditor;
4291
4374
  e.subscriptions = [], e.destroy ? e.destroy() : originalFnMap.set(e, {
4292
4375
  apply: e.apply,
4293
4376
  onChange: e.onChange,
4294
4377
  normalizeNode: e.normalizeNode
4295
4378
  });
4296
- const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(
4379
+ const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(editorActor, schemaTypes), withSchemaTypes = createWithSchemaTypes({
4297
4380
  editorActor,
4298
- schemaTypes,
4299
- keyGenerator
4300
- ), withSchemaTypes = createWithSchemaTypes({
4301
- editorActor,
4302
- schemaTypes,
4303
- keyGenerator
4381
+ schemaTypes
4304
4382
  }), withEditableAPI = createWithEditableAPI(
4383
+ editorActor,
4305
4384
  portableTextEditor,
4306
- schemaTypes,
4307
- keyGenerator
4385
+ schemaTypes
4308
4386
  ), withPatches = createWithPatches({
4309
4387
  editorActor,
4310
- keyGenerator,
4311
4388
  patches$,
4312
4389
  patchFunctions: operationToPatches,
4313
4390
  readOnly,
@@ -4318,13 +4395,12 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4318
4395
  blockSchemaType: schemaTypes.block
4319
4396
  }), withPortableTextMarkModel = createWithPortableTextMarkModel(
4320
4397
  editorActor,
4321
- schemaTypes,
4322
- keyGenerator
4398
+ schemaTypes
4323
4399
  ), withPortableTextBlockStyle = createWithPortableTextBlockStyle(
4324
4400
  editorActor,
4325
4401
  schemaTypes
4326
- ), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(schemaTypes, keyGenerator), withUtils = createWithUtils({
4327
- keyGenerator,
4402
+ ), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(editorActor, schemaTypes), withUtils = createWithUtils({
4403
+ editorActor,
4328
4404
  schemaTypes,
4329
4405
  portableTextEditor
4330
4406
  }), withPortableTextSelections = createWithPortableTextSelections(
@@ -4392,10 +4468,9 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4392
4468
  };
4393
4469
  }, debug$6 = debugWithName("component:PortableTextEditor:SlateContainer");
4394
4470
  function SlateContainer(props) {
4395
- const { patches$, portableTextEditor, readOnly, maxBlocks, keyGenerator } = props, [[slateEditor, subscribe]] = useState(() => {
4471
+ const { patches$, portableTextEditor, readOnly, maxBlocks } = props, [[slateEditor, subscribe]] = useState(() => {
4396
4472
  debug$6("Creating new Slate editor instance");
4397
4473
  const { editor, subscribe: _sub } = withPlugins(withReact(createEditor()), {
4398
- keyGenerator,
4399
4474
  maxBlocks,
4400
4475
  patches$,
4401
4476
  portableTextEditor,
@@ -4410,33 +4485,18 @@ function SlateContainer(props) {
4410
4485
  };
4411
4486
  }, [subscribe]), useEffect(() => {
4412
4487
  debug$6("Re-initializing plugin chain"), withPlugins(slateEditor, {
4413
- keyGenerator,
4414
4488
  maxBlocks,
4415
4489
  patches$,
4416
4490
  portableTextEditor,
4417
4491
  readOnly
4418
4492
  });
4419
- }, [
4420
- keyGenerator,
4421
- portableTextEditor,
4422
- maxBlocks,
4423
- readOnly,
4424
- patches$,
4425
- slateEditor
4426
- ]);
4493
+ }, [portableTextEditor, maxBlocks, readOnly, patches$, slateEditor]);
4427
4494
  const initialValue = useMemo(() => [slateEditor.pteCreateTextBlock({ decorators: [] })], [slateEditor]);
4428
4495
  return useEffect(() => () => {
4429
4496
  debug$6("Destroying Slate editor"), slateEditor.destroy();
4430
4497
  }, [slateEditor]), /* @__PURE__ */ jsx(Slate, { editor: slateEditor, initialValue, children: props.children });
4431
4498
  }
4432
- const defaultKeyGenerator = () => randomKey(12), PortableTextEditorKeyGeneratorContext = createContext(defaultKeyGenerator), usePortableTextEditorKeyGenerator = () => {
4433
- const keyGenerator = useContext(PortableTextEditorKeyGeneratorContext);
4434
- if (keyGenerator === void 0)
4435
- throw new Error(
4436
- "The `usePortableTextEditorKeyGenerator` hook must be used inside the <PortableTextEditor> component's context."
4437
- );
4438
- return keyGenerator;
4439
- }, PortableTextEditorReadOnlyContext = createContext(!1), usePortableTextEditorReadOnlyStatus = () => {
4499
+ const PortableTextEditorReadOnlyContext = createContext(!1), usePortableTextEditorReadOnlyStatus = () => {
4440
4500
  const readOnly = useContext(PortableTextEditorReadOnlyContext);
4441
4501
  if (readOnly === void 0)
4442
4502
  throw new Error(
@@ -4445,7 +4505,7 @@ const defaultKeyGenerator = () => randomKey(12), PortableTextEditorKeyGeneratorC
4445
4505
  return readOnly;
4446
4506
  }, debug$5 = debugWithName("hook:useSyncValue"), CURRENT_VALUE = /* @__PURE__ */ new WeakMap();
4447
4507
  function useSyncValue(props) {
4448
- const { editorActor, portableTextEditor, readOnly, keyGenerator } = props, { schemaTypes } = portableTextEditor, previousValue = useRef(), slateEditor = useSlate(), updateValueFunctionRef = useRef(), updateFromCurrentValue = useCallback(() => {
4508
+ const { editorActor, portableTextEditor, readOnly } = props, { schemaTypes } = portableTextEditor, previousValue = useRef(), slateEditor = useSlate(), updateValueFunctionRef = useRef(), updateFromCurrentValue = useCallback(() => {
4449
4509
  const currentValue = CURRENT_VALUE.get(portableTextEditor);
4450
4510
  if (previousValue.current === currentValue) {
4451
4511
  debug$5("Value is the same object as previous, not need to sync");
@@ -4511,7 +4571,7 @@ function useSyncValue(props) {
4511
4571
  const validationValue = [value[currentBlockIndex]], validation = validateValue(
4512
4572
  validationValue,
4513
4573
  schemaTypes,
4514
- keyGenerator
4574
+ editorActor.getSnapshot().context.keyGenerator
4515
4575
  );
4516
4576
  !validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !readOnly && previousValue.current && previousValue.current !== value && (console.warn(
4517
4577
  `${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`
@@ -4536,7 +4596,7 @@ function useSyncValue(props) {
4536
4596
  const validationValue = [value[currentBlockIndex]], validation = validateValue(
4537
4597
  validationValue,
4538
4598
  schemaTypes,
4539
- keyGenerator
4599
+ editorActor.getSnapshot().context.keyGenerator
4540
4600
  );
4541
4601
  debug$5.enabled && debug$5(
4542
4602
  "Validating and inserting new block in the end of the value",
@@ -4583,7 +4643,6 @@ function useSyncValue(props) {
4583
4643
  return updateValueFunctionRef.current = updateFunction, updateFunction;
4584
4644
  }, [
4585
4645
  editorActor,
4586
- keyGenerator,
4587
4646
  portableTextEditor,
4588
4647
  readOnly,
4589
4648
  schemaTypes,
@@ -4651,9 +4710,8 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
4651
4710
  }
4652
4711
  const debug$4 = debugWithName("component:PortableTextEditor:Synchronizer"), debugVerbose$1 = debug$4.enabled && !1, FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3;
4653
4712
  function Synchronizer(props) {
4654
- const portableTextEditor = usePortableTextEditor(), keyGenerator = usePortableTextEditorKeyGenerator(), readOnly = usePortableTextEditorReadOnlyStatus(), { editorActor, getValue, onChange, value } = props, pendingPatches = useRef([]), syncValue = useSyncValue({
4713
+ const portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), { editorActor, getValue, onChange, value } = props, pendingPatches = useRef([]), syncValue = useSyncValue({
4655
4714
  editorActor,
4656
- keyGenerator,
4657
4715
  portableTextEditor,
4658
4716
  readOnly
4659
4717
  }), slateEditor = useSlate();
@@ -4767,7 +4825,8 @@ const networkLogic = fromCallback(({ sendBack }) => {
4767
4825
  types: {
4768
4826
  context: {},
4769
4827
  events: {},
4770
- emitted: {}
4828
+ emitted: {},
4829
+ input: {}
4771
4830
  },
4772
4831
  actions: {
4773
4832
  "emit patch event": emit(({ event }) => (assertEvent(event, "patch"), event)),
@@ -4788,9 +4847,10 @@ const networkLogic = fromCallback(({ sendBack }) => {
4788
4847
  }
4789
4848
  }).createMachine({
4790
4849
  id: "editor",
4791
- context: {
4850
+ context: ({ input }) => ({
4851
+ keyGenerator: input.keyGenerator,
4792
4852
  pendingEvents: []
4793
- },
4853
+ }),
4794
4854
  invoke: {
4795
4855
  id: "networkLogic",
4796
4856
  src: "networkLogic"
@@ -4878,7 +4938,7 @@ function PortableTextEditorSelectionProvider(props) {
4878
4938
  };
4879
4939
  }, [props.editorActor]), /* @__PURE__ */ jsx(PortableTextEditorSelectionContext.Provider, { value: selection, children: props.children });
4880
4940
  }
4881
- const debug$2 = debugWithName("component:PortableTextEditor");
4941
+ const defaultKeyGenerator = () => randomKey(12), debug$2 = debugWithName("component:PortableTextEditor");
4882
4942
  class PortableTextEditor extends Component {
4883
4943
  /**
4884
4944
  * @internal
@@ -4902,7 +4962,11 @@ class PortableTextEditor extends Component {
4902
4962
  throw new Error('PortableTextEditor: missing "schemaType" property');
4903
4963
  props.incomingPatches$ && console.warn(
4904
4964
  "The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"
4905
- ), this.editorActor = createActor(editorMachine), this.editorActor.start(), this.schemaTypes = getPortableTextMemberSchemaTypes(
4965
+ ), this.editorActor = createActor(editorMachine, {
4966
+ input: {
4967
+ keyGenerator: props.keyGenerator || defaultKeyGenerator
4968
+ }
4969
+ }), this.editorActor.start(), this.schemaTypes = getPortableTextMemberSchemaTypes(
4906
4970
  props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)
4907
4971
  );
4908
4972
  }
@@ -4919,35 +4983,28 @@ class PortableTextEditor extends Component {
4919
4983
  return this.editable.getValue();
4920
4984
  };
4921
4985
  render() {
4922
- 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;
4986
+ 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;
4923
4987
  return /* @__PURE__ */ jsx(
4924
4988
  SlateContainer,
4925
4989
  {
4926
- keyGenerator,
4927
4990
  maxBlocks,
4928
4991
  patches$: _patches$,
4929
4992
  portableTextEditor: this,
4930
4993
  readOnly,
4931
- children: /* @__PURE__ */ jsx(PortableTextEditorKeyGeneratorContext.Provider, { value: keyGenerator, children: /* @__PURE__ */ jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsx(PortableTextEditorReadOnlyContext.Provider, { value: readOnly, children: /* @__PURE__ */ jsxs(
4932
- PortableTextEditorSelectionProvider,
4933
- {
4934
- editorActor: this.editorActor,
4935
- children: [
4936
- /* @__PURE__ */ jsx(
4937
- Synchronizer,
4938
- {
4939
- editorActor: this.editorActor,
4940
- getValue: this.getValue,
4941
- onChange: (change) => {
4942
- this.props.onChange(change), this.change$.next(change);
4943
- },
4944
- value
4945
- }
4946
- ),
4947
- children
4948
- ]
4949
- }
4950
- ) }) }) })
4994
+ children: /* @__PURE__ */ jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsx(PortableTextEditorReadOnlyContext.Provider, { value: readOnly, children: /* @__PURE__ */ jsxs(PortableTextEditorSelectionProvider, { editorActor: this.editorActor, children: [
4995
+ /* @__PURE__ */ jsx(
4996
+ Synchronizer,
4997
+ {
4998
+ editorActor: this.editorActor,
4999
+ getValue: this.getValue,
5000
+ onChange: (change) => {
5001
+ this.props.onChange(change), this.change$.next(change);
5002
+ },
5003
+ value
5004
+ }
5005
+ ),
5006
+ children
5007
+ ] }) }) })
4951
5008
  }
4952
5009
  );
4953
5010
  }
@@ -5213,7 +5270,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5213
5270
  scrollSelectionIntoView,
5214
5271
  spellCheck,
5215
5272
  ...restProps
5216
- } = props, portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), keyGenerator = usePortableTextEditorKeyGenerator(), ref = useRef(null), [editableElement, setEditableElement] = useState(
5273
+ } = props, portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), ref = useRef(null), [editableElement, setEditableElement] = useState(
5217
5274
  null
5218
5275
  ), [hasInvalidValue, setHasInvalidValue] = useState(!1), [rangeDecorationState, setRangeDecorationsState] = useState([]);
5219
5276
  useImperativeHandle(
@@ -5221,8 +5278,8 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5221
5278
  () => ref.current
5222
5279
  );
5223
5280
  const rangeDecorationsRef = useRef(rangeDecorations), { editorActor, schemaTypes } = portableTextEditor, slateEditor = useSlate(), blockTypeName = schemaTypes.block.name, withInsertData = useMemo(
5224
- () => createWithInsertData(editorActor, schemaTypes, keyGenerator),
5225
- [editorActor, keyGenerator, schemaTypes]
5281
+ () => createWithInsertData(editorActor, schemaTypes),
5282
+ [editorActor, schemaTypes]
5226
5283
  ), withHotKeys = useMemo(
5227
5284
  () => createWithHotkeys(schemaTypes, portableTextEditor, hotkeys),
5228
5285
  [hotkeys, portableTextEditor, schemaTypes]