@portabletext/editor 1.43.1 → 1.44.0

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 (72) hide show
  1. package/lib/_chunks-cjs/behavior.markdown.cjs +1 -1
  2. package/lib/_chunks-cjs/editor-provider.cjs +444 -622
  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 +433 -610
  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 +1860 -5464
  27. package/lib/behaviors/index.d.ts +1860 -5464
  28. package/lib/index.cjs +4 -4
  29. package/lib/index.cjs.map +1 -1
  30. package/lib/index.d.cts +1529 -5062
  31. package/lib/index.d.ts +1529 -5062
  32. package/lib/index.js +3 -3
  33. package/lib/plugins/index.cjs +1 -1
  34. package/lib/plugins/index.d.cts +1529 -5062
  35. package/lib/plugins/index.d.ts +1529 -5062
  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 +1508 -5064
  40. package/lib/selectors/index.d.ts +1508 -5064
  41. package/lib/selectors/index.js +2 -1
  42. package/lib/utils/index.d.cts +1491 -5057
  43. package/lib/utils/index.d.ts +1491 -5057
  44. package/package.json +1 -1
  45. package/src/behavior-actions/behavior.actions.ts +1 -131
  46. package/src/behaviors/behavior.default.ts +47 -50
  47. package/src/behaviors/behavior.internal.insert.ts +118 -0
  48. package/src/behaviors/behavior.internal.list-item.ts +61 -0
  49. package/src/behaviors/behavior.internal.select.ts +62 -0
  50. package/src/behaviors/behavior.internal.style.ts +54 -0
  51. package/src/behaviors/behavior.perform-event.ts +15 -13
  52. package/src/behaviors/behavior.types.event.ts +143 -100
  53. package/src/editor/create-editor.ts +2 -2
  54. package/src/editor/editor-machine.ts +3 -4
  55. package/src/editor/plugins/createWithEditableAPI.ts +1 -2
  56. package/src/internal-utils/slate-utils.ts +52 -0
  57. package/src/plugins/plugin.internal.editor-actor-ref.tsx +15 -0
  58. package/src/plugins/plugin.internal.slate-editor-ref.tsx +15 -0
  59. package/src/selectors/index.ts +2 -1
  60. package/src/selectors/selector.get-selected-text-blocks.ts +67 -0
  61. package/lib/_chunks-cjs/selector.is-active-style.cjs.map +0 -1
  62. package/lib/_chunks-es/selector.is-active-style.js.map +0 -1
  63. package/src/behavior-actions/behavior.action.data-transfer-set.ts +0 -7
  64. package/src/behavior-actions/behavior.action.deserialization.failure.ts +0 -9
  65. package/src/behavior-actions/behavior.action.deserialization.success.ts +0 -16
  66. package/src/behavior-actions/behavior.action.insert-blocks.ts +0 -140
  67. package/src/behavior-actions/behavior.action.list-item.ts +0 -100
  68. package/src/behavior-actions/behavior.action.select.next-block.ts +0 -44
  69. package/src/behavior-actions/behavior.action.select.previous-block.ts +0 -48
  70. package/src/behavior-actions/behavior.action.serialization.failure.ts +0 -9
  71. package/src/behavior-actions/behavior.action.serialization.success.ts +0 -17
  72. package/src/behavior-actions/behavior.action.style.ts +0 -108
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- var jsxRuntime = require("react/jsx-runtime"), reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), slateReact = require("slate-react"), react = require("@xstate/react"), debug$g = require("debug"), isEqual = require("lodash/isEqual.js"), slate = require("slate"), xstate = require("xstate"), patches = require("@portabletext/patches"), types = require("@sanity/types"), flatten = require("lodash/flatten.js"), isPlainObject = require("lodash/isPlainObject.js"), uniq = require("lodash/uniq.js"), getRandomValues = require("get-random-values-esm"), util_selectionPointToBlockOffset = require("./util.selection-point-to-block-offset.cjs"), util_sliceBlocks = require("./util.slice-blocks.cjs"), blockTools = require("@portabletext/block-tools"), toHtml = require("@portabletext/to-html"), schema = require("@sanity/schema"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), omit = require("lodash/omit.js"), selector_isOverlappingSelection = require("./selector.is-overlapping-selection.cjs"), selector_isActiveStyle = require("./selector.is-active-style.cjs"), slateDom = require("slate-dom"), startCase = require("lodash.startcase"), behavior_core = require("./behavior.core.cjs"), rxjs = require("rxjs"), useEffectEvent = require("use-effect-event");
2
+ var jsxRuntime = require("react/jsx-runtime"), reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), slateReact = require("slate-react"), react = require("@xstate/react"), debug$g = require("debug"), isEqual = require("lodash/isEqual.js"), slate = require("slate"), xstate = require("xstate"), patches = require("@portabletext/patches"), types = require("@sanity/types"), flatten = require("lodash/flatten.js"), isPlainObject = require("lodash/isPlainObject.js"), uniq = require("lodash/uniq.js"), getRandomValues = require("get-random-values-esm"), parseBlocks = require("./parse-blocks.cjs"), util_sliceBlocks = require("./util.slice-blocks.cjs"), blockTools = require("@portabletext/block-tools"), toHtml = require("@portabletext/to-html"), schema = require("@sanity/schema"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), omit = require("lodash/omit.js"), util_selectionPointToBlockOffset = require("./util.selection-point-to-block-offset.cjs"), selector_isSelectingEntireBlocks = require("./selector.is-selecting-entire-blocks.cjs"), slateDom = require("slate-dom"), startCase = require("lodash.startcase"), behavior_core = require("./behavior.core.cjs"), selector_isOverlappingSelection = require("./selector.is-overlapping-selection.cjs"), rxjs = require("rxjs"), useEffectEvent = require("use-effect-event");
3
3
  function _interopDefaultCompat(e) {
4
4
  return e && typeof e == "object" && "default" in e ? e : { default: e };
5
5
  }
@@ -383,6 +383,30 @@ function isBlockElement({
383
383
  }, node) {
384
384
  return slate.Element.isElement(node) && !editor.isInline(node) && (schema2.block.name === node._type || schema2.blockObjects.some((blockObject) => blockObject.name === node._type));
385
385
  }
