@portabletext/editor 2.8.3 → 2.8.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.
package/lib/index.js CHANGED
@@ -3698,19 +3698,17 @@ function toInt(num) {
3698
3698
  return parseInt(num, 10);
3699
3699
  }
3700
3700
  const CURRENT_UNDO_STEP = /* @__PURE__ */ new WeakMap();
3701
- function withUndoStep(editor, fn) {
3702
- const current = CURRENT_UNDO_STEP.get(editor);
3703
- if (current) {
3704
- fn();
3705
- return;
3706
- }
3707
- CURRENT_UNDO_STEP.set(editor, current ?? {
3708
- undoStepId: defaultKeyGenerator()
3709
- }), fn(), CURRENT_UNDO_STEP.set(editor, void 0);
3710
- }
3711
3701
  function getCurrentUndoStepId(editor) {
3712
3702
  return CURRENT_UNDO_STEP.get(editor)?.undoStepId;
3713
3703
  }
3704
+ function createUndoStep(editor) {
3705
+ CURRENT_UNDO_STEP.set(editor, {
3706
+ undoStepId: defaultKeyGenerator()
3707
+ });
3708
+ }
3709
+ function clearUndoStep(editor) {
3710
+ CURRENT_UNDO_STEP.set(editor, void 0);
3711
+ }
3714
3712
  const debug$b = debugWithName("plugin:withUndoRedo"), SAVING = /* @__PURE__ */ new WeakMap(), REMOTE_PATCHES = /* @__PURE__ */ new WeakMap(), UNDO_STEP_LIMIT = 1e3, isSaving = (editor) => {
3715
3713
  const state = SAVING.get(editor);
3716
3714
  return state === void 0 ? !0 : state;
@@ -9056,7 +9054,7 @@ function performEvent({
9056
9054
  nativeEvent,
9057
9055
  sendBack
9058
9056
  }) {
9059
- debug$6(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2));
9057
+ mode === "send" && !isNativeBehaviorEvent(event) && createUndoStep(editor), debug$6(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2));
9060
9058
  const eventBehaviors = [...remainingEventBehaviors, ...abstractBehaviors].filter((behavior) => {
9061
9059
  if (behavior.on === "*")
9062
9060
  return !0;
@@ -9064,7 +9062,7 @@ function performEvent({
9064
9062
  return listenedNamespace !== void 0 && eventNamespace !== void 0 && listenedNamespace === eventNamespace || listenedNamespace !== void 0 && eventNamespace === void 0 && listenedNamespace === event.type ? !0 : behavior.on === event.type;
9065
9063
  });
9066
9064
  if (eventBehaviors.length === 0 && isSyntheticBehaviorEvent(event)) {
9067
- nativeEvent?.preventDefault(), withApplyingBehaviorOperations(editor, () => {
9065
+ nativeEvent?.preventDefault(), mode === "send" && clearUndoStep(editor), withApplyingBehaviorOperations(editor, () => {
9068
9066
  debug$6(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2)), performOperation({
9069
9067
  context: {
9070
9068
  keyGenerator,
@@ -9108,63 +9106,63 @@ function performEvent({
9108
9106
  }
9109
9107
  if (actions.length !== 0) {
9110
9108
  if (nativeEventPrevented = actions.some((action) => action.type === "raise" || action.type === "execute") || !actions.some((action) => action.type === "forward"), actions.some((action) => action.type === "execute")) {
9111
- withUndoStep(editor, () => {
9112
- for (const action of actions) {
9113
- if (action.type === "effect") {
9114
- try {
9115
- action.effect({
9116
- send: sendBack
9117
- });
9118
- } catch (error) {
9119
- console.error(new Error(`Executing effect as a result of "${event.type}" failed due to: ${error.message}`));
9120
- }
9121
- continue;
9122
- }
9123
- if (action.type === "forward") {
9124
- const remainingEventBehaviors2 = eventBehaviors.slice(eventBehaviorIndex + 1);
9125
- performEvent({
9126
- mode: "forward",
9127
- behaviors,
9128
- remainingEventBehaviors: remainingEventBehaviors2,
9129
- event: action.event,
9130
- editor,
9131
- keyGenerator,
9132
- schema,
9133
- getSnapshot,
9134
- nativeEvent,
9135
- sendBack
9136
- });
9137
- continue;
9138
- }
9139
- if (action.type === "raise") {
9140
- performEvent({
9141
- mode: "raise",
9142
- behaviors,
9143
- remainingEventBehaviors: behaviors,
9144
- event: action.event,
9145
- editor,
9146
- keyGenerator,
9147
- schema,
9148
- getSnapshot,
9149
- nativeEvent,
9150
- sendBack
9109
+ createUndoStep(editor);
9110
+ for (const action of actions) {
9111
+ if (action.type === "effect") {
9112
+ try {
9113
+ action.effect({
9114
+ send: sendBack
9151
9115
  });
9152
- continue;
9116
+ } catch (error) {
9117
+ console.error(new Error(`Executing effect as a result of "${event.type}" failed due to: ${error.message}`));
9153
9118
  }
9119
+ continue;
9120
+ }
9121
+ if (action.type === "forward") {
9122
+ const remainingEventBehaviors2 = eventBehaviors.slice(eventBehaviorIndex + 1);
9154
9123
  performEvent({
9155
- mode: "execute",
9124
+ mode: "forward",
9156
9125
  behaviors,
9157
- remainingEventBehaviors: [],
9126
+ remainingEventBehaviors: remainingEventBehaviors2,
9158
9127
  event: action.event,
9159
9128
  editor,
9160
9129
  keyGenerator,
9161
9130
  schema,
9162
9131
  getSnapshot,
9163
- nativeEvent: void 0,
9132
+ nativeEvent,
9164
9133
  sendBack
9165
9134
  });
9135
+ continue;
9166
9136
  }
9167
- });
9137
+ if (action.type === "raise") {
9138
+ performEvent({
9139
+ mode: "raise",
9140
+ behaviors,
9141
+ remainingEventBehaviors: behaviors,
9142
+ event: action.event,
9143
+ editor,
9144
+ keyGenerator,
9145
+ schema,
9146
+ getSnapshot,
9147
+ nativeEvent,
9148
+ sendBack
9149
+ });
9150
+ continue;
9151
+ }
9152
+ performEvent({
9153
+ mode: "execute",
9154
+ behaviors,
9155
+ remainingEventBehaviors: [],
9156
+ event: action.event,
9157
+ editor,
9158
+ keyGenerator,
9159
+ schema,
9160
+ getSnapshot,
9161
+ nativeEvent: void 0,
9162
+ sendBack
9163
+ });
9164
+ }
9165
+ clearUndoStep(editor);
9168
9166
  continue;
9169
9167
  }
9170
9168
  for (const action of actions) {
@@ -9216,7 +9214,7 @@ function performEvent({
9216
9214
  break;
9217
9215
  }
9218
9216
  }
9219
- !defaultBehaviorOverwritten && isSyntheticBehaviorEvent(event) ? (nativeEvent?.preventDefault(), withApplyingBehaviorOperations(editor, () => {
9217
+ !defaultBehaviorOverwritten && isSyntheticBehaviorEvent(event) ? (nativeEvent?.preventDefault(), mode === "send" && clearUndoStep(editor), withApplyingBehaviorOperations(editor, () => {
9220
9218
  debug$6(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2)), performOperation({
9221
9219
  context: {
9222
9220
  keyGenerator,
@@ -9297,7 +9295,44 @@ function createEditorSnapshot({
9297
9295
  decoratorState: editor.decoratorState
9298
9296
  };
9299
9297
  }
9300
- const debug$5 = debugWithName("editor machine"), editorMachine = setup({
9298
+ const debug$5 = debugWithName("editor machine");
9299
+ function rerouteExternalBehaviorEvent({
9300
+ event,
9301
+ slateEditor
9302
+ }) {
9303
+ switch (event.type) {
9304
+ case "blur":
9305
+ return {
9306
+ type: "blur",
9307
+ editor: slateEditor
9308
+ };
9309
+ case "focus":
9310
+ return {
9311
+ type: "focus",
9312
+ editor: slateEditor
9313
+ };
9314
+ case "insert.block object":
9315
+ return {
9316
+ type: "behavior event",
9317
+ behaviorEvent: {
9318
+ type: "insert.block",
9319
+ block: {
9320
+ _type: event.blockObject.name,
9321
+ ...event.blockObject.value ?? {}
9322
+ },
9323
+ placement: event.placement
9324
+ },
9325
+ editor: slateEditor
9326
+ };
9327
+ default:
9328
+ return {
9329
+ type: "behavior event",
9330
+ behaviorEvent: event,
9331
+ editor: slateEditor
9332
+ };
9333
+ }
9334
+ }
9335
+ const editorMachine = setup({
9301
9336
  types: {
9302
9337
  context: {},
9303
9338
  events: {},
@@ -9399,7 +9434,7 @@ const debug$5 = debugWithName("editor machine"), editorMachine = setup({
9399
9434
  try {
9400
9435
  const behaviors = [...context.behaviors.values()].map((config) => config.behavior);
9401
9436
  performEvent({
9402
- mode: "raise",
9437
+ mode: "send",
9403
9438
  behaviors,
9404
9439
  remainingEventBehaviors: behaviors,
9405
9440
  event: event.behaviorEvent,
@@ -9421,11 +9456,10 @@ const debug$5 = debugWithName("editor machine"), editorMachine = setup({
9421
9456
  self.send(eventSentBack);
9422
9457
  return;
9423
9458
  }
9424
- self.send({
9425
- type: "behavior event",
9426
- behaviorEvent: eventSentBack,
9427
- editor: event.editor
9428
- });
9459
+ self.send(rerouteExternalBehaviorEvent({
9460
+ event: eventSentBack,
9461
+ slateEditor: event.editor
9462
+ }));
9429
9463
  }
9430
9464
  });
9431
9465
  } catch (error) {
@@ -11371,38 +11405,11 @@ function createInternalEditor(config) {
11371
11405
  case "update maxBlocks":
11372
11406
  editorActor.send(event);
11373
11407
  break;
11374
- case "blur":
11375
- editorActor.send({
11376
- type: "blur",
11377
- editor: slateEditor.instance
11378
- });
11379
- break;
11380
- case "focus":
11381
- editorActor.send({
11382
- type: "focus",
11383
- editor: slateEditor.instance
11384
- });
11385
- break;
11386
- case "insert.block object":
11387
- editorActor.send({
11388
- type: "behavior event",
11389
- behaviorEvent: {
11390
- type: "insert.block",
11391
- block: {
11392
- _type: event.blockObject.name,
11393
- ...event.blockObject.value ?? {}
11394
- },
11395
- placement: event.placement
11396
- },
11397
- editor: slateEditor.instance
11398
- });
11399
- break;
11400
11408
  default:
11401
- editorActor.send({
11402
- type: "behavior event",
11403
- behaviorEvent: event,
11404
- editor: slateEditor.instance
11405
- });
11409
+ editorActor.send(rerouteExternalBehaviorEvent({
11410
+ event,
11411
+ slateEditor: slateEditor.instance
11412
+ }));
11406
11413
  }
11407
11414
  },
11408
11415
  on: (event, listener) => relayActor.on(event, (event2) => {