@portabletext/editor 1.47.14 → 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 (49) 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 +171 -115
  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 +172 -116
  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 +6 -4
  18. package/lib/index.d.ts +6 -4
  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 +3 -3
  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-context.tsx +7 -0
  41. package/src/editor/editor-machine.ts +1 -0
  42. package/src/editor/editor-provider.tsx +1 -3
  43. package/src/editor/plugins/createWithUndoRedo.ts +8 -11
  44. package/src/editor/with-applying-behavior-actions.ts +9 -11
  45. package/src/internal-utils/global-scope.ts +19 -0
  46. package/src/internal-utils/globally-scoped-context.ts +39 -0
  47. package/src/plugins/plugin.decorator-shortcut.ts +3 -2
  48. package/src/plugins/plugin.one-line.tsx +3 -3
  49. 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,
@@ -5771,7 +5767,20 @@ function createInternalEditor(editorActor) {
5771
5767
  }
5772
5768
  };
5773
5769
  }
5774
- const EditorActorContext = React.createContext({}), abstractAnnotationBehaviors = [behavior_core.defineBehavior({
5770
+ const EditorActorContext = React.createContext({});
5771
+ function getGlobalScope() {
5772
+ if (typeof globalThis < "u") return globalThis;
5773
+ if (typeof window < "u") return window;
5774
+ if (typeof self < "u") return self;
5775
+ if (typeof global < "u") return global;
5776
+ throw new Error("@portabletext/editor: could not locate global scope");
5777
+ }
5778
+ const globalScope = getGlobalScope();
5779
+ function createGloballyScopedContext(key, defaultValue) {
5780
+ const symbol = Symbol.for(key);
5781
+ return typeof document > "u" ? React.createContext(defaultValue) : (globalScope[symbol] = globalScope[symbol] ?? React.createContext(defaultValue), globalScope[symbol]);
5782
+ }
5783
+ const EditorContext = createGloballyScopedContext("@portabletext/editor/context/editor", null), abstractAnnotationBehaviors = [behavior_core.defineBehavior({
5775
5784
  on: "annotation.toggle",
5776
5785
  guard: ({
5777
5786
  snapshot,
@@ -6353,9 +6362,7 @@ const EditorActorContext = React.createContext({}), abstractAnnotationBehaviors
6353
6362
  const focusSpan = selector_isOverlappingSelection.getFocusSpan(snapshot), selectionCollapsed = selector_isOverlappingSelection.isSelectionCollapsed(snapshot);
6354
6363
  return focusSpan && selectionCollapsed;
6355
6364
  },
6356
- actions: [() => [{
6357
- type: "noop"
6358
- }]]
6365
+ actions: []
6359
6366
  }),
6360
6367
  behavior_core.defineBehavior({
6361
6368
  on: "clipboard.copy",
@@ -6374,9 +6381,7 @@ const EditorActorContext = React.createContext({}), abstractAnnotationBehaviors
6374
6381
  const focusSpan = selector_isOverlappingSelection.getFocusSpan(snapshot), selectionCollapsed = selector_isOverlappingSelection.isSelectionCollapsed(snapshot);
6375
6382
  return focusSpan && selectionCollapsed;
6376
6383
  },
6377
- actions: [() => [{
6378
- type: "noop"
6379
- }]]
6384
+ actions: []
6380
6385
  }),
6381
6386
  behavior_core.defineBehavior({
6382
6387
  on: "clipboard.cut",
@@ -6443,9 +6448,7 @@ const EditorActorContext = React.createContext({}), abstractAnnotationBehaviors
6443
6448
  }
6444
6449
  }) : !1;
6445
6450
  },
6446
- actions: [() => [{
6447
- type: "noop"
6448
- }]]
6451
+ actions: []
6449
6452
  }),
