@portabletext/editor 1.43.0 → 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 (73) hide show
  1. package/lib/_chunks-cjs/behavior.markdown.cjs +1 -1
  2. package/lib/_chunks-cjs/editor-provider.cjs +445 -623
  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 +434 -611
  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/editor/sync-machine.ts +1 -1
  57. package/src/internal-utils/slate-utils.ts +52 -0
  58. package/src/plugins/plugin.internal.editor-actor-ref.tsx +15 -0
  59. package/src/plugins/plugin.internal.slate-editor-ref.tsx +15 -0
  60. package/src/selectors/index.ts +2 -1
  61. package/src/selectors/selector.get-selected-text-blocks.ts +67 -0
  62. package/lib/_chunks-cjs/selector.is-active-style.cjs.map +0 -1
  63. package/lib/_chunks-es/selector.is-active-style.js.map +0 -1
  64. package/src/behavior-actions/behavior.action.data-transfer-set.ts +0 -7
  65. package/src/behavior-actions/behavior.action.deserialization.failure.ts +0 -9
  66. package/src/behavior-actions/behavior.action.deserialization.success.ts +0 -16
  67. package/src/behavior-actions/behavior.action.insert-blocks.ts +0 -140
  68. package/src/behavior-actions/behavior.action.list-item.ts +0 -100
  69. package/src/behavior-actions/behavior.action.select.next-block.ts +0 -44
  70. package/src/behavior-actions/behavior.action.select.previous-block.ts +0 -48
  71. package/src/behavior-actions/behavior.action.serialization.failure.ts +0 -9
  72. package/src/behavior-actions/behavior.action.serialization.success.ts +0 -17
  73. 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, getSelectedTextBlocks, isActiveListItem, isActiveStyle, isActiveAnnotation, isActiveDecorator, 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)
@@ -2236,7 +2261,7 @@ async function* getBlocks({
2236
2261
  }) {
2237
2262
  let index = 0;
2238
2263
  for await (const block of slateValue)
2239
- streamBlocks && await new Promise((resolve) => setTimeout(resolve, 0)), yield [block, index], index++;
2264
+ streamBlocks && index % 10 === 0 && await new Promise((resolve) => setTimeout(resolve, 0)), yield [block, index], index++;
2240
2265
  }
