Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for empty delta causing consecutive changes to replace previous stack #4554 #4555

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/quill/src/modules/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class History extends Module<HistoryOptions> {
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 &&
Expand All @@ -120,10 +121,10 @@ class History extends Module<HistoryOptions> {
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) {
Expand Down
34 changes: 34 additions & 0 deletions packages/quill/test/unit/modules/history.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down