@portabletext/editor 2.8.4 → 2.9.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
@@ -2255,8 +2255,12 @@ PortableTextEditable.displayName = "ForwardRef(PortableTextEditable)";
2255
2255
  function validateSelection(slateEditor, activeElement) {
2256
2256
  if (!slateEditor.selection)
2257
2257
  return;
2258
- const root = slateReact.ReactEditor.findDocumentOrShadowRoot(slateEditor);
2259
- if (activeElement !== root.activeElement)
2258
+ let root;
2259
+ try {
2260
+ root = slateReact.ReactEditor.findDocumentOrShadowRoot(slateEditor);
2261
+ } catch {
2262
+ }
2263
+ if (!root || activeElement !== root.activeElement)
2260
2264
  return;
2261
2265
  const domSelection = slateReact.ReactEditor.getWindow(slateEditor).getSelection();
2262
2266
  if (!domSelection || domSelection.rangeCount === 0)
@@ -8240,28 +8244,104 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = behaviors_index.defineBehavior
8240
8244
  on: "insert.blocks",
8241
8245
  guard: ({
8242
8246
  event
8243
- }) => event.placement === "before",
8247
+ }) => {
8248
+ const onlyBlock = event.blocks.length === 1 ? event.blocks.at(0) : void 0;
8249
+ return onlyBlock ? {
8250
+ onlyBlock
8251
+ } : !1;
8252
+ },
8244
8253
  actions: [({
8245
8254
  event
8246
- }) => event.blocks.map((block, index) => behaviors_index.raise({
8255
+ }, {
8256
+ onlyBlock
8257
+ }) => [behaviors_index.raise({
8258
+ type: "insert.block",
8259
+ block: onlyBlock,
8260
+ placement: event.placement,
8261
+ select: event.select ?? "end"
8262
+ })]]
8263
+ }), behaviors_index.defineBehavior({
8264
+ on: "insert.blocks",
8265
+ guard: ({
8266
+ snapshot,
8267
+ event
8268
+ }) => {
8269
+ if (event.placement !== "before")
8270
+ return !1;
8271
+ const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8272
+ return {
8273
+ firstBlockKey,
8274
+ lastBlockKey
8275
+ };
8276
+ },
8277
+ actions: [({
8278
+ snapshot,
8279
+ event
8280
+ }, {
8281
+ firstBlockKey,
8282
+ lastBlockKey
8283
+ }) => [...event.blocks.map((block, index) => behaviors_index.raise({
8247
8284
  type: "insert.block",
8248
8285
  block,
8249
8286
  placement: index === 0 ? "before" : "after",
8250
- select: event.select ?? "end"
8251
- }))]
8287
+ select: index !== event.blocks.length - 1 ? "end" : "none"
8288
+ })), ...event.select === "none" ? [behaviors_index.raise({
8289
+ type: "select",
8290
+ at: snapshot.context.selection
8291
+ })] : event.select === "start" ? [behaviors_index.raise({
8292
+ type: "select.block",
8293
+ at: [{
8294
+ _key: firstBlockKey
8295
+ }],
8296
+ select: "start"
8297
+ })] : [behaviors_index.raise({
8298
+ type: "select.block",
8299
+ at: [{
8300
+ _key: lastBlockKey
8301
+ }],
8302
+ select: "end"
8303
+ })]]]
8252
8304
  }), behaviors_index.defineBehavior({
8253
8305
  on: "insert.blocks",
8254
8306
  guard: ({
8307
+ snapshot,
8255
8308
  event
8256
- }) => event.placement === "after",
8309
+ }) => {
8310
+ if (event.placement !== "after")
8311
+ return !1;
8312
+ const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8313
+ return {
8314
+ firstBlockKey,
8315
+ lastBlockKey
8316
+ };
8317
+ },
8257
8318
  actions: [({
8319
+ snapshot,
8258
8320
  event
8259
- }) => event.blocks.map((block) => behaviors_index.raise({
8321
+ }, {
8322
+ firstBlockKey,
8323
+ lastBlockKey
8324
+ }) => [...event.blocks.map((block, index) => behaviors_index.raise({
8260
8325
  type: "insert.block",
8261
8326
  block,
8262
8327
  placement: "after",
8263
- select: event.select ?? "end"
8264
- }))]
8328
+ select: index !== event.blocks.length - 1 ? "end" : "none"
8329
+ })), ...event.select === "none" ? [behaviors_index.raise({
8330
+ type: "select",
8331
+ at: snapshot.context.selection
8332
+ })] : event.select === "start" ? [behaviors_index.raise({
8333
+ type: "select.block",
8334
+ at: [{
8335
+ _key: firstBlockKey
8336
+ }],
8337
+ select: "start"
8338
+ })] : [behaviors_index.raise({
8339
+ type: "select.block",
8340
+ at: [{
8341
+ _key: lastBlockKey
8342
+ }],
8343
+ select: "end"
8344
+ })]]]
8265
8345
  }), behaviors_index.defineBehavior({
8266
8346
  on: "insert.blocks",
8267
8347
  guard: ({
@@ -8271,49 +8351,123 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = behaviors_index.defineBehavior
8271
8351
  if (event.placement !== "auto")
8272
8352
  return !1;
8273
8353
  const focusTextBlock = selector_isSelectionExpanded.getFocusTextBlock(snapshot);
8274
- return focusTextBlock ? {
8275
- focusTextBlock
8276
- } : !1;
8354
+ if (!focusTextBlock || !util_isSelectionCollapsed.isEmptyTextBlock(snapshot.context, focusTextBlock.node))
8355
+ return !1;
8356
+ const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8357
+ return {
8358
+ focusTextBlock,
8359
+ firstBlockKey,
8360
+ lastBlockKey
8361
+ };
8277
8362
  },
8278
8363
  actions: [({
8279
- snapshot,
8280
8364
  event
8281
8365
  }, {
8282
- focusTextBlock
8283
- }) => event.blocks.length === 1 ? [behaviors_index.raise({
8284
- type: "insert.block",
8285
- block: event.blocks[0],
8286
- placement: "auto",
8287
- select: event.select ?? "end"
8288
- })] : util_isSelectionCollapsed.isEmptyTextBlock(snapshot.context, focusTextBlock.node) ? event.blocks.map((block, index) => behaviors_index.raise({
8366
+ firstBlockKey,
8367
+ lastBlockKey
8368
+ }) => [...event.blocks.map((block, index) => behaviors_index.raise({
8289
8369
  type: "insert.block",
8290
8370
  block,
8291
8371
  placement: index === 0 ? "auto" : "after",
8292
- select: event.select ?? "end"
8293
- })) : event.blocks.flatMap((block, index) => index === 0 ? [behaviors_index.raise({
8294
- type: "split"
8295
- }), behaviors_index.raise({
8296
- type: "select.previous block",
8372
+ select: index !== event.blocks.length - 1 ? "end" : "none"
8373
+ })), ...event.select === "none" || event.select === "start" ? [behaviors_index.raise({
8374
+ type: "select.block",
8375
+ at: [{
8376
+ _key: firstBlockKey
8377
+ }],
8378
+ select: "start"
8379
+ })] : [behaviors_index.raise({
8380
+ type: "select.block",
8381
+ at: [{
8382
+ _key: lastBlockKey
8383
+ }],
8297
8384
  select: "end"
8298
- }), behaviors_index.raise({
8385
+ })]]]
8386
+ }), behaviors_index.defineBehavior({
8387
+ on: "insert.blocks",
8388
+ guard: ({
8389
+ snapshot,
8390
+ event
8391
+ }) => {
8392
+ if (event.placement !== "auto")
8393
+ return !1;
8394
+ const focusTextBlock = selector_isSelectionExpanded.getFocusTextBlock(snapshot);
8395
+ if (!focusTextBlock || !snapshot.context.selection)
8396
+ return !1;
8397
+ const focusBlockStartPoint = util_sliceBlocks.getBlockStartPoint({
8398
+ context: snapshot.context,
8399
+ block: focusTextBlock
8400
+ }), focusBlockEndPoint = util_isSelectionCollapsed.getBlockEndPoint({
8401
+ context: snapshot.context,
8402
+ block: focusTextBlock
8403
+ }), focusTextBlockAfter = util_sliceTextBlock.sliceTextBlock({
8404
+ context: {
8405
+ schema: snapshot.context.schema,
8406
+ selection: {
8407
+ anchor: snapshot.context.selection.focus,
8408
+ focus: focusBlockEndPoint
8409
+ }
8410
+ },
8411
+ block: focusTextBlock.node
8412
+ });
8413
+ return {
8414
+ firstBlockKey: event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(),
8415
+ focusBlockStartPoint,
8416
+ focusBlockEndPoint,
8417
+ focusTextBlockAfter,
8418
+ selection: snapshot.context.selection
8419
+ };
8420
+ },
8421
+ actions: [({
8422
+ event
8423
+ }, {
8424
+ focusBlockEndPoint,
8425
+ focusTextBlockAfter,
8426
+ selection,
8427
+ firstBlockKey,
8428
+ focusBlockStartPoint
8429
+ }) => [...event.blocks.flatMap((block, index) => index === 0 ? [...util_isSelectionCollapsed.isEqualSelectionPoints(selection.focus, focusBlockEndPoint) ? [] : [behaviors_index.raise({
8430
+ type: "delete",
8431
+ at: {
8432
+ anchor: selection.focus,
8433
+ focus: focusBlockEndPoint
8434
+ }
8435
+ })], behaviors_index.raise({
8299
8436
  type: "insert.block",
8300
8437
  block,
8301
8438
  placement: "auto",
8302
- select: event.select ?? "end"
8439
+ select: "end"
8303
8440
  })] : index === event.blocks.length - 1 ? [behaviors_index.raise({
8304
- type: "select.next block",
8305
- select: "start"
8306
- }), behaviors_index.raise({
8307
8441
  type: "insert.block",
8308
8442
  block,
8443
+ placement: "after",
8444
+ select: "end"
8445
+ }), behaviors_index.raise({
8446
+ type: "insert.block",
8447
+ block: focusTextBlockAfter,
8309
8448
  placement: "auto",
8310
- select: event.select ?? "end"
8449
+ select: event.select === "end" ? "none" : "end"
8311
8450
  })] : [behaviors_index.raise({
8312
8451
  type: "insert.block",
8313
8452
  block,
8314
8453
  placement: "after",
8315
- select: event.select ?? "end"
8316
- })])]
8454
+ select: "end"
8455
+ })]), ...event.select === "none" ? [behaviors_index.raise({
8456
+ type: "select",
8457
+ at: selection
8458
+ })] : event.select === "start" ? [util_isSelectionCollapsed.isEqualSelectionPoints(selection.focus, focusBlockStartPoint) ? behaviors_index.raise({
8459
+ type: "select.block",
8460
+ at: [{
8461
+ _key: firstBlockKey
8462
+ }],
8463
+ select: "start"
8464
+ }) : behaviors_index.raise({
8465
+ type: "select",
8466
+ at: {
8467
+ anchor: selection.focus,
8468
+ focus: selection.focus
8469
+ }
8470
+ })] : []]]
8317
8471
  }), behaviors_index.defineBehavior({
8318
8472
  on: "insert.blocks",
8319
8473
  guard: ({
@@ -8569,62 +8723,99 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = behaviors_index.defineBehavior
8569
8723
  to: nextBlock.path
8570
8724
  })]]
