delta.mdx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ---
  2. title: Delta
  3. ---
  4. Deltas are a simple, yet expressive format that can be used to describe Quill's contents and changes. The format is a strict subset of JSON, is human readable, and easily parsible by machines. Deltas can describe any Quill document, includes all text and formatting information, without the ambiguity and complexity of HTML.
  5. <Hint>
  6. Don't be confused by its name <em>Delta</em>&mdash;Deltas represents both documents and changes to documents. If you think of Deltas as the instructions from going from one document to another, the way Deltas represent a document is by expressing the instructions starting from an empty document.
  7. </Hint>
  8. Deltas are implemented as a separate [standalone library](https://github.com/quilljs/delta/), allowing its use outside of Quill. It is suitable for [Operational Transform](https://en.wikipedia.org/wiki/Operational_transformation) and can be used in realtime, Google Docs like applications. For a more in depth explanation behind Deltas, see [Designing the Delta Format](/guides/designing-the-delta-format/).
  9. <Hint>
  10. It is not recommended to construct Deltas by hand&mdash;rather use the chainable [`insert()`](https://github.com/quilljs/delta#insert), [`delete()`](https://github.com/quilljs/delta#delete), and [`retain()`](https://github.com/quilljs/delta#retain) methods to create new Deltas. You can use [`import()`](/docs/api/#import) to access Delta from Quill.
  11. </Hint>
  12. ## Document
  13. The Delta format is almost entirely self-explanatory&mdash;the example below describes the string "Gandalf the Grey" where "Gandalf" is bolded and "Grey" is colored #cccccc.
  14. ```javascript
  15. {
  16. ops: [
  17. { insert: 'Gandalf', attributes: { bold: true } },
  18. { insert: ' the ' },
  19. { insert: 'Grey', attributes: { color: '#cccccc' } }
  20. ]
  21. }
  22. ```
  23. As its name would imply, describing content is actually a special case for Deltas. The above example is more specifically instructions to insert a bolded string "Gandalf", an unformatted string " the ", followed by the string "Grey" colored #cccccc. When Deltas are used to describe content, it can be thought of as the content that would be created if the Delta was applied to an empty document.
  24. Since Deltas are a data format, there is no inherent meaning to the values of `attribute` keypairs. For example, there is nothing in the Delta format that dictates color value must be in hex&mdash;this is a choice that Quill makes, and can be modified if desired with [Parchment](https://github.com/quilljs/parchment/).
  25. ### Embeds
  26. For non-text content such as images or formulas, the insert key can be an object. The object should have one key, which will be used to determine its type. This is the `blotName` if you are building custom content with [Parchment](https://github.com/quilljs/parchment/). Like text, embeds can still have an `attributes` key to describe formatting to be applied to the embed. All embeds have a length of one.
  27. ```javascript
  28. {
  29. ops: [{
  30. // An image link
  31. insert: {
  32. image: 'https://quilljs.com/assets/images/icon.png'
  33. },
  34. attributes: {
  35. link: 'https://quilljs.com'
  36. }
  37. }]
  38. }
  39. ```
  40. ### Line Formatting
  41. Attributes associated with a newline character describes formatting for that line.
  42. ```javascript
  43. {
  44. ops: [
  45. { insert: 'The Two Towers' },
  46. { insert: '\n', attributes: { header: 1 } },
  47. { insert: 'Aragorn sped on up the hill.\n' }
  48. ]
  49. }
  50. ```
  51. All Quill documents must end with a newline character, even if there is no formatting applied to the last line. This way, you will always have a character position to apply line formatting to.
  52. Many line formats are exclusive. For example Quill does not allow a line to simultaneously be both a header and a list, despite being possible to represent in the Delta format.
  53. ## Changes
  54. When you register a listener for Quill's [`text-change`](/docs/api/#text-change) event, one of the arguments you will get is a Delta describing what changed. In addition to `insert` operations, this Delta might also have `delete` or `retain` operations.
  55. ### Delete
  56. The `delete` operation instructs exactly what it implies: delete the next number of characters.
  57. ```javascript
  58. {
  59. ops: [
  60. { delete: 10 } // Delete the next 10 characters
  61. ]
  62. }
  63. ```
  64. Since `delete` operations do not include _what_ was deleted, a Delta is not reversible.
  65. ### Retain
  66. A `retain` operation simply means keep the next number of characters, without modification. If `attributes` is specified, it still means keep those characters, but apply the formatting specified by the `attributes` object. A `null` value for an attributes key is used to specify format removal.
  67. Starting with the above "Gandalf the Grey" example:
  68. ```javascript
  69. // {
  70. // ops: [
  71. // { insert: 'Gandalf', attributes: { bold: true } },
  72. // { insert: ' the ' },
  73. // { insert: 'Grey', attributes: { color: '#cccccc' } }
  74. // ]
  75. // }
  76. {
  77. ops: [
  78. // Unbold and italicize "Gandalf"
  79. { retain: 7, attributes: { bold: null, italic: true } },
  80. // Keep " the " as is
  81. { retain: 5 },
  82. // Insert "White" formatted with color #fff
  83. { insert: 'White', attributes: { color: '#fff' } },
  84. // Delete "Grey"
  85. { delete: 4 }
  86. ]
  87. }
  88. ```
  89. Note that a Delta's instructions always starts at the beginning of the document. And because of plain `retain` operations, we never need to specify an index for a `delete` or `insert` operation.
  90. ### Playground
  91. Play around with Quill and take a look at how its content and changes look. Open your developer console for another view into the Deltas.
  92. <SandpackWithQuillTemplate
  93. preferPreview
  94. afterEditor={`
  95. <pre id="playground" style="font-size: 12px">
  96. </pre>
  97. `}
  98. files={{
  99. 'index.js': `
  100. const quill = new Quill('#editor', { theme: 'snow' });
  101. quill.on(Quill.events.TEXT_CHANGE, update);
  102. const playground = document.querySelector('#playground');
  103. update();
  104. function formatDelta(delta) {
  105. return \`<div>\${JSON.stringify(delta.ops, null, 2)}</div>\`;
  106. }
  107. function update(delta) {
  108. const contents = quill.getContents();
  109. let html = \`<h3>contents</h3>\${formatDelta(contents)}\`
  110. if (delta) {
  111. html = \`\${html}<h3>change</h3>\${formatDelta(delta)}\`;
  112. }
  113. playground.innerHTML = html;
  114. }
  115. `
  116. }}
  117. />