不同的JS语法 - 如何将这些属性添加到变量?

时间:2018-01-02 19:08:26

标签: javascript jquery arrays quill

我有两个相似的js代码。

这个问题与js的关系比图书馆更多。 我使用quilljs库来实现文本编辑器,我可以在加载库之前自定义它的设置。

我已经按照这样的方式配置了库:

var toolbarOptions = [
  [{ 'font': [] }],
  [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
  [{ 'align': [] }],
  ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
  [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
  ['blockquote', 'code-block'],

  [{ 'list': 'ordered'}, { 'list': 'bullet' }],
  [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
  [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
  [ 'link', 'video', 'formula' ], 
  ['clean']                                        // remove formatting button
];

var quill = new Quill('#editor', {
  modules: {
    toolbar: toolbarOptions
  },
  placeholder: 'Compose a post...',
  theme: 'snow',
  'image-tooltip': true,
  'link-tooltip': true
});

$('#contenthidden').val(quill.root.innerHTML);

     );

无论如何,我需要将下面的代码添加到toolbarOptions变量中,然后我必须加载quilljs等等...  但它的内容完全不同。 Javascript智能我无法理解如何将此toolbarOptions变量的内容添加到上面的第一个。

var toolbarOptions = {
  handlers: {
    // handlers object will be merged with default handlers object
    'link': function(value) {
      if (value) {
        var href = prompt('Enter the URL');
        this.quill.format('link', href);
      } else {
        this.quill.format('link', false);
      }
    }
  }
}

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:1)

查看文档看起来省略:

toolbar : { container : containerOptions, handlers : handlerOptions } 

并且只做:

toolbar : containerOptions

只是一种快捷方式,因为container选项非常常见。因此,您必须使用第一个表单来指定处理程序选项。

var toolbarOptions = {
    container: [
      [{ 'font': [] }],
      [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
      [{ 'align': [] }],
      ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
      [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
      ['blockquote', 'code-block'],

      [{ 'list': 'ordered'}, { 'list': 'bullet' }],
      [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
      [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
      [ 'link', 'video', 'formula' ], 
      ['clean']                                        // remove formatting button
    ],
  handlers: {
    // handlers object will be merged with default handlers object
    'link': function(value) {
      if (value) {
        var href = prompt('Enter the URL');
        this.quill.format('link', href);
      } else {
        this.quill.format('link', false);
      }
    }
  }
}