@portabletext/editor 1.47.15 → 1.48.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 (45) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +8 -9
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/editor-provider.cjs +154 -110
  4. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  5. package/lib/_chunks-es/behavior.core.js +8 -9
  6. package/lib/_chunks-es/behavior.core.js.map +1 -1
  7. package/lib/_chunks-es/editor-provider.js +155 -111
  8. package/lib/_chunks-es/editor-provider.js.map +1 -1
  9. package/lib/behaviors/index.cjs +11 -11
  10. package/lib/behaviors/index.cjs.map +1 -1
  11. package/lib/behaviors/index.d.cts +11 -8
  12. package/lib/behaviors/index.d.ts +11 -8
  13. package/lib/behaviors/index.js +12 -12
  14. package/lib/behaviors/index.js.map +1 -1
  15. package/lib/index.cjs +4 -4
  16. package/lib/index.cjs.map +1 -1
  17. package/lib/index.d.cts +4 -3
  18. package/lib/index.d.ts +4 -3
  19. package/lib/index.js +5 -5
  20. package/lib/index.js.map +1 -1
  21. package/lib/plugins/index.cjs +7 -11
  22. package/lib/plugins/index.cjs.map +1 -1
  23. package/lib/plugins/index.d.cts +4 -3
  24. package/lib/plugins/index.d.ts +4 -3
  25. package/lib/plugins/index.js +8 -12
  26. package/lib/plugins/index.js.map +1 -1
  27. package/lib/selectors/index.d.cts +4 -3
  28. package/lib/selectors/index.d.ts +4 -3
  29. package/lib/utils/index.d.cts +4 -3
  30. package/lib/utils/index.d.ts +4 -3
  31. package/package.json +1 -1
  32. package/src/behavior-actions/behavior.actions.ts +0 -9
  33. package/src/behaviors/behavior.core.dnd.ts +1 -1
  34. package/src/behaviors/behavior.default.ts +3 -3
  35. package/src/behaviors/behavior.emoji-picker.ts +7 -9
  36. package/src/behaviors/behavior.perform-event.ts +143 -76
  37. package/src/behaviors/behavior.types.action.ts +14 -11
  38. package/src/behaviors/index.ts +1 -1
  39. package/src/editor/components/Element.tsx +3 -5
  40. package/src/editor/editor-machine.ts +1 -0
  41. package/src/editor/plugins/createWithUndoRedo.ts +8 -11
  42. package/src/editor/with-applying-behavior-actions.ts +9 -11
  43. package/src/plugins/plugin.decorator-shortcut.ts +3 -2
  44. package/src/plugins/plugin.one-line.tsx +3 -3
  45. package/src/behavior-actions/behavior.action.noop.ts +0 -5
@@ -5,7 +5,7 @@ import { withReact, ReactEditor, Slate } from "slate-react";
5
5
  import { c } from "react-compiler-runtime";
6
6
  import debug$g from "debug";
7
7
  import isEqual from "lodash/isEqual.js";
8
- import { Element, Text, Editor, Path, Operation, Transforms, Node, Range, Point, createEditor } from "slate";
8
+ import { Element, Text, Editor, Operation, Transforms, Path, Node, Range, Point, createEditor } from "slate";
9
9
  import { setup, stateIn, fromCallback, assign, enqueueActions, emit, assertEvent, and, not, createActor } from "xstate";
10
10
  import { unset, set, setIfMissing, insert, diffMatchPatch as diffMatchPatch$1, applyAll } from "@portabletext/patches";
11
11
  import { isPortableTextTextBlock, isKeySegment, isPortableTextSpan as isPortableTextSpan$1, isPortableTextListBlock, defineType, defineField } from "@sanity/types";
