@portabletext/editor 1.47.11 → 1.47.13

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.
Files changed (47) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +61 -3
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/editor-provider.cjs +88 -58
  4. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  5. package/lib/_chunks-cjs/util.get-selection-start-point.cjs +10 -0
  6. package/lib/_chunks-cjs/util.get-selection-start-point.cjs.map +1 -0
  7. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs +0 -8
  8. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs.map +1 -1
  9. package/lib/_chunks-es/behavior.core.js +63 -4
  10. package/lib/_chunks-es/behavior.core.js.map +1 -1
  11. package/lib/_chunks-es/editor-provider.js +90 -59
  12. package/lib/_chunks-es/editor-provider.js.map +1 -1
  13. package/lib/_chunks-es/util.get-selection-start-point.js +11 -0
  14. package/lib/_chunks-es/util.get-selection-start-point.js.map +1 -0
  15. package/lib/_chunks-es/util.selection-point-to-block-offset.js +0 -8
  16. package/lib/_chunks-es/util.selection-point-to-block-offset.js.map +1 -1
  17. package/lib/behaviors/index.d.cts +4876 -288
  18. package/lib/behaviors/index.d.ts +4876 -288
  19. package/lib/index.cjs +3 -3
  20. package/lib/index.cjs.map +1 -1
  21. package/lib/index.d.cts +29 -42
  22. package/lib/index.d.ts +29 -42
  23. package/lib/index.js +1 -1
  24. package/lib/plugins/index.cjs +0 -9
  25. package/lib/plugins/index.cjs.map +1 -1
  26. package/lib/plugins/index.d.cts +29 -42
  27. package/lib/plugins/index.d.ts +29 -42
  28. package/lib/plugins/index.js +0 -9
  29. package/lib/plugins/index.js.map +1 -1
  30. package/lib/selectors/index.d.cts +29 -41
  31. package/lib/selectors/index.d.ts +29 -41
  32. package/lib/utils/index.cjs +3 -3
  33. package/lib/utils/index.d.cts +29 -41
  34. package/lib/utils/index.d.ts +29 -41
  35. package/lib/utils/index.js +2 -1
  36. package/lib/utils/index.js.map +1 -1
  37. package/package.json +6 -6
  38. package/src/behavior-actions/behavior.action.insert.block.ts +136 -45
  39. package/src/behaviors/behavior.abstract.insert.ts +2 -2
  40. package/src/behaviors/behavior.abstract.split.ts +2 -46
  41. package/src/behaviors/behavior.core.insert-break.ts +113 -0
  42. package/src/behaviors/behavior.core.ts +2 -0
  43. package/src/behaviors/behavior.perform-event.ts +2 -7
  44. package/src/behaviors/behavior.types.action.ts +7 -13
  45. package/src/behaviors/behavior.types.event.ts +31 -33
  46. package/src/internal-utils/slate-utils.ts +50 -1
  47. package/src/plugins/plugin.one-line.tsx +0 -7
@@ -14,7 +14,7 @@ import isPlainObject from "lodash/isPlainObject.js";
14
14
  import uniq from "lodash/uniq.js";
15
15
  import getRandomValues from "get-random-values-esm";
16
16
  import { parseBlock, parseAnnotation, isTextBlock, parseInlineObject } from "./parse-blocks.js";
17
- import { sliceBlocks, blockOffsetToSpanSelectionPoint, isEmptyTextBlock, getBlockEndPoint, getBlockStartPoint, isEqualSelectionPoints, getTextBlockText } from "./util.slice-blocks.js";
17
+ import { sliceBlocks, blockOffsetToSpanSelectionPoint, isEmptyTextBlock, getBlockEndPoint, getBlockStartPoint, getTextBlockText } from "./util.slice-blocks.js";
18
18
  import { htmlToBlocks } from "@portabletext/block-tools";
19
19
  import { toHTML } from "@portabletext/to-html";
20
20
  import { Schema } from "@sanity/schema";
@@ -22,12 +22,13 @@ import get from "lodash/get.js";
22
22
  import isUndefined from "lodash/isUndefined.js";
23
23
  import omitBy from "lodash/omitBy.js";
24
24
  import omit from "lodash/omit.js";
