@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.
@@ -2979,112 +2979,6 @@ function isPortableTextBlock(node) {
2979
2979
  node.children.every((child) => typeof child == "object" && "_type" in child)
2980
2980
  );
2981
2981
  }
2982
- const decoratorAddActionImplementation = ({
2983
- context,
2984
- action
2985
- }) => {
2986
- 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 ? util_sliceBlocks.blockOffsetToSpanSelectionPoint({
2987
- value,
2988
- blockOffset: action.offsets.anchor,
2989
- direction: "backward"
2990
- }) : void 0, manualFocus = action.offsets?.focus ? util_sliceBlocks.blockOffsetToSpanSelectionPoint({
2991
- value,
2992
- blockOffset: action.offsets.focus,
2993
- direction: "forward"
2994
- }) : void 0, manualSelection = manualAnchor && manualFocus ? {
2995
- anchor: manualAnchor,
2996
- focus: manualFocus
2997
- } : void 0, selection = manualSelection ? toSlateRange(manualSelection, action.editor) ?? editor.selection : editor.selection;
2998
- if (!selection)
2999
- return;
3000
- const editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
3001
- value,
3002
- selectionPoint: editorSelection.anchor
3003
- }) : void 0, focusOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
3004
- value,
3005
- selectionPoint: editorSelection.focus
3006
- }) : void 0;
3007
- if (!anchorOffset || !focusOffset)
3008
- throw new Error("Unable to find anchor or focus offset");
3009
- if (slate.Range.isExpanded(selection)) {
3010
- slate.Transforms.setNodes(editor, {}, {
3011
- at: selection,
3012
- match: slate.Text.isText,
3013
- split: !0,
3014
- hanging: !0
3015
- });
3016
- const newValue = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), newSelection = util_selectionPointToBlockOffset.blockOffsetsToSelection({
3017
- value: newValue,
3018
- offsets: {
3019
- anchor: anchorOffset,
3020
- focus: focusOffset
3021
- },
3022
- backward: editorSelection?.backward
3023
- }), trimmedSelection = selector_isSelectingEntireBlocks.getTrimmedSelection({
3024
- context: {
3025
- activeDecorators: [],
3026
- converters: [],
3027
- keyGenerator: context.keyGenerator,
3028
- readOnly: !1,
3029
- schema: context.schema,
3030
- selection: newSelection,
3031
- value: newValue
3032
- }
3033
- });
3034
- if (!trimmedSelection)
3035
- throw new Error("Unable to find trimmed selection");
3036
- const newRange = toSlateRange(trimmedSelection, editor);
3037
- if (!newRange)
3038
- throw new Error("Unable to find new selection");
3039
- const splitTextNodes = slate.Range.isRange(newRange) ? [...slate.Editor.nodes(editor, {
3040
- at: newRange,
3041
- match: (node) => slate.Text.isText(node)
3042
- })] : [];
3043
- for (const [node, path] of splitTextNodes) {
3044
- const marks = [...(Array.isArray(node.marks) ? node.marks : []).filter((eMark) => eMark !== mark), mark];
3045
- slate.Transforms.setNodes(editor, {
3046
- marks
3047
- }, {
3048
- at: path,
3049
- match: slate.Text.isText,
3050
- split: !0,
3051
- hanging: !0
3052
- });
3053
- }
3054
- } else {
3055
- if (!Array.from(slate.Editor.nodes(editor, {
3056
- at: selection,
3057
- match: (node) => editor.isTextSpan(node)
3058
- }))?.at(0))
3059
- return;
3060
- const [block, blockPath] = slate.Editor.node(editor, selection, {
3061
- depth: 1
3062
- }), lonelyEmptySpan = editor.isTextBlock(block) && block.children.length === 1 && editor.isTextSpan(block.children[0]) && block.children[0].text === "" ? block.children[0] : void 0;
3063
- if (lonelyEmptySpan) {
3064
- const existingMarks = lonelyEmptySpan.marks ?? [], existingMarksWithoutDecorator = existingMarks.filter((existingMark) => existingMark !== mark);
3065
- slate.Transforms.setNodes(editor, {
3066
- marks: existingMarks.length === existingMarksWithoutDecorator.length ? [...existingMarks, mark] : existingMarksWithoutDecorator
3067
- }, {
3068
- at: blockPath,
3069
- match: (node) => editor.isTextSpan(node)
3070
- });
3071
- } else {
3072
- const existingMarks = {
3073
- ...slate.Editor.marks(editor) || {}
3074
- }.marks || [], marks = {
3075
- ...slate.Editor.marks(editor) || {},
3076
- marks: [...existingMarks, mark]
3077
- };
3078
- editor.marks = marks;
3079
- }
3080
- }
3081
- if (editor.selection) {
3082
- const selection2 = editor.selection;
3083
- editor.selection = {
3084
- ...selection2
3085
- };
3086
- }
3087
- };
3088
2982
  function getPreviousSpan({
3089
2983
  editor,
3090
2984
  blockPath,
@@ -3542,25 +3436,7 @@ function isDecoratorActive({
3542
3436
  ...slate.Editor.marks(editor) || {}
3543
3437
  }.marks || []).includes(decorator);
3544
3438
  }
3545
- const toggleDecoratorActionImplementation = ({
3546
- context,
3547
- action
3548
- }) => {
3549
- isDecoratorActive({
3550
- editor: action.editor,
3551
- decorator: action.decorator
3552
- }) ? removeDecoratorActionImplementation({
3553
- action: {
3554
- editor: action.editor,
3555
- decorator: action.decorator
3556
- }
3557
- }) : decoratorAddActionImplementation({
3558
- context,
3559
- action: {
3560
- ...action
3561
- }
3562
- });
3563
- }, debug$a = debugWithName("API:editable");
3439
+ const debug$a = debugWithName("API:editable");
3564
3440
  function createEditableAPI(editor, editorActor) {
3565
3441
  const types2 = editorActor.getSnapshot().context.schema;
3566
3442
  return {
@@ -4031,30 +3907,6 @@ const addAnnotationActionImplementation = ({
4031
3907
  }
4032
3908
  }
4033
3909
  }
4034
- }, toggleAnnotationActionImplementation = ({
4035
- context,
4036
- action
4037
- }) => {
4038
- if (isAnnotationActive({
4039
- editor: action.editor,
4040
- annotation: {
4041
- name: action.annotation.name
4042
- }
4043
- }))
4044
- removeAnnotationActionImplementation({
4045
- action: {
4046
- annotation: action.annotation,
4047
- editor: action.editor
4048
- }
4049
- });
4050
- else
4051
- return addAnnotationActionImplementation({
4052
- context,
4053
- action: {
4054
- annotation: action.annotation,
4055
- editor: action.editor
4056
- }
4057
- });
4058
3910
  }, blockSetBehaviorActionImplementation = ({
4059
3911
  context,
4060
3912
  action
@@ -4169,6 +4021,111 @@ const addAnnotationActionImplementation = ({
4169
4021
  action
4170
4022
  }) => {
4171
4023
  slateReact.ReactEditor.blur(action.editor);
4024
+ }, decoratorAddActionImplementation = ({
4025
+ context,
4026
+ action
4027
+ }) => {
4028
+ 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 ? util_sliceBlocks.blockOffsetToSpanSelectionPoint({
4029
+ value,
4030
+ blockOffset: action.offsets.anchor,
4031
+ direction: "backward"
4032
+ }) : void 0, manualFocus = action.offsets?.focus ? util_sliceBlocks.blockOffsetToSpanSelectionPoint({
4033
+ value,
4034
+ blockOffset: action.offsets.focus,
4035
+ direction: "forward"
4036
+ }) : void 0, manualSelection = manualAnchor && manualFocus ? {
4037
+ anchor: manualAnchor,
4038
+ focus: manualFocus
4039
+ } : void 0, selection = manualSelection ? toSlateRange(manualSelection, action.editor) ?? editor.selection : editor.selection;
4040
+ if (!selection)
4041
+ return;
4042
+ const editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
4043
+ value,
4044
+ selectionPoint: editorSelection.anchor
4045
+ }) : void 0, focusOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
4046
+ value,
4047
+ selectionPoint: editorSelection.focus
4048
+ }) : void 0;
4049
+ if (!anchorOffset || !focusOffset)
4050
+ throw new Error("Unable to find anchor or focus offset");
4051
+ if (slate.Range.isExpanded(selection)) {
4052
+ slate.Transforms.setNodes(editor, {}, {
4053
+ at: selection,
4054
+ match: slate.Text.isText,
4055
+ split: !0,
4056
+ hanging: !0
4057
+ });
4058
+ const newValue = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), newSelection = util_selectionPointToBlockOffset.blockOffsetsToSelection({
4059
+ value: newValue,
4060
+ offsets: {
4061
+ anchor: anchorOffset,
4062
+ focus: focusOffset
4063
+ },
4064
+ backward: editorSelection?.backward
4065
+ }), trimmedSelection = selector_isSelectingEntireBlocks.getTrimmedSelection({
4066
+ context: {
4067
+ activeDecorators: [],
4068
+ converters: [],
4069
+ keyGenerator: context.keyGenerator,
4070
+ readOnly: !1,
4071
+ schema: context.schema,
4072
+ selection: newSelection,
4073
+ value: newValue
4074
+ }
4075
+ });
4076
+ if (!trimmedSelection)
4077
+ throw new Error("Unable to find trimmed selection");
4078
+ const newRange = toSlateRange(trimmedSelection, editor);
4079
+ if (!newRange)
4080
+ throw new Error("Unable to find new selection");
4081
+ const splitTextNodes = slate.Range.isRange(newRange) ? [...slate.Editor.nodes(editor, {
4082
+ at: newRange,
4083
+ match: (node) => slate.Text.isText(node)
4084
+ })] : [];
4085
+ for (const [node, path] of splitTextNodes) {
4086
+ const marks = [...(Array.isArray(node.marks) ? node.marks : []).filter((eMark) => eMark !== mark), mark];
4087
+ slate.Transforms.setNodes(editor, {
4088
+ marks
4089
+ }, {
4090
+ at: path,
4091
+ match: slate.Text.isText,
4092
+ split: !0,
4093
+ hanging: !0
4094
+ });
4095
+ }
4096
+ } else {
4097
+ if (!Array.from(slate.Editor.nodes(editor, {
4098
+ at: selection,
4099
+ match: (node) => editor.isTextSpan(node)
4100
+ }))?.at(0))
4101
+ return;
4102
+ const [block, blockPath] = slate.Editor.node(editor, selection, {
4103
+ depth: 1
4104
+ }), lonelyEmptySpan = editor.isTextBlock(block) && block.children.length === 1 && editor.isTextSpan(block.children[0]) && block.children[0].text === "" ? block.children[0] : void 0;
4105
+ if (lonelyEmptySpan) {
4106
+ const existingMarks = lonelyEmptySpan.marks ?? [], existingMarksWithoutDecorator = existingMarks.filter((existingMark) => existingMark !== mark);
4107
+ slate.Transforms.setNodes(editor, {
4108
+ marks: existingMarks.length === existingMarksWithoutDecorator.length ? [...existingMarks, mark] : existingMarksWithoutDecorator
4109
+ }, {
4110
+ at: blockPath,
4111
+ match: (node) => editor.isTextSpan(node)
4112
+ });
4113
+ } else {
4114
+ const existingMarks = {
4115
+ ...slate.Editor.marks(editor) || {}
4116
+ }.marks || [], marks = {
4117
+ ...slate.Editor.marks(editor) || {},
4118
+ marks: [...existingMarks, mark]
4119
+ };
4120
+ editor.marks = marks;
4121
+ }
4122
+ }
4123
+ if (editor.selection) {
4124
+ const selection2 = editor.selection;
4125
+ editor.selection = {
4126
+ ...selection2
4127
+ };
4128
+ }
4172
4129
  }, deleteActionImplementation = ({
4173
4130
  action
4174
4131
  }) => {
@@ -4535,13 +4492,11 @@ const insertTextActionImplementation = ({
4535
4492
  }, debug$9 = debugWithName("behaviors:action"), behaviorActionImplementations = {
4536
4493
  "annotation.add": addAnnotationActionImplementation,
4537
4494
  "annotation.remove": removeAnnotationActionImplementation,
4538
- "annotation.toggle": toggleAnnotationActionImplementation,
4539
4495
  "block.set": blockSetBehaviorActionImplementation,
4540
4496
  "block.unset": blockUnsetBehaviorActionImplementation,
4541
4497
  blur: blurActionImplementation,
4542
4498
  "decorator.add": decoratorAddActionImplementation,
4543
4499
  "decorator.remove": removeDecoratorActionImplementation,
4544
- "decorator.toggle": toggleDecoratorActionImplementation,
4545
4500
  focus: focusActionImplementation,
4546
4501
  delete: deleteActionImplementation,
4547
4502
  "delete.backward": deleteBackwardActionImplementation,
@@ -4582,13 +4537,6 @@ function performAction({
4582
4537
  });
4583
4538
  break;
4584
4539
  }
4585
- case "annotation.toggle": {
4586
- behaviorActionImplementations["annotation.toggle"]({
4587
- context,
4588
- action
4589
- });
4590
- break;
4591
- }
4592
4540
  case "block.set": {
4593
4541
  behaviorActionImplementations["block.set"]({
4594
4542
  context,
@@ -4624,13 +4572,6 @@ function performAction({
4624
4572
  });
4625
4573
  break;
4626
4574
  }
4627
- case "decorator.toggle": {
4628
- behaviorActionImplementations["decorator.toggle"]({
4629
- context,
4630
- action
4631
- });
4632
- break;
4633
- }
4634
4575
  case "delete": {
4635
4576
  behaviorActionImplementations.delete({
4636
4577
  context,
@@ -5669,7 +5610,67 @@ const keyIs = {
5669
5610
  actions: [() => [behavior_core.raise({
5670
5611
  type: "insert.soft break"
5671
5612
  })]]
5672
- }), internalInsertBehaviors = [behavior_core.defineBehavior({
5613
+ }), internalAnnotationBehaviors = [behavior_core.defineBehavior({
5614
+ on: "annotation.toggle",
5615
+ guard: ({
5616
+ snapshot,
5617
+ event
5618
+ }) => selector_isSelectingEntireBlocks.isActiveAnnotation(event.annotation.name)(snapshot),
5619
+ actions: [({
5620
+ event
5621
+ }) => [behavior_core.raise({
5622
+ type: "annotation.remove",
5623
+ annotation: event.annotation
5624
+ })]]
5625
+ }), behavior_core.defineBehavior({
5626
+ on: "annotation.toggle",
5627
+ guard: ({
5628
+ snapshot,
5629
+ event
5630
+ }) => !selector_isSelectingEntireBlocks.isActiveAnnotation(event.annotation.name)(snapshot),
5631
+ actions: [({
5632
+ event
5633
+ }) => [behavior_core.raise({
5634
+ type: "annotation.add",
5635
+ annotation: event.annotation
5636
+ })]]
5637
+ })], internalDecoratorBehaviors = [behavior_core.defineBehavior({
5638
+ on: "decorator.toggle",
5639
+ guard: ({
5640
+ snapshot,
5641
+ event
5642
+ }) => selector_isSelectingEntireBlocks.isActiveDecorator(event.decorator)(snapshot),
5643
+ actions: [({
5644
+ event
5645
+ }) => [behavior_core.raise({
5646
+ type: "decorator.remove",
5647
+ decorator: event.decorator
5648
+ })]]
5649
+ }), behavior_core.defineBehavior({
5650
+ on: "decorator.toggle",
5651
+ guard: ({
5652
+ snapshot,
5653
+ event
5654
+ }) => {
5655
+ const manualSelection = event.offsets ? util_selectionPointToBlockOffset.blockOffsetsToSelection({
5656
+ value: snapshot.context.value,
5657
+ offsets: event.offsets
5658
+ }) : null;
5659
+ return manualSelection ? !selector_isSelectingEntireBlocks.isActiveDecorator(event.decorator)({
5660
+ ...snapshot,
5661
+ context: {
5662
+ ...snapshot.context,
5663
+ selection: manualSelection
5664
+ }
5665
+ }) : !selector_isSelectingEntireBlocks.isActiveDecorator(event.decorator)(snapshot);
5666
+ },
5667
+ actions: [({
5668
+ event
5669
+ }) => [behavior_core.raise({
5670
+ ...event,
5671
+ type: "decorator.add"
5672
+ })]]
5673
+ })], internalInsertBehaviors = [behavior_core.defineBehavior({
5673
5674
  on: "insert.blocks",
5674
5675
  guard: ({
5675
5676
  event
@@ -5904,67 +5905,7 @@ const keyIs = {
5904
5905
  type: "style.add",
5905
5906
  style: event.style
5906
5907
  })]]
5907
- })], toggleAnnotationOff = behavior_core.defineBehavior({
5908
- on: "annotation.toggle",
5909
- guard: ({
5910
- snapshot,
5911
- event
5912
- }) => selector_isSelectingEntireBlocks.isActiveAnnotation(event.annotation.name)(snapshot),
5913
- actions: [({
5914
- event
5915
- }) => [behavior_core.raise({
5916
- type: "annotation.remove",
5917
- annotation: event.annotation
5918
- })]]
5919
- }), toggleAnnotationOn = behavior_core.defineBehavior({
5920
- on: "annotation.toggle",
5921
- guard: ({
5922
- snapshot,
5923
- event
5924
- }) => !selector_isSelectingEntireBlocks.isActiveAnnotation(event.annotation.name)(snapshot),
5925
- actions: [({
5926
- event
5927
- }) => [behavior_core.raise({
5928
- type: "annotation.add",
5929
- annotation: event.annotation
5930
- })]]
5931
- }), toggleDecoratorOff = behavior_core.defineBehavior({
5932
- on: "decorator.toggle",
5933
- guard: ({
5934
- snapshot,
5935
- event
5936
- }) => selector_isSelectingEntireBlocks.isActiveDecorator(event.decorator)(snapshot),
5937
- actions: [({
5938
- event
5939
- }) => [behavior_core.raise({
5940
- type: "decorator.remove",
5941
- decorator: event.decorator
5942
- })]]
5943
- }), toggleDecoratorOn = behavior_core.defineBehavior({
5944
- on: "decorator.toggle",
5945
- guard: ({
5946
- snapshot,
5947
- event
5948
- }) => {
5949
- const manualSelection = event.offsets ? util_selectionPointToBlockOffset.blockOffsetsToSelection({
5950
- value: snapshot.context.value,
5951
- offsets: event.offsets
5952
- }) : null;
5953
- return manualSelection ? !selector_isSelectingEntireBlocks.isActiveDecorator(event.decorator)({
5954
- ...snapshot,
5955
- context: {
5956
- ...snapshot.context,
5957
- selection: manualSelection
5958
- }
5959
- }) : !selector_isSelectingEntireBlocks.isActiveDecorator(event.decorator)(snapshot);
5960
- },
5961
- actions: [({
5962
- event
5963
- }) => [behavior_core.raise({
5964
- ...event,
5965
- type: "decorator.add"
5966
- })]]
5967
- }), raiseDeserializationSuccessOrFailure = behavior_core.defineBehavior({
5908
+ })], raiseDeserializationSuccessOrFailure = behavior_core.defineBehavior({
5968
5909
  on: "deserialize",
5969
5910
  guard: ({
5970
5911
  snapshot,
@@ -6292,14 +6233,12 @@ const keyIs = {
6292
6233
  originEvent: event
6293
6234
  })]]
6294
6235
  }),
6236
+ ...internalAnnotationBehaviors,
6237
+ ...internalDecoratorBehaviors,
6295
6238
  ...internalInsertBehaviors,
6296
6239
  ...internalListItemBehaviors,
6297
6240
  ...internalStyleBehaviors,
6298
6241
  ...internalSelectBehaviors,
6299
- toggleAnnotationOff,
6300
- toggleAnnotationOn,
6301
- toggleDecoratorOff,
6302
- toggleDecoratorOn,
6303
6242
  raiseDeserializationSuccessOrFailure,
6304
6243
  raiseSerializationSuccessOrFailure,
6305
6244
  raiseInsertSoftBreak