@@ -1336,19 +1336,19 @@ function getCurrentActionId(editor) {
1336
1336
  function isApplyingBehaviorActions(editor) {
1337
1337
  return getCurrentActionId(editor) !== void 0;
1338
1338
  }
1339
- const CURRENT_BEHAVIOR_ACTION_SET = /* @__PURE__ */ new WeakMap();
1340
- function withApplyingBehaviorActionSet(editor, fn) {
1341
- const current = CURRENT_BEHAVIOR_ACTION_SET.get(editor);
1339
+ const CURRENT_UNDO_STEP = /* @__PURE__ */ new WeakMap();
1340
+ function withUndoStep(editor, fn) {
1341
+ const current = CURRENT_UNDO_STEP.get(editor);
1342
1342
  if (current) {
1343
1343
  fn();
1344
1344
  return;
1345
1345
  }
1346
- CURRENT_BEHAVIOR_ACTION_SET.set(editor, current ?? {
1347
- actionSetId: defaultKeyGenerator()
1348
- }), fn(), CURRENT_BEHAVIOR_ACTION_SET.set(editor, void 0);
1346
+ CURRENT_UNDO_STEP.set(editor, current ?? {
1347
+ undoStepId: defaultKeyGenerator()
1348
+ }), fn(), CURRENT_UNDO_STEP.set(editor, void 0);
1349
1349
  }
1350
- function getCurrentBehaviorActionSetId(editor) {
1351
- return CURRENT_BEHAVIOR_ACTION_SET.get(editor)?.actionSetId;
1350
+ function getCurrentUndoStepId(editor) {
1351
+ return CURRENT_UNDO_STEP.get(editor)?.undoStepId;
1352
1352
  }
1353
1353
  const debug$f = debugWithName("plugin:withUndoRedo"), SAVING = /* @__PURE__ */ new WeakMap(), REMOTE_PATCHES = /* @__PURE__ */ new WeakMap(), UNDO_STEP_LIMIT = 1e3, isSaving = (editor) => {
1354
1354
  const state = SAVING.get(editor);
@@ -1361,7 +1361,7 @@ function createWithUndoRedo(options) {
1361
1361
  return (editor) => {
1362
1362
  let previousSnapshot = fromSlateValue(editor.children, editorActor.getSnapshot().context.schema.block.name);
1363
1363
  const remotePatches = getRemotePatches(editor);
1364
- let previousBehaviorActionSetId = getCurrentBehaviorActionSetId(editor);
1364
+ let previousUndoStepId = getCurrentUndoStepId(editor);
1365
1365
  options.subscriptions.push(() => {
1366
1366
  debug$f("Subscribing to patches");
1367
1367
  const sub = editorActor.on("patches", ({
@@ -1417,10 +1417,10 @@ function createWithUndoRedo(options) {
1417
1417
  history
1418
1418
  } = editor, {
1419
1419
  undos
1420
- } = history, step = undos[undos.length - 1], lastOp = step && step.operations && step.operations[step.operations.length - 1], overwrite = shouldOverwrite(op, lastOp), save = isSaving(editor), currentBehaviorActionSetId = getCurrentBehaviorActionSetId(editor);
1421
- let merge = currentBehaviorActionSetId !== void 0 && previousBehaviorActionSetId === void 0 ? !1 : currentBehaviorActionSetId !== void 0 && previousBehaviorActionSetId !== void 0 ? currentBehaviorActionSetId === previousBehaviorActionSetId : !0;
1420
+ } = history, step = undos[undos.length - 1], lastOp = step && step.operations && step.operations[step.operations.length - 1], overwrite = shouldOverwrite(op, lastOp), save = isSaving(editor), currentUndoStepId = getCurrentUndoStepId(editor);
1421
+ let merge = currentUndoStepId !== void 0 && previousUndoStepId === void 0 ? !1 : currentUndoStepId !== void 0 && previousUndoStepId !== void 0 ? currentUndoStepId === previousUndoStepId : !0;
1422
1422
  if (save) {
1423
- if (step ? operations.length === 0 && (merge = currentBehaviorActionSetId === void 0 && previousBehaviorActionSetId === void 0 ? shouldMerge(op, lastOp) || overwrite : merge) : merge = !1, step && merge)
1423
+ if (step ? operations.length === 0 && (merge = currentUndoStepId === void 0 && previousUndoStepId === void 0 ? shouldMerge(op, lastOp) || overwrite : merge) : merge = !1, step && merge)
1424
1424
  step.operations.push(op);
1425
1425
  else {
1426
1426
  const newStep = {
@@ -1433,7 +1433,7 @@ function createWithUndoRedo(options) {
1433
1433
  undos.shift();
1434
1434
  shouldClear(op) && (history.redos = []);
1435
1435
  }
1436
- previousBehaviorActionSetId = currentBehaviorActionSetId, apply2(op);
1436
+ previousUndoStepId = currentUndoStepId, apply2(op);
1437
1437
  }, editor;
1438
1438
  };
1439
1439
  }
@@ -4037,7 +4037,6 @@ const moveBackwardActionImplementation = ({
4037
4037
  unit: "character",
4038
4038
  distance: action.distance
4039
4039
  });
4040
- }, noopActionImplementation = () => {
4041
4040
  }, selectActionImplementation = ({
4042
4041
  action
4043
4042
  }) => {
@@ -4064,7 +4063,6 @@ const moveBackwardActionImplementation = ({
4064
4063
  "move.backward": moveBackwardActionImplementation,
4065
4064
  "move.block": moveBlockActionImplementation,
4066
4065
  "move.forward": moveForwardActionImplementation,
4067
- noop: noopActionImplementation,
4068
4066
  select: selectActionImplementation
4069
4067
  };
4070
4068
  function performAction({
@@ -4212,8 +4210,6 @@ function performAction({
4212
4210
  });
4213
4211
  break;
4214
4212
  }
4215
- case "noop":
4216
- break;
4217
4213
  default: {
4218
4214
  behaviorActionImplementations.select({
4219
4215
  context,
@@ -6393,9 +6389,7 @@ const EditorContext = createGloballyScopedContext("@portabletext/editor/context/
6393
6389
  const focusSpan = getFocusSpan(snapshot), selectionCollapsed = isSelectionCollapsed(snapshot);
6394
6390
  return focusSpan && selectionCollapsed;
6395
6391
  },
6396
- actions: [() => [{
6397
- type: "noop"
6398
- }]]
6392
+ actions: []
6399
6393
  }),
6400
6394
  defineBehavior({
6401
6395
  on: "clipboard.copy",
@@ -6414,9 +6408,7 @@ const EditorContext = createGloballyScopedContext("@portabletext/editor/context/
6414
6408
  const focusSpan = getFocusSpan(snapshot), selectionCollapsed = isSelectionCollapsed(snapshot);
6415
6409
  return focusSpan && selectionCollapsed;
6416
6410
  },
6417
- actions: [() => [{
6418
- type: "noop"
6419
- }]]
6411
+ actions: []
6420
6412
  }),
6421
6413
  defineBehavior({
6422
6414
  on: "clipboard.cut",
@@ -6483,9 +6475,7 @@ const EditorContext = createGloballyScopedContext("@portabletext/editor/context/
6483
6475
  }
6484
6476
  }) : !1;
6485
6477
  },
6486
- actions: [() => [{
6487
- type: "noop"
6488
- }]]
6478
+ actions: []
6489
6479
  }),
6490
6480
  defineBehavior({
6491
6481
  on: "drag.drop",
@@ -6691,6 +6681,7 @@ function eventCategory(event) {
6691
6681
  function performEvent({
6692
6682
  mode,
6693
6683
  behaviors,
6684
+ remainingEventBehaviors,
6694
6685
  event,
6695
6686
  editor,
6696
6687
  keyGenerator,
@@ -6698,11 +6689,11 @@ function performEvent({
6698
6689
  getSnapshot,
6699
6690
  nativeEvent
6700
6691
  }) {
6701
- debug$2(`(${isNativeBehaviorEvent(event) ? "" : `${mode}:`}${eventCategory(event)})`, JSON.stringify(event, null, 2));
6692
+ debug$2(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2));
6702
6693
  const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isAbstractBehaviorEvent(event) ? void 0 : {
6703
6694
  ...event,
6704
6695
  editor
6705
- }, eventBehaviors = (mode === "raise" ? [...behaviors, ...defaultBehaviors] : behaviors).filter((behavior) => {
6696
+ }, eventBehaviors = [...remainingEventBehaviors, ...defaultBehaviors].filter((behavior) => {
6706
6697
  if (behavior.on === "*")
6707
6698
  return !0;
6708
6699
  const [listenedNamespace] = behavior.on.includes("*") && behavior.on.includes(".") ? behavior.on.split(".") : [void 0], [eventNamespace] = event.type.includes(".") ? event.type.split(".") : [void 0];
@@ -6727,115 +6718,167 @@ function performEvent({
6727
6718
  return;
6728
6719
  }
6729
6720
  const guardSnapshot = getSnapshot();
6730
- let behaviorOverwritten = !1;
6721
+ let nativeEventPrevented = !1, defaultBehaviorOverwritten = !1, eventBehaviorIndex = -1;
6731
6722
  for (const eventBehavior of eventBehaviors) {
6723
+ eventBehaviorIndex++;
6732
6724
  const shouldRun = eventBehavior.guard === void 0 || eventBehavior.guard({
6733
6725
  snapshot: guardSnapshot,
6734
6726
  event
6735
6727
  });
6736
6728
  if (shouldRun) {
6729
+ defaultBehaviorOverwritten = !0;
6737
6730
  for (const actionSet of eventBehavior.actions) {
6738
6731
  const actionsSnapshot = getSnapshot(), actions = actionSet({
6739
6732
  snapshot: actionsSnapshot,
6740
6733
  event
6741
6734
  }, shouldRun);
6742
- actions.length !== 0 && (behaviorOverwritten = behaviorOverwritten || actions.some((action) => action.type !== "effect"), withApplyingBehaviorActionSet(editor, () => {
6735
+ if (actions.length !== 0) {
6736
+ if (actions.some((action) => action.type === "execute")) {
6737
+ nativeEventPrevented = !0, withUndoStep(editor, () => {
6738
+ for (const action of actions) {
6739
+ if (action.type === "effect") {
6740
+ performAction({
6741
+ context: {
6742
+ keyGenerator,
6743
+ schema
6744
+ },
6745
+ action: {
6746
+ ...action,
6747
+ editor
6748
+ }
6749
+ });
6750
+ continue;
6751
+ }
6752
+ if (action.type === "forward") {
6753
+ const remainingEventBehaviors2 = eventBehaviors.slice(eventBehaviorIndex + 1);
6754
+ performEvent({
6755
+ mode: "forward",
6756
+ behaviors,
6757
+ remainingEventBehaviors: remainingEventBehaviors2,
6758
+ event: action.event,
6759
+ editor,
6760
+ keyGenerator,
6761
+ schema,
6762
+ getSnapshot,
6763
+ nativeEvent
6764
+ });
6765
+ continue;
6766
+ }
6767
+ if (action.type === "raise") {
6768
+ performEvent({
6769
+ mode: "raise",
6770
+ behaviors,
6771
+ remainingEventBehaviors: behaviors,
6772
+ event: action.event,
6773
+ editor,
6774
+ keyGenerator,
6775
+ schema,
6776
+ getSnapshot,
6777
+ nativeEvent
6778
+ });
6779
+ continue;
6780
+ }
6781
+ if (isAbstractBehaviorEvent(action.event))
6782
+ performEvent({
6783
+ mode: "execute",
6784
+ behaviors,
6785
+ remainingEventBehaviors: behaviors,
6786
+ event: action.event,
6787
+ editor,
6788
+ keyGenerator,
6789
+ schema,
6790
+ getSnapshot,
6791
+ nativeEvent: void 0
6792
+ });
6793
+ else {
6794
+ const internalAction = {
6795
+ ...action.event,
6796
+ editor
6797
+ };
6798
+ let actionFailed = !1;
6799
+ if (withApplyingBehaviorActions(editor, () => {
6800
+ try {
6801
+ performAction({
6802
+ context: {
6803
+ keyGenerator,
6804
+ schema
6805
+ },
6806
+ action: internalAction
6807
+ });
6808
+ } catch (error) {
6809
+ console.error(new Error(`Performing action "${action.event.type}" as a result of "${event.type}" failed due to: ${error.message}`)), actionFailed = !0;
6810
+ }
6811
+ }), actionFailed)
6812
+ break;
6813
+ editor.onChange();
6814
+ }
6815
+ }
6816
+ });
6817
+ continue;
6818
+ }
6743
6819
  for (const action of actions) {
6744
- if (action.type === "raise") {
6820
+ if (action.type === "effect") {
6821
+ performAction({
6822
+ context: {
6823
+ keyGenerator,
6824
+ schema
6825
+ },
6826
+ action: {
6827
+ ...action,
6828
+ editor
6829
+ }
6830
+ });
6831
+ continue;
6832
+ }
6833
+ if (action.type === "forward") {
6834
+ const remainingEventBehaviors2 = eventBehaviors.slice(eventBehaviorIndex + 1);
6745
6835
  performEvent({
6746
- mode,
6747
- behaviors: mode === "execute" ? isCustomBehaviorEvent(action.event) ? [...behaviors, ...defaultBehaviors] : defaultBehaviors : [...behaviors, ...defaultBehaviors],
6836
+ mode: "forward",
6837
+ behaviors,
6838
+ remainingEventBehaviors: remainingEventBehaviors2,
6748
6839
  event: action.event,
6749
6840
  editor,
6750
6841
  keyGenerator,
6751
6842
  schema,
6752
6843
  getSnapshot,
6753
- nativeEvent: void 0
6844
+ nativeEvent
6754
6845
  });
6755
6846
  continue;
6756
6847
  }
6757
- if (action.type === "execute") {
6758
- if (isAbstractBehaviorEvent(action.event))
6759
- performEvent({
6760
- mode: "execute",
6761
- behaviors: defaultBehaviors,
6762
- event: action.event,
6763
- editor,
6764
- keyGenerator,
6765
- schema,
6766
- getSnapshot,
6767
- nativeEvent: void 0
6768
- });
6769
- else {
6770
- const internalAction2 = {
6771
- ...action.event,
6772
- editor
6773
- };
6774
- let actionFailed2 = !1;
6775
- if (withApplyingBehaviorActions(editor, () => {
6776
- try {
6777
- performAction({
6778
- context: {
6779
- keyGenerator,
6780
- schema
6781
- },
6782
- action: internalAction2
6783
- });
6784
- } catch (error) {
6785
- console.error(new Error(`Performing action "${action.event.type}" as a result of "${event.type}" failed due to: ${error.message}`)), actionFailed2 = !0;
6786
- }
6787
- }), actionFailed2)
6788
- break;
6789
- editor.onChange();
6790
- }
6848
+ if (action.type === "raise") {
6849
+ nativeEventPrevented = !0, performEvent({
6850
+ mode: "raise",
6851
+ behaviors,
6852
+ remainingEventBehaviors: behaviors,
6853
+ event: action.event,
6854
+ editor,
6855
+ keyGenerator,
6856
+ schema,
6857
+ getSnapshot,
6858
+ nativeEvent
6859
+ });
6791
6860
  continue;
6792
6861
  }
6793
- const internalAction = {
6794
- ...action,
6795
- editor
6796
- };
6797
- let actionFailed = !1;
6798
- if (withApplyingBehaviorActions(editor, () => {
6799
- try {
6800
- performAction({
6801
- context: {
6802
- keyGenerator,
6803
- schema
6804
- },
6805
- action: internalAction
6806
- });
6807
- } catch (error) {
6808
- console.error(new Error(`Performing action "${internalAction.type}" as a result of "${event.type}" failed due to: ${error.message}`)), actionFailed = !0;
6809
- }
6810
- }), actionFailed)
6811
- break;
6812
- editor.onChange();
6862
+ action.type === "execute" && console.error("Unexpected action type: `execute`");
6813
6863
  }
6814
- }));
6815
- }
6816
- if (behaviorOverwritten) {
6817
- nativeEvent?.preventDefault();
6818
- break;
6864
+ }
6819
6865
  }
6866
+ break;
6820
6867
  }
6821
6868
  }
6822
- if (!behaviorOverwritten) {
6823
- if (!defaultAction)
6824
- return;
6825
- withApplyingBehaviorActions(editor, () => {
6826
- try {
6827
- performAction({
6828
- context: {
6829
- keyGenerator,
6830
- schema
6831
- },
6832
- action: defaultAction
6833
- });
6834
- } catch (error) {
6835
- console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6836
- }
6837
- }), editor.onChange();
6838
- }
6869
+ !defaultBehaviorOverwritten && defaultAction ? (nativeEvent?.preventDefault(), withApplyingBehaviorActions(editor, () => {
6870
+ try {
6871
+ performAction({
6872
+ context: {
6873
+ keyGenerator,
6874
+ schema
6875
+ },
6876
+ action: defaultAction
6877
+ });
6878
+ } catch (error) {
6879
+ console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6880
+ }
6881
+ }), editor.onChange()) : nativeEventPrevented && nativeEvent?.preventDefault();
6839
6882
  }
6840
6883
  function createEditorSnapshot({
6841
6884
  converters,
@@ -6973,6 +7016,7 @@ const editorMachine = setup({
6973
7016
  assertEvent(event, ["behavior event"]), performEvent({
6974
7017
  mode: "raise",
6975
7018
  behaviors: [...context.behaviors.values()],
7019
+ remainingEventBehaviors: [...context.behaviors.values()],
6976
7020
  event: event.behaviorEvent,
6977
7021
  editor: event.editor,
6978
7022
  keyGenerator: context.keyGenerator,