@portabletext/editor 1.44.0 → 1.44.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.
@@ -23,7 +23,7 @@ import isUndefined from "lodash/isUndefined.js";
23
23
  import omitBy from "lodash/omitBy.js";
24
24
  import omit from "lodash/omit.js";
25
25
  import { selectionPointToBlockOffset, blockOffsetsToSelection } from "./util.selection-point-to-block-offset.js";
26
- import { getTrimmedSelection, getSelectedTextBlocks, isActiveListItem, isActiveStyle, isActiveAnnotation, isActiveDecorator, isSelectingEntireBlocks, getActiveAnnotations } from "./selector.is-selecting-entire-blocks.js";
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";
@@ -3005,112 +3005,6 @@ function isPortableTextBlock(node) {
3005
3005
  node.children.every((child) => typeof child == "object" && "_type" in child)
3006
3006
  );
3007
3007
  }
3008
- const decoratorAddActionImplementation = ({
3009
- context,
3010
- action
3011
- }) => {
3012
- const editor = action.editor, mark = action.decorator, value = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), manualAnchor = action.offsets?.anchor ? blockOffsetToSpanSelectionPoint({
3013
- value,
3014
- blockOffset: action.offsets.anchor,
3015
- direction: "backward"
3016
- }) : void 0, manualFocus = action.offsets?.focus ? blockOffsetToSpanSelectionPoint({
3017
- value,
3018
- blockOffset: action.offsets.focus,
3019
- direction: "forward"
3020
- }) : void 0, manualSelection = manualAnchor && manualFocus ? {
3021
- anchor: manualAnchor,
3022
- focus: manualFocus
3023
- } : void 0, selection = manualSelection ? toSlateRange(manualSelection, action.editor) ?? editor.selection : editor.selection;
3024
- if (!selection)
3025
- return;
3026
- const editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? selectionPointToBlockOffset({
3027
- value,
3028
- selectionPoint: editorSelection.anchor
3029
- }) : void 0, focusOffset = editorSelection ? selectionPointToBlockOffset({
3030
- value,
3031
- selectionPoint: editorSelection.focus
3032
- }) : void 0;
3033
- if (!anchorOffset || !focusOffset)
3034
- throw new Error("Unable to find anchor or focus offset");
3035
- if (Range.isExpanded(selection)) {
3036
- Transforms.setNodes(editor, {}, {
3037
- at: selection,
3038
- match: Text.isText,
3039
- split: !0,
3040
- hanging: !0
3041
- });
3042
- const newValue = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), newSelection = blockOffsetsToSelection({
3043
- value: newValue,
3044
- offsets: {
3045
- anchor: anchorOffset,
3046
- focus: focusOffset
3047
- },
3048
- backward: editorSelection?.backward
3049
- }), trimmedSelection = getTrimmedSelection({
3050
- context: {
3051
- activeDecorators: [],
3052
- converters: [],
3053
- keyGenerator: context.keyGenerator,
3054
- readOnly: !1,
3055
- schema: context.schema,
3056
- selection: newSelection,
3057
- value: newValue
3058
- }
3059
- });
3060
- if (!trimmedSelection)
3061
- throw new Error("Unable to find trimmed selection");
3062
- const newRange = toSlateRange(trimmedSelection, editor);
3063
- if (!newRange)
3064
- throw new Error("Unable to find new selection");
3065
- const splitTextNodes = Range.isRange(newRange) ? [...Editor.nodes(editor, {
3066
- at: newRange,
3067
- match: (node) => Text.isText(node)
3068
- })] : [];
3069
- for (const [node, path] of splitTextNodes) {
3070
- const marks = [...(Array.isArray(node.marks) ? node.marks : []).filter((eMark) => eMark !== mark), mark];
3071
- Transforms.setNodes(editor, {
3072
- marks
3073
- }, {
3074
- at: path,
3075
- match: Text.isText,
3076
- split: !0,
3077
- hanging: !0
3078
- });
3079
- }
3080
- } else {
3081
- if (!Array.from(Editor.nodes(editor, {
3082
- at: selection,
3083
- match: (node) => editor.isTextSpan(node)
3084
- }))?.at(0))
3085
- return;
3086
- const [block, blockPath] = Editor.node(editor, selection, {
3087
- depth: 1
3088
- }), lonelyEmptySpan = editor.isTextBlock(block) && block.children.length === 1 && editor.isTextSpan(block.children[0]) && block.children[0].text === "" ? block.children[0] : void 0;
3089
- if (lonelyEmptySpan) {
3090
- const existingMarks = lonelyEmptySpan.marks ?? [], existingMarksWithoutDecorator = existingMarks.filter((existingMark) => existingMark !== mark);
3091
- Transforms.setNodes(editor, {
3092
- marks: existingMarks.length === existingMarksWithoutDecorator.length ? [...existingMarks, mark] : existingMarksWithoutDecorator
3093
- }, {
3094
- at: blockPath,
3095
- match: (node) => editor.isTextSpan(node)
3096
- });
3097
- } else {
3098
- const existingMarks = {
3099
- ...Editor.marks(editor) || {}
3100
- }.marks || [], marks = {
3101
- ...Editor.marks(editor) || {},
3102
- marks: [...existingMarks, mark]
3103
- };
3104
- editor.marks = marks;
3105
- }
3106
- }
3107
- if (editor.selection) {
3108
- const selection2 = editor.selection;
3109
- editor.selection = {
3110
- ...selection2
3111
- };
3112
- }
3113
- };
3114
3008
  function getPreviousSpan({
3115
3009
  editor,
3116
3010
  blockPath,
@@ -3568,25 +3462,7 @@ function isDecoratorActive({
3568
3462
  ...Editor.marks(editor) || {}
3569
3463
  }.marks || []).includes(decorator);
3570
3464
  }
3571
- const toggleDecoratorActionImplementation = ({
3572
- context,
3573
- action
3574
- }) => {
3575
- isDecoratorActive({
3576
- editor: action.editor,
3577
- decorator: action.decorator
3578
- }) ? removeDecoratorActionImplementation({
3579
- action: {
3580
- editor: action.editor,
3581
- decorator: action.decorator
3582
- }
3583
- }) : decoratorAddActionImplementation({
3584
- context,
3585
- action: {
3586
- ...action
3587
- }
3588
- });
3589
- }, debug$a = debugWithName("API:editable");
3465
+ const debug$a = debugWithName("API:editable");
3590
3466
  function createEditableAPI(editor, editorActor) {
3591
3467
  const types = editorActor.getSnapshot().context.schema;
3592
3468
  return {
@@ -4057,30 +3933,6 @@ const addAnnotationActionImplementation = ({
4057
3933
  }
4058
3934
  }
4059
3935
  }
4060
- }, toggleAnnotationActionImplementation = ({
4061
- context,
4062
- action
4063
- }) => {
4064
- if (isAnnotationActive({
4065
- editor: action.editor,
4066
- annotation: {
4067
- name: action.annotation.name
4068
- }
4069
- }))
4070
- removeAnnotationActionImplementation({
4071
- action: {
4072
- annotation: action.annotation,
4073
- editor: action.editor
4074
- }
4075
- });
4076
- else
4077
- return addAnnotationActionImplementation({
4078
- context,
4079
- action: {
4080
- annotation: action.annotation,
4081
- editor: action.editor
4082
- }
4083
- });
4084
3936
  }, blockSetBehaviorActionImplementation = ({
4085
3937
  context,
4086
3938
  action
@@ -4195,6 +4047,111 @@ const addAnnotationActionImplementation = ({
4195
4047
  action
4196
4048
  }) => {
4197
4049
  ReactEditor.blur(action.editor);
4050
+ }, decoratorAddActionImplementation = ({
4051
+ context,
4052
+ action
4053
+ }) => {
4054
+ const editor = action.editor, mark = action.decorator, value = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), manualAnchor = action.offsets?.anchor ? blockOffsetToSpanSelectionPoint({
4055
+ value,
4056
+ blockOffset: action.offsets.anchor,
4057
+ direction: "backward"
4058
+ }) : void 0, manualFocus = action.offsets?.focus ? blockOffsetToSpanSelectionPoint({
4059
+ value,
4060
+ blockOffset: action.offsets.focus,
4061
+ direction: "forward"
4062
+ }) : void 0, manualSelection = manualAnchor && manualFocus ? {
4063
+ anchor: manualAnchor,
4064
+ focus: manualFocus
4065
+ } : void 0, selection = manualSelection ? toSlateRange(manualSelection, action.editor) ?? editor.selection : editor.selection;
4066
+ if (!selection)
4067
+ return;
4068
+ const editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? selectionPointToBlockOffset({
4069
+ value,
4070
+ selectionPoint: editorSelection.anchor
4071
+ }) : void 0, focusOffset = editorSelection ? selectionPointToBlockOffset({
4072
+ value,
4073
+ selectionPoint: editorSelection.focus
4074
+ }) : void 0;
4075
+ if (!anchorOffset || !focusOffset)
4076
+ throw new Error("Unable to find anchor or focus offset");
4077
+ if (Range.isExpanded(selection)) {
4078
+ Transforms.setNodes(editor, {}, {
4079
+ at: selection,
4080
+ match: Text.isText,
4081
+ split: !0,
4082
+ hanging: !0
4083
+ });
4084
+ const newValue = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), newSelection = blockOffsetsToSelection({
4085
+ value: newValue,
4086
+ offsets: {
4087
+ anchor: anchorOffset,
4088
+ focus: focusOffset
4089
+ },
4090
+ backward: editorSelection?.backward
4091
+ }), trimmedSelection = getTrimmedSelection({
4092
+ context: {
4093
+ activeDecorators: [],
4094
+ converters: [],
4095
+ keyGenerator: context.keyGenerator,
4096
+ readOnly: !1,
4097
+ schema: context.schema,
4098
+ selection: newSelection,
4099
+ value: newValue
4100
+ }
4101
+ });
4102
+ if (!trimmedSelection)
4103
+ throw new Error("Unable to find trimmed selection");
4104
+ const newRange = toSlateRange(trimmedSelection, editor);
4105
+ if (!newRange)
4106
+ throw new Error("Unable to find new selection");
4107
+ const splitTextNodes = Range.isRange(newRange) ? [...Editor.nodes(editor, {
4108
+ at: newRange,
4109
+ match: (node) => Text.isText(node)
4110
+ })] : [];
4111
+ for (const [node, path] of splitTextNodes) {
4112
+ const marks = [...(Array.isArray(node.marks) ? node.marks : []).filter((eMark) => eMark !== mark), mark];
4113
+ Transforms.setNodes(editor, {
4114
+ marks
4115
+ }, {
4116
+ at: path,
4117
+ match: Text.isText,
4118
+ split: !0,
4119
+ hanging: !0
4120
+ });
4121
+ }
4122
+ } else {
4123
+ if (!Array.from(Editor.nodes(editor, {
4124
+ at: selection,
4125
+ match: (node) => editor.isTextSpan(node)
4126
+ }))?.at(0))
4127
+ return;
4128
+ const [block, blockPath] = Editor.node(editor, selection, {
4129
+ depth: 1
4130
+ }), lonelyEmptySpan = editor.isTextBlock(block) && block.children.length === 1 && editor.isTextSpan(block.children[0]) && block.children[0].text === "" ? block.children[0] : void 0;
4131
+ if (lonelyEmptySpan) {
4132
+ const existingMarks = lonelyEmptySpan.marks ?? [], existingMarksWithoutDecorator = existingMarks.filter((existingMark) => existingMark !== mark);
4133
+ Transforms.setNodes(editor, {
4134
+ marks: existingMarks.length === existingMarksWithoutDecorator.length ? [...existingMarks, mark] : existingMarksWithoutDecorator
4135
+ }, {
4136
+ at: blockPath,
4137
+ match: (node) => editor.isTextSpan(node)
4138
+ });
4139
+ } else {
4140
+ const existingMarks = {
4141
+ ...Editor.marks(editor) || {}
4142
+ }.marks || [], marks = {
4143
+ ...Editor.marks(editor) || {},
4144
+ marks: [...existingMarks, mark]
4145
+ };
4146
+ editor.marks = marks;
4147
+ }
4148
+ }
4149
+ if (editor.selection) {
4150
+ const selection2 = editor.selection;
4151
+ editor.selection = {
4152
+ ...selection2
4153
+ };
4154
+ }
4198
4155
  }, deleteActionImplementation = ({
4199
4156
  action
4200
4157
  }) => {
@@ -4561,13 +4518,11 @@ const insertTextActionImplementation = ({
4561
4518
  }, debug$9 = debugWithName("behaviors:action"), behaviorActionImplementations = {
4562
4519
  "annotation.add": addAnnotationActionImplementation,
4563
4520
  "annotation.remove": removeAnnotationActionImplementation,
4564
- "annotation.toggle": toggleAnnotationActionImplementation,
4565
4521
  "block.set": blockSetBehaviorActionImplementation,
4566
4522
  "block.unset": blockUnsetBehaviorActionImplementation,
4567
4523
  blur: blurActionImplementation,
4568
4524
  "decorator.add": decoratorAddActionImplementation,
4569
4525
  "decorator.remove": removeDecoratorActionImplementation,
4570
- "decorator.toggle": toggleDecoratorActionImplementation,
4571
4526
  focus: focusActionImplementation,
4572
4527
  delete: deleteActionImplementation,
4573
4528
  "delete.backward": deleteBackwardActionImplementation,
@@ -4608,13 +4563,6 @@ function performAction({
4608
4563
  });
4609
4564
  break;
4610
4565
  }
4611
- case "annotation.toggle": {
4612
- behaviorActionImplementations["annotation.toggle"]({
4613
- context,
4614
- action
4615
- });
4616
- break;
4617
- }
4618
4566
  case "block.set": {
4619
4567
  behaviorActionImplementations["block.set"]({
4620
4568
  context,
@@ -4650,13 +4598,6 @@ function performAction({
4650
4598
  });
4651
4599
  break;
4652
4600
  }
4653
- case "decorator.toggle": {
4654
- behaviorActionImplementations["decorator.toggle"]({
4655
- context,
4656
- action
4657
- });
4658
- break;
4659
- }
4660
4601
  case "delete": {
4661
4602
  behaviorActionImplementations.delete({
4662
4603
  context,
@@ -5695,7 +5636,67 @@ const keyIs = {
5695
5636
  actions: [() => [raise({
5696
5637
  type: "insert.soft break"
5697
5638
  })]]
5698
- }), internalInsertBehaviors = [defineBehavior({
5639
+ }), internalAnnotationBehaviors = [defineBehavior({
5640
+ on: "annotation.toggle",
5641
+ guard: ({
5642
+ snapshot,
5643
+ event
5644
+ }) => isActiveAnnotation(event.annotation.name)(snapshot),
5645
+ actions: [({
5646
+ event
5647
+ }) => [raise({
5648
+ type: "annotation.remove",
5649
+ annotation: event.annotation
5650
+ })]]
5651
+ }), defineBehavior({
5652
+ on: "annotation.toggle",
5653
+ guard: ({
5654
+ snapshot,
5655
+ event
5656
+ }) => !isActiveAnnotation(event.annotation.name)(snapshot),
5657
+ actions: [({
5658
+ event
5659
+ }) => [raise({
5660
+ type: "annotation.add",
5661
+ annotation: event.annotation
5662
+ })]]
5663
+ })], internalDecoratorBehaviors = [defineBehavior({
5664
+ on: "decorator.toggle",
5665
+ guard: ({
5666
+ snapshot,
5667
+ event
5668
+ }) => isActiveDecorator(event.decorator)(snapshot),
5669
+ actions: [({
5670
+ event
5671
+ }) => [raise({
5672
+ type: "decorator.remove",
5673
+ decorator: event.decorator
5674
+ })]]
5675
+ }), defineBehavior({
5676
+ on: "decorator.toggle",
5677
+ guard: ({
5678
+ snapshot,
5679
+ event
5680
+ }) => {
5681
+ const manualSelection = event.offsets ? blockOffsetsToSelection({
5682
+ value: snapshot.context.value,
5683
+ offsets: event.offsets
5684
+ }) : null;
5685
+ return manualSelection ? !isActiveDecorator(event.decorator)({
5686
+ ...snapshot,
5687
+ context: {
5688
+ ...snapshot.context,
5689
+ selection: manualSelection
5690
+ }
5691
+ }) : !isActiveDecorator(event.decorator)(snapshot);
5692
+ },
5693
+ actions: [({
5694
+ event
5695
+ }) => [raise({
5696
+ ...event,
5697
+ type: "decorator.add"
5698
+ })]]
5699
+ })], internalInsertBehaviors = [defineBehavior({
5699
5700
  on: "insert.blocks",
5700
5701
  guard: ({
5701
5702
  event
@@ -5930,67 +5931,7 @@ const keyIs = {
5930
5931
  type: "style.add",
5931
5932
  style: event.style
5932
5933
  })]]
5933
- })], toggleAnnotationOff = defineBehavior({
5934
- on: "annotation.toggle",
5935
- guard: ({
5936
- snapshot,
5937
- event
5938
- }) => isActiveAnnotation(event.annotation.name)(snapshot),
5939
- actions: [({
5940
- event
5941
- }) => [raise({
5942
- type: "annotation.remove",
5943
- annotation: event.annotation
5944
- })]]
5945
- }), toggleAnnotationOn = defineBehavior({
5946
- on: "annotation.toggle",
5947
- guard: ({
5948
- snapshot,
5949
- event
5950
- }) => !isActiveAnnotation(event.annotation.name)(snapshot),
5951
- actions: [({
5952
- event
5953
- }) => [raise({
5954
- type: "annotation.add",
5955
- annotation: event.annotation
5956
- })]]
5957
- }), toggleDecoratorOff = defineBehavior({
5958
- on: "decorator.toggle",
5959
- guard: ({
5960
- snapshot,
5961
- event
5962
- }) => isActiveDecorator(event.decorator)(snapshot),
5963
- actions: [({
5964
- event
5965
- }) => [raise({
5966
- type: "decorator.remove",
5967
- decorator: event.decorator
5968
- })]]
5969
- }), toggleDecoratorOn = defineBehavior({
5970
- on: "decorator.toggle",
5971
- guard: ({
5972
- snapshot,
5973
- event
5974
- }) => {
5975
- const manualSelection = event.offsets ? blockOffsetsToSelection({
5976
- value: snapshot.context.value,
5977
- offsets: event.offsets
5978
- }) : null;
5979
- return manualSelection ? !isActiveDecorator(event.decorator)({
5980
- ...snapshot,
5981
- context: {
5982
- ...snapshot.context,
5983
- selection: manualSelection
5984
- }
5985
- }) : !isActiveDecorator(event.decorator)(snapshot);
5986
- },
5987
- actions: [({
5988
- event
5989
- }) => [raise({
5990
- ...event,
5991
- type: "decorator.add"
5992
- })]]
5993
- }), raiseDeserializationSuccessOrFailure = defineBehavior({
5934
+ })], raiseDeserializationSuccessOrFailure = defineBehavior({
5994
5935
  on: "deserialize",
5995
5936
  guard: ({
5996
5937
  snapshot,
@@ -6318,14 +6259,12 @@ const keyIs = {
6318
6259
  originEvent: event
6319
6260
  })]]
6320
6261
  }),
6262
+ ...internalAnnotationBehaviors,
6263
+ ...internalDecoratorBehaviors,
6321
6264
  ...internalInsertBehaviors,
6322
6265
  ...internalListItemBehaviors,
6323
6266
  ...internalStyleBehaviors,
6324
6267
  ...internalSelectBehaviors,
6325
- toggleAnnotationOff,
6326
- toggleAnnotationOn,
6327
- toggleDecoratorOff,
6328
- toggleDecoratorOn,
6329
6268
  raiseDeserializationSuccessOrFailure,
6330
6269
  raiseSerializationSuccessOrFailure,
6331
6270
  raiseInsertSoftBreak