@portabletext/editor 4.0.2 → 4.1.0

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
@@ -4774,6 +4774,9 @@ function createApplyPatch(context) {
4774
4774
  case "set":
4775
4775
  changed = setPatch(editor, patch);
4776
4776
  break;
4777
+ case "setIfMissing":
4778
+ changed = setPatch(editor, patch);
4779
+ break;
4777
4780
  case "diffMatchPatch":
4778
4781
  changed = diffMatchPatch(editor, patch);
4779
4782
  break;
@@ -4899,6 +4902,8 @@ function setPatch(editor, patch) {
4899
4902
  if (isTextBlock2 && child) {
4900
4903
  if (Text.isText(child.node))
4901
4904
  if (Text.isText(value)) {
4905
+ if (patch.type === "setIfMissing")
4906
+ return !1;
4902
4907
  const oldText = child.node.text, newText = value.text;
4903
4908
  oldText !== newText && (editor.apply({
4904
4909
  type: "remove_text",
@@ -4939,38 +4944,6 @@ function setPatch(editor, patch) {
4939
4944
  });
4940
4945
  }
4941
4946
  return !0;
4942
- } else if (Element$1.isElement(block.node) && patch.path.length === 1) {
4943
- const {
4944
- children,
4945
- ...nextRest
4946
- } = value, {
4947
- children: _prevChildren,
4948
- ...prevRest
4949
- } = block.node || {
4950
- children: void 0
4951
- };
4952
- editor.apply({
4953
- type: "set_node",
4954
- path: [block.index],
4955
- properties: {
4956
- ...prevRest
4957
- },
4958
- newProperties: nextRest
4959
- });
4960
- const blockNode = block.node;
4961
- blockNode.children.forEach((child2, childIndex) => {
4962
- editor.apply({
4963
- type: "remove_node",
4964
- path: [block.index, blockNode.children.length - 1 - childIndex],
4965
- node: child2
4966
- });
4967
- }), Array.isArray(children) && children.forEach((child2, childIndex) => {
4968
- editor.apply({
4969
- type: "insert_node",
4970
- path: [block.index, childIndex],
4971
- node: child2
4972
- });
4973
- });
4974
4947
  } else if (block && "value" in block.node)
4975
4948
  if (patch.path.length > 1 && patch.path[1] !== "children") {
4976
4949
  const newVal = applyAll(block.node.value, [{
@@ -9289,7 +9262,11 @@ const abstractDeserializeBehaviors = [
9289
9262
  originEvent: event
9290
9263
  })]]
9291
9264
  })
9292
- ], abstractInsertBehaviors = [
9265
+ ];
9266
+ function getUniqueBlockKey(blockKey) {
9267
+ return (snapshot) => !blockKey || snapshot.blockIndexMap.has(blockKey) ? snapshot.context.keyGenerator() : blockKey;
9268
+ }
9269
+ const abstractInsertBehaviors = [
9293
9270
  defineBehavior({
9294
9271
  on: "insert.blocks",
9295
9272
  guard: ({
@@ -9319,7 +9296,7 @@ const abstractDeserializeBehaviors = [
9319
9296
  }) => {
9320
9297
  if (event.placement !== "before")
9321
9298
  return !1;
9322
- const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
9299
+ const firstBlockKey = getUniqueBlockKey(event.blocks.at(0)?._key)(snapshot), lastBlockKey = getUniqueBlockKey(event.blocks.at(-1)?._key)(snapshot);
9323
9300
  return {
9324
9301
  firstBlockKey,
9325
9302
  lastBlockKey
@@ -9333,7 +9310,13 @@ const abstractDeserializeBehaviors = [
9333
9310
  lastBlockKey
9334
9311
  }) => [...event.blocks.map((block, index) => raise({
9335
9312
  type: "insert.block",
9336
- block,
9313
+ block: index === 0 ? {
9314
+ ...block,
9315
+ _key: firstBlockKey
9316
+ } : index === event.blocks.length - 1 ? {
9317
+ ...block,
9318
+ _key: lastBlockKey
9319
+ } : block,
9337
9320
  placement: index === 0 ? "before" : "after",
9338
9321
  select: index !== event.blocks.length - 1 ? "end" : "none"
9339
9322
  })), ...event.select === "none" ? [raise({
@@ -9361,7 +9344,7 @@ const abstractDeserializeBehaviors = [
9361
9344
  }) => {
9362
9345
  if (event.placement !== "after")
9363
9346
  return !1;
9364
- const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
9347
+ const firstBlockKey = getUniqueBlockKey(event.blocks.at(0)?._key)(snapshot), lastBlockKey = getUniqueBlockKey(event.blocks.at(-1)?._key)(snapshot);
9365
9348
  return {
9366
9349
  firstBlockKey,
9367
9350
  lastBlockKey
@@ -9375,7 +9358,13 @@ const abstractDeserializeBehaviors = [
9375
9358
  lastBlockKey
9376
9359
  }) => [...event.blocks.map((block, index) => raise({
9377
9360
  type: "insert.block",
9378
- block,
9361
+ block: index === 0 ? {
9362
+ ...block,
9363
+ _key: firstBlockKey
9364
+ } : index === event.blocks.length - 1 ? {
9365
+ ...block,
9366
+ _key: lastBlockKey
9367
+ } : block,
9379
9368
  placement: "after",
9380
9369
  select: index !== event.blocks.length - 1 ? "end" : "none"
9381
9370
  })), ...event.select === "none" ? [raise({
@@ -9406,7 +9395,7 @@ const abstractDeserializeBehaviors = [
9406
9395
  const focusTextBlock = getFocusTextBlock(snapshot);
9407
9396
  if (!focusTextBlock || !isEmptyTextBlock(snapshot.context, focusTextBlock.node))
9408
9397
  return !1;
9409
- const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
9398
+ const firstBlockKey = getUniqueBlockKey(event.blocks.at(0)?._key)(snapshot), lastBlockKey = getUniqueBlockKey(event.blocks.at(-1)?._key)(snapshot);
9410
9399
  return {
9411
9400
  focusTextBlock,
9412
9401
  firstBlockKey,
@@ -9420,10 +9409,22 @@ const abstractDeserializeBehaviors = [
9420
9409
  lastBlockKey
9421
9410
  }) => [...event.blocks.map((block, index) => raise({
9422
9411
  type: "insert.block",
9423
- block,
9412
+ block: index === 0 ? {
9413
+ ...block,
9414
+ _key: firstBlockKey
9415
+ } : index === event.blocks.length - 1 ? {
9416
+ ...block,
9417
+ _key: lastBlockKey
9418
+ } : block,
9424
9419
  placement: index === 0 ? "auto" : "after",
9425
9420
  select: index !== event.blocks.length - 1 ? "end" : "none"
9426
- })), ...event.select === "none" || event.select === "start" ? [raise({
9421
+ })), ...event.select === "none" ? [raise({
9422
+ type: "select.block",
9423
+ at: [{
9424
+ _key: firstBlockKey
9425
+ }],
9426
+ select: "start"
9427
+ })] : event.select === "start" ? [raise({
9427
9428
  type: "select.block",
9428
9429
  at: [{
9429
9430
  _key: firstBlockKey
@@ -9463,23 +9464,28 @@ const abstractDeserializeBehaviors = [
9463
9464
  }
9464
9465
  },
9465
9466
  block: focusTextBlock.node
9466
- });
9467
+ }), firstBlockKey = getUniqueBlockKey(event.blocks.at(0)?._key)(snapshot), lastBlockKey = getUniqueBlockKey(event.blocks.at(-1)?._key)(snapshot), isFirstBlockTextBlock = isTextBlock(snapshot.context, event.blocks.at(0));
9467
9468
  return {
9468
- firstBlockKey: event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(),
9469
+ firstBlockKey,
9470
+ lastBlockKey,
9469
9471
  focusBlockStartPoint,
9470
9472
  focusBlockEndPoint,
9471
9473
  focusTextBlockAfter,
9472
- selection: snapshot.context.selection
9474
+ selection: snapshot.context.selection,
9475
+ isFirstBlockTextBlock
9473
9476
  };
9474
9477
  },
9475
9478
  actions: [({
9479
+ snapshot,
9476
9480
  event
9477
9481
  }, {
9478
9482
  focusBlockEndPoint,
9479
9483
  focusTextBlockAfter,
9480
9484
  selection,
9481
9485
  firstBlockKey,
9482
- focusBlockStartPoint
9486
+ lastBlockKey,
9487
+ focusBlockStartPoint,
9488
+ isFirstBlockTextBlock
9483
9489
  }) => [...event.blocks.flatMap((block, index) => index === 0 ? [...isEqualSelectionPoints(selection.focus, focusBlockEndPoint) ? [] : [raise({
9484
9490
  type: "delete",
9485
9491
  at: {
@@ -9488,20 +9494,26 @@ const abstractDeserializeBehaviors = [
9488
9494
  }
9489
9495
  })], raise({
9490
9496
  type: "insert.block",
9491
- block,
9497
+ block: {
9498
+ ...block,
9499
+ _key: firstBlockKey
9500
+ },
9492
9501
  placement: "auto",
9493
9502
  select: "end"
9494
9503
  })] : index === event.blocks.length - 1 ? [raise({
9495
9504
  type: "insert.block",
9496
- block,
9505
+ block: {
9506
+ ...block,
9507
+ _key: lastBlockKey
9508
+ },
9497
9509
  placement: "after",
9498
9510
  select: "end"
9499
- }), raise({
9511
+ }), ...isEmptyTextBlock(snapshot.context, focusTextBlockAfter) ? [] : [raise({
9500
9512
  type: "insert.block",
9501
9513
  block: focusTextBlockAfter,
9502
9514
  placement: "auto",
9503
9515
  select: event.select === "end" ? "none" : "end"
9504
- })] : [raise({
9516
+ })]] : [raise({
9505
9517
  type: "insert.block",
9506
9518
  block,
9507
9519
  placement: "after",
@@ -9509,7 +9521,7 @@ const abstractDeserializeBehaviors = [
9509
9521
  })]), ...event.select === "none" ? [raise({
9510
9522
  type: "select",
9511
9523
  at: selection
9512
- })] : event.select === "start" ? [isEqualSelectionPoints(selection.focus, focusBlockStartPoint) ? raise({
9524
+ })] : event.select === "start" ? [isEqualSelectionPoints(selection.focus, focusBlockStartPoint) || !isFirstBlockTextBlock ? raise({
9513
9525
  type: "select.block",
9514
9526
  at: [{
9515
9527
  _key: firstBlockKey
@@ -9526,16 +9538,51 @@ const abstractDeserializeBehaviors = [
9526
9538
  defineBehavior({
9527
9539
  on: "insert.blocks",
9528
9540
  guard: ({
9541
+ snapshot,
9529
9542
  event
9530
- }) => event.placement === "auto",
9543
+ }) => {
9544
+ if (event.placement !== "auto")
9545
+ return !1;
9546
+ const selection = snapshot.context.selection, firstBlockKey = getUniqueBlockKey(event.blocks.at(0)?._key)(snapshot), lastBlockKey = getUniqueBlockKey(event.blocks.at(-1)?._key)(snapshot);
9547
+ return {
9548
+ firstBlockKey,
9549
+ lastBlockKey,
9550
+ selection
9551
+ };
9552
+ },
9531
9553
  actions: [({
9532
9554
  event
9533
- }) => event.blocks.map((block, index) => raise({
9555
+ }, {
9556
+ firstBlockKey,
9557
+ lastBlockKey,
9558
+ selection
9559
+ }) => [...event.blocks.map((block, index) => raise({
9534
9560
  type: "insert.block",
9535
- block,
9561
+ block: index === 0 ? {
9562
+ ...block,
9563
+ _key: firstBlockKey
9564
+ } : index === event.blocks.length - 1 ? {
9565
+ ...block,
9566
+ _key: lastBlockKey
9567
+ } : block,
9536
9568
  placement: index === 0 ? "auto" : "after",
9537
- select: event.select ?? "end"
9538
- }))]
9569
+ select: index !== event.blocks.length - 1 ? "end" : event.select ?? "end"
9570
+ })), ...event.select === "none" ? [raise({
9571
+ type: "select",
9572
+ at: selection
9573
+ })] : event.select === "start" ? [raise({
9574
+ type: "select.block",
9575
+ at: [{
9576
+ _key: firstBlockKey
9577
+ }],
9578
+ select: "start"
9579
+ })] : [raise({
9580
+ type: "select.block",
9581
+ at: [{
9582
+ _key: lastBlockKey
9583
+ }],
9584
+ select: "end"
9585
+ })]]]
9539
9586
  }),
9540
9587
  defineBehavior({
9541
9588
  on: "insert.break",