6450
6453
  behavior_core.defineBehavior({
6451
6454
  on: "drag.drop",
@@ -6651,6 +6654,7 @@ function eventCategory(event) {
6651
6654
  function performEvent({
6652
6655
  mode,
6653
6656
  behaviors,
6657
+ remainingEventBehaviors,
6654
6658
  event,
6655
6659
  editor,
6656
6660
  keyGenerator,
@@ -6658,11 +6662,11 @@ function performEvent({
6658
6662
  getSnapshot,
6659
6663
  nativeEvent
6660
6664
  }) {
6661
- debug$2(`(${isNativeBehaviorEvent(event) ? "" : `${mode}:`}${eventCategory(event)})`, JSON.stringify(event, null, 2));
6665
+ debug$2(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2));
6662
6666
  const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isAbstractBehaviorEvent(event) ? void 0 : {
6663
6667
  ...event,
6664
6668
  editor
6665
- }, eventBehaviors = (mode === "raise" ? [...behaviors, ...defaultBehaviors] : behaviors).filter((behavior) => {
6669
+ }, eventBehaviors = [...remainingEventBehaviors, ...defaultBehaviors].filter((behavior) => {
6666
6670
  if (behavior.on === "*")
6667
6671
  return !0;
6668
6672
  const [listenedNamespace] = behavior.on.includes("*") && behavior.on.includes(".") ? behavior.on.split(".") : [void 0], [eventNamespace] = event.type.includes(".") ? event.type.split(".") : [void 0];
@@ -6687,115 +6691,167 @@ function performEvent({
6687
6691
  return;
6688
6692
  }
6689
6693
  const guardSnapshot = getSnapshot();
6690
- let behaviorOverwritten = !1;
6694
+ let nativeEventPrevented = !1, defaultBehaviorOverwritten = !1, eventBehaviorIndex = -1;
6691
6695
  for (const eventBehavior of eventBehaviors) {
6696
+ eventBehaviorIndex++;
6692
6697
  const shouldRun = eventBehavior.guard === void 0 || eventBehavior.guard({
6693
6698
  snapshot: guardSnapshot,
6694
6699
  event
6695
6700
  });
6696
6701
  if (shouldRun) {
6702
+ defaultBehaviorOverwritten = !0;
6697
6703
  for (const actionSet of eventBehavior.actions) {
6698
6704
  const actionsSnapshot = getSnapshot(), actions = actionSet({
6699
6705
  snapshot: actionsSnapshot,
6700
6706
  event
6701
6707
  }, shouldRun);
6702
- 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
+ }
6703
6792
  for (const action of actions) {
6704
- 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);
6705
6808
  performEvent({
6706
- mode,
6707
- behaviors: mode === "execute" ? isCustomBehaviorEvent(action.event) ? [...behaviors, ...defaultBehaviors] : defaultBehaviors : [...behaviors, ...defaultBehaviors],
6809
+ mode: "forward",
6810
+ behaviors,
6811
+ remainingEventBehaviors: remainingEventBehaviors2,
6708
6812
  event: action.event,
6709
6813
  editor,
6710
6814
  keyGenerator,
6711
6815
  schema: schema2,
6712
6816
  getSnapshot,
6713
- nativeEvent: void 0
6817
+ nativeEvent
6714
6818
  });
6715
6819
  continue;
6716
6820
  }
6717
- if (action.type === "execute") {
6718
- if (isAbstractBehaviorEvent(action.event))
6719
- performEvent({
6720
- mode: "execute",
6721
- behaviors: defaultBehaviors,
6722
- event: action.event,
6723
- editor,
6724
- keyGenerator,
6725
- schema: schema2,
6726
- getSnapshot,
6727
- nativeEvent: void 0
6728
- });
6729
- else {
6730
- const internalAction2 = {
6731
- ...action.event,
6732
- editor
6733
- };
6734
- let actionFailed2 = !1;
6735
- if (withApplyingBehaviorActions(editor, () => {
6736
- try {
6737
- performAction({
6738
- context: {
6739
- keyGenerator,
6740
- schema: schema2
6741
- },
6742
- action: internalAction2
6743
- });
6744
- } catch (error) {
6745
- console.error(new Error(`Performing action "${action.event.type}" as a result of "${event.type}" failed due to: ${error.message}`)), actionFailed2 = !0;
6746
- }
6747
- }), actionFailed2)
6748
- break;
6749
- editor.onChange();
6750
- }
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
+ });
6751
6833
  continue;
6752
6834
  }
6753
- const internalAction = {
6754
- ...action,
6755
- editor
6756
- };
6757
- let actionFailed = !1;
6758
- if (withApplyingBehaviorActions(editor, () => {
6759
- try {
6760
- performAction({
6761
- context: {
6762
- keyGenerator,
6763
- schema: schema2
6764
- },
6765
- action: internalAction
6766
- });
6767
- } catch (error) {
6768
- console.error(new Error(`Performing action "${internalAction.type}" as a result of "${event.type}" failed due to: ${error.message}`)), actionFailed = !0;
6769
- }
6770
- }), actionFailed)
6771
- break;
6772
- editor.onChange();
6835
+ action.type === "execute" && console.error("Unexpected action type: `execute`");
6773
6836
  }
6774
- }));
6775
- }
6776
- if (behaviorOverwritten) {
6777
- nativeEvent?.preventDefault();
6778
- break;
6837
+ }
6779
6838
  }
6839
+ break;
6780
6840
  }
6781
6841
  }
