@portabletext/editor 1.43.1 → 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.
Files changed (75) hide show
  1. package/lib/_chunks-cjs/behavior.markdown.cjs +1 -1
  2. package/lib/_chunks-cjs/editor-provider.cjs +555 -794
  3. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  4. package/lib/_chunks-cjs/parse-blocks.cjs +151 -0
  5. package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -0
  6. package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs +2 -1
  7. package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs.map +1 -1
  8. package/lib/_chunks-cjs/{selector.is-active-style.cjs → selector.is-selecting-entire-blocks.cjs} +47 -11
  9. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -0
  10. package/lib/_chunks-cjs/util.merge-text-blocks.cjs +3 -3
  11. package/lib/_chunks-cjs/util.merge-text-blocks.cjs.map +1 -1
  12. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs +0 -149
  13. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs.map +1 -1
  14. package/lib/_chunks-es/behavior.markdown.js +1 -1
  15. package/lib/_chunks-es/editor-provider.js +539 -777
  16. package/lib/_chunks-es/editor-provider.js.map +1 -1
  17. package/lib/_chunks-es/parse-blocks.js +152 -0
  18. package/lib/_chunks-es/parse-blocks.js.map +1 -0
  19. package/lib/_chunks-es/selector.is-overlapping-selection.js +2 -1
  20. package/lib/_chunks-es/selector.is-overlapping-selection.js.map +1 -1
  21. package/lib/_chunks-es/{selector.is-active-style.js → selector.is-selecting-entire-blocks.js} +49 -12
  22. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -0
  23. package/lib/_chunks-es/util.merge-text-blocks.js +1 -1
  24. package/lib/_chunks-es/util.selection-point-to-block-offset.js +0 -149
  25. package/lib/_chunks-es/util.selection-point-to-block-offset.js.map +1 -1
  26. package/lib/behaviors/index.d.cts +2819 -6421
  27. package/lib/behaviors/index.d.ts +2819 -6421
  28. package/lib/index.cjs +4 -4
  29. package/lib/index.cjs.map +1 -1
  30. package/lib/index.d.cts +2496 -6025
  31. package/lib/index.d.ts +2496 -6025
  32. package/lib/index.js +3 -3
  33. package/lib/plugins/index.cjs +1 -1
  34. package/lib/plugins/index.d.cts +2496 -6025
  35. package/lib/plugins/index.d.ts +2496 -6025
  36. package/lib/plugins/index.js +1 -1
  37. package/lib/selectors/index.cjs +12 -11
  38. package/lib/selectors/index.cjs.map +1 -1
  39. package/lib/selectors/index.d.cts +2464 -6018
  40. package/lib/selectors/index.d.ts +2464 -6018
  41. package/lib/selectors/index.js +2 -1
  42. package/lib/utils/index.d.cts +2458 -6022
  43. package/lib/utils/index.d.ts +2458 -6022
  44. package/package.json +2 -2
  45. package/src/behavior-actions/behavior.actions.ts +2 -152
  46. package/src/behaviors/behavior.default.ts +52 -120
  47. package/src/behaviors/behavior.internal.annotation.ts +26 -0
  48. package/src/behaviors/behavior.internal.decorator.ts +47 -0
  49. package/src/behaviors/behavior.internal.insert.ts +118 -0
  50. package/src/behaviors/behavior.internal.list-item.ts +61 -0
  51. package/src/behaviors/behavior.internal.select.ts +62 -0
  52. package/src/behaviors/behavior.internal.style.ts +54 -0
  53. package/src/behaviors/behavior.perform-event.ts +15 -13
  54. package/src/behaviors/behavior.types.event.ts +155 -108
  55. package/src/editor/create-editor.ts +2 -2
  56. package/src/editor/editor-machine.ts +3 -4
  57. package/src/editor/plugins/createWithEditableAPI.ts +1 -32
  58. package/src/editor/plugins/createWithPortableTextMarkModel.ts +0 -29
  59. package/src/internal-utils/slate-utils.ts +52 -0
  60. package/src/plugins/plugin.internal.editor-actor-ref.tsx +15 -0
  61. package/src/plugins/plugin.internal.slate-editor-ref.tsx +15 -0
  62. package/src/selectors/index.ts +2 -1
  63. package/src/selectors/selector.get-selected-text-blocks.ts +67 -0
  64. package/lib/_chunks-cjs/selector.is-active-style.cjs.map +0 -1
  65. package/lib/_chunks-es/selector.is-active-style.js.map +0 -1
  66. package/src/behavior-actions/behavior.action.data-transfer-set.ts +0 -7
  67. package/src/behavior-actions/behavior.action.deserialization.failure.ts +0 -9
  68. package/src/behavior-actions/behavior.action.deserialization.success.ts +0 -16
  69. package/src/behavior-actions/behavior.action.insert-blocks.ts +0 -140
  70. package/src/behavior-actions/behavior.action.list-item.ts +0 -100
  71. package/src/behavior-actions/behavior.action.select.next-block.ts +0 -44
  72. package/src/behavior-actions/behavior.action.select.previous-block.ts +0 -48
  73. package/src/behavior-actions/behavior.action.serialization.failure.ts +0 -9
  74. package/src/behavior-actions/behavior.action.serialization.success.ts +0 -17
  75. package/src/behavior-actions/behavior.action.style.ts +0 -108
@@ -13,8 +13,8 @@ import flatten from "lodash/flatten.js";
13
13
  import isPlainObject from "lodash/isPlainObject.js";
14
14
  import uniq from "lodash/uniq.js";
15
15
  import getRandomValues from "get-random-values-esm";
