customization.mdx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ---
  2. title: Customization
  3. ---
  4. Quill was designed with customization and extension in mind. This is achieved by implementing a small editor core exposed by a granular, well defined [API](/docs/api). The core is augmented by [modules](/docs/modules), using the same [APIs](/docs/api) you have access to.
  5. In general, common customizations are handled in [configurations](#configurations/), user interfaces by [Themes](#themes) and CSS, functionality by [modules](#modules), and editor contents by [Parchment](#content-and-formatting).
  6. ### Configurations
  7. Quill favors Code over Configuration™, but for very common needs, especially where the equivalent code would be lengthy or complex, Quill provides [configuration](/docs/configuration/) options. This would be a good first place to look to determine if you even need to implement any custom functionality.
  8. Two of the most powerful options is `modules` and `theme`. You can drastically change or expand what Quill can and does do by simply adding or removing individual [modules](/docs/modules/) or using a different [theme](/docs/themes/).
  9. ### Themes
  10. Quill officially supports a standard toolbar theme [Snow](/docs/themes/#snow) and a floating tooltip theme [Bubble](/docs/themes/#bubble). Since Quill is not confined within an iframe like many legacy editors, many visual modifications can be made with just CSS, using one of the existing themes.
  11. If you would like to drastically change UI interactions, you can omit the `theme` configuration option, which will give you an unstyled Quill editor. You do still need to include a minimal stylesheet that, for example, makes sure spaces render in all browsers and ordered lists are appropriately numbered.
  12. ```html
  13. <link rel="stylesheet" href="{{site.cdn}}/quill.core.css" />
  14. ```
  15. From there you can implement and attach your own UI elements like custom dropdowns or tooltips. The last section of [Cloning Medium with Parchment](/guides/cloning-medium-with-parchment/#final-polish) provides an example of this in action.
  16. ### Modules
  17. Quill is designed with a modular architecture composed of a small editing core, surrounded by modules that augment its functionality. Some of this functionality is quite integral to editing, such as the [History](/docs/modules/history/) module, which manages undo and redo. Because all modules use the same public [API](/docs/api) exposed to the developer, even interchanging core modules is possible, when necessary.
  18. Like Quill's core itself, many [modules](/docs/modules/) expose additional configuration options and APIs. Before deciding to replace a module, take a look at its documentation. Often your desired customization is already implemented as a configuration or API call.
  19. Otherwise, if you would like to drastically change functionality an existing module already covers, you can simply not include it&mdash;or explicitly exclude it when a theme includes it by default&mdash;and implement the functionality to your liking external to Quill, using the same [API](/docs/api) the default module uses.
  20. <SandpackWithQuillTemplate
  21. files={{
  22. 'index.js': `
  23. const quill = new Quill('#editor', {
  24. modules: {
  25. toolbar: false // Snow includes toolbar by default
  26. },
  27. theme: 'snow'
  28. });
  29. `,
  30. }}
  31. />
  32. A few modules&mdash;[Clipboard](/docs/modules/clipboard/), [Keyboard](/docs/modules/keyboard/), and [History](/docs/modules/history/)&mdash;need to be included as core functionality depend on the APIs they provide. For example, even though undo and redo is basic, expected, editor functionality, the native browser behavior handling of this is inconsistent and unpredictable. The History module bridges the gap by implementing its own undo manager and exposing `undo()` and `redo()` as APIs.
  33. Nevertheless, staying true to Quill modular design, you can still drastically change the way undo and redo&mdash;or any other core functionality&mdash;works by implementing your own undo manager to replace the History module. As long as you implement the same API interface, Quill will happily use your replacement module. This is most easily done by inheriting from the existing module, and overwriting the methods you want to change. Take a look at the [modules](/docs/modules/) documentation for a very simple example of overwriting the core [Clipboard](/docs/modules/clipboard/) module.
  34. Finally, you may want to add functionality not provided by existing modules. In this case, it may be convenient to organize this as a Quill module, which the [Building A Custom Module](/guides/building-a-custom-module/) guide covers. Of course, it is certainly valid to just keep this logic separate from Quill, in your application code instead.
  35. ### Content and Formatting
  36. Quill allows modification and extension of the contents and formats that it understands through its document model [Parchment](https://github.com/quilljs/parchment/). Content and formats are represented in Parchment as either Blots or Attributors, which roughly correspond to Nodes or Attributes in the DOM.
  37. #### Class vs Inline
  38. Quill uses classes, instead of inline style attributes, when possible, but both are implemented for you to pick and choose. A live example of this is implemented as a [Playground snippet](/playground/#class-vs-inline-style).
  39. ```js
  40. const ColorClass = Quill.import('attributors/class/color');
  41. const SizeStyle = Quill.import('attributors/style/size');
  42. Quill.register(ColorClass, true);
  43. Quill.register(SizeStyle, true);
  44. // Initialize as you would normally
  45. const quill = new Quill('#editor', {
  46. modules: {
  47. toolbar: true,
  48. },
  49. theme: 'snow',
  50. });
  51. ```
  52. #### Customizing Attributors
  53. In addition to choosing the particular Attributor, you can also customize existing ones. Here is an example of the font whitelist to add additional fonts.
  54. ```js
  55. const FontAttributor = Quill.import('attributors/class/font');
  56. FontAttributor.whitelist = [
  57. 'sofia',
  58. 'slabo',
  59. 'roboto',
  60. 'inconsolata',
  61. 'ubuntu',
  62. ];
  63. Quill.register(FontAttributor, true);
  64. ```
  65. Note you still need to add styling for these classes into your CSS files.
  66. ```html
  67. <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" />
  68. <style>
  69. .ql-font-roboto {
  70. font-family: 'Roboto', sans-serif;
  71. }
  72. </style>
  73. ```
  74. #### Customizing Blots
  75. Formats represented by Blots can also be customized. Here is how you would change the DOM Node used to represent bold formatting.
  76. <SandpackWithQuillTemplate
  77. files={{
  78. 'index.js': `
  79. const Bold = Quill.import('formats/bold');
  80. Bold.tagName = 'B'; // Quill uses <strong> by default
  81. Quill.register(Bold, true);
  82. // Initialize as you would normally
  83. const quill = new Quill('#editor', {
  84. modules: {
  85. toolbar: true
  86. },
  87. theme: 'snow'
  88. });
  89. const Delta = Quill.import('delta');
  90. quill.setContents(
  91. new Delta()
  92. .insert('Rendered with <b>!', { bold: true })
  93. .insert('\\n')
  94. );
  95. `
  96. }}
  97. />
  98. #### Extending Blots
  99. You can also extend existing formats. Here is a quick ES6 implementation of a list item that does not permit formatting its contents. Code blocks are implemented in exactly this way.
  100. ```js
  101. const ListItem = Quill.import('formats/list/item');
  102. class PlainListItem extends ListItem {
  103. formatAt(index, length, name, value) {
  104. if (name === 'list') {
  105. // Allow changing or removing list format
  106. super.formatAt(name, value);
  107. }
  108. // Otherwise ignore
  109. }
  110. }
  111. Quill.register(PlainListItem, true);
  112. // Initialize as you would normally
  113. const quill = new Quill('#editor', {
  114. modules: {
  115. toolbar: true,
  116. },
  117. theme: 'snow',
  118. });
  119. ```
  120. You can view a list of Blots and Attributors available by calling `console.log(Quill.imports);`. Direct modification of this object is not supported. Use [`Quill.register`](/docs/api/#register) instead.
  121. A complete reference on Parchment, Blots and Attributors can be found on Parchment's own [README](https://github.com/quilljs/parchment/). For an in-depth walkthrough, take a look at [Cloning Medium with Parchment](/guides/cloning-medium-with-parchment/), which starts with Quill understanding just plain text, to adding all of the formats [Medium](https://medium.com/) supports. Most of the time, you will not have to build formats from scratch since most are already implemented in Quill, but it is still useful to understanding how Quill works at this deeper level.