@portabletext/editor 2.17.0 → 2.17.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.cjs CHANGED
@@ -4599,24 +4599,21 @@ const addAnnotationOperationImplementation = ({
4599
4599
  context,
4600
4600
  operation
4601
4601
  }) => {
4602
- const anchorBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(operation.at.anchor), focusBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(operation.at.focus), startBlockKey = operation.at.backward ? focusBlockKey : anchorBlockKey, endBlockKey = operation.at.backward ? anchorBlockKey : focusBlockKey, endOffset = operation.at.backward ? operation.at.focus.offset : operation.at.anchor.offset;
4603
- if (!startBlockKey)
4604
- throw new Error("Failed to get start block key");
4605
- if (!endBlockKey)
4606
- throw new Error("Failed to get end block key");
4607
- const startBlockIndex = operation.editor.blockIndexMap.get(startBlockKey);
4608
- if (startBlockIndex === void 0)
4609
- throw new Error("Failed to get start block index");
4610
- const startBlock = operation.editor.value.at(startBlockIndex);
4611
- if (!startBlock)
4612
- throw new Error("Failed to get start block");
4613
- const endBlockIndex = operation.editor.blockIndexMap.get(endBlockKey);
4614
- if (endBlockIndex === void 0)
4615
- throw new Error("Failed to get end block index");
4616
- const endBlock = operation.editor.value.at(endBlockIndex);
4617
- if (!endBlock)
4618
- throw new Error("Failed to get end block");
4602
+ const at = operation.at ? toSlateRange({
4603
+ context: {
4604
+ schema: context.schema,
4605
+ value: operation.editor.value,
4606
+ selection: operation.at
4607
+ },
4608
+ blockIndexMap: operation.editor.blockIndexMap
4609
+ }) : void 0, selection = operation.editor.selection ? slateRangeToSelection({
4610
+ schema: context.schema,
4611
+ editor: operation.editor,
4612
+ range: operation.editor.selection
4613
+ }) : void 0, reverse = operation.direction === "backward", anchorPoint = operation.at?.anchor ?? selection?.anchor, focusPoint = operation.at?.focus ?? selection?.focus, startPoint = reverse ? focusPoint : anchorPoint, endPoint = reverse ? anchorPoint : focusPoint, startBlockKey = startPoint ? util_getTextBlockText.getBlockKeyFromSelectionPoint(startPoint) : void 0, endBlockKey = endPoint ? util_getTextBlockText.getBlockKeyFromSelectionPoint(endPoint) : void 0, startBlockIndex = startBlockKey ? operation.editor.blockIndexMap.get(startBlockKey) : void 0, endBlockIndex = endBlockKey ? operation.editor.blockIndexMap.get(endBlockKey) : void 0, startBlock = startBlockIndex ? operation.editor.value.at(startBlockIndex) : void 0, endBlock = endBlockIndex ? operation.editor.value.at(endBlockIndex) : void 0;
4619
4614
  if (operation.unit === "block") {
4615
+ if (startBlockIndex === void 0 || endBlockIndex === void 0)
4616
+ throw new Error("Failed to get start or end block index");
4620
4617
  slate.Transforms.removeNodes(operation.editor, {
4621
4618
  at: {
4622
4619
  anchor: {
@@ -4632,17 +4629,10 @@ const addAnnotationOperationImplementation = ({
4632
4629
  }), operation.editor.children.length === 0 && slate.Transforms.insertNodes(operation.editor, createPlaceholderBlock(context));
4633
4630
  return;
4634
4631
  }
4635
- const range = toSlateRange({
4636
- context: {
4637
- schema: context.schema,
4638
- value: operation.editor.value,
4639
- selection: operation.at
4640
- },
4641
- blockIndexMap: operation.editor.blockIndexMap
4642
- });
4643
- if (!range)
4644
- throw new Error(`Failed to get Slate Range for selection ${JSON.stringify(operation.at)}`);
4645
4632
  if (operation.direction === "backward" && operation.unit === "line") {
4633
+ const range = at ?? operation.editor.selection ?? void 0;
4634
+ if (!range)
4635
+ throw new Error("Unable to delete line without a selection");
4646
4636
  const parentBlockEntry = slate.Editor.above(operation.editor, {
4647
4637
  match: (n) => slate.Element.isElement(n) && slate.Editor.isBlock(operation.editor, n),
4648
4638
  at: range
@@ -4657,15 +4647,14 @@ const addAnnotationOperationImplementation = ({
4657
4647
  }
4658
4648
  }
4659
4649
  }
4660
- const hanging = schema.isTextBlock(context, endBlock) && endOffset === 0;
4661
- slate.deleteText(operation.editor, {
4662
- at: range,
4663
- reverse: operation.direction === "backward",
4664
- unit: operation.unit,
4665
- hanging
4666
- }), operation.editor.selection && schema.isTextBlock(context, startBlock) && schema.isTextBlock(context, endBlock) && slate.setSelection(operation.editor, {
4667
- anchor: operation.editor.selection.focus,
4668
- focus: operation.editor.selection.focus
4650
+ const hanging = reverse ? endPoint ? schema.isTextBlock(context, endBlock) ? endPoint.offset === 0 : !0 : !1 : startPoint ? schema.isTextBlock(context, startBlock) ? startPoint.offset === 0 : !0 : !1;
4651
+ at ? slate.deleteText(operation.editor, {
4652
+ at,
4653
+ hanging,
4654
+ reverse
4655
+ }) : slate.deleteText(operation.editor, {
4656
+ hanging,
4657
+ reverse
4669
4658
  });
4670
4659
  };
4671
4660
  function findCurrentLineRange(editor, parentRange) {
@@ -5229,21 +5218,12 @@ function createWithEventListeners(editorActor) {
5229
5218
  editorDelete(options);
5230
5219
  return;
5231
5220
  }
5232
- const at = options?.at ?? editor.selection;
5233
- if (!at) {
5234
- console.error("Unexpected call to .delete(...) without `at` option");
5235
- return;
5236
- }
5237
- const range = slate.Editor.range(editor, at), selection = slateRangeToSelection({
5221
+ const range = options?.at ? slate.Editor.range(editor, options.at) : void 0, selection = range ? slateRangeToSelection({
5238
5222
  schema: editorActor.getSnapshot().context.schema,
5239
5223
  editor,
5240
5224
  range
5241
- });
5242
- if (!selection) {
5243
- console.error("Unexpected call to .delete(...) with invalid `at` option");
5244
- return;
5245
- }
5246
- editorActor.send({
5225
+ }) : void 0;
5226
+ selection ? editorActor.send({
5247
5227
  type: "behavior event",
5248
5228
  behaviorEvent: {
5249
5229
  type: "delete",
@@ -5252,6 +5232,14 @@ function createWithEventListeners(editorActor) {
5252
5232
  unit: options?.unit
5253
5233
  },
5254
5234
  editor
5235
+ }) : editorActor.send({
5236
+ type: "behavior event",
5237
+ behaviorEvent: {
5238
+ type: "delete",
5239
+ direction: options?.reverse ? "backward" : "forward",
5240
+ unit: options?.unit
5241
+ },
5242
+ editor
5255
5243
  });
5256
5244
  }, editor.deleteBackward = (unit) => {
5257
5245
  if (isPerformingBehaviorOperation(editor)) {
@@ -7870,11 +7858,14 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = behaviors_index.defineBehavior
7870
7858
  snapshot,
7871
7859
  event
7872
7860
  }) => {
7861
+ const at = event.at ?? snapshot.context.selection;
7862
+ if (!at)
7863
+ return !1;
7873
7864
  const blocksToDelete = selector_isAtTheStartOfBlock.getSelectedBlocks({
7874
7865
  ...snapshot,
7875
7866
  context: {
7876
7867
  ...snapshot.context,
7877
- selection: event.at
7868
+ selection: at
7878
7869
  }
7879
7870
  });
7880
7871
  if (blocksToDelete.length < 2)
@@ -7885,12 +7876,12 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = behaviors_index.defineBehavior
7885
7876
  const deleteStartPoint = selector_getSelectionText.getSelectionStartPoint({
7886
7877
  context: {
7887
7878
  ...snapshot.context,
7888
- selection: event.at
7879
+ selection: at
7889
7880
  }
7890
7881
  }), deleteEndPoint = selector_isAtTheStartOfBlock.getSelectionEndPoint({
7891
7882
  context: {
7892
7883
  ...snapshot.context,
7893
- selection: event.at
7884
+ selection: at
7894
7885
  }
7895
7886
  });
7896
7887
  if (!deleteStartPoint || !deleteEndPoint)
@@ -8275,18 +8266,13 @@ const abstractAnnotationBehaviors = [behaviors_index.defineBehavior({
8275
8266
  on: "delete.backward",
8276
8267
  guard: ({
8277
8268
  snapshot
8278
- }) => snapshot.context.selection ? {
8279
- selection: snapshot.context.selection
8280
- } : !1,
8269
+ }) => snapshot.context.selection,
8281
8270
  actions: [({
8282
8271
  event
8283
- }, {
8284
- selection
8285
8272
  }) => [behaviors_index.raise({
8286
8273
  type: "delete",
8287
8274
  direction: "backward",
8288
- unit: event.unit,
8289
- at: selection
8275
+ unit: event.unit
8290
8276
  })]]
8291
8277
  }), behaviors_index.defineBehavior({
8292
8278
  on: "delete",
@@ -8330,18 +8316,13 @@ const abstractAnnotationBehaviors = [behaviors_index.defineBehavior({
8330
8316
  on: "delete.forward",
8331
8317
  guard: ({
8332
8318
  snapshot
8333
- }) => snapshot.context.selection ? {
8334
- selection: snapshot.context.selection
8335
- } : !1,
8319
+ }) => snapshot.context.selection,
8336
8320
  actions: [({
8337
8321
  event
8338
- }, {
8339
- selection
8340
8322
  }) => [behaviors_index.raise({
8341
8323
  type: "delete",
8342
8324
  direction: "forward",
8343
- unit: event.unit,
8344
- at: selection
8325
+ unit: event.unit
8345
8326
  })]]
8346
8327
  }), behaviors_index.defineBehavior({
8347
8328
  on: "delete",
@@ -8351,17 +8332,20 @@ const abstractAnnotationBehaviors = [behaviors_index.defineBehavior({
8351
8332
  }) => {
8352
8333
  if (event.direction !== "forward")
8353
8334
  return !1;
8335
+ const at = event.at ?? snapshot.context.selection;
8336
+ if (!at)
8337
+ return !1;
8354
8338
  const nextBlock = selector_isAtTheStartOfBlock.getNextBlock({
8355
8339
  ...snapshot,
8356
8340
  context: {
8357
8341
  ...snapshot.context,
8358
- selection: event.at
8342
+ selection: at
8359
8343
  }
8360
8344
  }), focusTextBlock = selector_getSelectionText.getFocusTextBlock({
8361
8345
  ...snapshot,
8362
8346
  context: {
8363
8347
  ...snapshot.context,
8364
- selection: event.at
8348
+ selection: at
8365
8349
  }
8366
8350
  });
8367
8351
  if (!nextBlock || !focusTextBlock || !util_isEmptyTextBlock.isEmptyTextBlock(snapshot.context, focusTextBlock.node))
@@ -8698,394 +8682,436 @@ const abstractAnnotationBehaviors = [behaviors_index.defineBehavior({
8698
8682
  }
8699
8683
  }]]
8700
8684
  })
8701
- ], abstractInsertBehaviors = [behaviors_index.defineBehavior({
8702
- on: "insert.blocks",
8703
- guard: ({
8704
- event
8705
- }) => {
8706
- const onlyBlock = event.blocks.length === 1 ? event.blocks.at(0) : void 0;
8707
- return onlyBlock ? {
8685
+ ], abstractInsertBehaviors = [
8686
+ behaviors_index.defineBehavior({
8687
+ on: "insert.blocks",
8688
+ guard: ({
8689
+ event
8690
+ }) => {
8691
+ const onlyBlock = event.blocks.length === 1 ? event.blocks.at(0) : void 0;
8692
+ return onlyBlock ? {
8693
+ onlyBlock
8694
+ } : !1;
8695
+ },
8696
+ actions: [({
8697
+ event
8698
+ }, {
8708
8699
  onlyBlock
8709
- } : !1;
8710
- },
8711
- actions: [({
8712
- event
8713
- }, {
8714
- onlyBlock
8715
- }) => [behaviors_index.raise({
8716
- type: "insert.block",
8717
- block: onlyBlock,
8718
- placement: event.placement,
8719
- select: event.select ?? "end"
8720
- })]]
8721
- }), behaviors_index.defineBehavior({
8722
- on: "insert.blocks",
8723
- guard: ({
8724
- snapshot,
8725
- event
8726
- }) => {
8727
- if (event.placement !== "before")
8728
- return !1;
8729
- const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8730
- return {
8700
+ }) => [behaviors_index.raise({
8701
+ type: "insert.block",
8702
+ block: onlyBlock,
8703
+ placement: event.placement,
8704
+ select: event.select ?? "end"
8705
+ })]]
8706
+ }),
8707
+ behaviors_index.defineBehavior({
8708
+ on: "insert.blocks",
8709
+ guard: ({
8710
+ snapshot,
8711
+ event
8712
+ }) => {
8713
+ if (event.placement !== "before")
8714
+ return !1;
8715
+ const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8716
+ return {
8717
+ firstBlockKey,
8718
+ lastBlockKey
8719
+ };
8720
+ },
8721
+ actions: [({
8722
+ snapshot,
8723
+ event
8724
+ }, {
8731
8725
  firstBlockKey,
8732
8726
  lastBlockKey
8733
- };
8734
- },
8735
- actions: [({
8736
- snapshot,
8737
- event
8738
- }, {
8739
- firstBlockKey,
8740
- lastBlockKey
8741
- }) => [...event.blocks.map((block, index) => behaviors_index.raise({
8742
- type: "insert.block",
8743
- block,
8744
- placement: index === 0 ? "before" : "after",
8745
- select: index !== event.blocks.length - 1 ? "end" : "none"
8746
- })), ...event.select === "none" ? [behaviors_index.raise({
8747
- type: "select",
8748
- at: snapshot.context.selection
8749
- })] : event.select === "start" ? [behaviors_index.raise({
8750
- type: "select.block",
8751
- at: [{
8752
- _key: firstBlockKey
8753
- }],
8754
- select: "start"
8755
- })] : [behaviors_index.raise({
8756
- type: "select.block",
8757
- at: [{
8758
- _key: lastBlockKey
8759
- }],
8760
- select: "end"
8761
- })]]]
8762
- }), behaviors_index.defineBehavior({
8763
- on: "insert.blocks",
8764
- guard: ({
8765
- snapshot,
8766
- event
8767
- }) => {
8768
- if (event.placement !== "after")
8769
- return !1;
8770
- const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8771
- return {
8727
+ }) => [...event.blocks.map((block, index) => behaviors_index.raise({
8728
+ type: "insert.block",
8729
+ block,
8730
+ placement: index === 0 ? "before" : "after",
8731
+ select: index !== event.blocks.length - 1 ? "end" : "none"
8732
+ })), ...event.select === "none" ? [behaviors_index.raise({
8733
+ type: "select",
8734
+ at: snapshot.context.selection
8735
+ })] : event.select === "start" ? [behaviors_index.raise({
8736
+ type: "select.block",
8737
+ at: [{
8738
+ _key: firstBlockKey
8739
+ }],
8740
+ select: "start"
8741
+ })] : [behaviors_index.raise({
8742
+ type: "select.block",
8743
+ at: [{
8744
+ _key: lastBlockKey
8745
+ }],
8746
+ select: "end"
8747
+ })]]]
8748
+ }),
8749
+ behaviors_index.defineBehavior({
8750
+ on: "insert.blocks",
8751
+ guard: ({
8752
+ snapshot,
8753
+ event
8754
+ }) => {
8755
+ if (event.placement !== "after")
8756
+ return !1;
8757
+ const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8758
+ return {
8759
+ firstBlockKey,
8760
+ lastBlockKey
8761
+ };
8762
+ },
8763
+ actions: [({
8764
+ snapshot,
8765
+ event
8766
+ }, {
8772
8767
  firstBlockKey,
8773
8768
  lastBlockKey
8774
- };
8775
- },
8776
- actions: [({
8777
- snapshot,
8778
- event
8779
- }, {
8780
- firstBlockKey,
8781
- lastBlockKey
8782
- }) => [...event.blocks.map((block, index) => behaviors_index.raise({
8783
- type: "insert.block",
8784
- block,
8785
- placement: "after",
8786
- select: index !== event.blocks.length - 1 ? "end" : "none"
8787
- })), ...event.select === "none" ? [behaviors_index.raise({
8788
- type: "select",
8789
- at: snapshot.context.selection
8790
- })] : event.select === "start" ? [behaviors_index.raise({
8791
- type: "select.block",
8792
- at: [{
8793
- _key: firstBlockKey
8794
- }],
8795
- select: "start"
8796
- })] : [behaviors_index.raise({
8797
- type: "select.block",
8798
- at: [{
8799
- _key: lastBlockKey
8800
- }],
8801
- select: "end"
8802
- })]]]
8803
- }), behaviors_index.defineBehavior({
8804
- on: "insert.blocks",
8805
- guard: ({
8806
- snapshot,
8807
- event
8808
- }) => {
8809
- if (event.placement !== "auto")
8810
- return !1;
8811
- const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot);
8812
- if (!focusTextBlock || !util_isEmptyTextBlock.isEmptyTextBlock(snapshot.context, focusTextBlock.node))
8813
- return !1;
8814
- const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8815
- return {
8816
- focusTextBlock,
8769
+ }) => [...event.blocks.map((block, index) => behaviors_index.raise({
8770
+ type: "insert.block",
8771
+ block,
8772
+ placement: "after",
8773
+ select: index !== event.blocks.length - 1 ? "end" : "none"
8774
+ })), ...event.select === "none" ? [behaviors_index.raise({
8775
+ type: "select",
8776
+ at: snapshot.context.selection
8777
+ })] : event.select === "start" ? [behaviors_index.raise({
8778
+ type: "select.block",
8779
+ at: [{
8780
+ _key: firstBlockKey
8781
+ }],
8782
+ select: "start"
8783
+ })] : [behaviors_index.raise({
8784
+ type: "select.block",
8785
+ at: [{
8786
+ _key: lastBlockKey
8787
+ }],
8788
+ select: "end"
8789
+ })]]]
8790
+ }),
8791
+ behaviors_index.defineBehavior({
8792
+ on: "insert.blocks",
8793
+ guard: ({
8794
+ snapshot,
8795
+ event
8796
+ }) => {
8797
+ if (event.placement !== "auto")
8798
+ return !1;
8799
+ const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot);
8800
+ if (!focusTextBlock || !util_isEmptyTextBlock.isEmptyTextBlock(snapshot.context, focusTextBlock.node))
8801
+ return !1;
8802
+ const firstBlockKey = event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(), lastBlockKey = event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator();
8803
+ return {
8804
+ focusTextBlock,
8805
+ firstBlockKey,
8806
+ lastBlockKey
8807
+ };
8808
+ },
8809
+ actions: [({
8810
+ event
8811
+ }, {
8817
8812
  firstBlockKey,
8818
8813
  lastBlockKey
8819
- };
8820
- },
8821
- actions: [({
8822
- event
8823
- }, {
8824
- firstBlockKey,
8825
- lastBlockKey
8826
- }) => [...event.blocks.map((block, index) => behaviors_index.raise({
8827
- type: "insert.block",
8828
- block,
8829
- placement: index === 0 ? "auto" : "after",
8830
- select: index !== event.blocks.length - 1 ? "end" : "none"
8831
- })), ...event.select === "none" || event.select === "start" ? [behaviors_index.raise({
8832
- type: "select.block",
8833
- at: [{
8834
- _key: firstBlockKey
8835
- }],
8836
- select: "start"
8837
- })] : [behaviors_index.raise({
8838
- type: "select.block",
8839
- at: [{
8840
- _key: lastBlockKey
8841
- }],
8842
- select: "end"
8843
- })]]]
8844
- }), behaviors_index.defineBehavior({
8845
- on: "insert.blocks",
8846
- guard: ({
8847
- snapshot,
8848
- event
8849
- }) => {
8850
- if (event.placement !== "auto")
8851
- return !1;
8852
- const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot);
8853
- if (!focusTextBlock || !snapshot.context.selection)
8854
- return !1;
8855
- const focusBlockStartPoint = util_getTextBlockText.getBlockStartPoint({
8856
- context: snapshot.context,
8857
- block: focusTextBlock
8858
- }), focusBlockEndPoint = util_isEmptyTextBlock.getBlockEndPoint({
8859
- context: snapshot.context,
8860
- block: focusTextBlock
8861
- }), focusTextBlockAfter = util_sliceTextBlock.sliceTextBlock({
8862
- context: {
8863
- schema: snapshot.context.schema,
8864
- selection: {
8865
- anchor: snapshot.context.selection.focus,
8866
- focus: focusBlockEndPoint
8867
- }
8868
- },
8869
- block: focusTextBlock.node
8870
- });
8871
- return {
8872
- firstBlockKey: event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(),
8873
- focusBlockStartPoint,
8814
+ }) => [...event.blocks.map((block, index) => behaviors_index.raise({
8815
+ type: "insert.block",
8816
+ block,
8817
+ placement: index === 0 ? "auto" : "after",
8818
+ select: index !== event.blocks.length - 1 ? "end" : "none"
8819
+ })), ...event.select === "none" || event.select === "start" ? [behaviors_index.raise({
8820
+ type: "select.block",
8821
+ at: [{
8822
+ _key: firstBlockKey
8823
+ }],
8824
+ select: "start"
8825
+ })] : [behaviors_index.raise({
8826
+ type: "select.block",
8827
+ at: [{
8828
+ _key: lastBlockKey
8829
+ }],
8830
+ select: "end"
8831
+ })]]]
8832
+ }),
8833
+ behaviors_index.defineBehavior({
8834
+ on: "insert.blocks",
8835
+ guard: ({
8836
+ snapshot,
8837
+ event
8838
+ }) => {
8839
+ if (event.placement !== "auto")
8840
+ return !1;
8841
+ const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot);
8842
+ if (!focusTextBlock || !snapshot.context.selection)
8843
+ return !1;
8844
+ const focusBlockStartPoint = util_getTextBlockText.getBlockStartPoint({
8845
+ context: snapshot.context,
8846
+ block: focusTextBlock
8847
+ }), focusBlockEndPoint = util_isEmptyTextBlock.getBlockEndPoint({
8848
+ context: snapshot.context,
8849
+ block: focusTextBlock
8850
+ }), focusTextBlockAfter = util_sliceTextBlock.sliceTextBlock({
8851
+ context: {
8852
+ schema: snapshot.context.schema,
8853
+ selection: {
8854
+ anchor: snapshot.context.selection.focus,
8855
+ focus: focusBlockEndPoint
8856
+ }
8857
+ },
8858
+ block: focusTextBlock.node
8859
+ });
8860
+ return {
8861
+ firstBlockKey: event.blocks.at(0)?._key ?? snapshot.context.keyGenerator(),
8862
+ focusBlockStartPoint,
8863
+ focusBlockEndPoint,
8864
+ focusTextBlockAfter,
8865
+ selection: snapshot.context.selection
8866
+ };
8867
+ },
8868
+ actions: [({
8869
+ event
8870
+ }, {
8874
8871
  focusBlockEndPoint,
8875
8872
  focusTextBlockAfter,
8876
- selection: snapshot.context.selection
8877
- };
8878
- },
8879
- actions: [({
8880
- event
8881
- }, {
8882
- focusBlockEndPoint,
8883
- focusTextBlockAfter,
8884
- selection,
8885
- firstBlockKey,
8886
- focusBlockStartPoint
8887
- }) => [...event.blocks.flatMap((block, index) => index === 0 ? [...util_isEmptyTextBlock.isEqualSelectionPoints(selection.focus, focusBlockEndPoint) ? [] : [behaviors_index.raise({
8888
- type: "delete",
8889
- at: {
8890
- anchor: selection.focus,
8891
- focus: focusBlockEndPoint
8892
- }
8893
- })], behaviors_index.raise({
8894
- type: "insert.block",
8895
- block,
8896
- placement: "auto",
8897
- select: "end"
8898
- })] : index === event.blocks.length - 1 ? [behaviors_index.raise({
8899
- type: "insert.block",
8900
- block,
8901
- placement: "after",
8902
- select: "end"
8903
- }), behaviors_index.raise({
8904
- type: "insert.block",
8905
- block: focusTextBlockAfter,
8906
- placement: "auto",
8907
- select: event.select === "end" ? "none" : "end"
8908
- })] : [behaviors_index.raise({
8909
- type: "insert.block",
8910
- block,
8911
- placement: "after",
8912
- select: "end"
8913
- })]), ...event.select === "none" ? [behaviors_index.raise({
8914
- type: "select",
8915
- at: selection
8916
- })] : event.select === "start" ? [util_isEmptyTextBlock.isEqualSelectionPoints(selection.focus, focusBlockStartPoint) ? behaviors_index.raise({
8917
- type: "select.block",
8918
- at: [{
8919
- _key: firstBlockKey
8920
- }],
8921
- select: "start"
8922
- }) : behaviors_index.raise({
8923
- type: "select",
8924
- at: {
8925
- anchor: selection.focus,
8926
- focus: selection.focus
8927
- }
8928
- })] : []]]
8929
- }), behaviors_index.defineBehavior({
8930
- on: "insert.blocks",
8931
- guard: ({
8932
- event
8933
- }) => event.placement === "auto",
8934
- actions: [({
8935
- event
8936
- }) => event.blocks.map((block, index) => behaviors_index.raise({
8937
- type: "insert.block",
8938
- block,
8939
- placement: index === 0 ? "auto" : "after",
8940
- select: event.select ?? "end"
8941
- }))]
8942
- }), behaviors_index.defineBehavior({
8943
- on: "insert.break",
8944
- actions: [() => [behaviors_index.raise({
8945
- type: "split"
8946
- })]]
8947
- }), behaviors_index.defineBehavior({
8948
- on: "insert.child",
8949
- guard: ({
8950
- snapshot
8951
- }) => {
8952
- const lastBlock = selector_isAtTheStartOfBlock.getLastBlock(snapshot);
8953
- return !lastBlock || snapshot.context.selection ? !1 : {
8954
- lastBlockEndPoint: util_isEmptyTextBlock.getBlockEndPoint({
8955
- context: snapshot.context,
8956
- block: lastBlock
8957
- })
8958
- };
8959
- },
8960
- actions: [({
8961
- event
8962
- }, {
8963
- lastBlockEndPoint
8964
- }) => [behaviors_index.raise({
8965
- type: "select",
8966
- at: {
8967
- anchor: lastBlockEndPoint,
8968
- focus: lastBlockEndPoint
8969
- }
8970
- }), behaviors_index.raise(event)]]
8971
- }), behaviors_index.defineBehavior({
8972
- on: "insert.child",
8973
- guard: ({
8974
- snapshot
8975
- }) => {
8976
- const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot);
8977
- return snapshot.context.selection && !focusTextBlock;
8978
- },
8979
- actions: [({
8980
- snapshot,
8981
- event
8982
- }) => [behaviors_index.raise({
8983
- type: "insert.block",
8984
- block: {
8985
- _type: snapshot.context.schema.block.name,
8986
- children: [{
8987
- _type: snapshot.context.schema.span.name,
8988
- text: "",
8989
- marks: []
8990
- }]
8873
+ selection,
8874
+ firstBlockKey,
8875
+ focusBlockStartPoint
8876
+ }) => [...event.blocks.flatMap((block, index) => index === 0 ? [...util_isEmptyTextBlock.isEqualSelectionPoints(selection.focus, focusBlockEndPoint) ? [] : [behaviors_index.raise({
8877
+ type: "delete",
8878
+ at: {
8879
+ anchor: selection.focus,
8880
+ focus: focusBlockEndPoint
8881
+ }
8882
+ })], behaviors_index.raise({
8883
+ type: "insert.block",
8884
+ block,
8885
+ placement: "auto",
8886
+ select: "end"
8887
+ })] : index === event.blocks.length - 1 ? [behaviors_index.raise({
8888
+ type: "insert.block",
8889
+ block,
8890
+ placement: "after",
8891
+ select: "end"
8892
+ }), behaviors_index.raise({
8893
+ type: "insert.block",
8894
+ block: focusTextBlockAfter,
8895
+ placement: "auto",
8896
+ select: event.select === "end" ? "none" : "end"
8897
+ })] : [behaviors_index.raise({
8898
+ type: "insert.block",
8899
+ block,
8900
+ placement: "after",
8901
+ select: "end"
8902
+ })]), ...event.select === "none" ? [behaviors_index.raise({
8903
+ type: "select",
8904
+ at: selection
8905
+ })] : event.select === "start" ? [util_isEmptyTextBlock.isEqualSelectionPoints(selection.focus, focusBlockStartPoint) ? behaviors_index.raise({
8906
+ type: "select.block",
8907
+ at: [{
8908
+ _key: firstBlockKey
8909
+ }],
8910
+ select: "start"
8911
+ }) : behaviors_index.raise({
8912
+ type: "select",
8913
+ at: {
8914
+ anchor: selection.focus,
8915
+ focus: selection.focus
8916
+ }
8917
+ })] : []]]
8918
+ }),
8919
+ behaviors_index.defineBehavior({
8920
+ on: "insert.blocks",
8921
+ guard: ({
8922
+ event
8923
+ }) => event.placement === "auto",
8924
+ actions: [({
8925
+ event
8926
+ }) => event.blocks.map((block, index) => behaviors_index.raise({
8927
+ type: "insert.block",
8928
+ block,
8929
+ placement: index === 0 ? "auto" : "after",
8930
+ select: event.select ?? "end"
8931
+ }))]
8932
+ }),
8933
+ behaviors_index.defineBehavior({
8934
+ on: "insert.break",
8935
+ actions: [() => [behaviors_index.raise({
8936
+ type: "split"
8937
+ })]]
8938
+ }),
8939
+ behaviors_index.defineBehavior({
8940
+ on: "insert.child",
8941
+ guard: ({
8942
+ snapshot
8943
+ }) => {
8944
+ const lastBlock = selector_isAtTheStartOfBlock.getLastBlock(snapshot);
8945
+ return !lastBlock || snapshot.context.selection ? !1 : {
8946
+ lastBlockEndPoint: util_isEmptyTextBlock.getBlockEndPoint({
8947
+ context: snapshot.context,
8948
+ block: lastBlock
8949
+ })
8950
+ };
8991
8951
  },
8992
- placement: "auto",
8993
- select: "end"
8994
- }), behaviors_index.raise(event)]]
8995
- }), behaviors_index.defineBehavior({
8996
- on: "insert.inline object",
8997
- actions: [({
8998
- event
8999
- }) => [behaviors_index.raise({
9000
- type: "insert.child",
9001
- child: {
9002
- _type: event.inlineObject.name,
9003
- ...event.inlineObject.value
9004
- }
9005
- })]]
9006
- }), behaviors_index.defineBehavior({
9007
- on: "insert.soft break",
9008
- actions: [() => [behaviors_index.raise({
9009
- type: "insert.text",
9010
- text: `
8952
+ actions: [({
8953
+ event
8954
+ }, {
8955
+ lastBlockEndPoint
8956
+ }) => [behaviors_index.raise({
8957
+ type: "select",
8958
+ at: {
8959
+ anchor: lastBlockEndPoint,
8960
+ focus: lastBlockEndPoint
8961
+ }
8962
+ }), behaviors_index.raise(event)]]
8963
+ }),
8964
+ behaviors_index.defineBehavior({
8965
+ on: "insert.child",
8966
+ guard: ({
8967
+ snapshot
8968
+ }) => {
8969
+ const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot);
8970
+ return snapshot.context.selection && !focusTextBlock;
8971
+ },
8972
+ actions: [({
8973
+ snapshot,
8974
+ event
8975
+ }) => [behaviors_index.raise({
8976
+ type: "insert.block",
8977
+ block: {
8978
+ _type: snapshot.context.schema.block.name,
8979
+ children: [{
8980
+ _type: snapshot.context.schema.span.name,
8981
+ text: "",
8982
+ marks: []
8983
+ }]
8984
+ },
8985
+ placement: "auto",
8986
+ select: "end"
8987
+ }), behaviors_index.raise(event)]]
8988
+ }),
8989
+ behaviors_index.defineBehavior({
8990
+ on: "insert.inline object",
8991
+ actions: [({
8992
+ event
8993
+ }) => [behaviors_index.raise({
8994
+ type: "insert.child",
8995
+ child: {
8996
+ _type: event.inlineObject.name,
8997
+ ...event.inlineObject.value
8998
+ }
8999
+ })]]
9000
+ }),
9001
+ behaviors_index.defineBehavior({
9002
+ on: "insert.soft break",
9003
+ actions: [() => [behaviors_index.raise({
9004
+ type: "insert.text",
9005
+ text: `
9011
9006
  `
9012
- })]]
9013
- }), behaviors_index.defineBehavior({
9014
- on: "insert.span",
9015
- guard: ({
9016
- snapshot
9017
- }) => !selector_getSelectionText.getFocusTextBlock(snapshot),
9018
- actions: [({
9019
- snapshot,
9020
- event
9021
- }) => [behaviors_index.raise({
9022
- type: "insert.block",
9023
- block: {
9024
- _type: snapshot.context.schema.block.name,
9025
- children: [{
9026
- _type: snapshot.context.schema.span.name,
9027
- text: "",
9028
- marks: []
9029
- }]
9007
+ })]]
9008
+ }),
9009
+ behaviors_index.defineBehavior({
9010
+ on: "insert.span",
9011
+ guard: ({
9012
+ snapshot
9013
+ }) => !selector_getSelectionText.getFocusTextBlock(snapshot),
9014
+ actions: [({
9015
+ snapshot,
9016
+ event
9017
+ }) => [behaviors_index.raise({
9018
+ type: "insert.block",
9019
+ block: {
9020
+ _type: snapshot.context.schema.block.name,
9021
+ children: [{
9022
+ _type: snapshot.context.schema.span.name,
9023
+ text: "",
9024
+ marks: []
9025
+ }]
9026
+ },
9027
+ placement: "auto",
9028
+ select: "end"
9029
+ }), behaviors_index.raise(event)]]
9030
+ }),
9031
+ behaviors_index.defineBehavior({
9032
+ on: "insert.span",
9033
+ guard: ({
9034
+ snapshot,
9035
+ event
9036
+ }) => {
9037
+ const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot);
9038
+ return {
9039
+ markDefs: event.annotations?.map((annotation) => ({
9040
+ _type: annotation.name,
9041
+ _key: snapshot.context.keyGenerator(),
9042
+ ...annotation.value
9043
+ })) ?? [],
9044
+ focusTextBlock
9045
+ };
9030
9046
  },
9031
- placement: "auto",
9032
- select: "end"
9033
- }), behaviors_index.raise(event)]]
9034
- }), behaviors_index.defineBehavior({
9035
- on: "insert.span",
9036
- guard: ({
9037
- snapshot,
9038
- event
9039
- }) => {
9040
- const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot);
9041
- return {
9042
- markDefs: event.annotations?.map((annotation) => ({
9043
- _type: annotation.name,
9044
- _key: snapshot.context.keyGenerator(),
9045
- ...annotation.value
9046
- })) ?? [],
9047
+ actions: [({
9048
+ snapshot,
9049
+ event
9050
+ }, {
9051
+ markDefs,
9047
9052
  focusTextBlock
9048
- };
9049
- },
9050
- actions: [({
9051
- snapshot,
9052
- event
9053
- }, {
9054
- markDefs,
9055
- focusTextBlock
9056
- }) => [...focusTextBlock ? [behaviors_index.raise({
9057
- type: "block.set",
9058
- at: focusTextBlock.path,
9059
- props: {
9060
- markDefs: [...focusTextBlock.node.markDefs ?? [], ...markDefs]
9061
- }
9062
- })] : [], behaviors_index.raise({
9063
- type: "insert.child",
9064
- child: {
9065
- _type: snapshot.context.schema.span.name,
9066
- text: event.text,
9067
- marks: [...event.decorators ?? [], ...markDefs.map((markDef) => markDef._key)]
9068
- }
9069
- })]]
9070
- }), behaviors_index.defineBehavior({
9071
- on: "insert.text",
9072
- guard: ({
9073
- snapshot
9074
- }) => {
9075
- const selection = snapshot.context.selection;
9076
- return !selection || util_isEmptyTextBlock.isSelectionCollapsed(selection) ? !1 : {
9077
- selection
9078
- };
9079
- },
9080
- actions: [({
9081
- event
9082
- }, {
9083
- selection
9084
- }) => [behaviors_index.raise({
9085
- type: "delete",
9086
- at: selection
9087
- }), behaviors_index.raise(event)]]
9088
- })], shiftLeft = keyboardShortcuts.createKeyboardShortcut({
9053
+ }) => [...focusTextBlock ? [behaviors_index.raise({
9054
+ type: "block.set",
9055
+ at: focusTextBlock.path,
9056
+ props: {
9057
+ markDefs: [...focusTextBlock.node.markDefs ?? [], ...markDefs]
9058
+ }
9059
+ })] : [], behaviors_index.raise({
9060
+ type: "insert.child",
9061
+ child: {
9062
+ _type: snapshot.context.schema.span.name,
9063
+ text: event.text,
9064
+ marks: [...event.decorators ?? [], ...markDefs.map((markDef) => markDef._key)]
9065
+ }
9066
+ })]]
9067
+ }),
9068
+ /**
9069
+ * If there's an expanded selection, then we delete the selection before we
9070
+ * insert the text.
9071
+ */
9072
+ behaviors_index.defineBehavior({
9073
+ on: "insert.text",
9074
+ guard: ({
9075
+ snapshot
9076
+ }) => selector_getSelectionText.isSelectionExpanded(snapshot),
9077
+ actions: [({
9078
+ event
9079
+ }) => [behaviors_index.raise({
9080
+ type: "delete"
9081
+ }), behaviors_index.raise(event)]]
9082
+ }),
9083
+ /**
9084
+ * If there's no selection, then we select the end of the editor before we
9085
+ * we insert the text.
9086
+ */
9087
+ behaviors_index.defineBehavior({
9088
+ on: "insert.text",
9089
+ guard: ({
9090
+ snapshot
9091
+ }) => {
9092
+ if (snapshot.context.selection)
9093
+ return !1;
9094
+ const lastBlok = selector_isAtTheStartOfBlock.getLastBlock(snapshot);
9095
+ return lastBlok ? {
9096
+ endPoint: util_isEmptyTextBlock.getBlockEndPoint({
9097
+ context: snapshot.context,
9098
+ block: lastBlok
9099
+ })
9100
+ } : !1;
9101
+ },
9102
+ actions: [({
9103
+ event
9104
+ }, {
9105
+ endPoint
9106
+ }) => [behaviors_index.raise({
9107
+ type: "select",
9108
+ at: {
9109
+ anchor: endPoint,
9110
+ focus: endPoint
9111
+ }
9112
+ }), behaviors_index.raise(event)]]
9113
+ })
9114
+ ], shiftLeft = keyboardShortcuts.createKeyboardShortcut({
9089
9115
  default: [{
9090
9116
  key: "ArrowLeft",
9091
9117
  shift: !0,
@@ -9558,86 +9584,34 @@ const abstractAnnotationBehaviors = [behaviors_index.defineBehavior({
9558
9584
  const selection = snapshot.context.selection;
9559
9585
  if (!selection || util_isEmptyTextBlock.isSelectionCollapsed(selection))
9560
9586
  return !1;
9561
- const selectionStartBlock = selector_isAtTheStartOfBlock.getSelectionStartBlock(snapshot), selectionEndBlock = selector_isAtTheStartOfBlock.getSelectionEndBlock(snapshot);
9562
- return !selectionStartBlock || !selectionEndBlock ? !1 : !schema.isTextBlock(snapshot.context, selectionStartBlock.node) && schema.isTextBlock(snapshot.context, selectionEndBlock.node) ? {
9563
- selection
9564
- } : !1;
9565
- },
9566
- actions: [(_, {
9567
- selection
9568
- }) => [behaviors_index.raise({
9569
- type: "delete",
9570
- at: selection
9571
- })]]
9572
- }),
9573
- behaviors_index.defineBehavior({
9574
- on: "split",
9575
- guard: ({
9576
- snapshot
9577
- }) => {
9578
- const selection = snapshot.context.selection;
9579
- if (!selection || util_isEmptyTextBlock.isSelectionCollapsed(selection))
9587
+ const startPoint = util_getTextBlockText.getSelectionStartPoint(selection), endPoint = util_getTextBlockText.getSelectionEndPoint(selection);
9588
+ if (!startPoint || !endPoint)
9580
9589
  return !1;
9581
- const selectionStartBlock = selector_isAtTheStartOfBlock.getSelectionStartBlock(snapshot), selectionEndBlock = selector_isAtTheStartOfBlock.getSelectionEndBlock(snapshot);
9582
- if (!selectionStartBlock || !selectionEndBlock || selectionStartBlock.node._key === selectionEndBlock.node._key)
9590
+ const startBlock = selector_isAtTheStartOfBlock.getSelectionStartBlock(snapshot), endBlock = selector_isAtTheStartOfBlock.getSelectionEndBlock(snapshot);
9591
+ if (!startBlock || !endBlock)
9583
9592
  return !1;
9584
- const startPoint = util_getTextBlockText.getSelectionStartPoint(selection), startBlockEndPoint = util_isEmptyTextBlock.getBlockEndPoint({
9593
+ const startBlockStartPoint = util_getTextBlockText.getBlockStartPoint({
9585
9594
  context: snapshot.context,
9586
- block: selectionStartBlock
9587
- }), endPoint = util_getTextBlockText.getSelectionEndPoint(selection), endBlockStartPoint = util_getTextBlockText.getBlockStartPoint({
9595
+ block: startBlock
9596
+ }), endBlockEndPoint = util_isEmptyTextBlock.getBlockEndPoint({
9588
9597
  context: snapshot.context,
9589
- block: selectionEndBlock
9590
- }), blocksInBetween = selector_getSelectionText.getSelectedValue(snapshot).filter((block) => block._key !== selectionStartBlock.node._key && block._key !== selectionEndBlock.node._key);
9591
- return {
9592
- startPoint,
9593
- startBlockEndPoint,
9594
- endPoint,
9595
- endBlockStartPoint,
9596
- blocksInBetween
9597
- };
9598
+ block: endBlock
9599
+ });
9600
+ return !!(schema.isTextBlock(snapshot.context, startBlock.node) && schema.isTextBlock(snapshot.context, endBlock.node) && !util_isEmptyTextBlock.isEqualSelectionPoints(startPoint, startBlockStartPoint) && !util_isEmptyTextBlock.isEqualSelectionPoints(endPoint, endBlockEndPoint));
9598
9601
  },
9599
- actions: [(_, {
9600
- startPoint,
9601
- startBlockEndPoint,
9602
- endPoint,
9603
- endBlockStartPoint,
9604
- blocksInBetween
9605
- }) => [behaviors_index.raise({
9606
- type: "delete",
9607
- at: {
9608
- anchor: startPoint,
9609
- focus: startBlockEndPoint
9610
- }
9611
- }), ...blocksInBetween.map((block) => behaviors_index.raise({
9612
- type: "delete.block",
9613
- at: [{
9614
- _key: block._key
9615
- }]
9616
- })), behaviors_index.raise({
9617
- type: "delete",
9618
- at: {
9619
- anchor: endBlockStartPoint,
9620
- focus: endPoint
9621
- }
9602
+ actions: [() => [behaviors_index.raise({
9603
+ type: "delete"
9604
+ }), behaviors_index.raise({
9605
+ type: "split"
9622
9606
  })]]
9623
9607
  }),
9624
9608
  behaviors_index.defineBehavior({
9625
9609
  on: "split",
9626
9610
  guard: ({
9627
9611
  snapshot
9628
- }) => {
9629
- const selection = snapshot.context.selection;
9630
- return !selection || util_isEmptyTextBlock.isSelectionCollapsed(selection) ? !1 : {
9631
- selection
9632
- };
9633
- },
9634
- actions: [(_, {
9635
- selection
9636
- }) => [behaviors_index.raise({
9637
- type: "delete",
9638
- at: selection
9639
- }), behaviors_index.raise({
9640
- type: "split"
9612
+ }) => selector_getSelectionText.isSelectionExpanded(snapshot),
9613
+ actions: [() => [behaviors_index.raise({
9614
+ type: "delete"
9641
9615
  })]]
9642
9616
  }),
9643
9617
  behaviors_index.defineBehavior({