2241
2266
  function syncBlock({
2242
2267
  context,
@@ -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
  }
@@ -4294,10 +4195,6 @@ const addAnnotationActionImplementation = ({
4294
4195
  action
4295
4196
  }) => {
4296
4197
  ReactEditor.blur(action.editor);
4297
- }, dataTransferSetActionImplementation = ({
4298
- action
4299
- }) => {
4300
- action.dataTransfer.setData(action.mimeType, action.data);
4301
4198
  }, deleteActionImplementation = ({
4302
4199
  action
4303
4200
  }) => {
@@ -4367,10 +4264,79 @@ const addAnnotationActionImplementation = ({
4367
4264
  Transforms.delete(action.editor, {
4368
4265
  at: range
4369
4266
  });
4370
- }, deserializationFailureActionImplementation = ({
4267
+ }, effectActionImplementation = ({
4268
+ action
4269
+ }) => {
4270
+ action.effect();
4271
+ }, focusActionImplementation = ({
4272
+ action
4273
+ }) => {
4274
+ ReactEditor.focus(action.editor);
4275
+ }, insertInlineObjectActionImplementation = ({
4276
+ context,
4277
+ action
4278
+ }) => {
4279
+ if (!context.schema.inlineObjects.some((inlineObject) => inlineObject.name === action.inlineObject.name)) {
4280
+ console.error("Unable to insert unknown inline object");
4281
+ return;
4282
+ }
4283
+ if (!action.editor.selection) {
4284
+ console.error("Unable to insert inline object without selection");
4285
+ return;
4286
+ }
4287
+ const [focusTextBlock] = Array.from(Editor.nodes(action.editor, {
4288
+ at: action.editor.selection.focus.path,
4289
+ match: (node) => action.editor.isTextBlock(node)
4290
+ })).at(0) ?? [void 0, void 0];
4291
+ if (!focusTextBlock) {
4292
+ console.error("Unable to perform action without focus text block");
4293
+ return;
4294
+ }
4295
+ const child = toSlateValue([{
4296
+ _type: context.schema.block.name,
4297
+ _key: context.keyGenerator(),
4298
+ children: [{
4299
+ _type: action.inlineObject.name,
4300
+ _key: context.keyGenerator(),
4301
+ ...action.inlineObject.value ?? {}
4302
+ }]
4303
+ }], {
4304
+ schemaTypes: context.schema
4305
+ }).at(0)?.children.at(0);
4306
+ if (!child) {
4307
+ console.error("Unable to insert inline object");
4308
+ return;
4309
+ }
4310
+ Transforms.insertNodes(action.editor, child);
4311
+ }, insertSpanActionImplementation = ({
4312
+ context,
4371
4313
  action
4372
4314
  }) => {
4373
- console.warn(`Deserialization of ${action.mimeType} failed with reason "${action.reason}"`);
4315
+ if (!action.editor.selection) {
4316
+ console.error("Unable to perform action without selection", action);
4317
+ return;
4318
+ }
4319
+ const [focusBlock, focusBlockPath] = Array.from(Editor.nodes(action.editor, {
4320
+ at: action.editor.selection.focus.path,
4321
+ match: (node) => action.editor.isTextBlock(node)
4322
+ }))[0] ?? [void 0, void 0];
4323
+ if (!focusBlock || !focusBlockPath) {
4324
+ console.error("Unable to perform action without focus block", action);
4325
+ return;
4326
+ }
4327
+ const markDefs = focusBlock.markDefs ?? [], annotations = action.annotations ? action.annotations.map((annotation) => ({
4328
+ _type: annotation.name,
4329
+ _key: context.keyGenerator(),
4330
+ ...annotation.value
4331
+ })) : void 0;
4332
+ annotations && annotations.length > 0 && Transforms.setNodes(action.editor, {
4333
+ markDefs: [...markDefs, ...annotations]
4334
+ }), Transforms.insertNodes(action.editor, {
4335
+ _type: "span",
4336
+ _key: context.keyGenerator(),
4337
+ text: action.text,
4338
+ marks: [...annotations?.map((annotation) => annotation._key) ?? [], ...action.decorators ?? []]
4339
+ });
4374
4340
  }, insertBlockActionImplementation = ({
4375
4341
  context,
4376
4342
  action
@@ -4516,331 +4482,82 @@ function insertBlock({
4516
4482
  }
4517
4483
  }
4518
4484
  }
4519
- const selectActionImplementation = ({
4485
+ const insertTextActionImplementation = ({
4520
4486
  action
4521
4487
  }) => {
4522
- const newSelection = toSlateRange(action.selection, action.editor);
4523
- newSelection ? Transforms.select(action.editor, newSelection) : Transforms.deselect(action.editor);
4524
- }, selectNextBlockActionImplementation = ({
4488
+ action.editor.insertText(action.text);
4489
+ }, moveBlockActionImplementation = ({
4490
+ action
4491
+ }) => {
4492
+ const at = [toSlatePath(action.at, action.editor)[0]], to = [toSlatePath(action.to, action.editor)[0]];
4493
+ Transforms.moveNodes(action.editor, {
4494
+ at,
4495
+ to,
4496
+ mode: "highest"
4497
+ });
4498
+ }, moveBlockDownActionImplementation = ({
4525
4499
  context,
4526
4500
  action
4527
4501
  }) => {
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");
4502
+ const at = [toSlatePath(action.at, action.editor)[0]], to = [Path.next(at)[0]], selection = toPortableTextRange(action.editor.children, {
4503
+ anchor: {
4504
+ path: to,
4505
+ offset: 0
4506
+ },
4507
+ focus: {
4508
+ path: to,
4509
+ offset: 0
4510
+ }
4511
+ }, context.schema), destinationBlockKey = selection && isKeyedSegment$1(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4512
+ if (destinationBlockKey === void 0) {
4513
+ console.error("Could not find destination block key");
4538
4514
  return;
4539
4515
  }
4540
- selectActionImplementation({
4516
+ moveBlockActionImplementation({
4541
4517
  action: {
4542
- selection: newSelection,
4518
+ at: action.at,
4519
+ to: [{
4520
+ _key: destinationBlockKey
4521
+ }],
4543
4522
  editor: action.editor
4544
4523
  }
4545
4524
  });
4546
- }, selectPreviousBlockActionImplementation = ({
4525
+ }, moveBlockUpActionImplementation = ({
4547
4526
  context,
4548
4527
  action
4549
4528
  }) => {
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");
4529
+ const at = [toSlatePath(action.at, action.editor)[0]];
4530
+ if (!Path.hasPrevious(at))
4557
4531
  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");
4532
+ const to = [Path.previous(at)[0]], selection = toPortableTextRange(action.editor.children, {
4533
+ anchor: {
4534
+ path: to,
4535
+ offset: 0
4536
+ },
4537
+ focus: {
4538
+ path: to,
4539
+ offset: 0
4540
+ }
4541
+ }, context.schema), destinationBlockKey = selection && isKeyedSegment$1(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4542
+ if (destinationBlockKey === void 0) {
4543
+ console.error("Could not find destination block key");
4565
4544
  return;
4566
4545
  }
4567
- selectActionImplementation({
4546
+ moveBlockActionImplementation({
4568
4547
  action: {
4569
- selection: newSelection,
4548
+ at: action.at,
4549
+ to: [{
4550
+ _key: destinationBlockKey
4551
+ }],
4570
4552
  editor: action.editor
4571
4553
  }
4572
4554
  });
4573
- }, insertBlocksActionImplementation = ({
4574
- context,
4555
+ }, noopActionImplementation = () => {
4556
+ }, selectActionImplementation = ({
4575
4557
  action
4576
4558
  }) => {
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;
4744
- }
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 = ({
4759
- action
4760
- }) => {
4761
- action.editor.insertText(action.text);
4762
- }, moveBlockActionImplementation = ({
4763
- action
4764
- }) => {
4765
- const at = [toSlatePath(action.at, action.editor)[0]], to = [toSlatePath(action.to, action.editor)[0]];
4766
- Transforms.moveNodes(action.editor, {
4767
- at,
4768
- to,
4769
- mode: "highest"
4770
- });
4771
- }, moveBlockDownActionImplementation = ({
4772
- context,
4773
- action
4774
- }) => {
4775
- const at = [toSlatePath(action.at, action.editor)[0]], to = [Path.next(at)[0]], selection = toPortableTextRange(action.editor.children, {
4776
- anchor: {
4777
- path: to,
4778
- offset: 0
4779
- },
4780
- focus: {
4781
- path: to,
4782
- offset: 0
4783
- }
4784
- }, context.schema), destinationBlockKey = selection && isKeyedSegment$1(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4785
- if (destinationBlockKey === void 0) {
4786
- console.error("Could not find destination block key");
4787
- return;
4788
- }
4789
- moveBlockActionImplementation({
4790
- action: {
4791
- at: action.at,
4792
- to: [{
4793
- _key: destinationBlockKey
4794
- }],
4795
- editor: action.editor
4796
- }
4797
- });
4798
- }, moveBlockUpActionImplementation = ({
4799
- context,
4800
- action
4801
- }) => {
4802
- const at = [toSlatePath(action.at, action.editor)[0]];
4803
- if (!Path.hasPrevious(at))
4804
- return;
4805
- const to = [Path.previous(at)[0]], selection = toPortableTextRange(action.editor.children, {
4806
- anchor: {
4807
- path: to,
4808
- offset: 0
4809
- },
4810
- focus: {
4811
- path: to,
4812
- offset: 0
4813
- }
4814
- }, context.schema), destinationBlockKey = selection && isKeyedSegment$1(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4815
- if (destinationBlockKey === void 0) {
4816
- console.error("Could not find destination block key");
4817
- return;
4818
- }
4819
- moveBlockActionImplementation({
4820
- action: {
4821
- at: action.at,
4822
- to: [{
4823
- _key: destinationBlockKey
4824
- }],
4825
- editor: action.editor
4826
- }
4827
- });
4828
- }, noopActionImplementation = () => {
4829
- }, serializationFailureActionImplementation = ({
4830
- action
4831
- }) => {
4832
- console.warn(`Serialization of ${action.mimeType} failed with reason "${action.reason}"`);
4833
- }, serializationSuccessActionImplementation = ({
4834
- context,
4835
- action
4836
- }) => {
4837
- dataTransferSetActionImplementation({
4838
- action: {
4839
- data: action.data,
4840
- dataTransfer: action.originEvent.originEvent.dataTransfer,
4841
- mimeType: action.mimeType
4842
- }
4843
- });
4559
+ const newSelection = toSlateRange(action.selection, action.editor);
4560
+ newSelection ? Transforms.select(action.editor, newSelection) : Transforms.deselect(action.editor);
4844
4561
  }, debug$9 = debugWithName("behaviors:action"), behaviorActionImplementations = {
4845
4562
  "annotation.add": addAnnotationActionImplementation,
4846
4563
  "annotation.remove": removeAnnotationActionImplementation,
@@ -4848,7 +4565,6 @@ const selectActionImplementation = ({
4848
4565
  "block.set": blockSetBehaviorActionImplementation,
4849
4566
  "block.unset": blockUnsetBehaviorActionImplementation,
4850
4567
  blur: blurActionImplementation,
4851
- "data transfer.set": dataTransferSetActionImplementation,
4852
4568
  "decorator.add": decoratorAddActionImplementation,
4853
4569
  "decorator.remove": removeDecoratorActionImplementation,
4854
4570
  "decorator.toggle": toggleDecoratorActionImplementation,
@@ -4858,33 +4574,20 @@ const selectActionImplementation = ({
4858
4574
  "delete.forward": deleteForwardActionImplementation,
4859
4575
  "delete.block": deleteBlockActionImplementation,
4860
4576
  "delete.text": deleteTextActionImplementation,
4861
- "deserialization.failure": deserializationFailureActionImplementation,
4862
- "deserialization.success": deserializationSuccessActionImplementation,
4863
4577
  "history.redo": historyRedoActionImplementation,
4864
4578
  "history.undo": historyUndoActionImplementation,
4865
4579
  "insert.block": insertBlockActionImplementation,
4866
- "insert.blocks": insertBlocksActionImplementation,
4867
4580
  "insert.break": insertBreakActionImplementation,
4868
4581
  "insert.inline object": insertInlineObjectActionImplementation,
4869
4582
  "insert.soft break": insertSoftBreakActionImplementation,
4870
4583
  "insert.span": insertSpanActionImplementation,
4871
4584
  "insert.text": insertTextActionImplementation,
4872
4585
  effect: effectActionImplementation,
4873
- "list item.add": addListItemActionImplementation,
4874
- "list item.remove": removeListItemActionImplementation,
4875
- "list item.toggle": toggleListItemActionImplementation,
4876
4586
  "move.block": moveBlockActionImplementation,
4877
4587
  "move.block down": moveBlockDownActionImplementation,
4878
4588
  "move.block up": moveBlockUpActionImplementation,
4879
4589
  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
4590
+ select: selectActionImplementation
4888
4591
  };
4889
4592
  function performAction({
4890
4593
  context,
@@ -4933,13 +4636,6 @@ function performAction({
4933
4636
  });
4934
4637
  break;
4935
4638
  }
4936
- case "data transfer.set": {
4937
- behaviorActionImplementations["data transfer.set"]({
4938
- context,
4939
- action
4940
- });
4941
- break;
4942
- }
4943
4639
  case "decorator.add": {
4944
4640
  behaviorActionImplementations["decorator.add"]({
4945
4641
  context,
@@ -4996,20 +4692,6 @@ function performAction({
4996
4692
  });
4997
4693
  break;
4998
4694
  }
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
4695
  case "effect": {
5014
4696
  behaviorActionImplementations.effect({
5015
4697
  context,
@@ -5045,13 +4727,6 @@ function performAction({
5045
4727
  });
5046
4728
  break;
5047
4729
  }
5048
- case "insert.blocks": {
5049
- behaviorActionImplementations["insert.blocks"]({
5050
- context,
5051
- action
5052
- });
5053
- break;
5054
- }
5055
4730
  case "insert.inline object": {
5056
4731
  behaviorActionImplementations["insert.inline object"]({
5057
4732
  context,
@@ -5087,27 +4762,6 @@ function performAction({
5087
4762
  });
5088
4763
  break;
5089
4764
  }
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
4765
  case "move.block": {
5112
4766
  behaviorActionImplementations["move.block"]({
5113
4767
  context,
@@ -5131,57 +4785,8 @@ function performAction({
5131
4785
  }
5132
4786
  case "noop":
5133
4787
  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
4788
  default: {
5184
- behaviorActionImplementations["style.toggle"]({
4789
+ behaviorActionImplementations.select({
5185
4790
  context,
5186
4791
  action
5187
4792
  });
@@ -6090,67 +5695,116 @@ const keyIs = {
6090
5695
  actions: [() => [raise({
6091
5696
  type: "insert.soft break"
6092
5697
  })]]
6093
- }), toggleAnnotationOff = defineBehavior({
6094
- on: "annotation.toggle",
5698
+ }), internalInsertBehaviors = [defineBehavior({
5699
+ on: "insert.blocks",
6095
5700
  guard: ({
6096
- snapshot,
6097
5701
  event
6098
- }) => isActiveAnnotation(event.annotation.name)(snapshot),
5702
+ }) => event.placement === "before",
6099
5703
  actions: [({
6100
5704
  event
6101
- }) => [raise({
6102
- type: "annotation.remove",
6103
- annotation: event.annotation
6104
- })]]
6105
- }), toggleAnnotationOn = defineBehavior({
6106
- on: "annotation.toggle",
5705
+ }) => event.blocks.map((block, index) => raise({
5706
+ type: "insert.block",
5707
+ block,
5708
+ placement: index === 0 ? "before" : "after",
5709
+ select: "end"
5710
+ }))]
5711
+ }), defineBehavior({
5712
+ on: "insert.blocks",
6107
5713
  guard: ({
6108
- snapshot,
6109
5714
  event
6110
- }) => !isActiveAnnotation(event.annotation.name)(snapshot),
5715
+ }) => event.placement === "after",
6111
5716
  actions: [({
6112
5717
  event
6113
- }) => [raise({
6114
- type: "annotation.add",
6115
- annotation: event.annotation
6116
- })]]
6117
- }), toggleDecoratorOff = defineBehavior({
6118
- on: "decorator.toggle",
5718
+ }) => event.blocks.map((block) => raise({
5719
+ type: "insert.block",
5720
+ block,
5721
+ placement: "after",
5722
+ select: "end"
5723
+ }))]
5724
+ }), defineBehavior({
5725
+ on: "insert.blocks",
6119
5726
  guard: ({
6120
5727
  snapshot,
6121
5728
  event
6122
- }) => isActiveDecorator(event.decorator)(snapshot),
5729
+ }) => !(event.placement !== "auto" || !getFocusTextBlock(snapshot)),
6123
5730
  actions: [({
6124
5731
  event
6125
- }) => [raise({
6126
- type: "decorator.remove",
6127
- decorator: event.decorator
6128
- })]]
6129
- }), toggleDecoratorOn = defineBehavior({
6130
- on: "decorator.toggle",
5732
+ }) => event.blocks.length === 1 ? [raise({
5733
+ type: "insert.block",
5734
+ block: event.blocks[0],
5735
+ placement: "auto",
5736
+ select: "end"
5737
+ })] : event.blocks.flatMap((block, index) => index === 0 ? [raise({
5738
+ type: "insert.break"
5739
+ }), raise({
5740
+ type: "select.previous block",
5741
+ select: "end"
5742
+ }), raise({
5743
+ type: "insert.block",
5744
+ block,
5745
+ placement: "auto",
5746
+ select: "end"
5747
+ })] : index === event.blocks.length - 1 ? [raise({
5748
+ type: "select.next block",
5749
+ select: "start"
5750
+ }), raise({
5751
+ type: "insert.block",
5752
+ block,
5753
+ placement: "auto",
5754
+ select: "end"
5755
+ })] : [raise({
5756
+ type: "insert.block",
5757
+ block,
5758
+ placement: "after",
5759
+ select: "end"
5760
+ })])]
5761
+ }), defineBehavior({
5762
+ on: "insert.blocks",
6131
5763
  guard: ({
6132
- snapshot,
6133
5764
  event
6134
- }) => {
6135
- const manualSelection = event.offsets ? blockOffsetsToSelection({
6136
- value: snapshot.context.value,
6137
- offsets: event.offsets
6138
- }) : null;
6139
- return manualSelection ? !isActiveDecorator(event.decorator)({
6140
- ...snapshot,
6141
- context: {
6142
- ...snapshot.context,
6143
- selection: manualSelection
6144
- }
6145
- }) : !isActiveDecorator(event.decorator)(snapshot);
6146
- },
5765
+ }) => event.placement === "auto",
6147
5766
  actions: [({
6148
5767
  event
6149
- }) => [raise({
6150
- ...event,
6151
- type: "decorator.add"
6152
- })]]
6153
- }), toggleListItemOff = defineBehavior({
5768
+ }) => event.blocks.map((block, index) => raise({
5769
+ type: "insert.block",
5770
+ block,
5771
+ placement: index === 0 ? "auto" : "after",
5772
+ select: "end"
5773
+ }))]
5774
+ })], internalListItemBehaviors = [defineBehavior({
5775
+ on: "list item.add",
5776
+ guard: ({
5777
+ snapshot
5778
+ }) => ({
5779
+ selectedTextBlocks: getSelectedTextBlocks(snapshot)
5780
+ }),
5781
+ actions: [({
5782
+ event
5783
+ }, {
5784
+ selectedTextBlocks
5785
+ }) => selectedTextBlocks.map((block) => raise({
5786
+ type: "block.set",
5787
+ at: block.path,
5788
+ props: {
5789
+ level: 1,
5790
+ listItem: event.listItem
5791
+ }
5792
+ }))]
5793
+ }), defineBehavior({
5794
+ on: "list item.remove",
5795
+ guard: ({
5796
+ snapshot
5797
+ }) => ({
5798
+ selectedTextBlocks: getSelectedTextBlocks(snapshot)
5799
+ }),
5800
+ actions: [(_, {
5801
+ selectedTextBlocks
5802
+ }) => selectedTextBlocks.map((block) => raise({
5803
+ type: "block.unset",
5804
+ at: block.path,
5805
+ props: ["level", "listItem"]
5806
+ }))]
5807
+ }), defineBehavior({
6154
5808
  on: "list item.toggle",
6155
5809
  guard: ({
6156
5810
  snapshot,
@@ -6162,7 +5816,7 @@ const keyIs = {
6162
5816
  type: "list item.remove",
6163
5817
  listItem: event.listItem
6164
5818
  })]]
6165
- }), toggleListItemOn = defineBehavior({
5819
+ }), defineBehavior({
6166
5820
  on: "list item.toggle",
6167
5821
  guard: ({
6168
5822
  snapshot,
@@ -6174,7 +5828,85 @@ const keyIs = {
6174
5828
  type: "list item.add",
6175
5829
  listItem: event.listItem
6176
5830
  })]]
6177
- }), toggleStyleOff = defineBehavior({
5831
+ })], internalSelectBehaviors = [defineBehavior({
5832
+ on: "select.previous block",
5833
+ guard: ({
5834
+ snapshot,
5835
+ event
5836
+ }) => {
5837
+ const previousBlock = getPreviousBlock(snapshot);
5838
+ if (!previousBlock)
5839
+ return !1;
5840
+ const point = event.select === "end" ? getBlockEndPoint(previousBlock) : getBlockStartPoint(previousBlock);
5841
+ return {
5842
+ selection: {
5843
+ anchor: point,
5844
+ focus: point
5845
+ }
5846
+ };
5847
+ },
5848
+ actions: [(_, {
5849
+ selection
5850
+ }) => [raise({
5851
+ type: "select",
5852
+ selection
5853
+ })]]
5854
+ }), defineBehavior({
5855
+ on: "select.next block",
5856
+ guard: ({
5857
+ snapshot,
5858
+ event
5859
+ }) => {
5860
+ const nextBlock = getNextBlock(snapshot);
5861
+ if (!nextBlock)
5862
+ return !1;
5863
+ const point = event.select === "end" ? getBlockEndPoint(nextBlock) : getBlockStartPoint(nextBlock);
5864
+ return {
5865
+ selection: {
5866
+ anchor: point,
5867
+ focus: point
5868
+ }
5869
+ };
5870
+ },
5871
+ actions: [(_, {
5872
+ selection
5873
+ }) => [raise({
5874
+ type: "select",
5875
+ selection
5876
+ })]]
5877
+ })], internalStyleBehaviors = [defineBehavior({
5878
+ on: "style.add",
5879
+ guard: ({
5880
+ snapshot
5881
+ }) => ({
5882
+ selectedTextBlocks: getSelectedTextBlocks(snapshot)
5883
+ }),
5884
+ actions: [({
5885
+ event
5886
+ }, {
5887
+ selectedTextBlocks
5888
+ }) => selectedTextBlocks.map((block) => raise({
5889
+ type: "block.set",
5890
+ at: block.path,
5891
+ props: {
5892
+ style: event.style
5893
+ }
5894
+ }))]
5895
+ }), defineBehavior({
5896
+ on: "style.remove",
5897
+ guard: ({
5898
+ snapshot
5899
+ }) => ({
5900
+ selectedTextBlocks: getSelectedTextBlocks(snapshot)
5901
+ }),
5902
+ actions: [(_, {
5903
+ selectedTextBlocks
5904
+ }) => selectedTextBlocks.map((block) => raise({
5905
+ type: "block.unset",
5906
+ at: block.path,
5907
+ props: ["style"]
5908
+ }))]
5909
+ }), defineBehavior({
6178
5910
  on: "style.toggle",
6179
5911
  guard: ({
6180
5912
  snapshot,
@@ -6186,7 +5918,7 @@ const keyIs = {
6186
5918
  type: "style.remove",
6187
5919
  style: event.style
6188
5920
  })]]
6189
- }), toggleStyleOn = defineBehavior({
5921
+ }), defineBehavior({
6190
5922
  on: "style.toggle",
6191
5923
  guard: ({
6192
5924
  snapshot,
@@ -6198,6 +5930,66 @@ const keyIs = {
6198
5930
  type: "style.add",
6199
5931
  style: event.style
6200
5932
  })]]
5933
+ })], toggleAnnotationOff = defineBehavior({
5934
+ on: "annotation.toggle",
5935
+ guard: ({
5936
+ snapshot,
5937
+ event
5938
+ }) => isActiveAnnotation(event.annotation.name)(snapshot),
5939
+ actions: [({
5940
+ event
5941
+ }) => [raise({
5942
+ type: "annotation.remove",
5943
+ annotation: event.annotation
5944
+ })]]
5945
+ }), toggleAnnotationOn = defineBehavior({
5946
+ on: "annotation.toggle",
5947
+ guard: ({
5948
+ snapshot,
5949
+ event
5950
+ }) => !isActiveAnnotation(event.annotation.name)(snapshot),
5951
+ actions: [({
5952
+ event
5953
+ }) => [raise({
5954
+ type: "annotation.add",
5955
+ annotation: event.annotation
5956
+ })]]
5957
+ }), toggleDecoratorOff = defineBehavior({
5958
+ on: "decorator.toggle",
5959
+ guard: ({
5960
+ snapshot,
5961
+ event
5962
+ }) => isActiveDecorator(event.decorator)(snapshot),
5963
+ actions: [({
5964
+ event
5965
+ }) => [raise({
5966
+ type: "decorator.remove",
5967
+ decorator: event.decorator
5968
+ })]]
5969
+ }), toggleDecoratorOn = defineBehavior({
5970
+ on: "decorator.toggle",
5971
+ guard: ({
5972
+ snapshot,
5973
+ event
5974
+ }) => {
5975
+ const manualSelection = event.offsets ? blockOffsetsToSelection({
5976
+ value: snapshot.context.value,
5977
+ offsets: event.offsets
5978
+ }) : null;
5979
+ return manualSelection ? !isActiveDecorator(event.decorator)({
5980
+ ...snapshot,
5981
+ context: {
5982
+ ...snapshot.context,
5983
+ selection: manualSelection
5984
+ }
5985
+ }) : !isActiveDecorator(event.decorator)(snapshot);
5986
+ },
5987
+ actions: [({
5988
+ event
5989
+ }) => [raise({
5990
+ ...event,
5991
+ type: "decorator.add"
5992
+ })]]
6201
5993
  }), raiseDeserializationSuccessOrFailure = defineBehavior({
6202
5994
  on: "deserialize",
6203
5995
  guard: ({
@@ -6315,12 +6107,23 @@ const keyIs = {
6315
6107
  on: "serialization.success",
6316
6108
  actions: [({
6317
6109
  event
6318
- }) => [raise({
6319
- type: "data transfer.set",
6320
- data: event.data,
6321
- dataTransfer: event.originEvent.originEvent.dataTransfer,
6322
- mimeType: event.mimeType
6323
- })]]
6110
+ }) => [{
6111
+ type: "effect",
6112
+ effect: () => {
6113
+ event.originEvent.originEvent.dataTransfer.setData(event.mimeType, event.data);
6114
+ }
6115
+ }]]
6116
+ }),
6117
+ defineBehavior({
6118
+ on: "serialization.failure",
6119
+ actions: [({
6120
+ event
6121
+ }) => [{
6122
+ type: "effect",
6123
+ effect: () => {
6124
+ console.warn(`Serialization of ${event.mimeType} failed with reason "${event.reason}"`);
6125
+ }
6126
+ }]]
6324
6127
  }),
6325
6128
  defineBehavior({
6326
6129
  on: "drag.drop",
@@ -6467,6 +6270,17 @@ const keyIs = {
6467
6270
  placement: "auto"
6468
6271
  })]]
6469
6272
  }),
6273
+ defineBehavior({
6274
+ on: "deserialization.failure",
6275
+ actions: [({
6276
+ event
6277
+ }) => [{
6278
+ type: "effect",
6279
+ effect: () => {
6280
+ console.warn(`Deserialization of ${event.mimeType} failed with reason "${event.reason}"`);
6281
+ }
6282
+ }]]
6283
+ }),
6470
6284
  defineBehavior({
6471
6285
  on: "clipboard.paste",
6472
6286
  guard: ({
@@ -6504,14 +6318,14 @@ const keyIs = {
6504
6318
  originEvent: event
6505
6319
  })]]
6506
6320
  }),
6321
+ ...internalInsertBehaviors,
6322
+ ...internalListItemBehaviors,
6323
+ ...internalStyleBehaviors,
6324
+ ...internalSelectBehaviors,
6507
6325
  toggleAnnotationOff,
6508
6326
  toggleAnnotationOn,
6509
6327
  toggleDecoratorOff,
6510
6328
  toggleDecoratorOn,
6511
- toggleListItemOff,
6512
- toggleListItemOn,
6513
- toggleStyleOff,
6514
- toggleStyleOn,
6515
6329
  raiseDeserializationSuccessOrFailure,
6516
6330
  raiseSerializationSuccessOrFailure,
6517
6331
  raiseInsertSoftBreak
@@ -6519,6 +6333,12 @@ const keyIs = {
6519
6333
  function isKeyboardBehaviorEvent(event) {
6520
6334
  return event.type.startsWith("keyboard.");
6521
6335
  }
6336
+ function isInternalBehaviorEvent(event) {
6337
+ 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.");
6338
+ }
6339
+ function isNativeBehaviorEvent(event) {
6340
+ return isClipboardBehaviorEvent(event) || isDragBehaviorEvent(event) || isInputBehaviorEvent(event) || isKeyboardBehaviorEvent(event) || isMouseBehaviorEvent(event);
6341
+ }
6522
6342
  function isClipboardBehaviorEvent(event) {
6523
6343
  return event.type.startsWith("clipboard.");
6524
6344
  }
@@ -6535,6 +6355,9 @@ function isCustomBehaviorEvent(event) {
6535
6355
  return event.type.startsWith("custom.");
6536
6356
  }
6537
6357
  const debug$2 = debugWithName("behaviors:event");
6358
+ function eventCategory(event) {
6359
+ return isNativeBehaviorEvent(event) ? "native" : isInternalBehaviorEvent(event) ? "internal" : isCustomBehaviorEvent(event) ? "custom" : "synthetic";
6360
+ }
6538
6361
  function performEvent({
6539
6362
  behaviors,
6540
6363
  event,
@@ -6545,8 +6368,8 @@ function performEvent({
6545
6368
  nativeEvent,
6546
6369
  defaultActionCallback
6547
6370
  }) {
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 : {
6371
+ debug$2(`(${eventCategory(event)})`, JSON.stringify(event, null, 2));
6372
+ const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isInternalBehaviorEvent(event) ? void 0 : {
6550
6373
  ...event,
6551
6374
  editor
6552
6375
  }, eventBehaviors = behaviors.filter((behavior) => {
@@ -6938,7 +6761,7 @@ const editorMachine = setup({
6938
6761
  actions: "handle behavior event",
6939
6762
  guard: ({
6940
6763
  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"
6764
+ }) => event.behaviorEvent.type === "clipboard.copy" || event.behaviorEvent.type === "serialize" || event.behaviorEvent.type === "serialization.failure" || event.behaviorEvent.type === "serialization.success" || event.behaviorEvent.type === "select"
6942
6765
  }
6943
6766
  },
6944
6767
  states: {