@portabletext/editor 1.1.9 → 1.1.11

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.
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), types = require("@sanity/types"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), schema = require("@sanity/schema"), isHotkeyEsm = require("is-hotkey-esm"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), flatten = require("lodash/flatten.js"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), debounce = require("lodash/debounce.js"), content = require("@sanity/util/content");
3
+ var jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), types = require("@sanity/types"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), schema = require("@sanity/schema"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), flatten = require("lodash/flatten.js"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), debounce = require("lodash/debounce.js"), content = require("@sanity/util/content");
4
4
  function _interopDefaultCompat(e) {
5
5
  return e && typeof e == "object" && "default" in e ? e : { default: e };
6
6
  }
@@ -799,20 +799,15 @@ function getFocusBlockObject(context) {
799
799
  const focusBlock = getFocusBlock(context);
800
800
  return focusBlock && !types.isPortableTextTextBlock(focusBlock.node) ? { node: focusBlock.node, path: focusBlock.path } : void 0;
801
801
  }
802
- const overwriteSoftReturn = {
803
- on: "key down",
804
- guard: ({ event }) => isHotkeyEsm.isHotkey("shift+enter", event.nativeEvent),
805
- actions: [
806
- ({ event }) => (event.nativeEvent.preventDefault(), { type: "insert text", text: `
807
- ` })
808
- ]
809
- }, enterOnVoidBlock = {
810
- on: "key down",
811
- guard: ({ context, event }) => isHotkeyEsm.isHotkey("enter", event.nativeEvent) ? !!getFocusBlockObject(context) : !1,
812
- actions: [
813
- ({ event }) => (event.nativeEvent.preventDefault(), { type: "insert text block", decorators: [] })
814
- ]
815
- }, coreBehaviors = [overwriteSoftReturn, enterOnVoidBlock], debug$k = debugWithName("operationToPatches");
802
+ const softReturn = {
803
+ on: "insert soft break",
804
+ actions: [() => [{ type: "insert text", text: `
805
+ ` }]]
806
+ }, breakingVoidBlock = {
807
+ on: "insert break",
808
+ guard: ({ context }) => !!getFocusBlockObject(context),
809
+ actions: [() => [{ type: "insert text block", decorators: [] }]]
810
+ }, coreBehaviors = [softReturn, breakingVoidBlock], debug$k = debugWithName("operationToPatches");
816
811
  function createOperationToPatches(types2) {
817
812
  const textBlockName = types2.block.name;
818
813
  function insertTextPatch(editor, operation, beforeValue) {
@@ -1087,6 +1082,46 @@ function createOperationToPatches(types2) {
1087
1082
  splitNodePatch
1088
1083
  };
1089
1084
  }
1085
+ function createWithEventListeners(editorActor) {
1086
+ return function(editor) {
1087
+ return editor.deleteBackward = (unit) => {
1088
+ editorActor.send({
1089
+ type: "behavior event",
1090
+ behaviorEvent: {
1091
+ type: "delete backward",
1092
+ unit
1093
+ },
1094
+ editor
1095
+ });
1096
+ }, editor.insertBreak = () => {
1097
+ editorActor.send({
1098
+ type: "behavior event",
1099
+ behaviorEvent: {
1100
+ type: "insert break"
1101
+ },
1102
+ editor
1103
+ });
1104
+ }, editor.insertSoftBreak = () => {
1105
+ editorActor.send({
1106
+ type: "behavior event",
1107
+ behaviorEvent: {
1108
+ type: "insert soft break"
1109
+ },
1110
+ editor
1111
+ });
1112
+ }, editor.insertText = (text, options) => {
1113
+ editorActor.send({
1114
+ type: "behavior event",
1115
+ behaviorEvent: {
1116
+ type: "insert text",
1117
+ text,
1118
+ options
1119
+ },
1120
+ editor
1121
+ });
1122
+ }, editor;
1123
+ };
1124
+ }
1090
1125
  const debug$j = debugWithName("API:editable");
1091
1126
  function createWithEditableAPI(editorActor, portableTextEditor, types$1) {
1092
1127
  return function(editor) {
@@ -1573,127 +1608,6 @@ function createWithEditableAPI(editorActor, portableTextEditor, types$1) {
1573
1608
  }), editor;
1574
1609
  };
1575
1610
  }