6782
- if (!behaviorOverwritten) {
6783
- if (!defaultAction)
6784
- return;
6785
- withApplyingBehaviorActions(editor, () => {
6786
- try {
6787
- performAction({
6788
- context: {
6789
- keyGenerator,
6790
- schema: schema2
6791
- },
6792
- action: defaultAction
6793
- });
6794
- } catch (error) {
6795
- console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6796
- }
6797
- }), editor.onChange();
6798
- }
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();
6799
6855
  }
6800
6856
  function createEditorSnapshot({
6801
6857
  converters,
@@ -6928,11 +6984,12 @@ const editorMachine = xstate.setup({
6928
6984
  "handle behavior event": ({
6929
6985
  context,
6930
6986
  event,
6931
- self
6987
+ self: self2
6932
6988
  }) => {
6933
6989
  xstate.assertEvent(event, ["behavior event"]), performEvent({
6934
6990
  mode: "raise",
6935
6991
  behaviors: [...context.behaviors.values()],
6992
+ remainingEventBehaviors: [...context.behaviors.values()],
6936
6993
  event: event.behaviorEvent,
6937
6994
  editor: event.editor,
6938
6995
  keyGenerator: context.keyGenerator,
@@ -6941,11 +6998,11 @@ const editorMachine = xstate.setup({
6941
6998
  converters: [...context.converters],
6942
6999
  editor: event.editor,
6943
7000
  keyGenerator: context.keyGenerator,
6944
- readOnly: self.getSnapshot().matches({
7001
+ readOnly: self2.getSnapshot().matches({
6945
7002
  "edit mode": "read only"
6946
7003
  }),
6947
7004
  schema: context.schema,
6948
- hasTag: (tag) => self.getSnapshot().hasTag(tag),
7005
+ hasTag: (tag) => self2.getSnapshot().hasTag(tag),
6949
7006
  internalDrag: context.internalDrag
6950
7007
  }),
6951
7008
  nativeEvent: event.nativeEvent
@@ -7822,7 +7879,6 @@ function RouteEventsToChanges(props) {
7822
7879
  let t2;
7823
7880
  return $[5] !== props.editorActor ? (t2 = [props.editorActor], $[5] = props.editorActor, $[6] = t2) : t2 = $[6], React.useEffect(t1, t2), null;
7824
7881
  }
7825
- const EditorContext = React__default.default.createContext(void 0);
7826
7882
  function EditorProvider(props) {
7827
7883
  const editorActor = react.useActorRef(editorMachine, {
7828
7884
  input: editorConfigToMachineInput(props.initialConfig)