@portabletext/editor 2.8.4 → 2.9.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
@@ -8269,28 +8269,104 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = defineBehavior({
8269
8269
  on: "insert.blocks",
8270
8270
  guard: ({
8271
8271
  event
8272
- }) => event.placement === "before",
8272
+ }) => {
8273
+ const onlyBlock = event.blocks.length === 1 ? event.blocks.at(0) : void 0;
8274
+ return onlyBlock ? {
8275
+ onlyBlock
8276
+ } : !1;
8277
+ },
8273
8278
  actions: [({
8274
8279
  event
8275
- }) => event.blocks.map((block, index) => raise({
8280
+ }, {
8281
+ onlyBlock
8282
+ }) => [raise({
8283
+ type: "insert.block",
8284
+ block: onlyBlock,
8285
+ placement: event.placement,
8286
+ select: event.select ?? "end"
8287
+ })]]
8288
+ }), defineBehavior({
8289
+ on: "insert.blocks",
8290
+ guard: ({
8291
+ snapshot,
8292
+ event
8293
+ }) => {
8294
+ if (event.placement !== "before")
8295
+ return !1;
8296
+ const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8297
+ return {
8298
+ firstBlockKey,
8299
+ lastBlockKey
8300
+ };
8301
+ },
8302
+ actions: [({
8303
+ snapshot,
8304
+ event
8305
+ }, {
8306
+ firstBlockKey,
8307
+ lastBlockKey
8308
+ }) => [...event.blocks.map((block, index) => raise({
8276
8309
  type: "insert.block",
8277
8310
  block,
8278
8311
  placement: index === 0 ? "before" : "after",
8279
- select: event.select ?? "end"
8280
- }))]
8312
+ select: index !== event.blocks.length - 1 ? "end" : "none"
8313
+ })), ...event.select === "none" ? [raise({
8314
+ type: "select",
8315
+ at: snapshot.context.selection
8316
+ })] : event.select === "start" ? [raise({
8317
+ type: "select.block",
8318
+ at: [{
8319
+ _key: firstBlockKey
8320
+ }],
8321
+ select: "start"
8322
+ })] : [raise({
8323
+ type: "select.block",
8324
+ at: [{
8325
+ _key: lastBlockKey
8326
+ }],
8327
+ select: "end"
8328
+ })]]]
8281
8329
  }), defineBehavior({
8282
8330
  on: "insert.blocks",
8283
8331
  guard: ({
8332
+ snapshot,
8284
8333
  event
8285
- }) => event.placement === "after",
8334
+ }) => {
8335
+ if (event.placement !== "after")
8336
+ return !1;
8337
+ const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8338
+ return {
8339
+ firstBlockKey,
8340
+ lastBlockKey
8341
+ };
8342
+ },
8286
8343
  actions: [({
8344
+ snapshot,
8287
8345
  event
8288
- }) => event.blocks.map((block) => raise({
8346
+ }, {
8347
+ firstBlockKey,
8348
+ lastBlockKey
8349
+ }) => [...event.blocks.map((block, index) => raise({
8289
8350
  type: "insert.block",
8290
8351
  block,
8291
8352
  placement: "after",
8292
- select: event.select ?? "end"
8293
- }))]
8353
+ select: index !== event.blocks.length - 1 ? "end" : "none"
8354
+ })), ...event.select === "none" ? [raise({
8355
+ type: "select",
8356
+ at: snapshot.context.selection
8357
+ })] : event.select === "start" ? [raise({
8358
+ type: "select.block",
8359
+ at: [{
8360
+ _key: firstBlockKey
8361
+ }],
8362
+ select: "start"
8363
+ })] : [raise({
8364
+ type: "select.block",
8365
+ at: [{
8366
+ _key: lastBlockKey
8367
+ }],
8368
+ select: "end"
8369
+ })]]]
8294
8370
  }), defineBehavior({
8295
8371
  on: "insert.blocks",
8296
8372
  guard: ({
@@ -8300,49 +8376,123 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = defineBehavior({
8300
8376
  if (event.placement !== "auto")
8301
8377
  return !1;
8302
8378
  const focusTextBlock = getFocusTextBlock(snapshot);
8303
- return focusTextBlock ? {
8304
- focusTextBlock
8305
- } : !1;
8379
+ if (!focusTextBlock || !isEmptyTextBlock(snapshot.context, focusTextBlock.node))
8380
+ return !1;
8381
+ const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8382
+ return {
8383
+ focusTextBlock,
8384
+ firstBlockKey,
8385
+ lastBlockKey
8386
+ };
8306
8387
  },
8307
8388
  actions: [({
8308
- snapshot,
8309
8389
  event
8310
8390
  }, {
8311
- focusTextBlock
8312
- }) => event.blocks.length === 1 ? [raise({
8313
- type: "insert.block",
8314
- block: event.blocks[0],
8315
- placement: "auto",
8316
- select: event.select ?? "end"
8317
- })] : isEmptyTextBlock(snapshot.context, focusTextBlock.node) ? event.blocks.map((block, index) => raise({
8391
+ firstBlockKey,
8392
+ lastBlockKey
8393
+ }) => [...event.blocks.map((block, index) => raise({
8318
8394
  type: "insert.block",
8319
8395
  block,
8320
8396
  placement: index === 0 ? "auto" : "after",
8321
- select: event.select ?? "end"
8322
- })) : event.blocks.flatMap((block, index) => index === 0 ? [raise({
8323
- type: "split"
8324
- }), raise({
8325
- type: "select.previous block",
8397
+ select: index !== event.blocks.length - 1 ? "end" : "none"
8398
+ })), ...event.select === "none" || event.select === "start" ? [raise({
8399
+ type: "select.block",
8400
+ at: [{
8401
+ _key: firstBlockKey
8402
+ }],
8403
+ select: "start"
8404
+ })] : [raise({
8405
+ type: "select.block",
8406
+ at: [{
8407
+ _key: lastBlockKey
8408
+ }],
8326
8409
  select: "end"
8327
- }), raise({
8410
+ })]]]
8411
+ }), defineBehavior({
8412
+ on: "insert.blocks",
8413
+ guard: ({
8414
+ snapshot,
8415
+ event
8416
+ }) => {
8417
+ if (event.placement !== "auto")
8418
+ return !1;
8419
+ const focusTextBlock = getFocusTextBlock(snapshot);
8420
+ if (!focusTextBlock || !snapshot.context.selection)
8421
+ return !1;
8422
+ const focusBlockStartPoint = getBlockStartPoint({
8423
+ context: snapshot.context,
8424
+ block: focusTextBlock
8425
+ }), focusBlockEndPoint = getBlockEndPoint({
8426
+ context: snapshot.context,
8427
+ block: focusTextBlock
8428
+ }), focusTextBlockAfter = sliceTextBlock({
8429
+ context: {
8430
+ schema: snapshot.context.schema,
8431
+ selection: {
8432
+ anchor: snapshot.context.selection.focus,
8433
+ focus: focusBlockEndPoint
8434
+ }
8435
+ },
8436
+ block: focusTextBlock.node
8437
+ });
8438
+ return {
8439
+ firstBlockKey: event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(),
8440
+ focusBlockStartPoint,
8441
+ focusBlockEndPoint,
8442
+ focusTextBlockAfter,
8443
+ selection: snapshot.context.selection
8444
+ };
8445
+ },
8446
+ actions: [({
8447
+ event
8448
+ }, {
8449
+ focusBlockEndPoint,
8450
+ focusTextBlockAfter,
8451
+ selection,
8452
+ firstBlockKey,
8453
+ focusBlockStartPoint
8454
+ }) => [...event.blocks.flatMap((block, index) => index === 0 ? [...isEqualSelectionPoints(selection.focus, focusBlockEndPoint) ? [] : [raise({
8455
+ type: "delete",
8456
+ at: {
8457
+ anchor: selection.focus,
8458
+ focus: focusBlockEndPoint
8459
+ }
8460
+ })], raise({
8328
8461
  type: "insert.block",
8329
8462
  block,
8330
8463
  placement: "auto",
8331
- select: event.select ?? "end"
8464
+ select: "end"
8332
8465
  })] : index === event.blocks.length - 1 ? [raise({
8333
- type: "select.next block",
8334
- select: "start"
8335
- }), raise({
8336
8466
  type: "insert.block",
8337
8467
  block,
8468
+ placement: "after",
8469
+ select: "end"
8470
+ }), raise({
8471
+ type: "insert.block",
8472
+ block: focusTextBlockAfter,
8338
8473
  placement: "auto",
8339
- select: event.select ?? "end"
8474
+ select: event.select === "end" ? "none" : "end"
8340
8475
  })] : [raise({
8341
8476
  type: "insert.block",
8342
8477
  block,
8343
8478
  placement: "after",
8344
- select: event.select ?? "end"
8345
- })])]
8479
+ select: "end"
8480
+ })]), ...event.select === "none" ? [raise({
8481
+ type: "select",
8482
+ at: selection
8483
+ })] : event.select === "start" ? [isEqualSelectionPoints(selection.focus, focusBlockStartPoint) ? raise({
8484
+ type: "select.block",
8485
+ at: [{
8486
+ _key: firstBlockKey
8487
+ }],
8488
+ select: "start"
8489
+ }) : raise({
8490
+ type: "select",
8491
+ at: {
8492
+ anchor: selection.focus,
8493
+ focus: selection.focus
8494
+ }
8495
+ })] : []]]
8346
8496
  }), defineBehavior({
8347
8497
  on: "insert.blocks",
8348
8498
  guard: ({
@@ -8598,62 +8748,99 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = defineBehavior({
8598
8748
  to: nextBlock.path
8599
8749
  })]]
8600
8750
  })], abstractSelectBehaviors = [defineBehavior({
8601
- on: "select.previous block",
8751
+ on: "select.block",
8602
8752
  guard: ({
8603
8753
  snapshot,
8604
8754
  event
8605
8755
  }) => {
8606
- const previousBlock = getPreviousBlock(snapshot);
8607
- if (!previousBlock)
8756
+ if (event.select !== "end")
8608
8757
  return !1;
8609
- const point = event.select === "end" ? getBlockEndPoint({
8610
- context: snapshot.context,
8611
- block: previousBlock
8612
- }) : getBlockStartPoint({
8613
- context: snapshot.context,
8614
- block: previousBlock
8615
- });
8616
- return {
8617
- selection: {
8618
- anchor: point,
8619
- focus: point
8758
+ const block = getFocusBlock$1({
8759
+ ...snapshot,
8760
+ context: {
8761
+ ...snapshot.context,
8762
+ selection: {
8763
+ anchor: {
8764
+ path: event.at,
8765
+ offset: 0
8766
+ },
8767
+ focus: {
8768
+ path: event.at,
8769
+ offset: 0
8770
+ }
8771
+ }
8620
8772
  }
8621
- };
8773
+ });
8774
+ return block ? {
8775
+ blockEndPoint: getBlockEndPoint({
8776
+ context: snapshot.context,
8777
+ block
8778
+ })
8779
+ } : !1;
8622
8780
  },
8623
8781
  actions: [(_, {
8624
- selection
8782
+ blockEndPoint
8625
8783
  }) => [raise({
8626
8784
  type: "select",
8627
- at: selection
8785
+ at: {
8786
+ anchor: blockEndPoint,
8787
+ focus: blockEndPoint
8788
+ }
8628
8789
  })]]
8629
8790
  }), defineBehavior({
8630
- on: "select.next block",
8791
+ on: "select.block",
8792
+ actions: [({
8793
+ event
8794
+ }) => [raise({
8795
+ type: "select",
8796
+ at: {
8797
+ anchor: {
8798
+ path: event.at,
8799
+ offset: 0
8800
+ },
8801
+ focus: {
8802
+ path: event.at,
8803
+ offset: 0
8804
+ }
8805
+ }
8806
+ })]]
8807
+ }), defineBehavior({
8808
+ on: "select.previous block",
8631
8809
  guard: ({
8632
- snapshot,
8810
+ snapshot
8811
+ }) => {
8812
+ const previousBlock = getPreviousBlock(snapshot);
8813
+ return previousBlock ? {
8814
+ previousBlock
8815
+ } : !1;
8816
+ },
8817
+ actions: [({
8633
8818
  event
8819
+ }, {
8820
+ previousBlock
8821
+ }) => [raise({
8822
+ type: "select.block",
8823
+ at: previousBlock.path,
8824
+ select: event.select
8825
+ })]]
8826
+ }), defineBehavior({
8827
+ on: "select.next block",
8828
+ guard: ({
8829
+ snapshot
8634
8830
  }) => {
8635
8831
  const nextBlock = getNextBlock(snapshot);
8636
- if (!nextBlock)
8637
- return !1;
8638
- const point = event.select === "end" ? getBlockEndPoint({
8639
- context: snapshot.context,
8640
- block: nextBlock
8641
- }) : getBlockStartPoint({
8642
- context: snapshot.context,
8643
- block: nextBlock
8644
- });
8645
- return {
8646
- selection: {
8647
- anchor: point,
8648
- focus: point
8649
- }
8650
- };
8832
+ return nextBlock ? {
8833
+ nextBlock
8834
+ } : !1;
8651
8835
  },
8652
- actions: [(_, {
8653
- selection
8836
+ actions: [({
8837
+ event
8838
+ }, {
8839
+ nextBlock
8654
8840
  }) => [raise({
8655
- type: "select",
8656
- at: selection
8841
+ type: "select.block",
8842
+ at: nextBlock.path,
8843
+ select: event.select
8657
8844
  })]]
8658
8845
  })], abstractSerializeBehaviors = [defineBehavior({
8659
8846
  on: "serialize",
@@ -9027,7 +9214,7 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = defineBehavior({
9027
9214
  function isSyntheticBehaviorEvent(event) {
9028
9215
  return !isCustomBehaviorEvent(event) && !isNativeBehaviorEvent(event) && !isAbstractBehaviorEvent(event);
9029
9216
  }
9030
- const abstractBehaviorEventTypes = ["annotation.set", "annotation.toggle", "decorator.toggle", "delete.backward", "delete.block", "delete.child", "delete.forward", "delete.text", "deserialize", "deserialize.data", "deserialization.success", "deserialization.failure", "insert.blocks", "insert.break", "insert.inline object", "insert.soft break", "insert.span", "list item.add", "list item.remove", "list item.toggle", "move.block down", "move.block up", "select.previous block", "select.next block", "serialize", "serialize.data", "serialization.success", "serialization.failure", "split", "style.add", "style.remove", "style.toggle"];
9217
+ const abstractBehaviorEventTypes = ["annotation.set", "annotation.toggle", "decorator.toggle", "delete.backward", "delete.block", "delete.child", "delete.forward", "delete.text", "deserialize", "deserialize.data", "deserialization.success", "deserialization.failure", "insert.blocks", "insert.break", "insert.inline object", "insert.soft break", "insert.span", "list item.add", "list item.remove", "list item.toggle", "move.block down", "move.block up", "select.block", "select.previous block", "select.next block", "serialize", "serialize.data", "serialization.success", "serialization.failure", "split", "style.add", "style.remove", "style.toggle"];
9031
9218
  function isAbstractBehaviorEvent(event) {
9032
9219
  return abstractBehaviorEventTypes.includes(event.type);
9033
9220
  }