Skip to content

Commit

Permalink
update quill-delta and use its invert()
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Feb 12, 2019
1 parent ee4814c commit 776df6d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 34 deletions.
25 changes: 3 additions & 22 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,7 +38,8 @@ class History extends Module {
change(source, dest) {
if (this.stack[source].length === 0) return;
const delta = this.stack[source].pop();
const inverseDelta = invertDelta(delta, this.quill.getContents());
const base = this.quill.getContents();
const inverseDelta = delta.invert(base);
this.stack[dest].push(inverseDelta);
this.lastRecorded = 0;
this.ignoreChange = true;
Expand All @@ -60,7 +60,7 @@ class History extends Module {
record(changeDelta, oldDelta) {
if (changeDelta.ops.length === 0) return;
this.stack.redo = [];
let undoDelta = invertDelta(changeDelta, oldDelta);
let undoDelta = changeDelta.invert(oldDelta);
const timestamp = Date.now();
if (
this.lastRecorded + this.options.delay > timestamp &&
Expand Down Expand Up @@ -134,23 +134,4 @@ function getLastChangeIndex(scroll, delta) {
return changeIndex;
}

function invertDelta(delta, base) {
const undoDelta = new Delta();
let baseIndex = 0;
delta.forEach(op => {
if (op.insert) {
undoDelta.delete(Op.length(op));
} else if (op.retain && op.attributes == null) {
undoDelta.retain(op.retain);
baseIndex += op.retain;
} else if (op.delete) {
base
.slice(baseIndex, baseIndex + op.delete)
.forEach(o => undoDelta.push(o));
baseIndex += op.delete;
}
});
return undoDelta;
}

export { History as default, getLastChangeIndex };
15 changes: 4 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"eventemitter3": "^3.1.0",
"extend": "^3.0.2",
"parchment": "2.0.0-dev.0",
"quill-delta": "4.1.0"
"quill-delta": "4.2.0"
},
"devDependencies": {
"@babel/core": "^7.2.2",
Expand Down

0 comments on commit 776df6d

Please sign in to comment.