how-to-customize-quill.mdx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ---
  2. title: How to Customize Quill
  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. ```js
  21. const quill = new Quill('#editor', {
  22. modules: {
  23. toolbar: false // Snow includes toolbar by default
  24. },
  25. theme: 'snow'
  26. });
  27. ```
  28. 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.
  29. 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.
  30. 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.
  31. ### Content and Formatting
  32. 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.
  33. #### Class vs Inline
  34. 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).
  35. ```js
  36. const ColorClass = Quill.import('attributors/class/color');
  37. const SizeStyle = Quill.import('attributors/style/size');
  38. Quill.register(ColorClass, true);
  39. Quill.register(SizeStyle, true);
  40. // Initialize as you would normally
  41. const quill = new Quill('#editor', {
  42. modules: {
  43. toolbar: true
  44. },
  45. theme: 'snow'
  46. });
  47. ```
  48. #### Customizing Attributors
  49. 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.
  50. ```js
  51. const FontAttributor = Quill.import('attributors/class/font');
  52. FontAttributor.whitelist = [
  53. 'sofia', 'slabo', 'roboto', 'inconsolata', 'ubuntu'
  54. ];
  55. Quill.register(FontAttributor, true);
  56. ```
  57. Note you still need to add styling for these classes into your CSS files.
  58. ```html
  59. <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
  60. <style>
  61. .ql-font-roboto {
  62. font-family: 'Roboto', sans-serif;
  63. }
  64. </style>
  65. ```
  66. #### Customizing Blots
  67. Formats represented by Blots can also be customized. Here is how you would change the DOM Node used to represent bold formatting.
  68. ```js
  69. const Bold = Quill.import('formats/bold');
  70. Bold.tagName = 'B'; // Quill uses <strong> by default
  71. Quill.register(Bold, true);
  72. // Initialize as you would normally
  73. const quill = new Quill('#editor', {
  74. modules: {
  75. toolbar: true
  76. },
  77. theme: 'snow'
  78. });
  79. ```
  80. #### Extending Blots
  81. 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.
  82. ```js
  83. const ListItem = Quill.import('formats/list/item');
  84. class PlainListItem extends ListItem {
  85. formatAt(index, length, name, value) {
  86. if (name === 'list') {
  87. // Allow changing or removing list format
  88. super.formatAt(name, value);
  89. }
  90. // Otherwise ignore
  91. }
  92. }
  93. Quill.register(PlainListItem, true);
  94. // Initialize as you would normally
  95. const quill = new Quill('#editor', {
  96. modules: {
  97. toolbar: true
  98. },
  99. theme: 'snow'
  100. });
  101. ```
  102. 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.
  103. 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.