Skip to content

Commit 5504800

Browse files
committed
undo on delete
1 parent 0de9504 commit 5504800

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

packages/super-editor/src/core/commands/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export * from './selectTextblockStart.js';
3232
export * from './selectTextblockEnd.js';
3333
export * from './insertContent.js';
3434
export * from './insertContentAt.js';
35+
export * from './undoInputRule.js';
3536

3637
// Lists
3738
export * from './wrapInList.js';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export const undoInputRule = () => ({ state, dispatch }) => {
2+
const plugins = state.plugins;
3+
4+
for (let i = 0; i < plugins.length; i += 1) {
5+
const plugin = plugins[i];
6+
let undoable;
7+
8+
// eslint-disable-next-line
9+
if (plugin.spec.isInputRules && (undoable = plugin.getState(state))) {
10+
if (dispatch) {
11+
const tr = state.tr;
12+
const toUndo = undoable.transform;
13+
14+
for (let j = toUndo.steps.length - 1; j >= 0; j -= 1) {
15+
tr.step(toUndo.steps[j].invert(toUndo.docs[j]));
16+
}
17+
18+
if (undoable.text) {
19+
const marks = tr.doc.resolve(undoable.from).marks();
20+
21+
tr.replaceWith(undoable.from, undoable.to, state.schema.text(undoable.text, marks));
22+
} else {
23+
tr.delete(undoable.from, undoable.to);
24+
}
25+
}
26+
27+
return true;
28+
}
29+
}
30+
31+
return false;
32+
}

packages/super-editor/src/core/extensions/keymap.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const Keymap = Extension.create({
2020

2121
const handleBackspace = () =>
2222
this.editor.commands.first(({ commands, tr }) => [
23+
() => commands.undoInputRule(),
2324
() => {
2425
tr.setMeta('inputType', 'deleteContentBackward');
2526
return false;

0 commit comments

Comments
 (0)