8571
8725
  })], abstractSelectBehaviors = [behaviors_index.defineBehavior({
8572
- on: "select.previous block",
8726
+ on: "select.block",
8573
8727
  guard: ({
8574
8728
  snapshot,
8575
8729
  event
8576
8730
  }) => {
8577
- const previousBlock = selector_isSelectingEntireBlocks.getPreviousBlock(snapshot);
8578
- if (!previousBlock)
8731
+ if (event.select !== "end")
8579
8732
  return !1;
8580
- const point = event.select === "end" ? util_isSelectionCollapsed.getBlockEndPoint({
8581
- context: snapshot.context,
8582
- block: previousBlock
8583
- }) : util_sliceBlocks.getBlockStartPoint({
8584
- context: snapshot.context,
8585
- block: previousBlock
8586
- });
8587
- return {
8588
- selection: {
8589
- anchor: point,
8590
- focus: point
8733
+ const block = selector_isSelectionExpanded.getFocusBlock({
8734
+ ...snapshot,
8735
+ context: {
8736
+ ...snapshot.context,
8737
+ selection: {
8738
+ anchor: {
8739
+ path: event.at,
8740
+ offset: 0
8741
+ },
8742
+ focus: {
8743
+ path: event.at,
8744
+ offset: 0
8745
+ }
8746
+ }
8591
8747
  }
8592
- };
8748
+ });
8749
+ return block ? {
8750
+ blockEndPoint: util_isSelectionCollapsed.getBlockEndPoint({
8751
+ context: snapshot.context,
8752
+ block
8753
+ })
8754
+ } : !1;
8593
8755
  },
8594
8756
  actions: [(_, {
8595
- selection
8757
+ blockEndPoint
8596
8758
  }) => [behaviors_index.raise({
8597
8759
  type: "select",
8598
- at: selection
8760
+ at: {
8761
+ anchor: blockEndPoint,
8762
+ focus: blockEndPoint
8763
+ }
8599
8764
  })]]
8600
8765
  }), behaviors_index.defineBehavior({
8601
- on: "select.next block",
8766
+ on: "select.block",
8767
+ actions: [({
8768
+ event
8769
+ }) => [behaviors_index.raise({
8770
+ type: "select",
8771
+ at: {
8772
+ anchor: {
8773
+ path: event.at,
8774
+ offset: 0
8775
+ },
8776
+ focus: {
8777
+ path: event.at,
8778
+ offset: 0
8779
+ }
8780
+ }
8781
+ })]]
8782
+ }), behaviors_index.defineBehavior({
8783
+ on: "select.previous block",
8602
8784
  guard: ({
8603
- snapshot,
8785
+ snapshot
8786
+ }) => {
8787
+ const previousBlock = selector_isSelectingEntireBlocks.getPreviousBlock(snapshot);
8788
+ return previousBlock ? {
8789
+ previousBlock
8790
+ } : !1;
8791
+ },
8792
+ actions: [({
8604
8793
  event
8794
+ }, {
8795
+ previousBlock
8796
+ }) => [behaviors_index.raise({
8797
+ type: "select.block",
8798
+ at: previousBlock.path,
8799
+ select: event.select
8800
+ })]]
8801
+ }), behaviors_index.defineBehavior({
8802
+ on: "select.next block",
8803
+ guard: ({
8804
+ snapshot
8605
8805
  }) => {
8606
8806
  const nextBlock = selector_isSelectingEntireBlocks.getNextBlock(snapshot);
8607
- if (!nextBlock)
8608
- return !1;
8609
- const point = event.select === "end" ? util_isSelectionCollapsed.getBlockEndPoint({
8610
- context: snapshot.context,
8611
- block: nextBlock
8612
- }) : util_sliceBlocks.getBlockStartPoint({
8613
- context: snapshot.context,
8614
- block: nextBlock
8615
- });
8616
- return {
8617
- selection: {
8618
- anchor: point,
8619
- focus: point
8620
- }
8621
- };
8807
+ return nextBlock ? {
8808
+ nextBlock
8809
+ } : !1;
8622
8810
  },
8623
- actions: [(_, {
8624
- selection
8811
+ actions: [({
8812
+ event
8813
+ }, {
8814
+ nextBlock
8625
8815
  }) => [behaviors_index.raise({
8626
- type: "select",
8627
- at: selection
8816
+ type: "select.block",
8817
+ at: nextBlock.path,
8818
+ select: event.select
8628
8819
  })]]
8629
8820
  })], abstractSerializeBehaviors = [behaviors_index.defineBehavior({
8630
8821
  on: "serialize",
@@ -8998,7 +9189,7 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = behaviors_index.defineBehavior
8998
9189
  function isSyntheticBehaviorEvent(event) {
8999
9190
  return !isCustomBehaviorEvent(event) && !isNativeBehaviorEvent(event) && !isAbstractBehaviorEvent(event);
9000
9191
  }
9001
- 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"];
9192
+ 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"];
9002
9193
  function isAbstractBehaviorEvent(event) {
9003
9194
  return abstractBehaviorEventTypes.includes(event.type);
9004
9195
  }