386
+ function isListItemActive({
387
+ editor,
388
+ listItem
389
+ }) {
390
+ if (!editor.selection)
391
+ return !1;
392
+ const selectedBlocks = [...slate.Editor.nodes(editor, {
393
+ at: editor.selection,
394
+ match: (node) => editor.isTextBlock(node)
395
+ })];
396
+ return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => editor.isListBlock(node) && node.listItem === listItem) : !1;
397
+ }
398
+ function isStyleActive({
399
+ editor,
400
+ style
401
+ }) {
402
+ if (!editor.selection)
403
+ return !1;
404
+ const selectedBlocks = [...slate.Editor.nodes(editor, {
405
+ at: editor.selection,
406
+ match: (node) => editor.isTextBlock(node)
407
+ })];
408
+ return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => node.style === style) : !1;
409
+ }
386
410
  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 = React.createContext({}), PortableTextEditorContext = React.createContext(null), usePortableTextEditor = () => {
387
411
  const editor = React.useContext(PortableTextEditorContext);
388
412
  if (!editor)
@@ -2517,7 +2541,7 @@ const converterJson = {
2517
2541
  reason: "Data is not an array"
2518
2542
  };
2519
2543
  const parsedBlocks = blocks.flatMap((block) => {
2520
- const parsedBlock = util_selectionPointToBlockOffset.parseBlock({
2544
+ const parsedBlock = parseBlocks.parseBlock({
2521
2545
  context: snapshot.context,
2522
2546
  block,
2523
2547
  options: {
@@ -2939,131 +2963,7 @@ const insertBreakActionImplementation = ({
2939
2963
  }) => {
2940
2964
  slate.insertText(action.editor, `
2941
2965
  `);
2942
- }, toggleListItemActionImplementation = ({
2943
- context,
2944
- action
2945
- }) => {
2946
- isListItemActive({
2947
- editor: action.editor,
2948
- listItem: action.listItem
2949
- }) ? removeListItemActionImplementation({
2950
- context,
2951
- action: {
2952
- ...action
2953
- }
2954
- }) : addListItemActionImplementation({
2955
- context,
2956
- action: {
2957
- ...action
2958
- }
2959
- });
2960
- }, removeListItemActionImplementation = ({
2961
- context,
2962
- action
2963
- }) => {
2964
- if (!action.editor.selection)
2965
- return;
2966
- const guards = selector_isOverlappingSelection.createGuards(context), selectedBlocks = [...slate.Editor.nodes(action.editor, {
2967
- at: action.editor.selection,
2968
- match: (node) => guards.isListBlock(node)
2969
- })];
2970
- for (const [, at] of selectedBlocks)
2971
- slate.Transforms.unsetNodes(action.editor, ["listItem", "level"], {
2972
- at
2973
- });
2974
- }, addListItemActionImplementation = ({
2975
- context,
2976
- action
2977
- }) => {
2978
- if (!action.editor.selection)
2979
- return;
2980
- const guards = selector_isOverlappingSelection.createGuards(context), selectedBlocks = [...slate.Editor.nodes(action.editor, {
2981
- at: action.editor.selection,
2982
- match: (node) => guards.isTextBlock(node)
2983
- })];
2984
- for (const [, at] of selectedBlocks)
2985
- slate.Transforms.setNodes(action.editor, {
2986
- level: 1,
2987
- listItem: action.listItem
2988
- }, {
2989
- at
2990
- });
2991
- };
2992
- function isListItemActive({
2993
- editor,
2994
- listItem
2995
- }) {
2996
- if (!editor.selection)
2997
- return !1;
2998
- const selectedBlocks = [...slate.Editor.nodes(editor, {
2999
- at: editor.selection,
3000
- match: (node) => editor.isTextBlock(node)
3001
- })];
3002
- return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => editor.isListBlock(node) && node.listItem === listItem) : !1;
3003
- }
3004
- const toggleStyleActionImplementation = ({
3005
- context,
3006
- action
3007
- }) => {
3008
- isStyleActive({
3009
- editor: action.editor,
3010
- style: action.style
3011
- }) ? removeStyleActionImplementation({
3012
- context,
3013
- action: {
3014
- ...action
3015
- }
3016
- }) : addStyleActionImplementation({
3017
- context,
3018
- action: {
3019
- ...action
3020
- }
3021
- });
3022
- }, removeStyleActionImplementation = ({
3023
- context,
3024
- action
3025
- }) => {
3026
- if (!action.editor.selection)
3027
- return;
3028
- const defaultStyle = context.schema.styles[0].value, guards = selector_isOverlappingSelection.createGuards(context), selectedBlocks = [...slate.Editor.nodes(action.editor, {
3029
- at: action.editor.selection,
3030
- match: (node) => guards.isTextBlock(node)
3031
- })];
3032
- for (const [, at] of selectedBlocks)
3033
- slate.Transforms.setNodes(action.editor, {
3034
- style: defaultStyle
3035
- }, {
3036
- at
3037
- });
3038
- }, addStyleActionImplementation = ({
3039
- context,
3040
- action
3041
- }) => {
3042
- if (!action.editor.selection)
3043
- return;
3044
- const guards = selector_isOverlappingSelection.createGuards(context), selectedBlocks = [...slate.Editor.nodes(action.editor, {
3045
- at: action.editor.selection,
3046
- match: (node) => guards.isTextBlock(node)
3047
- })];
3048
- for (const [, at] of selectedBlocks)
3049
- slate.Transforms.setNodes(action.editor, {
3050
- style: action.style
3051
- }, {
3052
- at
3053
- });
3054
2966
  };
3055
- function isStyleActive({
3056
- editor,
3057
- style
3058
- }) {
3059
- if (!editor.selection)
3060
- return !1;
3061
- const selectedBlocks = [...slate.Editor.nodes(editor, {
3062
- at: editor.selection,
3063
- match: (node) => editor.isTextBlock(node)
3064
- })];
3065
- return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => node.style === style) : !1;
3066
- }
3067
2967
  function isPortableTextSpan(node) {
3068
2968
  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"));
3069
2969
  }
@@ -3120,7 +3020,7 @@ const decoratorAddActionImplementation = ({
3120
3020
  focus: focusOffset
3121
3021
  },
3122
3022
  backward: editorSelection?.backward
3123
- }), trimmedSelection = selector_isActiveStyle.getTrimmedSelection({
3023
+ }), trimmedSelection = selector_isSelectingEntireBlocks.getTrimmedSelection({
3124
3024
  context: {
3125
3025
  activeDecorators: [],
3126
3026
  converters: [],
@@ -4182,7 +4082,7 @@ const addAnnotationActionImplementation = ({
4182
4082
  const {
4183
4083
  _type,
4184
4084
  ...filteredProps
4185
- } = action.props, updatedBlock = util_selectionPointToBlockOffset.parseBlock({
4085
+ } = action.props, updatedBlock = parseBlocks.parseBlock({
4186
4086
  context,
4187
4087
  block: {
4188
4088
  ...parsedBlock,
@@ -4226,8 +4126,8 @@ const addAnnotationActionImplementation = ({
4226
4126
  const parsedBlock = fromSlateValue([block], context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(action.editor)).at(0);
4227
4127
  if (!parsedBlock)
4228
4128
  throw new Error(`Unable to parse block at ${JSON.stringify(action.at)}`);
4229
- if (util_selectionPointToBlockOffset.isTextBlock(context.schema, parsedBlock)) {
4230
- const propsToRemove = action.props.filter((prop) => prop !== "_type"), updatedTextBlock = util_selectionPointToBlockOffset.parseBlock({
4129
+ if (parseBlocks.isTextBlock(context.schema, parsedBlock)) {
4130
+ const propsToRemove = action.props.filter((prop) => prop !== "_type"), updatedTextBlock = parseBlocks.parseBlock({
4231
4131
  context,
4232
4132
  block: omit__default.default(parsedBlock, propsToRemove),
4233
4133
  options: {
@@ -4244,7 +4144,7 @@ const addAnnotationActionImplementation = ({
4244
4144
  });
4245
4145
  return;
4246
4146
  }
4247
- const updatedBlockObject = util_selectionPointToBlockOffset.parseBlock({
4147
+ const updatedBlockObject = parseBlocks.parseBlock({
4248
4148
  context,
4249
4149
  block: omit__default.default(parsedBlock, action.props.filter((prop) => prop !== "_type")),
4250
4150
  options: {
@@ -4269,10 +4169,6 @@ const addAnnotationActionImplementation = ({
4269
4169
  action
4270
4170
  }) => {
4271
4171
  slateReact.ReactEditor.blur(action.editor);
4272
- }, dataTransferSetActionImplementation = ({
4273
- action
4274
- }) => {
4275
- action.dataTransfer.setData(action.mimeType, action.data);
4276
4172
  }, deleteActionImplementation = ({
4277
4173
  action
4278
4174
  }) => {
@@ -4323,7 +4219,7 @@ const addAnnotationActionImplementation = ({
4323
4219
  });
4324
4220
  if (!selection)
4325
4221
  throw new Error("Unable to find selection from block offsets");
4326
- const trimmedSelection = selector_isActiveStyle.getTrimmedSelection({
4222
+ const trimmedSelection = selector_isSelectingEntireBlocks.getTrimmedSelection({
4327
4223
  context: {
4328
4224
  converters: [],
4329
4225
  schema: context.schema,
@@ -4342,15 +4238,84 @@ const addAnnotationActionImplementation = ({
4342
4238
  slate.Transforms.delete(action.editor, {
4343
4239
  at: range
4344
4240
  });
4345
- }, deserializationFailureActionImplementation = ({
4241
+ }, effectActionImplementation = ({
4242
+ action
4243
+ }) => {
4244
+ action.effect();
4245
+ }, focusActionImplementation = ({
4246
+ action
4247
+ }) => {
4248
+ slateReact.ReactEditor.focus(action.editor);
4249
+ }, insertInlineObjectActionImplementation = ({
4250
+ context,
4251
+ action
4252
+ }) => {
4253
+ if (!context.schema.inlineObjects.some((inlineObject) => inlineObject.name === action.inlineObject.name)) {
4254
+ console.error("Unable to insert unknown inline object");
4255
+ return;
4256
+ }
4257
+ if (!action.editor.selection) {
4258
+ console.error("Unable to insert inline object without selection");
4259
+ return;
4260
+ }
4261
+ const [focusTextBlock] = Array.from(slate.Editor.nodes(action.editor, {
4262
+ at: action.editor.selection.focus.path,
4263
+ match: (node) => action.editor.isTextBlock(node)
4264
+ })).at(0) ?? [void 0, void 0];
4265
+ if (!focusTextBlock) {
4266
+ console.error("Unable to perform action without focus text block");
4267
+ return;
4268
+ }
4269
+ const child = toSlateValue([{
4270
+ _type: context.schema.block.name,
4271
+ _key: context.keyGenerator(),
4272
+ children: [{
4273
+ _type: action.inlineObject.name,
4274
+ _key: context.keyGenerator(),
4275
+ ...action.inlineObject.value ?? {}
4276
+ }]
4277
+ }], {
4278
+ schemaTypes: context.schema
4279
+ }).at(0)?.children.at(0);
4280
+ if (!child) {
4281
+ console.error("Unable to insert inline object");
4282
+ return;
4283
+ }
4284
+ slate.Transforms.insertNodes(action.editor, child);
4285
+ }, insertSpanActionImplementation = ({
4286
+ context,
4346
4287
  action
4347
4288
  }) => {
4348
- console.warn(`Deserialization of ${action.mimeType} failed with reason "${action.reason}"`);
4289
+ if (!action.editor.selection) {
4290
+ console.error("Unable to perform action without selection", action);
4291
+ return;
4292
+ }
4293
+ const [focusBlock, focusBlockPath] = Array.from(slate.Editor.nodes(action.editor, {
4294
+ at: action.editor.selection.focus.path,
4295
+ match: (node) => action.editor.isTextBlock(node)
4296
+ }))[0] ?? [void 0, void 0];
4297
+ if (!focusBlock || !focusBlockPath) {
4298
+ console.error("Unable to perform action without focus block", action);
4299
+ return;
4300
+ }
4301
+ const markDefs = focusBlock.markDefs ?? [], annotations = action.annotations ? action.annotations.map((annotation) => ({
4302
+ _type: annotation.name,
4303
+ _key: context.keyGenerator(),
4304
+ ...annotation.value
4305
+ })) : void 0;
4306
+ annotations && annotations.length > 0 && slate.Transforms.setNodes(action.editor, {
4307
+ markDefs: [...markDefs, ...annotations]
4308
+ }), slate.Transforms.insertNodes(action.editor, {
4309
+ _type: "span",
4310
+ _key: context.keyGenerator(),
4311
+ text: action.text,
4312
+ marks: [...annotations?.map((annotation) => annotation._key) ?? [], ...action.decorators ?? []]
4313
+ });
4349
4314
  }, insertBlockActionImplementation = ({
4350
4315
  context,
4351
4316
  action
4352
4317
  }) => {
4353
- const parsedBlock = util_selectionPointToBlockOffset.parseBlock({
4318
+ const parsedBlock = parseBlocks.parseBlock({
4354
4319
  block: action.block,
4355
4320
  context,
4356
4321
  options: {
@@ -4491,331 +4456,82 @@ function insertBlock({
4491
4456
  }
4492
4457
  }
4493
4458
  }
4494
- const selectActionImplementation = ({
4459
+ const insertTextActionImplementation = ({
4495
4460
  action
4496
4461
  }) => {
4497
- const newSelection = toSlateRange(action.selection, action.editor);
4498
- newSelection ? slate.Transforms.select(action.editor, newSelection) : slate.Transforms.deselect(action.editor);
4499
- }, selectNextBlockActionImplementation = ({
4462
+ action.editor.insertText(action.text);
4463
+ }, moveBlockActionImplementation = ({
4464
+ action
4465
+ }) => {
4466
+ const at = [toSlatePath(action.at, action.editor)[0]], to = [toSlatePath(action.to, action.editor)[0]];
4467
+ slate.Transforms.moveNodes(action.editor, {
4468
+ at,
4469
+ to,
4470
+ mode: "highest"
4471
+ });
4472
+ }, moveBlockDownActionImplementation = ({
4500
4473
  context,
4501
4474
  action
4502
4475
  }) => {
4503
- if (!action.editor.selection) {
4504
- console.error("Unable to select previous block without a selection");
4505
- return;
4506
- }
4507
- const nextBlockPath = [action.editor.selection.focus.path.slice(0, 1)[0] + 1], position = action.select === "end" ? slate.Editor.end(action.editor, nextBlockPath) : slate.Editor.start(action.editor, nextBlockPath), newSelection = toPortableTextRange(action.editor.children, {
4508
- anchor: position,
4509
- focus: position
4510
- }, context.schema);
4511
- if (!newSelection) {
4512
- console.error("Could not find selection for next block");
4476
+ const at = [toSlatePath(action.at, action.editor)[0]], to = [slate.Path.next(at)[0]], selection = toPortableTextRange(action.editor.children, {
4477
+ anchor: {
4478
+ path: to,
4479
+ offset: 0
4480
+ },
4481
+ focus: {
4482
+ path: to,
4483
+ offset: 0
4484
+ }
4485
+ }, context.schema), destinationBlockKey = selection && util_sliceBlocks.isKeyedSegment(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4486
+ if (destinationBlockKey === void 0) {
4487
+ console.error("Could not find destination block key");
4513
4488
  return;
4514
4489
  }
4515
- selectActionImplementation({
4490
+ moveBlockActionImplementation({
4516
4491
  action: {
4517
- selection: newSelection,
4492
+ at: action.at,
4493
+ to: [{
4494
+ _key: destinationBlockKey
4495
+ }],
4518
4496
  editor: action.editor
4519
4497
  }
4520
4498
  });
4521
- }, selectPreviousBlockActionImplementation = ({
4499
+ }, moveBlockUpActionImplementation = ({
4522
4500
  context,
4523
4501
  action
4524
4502
  }) => {
4525
- if (!action.editor.selection) {
4526
- console.error("Unable to select previous block without a selection");
4527
- return;
4528
- }
4529
- const blockPath = action.editor.selection.focus.path.slice(0, 1);
4530
- if (!slate.Path.hasPrevious(blockPath)) {
4531
- console.error("There's no previous block to select");
4503
+ const at = [toSlatePath(action.at, action.editor)[0]];
4504
+ if (!slate.Path.hasPrevious(at))
4532
4505
  return;
4533
- }
4534
- const position = action.select === "end" ? slate.Editor.end(action.editor, slate.Path.previous(blockPath)) : slate.Editor.start(action.editor, slate.Path.previous(blockPath)), newSelection = toPortableTextRange(action.editor.children, {
4535
- anchor: position,
4536
- focus: position
4537
- }, context.schema);
4538
- if (!newSelection) {
4539
- console.error("Could not find selection for previous block");
4506
+ const to = [slate.Path.previous(at)[0]], selection = toPortableTextRange(action.editor.children, {
4507
+ anchor: {
4508
+ path: to,
4509
+ offset: 0
4510
+ },
4511
+ focus: {
4512
+ path: to,
4513
+ offset: 0
4514
+ }
4515
+ }, context.schema), destinationBlockKey = selection && util_sliceBlocks.isKeyedSegment(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4516
+ if (destinationBlockKey === void 0) {
4517
+ console.error("Could not find destination block key");
4540
4518
  return;
4541
4519
  }
4542
- selectActionImplementation({
4520
+ moveBlockActionImplementation({
4543
4521
  action: {
4544
- selection: newSelection,
4522
+ at: action.at,
4523
+ to: [{
4524
+ _key: destinationBlockKey
4525
+ }],
4545
4526
  editor: action.editor
4546
4527
  }
4547
4528
  });
4548
- }, insertBlocksActionImplementation = ({
4549
- context,
4529
+ }, noopActionImplementation = () => {
4530
+ }, selectActionImplementation = ({
4550
4531
  action
4551
4532
  }) => {
4552
- const parsedBlocks = util_selectionPointToBlockOffset.parseBlocks({
4553
- context,
4554
- blocks: action.blocks,
4555
- options: {
4556
- refreshKeys: !1
4557
- }
4558
- });
4559
- if (parsedBlocks.length === 0)
4560
- throw new Error(`Failed to parse blocks ${JSON.stringify(action.blocks)}`);
4561
- const fragment = toSlateValue(parsedBlocks, {
4562
- schemaTypes: context.schema
4563
- });
4564
- if (fragment.length === 0)
4565
- throw new Error(`Failed to convert blocks to Slate fragment ${JSON.stringify(parsedBlocks)}`);
4566
- const [focusBlock] = getFocusBlock({
4567
- editor: action.editor
4568
- });
4569
- if (action.placement === "before") {
4570
- let index = 0;
4571
- for (const block of fragment)
4572
- insertBlock({
4573
- block,
4574
- placement: index === 0 ? "before" : "after",
4575
- select: "end",
4576
- editor: action.editor,
4577
- schema: context.schema
4578
- }), index++;
4579
- } else if (action.placement === "after")
4580
- for (const block of fragment)
4581
- insertBlock({
4582
- block,
4583
- placement: "after",
4584
- select: "end",
4585
- editor: action.editor,
4586
- schema: context.schema
4587
- });
4588
- else if (focusBlock && action.editor.isTextBlock(focusBlock)) {
4589
- if (fragment.length === 1) {
4590
- insertBlock({
4591
- block: fragment[0],
4592
- placement: "auto",
4593
- select: "end",
4594
- editor: action.editor,
4595
- schema: context.schema
4596
- });
4597
- return;
4598
- }
4599
- let index = 0;
4600
- for (const block of fragment)
4601
- index === 0 ? (insertBreakActionImplementation({
4602
- context,
4603
- action: {
4604
- editor: action.editor
4605
- }
4606
- }), selectPreviousBlockActionImplementation({
4607
- context,
4608
- action: {
4609
- editor: action.editor,
4610
- select: "end"
4611
- }
4612
- }), insertBlock({
4613
- block,
4614
- placement: "auto",
4615
- select: "end",
4616
- editor: action.editor,
4617
- schema: context.schema
4618
- })) : index === fragment.length - 1 ? (selectNextBlockActionImplementation({
4619
- context,
4620
- action: {
4621
- editor: action.editor,
4622
- select: "start"
4623
- }
4624
- }), insertBlock({
4625
- block,
4626
- placement: "auto",
4627
- select: "end",
4628
- editor: action.editor,
4629
- schema: context.schema
4630
- })) : insertBlock({
4631
- block,
4632
- placement: "after",
4633
- select: "end",
4634
- editor: action.editor,
4635
- schema: context.schema
4636
- }), index++;
4637
- } else {
4638
- let index = 0;
4639
- for (const block of fragment)
4640
- insertBlock({
4641
- block,
4642
- placement: index === 0 ? "auto" : "after",
4643
- select: "end",
4644
- editor: action.editor,
4645
- schema: context.schema
4646
- }), index++;
4647
- }
4648
- }, deserializationSuccessActionImplementation = ({
4649
- context,
4650
- action
4651
- }) => {
4652
- insertBlocksActionImplementation({
4653
- context,
4654
- action: {
4655
- blocks: action.data,
4656
- editor: action.editor,
4657
- placement: "auto"
4658
- }
4659
- });
4660
- }, effectActionImplementation = ({
4661
- action
4662
- }) => {
4663
- action.effect();
4664
- }, focusActionImplementation = ({
4665
- action
4666
- }) => {
4667
- slateReact.ReactEditor.focus(action.editor);
4668
- }, insertInlineObjectActionImplementation = ({
4669
- context,
4670
- action
4671
- }) => {
4672
- if (!context.schema.inlineObjects.some((inlineObject) => inlineObject.name === action.inlineObject.name)) {
4673
- console.error("Unable to insert unknown inline object");
4674
- return;
4675
- }
4676
- if (!action.editor.selection) {
4677
- console.error("Unable to insert inline object without selection");
4678
- return;
4679
- }
4680
- const [focusTextBlock] = Array.from(slate.Editor.nodes(action.editor, {
4681
- at: action.editor.selection.focus.path,
4682
- match: (node) => action.editor.isTextBlock(node)
4683
- })).at(0) ?? [void 0, void 0];
4684
- if (!focusTextBlock) {
4685
- console.error("Unable to perform action without focus text block");
4686
- return;
4687
- }
4688
- const child = toSlateValue([{
4689
- _type: context.schema.block.name,
4690
- _key: context.keyGenerator(),
4691
- children: [{
4692
- _type: action.inlineObject.name,
4693
- _key: context.keyGenerator(),
4694
- ...action.inlineObject.value ?? {}
4695
- }]
4696
- }], {
4697
- schemaTypes: context.schema
4698
- }).at(0)?.children.at(0);
4699
- if (!child) {
4700
- console.error("Unable to insert inline object");
4701
- return;
4702
- }
4703
- slate.Transforms.insertNodes(action.editor, child);
4704
- }, insertSpanActionImplementation = ({
4705
- context,
4706
- action
4707
- }) => {
4708
- if (!action.editor.selection) {
4709
- console.error("Unable to perform action without selection", action);
4710
- return;
4711
- }
4712
- const [focusBlock, focusBlockPath] = Array.from(slate.Editor.nodes(action.editor, {
4713
- at: action.editor.selection.focus.path,
4714
- match: (node) => action.editor.isTextBlock(node)
4715
- }))[0] ?? [void 0, void 0];
4716
- if (!focusBlock || !focusBlockPath) {
4717
- console.error("Unable to perform action without focus block", action);
4718
- return;
4719
- }
4720
- const markDefs = focusBlock.markDefs ?? [], annotations = action.annotations ? action.annotations.map((annotation) => ({
4721
- _type: annotation.name,
4722
- _key: context.keyGenerator(),
4723
- ...annotation.value
4724
- })) : void 0;
4725
- annotations && annotations.length > 0 && slate.Transforms.setNodes(action.editor, {
4726
- markDefs: [...markDefs, ...annotations]
4727
- }), slate.Transforms.insertNodes(action.editor, {
4728
- _type: "span",
4729
- _key: context.keyGenerator(),
4730
- text: action.text,
4731
- marks: [...annotations?.map((annotation) => annotation._key) ?? [], ...action.decorators ?? []]
4732
- });
4733
- }, insertTextActionImplementation = ({
4734
- action
4735
- }) => {
4736
- action.editor.insertText(action.text);
4737
- }, moveBlockActionImplementation = ({
4738
- action
4739
- }) => {
4740
- const at = [toSlatePath(action.at, action.editor)[0]], to = [toSlatePath(action.to, action.editor)[0]];
4741
- slate.Transforms.moveNodes(action.editor, {
4742
- at,
4743
- to,
4744
- mode: "highest"
4745
- });
4746
- }, moveBlockDownActionImplementation = ({
4747
- context,
4748
- action
4749
- }) => {
4750
- const at = [toSlatePath(action.at, action.editor)[0]], to = [slate.Path.next(at)[0]], selection = toPortableTextRange(action.editor.children, {
4751
- anchor: {
4752
- path: to,
4753
- offset: 0
4754
- },
4755
- focus: {
4756
- path: to,
4757
- offset: 0
4758
- }
4759
- }, context.schema), destinationBlockKey = selection && util_sliceBlocks.isKeyedSegment(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4760
- if (destinationBlockKey === void 0) {
4761
- console.error("Could not find destination block key");
4762
- return;
4763
- }
4764
- moveBlockActionImplementation({
4765
- action: {
4766
- at: action.at,
4767
- to: [{
4768
- _key: destinationBlockKey
4769
- }],
4770
- editor: action.editor
4771
- }
4772
- });
4773
- }, moveBlockUpActionImplementation = ({
4774
- context,
4775
- action
4776
- }) => {
4777
- const at = [toSlatePath(action.at, action.editor)[0]];
4778
- if (!slate.Path.hasPrevious(at))
4779
- return;
4780
- const to = [slate.Path.previous(at)[0]], selection = toPortableTextRange(action.editor.children, {
4781
- anchor: {
4782
- path: to,
4783
- offset: 0
4784
- },
4785
- focus: {
4786
- path: to,
4787
- offset: 0
4788
- }
4789
- }, context.schema), destinationBlockKey = selection && util_sliceBlocks.isKeyedSegment(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4790
- if (destinationBlockKey === void 0) {
4791
- console.error("Could not find destination block key");
4792
- return;
4793
- }
4794
- moveBlockActionImplementation({
4795
- action: {
4796
- at: action.at,
4797
- to: [{
4798
- _key: destinationBlockKey
4799
- }],
4800
- editor: action.editor
4801
- }
4802
- });
4803
- }, noopActionImplementation = () => {
4804
- }, serializationFailureActionImplementation = ({
4805
- action
4806
- }) => {
4807
- console.warn(`Serialization of ${action.mimeType} failed with reason "${action.reason}"`);
4808
- }, serializationSuccessActionImplementation = ({
4809
- context,
4810
- action
4811
- }) => {
4812
- dataTransferSetActionImplementation({
4813
- action: {
4814
- data: action.data,
4815
- dataTransfer: action.originEvent.originEvent.dataTransfer,
4816
- mimeType: action.mimeType
4817
- }
4818
- });
4533
+ const newSelection = toSlateRange(action.selection, action.editor);
4534
+ newSelection ? slate.Transforms.select(action.editor, newSelection) : slate.Transforms.deselect(action.editor);
4819
4535
  }, debug$9 = debugWithName("behaviors:action"), behaviorActionImplementations = {
4820
4536
  "annotation.add": addAnnotationActionImplementation,
4821
4537
  "annotation.remove": removeAnnotationActionImplementation,
@@ -4823,7 +4539,6 @@ const selectActionImplementation = ({
4823
4539
  "block.set": blockSetBehaviorActionImplementation,
4824
4540
  "block.unset": blockUnsetBehaviorActionImplementation,
4825
4541
  blur: blurActionImplementation,
4826
- "data transfer.set": dataTransferSetActionImplementation,
4827
4542
  "decorator.add": decoratorAddActionImplementation,
4828
4543
  "decorator.remove": removeDecoratorActionImplementation,
4829
4544
  "decorator.toggle": toggleDecoratorActionImplementation,
@@ -4833,33 +4548,20 @@ const selectActionImplementation = ({
4833
4548
  "delete.forward": deleteForwardActionImplementation,
4834
4549
  "delete.block": deleteBlockActionImplementation,
4835
4550
  "delete.text": deleteTextActionImplementation,
4836
- "deserialization.failure": deserializationFailureActionImplementation,
4837
- "deserialization.success": deserializationSuccessActionImplementation,
4838
4551
  "history.redo": historyRedoActionImplementation,
4839
4552
  "history.undo": historyUndoActionImplementation,
4840
4553
  "insert.block": insertBlockActionImplementation,
4841
- "insert.blocks": insertBlocksActionImplementation,
4842
4554
  "insert.break": insertBreakActionImplementation,
4843
4555
  "insert.inline object": insertInlineObjectActionImplementation,
4844
4556
  "insert.soft break": insertSoftBreakActionImplementation,
4845
4557
  "insert.span": insertSpanActionImplementation,
4846
4558
  "insert.text": insertTextActionImplementation,
4847
4559
  effect: effectActionImplementation,
4848
- "list item.add": addListItemActionImplementation,
4849
- "list item.remove": removeListItemActionImplementation,
4850
- "list item.toggle": toggleListItemActionImplementation,
4851
4560
  "move.block": moveBlockActionImplementation,
4852
4561
  "move.block down": moveBlockDownActionImplementation,
4853
4562
  "move.block up": moveBlockUpActionImplementation,
4854
4563
  noop: noopActionImplementation,
4855
- select: selectActionImplementation,
4856
- "select.previous block": selectPreviousBlockActionImplementation,
4857
- "select.next block": selectNextBlockActionImplementation,
4858
- "serialization.failure": serializationFailureActionImplementation,
4859
- "serialization.success": serializationSuccessActionImplementation,
4860
- "style.toggle": toggleStyleActionImplementation,
4861
- "style.add": addStyleActionImplementation,
4862
- "style.remove": removeStyleActionImplementation
4564
+ select: selectActionImplementation
4863
4565
  };
4864
4566
  function performAction({
4865
4567
  context,
@@ -4908,13 +4610,6 @@ function performAction({
4908
4610
  });
4909
4611
  break;
4910
4612
  }
4911
- case "data transfer.set": {
4912
- behaviorActionImplementations["data transfer.set"]({
4913
- context,
4914
- action
4915
- });
4916
- break;
4917
- }
4918
4613
  case "decorator.add": {
4919
4614
  behaviorActionImplementations["decorator.add"]({
4920
4615
  context,
@@ -4971,20 +4666,6 @@ function performAction({
4971
4666
  });
4972
4667
  break;
4973
4668
  }
4974
- case "deserialization.failure": {
4975
- behaviorActionImplementations["deserialization.failure"]({
4976
- context,
4977
- action
4978
- });
4979
- break;
4980
- }
4981
- case "deserialization.success": {
4982
- behaviorActionImplementations["deserialization.success"]({
4983
- context,
4984
- action
4985
- });
4986
- break;
4987
- }
4988
4669
  case "effect": {
4989
4670
  behaviorActionImplementations.effect({
4990
4671
  context,
@@ -5020,13 +4701,6 @@ function performAction({
5020
4701
  });
5021
4702
  break;
5022
4703
  }
5023
- case "insert.blocks": {
5024
- behaviorActionImplementations["insert.blocks"]({
5025
- context,
5026
- action
5027
- });
5028
- break;
5029
- }
5030
4704
  case "insert.inline object": {
5031
4705
  behaviorActionImplementations["insert.inline object"]({
5032
4706
  context,
@@ -5062,27 +4736,6 @@ function performAction({
5062
4736
  });
5063
4737
  break;
5064
4738
  }
5065
- case "list item.add": {
5066
- behaviorActionImplementations["list item.add"]({
5067
- context,
5068
- action
5069
- });
5070
- break;
5071
- }
5072
- case "list item.remove": {
5073
- behaviorActionImplementations["list item.remove"]({
5074
- context,
5075
- action
5076
- });
5077
- break;
5078
- }
5079
- case "list item.toggle": {
5080
- behaviorActionImplementations["list item.toggle"]({
5081
- context,
5082
- action
5083
- });
5084
- break;
5085
- }
5086
4739
  case "move.block": {
5087
4740
  behaviorActionImplementations["move.block"]({
5088
4741
  context,
@@ -5106,57 +4759,8 @@ function performAction({
5106
4759
  }
5107
4760
  case "noop":
5108
4761
  break;
5109
- case "select": {
5110
- behaviorActionImplementations.select({
5111
- context,
5112
- action
5113
- });
5114
- break;
5115
- }
5116
- case "select.previous block": {
5117
- behaviorActionImplementations["select.previous block"]({
5118
- context,
5119
- action
5120
- });
5121
- break;
5122
- }
5123
- case "select.next block": {
5124
- behaviorActionImplementations["select.next block"]({
5125
- context,
5126
- action
5127
- });
5128
- break;
5129
- }
5130
- case "serialization.failure": {
5131
- behaviorActionImplementations["serialization.failure"]({
5132
- context,
5133
- action
5134
- });
5135
- break;
5136
- }
5137
- case "serialization.success": {
5138
- behaviorActionImplementations["serialization.success"]({
5139
- context,
5140
- action
5141
- });
5142
- break;
5143
- }
5144
- case "style.add": {
5145
- behaviorActionImplementations["style.add"]({
5146
- context,
5147
- action
5148
- });
5149
- break;
5150
- }
5151
- case "style.remove": {
5152
- behaviorActionImplementations["style.remove"]({
5153
- context,
5154
- action
5155
- });
5156
- break;
5157
- }
5158
4762
  default: {
5159
- behaviorActionImplementations["style.toggle"]({
4763
+ behaviorActionImplementations.select({
5160
4764
  context,
5161
4765
  action
5162
4766
  });
@@ -6065,114 +5669,301 @@ const keyIs = {
6065
5669
  actions: [() => [behavior_core.raise({
6066
5670
  type: "insert.soft break"
6067
5671
  })]]
6068
- }), toggleAnnotationOff = behavior_core.defineBehavior({
6069
- on: "annotation.toggle",
5672
+ }), internalInsertBehaviors = [behavior_core.defineBehavior({
5673
+ on: "insert.blocks",
6070
5674
  guard: ({
6071
- snapshot,
6072
5675
  event
6073
- }) => selector_isActiveStyle.isActiveAnnotation(event.annotation.name)(snapshot),
5676
+ }) => event.placement === "before",
6074
5677
  actions: [({
6075
5678
  event
6076
- }) => [behavior_core.raise({
6077
- type: "annotation.remove",
6078
- annotation: event.annotation
6079
- })]]
6080
- }), toggleAnnotationOn = behavior_core.defineBehavior({
6081
- on: "annotation.toggle",
5679
+ }) => event.blocks.map((block, index) => behavior_core.raise({
5680
+ type: "insert.block",
5681
+ block,
5682
+ placement: index === 0 ? "before" : "after",
5683
+ select: "end"
5684
+ }))]
5685
+ }), behavior_core.defineBehavior({
5686
+ on: "insert.blocks",
6082
5687
  guard: ({
6083
- snapshot,
6084
5688
  event
6085
- }) => !selector_isActiveStyle.isActiveAnnotation(event.annotation.name)(snapshot),
5689
+ }) => event.placement === "after",
6086
5690
  actions: [({
6087
5691
  event
6088
- }) => [behavior_core.raise({
6089
- type: "annotation.add",
6090
- annotation: event.annotation
6091
- })]]
6092
- }), toggleDecoratorOff = behavior_core.defineBehavior({
6093
- on: "decorator.toggle",
5692
+ }) => event.blocks.map((block) => behavior_core.raise({
5693
+ type: "insert.block",
5694
+ block,
5695
+ placement: "after",
5696
+ select: "end"
5697
+ }))]
5698
+ }), behavior_core.defineBehavior({
5699
+ on: "insert.blocks",
6094
5700
  guard: ({
6095
5701
  snapshot,
6096
5702
  event
6097
- }) => selector_isActiveStyle.isActiveDecorator(event.decorator)(snapshot),
5703
+ }) => !(event.placement !== "auto" || !selector_isOverlappingSelection.getFocusTextBlock(snapshot)),
6098
5704
  actions: [({
6099
5705
  event
6100
- }) => [behavior_core.raise({
6101
- type: "decorator.remove",
6102
- decorator: event.decorator
6103
- })]]
6104
- }), toggleDecoratorOn = behavior_core.defineBehavior({
6105
- on: "decorator.toggle",
5706
+ }) => event.blocks.length === 1 ? [behavior_core.raise({
5707
+ type: "insert.block",
5708
+ block: event.blocks[0],
5709
+ placement: "auto",
5710
+ select: "end"
5711
+ })] : event.blocks.flatMap((block, index) => index === 0 ? [behavior_core.raise({
5712
+ type: "insert.break"
5713
+ }), behavior_core.raise({
5714
+ type: "select.previous block",
5715
+ select: "end"
5716
+ }), behavior_core.raise({
5717
+ type: "insert.block",
5718
+ block,
5719
+ placement: "auto",
5720
+ select: "end"
5721
+ })] : index === event.blocks.length - 1 ? [behavior_core.raise({
5722
+ type: "select.next block",
5723
+ select: "start"
5724
+ }), behavior_core.raise({
5725
+ type: "insert.block",
5726
+ block,
5727
+ placement: "auto",
5728
+ select: "end"
5729
+ })] : [behavior_core.raise({
5730
+ type: "insert.block",
5731
+ block,
5732
+ placement: "after",
5733
+ select: "end"
5734
+ })])]
5735
+ }), behavior_core.defineBehavior({
5736
+ on: "insert.blocks",
6106
5737
  guard: ({
6107
- snapshot,
6108
5738
  event
6109
- }) => {
6110
- const manualSelection = event.offsets ? util_selectionPointToBlockOffset.blockOffsetsToSelection({
6111
- value: snapshot.context.value,
6112
- offsets: event.offsets
6113
- }) : null;
6114
- return manualSelection ? !selector_isActiveStyle.isActiveDecorator(event.decorator)({
6115
- ...snapshot,
6116
- context: {
6117
- ...snapshot.context,
6118
- selection: manualSelection
6119
- }
6120
- }) : !selector_isActiveStyle.isActiveDecorator(event.decorator)(snapshot);
6121
- },
5739
+ }) => event.placement === "auto",
6122
5740
  actions: [({
6123
5741
  event
6124
- }) => [behavior_core.raise({
6125
- ...event,
6126
- type: "decorator.add"
6127
- })]]
6128
- }), toggleListItemOff = behavior_core.defineBehavior({
5742
+ }) => event.blocks.map((block, index) => behavior_core.raise({
5743
+ type: "insert.block",
5744
+ block,
5745
+ placement: index === 0 ? "auto" : "after",
5746
+ select: "end"
5747
+ }))]
5748
+ })], internalListItemBehaviors = [behavior_core.defineBehavior({
5749
+ on: "list item.add",
5750
+ guard: ({
5751
+ snapshot
5752
+ }) => ({
5753
+ selectedTextBlocks: selector_isSelectingEntireBlocks.getSelectedTextBlocks(snapshot)
5754
+ }),
5755
+ actions: [({
5756
+ event
5757
+ }, {
5758
+ selectedTextBlocks
5759
+ }) => selectedTextBlocks.map((block) => behavior_core.raise({
5760
+ type: "block.set",
5761
+ at: block.path,
5762
+ props: {
5763
+ level: 1,
5764
+ listItem: event.listItem
5765
+ }
5766
+ }))]
5767
+ }), behavior_core.defineBehavior({
5768
+ on: "list item.remove",
5769
+ guard: ({
5770
+ snapshot
5771
+ }) => ({
5772
+ selectedTextBlocks: selector_isSelectingEntireBlocks.getSelectedTextBlocks(snapshot)
5773
+ }),
5774
+ actions: [(_, {
5775
+ selectedTextBlocks
5776
+ }) => selectedTextBlocks.map((block) => behavior_core.raise({
5777
+ type: "block.unset",
5778
+ at: block.path,
5779
+ props: ["level", "listItem"]
5780
+ }))]
5781
+ }), behavior_core.defineBehavior({
6129
5782
  on: "list item.toggle",
6130
5783
  guard: ({
6131
5784
  snapshot,
6132
5785
  event
6133
- }) => selector_isActiveStyle.isActiveListItem(event.listItem)(snapshot),
5786
+ }) => selector_isSelectingEntireBlocks.isActiveListItem(event.listItem)(snapshot),
6134
5787
  actions: [({
6135
5788
  event
6136
5789
  }) => [behavior_core.raise({
6137
5790
  type: "list item.remove",
6138
5791
  listItem: event.listItem
6139
5792
  })]]
6140
- }), toggleListItemOn = behavior_core.defineBehavior({
5793
+ }), behavior_core.defineBehavior({
6141
5794
  on: "list item.toggle",
6142
5795
  guard: ({
6143
5796
  snapshot,
6144
5797
  event
6145
- }) => !selector_isActiveStyle.isActiveListItem(event.listItem)(snapshot),
5798
+ }) => !selector_isSelectingEntireBlocks.isActiveListItem(event.listItem)(snapshot),
6146
5799
  actions: [({
6147
5800
  event
6148
5801
  }) => [behavior_core.raise({
6149
5802
  type: "list item.add",
6150
5803
  listItem: event.listItem
6151
5804
  })]]
6152
- }), toggleStyleOff = behavior_core.defineBehavior({
5805
+ })], internalSelectBehaviors = [behavior_core.defineBehavior({
5806
+ on: "select.previous block",
5807
+ guard: ({
5808
+ snapshot,
5809
+ event
5810
+ }) => {
5811
+ const previousBlock = selector_isOverlappingSelection.getPreviousBlock(snapshot);
5812
+ if (!previousBlock)
5813
+ return !1;
5814
+ const point = event.select === "end" ? util_sliceBlocks.getBlockEndPoint(previousBlock) : util_sliceBlocks.getBlockStartPoint(previousBlock);
5815
+ return {
5816
+ selection: {
5817
+ anchor: point,
5818
+ focus: point
5819
+ }
5820
+ };
5821
+ },
5822
+ actions: [(_, {
5823
+ selection
5824
+ }) => [behavior_core.raise({
5825
+ type: "select",
5826
+ selection
5827
+ })]]
5828
+ }), behavior_core.defineBehavior({
5829
+ on: "select.next block",
5830
+ guard: ({
5831
+ snapshot,
5832
+ event
5833
+ }) => {
5834
+ const nextBlock = selector_isOverlappingSelection.getNextBlock(snapshot);
5835
+ if (!nextBlock)
5836
+ return !1;
5837
+ const point = event.select === "end" ? util_sliceBlocks.getBlockEndPoint(nextBlock) : util_sliceBlocks.getBlockStartPoint(nextBlock);
5838
+ return {
5839
+ selection: {
5840
+ anchor: point,
5841
+ focus: point
5842
+ }
5843
+ };
5844
+ },
5845
+ actions: [(_, {
5846
+ selection
5847
+ }) => [behavior_core.raise({
5848
+ type: "select",
5849
+ selection
5850
+ })]]
5851
+ })], internalStyleBehaviors = [behavior_core.defineBehavior({
5852
+ on: "style.add",
5853
+ guard: ({
5854
+ snapshot
5855
+ }) => ({
5856
+ selectedTextBlocks: selector_isSelectingEntireBlocks.getSelectedTextBlocks(snapshot)
5857
+ }),
5858
+ actions: [({
5859
+ event
5860
+ }, {
5861
+ selectedTextBlocks
5862
+ }) => selectedTextBlocks.map((block) => behavior_core.raise({
5863
+ type: "block.set",
5864
+ at: block.path,
5865
+ props: {
5866
+ style: event.style
5867
+ }
5868
+ }))]
5869
+ }), behavior_core.defineBehavior({
5870
+ on: "style.remove",
5871
+ guard: ({
5872
+ snapshot
5873
+ }) => ({
5874
+ selectedTextBlocks: selector_isSelectingEntireBlocks.getSelectedTextBlocks(snapshot)
5875
+ }),
5876
+ actions: [(_, {
5877
+ selectedTextBlocks
5878
+ }) => selectedTextBlocks.map((block) => behavior_core.raise({
5879
+ type: "block.unset",
5880
+ at: block.path,
5881
+ props: ["style"]
5882
+ }))]
5883
+ }), behavior_core.defineBehavior({
6153
5884
  on: "style.toggle",
6154
5885
  guard: ({
6155
5886
  snapshot,
6156
5887
  event
6157
- }) => selector_isActiveStyle.isActiveStyle(event.style)(snapshot),
5888
+ }) => selector_isSelectingEntireBlocks.isActiveStyle(event.style)(snapshot),
6158
5889
  actions: [({
6159
5890
  event
6160
5891
  }) => [behavior_core.raise({
6161
5892
  type: "style.remove",
6162
5893
  style: event.style
6163
5894
  })]]
6164
- }), toggleStyleOn = behavior_core.defineBehavior({
5895
+ }), behavior_core.defineBehavior({
6165
5896
  on: "style.toggle",
6166
5897
  guard: ({
6167
5898
  snapshot,
6168
5899
  event
6169
- }) => !selector_isActiveStyle.isActiveStyle(event.style)(snapshot),
5900
+ }) => !selector_isSelectingEntireBlocks.isActiveStyle(event.style)(snapshot),
6170
5901
  actions: [({
6171
5902
  event
6172
5903
  }) => [behavior_core.raise({
6173
5904
  type: "style.add",
6174
5905
  style: event.style
6175
5906
  })]]
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
+ })]]
6176
5967
  }), raiseDeserializationSuccessOrFailure = behavior_core.defineBehavior({
6177
5968
  on: "deserialize",
6178
5969
  guard: ({
@@ -6290,12 +6081,23 @@ const keyIs = {
6290
6081
  on: "serialization.success",
6291
6082
  actions: [({
6292
6083
  event
6293
- }) => [behavior_core.raise({
6294
- type: "data transfer.set",
6295
- data: event.data,
6296
- dataTransfer: event.originEvent.originEvent.dataTransfer,
6297
- mimeType: event.mimeType
6298
- })]]
6084
+ }) => [{
6085
+ type: "effect",
6086
+ effect: () => {
6087
+ event.originEvent.originEvent.dataTransfer.setData(event.mimeType, event.data);
6088
+ }
6089
+ }]]
6090
+ }),
6091
+ behavior_core.defineBehavior({
6092
+ on: "serialization.failure",
6093
+ actions: [({
6094
+ event
6095
+ }) => [{
6096
+ type: "effect",
6097
+ effect: () => {
6098
+ console.warn(`Serialization of ${event.mimeType} failed with reason "${event.reason}"`);
6099
+ }
6100
+ }]]
6299
6101
  }),
6300
6102
  behavior_core.defineBehavior({
6301
6103
  on: "drag.drop",
@@ -6342,7 +6144,7 @@ const keyIs = {
6342
6144
  ...snapshot.context,
6343
6145
  selection: dragOrigin.selection
6344
6146
  }
6345
- }) : !1, draggingEntireBlocks = selector_isActiveStyle.isSelectingEntireBlocks({
6147
+ }) : !1, draggingEntireBlocks = selector_isSelectingEntireBlocks.isSelectingEntireBlocks({
6346
6148
  context: {
6347
6149
  ...snapshot.context,
6348
6150
  selection: dragOrigin.selection
@@ -6393,9 +6195,9 @@ const keyIs = {
6393
6195
  if (selector_isOverlappingSelection.getFocusTextBlock(snapshot) && event.mimeType === "text/plain" && event.originEvent.type === "clipboard.paste") {
6394
6196
  const activeDecorators = snapshot.context.activeDecorators;
6395
6197
  return {
6396
- activeAnnotations: selector_isActiveStyle.getActiveAnnotations(snapshot),
6198
+ activeAnnotations: selector_isSelectingEntireBlocks.getActiveAnnotations(snapshot),
6397
6199
  activeDecorators,
6398
- textRuns: event.data.flatMap((block) => util_selectionPointToBlockOffset.isTextBlock(snapshot.context.schema, block) ? [util_sliceBlocks.getTextBlockText(block)] : [])
6200
+ textRuns: event.data.flatMap((block) => parseBlocks.isTextBlock(snapshot.context.schema, block) ? [util_sliceBlocks.getTextBlockText(block)] : [])
6399
6201
  };
6400
6202
  }
6401
6203
  return !1;
@@ -6442,6 +6244,17 @@ const keyIs = {
6442
6244
  placement: "auto"
6443
6245
  })]]
6444
6246
  }),
6247
+ behavior_core.defineBehavior({
6248
+ on: "deserialization.failure",
6249
+ actions: [({
6250
+ event
6251
+ }) => [{
6252
+ type: "effect",
6253
+ effect: () => {
6254
+ console.warn(`Deserialization of ${event.mimeType} failed with reason "${event.reason}"`);
6255
+ }
6256
+ }]]
6257
+ }),
6445
6258
  behavior_core.defineBehavior({
6446
6259
  on: "clipboard.paste",
6447
6260
  guard: ({
@@ -6479,14 +6292,14 @@ const keyIs = {
6479
6292
  originEvent: event
6480
6293
  })]]
6481
6294
  }),
6295
+ ...internalInsertBehaviors,
6296
+ ...internalListItemBehaviors,
6297
+ ...internalStyleBehaviors,
6298
+ ...internalSelectBehaviors,
6482
6299
  toggleAnnotationOff,
6483
6300
  toggleAnnotationOn,
6484
6301
  toggleDecoratorOff,
6485
6302
  toggleDecoratorOn,
6486
- toggleListItemOff,
6487
- toggleListItemOn,
6488
- toggleStyleOff,
6489
- toggleStyleOn,
6490
6303
  raiseDeserializationSuccessOrFailure,
6491
6304
  raiseSerializationSuccessOrFailure,
6492
6305
  raiseInsertSoftBreak
@@ -6494,6 +6307,12 @@ const keyIs = {
6494
6307
  function isKeyboardBehaviorEvent(event) {
6495
6308
  return event.type.startsWith("keyboard.");
6496
6309
  }
6310
+ function isInternalBehaviorEvent(event) {
6311
+ 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.");
6312
+ }
6313
+ function isNativeBehaviorEvent(event) {
6314
+ return isClipboardBehaviorEvent(event) || isDragBehaviorEvent(event) || isInputBehaviorEvent(event) || isKeyboardBehaviorEvent(event) || isMouseBehaviorEvent(event);
6315
+ }
6497
6316
  function isClipboardBehaviorEvent(event) {
6498
6317
  return event.type.startsWith("clipboard.");
6499
6318
  }
@@ -6510,6 +6329,9 @@ function isCustomBehaviorEvent(event) {
6510
6329
  return event.type.startsWith("custom.");
6511
6330
  }
6512
6331
  const debug$2 = debugWithName("behaviors:event");
6332
+ function eventCategory(event) {
6333
+ return isNativeBehaviorEvent(event) ? "native" : isInternalBehaviorEvent(event) ? "internal" : isCustomBehaviorEvent(event) ? "custom" : "synthetic";
6334
+ }
6513
6335
  function performEvent({
6514
6336
  behaviors,
6515
6337
  event,
@@ -6520,8 +6342,8 @@ function performEvent({
6520
6342
  nativeEvent,
6521
6343
  defaultActionCallback
6522
6344
  }) {
6523
- debug$2(JSON.stringify(event, null, 2));
6524
- const defaultAction = isCustomBehaviorEvent(event) || isClipboardBehaviorEvent(event) || isDragBehaviorEvent(event) || isInputBehaviorEvent(event) || isKeyboardBehaviorEvent(event) || isMouseBehaviorEvent(event) || event.type === "deserialize" || event.type === "serialize" ? void 0 : {
6345
+ debug$2(`(${eventCategory(event)})`, JSON.stringify(event, null, 2));
6346
+ const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isInternalBehaviorEvent(event) ? void 0 : {
6525
6347
  ...event,
6526
6348
  editor
6527
6349
  }, eventBehaviors = behaviors.filter((behavior) => {
@@ -6913,7 +6735,7 @@ const editorMachine = xstate.setup({
6913
6735
  actions: "handle behavior event",
6914
6736
  guard: ({
6915
6737
  event
6916
- }) => 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"
6738
+ }) => event.behaviorEvent.type === "clipboard.copy" || event.behaviorEvent.type === "serialize" || event.behaviorEvent.type === "serialization.failure" || event.behaviorEvent.type === "serialization.success" || event.behaviorEvent.type === "select"
6917
6739
  }
6918
6740
  },
6919
6741
  states: {