slab/delta

Making Delete Op invertible?

lxcid opened this issue · 1 comments

lxcid commented

Quill Delta is pretty compact representation for Rich Text. I really like it.

One issue I recently found was that delete operation are no invertible. So it can make undo/redo hard to implement.

But due to rich text can make up of multiple operations, I believe that if delete op is to be invertible, it have to store an array of insert ops.

var delta = new Delta([
  { insert: 'Gandalf', attributes: { bold: true } },
  { insert: ' the ' },
  { insert: 'Grey', attributes: { color: '#ccc' } }
]);

// If we were to delete 'dalf the Grey'…

var oldDeleteOp = [{ retain: 3 }, { delete: 13 }];
var proposedDeleteOp = [{ retain: 3 }, { delete: [
  { insert: 'dalf', attributes: { bold: true } },
  { insert: ' the ' },
  { insert: 'Grey', attributes: { color: '#ccc' } }
] }];

Which also meaning, the delete op cannot store non-document delta op. This definitely complicate Quill Delta format though.

We had considered this in the past and decided on at least not yet. I think what is lacking is a compelling use case and without it other things will continually be prioritized above it. The undo/redo case is compelling but the couple times it did come up (like for Quill) the old contents was available so it was quite easy to implement:

var newDelta = oldDelta.compose(changeDelta);
var invertDelta = newDelta.diff(oldDelta);