diff --git a/packages/quill/src/modules/history.ts b/packages/quill/src/modules/history.ts index 73cf47ada6..eb1c99eedd 100644 --- a/packages/quill/src/modules/history.ts +++ b/packages/quill/src/modules/history.ts @@ -110,6 +110,7 @@ class History extends Module { let undoDelta = changeDelta.invert(oldDelta); let undoRange = this.currentRange; const timestamp = Date.now(); + if (undoDelta.length() === 0) return; if ( // @ts-expect-error Fix me later this.lastRecorded + this.options.delay > timestamp && @@ -120,10 +121,10 @@ class History extends Module { undoDelta = undoDelta.compose(item.delta); undoRange = item.range; } + if (undoDelta.length() === 0) return; } else { this.lastRecorded = timestamp; } - if (undoDelta.length() === 0) return; this.stack.undo.push({ delta: undoDelta, range: undoRange }); // @ts-expect-error Fix me later if (this.stack.undo.length > this.options.maxStack) { diff --git a/packages/quill/test/unit/modules/history.spec.ts b/packages/quill/test/unit/modules/history.spec.ts index d8ab3e8652..146cc762b5 100644 --- a/packages/quill/test/unit/modules/history.spec.ts +++ b/packages/quill/test/unit/modules/history.spec.ts @@ -210,6 +210,40 @@ describe('History', () => { ); }); + test('correctly ignores empty changes', async () => { + const { quill } = setup(); + quill.updateContents( + new Delta().retain(12).insert('es'), + Quill.sources.USER, + ); + quill.history.lastRecorded = 0; + quill.updateContents( + new Delta().retain(14).insert('!'), + Quill.sources.USER, + ); + // @ts-expect-error + await sleep((quill.history.options.delay as number) * 1.25); + + // empty change + quill.deleteText({ + index: 2, + length: 0, + }); + + quill.updateContents( + new Delta().retain(15).insert('!'), + Quill.sources.USER, + ); + expect(quill.getContents()).toEqual( + new Delta().insert('The lazy foxes!!\n'), + ); + + quill.history.undo(); + expect(quill.getContents()).toEqual( + new Delta().insert('The lazy foxes!\n'), + ); + }); + test('ignore remote changes', () => { const { quill } = setup(); // @ts-expect-error