@portabletext/editor 1.47.3 → 1.47.4

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 (39) hide show
  1. package/lib/_chunks-cjs/editor-provider.cjs +125 -100
  2. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  3. package/lib/_chunks-cjs/util.is-selection-collapsed.cjs +0 -5
  4. package/lib/_chunks-cjs/util.is-selection-collapsed.cjs.map +1 -1
  5. package/lib/_chunks-es/editor-provider.js +126 -101
  6. package/lib/_chunks-es/editor-provider.js.map +1 -1
  7. package/lib/_chunks-es/util.is-selection-collapsed.js +0 -5
  8. package/lib/_chunks-es/util.is-selection-collapsed.js.map +1 -1
  9. package/lib/behaviors/index.d.cts +404 -543
  10. package/lib/behaviors/index.d.ts +404 -543
  11. package/lib/index.cjs +3 -8
  12. package/lib/index.cjs.map +1 -1
  13. package/lib/index.d.cts +411 -34
  14. package/lib/index.d.ts +411 -34
  15. package/lib/index.js +4 -9
  16. package/lib/index.js.map +1 -1
  17. package/lib/plugins/index.d.cts +411 -34
  18. package/lib/plugins/index.d.ts +411 -34
  19. package/lib/selectors/index.d.cts +404 -33
  20. package/lib/selectors/index.d.ts +404 -33
  21. package/lib/utils/index.cjs +4 -1
  22. package/lib/utils/index.cjs.map +1 -1
  23. package/lib/utils/index.d.cts +404 -33
  24. package/lib/utils/index.d.ts +404 -33
  25. package/lib/utils/index.js +6 -3
  26. package/lib/utils/index.js.map +1 -1
  27. package/package.json +1 -1
  28. package/src/behavior-actions/behavior.action.insert.text.ts +10 -1
  29. package/src/behavior-actions/behavior.actions.ts +0 -18
  30. package/src/behaviors/behavior.perform-event.ts +55 -65
  31. package/src/behaviors/behavior.types.event.ts +7 -9
  32. package/src/editor/Editable.tsx +4 -17
  33. package/src/editor/create-editor.ts +14 -0
  34. package/src/editor/editor-machine.ts +76 -5
  35. package/src/editor/plugins/create-with-event-listeners.ts +0 -6
  36. package/src/editor/plugins/createWithEditableAPI.ts +2 -8
  37. package/src/editor/with-applying-behavior-actions.ts +2 -2
  38. package/src/behavior-actions/behavior.action.blur.ts +0 -8
  39. package/src/behavior-actions/behavior.action.focus.ts +0 -8
@@ -1313,12 +1313,12 @@ const CURRENT_BEHAVIOR_ACTION_SET = /* @__PURE__ */ new WeakMap();
1313
1313
  function withApplyingBehaviorActionSet(editor, fn) {
1314
1314
  const current = CURRENT_BEHAVIOR_ACTION_SET.get(editor);
1315
1315
  if (current) {
1316
- withApplyingBehaviorActions(editor, fn);
1316
+ fn();
1317
1317
  return;
1318
1318
  }
1319
1319
  CURRENT_BEHAVIOR_ACTION_SET.set(editor, current ?? {
1320
1320
  actionSetId: defaultKeyGenerator()
1321
- }), withApplyingBehaviorActions(editor, fn), CURRENT_BEHAVIOR_ACTION_SET.set(editor, void 0);
1321
+ }), fn(), CURRENT_BEHAVIOR_ACTION_SET.set(editor, void 0);
1322
1322
  }
