@portabletext/editor 1.26.0 → 1.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.cjs CHANGED
@@ -2547,18 +2547,34 @@ function isRecord(value) {
2547
2547
  }
2548
2548
  function parseBlock({
2549
2549
  context,
2550
- block
2550
+ block,
2551
+ options
2551
2552
  }) {
2552
2553
  if (!isTypedObject(block) || block._type !== context.schema.block.name && !context.schema.blockObjects.some((blockObject) => blockObject.name === block._type))
2553
2554
  return;
2554
- if (!types.isPortableTextTextBlock(block))
2555
+ if (block._type !== context.schema.block.name) {
2556
+ const _key = options.refreshKeys ? context.keyGenerator() : typeof block._key == "string" ? block._key : context.keyGenerator();
2555
2557
  return {
2556
2558
  ...block,
2557
- _key: context.keyGenerator()
2559
+ _key
2560
+ };
2561
+ }
2562
+ if (!types.isPortableTextTextBlock(block))
2563
+ return {
2564
+ _type: context.schema.block.name,
2565
+ _key: options.refreshKeys ? context.keyGenerator() : typeof block._key == "string" ? block._key : context.keyGenerator(),
2566
+ children: [{
2567
+ _key: context.keyGenerator(),
2568
+ _type: context.schema.span.name,
2569
+ text: "",
2570
+ marks: []
2571
+ }],
2572
+ markDefs: [],
2573
+ style: context.schema.styles[0].value
2558
2574
  };
2559
2575
  const markDefKeyMap = /* @__PURE__ */ new Map(), markDefs = (block.markDefs ?? []).flatMap((markDef) => {
2560
2576
  if (context.schema.annotations.some((annotation) => annotation.name === markDef._type)) {
2561
- const _key = context.keyGenerator();
2577
+ const _key = options.refreshKeys ? context.keyGenerator() : markDef._key;
2562
2578
  return markDefKeyMap.set(markDef._key, _key), [{
2563
2579
  ...markDef,
2564
2580
  _key
@@ -2573,17 +2589,17 @@ function parseBlock({
2573
2589
  if (!types.isPortableTextSpan(child))
2574
2590
  return [{
2575
2591
  ...child,
2576
- _key: context.keyGenerator()
2592
+ _key: options.refreshKeys ? context.keyGenerator() : child._key
2577
2593
  }];
2578
2594
  const marks = (child.marks ?? []).flatMap((mark) => markDefKeyMap.has(mark) ? [markDefKeyMap.get(mark)] : context.schema.decorators.some((decorator) => decorator.value === mark) ? [mark] : []);
2579
2595
  return [{
2580
2596
  ...child,
2581
- _key: context.keyGenerator(),
2597
+ _key: options.refreshKeys ? context.keyGenerator() : child._key,
2582
2598
  marks
2583
2599
  }];
2584
2600
  }), parsedBlock = {
2585
2601
  ...block,
2586
- _key: context.keyGenerator(),
2602
+ _key: options.refreshKeys ? context.keyGenerator() : block._key,
2587
2603
  children: children.length > 0 ? children : [{
2588
2604
  _key: context.keyGenerator(),
2589
2605
  _type: context.schema.span.name,
@@ -2636,7 +2652,10 @@ const converterPortableText = {
2636
2652
  const parsedBlocks = blocks.flatMap((block) => {
2637
2653
  const parsedBlock = parseBlock({
2638
2654
  context,
2639
- block
2655
+ block,
2656
+ options: {
2657
+ refreshKeys: !0
2658
+ }
2640
2659
  });
2641
2660
  return parsedBlock ? [parsedBlock] : [];
2642
2661
  });
@@ -3207,21 +3226,23 @@ function createWithObjectKeys(editorActor, schemaTypes) {
3207
3226
  return;
3208
3227
  }
3209
3228
  if (operation.type === "split_node") {
3229
+ const existingKeys = [...slate.Node.descendants(editor)].map(([node]) => node._key);
3210
3230
  apply2({
3211
3231
  ...operation,
3212
3232
  properties: {
3213
3233
  ...operation.properties,
3214
- _key: editorActor.getSnapshot().context.keyGenerator()
3234
+ _key: operation.properties._key === void 0 || existingKeys.includes(operation.properties._key) ? editorActor.getSnapshot().context.keyGenerator() : operation.properties._key
3215
3235
  }
3216
3236
  });
3217
3237
  return;
3218
3238
  }
3219
3239
  if (operation.type === "insert_node" && !slate.Editor.isEditor(operation.node)) {
3240
+ const existingKeys = [...slate.Node.descendants(editor)].map(([node]) => node._key);
3220
3241
  apply2({
3221
3242
  ...operation,
3222
3243
  node: {
3223
3244
  ...operation.node,
3224
- _key: editorActor.getSnapshot().context.keyGenerator()
3245
+ _key: operation.node._key === void 0 || existingKeys.includes(operation.node._key) ? editorActor.getSnapshot().context.keyGenerator() : operation.node._key
3225
3246
  }
3226
3247
  });
3227
3248
  return;
@@ -3909,6 +3930,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
3909
3930
  if (atTheEndOfAnnotation && isPortableTextSpan(op.node) && op.node.marks?.some((mark) => annotationsEnding.includes(mark))) {
3910
3931
  slate.Transforms.insertNodes(editor, {
3911
3932
  ...op.node,
3933
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3912
3934
  marks: op.node.marks?.filter((mark) => !annotationsEnding.includes(mark)) ?? []
3913
3935
  });
3914
3936
  return;
@@ -3917,6 +3939,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
3917
3939
  if (atTheStartOfAnnotation && isPortableTextSpan(op.node) && op.node.marks?.some((mark) => annotationsStarting.includes(mark))) {
3918
3940
  slate.Transforms.insertNodes(editor, {
3919
3941
  ...op.node,
3942
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3920
3943
  marks: op.node.marks?.filter((mark) => !annotationsStarting.includes(mark)) ?? []
3921
3944
  });
3922
3945
  return;
@@ -3925,6 +3948,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
3925
3948
  if (nextSpanDecorators.length > 0 && atTheEndOfAnnotation && !atTheStartOfAnnotation && isPortableTextSpan(op.node) && op.node.marks?.length === 0) {
3926
3949
  slate.Transforms.insertNodes(editor, {
3927
3950
  ...op.node,
3951
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3928
3952
  marks: nextSpanDecorators
3929
3953
  });
3930
3954
  return;
@@ -5300,7 +5324,10 @@ const blockSetBehaviorActionImplementation = ({
5300
5324
  }) => {
5301
5325
  const parsedBlock = parseBlock({
5302
5326
  block: action.block,
5303
- context
5327
+ context,
5328
+ options: {
5329
+ refreshKeys: !1
5330
+ }
5304
5331
  });
5305
5332
  if (!parsedBlock)
5306
5333
  throw new Error(`Failed to parse block ${JSON.stringify(action.block)}`);