25
- import { selectionPointToBlockOffset, blockOffsetsToSelection, getSelectionStartPoint, getSelectionEndPoint } from "./util.selection-point-to-block-offset.js";
25
+ import { selectionPointToBlockOffset, blockOffsetsToSelection } from "./util.selection-point-to-block-offset.js";
26
26
  import { getTrimmedSelection, isActiveAnnotation, isActiveDecorator, getSelectedTextBlocks, isActiveListItem, isActiveStyle, isSelectingEntireBlocks, getActiveAnnotations } from "./selector.is-selecting-entire-blocks.js";
27
27
  import { DOMEditor } from "slate-dom";
28
28
  import startCase from "lodash.startcase";
29
29
  import { defineBehavior, raise, coreBehaviors } from "./behavior.core.js";
30
- import { getFocusTextBlock, getPreviousBlock, getNextBlock, isSelectionExpanded, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusBlockObject, getFocusSpan, isSelectionCollapsed, isOverlappingSelection, getSelectedBlocks } from "./selector.is-overlapping-selection.js";
30
+ import { getFocusTextBlock, getPreviousBlock, getNextBlock, getFocusBlockObject, getFocusSpan, isSelectionCollapsed, isOverlappingSelection, getSelectedBlocks, isSelectionExpanded } from "./selector.is-overlapping-selection.js";
31
+ import { getSelectionStartPoint, getSelectionEndPoint } from "./util.get-selection-start-point.js";
31
32
  import { Subject } from "rxjs";
32
33
  import { useEffectEvent } from "use-effect-event";
33
34
  const rootName = "sanity-pte:";
@@ -3429,13 +3430,35 @@ function getFocusBlock({
3429
3430
  return [void 0, void 0];
3430
3431
  }
3431
3432
  }