1576
- function createWithInsertBreak(editorActor, types2) {
1577
- return function(editor) {
1578
- const { insertBreak } = editor;
1579
- return editor.insertBreak = () => {
1580
- if (!editor.selection) {
1581
- insertBreak();
1582
- return;
1583
- }
1584
- const [focusSpan] = Array.from(
1585
- slate.Editor.nodes(editor, {
1586
- mode: "lowest",
1587
- at: editor.selection.focus,
1588
- match: (n) => editor.isTextSpan(n),
1589
- voids: !1
1590
- })
1591
- )[0] ?? [void 0], focusDecorators = focusSpan.marks?.filter(
1592
- (mark) => types2.decorators.some((decorator) => decorator.value === mark)
1593
- ) ?? [], focusAnnotations = focusSpan.marks?.filter(
1594
- (mark) => !types2.decorators.some((decorator) => decorator.value === mark)
1595
- ) ?? [], focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
1596
- if (editor.isTextBlock(focusBlock)) {
1597
- const [start, end] = slate.Range.edges(editor.selection), atTheStartOfBlock = isEqual__default.default(end, {
1598
- path: [...focusBlockPath, 0],
1599
- offset: 0
1600
- });
1601
- if (atTheStartOfBlock && slate.Range.isCollapsed(editor.selection)) {
1602
- slate.Editor.insertNode(
1603
- editor,
1604
- editor.pteCreateTextBlock({
1605
- decorators: focusAnnotations.length === 0 ? focusDecorators : []
1606
- })
1607
- );
1608
- const [nextBlockPath] = slate.Path.next(focusBlockPath);
1609
- slate.Transforms.select(editor, {
1610
- anchor: { path: [nextBlockPath, 0], offset: 0 },
1611
- focus: { path: [nextBlockPath, 0], offset: 0 }
1612
- });
1613
- return;
1614
- }
1615
- const lastFocusBlockChild = focusBlock.children[focusBlock.children.length - 1], atTheEndOfBlock = isEqual__default.default(start, {
1616
- path: [...focusBlockPath, focusBlock.children.length - 1],
1617
- offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
1618
- });
1619
- if (atTheEndOfBlock && slate.Range.isCollapsed(editor.selection)) {
1620
- slate.Editor.insertNode(
1621
- editor,
1622
- editor.pteCreateTextBlock({
1623
- decorators: []
1624
- })
1625
- );
1626
- const [nextBlockPath] = slate.Path.next(focusBlockPath);
1627
- slate.Transforms.setSelection(editor, {
1628
- anchor: { path: [nextBlockPath, 0], offset: 0 },
1629
- focus: { path: [nextBlockPath, 0], offset: 0 }
1630
- });
1631
- return;
1632
- }
1633
- if (!atTheStartOfBlock && !atTheEndOfBlock) {
1634
- slate.Editor.withoutNormalizing(editor, () => {
1635
- if (!editor.selection)
1636
- return;
1637
- slate.Transforms.splitNodes(editor, {
1638
- at: editor.selection
1639
- });
1640
- const [nextNode, nextNodePath] = slate.Editor.node(
1641
- editor,
1642
- slate.Path.next(focusBlockPath),
1643
- { depth: 1 }
1644
- );
1645
- if (slate.Transforms.setSelection(editor, {
1646
- anchor: { path: [...nextNodePath, 0], offset: 0 },
1647
- focus: { path: [...nextNodePath, 0], offset: 0 }
1648
- }), editor.isTextBlock(nextNode) && nextNode.markDefs && nextNode.markDefs.length > 0) {
1649
- const newMarkDefKeys = /* @__PURE__ */ new Map(), prevNodeSpans = Array.from(
1650
- slate.Node.children(editor, focusBlockPath)
1651
- ).map((entry) => entry[0]).filter((node) => editor.isTextSpan(node)), children = slate.Node.children(editor, nextNodePath);
1652
- for (const [child, childPath] of children) {
1653
- if (!editor.isTextSpan(child))
1654
- continue;
1655
- const marks = child.marks ?? [];
1656
- for (const mark of marks)
1657
- types2.decorators.some(
1658
- (decorator) => decorator.value === mark
1659
- ) || prevNodeSpans.some(
1660
- (prevNodeSpan) => prevNodeSpan.marks?.includes(mark)
1661
- ) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(
1662
- mark,
1663
- editorActor.getSnapshot().context.keyGenerator()
1664
- );
1665
- const newMarks = marks.map(
1666
- (mark) => newMarkDefKeys.get(mark) ?? mark
1667
- );
1668
- isEqual__default.default(marks, newMarks) || slate.Transforms.setNodes(
1669
- editor,
1670
- { marks: newMarks },
1671
- {
1672
- at: childPath
1673
- }
1674
- );
1675
- }
1676
- const newMarkDefs = nextNode.markDefs.map((markDef) => ({
1677
- ...markDef,
1678
- _key: newMarkDefKeys.get(markDef._key) ?? markDef._key
1679
- }));
1680
- isEqual__default.default(nextNode.markDefs, newMarkDefs) || slate.Transforms.setNodes(
1681
- editor,
1682
- { markDefs: newMarkDefs },
1683
- {
1684
- at: nextNodePath,
1685
- match: (node) => editor.isTextBlock(node)
1686
- }
1687
- );
1688
- }
1689
- }), editor.onChange();
1690
- return;
1691
- }
1692
- }
1693
- insertBreak();
1694
- }, editor;
1695
- };
1696
- }
1697
1611
  function withRemoteChanges(editor, fn) {
1698
1612
  const prev = isChangingRemotely(editor) || !1;
1699
1613
  IS_PROCESSING_REMOTE_CHANGES.set(editor, !0), fn(), IS_PROCESSING_REMOTE_CHANGES.set(editor, prev);
@@ -3778,6 +3692,8 @@ function createWithUtils({
3778
3692
  _type: schemaTypes.block.name,
3779
3693
  _key: editorActor.getSnapshot().context.keyGenerator(),
3780
3694
  style: schemaTypes.styles[0].value || "normal",
3695
+ ...options.listItem ? { listItem: options.listItem } : {},
3696
+ ...options.level ? { level: options.level } : {},
3781
3697
  markDefs: [],
3782
3698
  children: [
3783
3699
  {
@@ -4397,14 +4313,14 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4397
4313
  ), withPortableTextBlockStyle = createWithPortableTextBlockStyle(
4398
4314
  editorActor,
4399
4315
  schemaTypes
4400
- ), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(editorActor, schemaTypes), withUtils = createWithUtils({
4316
+ ), withPlaceholderBlock = createWithPlaceholderBlock(), withUtils = createWithUtils({
4401
4317
  editorActor,
4402
4318
  schemaTypes,
4403
4319
  portableTextEditor
4404
4320
  }), withPortableTextSelections = createWithPortableTextSelections(
4405
4321
  editorActor,
4406
4322
  schemaTypes
4407
- );
4323
+ ), withEventListeners = createWithEventListeners(editorActor);
4408
4324
  return e.destroy = () => {
4409
4325
  const originalFunctions = originalFnMap.get(e);
4410
4326
  if (!originalFunctions)
@@ -4418,9 +4334,7 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4418
4334
  withUtils(
4419
4335
  withPlaceholderBlock(
4420
4336
  withPortableTextLists(
4421
- withPortableTextSelections(
4422
- withEditableAPI(withInsertBreak(e))
4423
- )
4337
+ withPortableTextSelections(withEditableAPI(e))
4424
4338
  )
4425
4339
  )
4426
4340
  )
@@ -4430,18 +4344,18 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4430
4344
  ),
4431
4345
  subscribe: () => noop__default.default
4432
4346
  } : {
4433
- editor: withSchemaTypes(
4434
- withObjectKeys(
4435
- withPortableTextMarkModel(
4436
- withPortableTextBlockStyle(
4437
- withPortableTextLists(
4438
- withPlaceholderBlock(
4439
- withUtils(
4440
- withMaxBlocks(
4441
- withUndoRedo(
4442
- withPatches(
4443
- withPortableTextSelections(
4444
- withEditableAPI(withInsertBreak(e))
4347
+ editor: withEventListeners(
4348
+ withSchemaTypes(
4349
+ withObjectKeys(
4350
+ withPortableTextMarkModel(
4351
+ withPortableTextBlockStyle(
4352
+ withPortableTextLists(
4353
+ withPlaceholderBlock(
4354
+ withUtils(
4355
+ withMaxBlocks(
4356
+ withUndoRedo(
4357
+ withPatches(
4358
+ withPortableTextSelections(withEditableAPI(e))
4445
4359
  )
4446
4360
  )
4447
4361
  )
@@ -4812,26 +4726,162 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
4812
4726
  debug$4("Value from props changed, syncing new value"), syncValue(value), isInitialValueFromProps.current && (editorActor.send({ type: "ready" }), isInitialValueFromProps.current = !1);
4813
4727
  }, [editorActor, syncValue, value]), null;
4814
4728
  }
4815
- const EditorActorContext = react.createContext({}), behaviorActionImplementations = {
4816
- "apply block style": ({ event }) => {
4817
- for (const path of event.paths) {
4729
+ const EditorActorContext = react.createContext({}), insertBreakActionImplementation = ({ context, action }) => {
4730
+ const keyGenerator = context.keyGenerator, schema2 = context.schema, editor = action.editor;
4731
+ if (!editor.selection)
4732
+ return;
4733
+ const [focusSpan] = Array.from(
4734
+ slate.Editor.nodes(editor, {
4735
+ mode: "lowest",
4736
+ at: editor.selection.focus,
4737
+ match: (n) => editor.isTextSpan(n),
4738
+ voids: !1
4739
+ })
4740
+ )[0] ?? [void 0], focusDecorators = focusSpan.marks?.filter(
4741
+ (mark) => schema2.decorators.some((decorator) => decorator.value === mark)
4742
+ ) ?? [], focusAnnotations = focusSpan.marks?.filter(
4743
+ (mark) => !schema2.decorators.some((decorator) => decorator.value === mark)
4744
+ ) ?? [], focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
4745
+ if (editor.isTextBlock(focusBlock)) {
4746
+ const [start, end] = slate.Range.edges(editor.selection), atTheStartOfBlock = isEqual__default.default(end, {
4747
+ path: [...focusBlockPath, 0],
4748
+ offset: 0
4749
+ });
4750
+ if (atTheStartOfBlock && slate.Range.isCollapsed(editor.selection)) {
4751
+ slate.Editor.insertNode(
4752
+ editor,
4753
+ editor.pteCreateTextBlock({
4754
+ decorators: focusAnnotations.length === 0 ? focusDecorators : [],
4755
+ listItem: focusBlock.listItem,
4756
+ level: focusBlock.level
4757
+ })
4758
+ );
4759
+ const [nextBlockPath] = slate.Path.next(focusBlockPath);
4760
+ slate.Transforms.select(editor, {
4761
+ anchor: { path: [nextBlockPath, 0], offset: 0 },
4762
+ focus: { path: [nextBlockPath, 0], offset: 0 }
4763
+ });
4764
+ return;
4765
+ }
4766
+ const lastFocusBlockChild = focusBlock.children[focusBlock.children.length - 1], atTheEndOfBlock = isEqual__default.default(start, {
4767
+ path: [...focusBlockPath, focusBlock.children.length - 1],
4768
+ offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
4769
+ });
4770
+ if (atTheEndOfBlock && slate.Range.isCollapsed(editor.selection)) {
4771
+ slate.Editor.insertNode(
4772
+ editor,
4773
+ editor.pteCreateTextBlock({
4774
+ decorators: [],
4775
+ listItem: focusBlock.listItem,
4776
+ level: focusBlock.level
4777
+ })
4778
+ );
4779
+ const [nextBlockPath] = slate.Path.next(focusBlockPath);
4780
+ slate.Transforms.setSelection(editor, {
4781
+ anchor: { path: [nextBlockPath, 0], offset: 0 },
4782
+ focus: { path: [nextBlockPath, 0], offset: 0 }
4783
+ });
4784
+ return;
4785
+ }
4786
+ if (!atTheStartOfBlock && !atTheEndOfBlock) {
4787
+ slate.Editor.withoutNormalizing(editor, () => {
4788
+ if (!editor.selection)
4789
+ return;
4790
+ slate.Transforms.splitNodes(editor, {
4791
+ at: editor.selection
4792
+ });
4793
+ const [nextNode, nextNodePath] = slate.Editor.node(
4794
+ editor,
4795
+ slate.Path.next(focusBlockPath),
4796
+ { depth: 1 }
4797
+ );
4798
+ if (slate.Transforms.setSelection(editor, {
4799
+ anchor: { path: [...nextNodePath, 0], offset: 0 },
4800
+ focus: { path: [...nextNodePath, 0], offset: 0 }
4801
+ }), editor.isTextBlock(nextNode) && nextNode.markDefs && nextNode.markDefs.length > 0) {
4802
+ const newMarkDefKeys = /* @__PURE__ */ new Map(), prevNodeSpans = Array.from(
4803
+ slate.Node.children(editor, focusBlockPath)
4804
+ ).map((entry) => entry[0]).filter((node) => editor.isTextSpan(node)), children = slate.Node.children(editor, nextNodePath);
4805
+ for (const [child, childPath] of children) {
4806
+ if (!editor.isTextSpan(child))
4807
+ continue;
4808
+ const marks = child.marks ?? [];
4809
+ for (const mark of marks)
4810
+ schema2.decorators.some((decorator) => decorator.value === mark) || prevNodeSpans.some(
4811
+ (prevNodeSpan) => prevNodeSpan.marks?.includes(mark)
4812
+ ) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
4813
+ const newMarks = marks.map(
4814
+ (mark) => newMarkDefKeys.get(mark) ?? mark
4815
+ );
4816
+ isEqual__default.default(marks, newMarks) || slate.Transforms.setNodes(
4817
+ editor,
4818
+ { marks: newMarks },
4819
+ {
4820
+ at: childPath
4821
+ }
4822
+ );
4823
+ }
4824
+ const newMarkDefs = nextNode.markDefs.map((markDef) => ({
4825
+ ...markDef,
4826
+ _key: newMarkDefKeys.get(markDef._key) ?? markDef._key
4827
+ }));
4828
+ isEqual__default.default(nextNode.markDefs, newMarkDefs) || slate.Transforms.setNodes(
4829
+ editor,
4830
+ { markDefs: newMarkDefs },
4831
+ {
4832
+ at: nextNodePath,
4833
+ match: (node) => editor.isTextBlock(node)
4834
+ }
4835
+ );
4836
+ }
4837
+ }), editor.onChange();
4838
+ return;
4839
+ }
4840
+ }
4841
+ }, behaviorActionImplementations = {
4842
+ "set block": ({ action }) => {
4843
+ for (const path of action.paths) {
4844
+ const at = toSlateRange(
4845
+ { anchor: { path, offset: 0 }, focus: { path, offset: 0 } },
4846
+ action.editor
4847
+ );
4848
+ slate.Transforms.setNodes(
4849
+ action.editor,
4850
+ {
4851
+ ...action.style ? { style: action.style } : {},
4852
+ ...action.listItem ? { listItem: action.listItem } : {},
4853
+ ...action.level ? { level: action.level } : {}
4854
+ },
4855
+ { at }
4856
+ );
4857
+ }
4858
+ },
4859
+ "unset block": ({ action }) => {
4860
+ for (const path of action.paths) {
4818
4861
  const at = toSlateRange(
4819
4862
  { anchor: { path, offset: 0 }, focus: { path, offset: 0 } },
4820
- event.editor
4863
+ action.editor
4821
4864
  );
4822
- slate.Transforms.setNodes(event.editor, { style: event.style }, { at });
4865
+ slate.Transforms.unsetNodes(action.editor, action.props, { at });
4823
4866
  }
4824
4867
  },
4825
- "delete text": ({ event }) => {
4826
- slate.Transforms.delete(event.editor, {
4827
- at: toSlateRange(event.selection, event.editor)
4868
+ "delete backward": ({ action }) => {
4869
+ slate.deleteBackward(action.editor, action.unit);
4870
+ },
4871
+ "delete text": ({ action }) => {
4872
+ slate.Transforms.delete(action.editor, {
4873
+ at: toSlateRange(action.selection, action.editor)
4828
4874
  });
4829
4875
  },
4830
- "insert text": ({ event }) => {
4831
- slate.Editor.insertText(event.editor, event.text);
4876
+ "insert break": insertBreakActionImplementation,
4877
+ // This mimics Slate's internal which also just does a regular insert break
4878
+ // when on soft break
4879
+ "insert soft break": insertBreakActionImplementation,
4880
+ "insert text": ({ action }) => {
4881
+ slate.insertText(action.editor, action.text);
4832
4882
  },
4833
- "insert text block": ({ context, event }) => {
4834
- slate.Editor.insertNode(event.editor, {
4883
+ "insert text block": ({ context, action }) => {
4884
+ slate.Editor.insertNode(action.editor, {
4835
4885
  _key: context.keyGenerator(),
4836
4886
  _type: context.schema.block.name,
4837
4887
  style: context.schema.styles[0].value ?? "normal",
@@ -4844,8 +4894,47 @@ const EditorActorContext = react.createContext({}), behaviorActionImplementation
4844
4894
  }
4845
4895
  ]
4846
4896
  });
4897
+ },
4898
+ effect: ({ action }) => {
4899
+ action.effect();
4900
+ }
4901
+ };
4902
+ function performDefaultAction({
4903
+ context,
4904
+ action
4905
+ }) {
4906
+ switch (action.type) {
4907
+ case "delete backward": {
4908
+ behaviorActionImplementations["delete backward"]({
4909
+ context,
4910
+ action
4911
+ });
4912
+ break;
4913
+ }
4914
+ case "insert break": {
4915
+ behaviorActionImplementations["insert break"]({
4916
+ context,
4917
+ action
4918
+ });
4919
+ break;
4920
+ }
4921
+ case "insert soft break": {
4922
+ behaviorActionImplementations["insert soft break"]({
4923
+ context,
4924
+ action
4925
+ });
4926
+ break;
4927
+ }
4928
+ case "insert text": {
4929
+ behaviorActionImplementations["insert text"]({
4930
+ context,
4931
+ action
4932
+ });
4933
+ break;
4934
+ }
4847
4935
  }
4848
- }, networkLogic = xstate.fromCallback(({ sendBack }) => {
4936
+ }
4937
+ const networkLogic = xstate.fromCallback(({ sendBack }) => {
4849
4938
  const onlineHandler = () => {
4850
4939
  sendBack({ type: "online" });
4851
4940
  }, offlineHandler = () => {
@@ -4878,12 +4967,17 @@ const EditorActorContext = react.createContext({}), behaviorActionImplementation
4878
4967
  pendingEvents: []
4879
4968
  }),
4880
4969
  "handle behavior event": xstate.enqueueActions(({ context, event, enqueue }) => {
4881
- xstate.assertEvent(event, ["key down", "before insert text"]);
4882
- const eventBehaviors = context.behaviors.filter(
4883
- (behavior) => behavior.on === event.type
4970
+ xstate.assertEvent(event, ["behavior event"]);
4971
+ const defaultAction = {
4972
+ ...event.behaviorEvent,
4973
+ editor: event.editor
4974
+ }, eventBehaviors = context.behaviors.filter(
4975
+ (behavior) => behavior.on === event.behaviorEvent.type
4884
4976
  );
4885
- if (eventBehaviors.length === 0)
4977
+ if (eventBehaviors.length === 0) {
4978
+ performDefaultAction({ context, action: defaultAction });
4886
4979
  return;
4980
+ }
4887
4981
  const value = fromSlateValue(
4888
4982
  event.editor.children,
4889
4983
  context.schema.block.name,
@@ -4896,7 +4990,7 @@ const EditorActorContext = react.createContext({}), behaviorActionImplementation
4896
4990
  if (!selection) {
4897
4991
  console.warn(
4898
4992
  `Unable to handle event ${event.type} due to missing selection`
4899
- );
4993
+ ), performDefaultAction({ context, action: defaultAction });
4900
4994
  return;
4901
4995
  }
4902
4996
  const behaviorContext = {
@@ -4904,22 +4998,28 @@ const EditorActorContext = react.createContext({}), behaviorActionImplementation
4904
4998
  value,
4905
4999
  selection
4906
5000
  };
5001
+ let behaviorOverwritten = !1;
4907
5002
  for (const eventBehavior of eventBehaviors) {
4908
5003
  const shouldRun = eventBehavior.guard?.({
4909
5004
  context: behaviorContext,
4910
- event
5005
+ event: event.behaviorEvent
4911
5006
  }) ?? !0;
4912
5007
  if (!shouldRun)
4913
5008
  continue;
4914
- const actions = eventBehavior.actions.map(
4915
- (action) => action({ context: behaviorContext, event }, shouldRun)
5009
+ const actionIntendSets = eventBehavior.actions.map(
5010
+ (actionSet) => actionSet(
5011
+ { context: behaviorContext, event: event.behaviorEvent },
5012
+ shouldRun
5013
+ )
4916
5014
  );
4917
- for (const action of actions)
4918
- typeof action == "object" && enqueue.raise({
4919
- ...action,
4920
- editor: event.editor
5015
+ for (const actionIntends of actionIntendSets)
5016
+ behaviorOverwritten = actionIntends.length > 0 && actionIntends.some((actionIntend) => actionIntend.type !== "effect"), enqueue.raise({
5017
+ type: "behavior action intends",
5018
+ editor: event.editor,
5019
+ actionIntends
4921
5020
  });
4922
5021
  }
5022
+ behaviorOverwritten || performDefaultAction({ context, action: defaultAction });
4923
5023
  })
4924
5024
  },
4925
5025
  actors: {
@@ -4951,23 +5051,83 @@ const EditorActorContext = react.createContext({}), behaviorActionImplementation
4951
5051
  loading: { actions: xstate.emit({ type: "loading" }) },
4952
5052
  "done loading": { actions: xstate.emit({ type: "done loading" }) },
4953
5053
  "update schema": { actions: "assign schema" },
4954
- "key down": {
4955
- actions: ["handle behavior event"]
4956
- },
4957
- "before insert text": {
4958
- actions: ["handle behavior event"]
4959
- },
4960
- "apply block style": {
4961
- actions: [behaviorActionImplementations["apply block style"]]
4962
- },
4963
- "delete text": {
4964
- actions: [behaviorActionImplementations["delete text"]]
4965
- },
4966
- "insert text": {
4967
- actions: [behaviorActionImplementations["insert text"]]
4968
- },
4969
- "insert text block": {
4970
- actions: [behaviorActionImplementations["insert text block"]]
5054
+ "behavior event": { actions: "handle behavior event" },
5055
+ "behavior action intends": {
5056
+ actions: [
5057
+ ({ context, event }) => {
5058
+ slate.Editor.withoutNormalizing(event.editor, () => {
5059
+ for (const actionIntend of event.actionIntends) {
5060
+ const action = {
5061
+ ...actionIntend,
5062
+ editor: event.editor
5063
+ };
5064
+ switch (action.type) {
5065
+ case "delete backward": {
5066
+ behaviorActionImplementations["delete backward"]({
5067
+ context,
5068
+ action
5069
+ });
5070
+ break;
5071
+ }
5072
+ case "delete text": {
5073
+ behaviorActionImplementations["delete text"]({
5074
+ context,
5075
+ action
5076
+ });
5077
+ break;
5078
+ }
5079
+ case "insert break": {
5080
+ behaviorActionImplementations["insert break"]({
5081
+ context,
5082
+ action
5083
+ });
5084
+ break;
5085
+ }
5086
+ case "insert soft break": {
5087
+ behaviorActionImplementations["insert soft break"]({
5088
+ context,
5089
+ action
5090
+ });
5091
+ break;
5092
+ }
5093
+ case "insert text": {
5094
+ behaviorActionImplementations["insert text"]({
5095
+ context,
5096
+ action
5097
+ });
5098
+ break;
5099
+ }
5100
+ case "insert text block": {
5101
+ behaviorActionImplementations["insert text block"]({
5102
+ context,
5103
+ action
5104
+ });
5105
+ break;
5106
+ }
5107
+ case "set block": {
5108
+ behaviorActionImplementations["set block"]({
5109
+ context,
5110
+ action
5111
+ });
5112
+ break;
5113
+ }
5114
+ case "unset block": {
5115
+ behaviorActionImplementations["unset block"]({
5116
+ context,
5117
+ action
5118
+ });
5119
+ break;
5120
+ }
5121
+ default:
5122
+ behaviorActionImplementations.effect({
5123
+ context,
5124
+ action
5125
+ });
5126
+ }
5127
+ }
5128
+ }), event.editor.onChange();
5129
+ }
5130
+ ]
4971
5131
  }
4972
5132
  },
4973
5133
  initial: "pristine",
@@ -5599,11 +5759,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5599
5759
  [editorActor, onBlur]
5600
5760
  ), handleOnBeforeInput = react.useCallback(
5601
5761
  (event) => {
5602
- onBeforeInput && onBeforeInput(event), !event.defaultPrevented && event.inputType === "insertText" && editorActor.send({
5603
- type: "before insert text",
5604
- nativeEvent: event,
5605
- editor: slateEditor
5606
- });
5762
+ onBeforeInput && onBeforeInput(event);
5607
5763
  },
5608
5764
  [onBeforeInput]
5609
5765
  ), validateSelection = react.useCallback(() => {
@@ -5642,11 +5798,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5642
5798
  }, [validateSelection, editableElement]);
5643
5799
  const handleKeyDown = react.useCallback(
5644
5800
  (event) => {
5645
- props.onKeyDown && props.onKeyDown(event), event.isDefaultPrevented() || (editorActor.send({
5646
- type: "key down",
5647
- nativeEvent: event.nativeEvent,
5648
- editor: slateEditor
5649
- }), slateEditor.pteWithHotKeys(event));
5801
+ props.onKeyDown && props.onKeyDown(event), event.isDefaultPrevented() || slateEditor.pteWithHotKeys(event);
5650
5802
  },
5651
5803
  [props, slateEditor]
5652
5804
  ), scrollSelectionIntoViewToSlate = react.useMemo(() => {