16
- import { parseBlock, selectionPointToBlockOffset, blockOffsetsToSelection, isTextBlock, parseBlocks } from "./util.selection-point-to-block-offset.js";
17
- import { sliceBlocks, blockOffsetToSpanSelectionPoint, isKeyedSegment as isKeyedSegment$1, getTextBlockText } from "./util.slice-blocks.js";
16
+ import { parseBlock, isTextBlock } from "./parse-blocks.js";
17
+ import { sliceBlocks, blockOffsetToSpanSelectionPoint, isKeyedSegment as isKeyedSegment$1, 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,11 +22,12 @@ 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 { createGuards, getFocusSpan, isSelectionCollapsed, isOverlappingSelection, getSelectedBlocks, getFocusTextBlock, isSelectionExpanded } from "./selector.is-overlapping-selection.js";
26
- import { getTrimmedSelection, isActiveAnnotation, isActiveDecorator, isActiveListItem, isActiveStyle, isSelectingEntireBlocks, getActiveAnnotations } from "./selector.is-active-style.js";
25
+ import { selectionPointToBlockOffset, blockOffsetsToSelection } from "./util.selection-point-to-block-offset.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";
30
+ import { getFocusTextBlock, getPreviousBlock, getNextBlock, getFocusSpan, isSelectionCollapsed, isOverlappingSelection, getSelectedBlocks, isSelectionExpanded } from "./selector.is-overlapping-selection.js";
30
31
  import { Subject } from "rxjs";
31
32
  import { useEffectEvent } from "use-effect-event";
32
33
  function createEditorSchema(portableTextType) {
@@ -408,6 +409,30 @@ function isBlockElement({
408
409
  }, node) {
409
410
  return Element.isElement(node) && !editor.isInline(node) && (schema.block.name === node._type || schema.blockObjects.some((blockObject) => blockObject.name === node._type));
410
411
  }
412
+ function isListItemActive({
413
+ editor,
414
+ listItem
415
+ }) {
416
+ if (!editor.selection)
417
+ return !1;
418
+ const selectedBlocks = [...Editor.nodes(editor, {
419
+ at: editor.selection,
420
+ match: (node) => editor.isTextBlock(node)
421
+ })];
422
+ return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => editor.isListBlock(node) && node.listItem === listItem) : !1;
423
+ }
424
+ function isStyleActive({
425
+ editor,
426
+ style
427
+ }) {
428
+ if (!editor.selection)
429
+ return !1;
430
+ const selectedBlocks = [...Editor.nodes(editor, {
431
+ at: editor.selection,
432
+ match: (node) => editor.isTextBlock(node)
433
+ })];
434
+ return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => node.style === style) : !1;
435
+ }
411
436
  const IS_PROCESSING_REMOTE_CHANGES = /* @__PURE__ */ new WeakMap(), KEY_TO_SLATE_ELEMENT = /* @__PURE__ */ new WeakMap(), KEY_TO_VALUE_ELEMENT = /* @__PURE__ */ new WeakMap(), SLATE_TO_PORTABLE_TEXT_RANGE = /* @__PURE__ */ new WeakMap(), EditorActorContext = createContext({}), PortableTextEditorContext = createContext(null), usePortableTextEditor = () => {
412
437
  const editor = useContext(PortableTextEditorContext);
413
438
  if (!editor)
@@ -2964,131 +2989,7 @@ const insertBreakActionImplementation = ({
2964
2989
  }) => {
2965
2990
  insertText(action.editor, `
2966
2991
  `);
2967
- }, toggleListItemActionImplementation = ({
2968
- context,
2969
- action
2970
- }) => {
2971
- isListItemActive({
2972
- editor: action.editor,
2973
- listItem: action.listItem
2974
- }) ? removeListItemActionImplementation({
2975
- context,
2976
- action: {
2977
- ...action
2978
- }
2979
- }) : addListItemActionImplementation({
2980
- context,
2981
- action: {
2982
- ...action
2983
- }
2984
- });
2985
- }, removeListItemActionImplementation = ({
2986
- context,
2987
- action
2988
- }) => {
2989
- if (!action.editor.selection)
2990
- return;
2991
- const guards = createGuards(context), selectedBlocks = [...Editor.nodes(action.editor, {
2992
- at: action.editor.selection,
2993
- match: (node) => guards.isListBlock(node)
2994
- })];
2995
- for (const [, at] of selectedBlocks)
2996
- Transforms.unsetNodes(action.editor, ["listItem", "level"], {
2997
- at
2998
- });
2999
- }, addListItemActionImplementation = ({
3000
- context,
3001
- action
3002
- }) => {
3003
- if (!action.editor.selection)
3004
- return;
3005
- const guards = createGuards(context), selectedBlocks = [...Editor.nodes(action.editor, {
3006
- at: action.editor.selection,
3007
- match: (node) => guards.isTextBlock(node)
3008
- })];
3009
- for (const [, at] of selectedBlocks)
3010
- Transforms.setNodes(action.editor, {
3011
- level: 1,
3012
- listItem: action.listItem
3013
- }, {
3014
- at
3015
- });
3016
- };
3017
- function isListItemActive({
3018
- editor,
3019
- listItem
3020
- }) {
3021
- if (!editor.selection)
3022
- return !1;
3023
- const selectedBlocks = [...Editor.nodes(editor, {
3024
- at: editor.selection,
3025
- match: (node) => editor.isTextBlock(node)
3026
- })];
3027
- return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => editor.isListBlock(node) && node.listItem === listItem) : !1;
3028
- }
3029
- const toggleStyleActionImplementation = ({
3030
- context,
3031
- action
3032
- }) => {
3033
- isStyleActive({
3034
- editor: action.editor,
3035
- style: action.style
3036
- }) ? removeStyleActionImplementation({
3037
- context,
3038
- action: {
3039
- ...action
3040
- }
3041
- }) : addStyleActionImplementation({
3042
- context,
3043
- action: {
3044
- ...action
3045
- }
3046
- });
3047
- }, removeStyleActionImplementation = ({
3048
- context,
3049
- action
3050
- }) => {
3051
- if (!action.editor.selection)
3052
- return;
3053
- const defaultStyle = context.schema.styles[0].value, guards = createGuards(context), selectedBlocks = [...Editor.nodes(action.editor, {
3054
- at: action.editor.selection,
3055
- match: (node) => guards.isTextBlock(node)
3056
- })];
3057
- for (const [, at] of selectedBlocks)
3058
- Transforms.setNodes(action.editor, {
3059
- style: defaultStyle
3060
- }, {
3061
- at
3062
- });
3063
- }, addStyleActionImplementation = ({
3064
- context,
3065
- action
3066
- }) => {
3067
- if (!action.editor.selection)
3068
- return;
3069
- const guards = createGuards(context), selectedBlocks = [...Editor.nodes(action.editor, {
3070
- at: action.editor.selection,
3071
- match: (node) => guards.isTextBlock(node)
3072
- })];
3073
- for (const [, at] of selectedBlocks)
3074
- Transforms.setNodes(action.editor, {
3075
- style: action.style
3076
- }, {
3077
- at
3078
- });
3079
2992
  };
3080
- function isStyleActive({
3081
- editor,
3082
- style
3083
- }) {
3084
- if (!editor.selection)
3085
- return !1;
3086
- const selectedBlocks = [...Editor.nodes(editor, {
3087
- at: editor.selection,
3088
- match: (node) => editor.isTextBlock(node)
3089
- })];
3090
- return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => node.style === style) : !1;
3091
- }
3092
2993
  function isPortableTextSpan(node) {
3093
2994
  return node._type === "span" && "text" in node && typeof node.text == "string" && (typeof node.marks > "u" || Array.isArray(node.marks) && node.marks.every((mark) => typeof mark == "string"));
3094
2995
  }
@@ -3104,112 +3005,6 @@ function isPortableTextBlock(node) {
3104
3005
  node.children.every((child) => typeof child == "object" && "_type" in child)
3105
3006
  );
3106
3007
  }
