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 collaborative undo correctness (fixes #2105) #2500

Merged
merged 7 commits into from
Feb 12, 2019
Merged
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
62 changes: 23 additions & 39 deletions modules/history.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Scope } from 'parchment';
import Delta, { Op } from 'quill-delta';
import Quill from '../core/quill';
import Module from '../core/module';

Expand Down Expand Up @@ -39,12 +38,14 @@ class History extends Module {
change(source, dest) {
if (this.stack[source].length === 0) return;
const delta = this.stack[source].pop();
this.stack[dest].push(delta);
const base = this.quill.getContents();
const inverseDelta = delta.invert(base);
this.stack[dest].push(inverseDelta);
this.lastRecorded = 0;
this.ignoreChange = true;
this.quill.updateContents(delta[source], Quill.sources.USER);
this.quill.updateContents(delta, Quill.sources.USER);
this.ignoreChange = false;
const index = getLastChangeIndex(this.quill.scroll, delta[source]);
const index = getLastChangeIndex(this.quill.scroll, delta);
this.quill.setSelection(index);
}

Expand All @@ -59,25 +60,19 @@ class History extends Module {
record(changeDelta, oldDelta) {
if (changeDelta.ops.length === 0) return;
this.stack.redo = [];
let undoDelta = guessUndoDelta(changeDelta);
if (undoDelta == null) {
undoDelta = this.quill.getContents().diff(oldDelta);
}
let undoDelta = changeDelta.invert(oldDelta);
const timestamp = Date.now();
if (
this.lastRecorded + this.options.delay > timestamp &&
this.stack.undo.length > 0
) {
const delta = this.stack.undo.pop();
undoDelta = undoDelta.compose(delta.undo);
changeDelta = delta.redo.compose(changeDelta);
undoDelta = undoDelta.compose(delta);
} else {
this.lastRecorded = timestamp;
}
this.stack.undo.push({
redo: changeDelta,
undo: undoDelta,
});
if (undoDelta.length() === 0) return;
this.stack.undo.push(undoDelta);
if (this.stack.undo.length > this.options.maxStack) {
this.stack.undo.shift();
}
Expand All @@ -88,14 +83,8 @@ class History extends Module {
}

transform(delta) {
this.stack.undo.forEach(change => {
change.undo = delta.transform(change.undo, true);
change.redo = delta.transform(change.redo, true);
});
this.stack.redo.forEach(change => {
change.undo = delta.transform(change.undo, true);
change.redo = delta.transform(change.redo, true);
});
transformStack(this.stack.undo, delta);
transformStack(this.stack.redo, delta);
}

undo() {
Expand All @@ -108,6 +97,18 @@ History.DEFAULTS = {
userOnly: false,
};

function transformStack(stack, delta) {
let remoteDelta = delta;
for (let i = stack.length - 1; i >= 0; i -= 1) {
const oldDelta = stack[i];
stack[i] = remoteDelta.transform(oldDelta, true);
remoteDelta = oldDelta.transform(remoteDelta);
if (stack[i].length() === 0) {
stack.splice(i, 1);
}
}
}

function endsWithNewlineChange(scroll, delta) {
const lastOp = delta.ops[delta.ops.length - 1];
if (lastOp == null) return false;
Expand All @@ -133,21 +134,4 @@ function getLastChangeIndex(scroll, delta) {
return changeIndex;
}

function guessUndoDelta(delta) {
const undoDelta = new Delta();
let failed = false;
delta.forEach(op => {
if (op.insert) {
undoDelta.delete(Op.length(op));
} else if (op.retain && op.attributes == null) {
undoDelta.retain(op.retain);
} else {
failed = true;
return false;
}
return true;
});
return failed ? null : undoDelta;
}

export { History as default, getLastChangeIndex };
Loading