3433
+ function getSelectionStartBlock({
3434
+ editor
3435
+ }) {
3436
+ if (!editor.selection)
3437
+ return [void 0, void 0];
3438
+ const selectionStartPoint = Range.start(editor.selection);
3439
+ return getPointBlock({
3440
+ editor,
3441
+ point: selectionStartPoint
3442
+ });
3443
+ }
3444
+ function getSelectionEndBlock({
3445
+ editor
3446
+ }) {
3447
+ if (!editor.selection)
3448
+ return [void 0, void 0];
3449
+ const selectionEndPoint = Range.end(editor.selection);
3450
+ return getPointBlock({
3451
+ editor,
3452
+ point: selectionEndPoint
3453
+ });
3454
+ }
3432
3455
  function getPointBlock({
3433
3456
  editor,
3434
3457
  point
3435
3458
  }) {
3436
3459
  try {
3437
3460
  const [block] = Editor.node(editor, point.path.slice(0, 1)) ?? [void 0, void 0];
3438
- return block ? [block, point.path] : [void 0, void 0];
3461
+ return block ? [block, point.path.slice(0, 1)] : [void 0, void 0];
3439
3462
  } catch {
3440
3463
  return [void 0, void 0];
3441
3464
  }
@@ -3849,10 +3872,12 @@ function insertBlock({
3849
3872
  editor,
3850
3873
  schema
3851
3874
  }) {
3852
- const [focusBlock, focusBlockPath] = getFocusBlock({
3875
+ const [startBlock, startBlockPath] = getSelectionStartBlock({
3876
+ editor
3877
+ }), [endBlock, endBlockPath] = getSelectionEndBlock({
3853
3878
  editor
3854
3879
  });
3855
- if (!editor.selection || !focusBlock || !focusBlockPath) {
3880
+ if (!editor.selection || !startBlock || !startBlockPath || !endBlock || !endBlockPath) {
3856
3881
  select !== "none" && DOMEditor.focus(editor);
3857
3882
  const [lastBlock, lastBlockPath] = getLastBlock({
3858
3883
  editor
@@ -3891,26 +3916,41 @@ function insertBlock({
3891
3916
  }), select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
3892
3917
  }
3893
3918
  } else if (placement === "before") {
3894
- const currentSelection = editor.selection;
3919
+ const currentSelection = editor.selection, selectionStartPoint = Range.start(currentSelection);
3895
3920
  Transforms.insertNodes(editor, [block], {
3896
- at: focusBlockPath,
3921
+ at: [selectionStartPoint.path[0]],
3897
3922
  select: !1
3898
- });
3899
- const adjustedSelection = Range.transform(currentSelection, {
3900
- type: "move_node",
3901
- path: focusBlockPath,
3902
- newPath: [focusBlockPath[0] + 1]
3903
- });
3904
- adjustedSelection ? Transforms.select(editor, adjustedSelection) : Transforms.select(editor, currentSelection), select === "start" ? Transforms.select(editor, Editor.start(editor, focusBlockPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, focusBlockPath));
3923
+ }), select === "start" ? Transforms.select(editor, Editor.start(editor, [selectionStartPoint.path[0]])) : select === "end" && Transforms.select(editor, Editor.end(editor, [selectionStartPoint.path[0]]));
3905
3924
  } else if (placement === "after") {
3906
- const nextPath = [focusBlockPath[0] + 1], currentSelection = editor.selection;
3925
+ const currentSelection = editor.selection, nextPath = [Range.end(currentSelection).path[0] + 1];
3907
3926
  Transforms.insertNodes(editor, [block], {
3908
3927
  at: nextPath,
3909
3928
  select: !1
3910
- }), Transforms.select(editor, currentSelection), select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
3929
+ }), select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
3911
3930
  } else {
3912
- const currentSelection = editor.selection, focusBlockStartPoint = Editor.start(editor, focusBlockPath);
3913
- if (editor.isTextBlock(focusBlock) && editor.isTextBlock(block)) {
3931
+ const currentSelection = editor.selection, endBlockEndPoint = Editor.start(editor, endBlockPath);
3932
+ if (Range.isExpanded(currentSelection) && !editor.isTextBlock(block)) {
3933
+ Transforms.delete(editor, {
3934
+ at: currentSelection
3935
+ });
3936
+ const newSelection = editor.selection, [focusBlock, focusBlockPath] = getFocusBlock({
3937
+ editor
3938
+ });
3939
+ Transforms.insertNodes(editor, [block], {
3940
+ voids: !0
3941
+ });
3942
+ const adjustedSelection = newSelection.anchor.offset === 0 ? Range.transform(newSelection, {
3943
+ type: "insert_node",
3944
+ node: block,
3945
+ path: [newSelection.anchor.path[0]]
3946
+ }) : newSelection;
3947
+ select === "none" && adjustedSelection && Transforms.select(editor, adjustedSelection), focusBlock && isEqualToEmptyEditor([focusBlock], schema) && Transforms.removeNodes(editor, {
3948
+ at: focusBlockPath
3949
+ });
3950
+ return;
3951
+ }
3952
+ if (editor.isTextBlock(endBlock) && editor.isTextBlock(block)) {
3953
+ const selectionStartPoint = Range.start(currentSelection);
3914
3954
  if (select === "end") {
3915
3955
  Transforms.insertFragment(editor, [block], {
3916
3956
  voids: !0
@@ -3920,32 +3960,44 @@ function insertBlock({
3920
3960
  Transforms.insertFragment(editor, [block], {
3921
3961
  at: currentSelection,
3922
3962
  voids: !0
3923
- }), select === "start" ? Point.equals(currentSelection.anchor, focusBlockStartPoint) ? Transforms.select(editor, Editor.start(editor, focusBlockPath)) : Transforms.select(editor, currentSelection) : Point.equals(currentSelection.anchor, focusBlockStartPoint) || Transforms.select(editor, currentSelection);
3924
- } else if (editor.isTextBlock(focusBlock)) {
3925
- const focusBlockStartPoint2 = Editor.start(editor, focusBlockPath), focusBlockEndPoint = Editor.end(editor, focusBlockPath);
3926
- if (Point.equals(currentSelection.anchor, focusBlockStartPoint2))
3963
+ }), select === "start" ? Transforms.select(editor, selectionStartPoint) : Point.equals(selectionStartPoint, endBlockEndPoint) || Transforms.select(editor, selectionStartPoint);
3964
+ } else if (editor.isTextBlock(endBlock)) {
3965
+ const endBlockStartPoint = Editor.start(editor, endBlockPath), endBlockEndPoint2 = Editor.end(editor, endBlockPath), selectionStartPoint = Range.start(currentSelection), selectionEndPoint = Range.end(currentSelection);
3966
+ if (Range.isCollapsed(currentSelection) && Point.equals(selectionStartPoint, endBlockStartPoint))
3927
3967
  Transforms.insertNodes(editor, [block], {
3928
- at: focusBlockPath,
3968
+ at: endBlockPath,
3929
3969
  select: !1
3930
- }), (select === "start" || select === "end") && Transforms.select(editor, Editor.start(editor, focusBlockPath)), isEqualToEmptyEditor([focusBlock], schema) && Transforms.removeNodes(editor, {
3931
- at: Path.next(focusBlockPath)
3970
+ }), (select === "start" || select === "end") && Transforms.select(editor, Editor.start(editor, endBlockPath)), isEqualToEmptyEditor([endBlock], schema) && Transforms.removeNodes(editor, {
3971
+ at: Path.next(endBlockPath)
3932
3972
  });
3933
- else if (Point.equals(currentSelection.focus, focusBlockEndPoint)) {
3934
- const nextPath = [focusBlockPath[0] + 1];
3973
+ else if (Range.isCollapsed(currentSelection) && Point.equals(selectionEndPoint, endBlockEndPoint2)) {
3974
+ const nextPath = [endBlockPath[0] + 1];
3935
3975
  Transforms.insertNodes(editor, [block], {
3936
3976
  at: nextPath,
3937
3977
  select: !1
3938
3978
  }), (select === "start" || select === "end") && Transforms.select(editor, Editor.start(editor, nextPath));
3939
- } else {
3979
+ } else if (Range.isExpanded(currentSelection) && Point.equals(selectionStartPoint, endBlockStartPoint) && Point.equals(selectionEndPoint, endBlockEndPoint2))
3980
+ Transforms.insertFragment(editor, [block], {
3981
+ at: currentSelection
3982
+ }), select === "start" ? Transforms.select(editor, Editor.start(editor, endBlockPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, endBlockPath));
3983
+ else if (Range.isExpanded(currentSelection) && Point.equals(selectionStartPoint, endBlockStartPoint))
3984
+ Transforms.insertFragment(editor, [block], {
3985
+ at: currentSelection
3986
+ }), select === "start" ? Transforms.select(editor, Editor.start(editor, endBlockPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, endBlockPath));
3987
+ else if (Range.isExpanded(currentSelection) && Point.equals(selectionEndPoint, endBlockEndPoint2))
3988
+ Transforms.insertFragment(editor, [block], {
3989
+ at: currentSelection
3990
+ }), select === "start" ? Transforms.select(editor, Editor.start(editor, Path.next(endBlockPath))) : select === "end" && Transforms.select(editor, Editor.end(editor, Path.next(endBlockPath)));
3991
+ else {
3940
3992
  const currentSelection2 = editor.selection, [focusChild] = getFocusChild({
3941
3993
  editor
3942
3994
  });
3943
3995
  if (focusChild && editor.isTextSpan(focusChild))
3944
3996
  Transforms.insertFragment(editor, [block], {
3945
3997
  at: currentSelection2
3946
- }), select === "start" || select === "end" ? Transforms.select(editor, [focusBlockPath[0] + 1]) : Transforms.select(editor, currentSelection2);
3998
+ }), select === "start" || select === "end" ? Transforms.select(editor, [endBlockPath[0] + 1]) : Transforms.select(editor, currentSelection2);
3947
3999
  else {
3948
- const nextPath = [focusBlockPath[0] + 1];
4000
+ const nextPath = [endBlockPath[0] + 1];
3949
4001
  Transforms.insertNodes(editor, [block], {
3950
4002
  at: nextPath,
3951
4003
  select: !1
@@ -3956,7 +4008,7 @@ function insertBlock({
3956
4008
  Transforms.insertNodes(editor, [block], {
3957
4009
  select: !1
3958
4010
  });
3959
- const nextPath = [focusBlockPath[0] + 1];
4011
+ const nextPath = [endBlockPath[0] + 1];
3960
4012
  select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
3961
4013
  }
3962
4014
  }
@@ -5893,7 +5945,7 @@ const EditorActorContext = createContext({}), abstractAnnotationBehaviors = [def
5893
5945
  placement: index === 0 ? "auto" : "after",
5894
5946
  select: "end"
5895
5947
  })) : event.blocks.flatMap((block, index) => index === 0 ? [raise({
5896
- type: "split.block"
5948
+ type: "split"
5897
5949
  }), raise({
5898
5950
  type: "select.previous block",
5899
5951
  select: "end"
@@ -5932,7 +5984,7 @@ const EditorActorContext = createContext({}), abstractAnnotationBehaviors = [def
5932
5984
  }), defineBehavior({
5933
5985
  on: "insert.break",
5934
5986
  actions: [() => [raise({
5935
- type: "split.block"
5987
+ type: "split"
5936
5988
  })]]
5937
5989
  }), defineBehavior({
5938
5990
  on: "insert.soft break",
@@ -6113,28 +6165,7 @@ const EditorActorContext = createContext({}), abstractAnnotationBehaviors = [def
6113
6165
  at: selection
6114
6166
  })]]
6115
6167
  })], abstractSplitBehaviors = [defineBehavior({
6116
- on: "split.block",
6117
- guard: ({
6118
- snapshot
6119
- }) => {
6120
- if (!snapshot.context.selection || !isSelectionExpanded(snapshot))
6121
- return !1;
6122
- const firstBlock = getFirstBlock$1(snapshot), lastBlock = getLastBlock$1(snapshot);
6123
- if (!firstBlock || !lastBlock)
6124
- return !1;
6125
- const firstBlockStartPoint = getBlockStartPoint(firstBlock), selectionStartPoint = getSelectionStartPoint(snapshot.context.selection), lastBlockEndPoint = getBlockEndPoint(lastBlock), selectionEndPoint = getSelectionEndPoint(snapshot.context.selection);
6126
- return isEqualSelectionPoints(firstBlockStartPoint, selectionStartPoint) && isEqualSelectionPoints(lastBlockEndPoint, selectionEndPoint) ? {
6127
- selection: snapshot.context.selection
6128
- } : !1;
6129
- },
6130
- actions: [(_, {
6131
- selection
6132
- }) => [raise({
6133
- type: "delete",
6134
- at: selection
6135
- })]]
6136
- }), defineBehavior({
6137
- on: "split.block",
6168
+ on: "split",
6138
6169
  guard: ({
6139
6170
  snapshot
6140
6171
  }) => {
@@ -6629,7 +6660,7 @@ const EditorActorContext = createContext({}), abstractAnnotationBehaviors = [def
6629
6660
  raiseDeserializationSuccessOrFailure,
6630
6661
  raiseSerializationSuccessOrFailure,
6631
6662
  raiseInsertSoftBreak
6632
- ], abstractBehaviorEventTypes = ["annotation.toggle", "decorator.toggle", "delete.text", "deserialize", "deserialization.success", "deserialization.failure", "insert.blocks", "insert.break", "insert.soft break", "list item.add", "list item.remove", "list item.toggle", "move.block down", "move.block up", "select.previous block", "select.next block", "serialize", "serialization.success", "serialization.failure", "split.block", "style.add", "style.remove", "style.toggle"];
6663
+ ], abstractBehaviorEventTypes = ["annotation.toggle", "decorator.toggle", "delete.text", "deserialize", "deserialization.success", "deserialization.failure", "insert.blocks", "insert.break", "insert.soft break", "list item.add", "list item.remove", "list item.toggle", "move.block down", "move.block up", "select.previous block", "select.next block", "serialize", "serialization.success", "serialization.failure", "split", "style.add", "style.remove", "style.toggle"];
6633
6664
  function isAbstractBehaviorEvent(event) {
6634
6665
  return abstractBehaviorEventTypes.includes(event.type);
6635
6666
  }
@@ -6711,10 +6742,10 @@ function performEvent({
6711
6742
  continue;
6712
6743
  }
6713
6744
  if (action.type === "execute") {
6714
- if (isAbstractBehaviorEvent(action.event) || isCustomBehaviorEvent(action.event))
6745
+ if (isAbstractBehaviorEvent(action.event))
6715
6746
  performEvent({
6716
6747
  mode: "execute",
6717
- behaviors: isCustomBehaviorEvent(action.event) ? [...behaviors, ...defaultBehaviors] : defaultBehaviors,
6748
+ behaviors: defaultBehaviors,
6718
6749
  event: action.event,
6719
6750
  editor,
6720
6751
  keyGenerator,