3107
- const decoratorAddActionImplementation = ({
3108
- context,
3109
- action
3110
- }) => {
3111
- 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({
3112
- value,
3113
- blockOffset: action.offsets.anchor,
3114
- direction: "backward"
3115
- }) : void 0, manualFocus = action.offsets?.focus ? blockOffsetToSpanSelectionPoint({
3116
- value,
3117
- blockOffset: action.offsets.focus,
3118
- direction: "forward"
3119
- }) : void 0, manualSelection = manualAnchor && manualFocus ? {
3120
- anchor: manualAnchor,
3121
- focus: manualFocus
3122
- } : void 0, selection = manualSelection ? toSlateRange(manualSelection, action.editor) ?? editor.selection : editor.selection;
3123
- if (!selection)
3124
- return;
3125
- const editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? selectionPointToBlockOffset({
3126
- value,
3127
- selectionPoint: editorSelection.anchor
3128
- }) : void 0, focusOffset = editorSelection ? selectionPointToBlockOffset({
3129
- value,
3130
- selectionPoint: editorSelection.focus
3131
- }) : void 0;
3132
- if (!anchorOffset || !focusOffset)
3133
- throw new Error("Unable to find anchor or focus offset");
3134
- if (Range.isExpanded(selection)) {
3135
- Transforms.setNodes(editor, {}, {
3136
- at: selection,
3137
- match: Text.isText,
3138
- split: !0,
3139
- hanging: !0
3140
- });
3141
- const newValue = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), newSelection = blockOffsetsToSelection({
3142
- value: newValue,
3143
- offsets: {
3144
- anchor: anchorOffset,
3145
- focus: focusOffset
3146
- },
3147
- backward: editorSelection?.backward
3148
- }), trimmedSelection = getTrimmedSelection({
3149
- context: {
3150
- activeDecorators: [],
3151
- converters: [],
3152
- keyGenerator: context.keyGenerator,
3153
- readOnly: !1,
3154
- schema: context.schema,
3155
- selection: newSelection,
3156
- value: newValue
3157
- }
3158
- });
3159
- if (!trimmedSelection)
3160
- throw new Error("Unable to find trimmed selection");
3161
- const newRange = toSlateRange(trimmedSelection, editor);
3162
- if (!newRange)
3163
- throw new Error("Unable to find new selection");
3164
- const splitTextNodes = Range.isRange(newRange) ? [...Editor.nodes(editor, {
3165
- at: newRange,
3166
- match: (node) => Text.isText(node)
3167
- })] : [];
3168
- for (const [node, path] of splitTextNodes) {
3169
- const marks = [...(Array.isArray(node.marks) ? node.marks : []).filter((eMark) => eMark !== mark), mark];
3170
- Transforms.setNodes(editor, {
3171
- marks
3172
- }, {
3173
- at: path,
3174
- match: Text.isText,
3175
- split: !0,
3176
- hanging: !0
3177
- });
3178
- }
3179
- } else {
3180
- if (!Array.from(Editor.nodes(editor, {
3181
- at: selection,
3182
- match: (node) => editor.isTextSpan(node)
3183
- }))?.at(0))
3184
- return;
3185
- const [block, blockPath] = Editor.node(editor, selection, {
3186
- depth: 1
3187
- }), lonelyEmptySpan = editor.isTextBlock(block) && block.children.length === 1 && editor.isTextSpan(block.children[0]) && block.children[0].text === "" ? block.children[0] : void 0;
3188
- if (lonelyEmptySpan) {
3189
- const existingMarks = lonelyEmptySpan.marks ?? [], existingMarksWithoutDecorator = existingMarks.filter((existingMark) => existingMark !== mark);
3190
- Transforms.setNodes(editor, {
3191
- marks: existingMarks.length === existingMarksWithoutDecorator.length ? [...existingMarks, mark] : existingMarksWithoutDecorator
3192
- }, {
3193
- at: blockPath,
3194
- match: (node) => editor.isTextSpan(node)
3195
- });
3196
- } else {
3197
- const existingMarks = {
3198
- ...Editor.marks(editor) || {}
3199
- }.marks || [], marks = {
3200
- ...Editor.marks(editor) || {},
3201
- marks: [...existingMarks, mark]
3202
- };
3203
- editor.marks = marks;
3204
- }
3205
- }
3206
- if (editor.selection) {
3207
- const selection2 = editor.selection;
3208
- editor.selection = {
3209
- ...selection2
3210
- };
3211
- }
3212
- };
3213
3008
  function getPreviousSpan({
3214
3009
  editor,
3215
3010
  blockPath,
@@ -3667,25 +3462,7 @@ function isDecoratorActive({
3667
3462
  ...Editor.marks(editor) || {}
3668
3463
  }.marks || []).includes(decorator);
3669
3464
  }
3670
- const toggleDecoratorActionImplementation = ({
3671
- context,
3672
- action
3673
- }) => {
3674
- isDecoratorActive({
3675
- editor: action.editor,
3676
- decorator: action.decorator
3677
- }) ? removeDecoratorActionImplementation({
3678
- action: {
3679
- editor: action.editor,
3680
- decorator: action.decorator
3681
- }
3682
- }) : decoratorAddActionImplementation({
3683
- context,
3684
- action: {
3685
- ...action
3686
- }
3687
- });
3688
- }, debug$a = debugWithName("API:editable");
3465
+ const debug$a = debugWithName("API:editable");
3689
3466
  function createEditableAPI(editor, editorActor) {
3690
3467
  const types = editorActor.getSnapshot().context.schema;
3691
3468
  return {
@@ -4156,30 +3933,6 @@ const addAnnotationActionImplementation = ({
4156
3933
  }
4157
3934
  }
4158
3935
  }
4159
- }, toggleAnnotationActionImplementation = ({
4160
- context,
4161
- action
4162
- }) => {
4163
- if (isAnnotationActive({
4164
- editor: action.editor,
4165
- annotation: {
4166
- name: action.annotation.name
4167
- }
4168
- }))
4169
- removeAnnotationActionImplementation({
4170
- action: {
4171
- annotation: action.annotation,
4172
- editor: action.editor
4173
- }
4174
- });
4175
- else
4176
- return addAnnotationActionImplementation({
4177
- context,
4178
- action: {
4179
- annotation: action.annotation,
4180
- editor: action.editor
4181
- }
4182
- });
4183
3936
  }, blockSetBehaviorActionImplementation = ({
4184
3937
  context,
4185
3938
  action
@@ -4294,40 +4047,141 @@ const addAnnotationActionImplementation = ({
4294
4047
  action
4295
4048
  }) => {
4296
4049
  ReactEditor.blur(action.editor);
4297
- }, dataTransferSetActionImplementation = ({
4298
- action
4299
- }) => {
4300
- action.dataTransfer.setData(action.mimeType, action.data);
4301
- }, deleteActionImplementation = ({
4302
- action
4303
- }) => {
4304
- const range = toSlateRange(action.selection, action.editor);
4305
- if (!range)
4306
- throw new Error(`Failed to get Slate Range for selection ${JSON.stringify(action.selection)}`);
4307
- action.editor.delete({
4308
- at: range
4309
- });
4310
- }, deleteBackwardActionImplementation = ({
4311
- action
4312
- }) => {
4313
- action.editor.deleteBackward(action.unit);
4314
- }, deleteBlockActionImplementation = ({
4050
+ }, decoratorAddActionImplementation = ({
4051
+ context,
4315
4052
  action
4316
4053
  }) => {
4317
- const range = toSlateRange({
4318
- anchor: {
4319
- path: action.at,
4320
- offset: 0
4321
- },
4322
- focus: {
4323
- path: action.at,
4324
- offset: 0
4325
- }
4326
- }, action.editor);
4327
- if (!range) {
4328
- console.error("Unable to find Slate range from selection points");
4329
- return;
4330
- }
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
+ }
4155
+ }, deleteActionImplementation = ({
4156
+ action
4157
+ }) => {
4158
+ const range = toSlateRange(action.selection, action.editor);
4159
+ if (!range)
4160
+ throw new Error(`Failed to get Slate Range for selection ${JSON.stringify(action.selection)}`);
4161
+ action.editor.delete({
4162
+ at: range
4163
+ });
4164
+ }, deleteBackwardActionImplementation = ({
4165
+ action
4166
+ }) => {
4167
+ action.editor.deleteBackward(action.unit);
4168
+ }, deleteBlockActionImplementation = ({
4169
+ action
4170
+ }) => {
4171
+ const range = toSlateRange({
4172
+ anchor: {
4173
+ path: action.at,
4174
+ offset: 0
4175
+ },
4176
+ focus: {
4177
+ path: action.at,
4178
+ offset: 0
4179
+ }
4180
+ }, action.editor);
4181
+ if (!range) {
4182
+ console.error("Unable to find Slate range from selection points");
4183
+ return;
4184
+ }
4331
4185
  Transforms.removeNodes(action.editor, {
4332
4186
  at: range
4333
4187
  });
@@ -4367,10 +4221,79 @@ const addAnnotationActionImplementation = ({
4367
4221
  Transforms.delete(action.editor, {
4368
4222
  at: range
4369
4223
  });
4370
- }, deserializationFailureActionImplementation = ({
4224
+ }, effectActionImplementation = ({
4225
+ action
4226
+ }) => {
4227
+ action.effect();
4228
+ }, focusActionImplementation = ({
4229
+ action
4230
+ }) => {
4231
+ ReactEditor.focus(action.editor);
4232
+ }, insertInlineObjectActionImplementation = ({
4233
+ context,
4234
+ action
4235
+ }) => {
4236
+ if (!context.schema.inlineObjects.some((inlineObject) => inlineObject.name === action.inlineObject.name)) {
4237
+ console.error("Unable to insert unknown inline object");
4238
+ return;
4239
+ }
4240
+ if (!action.editor.selection) {
4241
+ console.error("Unable to insert inline object without selection");
4242
+ return;
4243
+ }
4244
+ const [focusTextBlock] = Array.from(Editor.nodes(action.editor, {
4245
+ at: action.editor.selection.focus.path,
4246
+ match: (node) => action.editor.isTextBlock(node)
4247
+ })).at(0) ?? [void 0, void 0];
4248
+ if (!focusTextBlock) {
4249
+ console.error("Unable to perform action without focus text block");
4250
+ return;
4251
+ }
4252
+ const child = toSlateValue([{
4253
+ _type: context.schema.block.name,
4254
+ _key: context.keyGenerator(),
4255
+ children: [{
4256
+ _type: action.inlineObject.name,
4257
+ _key: context.keyGenerator(),
4258
+ ...action.inlineObject.value ?? {}
4259
+ }]
4260
+ }], {
4261
+ schemaTypes: context.schema
4262
+ }).at(0)?.children.at(0);
4263
+ if (!child) {
4264
+ console.error("Unable to insert inline object");
4265
+ return;
4266
+ }
4267
+ Transforms.insertNodes(action.editor, child);
4268
+ }, insertSpanActionImplementation = ({
4269
+ context,
4371
4270
  action
4372
4271
  }) => {
4373
- console.warn(`Deserialization of ${action.mimeType} failed with reason "${action.reason}"`);
4272
+ if (!action.editor.selection) {
4273
+ console.error("Unable to perform action without selection", action);
4274
+ return;
4275
+ }
4276
+ const [focusBlock, focusBlockPath] = Array.from(Editor.nodes(action.editor, {
4277
+ at: action.editor.selection.focus.path,
4278
+ match: (node) => action.editor.isTextBlock(node)
4279
+ }))[0] ?? [void 0, void 0];
4280
+ if (!focusBlock || !focusBlockPath) {
4281
+ console.error("Unable to perform action without focus block", action);
4282
+ return;
4283
+ }
4284
+ const markDefs = focusBlock.markDefs ?? [], annotations = action.annotations ? action.annotations.map((annotation) => ({
4285
+ _type: annotation.name,
4286
+ _key: context.keyGenerator(),
4287
+ ...annotation.value
4288
+ })) : void 0;
4289
+ annotations && annotations.length > 0 && Transforms.setNodes(action.editor, {
4290
+ markDefs: [...markDefs, ...annotations]
4291
+ }), Transforms.insertNodes(action.editor, {
4292
+ _type: "span",
4293
+ _key: context.keyGenerator(),
4294
+ text: action.text,
4295
+ marks: [...annotations?.map((annotation) => annotation._key) ?? [], ...action.decorators ?? []]
4296
+ });
4374
4297
  }, insertBlockActionImplementation = ({
4375
4298
  context,
4376
4299
  action
@@ -4458,304 +4381,65 @@ function insertBlock({
4458
4381
  });
4459
4382
  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));
4460
4383
  } else if (placement === "after") {
4461
- const nextPath = [focusBlockPath[0] + 1], currentSelection = editor.selection;
4462
- Transforms.insertNodes(editor, [block], {
4463
- at: nextPath,
4464
- select: !1
4465
- }), Transforms.select(editor, currentSelection), select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
4466
- } else {
4467
- const currentSelection = editor.selection, focusBlockStartPoint = Editor.start(editor, focusBlockPath);
4468
- if (editor.isTextBlock(focusBlock) && editor.isTextBlock(block)) {
4469
- if (select === "end") {
4470
- Transforms.insertFragment(editor, [block], {
4471
- voids: !0
4472
- });
4473
- return;
4474
- }
4475
- Transforms.insertFragment(editor, [block], {
4476
- at: currentSelection,
4477
- voids: !0
4478
- }), 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);
4479
- } else if (editor.isTextBlock(focusBlock)) {
4480
- const focusBlockStartPoint2 = Editor.start(editor, focusBlockPath), focusBlockEndPoint = Editor.end(editor, focusBlockPath);
4481
- if (Point.equals(currentSelection.anchor, focusBlockStartPoint2))
4482
- Transforms.insertNodes(editor, [block], {
4483
- at: focusBlockPath,
4484
- select: !1
4485
- }), (select === "start" || select === "end") && Transforms.select(editor, Editor.start(editor, focusBlockPath)), isEqualToEmptyEditor([focusBlock], schema) && Transforms.removeNodes(editor, {
4486
- at: Path.next(focusBlockPath)
4487
- });
4488
- else if (Point.equals(currentSelection.focus, focusBlockEndPoint)) {
4489
- const nextPath = [focusBlockPath[0] + 1];
4490
- Transforms.insertNodes(editor, [block], {
4491
- at: nextPath,
4492
- select: !1
4493
- }), (select === "start" || select === "end") && Transforms.select(editor, Editor.start(editor, nextPath));
4494
- } else {
4495
- const currentSelection2 = editor.selection, [focusChild] = getFocusChild({
4496
- editor
4497
- });
4498
- if (focusChild && editor.isTextSpan(focusChild))
4499
- Transforms.insertFragment(editor, [block], {
4500
- at: currentSelection2
4501
- }), select === "start" || select === "end" ? Transforms.select(editor, [focusBlockPath[0] + 1]) : Transforms.select(editor, currentSelection2);
4502
- else {
4503
- const nextPath = [focusBlockPath[0] + 1];
4504
- Transforms.insertNodes(editor, [block], {
4505
- at: nextPath,
4506
- select: !1
4507
- }), Transforms.select(editor, currentSelection2), select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
4508
- }
4509
- }
4510
- } else {
4511
- Transforms.insertNodes(editor, [block], {
4512
- select: !1
4513
- });
4514
- const nextPath = [focusBlockPath[0] + 1];
4515
- select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
4516
- }
4517
- }
4518
- }
4519
- const selectActionImplementation = ({
4520
- action
4521
- }) => {
4522
- const newSelection = toSlateRange(action.selection, action.editor);
4523
- newSelection ? Transforms.select(action.editor, newSelection) : Transforms.deselect(action.editor);
4524
- }, selectNextBlockActionImplementation = ({
4525
- context,
4526
- action
4527
- }) => {
4528
- if (!action.editor.selection) {
4529
- console.error("Unable to select previous block without a selection");
4530
- return;
4531
- }
4532
- const nextBlockPath = [action.editor.selection.focus.path.slice(0, 1)[0] + 1], position = action.select === "end" ? Editor.end(action.editor, nextBlockPath) : Editor.start(action.editor, nextBlockPath), newSelection = toPortableTextRange(action.editor.children, {
4533
- anchor: position,
4534
- focus: position
4535
- }, context.schema);
4536
- if (!newSelection) {
4537
- console.error("Could not find selection for next block");
4538
- return;
4539
- }
4540
- selectActionImplementation({
4541
- action: {
4542
- selection: newSelection,
4543
- editor: action.editor
4544
- }
4545
- });
4546
- }, selectPreviousBlockActionImplementation = ({
4547
- context,
4548
- action
4549
- }) => {
4550
- if (!action.editor.selection) {
4551
- console.error("Unable to select previous block without a selection");
4552
- return;
4553
- }
4554
- const blockPath = action.editor.selection.focus.path.slice(0, 1);
4555
- if (!Path.hasPrevious(blockPath)) {
4556
- console.error("There's no previous block to select");
4557
- return;
4558
- }
4559
- const position = action.select === "end" ? Editor.end(action.editor, Path.previous(blockPath)) : Editor.start(action.editor, Path.previous(blockPath)), newSelection = toPortableTextRange(action.editor.children, {
4560
- anchor: position,
4561
- focus: position
4562
- }, context.schema);
4563
- if (!newSelection) {
4564
- console.error("Could not find selection for previous block");
4565
- return;
4566
- }
4567
- selectActionImplementation({
4568
- action: {
4569
- selection: newSelection,
4570
- editor: action.editor
4571
- }
4572
- });
4573
- }, insertBlocksActionImplementation = ({
4574
- context,
4575
- action
4576
- }) => {
4577
- const parsedBlocks = parseBlocks({
4578
- context,
4579
- blocks: action.blocks,
4580
- options: {
4581
- refreshKeys: !1
4582
- }
4583
- });
4584
- if (parsedBlocks.length === 0)
4585
- throw new Error(`Failed to parse blocks ${JSON.stringify(action.blocks)}`);
4586
- const fragment = toSlateValue(parsedBlocks, {
4587
- schemaTypes: context.schema
4588
- });
4589
- if (fragment.length === 0)
4590
- throw new Error(`Failed to convert blocks to Slate fragment ${JSON.stringify(parsedBlocks)}`);
4591
- const [focusBlock] = getFocusBlock({
4592
- editor: action.editor
4593
- });
4594
- if (action.placement === "before") {
4595
- let index = 0;
4596
- for (const block of fragment)
4597
- insertBlock({
4598
- block,
4599
- placement: index === 0 ? "before" : "after",
4600
- select: "end",
4601
- editor: action.editor,
4602
- schema: context.schema
4603
- }), index++;
4604
- } else if (action.placement === "after")
4605
- for (const block of fragment)
4606
- insertBlock({
4607
- block,
4608
- placement: "after",
4609
- select: "end",
4610
- editor: action.editor,
4611
- schema: context.schema
4612
- });
4613
- else if (focusBlock && action.editor.isTextBlock(focusBlock)) {
4614
- if (fragment.length === 1) {
4615
- insertBlock({
4616
- block: fragment[0],
4617
- placement: "auto",
4618
- select: "end",
4619
- editor: action.editor,
4620
- schema: context.schema
4621
- });
4622
- return;
4623
- }
4624
- let index = 0;
4625
- for (const block of fragment)
4626
- index === 0 ? (insertBreakActionImplementation({
4627
- context,
4628
- action: {
4629
- editor: action.editor
4630
- }
4631
- }), selectPreviousBlockActionImplementation({
4632
- context,
4633
- action: {
4634
- editor: action.editor,
4635
- select: "end"
4636
- }
4637
- }), insertBlock({
4638
- block,
4639
- placement: "auto",
4640
- select: "end",
4641
- editor: action.editor,
4642
- schema: context.schema
4643
- })) : index === fragment.length - 1 ? (selectNextBlockActionImplementation({
4644
- context,
4645
- action: {
4646
- editor: action.editor,
4647
- select: "start"
4648
- }
4649
- }), insertBlock({
4650
- block,
4651
- placement: "auto",
4652
- select: "end",
4653
- editor: action.editor,
4654
- schema: context.schema
4655
- })) : insertBlock({
4656
- block,
4657
- placement: "after",
4658
- select: "end",
4659
- editor: action.editor,
4660
- schema: context.schema
4661
- }), index++;
4662
- } else {
4663
- let index = 0;
4664
- for (const block of fragment)
4665
- insertBlock({
4666
- block,
4667
- placement: index === 0 ? "auto" : "after",
4668
- select: "end",
4669
- editor: action.editor,
4670
- schema: context.schema
4671
- }), index++;
4672
- }
4673
- }, deserializationSuccessActionImplementation = ({
4674
- context,
4675
- action
4676
- }) => {
4677
- insertBlocksActionImplementation({
4678
- context,
4679
- action: {
4680
- blocks: action.data,
4681
- editor: action.editor,
4682
- placement: "auto"
4683
- }
4684
- });
4685
- }, effectActionImplementation = ({
4686
- action
4687
- }) => {
4688
- action.effect();
4689
- }, focusActionImplementation = ({
4690
- action
4691
- }) => {
4692
- ReactEditor.focus(action.editor);
4693
- }, insertInlineObjectActionImplementation = ({
4694
- context,
4695
- action
4696
- }) => {
4697
- if (!context.schema.inlineObjects.some((inlineObject) => inlineObject.name === action.inlineObject.name)) {
4698
- console.error("Unable to insert unknown inline object");
4699
- return;
4700
- }
4701
- if (!action.editor.selection) {
4702
- console.error("Unable to insert inline object without selection");
4703
- return;
4704
- }
4705
- const [focusTextBlock] = Array.from(Editor.nodes(action.editor, {
4706
- at: action.editor.selection.focus.path,
4707
- match: (node) => action.editor.isTextBlock(node)
4708
- })).at(0) ?? [void 0, void 0];
4709
- if (!focusTextBlock) {
4710
- console.error("Unable to perform action without focus text block");
4711
- return;
4712
- }
4713
- const child = toSlateValue([{
4714
- _type: context.schema.block.name,
4715
- _key: context.keyGenerator(),
4716
- children: [{
4717
- _type: action.inlineObject.name,
4718
- _key: context.keyGenerator(),
4719
- ...action.inlineObject.value ?? {}
4720
- }]
4721
- }], {
4722
- schemaTypes: context.schema
4723
- }).at(0)?.children.at(0);
4724
- if (!child) {
4725
- console.error("Unable to insert inline object");
4726
- return;
4727
- }
4728
- Transforms.insertNodes(action.editor, child);
4729
- }, insertSpanActionImplementation = ({
4730
- context,
4731
- action
4732
- }) => {
4733
- if (!action.editor.selection) {
4734
- console.error("Unable to perform action without selection", action);
4735
- return;
4736
- }
4737
- const [focusBlock, focusBlockPath] = Array.from(Editor.nodes(action.editor, {
4738
- at: action.editor.selection.focus.path,
4739
- match: (node) => action.editor.isTextBlock(node)
4740
- }))[0] ?? [void 0, void 0];
4741
- if (!focusBlock || !focusBlockPath) {
4742
- console.error("Unable to perform action without focus block", action);
4743
- return;
4384
+ const nextPath = [focusBlockPath[0] + 1], currentSelection = editor.selection;
4385
+ Transforms.insertNodes(editor, [block], {
4386
+ at: nextPath,
4387
+ select: !1
4388
+ }), Transforms.select(editor, currentSelection), select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
4389
+ } else {
4390
+ const currentSelection = editor.selection, focusBlockStartPoint = Editor.start(editor, focusBlockPath);
4391
+ if (editor.isTextBlock(focusBlock) && editor.isTextBlock(block)) {
4392
+ if (select === "end") {
4393
+ Transforms.insertFragment(editor, [block], {
4394
+ voids: !0
4395
+ });
4396
+ return;
4397
+ }
4398
+ Transforms.insertFragment(editor, [block], {
4399
+ at: currentSelection,
4400
+ voids: !0
4401
+ }), 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);
4402
+ } else if (editor.isTextBlock(focusBlock)) {
4403
+ const focusBlockStartPoint2 = Editor.start(editor, focusBlockPath), focusBlockEndPoint = Editor.end(editor, focusBlockPath);
4404
+ if (Point.equals(currentSelection.anchor, focusBlockStartPoint2))
4405
+ Transforms.insertNodes(editor, [block], {
4406
+ at: focusBlockPath,
4407
+ select: !1
4408
+ }), (select === "start" || select === "end") && Transforms.select(editor, Editor.start(editor, focusBlockPath)), isEqualToEmptyEditor([focusBlock], schema) && Transforms.removeNodes(editor, {
4409
+ at: Path.next(focusBlockPath)
4410
+ });
4411
+ else if (Point.equals(currentSelection.focus, focusBlockEndPoint)) {
4412
+ const nextPath = [focusBlockPath[0] + 1];
4413
+ Transforms.insertNodes(editor, [block], {
4414
+ at: nextPath,
4415
+ select: !1
4416
+ }), (select === "start" || select === "end") && Transforms.select(editor, Editor.start(editor, nextPath));
4417
+ } else {
4418
+ const currentSelection2 = editor.selection, [focusChild] = getFocusChild({
4419
+ editor
4420
+ });
4421
+ if (focusChild && editor.isTextSpan(focusChild))
4422
+ Transforms.insertFragment(editor, [block], {
4423
+ at: currentSelection2
4424
+ }), select === "start" || select === "end" ? Transforms.select(editor, [focusBlockPath[0] + 1]) : Transforms.select(editor, currentSelection2);
4425
+ else {
4426
+ const nextPath = [focusBlockPath[0] + 1];
4427
+ Transforms.insertNodes(editor, [block], {
4428
+ at: nextPath,
4429
+ select: !1
4430
+ }), Transforms.select(editor, currentSelection2), select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
4431
+ }
4432
+ }
4433
+ } else {
4434
+ Transforms.insertNodes(editor, [block], {
4435
+ select: !1
4436
+ });
4437
+ const nextPath = [focusBlockPath[0] + 1];
4438
+ select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
4439
+ }
4744
4440
  }
4745
- const markDefs = focusBlock.markDefs ?? [], annotations = action.annotations ? action.annotations.map((annotation) => ({
4746
- _type: annotation.name,
4747
- _key: context.keyGenerator(),
4748
- ...annotation.value
4749
- })) : void 0;
4750
- annotations && annotations.length > 0 && Transforms.setNodes(action.editor, {
4751
- markDefs: [...markDefs, ...annotations]
4752
- }), Transforms.insertNodes(action.editor, {
4753
- _type: "span",
4754
- _key: context.keyGenerator(),
4755
- text: action.text,
4756
- marks: [...annotations?.map((annotation) => annotation._key) ?? [], ...action.decorators ?? []]
4757
- });
4758
- }, insertTextActionImplementation = ({
4441
+ }
4442
+ const insertTextActionImplementation = ({
4759
4443
  action
4760
4444
  }) => {
4761
4445
  action.editor.insertText(action.text);
@@ -4826,65 +4510,39 @@ const selectActionImplementation = ({
4826
4510
  }
4827
4511
  });
4828
4512
  }, noopActionImplementation = () => {
4829
- }, serializationFailureActionImplementation = ({
4830
- action
4831
- }) => {
4832
- console.warn(`Serialization of ${action.mimeType} failed with reason "${action.reason}"`);
4833
- }, serializationSuccessActionImplementation = ({
4834
- context,
4513
+ }, selectActionImplementation = ({
4835
4514
  action
4836
4515
  }) => {
4837
- dataTransferSetActionImplementation({
4838
- action: {
4839
- data: action.data,
4840
- dataTransfer: action.originEvent.originEvent.dataTransfer,
4841
- mimeType: action.mimeType
4842
- }
4843
- });
4516
+ const newSelection = toSlateRange(action.selection, action.editor);
4517
+ newSelection ? Transforms.select(action.editor, newSelection) : Transforms.deselect(action.editor);
4844
4518
  }, debug$9 = debugWithName("behaviors:action"), behaviorActionImplementations = {
4845
4519
  "annotation.add": addAnnotationActionImplementation,
4846
4520
  "annotation.remove": removeAnnotationActionImplementation,
4847
- "annotation.toggle": toggleAnnotationActionImplementation,
4848
4521
  "block.set": blockSetBehaviorActionImplementation,
4849
4522
  "block.unset": blockUnsetBehaviorActionImplementation,
4850
4523
  blur: blurActionImplementation,
4851
- "data transfer.set": dataTransferSetActionImplementation,
4852
4524
  "decorator.add": decoratorAddActionImplementation,
4853
4525
  "decorator.remove": removeDecoratorActionImplementation,
4854
- "decorator.toggle": toggleDecoratorActionImplementation,
4855
4526
  focus: focusActionImplementation,
4856
4527
  delete: deleteActionImplementation,
4857
4528
  "delete.backward": deleteBackwardActionImplementation,
4858
4529
  "delete.forward": deleteForwardActionImplementation,
4859
4530
  "delete.block": deleteBlockActionImplementation,
4860
4531
  "delete.text": deleteTextActionImplementation,
4861
- "deserialization.failure": deserializationFailureActionImplementation,
4862
- "deserialization.success": deserializationSuccessActionImplementation,
4863
4532
  "history.redo": historyRedoActionImplementation,
4864
4533
  "history.undo": historyUndoActionImplementation,
4865
4534
  "insert.block": insertBlockActionImplementation,
4866
- "insert.blocks": insertBlocksActionImplementation,
4867
4535
  "insert.break": insertBreakActionImplementation,
4868
4536
  "insert.inline object": insertInlineObjectActionImplementation,
4869
4537
  "insert.soft break": insertSoftBreakActionImplementation,
4870
4538
  "insert.span": insertSpanActionImplementation,
4871
4539
  "insert.text": insertTextActionImplementation,
4872
4540
  effect: effectActionImplementation,
4873
- "list item.add": addListItemActionImplementation,
4874
- "list item.remove": removeListItemActionImplementation,
4875
- "list item.toggle": toggleListItemActionImplementation,
4876
4541
  "move.block": moveBlockActionImplementation,
4877
4542
  "move.block down": moveBlockDownActionImplementation,
4878
4543
  "move.block up": moveBlockUpActionImplementation,
4879
4544
  noop: noopActionImplementation,
4880
- select: selectActionImplementation,
4881
- "select.previous block": selectPreviousBlockActionImplementation,
4882
- "select.next block": selectNextBlockActionImplementation,
4883
- "serialization.failure": serializationFailureActionImplementation,
4884
- "serialization.success": serializationSuccessActionImplementation,
4885
- "style.toggle": toggleStyleActionImplementation,
4886
- "style.add": addStyleActionImplementation,
4887
- "style.remove": removeStyleActionImplementation
4545
+ select: selectActionImplementation
4888
4546
  };
4889
4547
  function performAction({
4890
4548
  context,
@@ -4905,13 +4563,6 @@ function performAction({
4905
4563
  });
4906
4564
  break;
4907
4565
  }
4908
- case "annotation.toggle": {
4909
- behaviorActionImplementations["annotation.toggle"]({
4910
- context,
4911
- action
4912
- });
4913
- break;
4914
- }
4915
4566
  case "block.set": {
4916
4567
  behaviorActionImplementations["block.set"]({
4917
4568
  context,
@@ -4933,13 +4584,6 @@ function performAction({
4933
4584
  });
4934
4585
  break;
4935
4586
  }
4936
- case "data transfer.set": {
4937
- behaviorActionImplementations["data transfer.set"]({
4938
- context,
4939
- action
4940
- });
4941
- break;
4942
- }
4943
4587
  case "decorator.add": {
4944
4588
  behaviorActionImplementations["decorator.add"]({
4945
4589
  context,
@@ -4954,13 +4598,6 @@ function performAction({
4954
4598
  });
4955
4599
  break;
4956
4600
  }
4957
- case "decorator.toggle": {
4958
- behaviorActionImplementations["decorator.toggle"]({
4959
- context,
4960
- action
4961
- });
4962
- break;
4963
- }
4964
4601
  case "delete": {
4965
4602
  behaviorActionImplementations.delete({
4966
4603
  context,
@@ -4996,20 +4633,6 @@ function performAction({
4996
4633
  });
4997
4634
  break;
4998
4635
  }
4999
- case "deserialization.failure": {
5000
- behaviorActionImplementations["deserialization.failure"]({
5001
- context,
5002
- action
5003
- });
5004
- break;
5005
- }
5006
- case "deserialization.success": {
5007
- behaviorActionImplementations["deserialization.success"]({
5008
- context,
5009
- action
5010
- });
5011
- break;
5012
- }
5013
4636
  case "effect": {
5014
4637
  behaviorActionImplementations.effect({
5015
4638
  context,
@@ -5045,13 +4668,6 @@ function performAction({
5045
4668
  });
5046
4669
  break;
5047
4670
  }
5048
- case "insert.blocks": {
5049
- behaviorActionImplementations["insert.blocks"]({
5050
- context,
5051
- action
5052
- });
5053
- break;
5054
- }
5055
4671
  case "insert.inline object": {
5056
4672
  behaviorActionImplementations["insert.inline object"]({
5057
4673
  context,
@@ -5087,27 +4703,6 @@ function performAction({
5087
4703
  });
5088
4704
  break;
5089
4705
  }
5090
- case "list item.add": {
5091
- behaviorActionImplementations["list item.add"]({
5092
- context,
5093
- action
5094
- });
5095
- break;
5096
- }
5097
- case "list item.remove": {
5098
- behaviorActionImplementations["list item.remove"]({
5099
- context,
5100
- action
5101
- });
5102
- break;
5103
- }
5104
- case "list item.toggle": {
5105
- behaviorActionImplementations["list item.toggle"]({
5106
- context,
5107
- action
5108
- });
5109
- break;
5110
- }
5111
4706
  case "move.block": {
5112
4707
  behaviorActionImplementations["move.block"]({
5113
4708
  context,
@@ -5131,57 +4726,8 @@ function performAction({
5131
4726
  }
5132
4727
  case "noop":
5133
4728
  break;
5134
- case "select": {
5135
- behaviorActionImplementations.select({
5136
- context,
5137
- action
5138
- });
5139
- break;
5140
- }
5141
- case "select.previous block": {
5142
- behaviorActionImplementations["select.previous block"]({
5143
- context,
5144
- action
5145
- });
5146
- break;
5147
- }
5148
- case "select.next block": {
5149
- behaviorActionImplementations["select.next block"]({
5150
- context,
5151
- action
5152
- });
5153
- break;
5154
- }
5155
- case "serialization.failure": {
5156
- behaviorActionImplementations["serialization.failure"]({
5157
- context,
5158
- action
5159
- });
5160
- break;
5161
- }
5162
- case "serialization.success": {
5163
- behaviorActionImplementations["serialization.success"]({
5164
- context,
5165
- action
5166
- });
5167
- break;
5168
- }
5169
- case "style.add": {
5170
- behaviorActionImplementations["style.add"]({
5171
- context,
5172
- action
5173
- });
5174
- break;
5175
- }
5176
- case "style.remove": {
5177
- behaviorActionImplementations["style.remove"]({
5178
- context,
5179
- action
5180
- });
5181
- break;
5182
- }
5183
4729
  default: {
5184
- behaviorActionImplementations["style.toggle"]({
4730
+ behaviorActionImplementations.select({
5185
4731
  context,
5186
4732
  action
5187
4733
  });
@@ -6090,7 +5636,7 @@ const keyIs = {
6090
5636
  actions: [() => [raise({
6091
5637
  type: "insert.soft break"
6092
5638
  })]]
6093
- }), toggleAnnotationOff = defineBehavior({
5639
+ }), internalAnnotationBehaviors = [defineBehavior({
6094
5640
  on: "annotation.toggle",
6095
5641
  guard: ({
6096
5642
  snapshot,
@@ -6102,7 +5648,7 @@ const keyIs = {
6102
5648
  type: "annotation.remove",
6103
5649
  annotation: event.annotation
6104
5650
  })]]
6105
- }), toggleAnnotationOn = defineBehavior({
5651
+ }), defineBehavior({
6106
5652
  on: "annotation.toggle",
6107
5653
  guard: ({
6108
5654
  snapshot,
@@ -6114,7 +5660,7 @@ const keyIs = {
6114
5660
  type: "annotation.add",
6115
5661
  annotation: event.annotation
6116
5662
  })]]
6117
- }), toggleDecoratorOff = defineBehavior({
5663
+ })], internalDecoratorBehaviors = [defineBehavior({
6118
5664
  on: "decorator.toggle",
6119
5665
  guard: ({
6120
5666
  snapshot,
@@ -6126,7 +5672,7 @@ const keyIs = {
6126
5672
  type: "decorator.remove",
6127
5673
  decorator: event.decorator
6128
5674
  })]]
6129
- }), toggleDecoratorOn = defineBehavior({
5675
+ }), defineBehavior({
6130
5676
  on: "decorator.toggle",
6131
5677
  guard: ({
6132
5678
  snapshot,
@@ -6150,7 +5696,116 @@ const keyIs = {
6150
5696
  ...event,
6151
5697
  type: "decorator.add"
6152
5698
  })]]
6153
- }), toggleListItemOff = defineBehavior({
5699
+ })], internalInsertBehaviors = [defineBehavior({
5700
+ on: "insert.blocks",
5701
+ guard: ({
5702
+ event
5703
+ }) => event.placement === "before",
5704
+ actions: [({
5705
+ event
5706
+ }) => event.blocks.map((block, index) => raise({
5707
+ type: "insert.block",
5708
+ block,
5709
+ placement: index === 0 ? "before" : "after",
5710
+ select: "end"
5711
+ }))]
5712
+ }), defineBehavior({
5713
+ on: "insert.blocks",
5714
+ guard: ({
5715
+ event
5716
+ }) => event.placement === "after",
5717
+ actions: [({
5718
+ event
5719
+ }) => event.blocks.map((block) => raise({
5720
+ type: "insert.block",
5721
+ block,
5722
+ placement: "after",
5723
+ select: "end"
5724
+ }))]
5725
+ }), defineBehavior({
5726
+ on: "insert.blocks",
5727
+ guard: ({
5728
+ snapshot,
5729
+ event
5730
+ }) => !(event.placement !== "auto" || !getFocusTextBlock(snapshot)),
5731
+ actions: [({
5732
+ event
5733
+ }) => event.blocks.length === 1 ? [raise({
5734
+ type: "insert.block",
5735
+ block: event.blocks[0],
5736
+ placement: "auto",
5737
+ select: "end"
5738
+ })] : event.blocks.flatMap((block, index) => index === 0 ? [raise({
5739
+ type: "insert.break"
5740
+ }), raise({
5741
+ type: "select.previous block",
5742
+ select: "end"
5743
+ }), raise({
5744
+ type: "insert.block",
5745
+ block,
5746
+ placement: "auto",
5747
+ select: "end"
5748
+ })] : index === event.blocks.length - 1 ? [raise({
5749
+ type: "select.next block",
5750
+ select: "start"
5751
+ }), raise({
5752
+ type: "insert.block",
5753
+ block,
5754
+ placement: "auto",
5755
+ select: "end"
5756
+ })] : [raise({
5757
+ type: "insert.block",
5758
+ block,
5759
+ placement: "after",
5760
+ select: "end"
5761
+ })])]
5762
+ }), defineBehavior({
5763
+ on: "insert.blocks",
5764
+ guard: ({
5765
+ event
5766
+ }) => event.placement === "auto",
5767
+ actions: [({
5768
+ event
5769
+ }) => event.blocks.map((block, index) => raise({
5770
+ type: "insert.block",
5771
+ block,
5772
+ placement: index === 0 ? "auto" : "after",
5773
+ select: "end"
5774
+ }))]
5775
+ })], internalListItemBehaviors = [defineBehavior({
5776
+ on: "list item.add",
5777
+ guard: ({
5778
+ snapshot
5779
+ }) => ({
5780
+ selectedTextBlocks: getSelectedTextBlocks(snapshot)
5781
+ }),
5782
+ actions: [({
5783
+ event
5784
+ }, {
5785
+ selectedTextBlocks
5786
+ }) => selectedTextBlocks.map((block) => raise({
5787
+ type: "block.set",
5788
+ at: block.path,
5789
+ props: {
5790
+ level: 1,
5791
+ listItem: event.listItem
5792
+ }
5793
+ }))]
5794
+ }), defineBehavior({
5795
+ on: "list item.remove",
5796
+ guard: ({
5797
+ snapshot
5798
+ }) => ({
5799
+ selectedTextBlocks: getSelectedTextBlocks(snapshot)
5800
+ }),
5801
+ actions: [(_, {
5802
+ selectedTextBlocks
5803
+ }) => selectedTextBlocks.map((block) => raise({
5804
+ type: "block.unset",
5805
+ at: block.path,
5806
+ props: ["level", "listItem"]
5807
+ }))]
5808
+ }), defineBehavior({
6154
5809
  on: "list item.toggle",
6155
5810
  guard: ({
6156
5811
  snapshot,
@@ -6162,7 +5817,7 @@ const keyIs = {
6162
5817
  type: "list item.remove",
6163
5818
  listItem: event.listItem
6164
5819
  })]]
6165
- }), toggleListItemOn = defineBehavior({
5820
+ }), defineBehavior({
6166
5821
  on: "list item.toggle",
6167
5822
  guard: ({
6168
5823
  snapshot,
@@ -6174,7 +5829,85 @@ const keyIs = {
6174
5829
  type: "list item.add",
6175
5830
  listItem: event.listItem
6176
5831
  })]]
6177
- }), toggleStyleOff = defineBehavior({
5832
+ })], internalSelectBehaviors = [defineBehavior({
5833
+ on: "select.previous block",
5834
+ guard: ({
5835
+ snapshot,
5836
+ event
5837
+ }) => {
5838
+ const previousBlock = getPreviousBlock(snapshot);
5839
+ if (!previousBlock)
5840
+ return !1;
5841
+ const point = event.select === "end" ? getBlockEndPoint(previousBlock) : getBlockStartPoint(previousBlock);
5842
+ return {
5843
+ selection: {
5844
+ anchor: point,
5845
+ focus: point
5846
+ }
5847
+ };
5848
+ },
5849
+ actions: [(_, {
5850
+ selection
5851
+ }) => [raise({
5852
+ type: "select",
5853
+ selection
5854
+ })]]
5855
+ }), defineBehavior({
5856
+ on: "select.next block",
5857
+ guard: ({
5858
+ snapshot,
5859
+ event
5860
+ }) => {
5861
+ const nextBlock = getNextBlock(snapshot);
5862
+ if (!nextBlock)
5863
+ return !1;
5864
+ const point = event.select === "end" ? getBlockEndPoint(nextBlock) : getBlockStartPoint(nextBlock);
5865
+ return {
5866
+ selection: {
5867
+ anchor: point,
5868
+ focus: point
5869
+ }
5870
+ };
5871
+ },
5872
+ actions: [(_, {
5873
+ selection
5874
+ }) => [raise({
5875
+ type: "select",
5876
+ selection
5877
+ })]]
5878
+ })], internalStyleBehaviors = [defineBehavior({
5879
+ on: "style.add",
5880
+ guard: ({
5881
+ snapshot
5882
+ }) => ({
5883
+ selectedTextBlocks: getSelectedTextBlocks(snapshot)
5884
+ }),
5885
+ actions: [({
5886
+ event
5887
+ }, {
5888
+ selectedTextBlocks
5889
+ }) => selectedTextBlocks.map((block) => raise({
5890
+ type: "block.set",
5891
+ at: block.path,
5892
+ props: {
5893
+ style: event.style
5894
+ }
5895
+ }))]
5896
+ }), defineBehavior({
5897
+ on: "style.remove",
5898
+ guard: ({
5899
+ snapshot
5900
+ }) => ({
5901
+ selectedTextBlocks: getSelectedTextBlocks(snapshot)
5902
+ }),
5903
+ actions: [(_, {
5904
+ selectedTextBlocks
5905
+ }) => selectedTextBlocks.map((block) => raise({
5906
+ type: "block.unset",
5907
+ at: block.path,
5908
+ props: ["style"]
5909
+ }))]
5910
+ }), defineBehavior({
6178
5911
  on: "style.toggle",
6179
5912
  guard: ({
6180
5913
  snapshot,
@@ -6186,7 +5919,7 @@ const keyIs = {
6186
5919
  type: "style.remove",
6187
5920
  style: event.style
6188
5921
  })]]
6189
- }), toggleStyleOn = defineBehavior({
5922
+ }), defineBehavior({
6190
5923
  on: "style.toggle",
6191
5924
  guard: ({
6192
5925
  snapshot,
@@ -6198,7 +5931,7 @@ const keyIs = {
6198
5931
  type: "style.add",
6199
5932
  style: event.style
6200
5933
  })]]
6201
- }), raiseDeserializationSuccessOrFailure = defineBehavior({
5934
+ })], raiseDeserializationSuccessOrFailure = defineBehavior({
6202
5935
  on: "deserialize",
6203
5936
  guard: ({
6204
5937
  snapshot,
@@ -6315,12 +6048,23 @@ const keyIs = {
6315
6048
  on: "serialization.success",
6316
6049
  actions: [({
6317
6050
  event
6318
- }) => [raise({
6319
- type: "data transfer.set",
6320
- data: event.data,
6321
- dataTransfer: event.originEvent.originEvent.dataTransfer,
6322
- mimeType: event.mimeType
6323
- })]]
6051
+ }) => [{
6052
+ type: "effect",
6053
+ effect: () => {
6054
+ event.originEvent.originEvent.dataTransfer.setData(event.mimeType, event.data);
6055
+ }
6056
+ }]]
6057
+ }),
6058
+ defineBehavior({
6059
+ on: "serialization.failure",
6060
+ actions: [({
6061
+ event
6062
+ }) => [{
6063
+ type: "effect",
6064
+ effect: () => {
6065
+ console.warn(`Serialization of ${event.mimeType} failed with reason "${event.reason}"`);
6066
+ }
6067
+ }]]
6324
6068
  }),
6325
6069
  defineBehavior({
6326
6070
  on: "drag.drop",
@@ -6467,6 +6211,17 @@ const keyIs = {
6467
6211
  placement: "auto"
6468
6212
  })]]
6469
6213
  }),
6214
+ defineBehavior({
6215
+ on: "deserialization.failure",
6216
+ actions: [({
6217
+ event
6218
+ }) => [{
6219
+ type: "effect",
6220
+ effect: () => {
6221
+ console.warn(`Deserialization of ${event.mimeType} failed with reason "${event.reason}"`);
6222
+ }
6223
+ }]]
6224
+ }),
6470
6225
  defineBehavior({
6471
6226
  on: "clipboard.paste",
6472
6227
  guard: ({
@@ -6504,14 +6259,12 @@ const keyIs = {
6504
6259
  originEvent: event
6505
6260
  })]]
6506
6261
  }),
6507
- toggleAnnotationOff,
6508
- toggleAnnotationOn,
6509
- toggleDecoratorOff,
6510
- toggleDecoratorOn,
6511
- toggleListItemOff,
6512
- toggleListItemOn,
6513
- toggleStyleOff,
6514
- toggleStyleOn,
6262
+ ...internalAnnotationBehaviors,
6263
+ ...internalDecoratorBehaviors,
6264
+ ...internalInsertBehaviors,
6265
+ ...internalListItemBehaviors,
6266
+ ...internalStyleBehaviors,
6267
+ ...internalSelectBehaviors,
6515
6268
  raiseDeserializationSuccessOrFailure,
6516
6269
  raiseSerializationSuccessOrFailure,
6517
6270
  raiseInsertSoftBreak
@@ -6519,6 +6272,12 @@ const keyIs = {
6519
6272
  function isKeyboardBehaviorEvent(event) {
6520
6273
  return event.type.startsWith("keyboard.");
6521
6274
  }
6275
+ function isInternalBehaviorEvent(event) {
6276
+ return event.type === "deserialize" || event.type.startsWith("deserialization.") || event.type === "insert.blocks" || event.type.startsWith("list item.") || event.type === "serialize" || event.type.startsWith("serialization.") || event.type === "select.next block" || event.type === "select.previous block" || event.type.startsWith("style.");
6277
+ }
6278
+ function isNativeBehaviorEvent(event) {
6279
+ return isClipboardBehaviorEvent(event) || isDragBehaviorEvent(event) || isInputBehaviorEvent(event) || isKeyboardBehaviorEvent(event) || isMouseBehaviorEvent(event);
6280
+ }
6522
6281
  function isClipboardBehaviorEvent(event) {
6523
6282
  return event.type.startsWith("clipboard.");
6524
6283
  }
@@ -6535,6 +6294,9 @@ function isCustomBehaviorEvent(event) {
6535
6294
  return event.type.startsWith("custom.");
6536
6295
  }
6537
6296
  const debug$2 = debugWithName("behaviors:event");
6297
+ function eventCategory(event) {
6298
+ return isNativeBehaviorEvent(event) ? "native" : isInternalBehaviorEvent(event) ? "internal" : isCustomBehaviorEvent(event) ? "custom" : "synthetic";
6299
+ }
6538
6300
  function performEvent({
6539
6301
  behaviors,
6540
6302
  event,
@@ -6545,8 +6307,8 @@ function performEvent({
6545
6307
  nativeEvent,
6546
6308
  defaultActionCallback
6547
6309
  }) {
6548
- debug$2(JSON.stringify(event, null, 2));
6549
- const defaultAction = isCustomBehaviorEvent(event) || isClipboardBehaviorEvent(event) || isDragBehaviorEvent(event) || isInputBehaviorEvent(event) || isKeyboardBehaviorEvent(event) || isMouseBehaviorEvent(event) || event.type === "deserialize" || event.type === "serialize" ? void 0 : {
6310
+ debug$2(`(${eventCategory(event)})`, JSON.stringify(event, null, 2));
6311
+ const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isInternalBehaviorEvent(event) ? void 0 : {
6550
6312
  ...event,
6551
6313
  editor
6552
6314
  }, eventBehaviors = behaviors.filter((behavior) => {
@@ -6938,7 +6700,7 @@ const editorMachine = setup({
6938
6700
  actions: "handle behavior event",
6939
6701
  guard: ({
6940
6702
  event
6941
- }) => event.behaviorEvent.type === "clipboard.copy" || event.behaviorEvent.type === "data transfer.set" || event.behaviorEvent.type === "serialize" || event.behaviorEvent.type === "serialization.failure" || event.behaviorEvent.type === "serialization.success" || event.behaviorEvent.type === "select"
6703
+ }) => event.behaviorEvent.type === "clipboard.copy" || event.behaviorEvent.type === "serialize" || event.behaviorEvent.type === "serialization.failure" || event.behaviorEvent.type === "serialization.success" || event.behaviorEvent.type === "select"
6942
6704
  }
6943
6705
  },
6944
6706
  states: {