diff --git a/packages/slate-history/src/with-history.ts b/packages/slate-history/src/with-history.ts index d27345bcf..89af48c68 100644 --- a/packages/slate-history/src/with-history.ts +++ b/packages/slate-history/src/with-history.ts @@ -49,16 +49,7 @@ export const withHistory = (editor: T) => { const inverseOps = batch.map(Operation.inverse).reverse() for (const op of inverseOps) { - // If the final operation is deselecting the editor, skip it. This is - if ( - op === inverseOps[inverseOps.length - 1] && - op.type === 'set_selection' && - op.newProperties == null - ) { - continue - } else { - e.apply(op) - } + e.apply(op) } }) }) @@ -155,7 +146,10 @@ const shouldMerge = (op: Operation, prev: Operation | undefined): boolean => { */ const shouldSave = (op: Operation, prev: Operation | undefined): boolean => { - if (op.type === 'set_selection' && op.newProperties == null) { + if ( + op.type === 'set_selection' && + (op.properties == null || op.newProperties == null) + ) { return false } diff --git a/packages/slate-history/test/undo/cursor/keep_after_focus_and_remove_text_undo.js b/packages/slate-history/test/undo/cursor/keep_after_focus_and_remove_text_undo.js new file mode 100644 index 000000000..c35b715dd --- /dev/null +++ b/packages/slate-history/test/undo/cursor/keep_after_focus_and_remove_text_undo.js @@ -0,0 +1,49 @@ +/** @jsx jsx */ + +import assert from 'assert' +import { Transforms, Editor } from 'slate' +import { jsx } from '../..' + +export const run = editor => { + // focus at the end + Transforms.select(editor, { + anchor: { path: [0, 0], offset: 5 }, + focus: { path: [0, 0], offset: 5 }, + }) + // select all + Transforms.select(editor, { + anchor: { path: [0, 0], offset: 5 }, + focus: { path: [0, 0], offset: 0 }, + }) + // remove + Editor.deleteFragment(editor) + // blur + Transforms.deselect(editor) + // focus back + Transforms.select(editor, { + anchor: { path: [0, 0], offset: 0 }, + focus: { path: [0, 0], offset: 0 }, + }) +} + +export const input = ( + + Hello + +) + +export const output = { + children: [ + { + children: [ + { + text: 'Hello', + }, + ], + }, + ], + selection: { + anchor: { path: [0, 0], offset: 5 }, + focus: { path: [0, 0], offset: 5 }, + }, +}