Skip to content

Commit

Permalink
optimize composing rest of delta
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Aug 1, 2018
1 parent 4836397 commit 5c40363
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/delta.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ Delta.prototype.compose = function (other) {
var attributes = op.attributes.compose(thisOp.attributes, otherOp.attributes, typeof thisOp.retain === 'number');
if (attributes) newOp.attributes = attributes;
delta.push(newOp);

// Optimization if rest of other is just retain
if (!otherIter.hasNext() && equal(delta.ops[delta.ops.length - 1], newOp)) {
var rest = new Delta(thisIter.rest());
return delta.concat(rest).chop();
}

// Other op should be delete, we could be an insert or retain
// Insert + delete cancels out
} else if (typeof otherOp['delete'] === 'number' && typeof thisOp.retain === 'number') {
Expand Down
16 changes: 16 additions & 0 deletions lib/op.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,21 @@ Iterator.prototype.peekType = function () {
return 'retain';
};

Iterator.prototype.rest = function () {
if (!this.hasNext()) {
return [];
} else if (this.offset === 0) {
return this.ops.slice(this.index);
} else {
var offset = this.offset;
var index = this.index;
var next = this.next();
var rest = this.ops.slice(this.index);
this.offset = offset;
this.index = index;
return [next].concat(rest);
}
};


module.exports = lib;
28 changes: 26 additions & 2 deletions test/delta/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('compose()', function () {
expect(attr1).toEqual(attr2);
});

it('retain optimization', function () {
it('retain start optimization', function () {
var a = new Delta().insert('A', { bold: true })
.insert('B')
.insert('C', { bold: true })
Expand All @@ -152,7 +152,7 @@ describe('compose()', function () {
expect(a.compose(b)).toEqual(expected);
});

it('retain optimization split', function () {
it('retain start optimization split', function () {
var a = new Delta().insert('A', { bold: true })
.insert('B')
.insert('C', { bold: true })
Expand All @@ -168,4 +168,28 @@ describe('compose()', function () {
.delete(1);
expect(a.compose(b)).toEqual(expected);
});

it('retain end optimization', function () {
var a = new Delta().insert('A', { bold: true })
.insert('B')
.insert('C', { bold: true });
var b = new Delta().delete(1);
var expected = new Delta().insert('B').insert('C', { bold: true })
expect(a.compose(b)).toEqual(expected);
});

it('retain end optimization join', function () {
var a = new Delta().insert('A', { bold: true })
.insert('B')
.insert('C', { bold: true })
.insert('D')
.insert('E', { bold: true })
.insert('F')
var b = new Delta().retain(1).delete(1);
var expected = new Delta().insert('AC', { bold: true })
.insert('D')
.insert('E', { bold: true })
.insert('F')
expect(a.compose(b)).toEqual(expected);
});
});
21 changes: 21 additions & 0 deletions test/op.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,26 @@ describe('op', function () {
expect(iter.next(1)).toEqual({ retain: 1 });
expect(iter.next(2)).toEqual({ retain: 2 });
});

it('rest()', function () {
var iter = op.iterator(this.delta.ops);
iter.next(2);
expect(iter.rest()).toEqual([
{ insert: 'llo', attributes: { bold: true }},
{ retain: 3 },
{ insert: 2, attributes: { src: 'http://quilljs.com/' } },
{ delete: 4 }
]);
iter.next(3);
expect(iter.rest()).toEqual([
{ retain: 3 },
{ insert: 2, attributes: { src: 'http://quilljs.com/' } },
{ delete: 4 }
]);
iter.next(3);
iter.next(2);
iter.next(4);
expect(iter.rest()).toEqual([]);
})
});
});

0 comments on commit 5c40363

Please sign in to comment.