a-new-delta.mdx 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ---
  2. title: A New Delta
  3. date: 2014-09-29
  4. ---
  5. Part of providing a complete API in Quill is providing events for when and what changes occur in the editor. Those changes are currently represented by a [Delta](/docs/delta/) object, which aims to be intuitive, human-readable, and expressive for any change or document that might need to represented. Over the past few weeks I’ve been working on a new format that better fulfills those goals and addresses the challenges in the current format.
  6. Documentation for the new Delta format can be found in its own [GitHub repository](https://github.com/ottypes/rich-text) but I will go over some of the rationale behind some of the changes in this post.
  7. {/* more */}
  8. ### Reduced Complexity
  9. When the Delta format was originally designed, it had ambitious goals of being general purpose and being able to represent any kind of document. The new format reduces the scope to just rich text documents, allowing for a much tighter implementation[^1].
  10. Quill is not specifically built to be a collaborative editor but the ability to be used as one is a good benchmark of the API. The new Delta format maintains this capability and fulfills the specifications of an [ottype](https://github.com/ottypes/docs), making it compatible with [ShareJS](https://github.com/share/ShareJS).
  11. ### Explicit Deletes
  12. In the current Delta format, a delete operation is implied by a lack of a retain operation. Basically everything is deleted unless you say it should be kept. This has some nice properties from an implementation perspective[^2] but was probably the largest source of confusion for users trying to work with Deltas and challenged the human-readability goal. It is very difficult to keep track of indexes to figure out what was not accounted for, to figure out what should be deleted.
  13. The new format has an explicit delete operation and by default everything is kept. Here’s a comparison of the two formats both representing removing the ‘b’ in ‘abc’.
  14. ```javascript
  15. var oldFormat = {
  16. startLength: 3,
  17. endLength: 2,
  18. ops: [
  19. { start: 0, end: 1 },
  20. { start: 2, end: 3 }
  21. ]
  22. };
  23. var newFormat = {
  24. ops: [
  25. { retain: 1 },
  26. { delete: 1 }
  27. ]
  28. };
  29. ```
  30. A side effect of having explicit deletes and defaulting to keeping text is that in practice the representation for new Deltas will usually be smaller.
  31. ### Embed Support
  32. The new Delta format provides native support for embeds, which can be used to represent images, video, etc. There is no support for this in the current format and implementation is hackily achieved by representing an ‘!’ with a image key in the attributes (which will break when video support is added).
  33. ```javascript
  34. var oldFormat = {
  35. startLength: 0,
  36. endLength: 1,
  37. ops: [{
  38. text: '!',
  39. attributes: { image: 'https://octodex.github.com/images/labtocat.png' }
  40. }]
  41. };
  42. var newFormat = {
  43. ops: [{
  44. insert: 1, attributes: { image: 'https://octodex.github.com/images/labtocat.png' }
  45. }]
  46. };
  47. ```
  48. ### Going Forward
  49. This new format will be the finalized representation for changes and state in Quill going forward and is one of the major steps toward a 1.0 release (a topic for another post).
  50. [^1]: Currently 28658 vs 9507 lines of code (though in practice is less relevant due to minification and gzip).
  51. [^2]: Minimizes number of operations to support, and easy to calculate the length of text of the resulting document which is useful for sanity checks.