1323
1323
  function getCurrentBehaviorActionSetId(editor) {
1324
1324
  return CURRENT_BEHAVIOR_ACTION_SET.get(editor)?.actionSetId;
@@ -2629,7 +2629,10 @@ function createOperationToPatches(editorActor) {
2629
2629
  const insertTextActionImplementation = ({
2630
2630
  action
2631
2631
  }) => {
2632
- slate.Transforms.insertText(action.editor, action.text);
2632
+ action.editor.marks ? slate.Transforms.insertNodes(action.editor, {
2633
+ text: action.text,
2634
+ ...action.editor.marks
2635
+ }) : slate.Transforms.insertText(action.editor, action.text), action.editor.marks = null;
2633
2636
  };
2634
2637
  function isPortableTextSpan(node) {
2635
2638
  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"));
@@ -3388,10 +3391,6 @@ const blockSetBehaviorActionImplementation = ({
3388
3391
  }, {
3389
3392
  at: location
3390
3393
  });
3391
- }, blurActionImplementation = ({
3392
- action
3393
- }) => {
3394
- slateReact.ReactEditor.blur(action.editor);
3395
3394
  };
3396
3395
  function getFocusBlock({
3397
3396
  editor
@@ -3722,10 +3721,6 @@ const decoratorAddActionImplementation = ({
3722
3721
  action
3723
3722
  }) => {
3724
3723
  action.effect();
3725
- }, focusActionImplementation = ({
3726
- action
3727
- }) => {
3728
- slateReact.ReactEditor.focus(action.editor);
3729
3724
  }, insertInlineObjectActionImplementation = ({
3730
3725
  context,
3731
3726
  action
@@ -4038,10 +4033,8 @@ const moveBackwardActionImplementation = ({
4038
4033
  "annotation.remove": removeAnnotationActionImplementation,
4039
4034
  "block.set": blockSetBehaviorActionImplementation,
4040
4035
  "block.unset": blockUnsetBehaviorActionImplementation,
4041
- blur: blurActionImplementation,
4042
4036
  "decorator.add": decoratorAddActionImplementation,
4043
4037
  "decorator.remove": removeDecoratorActionImplementation,
4044
- focus: focusActionImplementation,
4045
4038
  delete: deleteActionImplementation,
4046
4039
  "delete.backward": deleteBackwardActionImplementation,
4047
4040
  "delete.forward": deleteForwardActionImplementation,
@@ -4093,13 +4086,6 @@ function performAction({
4093
4086
  });
4094
4087
  break;
4095
4088
  }
4096
- case "blur": {
4097
- behaviorActionImplementations.blur({
4098
- context,
4099
- action
4100
- });
4101
- break;
4102
- }
4103
4089
  case "decorator.add": {
4104
4090
  behaviorActionImplementations["decorator.add"]({
4105
4091
  context,
@@ -4149,13 +4135,6 @@ function performAction({
4149
4135
  });
4150
4136
  break;
4151
4137
  }
4152
- case "focus": {
4153
- behaviorActionImplementations.focus({
4154
- context,
4155
- action
4156
- });
4157
- break;
4158
- }
4159
4138
  case "history.redo": {
4160
4139
  behaviorActionImplementations["history.redo"]({
4161
4140
  context,
@@ -4332,10 +4311,7 @@ function createWithEventListeners(editorActor) {
4332
4311
  type: "insert.text",
4333
4312
  text
4334
4313
  },
4335
- editor,
4336
- defaultActionCallback: () => {
4337
- insertText(text, options);
4338
- }
4314
+ editor
4339
4315
  });
4340
4316
  }, editor.redo = () => {
4341
4317
  if (isApplyingBehaviorActions(editor)) {
@@ -4374,10 +4350,7 @@ function createWithEventListeners(editorActor) {
4374
4350
  range
4375
4351
  })
4376
4352
  },
4377
- editor,
4378
- defaultActionCallback: () => {
4379
- select(location);
4380
- }
4353
+ editor
4381
4354
  });
4382
4355
  }, editor.setFragmentData = () => {
4383
4356
  console.warn("Unexpected call to .setFragmentData(...)");
@@ -5860,8 +5833,7 @@ function performEvent({
5860
5833
  keyGenerator,
5861
5834
  schema: schema2,
5862
5835
  getSnapshot,
5863
- nativeEvent,
5864
- defaultActionCallback
5836
+ nativeEvent
5865
5837
  }) {
5866
5838
  debug$3(`(${eventCategory(event)})`, JSON.stringify(event, null, 2));
5867
5839
  const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isAbstractBehaviorEvent(event) ? void 0 : {
@@ -5874,16 +5846,6 @@ function performEvent({
5874
5846
  return listenedNamespace !== void 0 && eventNamespace !== void 0 && listenedNamespace === eventNamespace || listenedNamespace !== void 0 && eventNamespace === void 0 && listenedNamespace === event.type ? !0 : behavior.on === event.type;
5875
5847
  });
5876
5848
  if (eventBehaviors.length === 0) {
5877
- if (defaultActionCallback) {
5878
- withApplyingBehaviorActions(editor, () => {
5879
- try {
5880
- defaultActionCallback();
5881
- } catch (error) {
5882
- console.error(new Error(`Performing action "${event.type}" failed due to: ${error.message}`));
5883
- }
5884
- });
5885
- return;
5886
- }
5887
5849
  if (!defaultAction)
5888
5850
  return;
5889
5851
  withApplyingBehaviorActions(editor, () => {
@@ -5894,11 +5856,11 @@ function performEvent({
5894
5856
  schema: schema2
5895
5857
  },
5896
5858
  action: defaultAction
5897
- }), editor.onChange();
5859
+ });
5898
5860
  } catch (error) {
5899
5861
  console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
5900
5862
  }
5901
- });
5863
+ }), editor.onChange();
5902
5864
  return;
5903
5865
  }
5904
5866
  const guardSnapshot = getSnapshot();
@@ -5925,7 +5887,6 @@ function performEvent({
5925
5887
  keyGenerator,
5926
5888
  schema: schema2,
5927
5889
  getSnapshot,
5928
- defaultActionCallback: void 0,
5929
5890
  nativeEvent: void 0
5930
5891
  });
5931
5892
  continue;
@@ -5940,45 +5901,53 @@ function performEvent({
5940
5901
  keyGenerator,
5941
5902
  schema: schema2,
5942
5903
  getSnapshot,
5943
- defaultActionCallback: void 0,
5944
5904
  nativeEvent: void 0
5945
5905
  });
5946
- else
5947
- try {
5948
- performAction({
5949
- context: {
5950
- keyGenerator,
5951
- schema: schema2
5952
- },
5953
- action: {
5954
- ...action.event,
5955
- editor
5956
- }
5957
- });
5958
- } catch (error) {
5959
- console.error(new Error(`Performing action "${action.event.type}" as a result of "${event.type}" failed due to: ${error.message}`));
5906
+ else {
5907
+ const internalAction2 = {
5908
+ ...action.event,
5909
+ editor
5910
+ };
5911
+ let actionFailed2 = !1;
5912
+ if (withApplyingBehaviorActions(editor, () => {
5913
+ try {
5914
+ performAction({
5915
+ context: {
5916
+ keyGenerator,
5917
+ schema: schema2
5918
+ },
5919
+ action: internalAction2
5920
+ });
5921
+ } catch (error) {
5922
+ console.error(new Error(`Performing action "${action.event.type}" as a result of "${event.type}" failed due to: ${error.message}`)), actionFailed2 = !0;
5923
+ }
5924
+ }), actionFailed2)
5960
5925
  break;
5961
- }
5926
+ editor.onChange();
5927
+ }
5962
5928
  continue;
5963
5929
  }
5964
5930
  const internalAction = {
5965
5931
  ...action,
5966
5932
  editor
5967
5933
  };
5968
- try {
5969
- performAction({
5970
- context: {
5971
- keyGenerator,
5972
- schema: schema2
5973
- },
5974
- action: internalAction
5975
- });
5976
- } catch (error) {
5977
- console.error(new Error(`Performing action "${internalAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
5934
+ let actionFailed = !1;
5935
+ if (withApplyingBehaviorActions(editor, () => {
5936
+ try {
5937
+ performAction({
5938
+ context: {
5939
+ keyGenerator,
5940
+ schema: schema2
5941
+ },
5942
+ action: internalAction
5943
+ });
5944
+ } catch (error) {
5945
+ console.error(new Error(`Performing action "${internalAction.type}" as a result of "${event.type}" failed due to: ${error.message}`)), actionFailed = !0;
5946
+ }
5947
+ }), actionFailed)
5978
5948
  break;
5979
- }
5949
+ editor.onChange();
5980
5950
  }
5981
- editor.onChange();
5982
5951
  }));
5983
5952
  }
5984
5953
  if (behaviorOverwritten) {
@@ -5988,16 +5957,6 @@ function performEvent({
5988
5957
  }
5989
5958
  }
5990
5959
  if (!behaviorOverwritten) {
5991
- if (defaultActionCallback) {
5992
- withApplyingBehaviorActions(editor, () => {
5993
- try {
5994
- defaultActionCallback();
5995
- } catch (error) {
5996
- console.error(new Error(`Performing "${event.type}" failed due to: ${error.message}`));
5997
- }
5998
- });
5999
- return;
6000
- }
6001
5960
  if (!defaultAction)
6002
5961
  return;
6003
5962
  withApplyingBehaviorActions(editor, () => {
@@ -6008,11 +5967,11 @@ function performEvent({
6008
5967
  schema: schema2
6009
5968
  },
6010
5969
  action: defaultAction
6011
- }), editor.onChange();
5970
+ });
6012
5971
  } catch (error) {
6013
5972
  console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6014
5973
  }
6015
- });
5974
+ }), editor.onChange();
6016
5975
  }
6017
5976
  }
6018
5977
  function slateChildrenToBlocks(schema2, value) {
@@ -6156,6 +6115,30 @@ const editorMachine = xstate.setup({
6156
6115
  "clear pending events": xstate.assign({
6157
6116
  pendingEvents: []
6158
6117
  }),
6118
+ "handle blur": ({
6119
+ event
6120
+ }) => {
6121
+ xstate.assertEvent(event, "blur");
6122
+ try {
6123
+ slateReact.ReactEditor.blur(event.editor);
6124
+ } catch (error) {
6125
+ console.error(new Error(`Failed to blur editor: ${error.message}`));
6126
+ }
6127
+ },
6128
+ "handle focus": ({
6129
+ context
6130
+ }) => {
6131
+ if (!context.slateEditor) {
6132
+ console.error("No Slate editor found to focus");
6133
+ return;
6134
+ }
6135
+ try {
6136
+ const currentSelection = context.slateEditor.selection;
6137
+ slateReact.ReactEditor.focus(context.slateEditor), currentSelection && slate.Transforms.select(context.slateEditor, currentSelection);
6138
+ } catch (error) {
6139
+ console.error(new Error(`Failed to focus editor: ${error.message}`));
6140
+ }
6141
+ },
6159
6142
  "handle behavior event": ({
6160
6143
  context,
6161
6144
  event,
@@ -6179,10 +6162,14 @@ const editorMachine = xstate.setup({
6179
6162
  hasTag: (tag) => self.getSnapshot().hasTag(tag),
6180
6163
  internalDrag: context.internalDrag
6181
6164
  }),
6182
- nativeEvent: event.nativeEvent,
6183
- defaultActionCallback: event.type === "behavior event" ? event.defaultActionCallback : void 0
6165
+ nativeEvent: event.nativeEvent
6184
6166
  });
6185
6167
  }
6168
+ },
6169
+ guards: {
6170
+ "slate is busy": ({
6171
+ context
6172
+ }) => context.slateEditor ? context.slateEditor.operations.length > 0 : !1
6186
6173
  }
6187
6174
  }).createMachine({
6188
6175
  id: "editor",
@@ -6361,6 +6348,17 @@ const editorMachine = xstate.setup({
6361
6348
  },
6362
6349
  "behavior event": {
6363
6350
  actions: "handle behavior event"
6351
+ },
6352
+ blur: {
6353
+ actions: "handle blur"
6354
+ },
6355
+ focus: {
6356
+ target: ".focusing",
6357
+ actions: [xstate.assign({
6358
+ slateEditor: ({
6359
+ event
6360
+ }) => event.editor
6361
+ })]
6364
6362
  }
6365
6363
  },
6366
6364
  initial: "idle",
@@ -6380,6 +6378,27 @@ const editorMachine = xstate.setup({
6380
6378
  }
6381
6379
  }
6382
6380
  },
6381
+ focusing: {
6382
+ initial: "checking if busy",
6383
+ states: {
6384
+ "checking if busy": {
6385
+ always: [{
6386
+ guard: "slate is busy",
6387
+ target: "busy"
6388
+ }, {
6389
+ target: "#editor.edit mode.editable.idle",
6390
+ actions: ["handle focus"]
6391
+ }]
6392
+ },
6393
+ busy: {
6394
+ after: {
6395
+ 10: {
6396
+ target: "checking if busy"
6397
+ }
6398
+ }
6399
+ }
6400
+ }
6401
+ },
6383
6402
  "dragging internally": {
6384
6403
  exit: [({
6385
6404
  context
@@ -6712,19 +6731,13 @@ function createEditableAPI(editor, editorActor) {
6712
6731
  return {
6713
6732
  focus: () => {
6714
6733
  editorActor.send({
6715
- type: "behavior event",
6716
- behaviorEvent: {
6717
- type: "focus"
6718
- },
6734
+ type: "focus",
6719
6735
  editor
6720
6736
  });
6721
6737
  },
6722
6738
  blur: () => {
6723
6739
  editorActor.send({
6724
- type: "behavior event",
6725
- behaviorEvent: {
6726
- type: "blur"
6727
- },
6740
+ type: "blur",
6728
6741
  editor
6729
6742
  });
6730
6743
  },
@@ -7136,6 +7149,18 @@ function createInternalEditorFromActor(editorActor, legacySchema) {
7136
7149
  case "update maxBlocks":
7137
7150
  editorActor.send(event);
7138
7151
  break;
7152
+ case "blur":
7153
+ editorActor.send({
7154
+ type: "blur",
7155
+ editor: slateEditor.instance
7156
+ });
7157
+ break;
7158
+ case "focus":
7159
+ editorActor.send({
7160
+ type: "focus",
7161
+ editor: slateEditor.instance
7162
+ });
7163
+ break;
7139
7164
  case "insert.block object":
7140
7165
  editorActor.send({
7141
7166
  type: "behavior event",