@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/_chunks-dts/behavior.types.action.d.cts +65 -65
- package/lib/_chunks-dts/behavior.types.action.d.ts +3 -3
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +103 -96
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +103 -96
- package/lib/index.js.map +1 -1
- package/package.json +12 -12
- package/src/behaviors/behavior.perform-event.ts +71 -61
- package/src/behaviors/behavior.types.action.ts +2 -1
- package/src/converters/converter.text-plain.test.ts +1 -2
- package/src/editor/create-editor.ts +7 -35
- package/src/editor/editor-machine.ts +54 -7
- package/src/editor/with-undo-step.ts +10 -0
- package/src/internal-utils/test-editor.tsx +51 -10
package/lib/index.cjs
CHANGED
|
@@ -3669,19 +3669,17 @@ function toInt(num) {
|
|
|
3669
3669
|
return parseInt(num, 10);
|
|
3670
3670
|
}
|
|
3671
3671
|
const CURRENT_UNDO_STEP = /* @__PURE__ */ new WeakMap();
|
|
3672
|
-
function withUndoStep(editor, fn) {
|
|
3673
|
-
const current = CURRENT_UNDO_STEP.get(editor);
|
|
3674
|
-
if (current) {
|
|
3675
|
-
fn();
|
|
3676
|
-
return;
|
|
3677
|
-
}
|
|
3678
|
-
CURRENT_UNDO_STEP.set(editor, current ?? {
|
|
3679
|
-
undoStepId: util_sliceBlocks.defaultKeyGenerator()
|
|
3680
|
-
}), fn(), CURRENT_UNDO_STEP.set(editor, void 0);
|
|
3681
|
-
}
|
|
3682
3672
|
function getCurrentUndoStepId(editor) {
|
|
3683
3673
|
return CURRENT_UNDO_STEP.get(editor)?.undoStepId;
|
|
3684
3674
|
}
|
|
3675
|
+
function createUndoStep(editor) {
|
|
3676
|
+
CURRENT_UNDO_STEP.set(editor, {
|
|
3677
|
+
undoStepId: util_sliceBlocks.defaultKeyGenerator()
|
|
3678
|
+
});
|
|
3679
|
+
}
|
|
3680
|
+
function clearUndoStep(editor) {
|
|
3681
|
+
CURRENT_UNDO_STEP.set(editor, void 0);
|
|
3682
|
+
}
|
|
3685
3683
|
const debug$b = debugWithName("plugin:withUndoRedo"), SAVING = /* @__PURE__ */ new WeakMap(), REMOTE_PATCHES = /* @__PURE__ */ new WeakMap(), UNDO_STEP_LIMIT = 1e3, isSaving = (editor) => {
|
|
3686
3684
|
const state = SAVING.get(editor);
|
|
3687
3685
|
return state === void 0 ? !0 : state;
|
|
@@ -9027,7 +9025,7 @@ function performEvent({
|
|
|
9027
9025
|
nativeEvent,
|
|
9028
9026
|
sendBack
|
|
9029
9027
|
}) {
|
|
9030
|
-
debug$6(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2));
|
|
9028
|
+
mode === "send" && !isNativeBehaviorEvent(event) && createUndoStep(editor), debug$6(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2));
|
|
9031
9029
|
const eventBehaviors = [...remainingEventBehaviors, ...abstractBehaviors].filter((behavior) => {
|
|
9032
9030
|
if (behavior.on === "*")
|
|
9033
9031
|
return !0;
|
|
@@ -9035,7 +9033,7 @@ function performEvent({
|
|
|
9035
9033
|
return listenedNamespace !== void 0 && eventNamespace !== void 0 && listenedNamespace === eventNamespace || listenedNamespace !== void 0 && eventNamespace === void 0 && listenedNamespace === event.type ? !0 : behavior.on === event.type;
|
|
9036
9034
|
});
|
|
9037
9035
|
if (eventBehaviors.length === 0 && isSyntheticBehaviorEvent(event)) {
|
|
9038
|
-
nativeEvent?.preventDefault(), withApplyingBehaviorOperations(editor, () => {
|
|
9036
|
+
nativeEvent?.preventDefault(), mode === "send" && clearUndoStep(editor), withApplyingBehaviorOperations(editor, () => {
|
|
9039
9037
|
debug$6(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2)), performOperation({
|
|
9040
9038
|
context: {
|
|
9041
9039
|
keyGenerator,
|
|
@@ -9079,63 +9077,63 @@ function performEvent({
|
|
|
9079
9077
|
}
|
|
9080
9078
|
if (actions.length !== 0) {
|
|
9081
9079
|
if (nativeEventPrevented = actions.some((action) => action.type === "raise" || action.type === "execute") || !actions.some((action) => action.type === "forward"), actions.some((action) => action.type === "execute")) {
|
|
9082
|
-
|
|
9083
|
-
|
|
9084
|
-
|
|
9085
|
-
|
|
9086
|
-
|
|
9087
|
-
|
|
9088
|
-
});
|
|
9089
|
-
} catch (error) {
|
|
9090
|
-
console.error(new Error(`Executing effect as a result of "${event.type}" failed due to: ${error.message}`));
|
|
9091
|
-
}
|
|
9092
|
-
continue;
|
|
9093
|
-
}
|
|
9094
|
-
if (action.type === "forward") {
|
|
9095
|
-
const remainingEventBehaviors2 = eventBehaviors.slice(eventBehaviorIndex + 1);
|
|
9096
|
-
performEvent({
|
|
9097
|
-
mode: "forward",
|
|
9098
|
-
behaviors,
|
|
9099
|
-
remainingEventBehaviors: remainingEventBehaviors2,
|
|
9100
|
-
event: action.event,
|
|
9101
|
-
editor,
|
|
9102
|
-
keyGenerator,
|
|
9103
|
-
schema: schema2,
|
|
9104
|
-
getSnapshot,
|
|
9105
|
-
nativeEvent,
|
|
9106
|
-
sendBack
|
|
9107
|
-
});
|
|
9108
|
-
continue;
|
|
9109
|
-
}
|
|
9110
|
-
if (action.type === "raise") {
|
|
9111
|
-
performEvent({
|
|
9112
|
-
mode: "raise",
|
|
9113
|
-
behaviors,
|
|
9114
|
-
remainingEventBehaviors: behaviors,
|
|
9115
|
-
event: action.event,
|
|
9116
|
-
editor,
|
|
9117
|
-
keyGenerator,
|
|
9118
|
-
schema: schema2,
|
|
9119
|
-
getSnapshot,
|
|
9120
|
-
nativeEvent,
|
|
9121
|
-
sendBack
|
|
9080
|
+
createUndoStep(editor);
|
|
9081
|
+
for (const action of actions) {
|
|
9082
|
+
if (action.type === "effect") {
|
|
9083
|
+
try {
|
|
9084
|
+
action.effect({
|
|
9085
|
+
send: sendBack
|
|
9122
9086
|
});
|
|
9123
|
-
|
|
9087
|
+
} catch (error) {
|
|
9088
|
+
console.error(new Error(`Executing effect as a result of "${event.type}" failed due to: ${error.message}`));
|
|
9124
9089
|
}
|
|
9090
|
+
continue;
|
|
9091
|
+
}
|
|
9092
|
+
if (action.type === "forward") {
|
|
9093
|
+
const remainingEventBehaviors2 = eventBehaviors.slice(eventBehaviorIndex + 1);
|
|
9125
9094
|
performEvent({
|
|
9126
|
-
mode: "
|
|
9095
|
+
mode: "forward",
|
|
9127
9096
|
behaviors,
|
|
9128
|
-
remainingEventBehaviors:
|
|
9097
|
+
remainingEventBehaviors: remainingEventBehaviors2,
|
|
9129
9098
|
event: action.event,
|
|
9130
9099
|
editor,
|
|
9131
9100
|
keyGenerator,
|
|
9132
9101
|
schema: schema2,
|
|
9133
9102
|
getSnapshot,
|
|
9134
|
-
nativeEvent
|
|
9103
|
+
nativeEvent,
|
|
9135
9104
|
sendBack
|
|
9136
9105
|
});
|
|
9106
|
+
continue;
|
|
9137
9107
|
}
|
|
9138
|
-
|
|
9108
|
+
if (action.type === "raise") {
|
|
9109
|
+
performEvent({
|
|
9110
|
+
mode: "raise",
|
|
9111
|
+
behaviors,
|
|
9112
|
+
remainingEventBehaviors: behaviors,
|
|
9113
|
+
event: action.event,
|
|
9114
|
+
editor,
|
|
9115
|
+
keyGenerator,
|
|
9116
|
+
schema: schema2,
|
|
9117
|
+
getSnapshot,
|
|
9118
|
+
nativeEvent,
|
|
9119
|
+
sendBack
|
|
9120
|
+
});
|
|
9121
|
+
continue;
|
|
9122
|
+
}
|
|
9123
|
+
performEvent({
|
|
9124
|
+
mode: "execute",
|
|
9125
|
+
behaviors,
|
|
9126
|
+
remainingEventBehaviors: [],
|
|
9127
|
+
event: action.event,
|
|
9128
|
+
editor,
|
|
9129
|
+
keyGenerator,
|
|
9130
|
+
schema: schema2,
|
|
9131
|
+
getSnapshot,
|
|
9132
|
+
nativeEvent: void 0,
|
|
9133
|
+
sendBack
|
|
9134
|
+
});
|
|
9135
|
+
}
|
|
9136
|
+
clearUndoStep(editor);
|
|
9139
9137
|
continue;
|
|
9140
9138
|
}
|
|
9141
9139
|
for (const action of actions) {
|
|
@@ -9187,7 +9185,7 @@ function performEvent({
|
|
|
9187
9185
|
break;
|
|
9188
9186
|
}
|
|
9189
9187
|
}
|
|
9190
|
-
!defaultBehaviorOverwritten && isSyntheticBehaviorEvent(event) ? (nativeEvent?.preventDefault(), withApplyingBehaviorOperations(editor, () => {
|
|
9188
|
+
!defaultBehaviorOverwritten && isSyntheticBehaviorEvent(event) ? (nativeEvent?.preventDefault(), mode === "send" && clearUndoStep(editor), withApplyingBehaviorOperations(editor, () => {
|
|
9191
9189
|
debug$6(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2)), performOperation({
|
|
9192
9190
|
context: {
|
|
9193
9191
|
keyGenerator,
|
|
@@ -9268,7 +9266,44 @@ function createEditorSnapshot({
|
|
|
9268
9266
|
decoratorState: editor.decoratorState
|
|
9269
9267
|
};
|
|
9270
9268
|
}
|
|
9271
|
-
const debug$5 = debugWithName("editor machine")
|
|
9269
|
+
const debug$5 = debugWithName("editor machine");
|
|
9270
|
+
function rerouteExternalBehaviorEvent({
|
|
9271
|
+
event,
|
|
9272
|
+
slateEditor
|
|
9273
|
+
}) {
|
|
9274
|
+
switch (event.type) {
|
|
9275
|
+
case "blur":
|
|
9276
|
+
return {
|
|
9277
|
+
type: "blur",
|
|
9278
|
+
editor: slateEditor
|
|
9279
|
+
};
|
|
9280
|
+
case "focus":
|
|
9281
|
+
return {
|
|
9282
|
+
type: "focus",
|
|
9283
|
+
editor: slateEditor
|
|
9284
|
+
};
|
|
9285
|
+
case "insert.block object":
|
|
9286
|
+
return {
|
|
9287
|
+
type: "behavior event",
|
|
9288
|
+
behaviorEvent: {
|
|
9289
|
+
type: "insert.block",
|
|
9290
|
+
block: {
|
|
9291
|
+
_type: event.blockObject.name,
|
|
9292
|
+
...event.blockObject.value ?? {}
|
|
9293
|
+
},
|
|
9294
|
+
placement: event.placement
|
|
9295
|
+
},
|
|
9296
|
+
editor: slateEditor
|
|
9297
|
+
};
|
|
9298
|
+
default:
|
|
9299
|
+
return {
|
|
9300
|
+
type: "behavior event",
|
|
9301
|
+
behaviorEvent: event,
|
|
9302
|
+
editor: slateEditor
|
|
9303
|
+
};
|
|
9304
|
+
}
|
|
9305
|
+
}
|
|
9306
|
+
const editorMachine = xstate.setup({
|
|
9272
9307
|
types: {
|
|
9273
9308
|
context: {},
|
|
9274
9309
|
events: {},
|
|
@@ -9370,7 +9405,7 @@ const debug$5 = debugWithName("editor machine"), editorMachine = xstate.setup({
|
|
|
9370
9405
|
try {
|
|
9371
9406
|
const behaviors = [...context.behaviors.values()].map((config) => config.behavior);
|
|
9372
9407
|
performEvent({
|
|
9373
|
-
mode: "
|
|
9408
|
+
mode: "send",
|
|
9374
9409
|
behaviors,
|
|
9375
9410
|
remainingEventBehaviors: behaviors,
|
|
9376
9411
|
event: event.behaviorEvent,
|
|
@@ -9392,11 +9427,10 @@ const debug$5 = debugWithName("editor machine"), editorMachine = xstate.setup({
|
|
|
9392
9427
|
self.send(eventSentBack);
|
|
9393
9428
|
return;
|
|
9394
9429
|
}
|
|
9395
|
-
self.send({
|
|
9396
|
-
|
|
9397
|
-
|
|
9398
|
-
|
|
9399
|
-
});
|
|
9430
|
+
self.send(rerouteExternalBehaviorEvent({
|
|
9431
|
+
event: eventSentBack,
|
|
9432
|
+
slateEditor: event.editor
|
|
9433
|
+
}));
|
|
9400
9434
|
}
|
|
9401
9435
|
});
|
|
9402
9436
|
} catch (error) {
|
|
@@ -11342,38 +11376,11 @@ function createInternalEditor(config) {
|
|
|
11342
11376
|
case "update maxBlocks":
|
|
11343
11377
|
editorActor.send(event);
|
|
11344
11378
|
break;
|
|
11345
|
-
case "blur":
|
|
11346
|
-
editorActor.send({
|
|
11347
|
-
type: "blur",
|
|
11348
|
-
editor: slateEditor.instance
|
|
11349
|
-
});
|
|
11350
|
-
break;
|
|
11351
|
-
case "focus":
|
|
11352
|
-
editorActor.send({
|
|
11353
|
-
type: "focus",
|
|
11354
|
-
editor: slateEditor.instance
|
|
11355
|
-
});
|
|
11356
|
-
break;
|
|
11357
|
-
case "insert.block object":
|
|
11358
|
-
editorActor.send({
|
|
11359
|
-
type: "behavior event",
|
|
11360
|
-
behaviorEvent: {
|
|
11361
|
-
type: "insert.block",
|
|
11362
|
-
block: {
|
|
11363
|
-
_type: event.blockObject.name,
|
|
11364
|
-
...event.blockObject.value ?? {}
|
|
11365
|
-
},
|
|
11366
|
-
placement: event.placement
|
|
11367
|
-
},
|
|
11368
|
-
editor: slateEditor.instance
|
|
11369
|
-
});
|
|
11370
|
-
break;
|
|
11371
11379
|
default:
|
|
11372
|
-
editorActor.send({
|
|
11373
|
-
|
|
11374
|
-
|
|
11375
|
-
|
|
11376
|
-
});
|
|
11380
|
+
editorActor.send(rerouteExternalBehaviorEvent({
|
|
11381
|
+
event,
|
|
11382
|
+
slateEditor: slateEditor.instance
|
|
11383
|
+
}));
|
|
11377
11384
|
}
|
|
11378
11385
|
},
|
|
11379
11386
|
on: (event, listener) => relayActor.on(event, (event2) => {
|