@portabletext/editor 1.26.0 → 1.26.2

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
@@ -2569,18 +2569,34 @@ function isRecord(value) {
2569
2569
  }
2570
2570
  function parseBlock({
2571
2571
  context,
2572
- block
2572
+ block,
2573
+ options
2573
2574
  }) {
2574
2575
  if (!isTypedObject(block) || block._type !== context.schema.block.name && !context.schema.blockObjects.some((blockObject) => blockObject.name === block._type))
2575
2576
  return;
2576
- if (!isPortableTextTextBlock(block))
2577
+ if (block._type !== context.schema.block.name) {
2578
+ const _key = options.refreshKeys ? context.keyGenerator() : typeof block._key == "string" ? block._key : context.keyGenerator();
2577
2579
  return {
2578
2580
  ...block,
2579
- _key: context.keyGenerator()
2581
+ _key
2582
+ };
2583
+ }
2584
+ if (!isPortableTextTextBlock(block))
2585
+ return {
2586
+ _type: context.schema.block.name,
2587
+ _key: options.refreshKeys ? context.keyGenerator() : typeof block._key == "string" ? block._key : context.keyGenerator(),
2588
+ children: [{
2589
+ _key: context.keyGenerator(),
2590
+ _type: context.schema.span.name,
2591
+ text: "",
2592
+ marks: []
2593
+ }],
2594
+ markDefs: [],
2595
+ style: context.schema.styles[0].value
2580
2596
  };
2581
2597
  const markDefKeyMap = /* @__PURE__ */ new Map(), markDefs = (block.markDefs ?? []).flatMap((markDef) => {
2582
2598
  if (context.schema.annotations.some((annotation) => annotation.name === markDef._type)) {
2583
- const _key = context.keyGenerator();
2599
+ const _key = options.refreshKeys ? context.keyGenerator() : markDef._key;
2584
2600
  return markDefKeyMap.set(markDef._key, _key), [{
2585
2601
  ...markDef,
2586
2602
  _key
@@ -2595,17 +2611,17 @@ function parseBlock({
2595
2611
  if (!isPortableTextSpan$1(child))
2596
2612
  return [{
2597
2613
  ...child,
2598
- _key: context.keyGenerator()
2614
+ _key: options.refreshKeys ? context.keyGenerator() : child._key
2599
2615
  }];
2600
2616
  const marks = (child.marks ?? []).flatMap((mark) => markDefKeyMap.has(mark) ? [markDefKeyMap.get(mark)] : context.schema.decorators.some((decorator) => decorator.value === mark) ? [mark] : []);
2601
2617
  return [{
2602
2618
  ...child,
2603
- _key: context.keyGenerator(),
2619
+ _key: options.refreshKeys ? context.keyGenerator() : child._key,
2604
2620
  marks
2605
2621
  }];
2606
2622
  }), parsedBlock = {
2607
2623
  ...block,
2608
- _key: context.keyGenerator(),
2624
+ _key: options.refreshKeys ? context.keyGenerator() : block._key,
2609
2625
  children: children.length > 0 ? children : [{
2610
2626
  _key: context.keyGenerator(),
2611
2627
  _type: context.schema.span.name,
@@ -2658,7 +2674,10 @@ const converterPortableText = {
2658
2674
  const parsedBlocks = blocks.flatMap((block) => {
2659
2675
  const parsedBlock = parseBlock({
2660
2676
  context,
2661
- block
2677
+ block,
2678
+ options: {
2679
+ refreshKeys: !0
2680
+ }
2662
2681
  });
2663
2682
  return parsedBlock ? [parsedBlock] : [];
2664
2683
  });
@@ -3229,21 +3248,23 @@ function createWithObjectKeys(editorActor, schemaTypes) {
3229
3248
  return;
3230
3249
  }
3231
3250
  if (operation.type === "split_node") {
3251
+ const existingKeys = [...Node.descendants(editor)].map(([node]) => node._key);
3232
3252
  apply2({
3233
3253
  ...operation,
3234
3254
  properties: {
3235
3255
  ...operation.properties,
3236
- _key: editorActor.getSnapshot().context.keyGenerator()
3256
+ _key: operation.properties._key === void 0 || existingKeys.includes(operation.properties._key) ? editorActor.getSnapshot().context.keyGenerator() : operation.properties._key
3237
3257
  }
3238
3258
  });
3239
3259
  return;
3240
3260
  }
3241
3261
  if (operation.type === "insert_node" && !Editor.isEditor(operation.node)) {
3262
+ const existingKeys = [...Node.descendants(editor)].map(([node]) => node._key);
3242
3263
  apply2({
3243
3264
  ...operation,
3244
3265
  node: {
3245
3266
  ...operation.node,
3246
- _key: editorActor.getSnapshot().context.keyGenerator()
3267
+ _key: operation.node._key === void 0 || existingKeys.includes(operation.node._key) ? editorActor.getSnapshot().context.keyGenerator() : operation.node._key
3247
3268
  }
3248
3269
  });
3249
3270
  return;
@@ -3931,6 +3952,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
3931
3952
  if (atTheEndOfAnnotation && isPortableTextSpan(op.node) && op.node.marks?.some((mark) => annotationsEnding.includes(mark))) {
3932
3953
  Transforms.insertNodes(editor, {
3933
3954
  ...op.node,
3955
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3934
3956
  marks: op.node.marks?.filter((mark) => !annotationsEnding.includes(mark)) ?? []
3935
3957
  });
3936
3958
  return;
@@ -3939,6 +3961,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
3939
3961
  if (atTheStartOfAnnotation && isPortableTextSpan(op.node) && op.node.marks?.some((mark) => annotationsStarting.includes(mark))) {
3940
3962
  Transforms.insertNodes(editor, {
3941
3963
  ...op.node,
3964
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3942
3965
  marks: op.node.marks?.filter((mark) => !annotationsStarting.includes(mark)) ?? []
3943
3966
  });
3944
3967
  return;
@@ -3947,6 +3970,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
3947
3970
  if (nextSpanDecorators.length > 0 && atTheEndOfAnnotation && !atTheStartOfAnnotation && isPortableTextSpan(op.node) && op.node.marks?.length === 0) {
3948
3971
  Transforms.insertNodes(editor, {
3949
3972
  ...op.node,
3973
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3950
3974
  marks: nextSpanDecorators
3951
3975
  });
3952
3976
  return;
@@ -5322,7 +5346,10 @@ const blockSetBehaviorActionImplementation = ({
5322
5346
  }) => {
5323
5347
  const parsedBlock = parseBlock({
5324
5348
  block: action.block,
5325
- context
5349
+ context,
5350
+ options: {
5351
+ refreshKeys: !1
5352
+ }
5326
5353
  });
5327
5354
  if (!parsedBlock)
5328
5355
  throw new Error(`Failed to parse block ${JSON.stringify(action.block)}`);