@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
@@ -1309,19 +1309,19 @@ function getCurrentActionId(editor) {
1309
1309
  function isApplyingBehaviorActions(editor) {
1310
1310
  return getCurrentActionId(editor) !== void 0;
1311
1311
  }
1312
- const CURRENT_BEHAVIOR_ACTION_SET = /* @__PURE__ */ new WeakMap();
1313
- function withApplyingBehaviorActionSet(editor, fn) {
1314
- const current = CURRENT_BEHAVIOR_ACTION_SET.get(editor);
1312
+ const CURRENT_UNDO_STEP = /* @__PURE__ */ new WeakMap();
1313
+ function withUndoStep(editor, fn) {
1314
+ const current = CURRENT_UNDO_STEP.get(editor);
1315
1315
  if (current) {
1316
1316
  fn();
1317
1317
  return;
1318
1318
  }
1319
- CURRENT_BEHAVIOR_ACTION_SET.set(editor, current ?? {
1320
- actionSetId: defaultKeyGenerator()
1321
- }), fn(), CURRENT_BEHAVIOR_ACTION_SET.set(editor, void 0);
1319
+ CURRENT_UNDO_STEP.set(editor, current ?? {
1320
+ undoStepId: defaultKeyGenerator()
1321
+ }), fn(), CURRENT_UNDO_STEP.set(editor, void 0);
1322
1322
  }
1323
- function getCurrentBehaviorActionSetId(editor) {
1324
- return CURRENT_BEHAVIOR_ACTION_SET.get(editor)?.actionSetId;
1323
+ function getCurrentUndoStepId(editor) {
1324
+ return CURRENT_UNDO_STEP.get(editor)?.undoStepId;
1325
1325
  }
1326
1326
  const debug$f = debugWithName("plugin:withUndoRedo"), SAVING = /* @__PURE__ */ new WeakMap(), REMOTE_PATCHES = /* @__PURE__ */ new WeakMap(), UNDO_STEP_LIMIT = 1e3, isSaving = (editor) => {
1327
1327
  const state = SAVING.get(editor);
@@ -1334,7 +1334,7 @@ function createWithUndoRedo(options) {
1334
1334
  return (editor) => {
1335
1335
  let previousSnapshot = fromSlateValue(editor.children, editorActor.getSnapshot().context.schema.block.name);
1336
1336
  const remotePatches = getRemotePatches(editor);
1337
- let previousBehaviorActionSetId = getCurrentBehaviorActionSetId(editor);
1337
+ let previousUndoStepId = getCurrentUndoStepId(editor);
1338
1338
  options.subscriptions.push(() => {
1339
1339
  debug$f("Subscribing to patches");
1340
1340
  const sub = editorActor.on("patches", ({
@@ -1390,10 +1390,10 @@ function createWithUndoRedo(options) {
1390
1390
  history
1391
1391
  } = editor, {
1392
1392
  undos
1393
- } = 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);
1394
- let merge = currentBehaviorActionSetId !== void 0 && previousBehaviorActionSetId === void 0 ? !1 : currentBehaviorActionSetId !== void 0 && previousBehaviorActionSetId !== void 0 ? currentBehaviorActionSetId === previousBehaviorActionSetId : !0;
1393
+ } = 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);
1394
+ let merge = currentUndoStepId !== void 0 && previousUndoStepId === void 0 ? !1 : currentUndoStepId !== void 0 && previousUndoStepId !== void 0 ? currentUndoStepId === previousUndoStepId : !0;
1395
1395
  if (save) {
1396
- if (step ? operations.length === 0 && (merge = currentBehaviorActionSetId === void 0 && previousBehaviorActionSetId === void 0 ? shouldMerge(op, lastOp) || overwrite : merge) : merge = !1, step && merge)
1396
+ if (step ? operations.length === 0 && (merge = currentUndoStepId === void 0 && previousUndoStepId === void 0 ? shouldMerge(op, lastOp) || overwrite : merge) : merge = !1, step && merge)
1397
1397
  step.operations.push(op);
1398
1398
  else {
1399
1399
  const newStep = {
@@ -1406,7 +1406,7 @@ function createWithUndoRedo(options) {
1406
1406
  undos.shift();
1407
1407
  shouldClear(op) && (history.redos = []);
1408
1408
  }
1409
- previousBehaviorActionSetId = currentBehaviorActionSetId, apply2(op);
1409
+ previousUndoStepId = currentUndoStepId, apply2(op);
1410
1410
  }, editor;
1411
1411
  };
1412
1412
  }
@@ -4010,7 +4010,6 @@ const moveBackwardActionImplementation = ({
4010
4010
  unit: "character",
4011
4011
  distance: action.distance
4012
4012
  });
4013
- }, noopActionImplementation = () => {
4014
4013
  }, selectActionImplementation = ({
4015
4014
  action
4016
4015
  }) => {
@@ -4037,7 +4036,6 @@ const moveBackwardActionImplementation = ({
4037
4036
  "move.backward": moveBackwardActionImplementation,
4038
4037
  "move.block": moveBlockActionImplementation,
4039
4038
  "move.forward": moveForwardActionImplementation,
4040
- noop: noopActionImplementation,
4041
4039
  select: selectActionImplementation
4042
4040
  };
4043
4041
  function performAction({
@@ -4185,8 +4183,6 @@ function performAction({
4185
4183
  });
4186
4184
  break;
4187
4185
  }
4188
- case "noop":
4189
- break;
4190
4186
  default: {
4191
4187
  behaviorActionImplementations.select({
4192
4188
  context,
@@ -6366,9 +6362,7 @@ const EditorContext = createGloballyScopedContext("@portabletext/editor/context/
6366
6362
  const focusSpan = selector_isOverlappingSelection.getFocusSpan(snapshot), selectionCollapsed = selector_isOverlappingSelection.isSelectionCollapsed(snapshot);
6367
6363
  return focusSpan && selectionCollapsed;
6368
6364
  },
6369
- actions: [() => [{
6370
- type: "noop"
6371
- }]]
6365
+ actions: []
6372
6366
  }),
6373
6367
  behavior_core.defineBehavior({
6374
6368
  on: "clipboard.copy",
@@ -6387,9 +6381,7 @@ const EditorContext = createGloballyScopedContext("@portabletext/editor/context/
6387
6381
  const focusSpan = selector_isOverlappingSelection.getFocusSpan(snapshot), selectionCollapsed = selector_isOverlappingSelection.isSelectionCollapsed(snapshot);
6388
6382
  return focusSpan && selectionCollapsed;
6389
6383
  },
6390
- actions: [() => [{
6391
- type: "noop"
6392
- }]]
6384
+ actions: []
6393
6385
  }),
6394
6386
  behavior_core.defineBehavior({
6395
6387
  on: "clipboard.cut",
@@ -6456,9 +6448,7 @@ const EditorContext = createGloballyScopedContext("@portabletext/editor/context/
6456
6448
  }
6457
6449
  }) : !1;
6458
6450
  },
6459
- actions: [() => [{
6460
- type: "noop"
6461
- }]]
6451
+ actions: []
6462
6452
  }),
6463
6453
  behavior_core.defineBehavior({
6464
6454
  on: "drag.drop",
@@ -6664,6 +6654,7 @@ function eventCategory(event) {
6664
6654
  function performEvent({
6665
6655
  mode,
6666
6656
  behaviors,
6657
+ remainingEventBehaviors,
6667
6658
  event,
6668
6659
  editor,
6669
6660
  keyGenerator,
@@ -6671,11 +6662,11 @@ function performEvent({
6671
6662
  getSnapshot,
6672
6663
  nativeEvent
6673
6664
  }) {
6674
- debug$2(`(${isNativeBehaviorEvent(event) ? "" : `${mode}:`}${eventCategory(event)})`, JSON.stringify(event, null, 2));
6665
+ debug$2(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2));
6675
6666
  const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isAbstractBehaviorEvent(event) ? void 0 : {
6676
6667
  ...event,
6677
6668
  editor
6678
- }, eventBehaviors = (mode === "raise" ? [...behaviors, ...defaultBehaviors] : behaviors).filter((behavior) => {
6669
+ }, eventBehaviors = [...remainingEventBehaviors, ...defaultBehaviors].filter((behavior) => {
6679
6670
  if (behavior.on === "*")
6680
6671
  return !0;
6681
6672
  const [listenedNamespace] = behavior.on.includes("*") && behavior.on.includes(".") ? behavior.on.split(".") : [void 0], [eventNamespace] = event.type.includes(".") ? event.type.split(".") : [void 0];
@@ -6700,115 +6691,167 @@ function performEvent({
6700
6691
  return;
6701
6692
  }
6702
6693
  const guardSnapshot = getSnapshot();
6703
- let behaviorOverwritten = !1;
6694
+ let nativeEventPrevented = !1, defaultBehaviorOverwritten = !1, eventBehaviorIndex = -1;
6704
6695
  for (const eventBehavior of eventBehaviors) {
6696
+ eventBehaviorIndex++;
6705
6697
  const shouldRun = eventBehavior.guard === void 0 || eventBehavior.guard({
6706
6698
  snapshot: guardSnapshot,
6707
6699
  event
6708
6700
  });
6709
6701
  if (shouldRun) {
6702
+ defaultBehaviorOverwritten = !0;
6710
6703
  for (const actionSet of eventBehavior.actions) {
6711
6704
  const actionsSnapshot = getSnapshot(), actions = actionSet({
6712
6705
  snapshot: actionsSnapshot,
6713
6706
  event
6714
6707
  }, shouldRun);
6715
- actions.length !== 0 && (behaviorOverwritten = behaviorOverwritten || actions.some((action) => action.type !== "effect"), withApplyingBehaviorActionSet(editor, () => {
6708
+ if (actions.length !== 0) {
6709
+ if (actions.some((action) => action.type === "execute")) {
6710
+ nativeEventPrevented = !0, withUndoStep(editor, () => {
6711
+ for (const action of actions) {
6712
+ if (action.type === "effect") {
6713
+ performAction({
6714
+ context: {
6715
+ keyGenerator,
6716
+ schema: schema2
6717
+ },
6718
+ action: {
6719
+ ...action,
6720
+ editor
6721
+ }
6722
+ });
6723
+ continue;
6724
+ }
6725
+ if (action.type === "forward") {
6726
+ const remainingEventBehaviors2 = eventBehaviors.slice(eventBehaviorIndex + 1);
6727
+ performEvent({
6728
+ mode: "forward",
6729
+ behaviors,
6730
+ remainingEventBehaviors: remainingEventBehaviors2,
6731
+ event: action.event,
6732
+ editor,
6733
+ keyGenerator,
6734
+ schema: schema2,
6735
+ getSnapshot,
6736
+ nativeEvent
6737
+ });
6738
+ continue;
6739
+ }
6740
+ if (action.type === "raise") {
6741
+ performEvent({
6742
+ mode: "raise",
6743
+ behaviors,
6744
+ remainingEventBehaviors: behaviors,
6745
+ event: action.event,
6746
+ editor,
6747
+ keyGenerator,
6748
+ schema: schema2,
6749
+ getSnapshot,
6750
+ nativeEvent
6751
+ });
6752
+ continue;
6753
+ }
6754
+ if (isAbstractBehaviorEvent(action.event))
6755
+ performEvent({
6756
+ mode: "execute",
6757
+ behaviors,
6758
+ remainingEventBehaviors: behaviors,
6759
+ event: action.event,
6760
+ editor,
6761
+ keyGenerator,
6762
+ schema: schema2,
6763
+ getSnapshot,
6764
+ nativeEvent: void 0
6765
+ });
6766
+ else {
6767
+ const internalAction = {
6768
+ ...action.event,
6769
+ editor
6770
+ };
6771
+ let actionFailed = !1;
6772
+ if (withApplyingBehaviorActions(editor, () => {
6773
+ try {
6774
+ performAction({
6775
+ context: {
6776
+ keyGenerator,
6777
+ schema: schema2
6778
+ },
6779
+ action: internalAction
6780
+ });
6781
+ } catch (error) {
6782
+ console.error(new Error(`Performing action "${action.event.type}" as a result of "${event.type}" failed due to: ${error.message}`)), actionFailed = !0;
6783
+ }
6784
+ }), actionFailed)
6785
+ break;
6786
+ editor.onChange();
6787
+ }
6788
+ }
6789
+ });
6790
+ continue;
6791
+ }
6716
6792
  for (const action of actions) {
6717
- if (action.type === "raise") {
6793
+ if (action.type === "effect") {
6794
+ performAction({
6795
+ context: {
6796
+ keyGenerator,
6797
+ schema: schema2
6798
+ },
6799
+ action: {
6800
+ ...action,
6801
+ editor
6802
+ }
6803
+ });
6804
+ continue;
6805
+ }
6806
+ if (action.type === "forward") {
6807
+ const remainingEventBehaviors2 = eventBehaviors.slice(eventBehaviorIndex + 1);
6718
6808
  performEvent({
6719
- mode,
6720
- behaviors: mode === "execute" ? isCustomBehaviorEvent(action.event) ? [...behaviors, ...defaultBehaviors] : defaultBehaviors : [...behaviors, ...defaultBehaviors],
6809
+ mode: "forward",
6810
+ behaviors,
6811
+ remainingEventBehaviors: remainingEventBehaviors2,
6721
6812
  event: action.event,
6722
6813
  editor,
6723
6814
  keyGenerator,
6724
6815
  schema: schema2,
6725
6816
  getSnapshot,
6726
- nativeEvent: void 0
6817
+ nativeEvent
6727
6818
  });
6728
6819
  continue;
6729
6820
  }
6730
- if (action.type === "execute") {
6731
- if (isAbstractBehaviorEvent(action.event))
6732
- performEvent({
6733
- mode: "execute",
6734
- behaviors: defaultBehaviors,
6735
- event: action.event,
6736
- editor,
6737
- keyGenerator,
6738
- schema: schema2,
6739
- getSnapshot,
6740
- nativeEvent: void 0
6741
- });
6742
- else {
6743
- const internalAction2 = {
6744
- ...action.event,
6745
- editor
6746
- };
6747
- let actionFailed2 = !1;
6748
- if (withApplyingBehaviorActions(editor, () => {
6749
- try {
6750
- performAction({
6751
- context: {
6752
- keyGenerator,
6753
- schema: schema2
6754
- },
6755
- action: internalAction2
6756
- });
6757
- } catch (error) {
6758
- console.error(new Error(`Performing action "${action.event.type}" as a result of "${event.type}" failed due to: ${error.message}`)), actionFailed2 = !0;
6759
- }
6760
- }), actionFailed2)
6761
- break;
6762
- editor.onChange();
6763
- }
6821
+ if (action.type === "raise") {
6822
+ nativeEventPrevented = !0, performEvent({
6823
+ mode: "raise",
6824
+ behaviors,
6825
+ remainingEventBehaviors: behaviors,
6826
+ event: action.event,
6827
+ editor,
6828
+ keyGenerator,
6829
+ schema: schema2,
6830
+ getSnapshot,
6831
+ nativeEvent
6832
+ });
6764
6833
  continue;
6765
6834
  }
6766
- const internalAction = {
6767
- ...action,
6768
- editor
6769
- };
6770
- let actionFailed = !1;
6771
- if (withApplyingBehaviorActions(editor, () => {
6772
- try {
6773
- performAction({
6774
- context: {
6775
- keyGenerator,
6776
- schema: schema2
6777
- },
6778
- action: internalAction
6779
- });
6780
- } catch (error) {
6781
- console.error(new Error(`Performing action "${internalAction.type}" as a result of "${event.type}" failed due to: ${error.message}`)), actionFailed = !0;
6782
- }
6783
- }), actionFailed)
6784
- break;
6785
- editor.onChange();
6835
+ action.type === "execute" && console.error("Unexpected action type: `execute`");
6786
6836
  }
6787
- }));
6788
- }
6789
- if (behaviorOverwritten) {
6790
- nativeEvent?.preventDefault();
6791
- break;
6837
+ }
6792
6838
  }
6839
+ break;
6793
6840
  }
6794
6841
  }
6795
- if (!behaviorOverwritten) {
6796
- if (!defaultAction)
6797
- return;
6798
- withApplyingBehaviorActions(editor, () => {
6799
- try {
6800
- performAction({
6801
- context: {
6802
- keyGenerator,
6803
- schema: schema2
6804
- },
6805
- action: defaultAction
6806
- });
6807
- } catch (error) {
6808
- console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6809
- }
6810
- }), editor.onChange();
6811
- }
6842
+ !defaultBehaviorOverwritten && defaultAction ? (nativeEvent?.preventDefault(), withApplyingBehaviorActions(editor, () => {
6843
+ try {
6844
+ performAction({
6845
+ context: {
6846
+ keyGenerator,
6847
+ schema: schema2
6848
+ },
6849
+ action: defaultAction
6850
+ });
6851
+ } catch (error) {
6852
+ console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6853
+ }
6854
+ }), editor.onChange()) : nativeEventPrevented && nativeEvent?.preventDefault();
6812
6855
  }
6813
6856
  function createEditorSnapshot({
6814
6857
  converters,
@@ -6946,6 +6989,7 @@ const editorMachine = xstate.setup({
6946
6989
  xstate.assertEvent(event, ["behavior event"]), performEvent({
6947
6990
  mode: "raise",
6948
6991
  behaviors: [...context.behaviors.values()],
6992
+ remainingEventBehaviors: [...context.behaviors.values()],
6949
6993
  event: event.behaviorEvent,
6950
6994
  editor: event.editor,
6951
6995
  keyGenerator: context.keyGenerator,