Toolbar Module

工具栏模块允许用户轻松格式化Quill的内容。

它可以配置自定义容器和处理程序。

  1. var quill = new Quill('#editor', {
  2. modules: {
  3. toolbar: {
  4. container: '#toolbar', // Selector for toolbar container
  5. handlers: {
  6. 'bold': customBoldHandler
  7. }
  8. }
  9. }
  10. });

由于container选项很常见,因此也允许使用顶级简写。

  1. var quill = new Quill('#editor', {
  2. modules: {
  3. // Equivalent to { toolbar: { container: '#toolbar' }}
  4. toolbar: '#toolbar'
  5. }
  6. });

Container

工具栏控件可以由简单的格式名称数组或自定义HTML容器指定。

从更简单的数组选项开始:

  1. var toolbarOptions = ['bold', 'italic', 'underline', 'strike'];
  2. var quill = new Quill('#editor', {
  3. modules: {
  4. toolbar: toolbarOptions
  5. }
  6. });

控件也可以按嵌套数组的一个级别进行分组。 这将使用类名称ql-formats将控件包装在<span>中,从而为要使用的主题提供结构。 例如,Snow在控件组之间添加了额外的间距。

  1. var toolbarOptions = [['bold', 'italic'], ['link', 'image']];

可以使用Object指定具有自定义值的按钮,并将格式名称作为其唯一键。

  1. var toolbarOptions = [{ 'header': '3' }];

下拉列表类似地由Object指定,但具有可能值的数组。 CSS用于控制下拉选项的可视标签。

  1. // 注意false,而不是'normal',是正确的值quill.format('size',false)删除格式,允许默认样式工作
  2. var toolbarOptions = [
  3. { size: [ 'small', false, 'large', 'huge' ]}
  4. ];

注意Themes还可以指定下拉列表的默认值。 例如,如果设置为空数组,则Snow会为colorbackground格式提供35种颜色的默认列表。

  1. var toolbarOptions = [
  2. ['bold', 'italic', 'underline', 'strike'], // toggled buttons
  3. ['blockquote', 'code-block'],
  4. [{ 'header': 1 }, { 'header': 2 }], // custom button values
  5. [{ 'list': 'ordered'}, { 'list': 'bullet' }],
  6. [{ 'script': 'sub'}, { 'script': 'super' }], // superscript/subscript
  7. [{ 'indent': '-1'}, { 'indent': '+1' }], // outdent/indent
  8. [{ 'direction': 'rtl' }], // text direction
  9. [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
  10. [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
  11. [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme
  12. [{ 'font': [] }],
  13. [{ 'align': [] }],
  14. ['clean'] // remove formatting button
  15. ];
  16. var quill = new Quill('#editor', {
  17. modules: {
  18. toolbar: toolbarOptions
  19. },
  20. theme: 'snow'
  21. });

对于需要更多自定义的用例,您可以在HTML中手动创建工具栏,并将DOM元素或选择器传递给Quill。ql-toolbar类将添加到工具栏容器中,Quill将适当的处理程序附加到<button><select>元素,其类名称为ql-${format}。Buttons元素可以选择具有自定义value属性。

  1. <!-- Create toolbar container -->
  2. <div id="toolbar">
  3. <!-- Add font size dropdown -->
  4. <select class="ql-size">
  5. <option value="small"></option>
  6. <!-- Note a missing, thus falsy value, is used to reset to default -->
  7. <option selected></option>
  8. <option value="large"></option>
  9. <option value="huge"></option>
  10. </select>
  11. <!-- Add a bold button -->
  12. <button class="ql-bold"></button>
  13. <!-- Add subscript and superscript buttons -->
  14. <button class="ql-script" value="sub"></button>
  15. <button class="ql-script" value="super"></button>
  16. </div>
  17. <div id="editor"></div>
  18. <!-- Initialize editor with toolbar -->
  19. <script>
  20. var quill = new Quill('#editor', {
  21. modules: {
  22. toolbar: '#toolbar'
  23. }
  24. });
  25. </script>

请注意,通过提供您自己的HTML元素,Quill会搜索特定的输入元素,但您自己的输入与Quill无关,仍然可以添加和设置样式并共存。

  1. <div id="toolbar">
  2. <!-- Add buttons as you would before -->
  3. <button class="ql-bold"></button>
  4. <button class="ql-italic"></button>
  5. <!-- But you can also add your own -->
  6. <button id="custom-button"></button>
  7. </div>
  8. <div id="editor"></div>
  9. <script>
  10. var quill = new Quill('#editor', {
  11. modules: {
  12. toolbar: '#toolbar'
  13. }
  14. });
  15. var customButton = document.querySelector('#custom-button');
  16. customButton.addEventListener('click', function() {
  17. console.log('Clicked!');
  18. });
  19. </script>

Handlers

默认情况下,工具栏控件应用并删除格式,但您也可以使用自定义处理程序覆盖它,例如为了显示外部UI。

处理程序函数将绑定到工具栏(因此使用this将引用工具栏实例)并在相应格式处于非活动状态时传递输入的value属性,否则传递false。 添加自定义处理程序将覆盖默认工具栏和主题行为。

  1. var toolbarOptions = {
  2. handlers: {
  3. // handlers object will be merged with default handlers object
  4. 'link': function(value) {
  5. if (value) {
  6. var href = prompt('Enter the URL');
  7. this.quill.format('link', href);
  8. } else {
  9. this.quill.format('link', false);
  10. }
  11. }
  12. }
  13. }
  14. var quill = new Quill('#editor', {
  15. modules: {
  16. toolbar: toolbarOptions
  17. }
  18. });
  19. // Handlers can also be added post initialization
  20. var toolbar = quill.getModule('toolbar');
  21. toolbar.addHandler('image', showImageUI);