简体   繁体   English

如何将工具栏按钮添加到自定义的下拉菜单中?

[英]How do I add toolbar buttons to a custom tinymce dropdown menu?

I've created a custom dropdown in tinymce like this: 我在tinymce中创建了一个自定义下拉列表,如下所示:

tinymce.init({
    toolbar: "alignment",

    setup: function(editor) {
        editor.addButton('alignment', {
            type: 'menubutton',
            text: 'Alignment',
            icon: false,
            menu: [
                { text: 'left', onclick: function() {tinymce.activeEditor.formatter.toggle('alignleft');}},
                { text: 'center', onclick: function() {tinymce.activeEditor.formatter.toggle('aligncenter');}},
                { text: 'right', onclick: function() {tinymce.activeEditor.formatter.toggle('alignright');}},
                { text: 'justify', onclick: function() {tinymce.activeEditor.formatter.toggle('alignjustify');}},
            ]
        });

    }

});

which creates this: 这创造了这个:

tinymce下拉

However what I'd like is to just move the alignment buttons from the main toolbar in the dropdown menu. 但是,我想要的是从下拉菜单中的主工具栏中移动对齐按钮。

How do I got about putting these actual buttons from the toolbar, into a dropdown menu? 如何将这些实际按钮从工具栏中放入下拉菜单? Is it like the code above or is aa totally different way? 它是像上面的代码还是完全不同的方式?

对齐按钮 So basically put these buttons in the dropdown above with the toggle states for on and off too. 因此,基本上将这些按钮放在上面的下拉列表中,也可以打开和关闭切换状态。

Try this setup - Plunker 试试这个设置 - Plunker

tinymce.init({
    selector: "textarea",
    toolbar: "styleselect | bold italic | alignment | alignmentv2",
    setup: function(editor) {
      editor.addButton('alignment', {
          type: 'listbox',
          text: 'Alignment',
          icon: false,
          onselect: function(e) {
            tinyMCE.execCommand(this.value());
          },
          values: [
              {icon: 'alignleft', value: 'JustifyLeft'},
              {icon: 'alignright', value: 'JustifyRight'},
              {icon: 'aligncenter', value: 'JustifyCenter'},
              {icon: 'alignjustify', value: 'JustifyFull'},
          ],
          onPostRender: function() {
            // Select the firts item by default
            this.value('JustifyLeft');
          }
      });

      editor.addButton('alignmentv2', {
            type: 'menubutton',
            text: 'Alignment v2',
            icon: false,
            menu: [
                {icon: 'alignleft', onclick: function() { console.log(editor); tinyMCE.execCommand('JustifyLeft'); }},
                {icon: 'alignright', onclick: function() { tinyMCE.execCommand('JustifyRight'); }}
            ]
        });
    }
});

@NoBugs, you can enhance the onselect method to perform the alignment icon update. @NoBugs,您可以增强onselect方法来执行对齐图标更新。

At first, by examining structure of this object in the onselect method we'll see that this.settings.values property stores an array with early defined values. 首先,通过检查onselect方法中this对象的结构,我们将看到this.settings.values属性存储具有早期定义值的数组。

By using one of many find utility functions we get the selected value item and update the icon as needed: 通过使用许多find实用程序函数之一,我们获取所选的值项并根据需要更新图标:

onselect: function() {
  selectedItem = find(this.settings.values, {value: this.value()})
  this.icon(selectedItem.icon)
  tinyMCE.execCommand(this.value());
}

Hope, this helps. 希望这可以帮助。 Cheers! 干杯!

This is probably best solved using a custom split button. 这可能是使用自定义拆分按钮最好解决的。 That way we can assign the last selected option to the main button. 这样我们就可以将最后选择的选项分配给主按钮。

See the result here - CodePen 在此处查看结果 - CodePen

tinymce.init({
  selector: '#editor',
  menubar: false,
  toolbar: 'bold italic underline | alignmentsplit | bullist numlist outdent indent',

  setup: function (editor) {
    editor.on('init', function() {
      this.getDoc().body.style.fontSize = '16px';
      this.getDoc().body.style.fontFamily = 'Georgia';
    });
    editor.addButton('alignmentsplit', {
      type: 'splitbutton',
      text: '',
      icon: 'alignleft',
      onclick: function(e) {
        tinyMCE.execCommand(this.value);
      },
      menu: [{
          icon: 'alignleft',
          text: 'Align Left',
          onclick: function() {
            tinyMCE.execCommand('JustifyLeft');
            this.parent().parent().icon('alignleft');
            this.parent().parent().value = 'JustifyLeft'
          }
        }, {
          icon: 'alignright',
          text: 'Align Right',
          onclick: function() {
            tinyMCE.execCommand('JustifyRight');
            this.parent().parent().icon('alignright');
            this.parent().parent().value = 'JustifyRight';
          }
        }, {
          icon: 'aligncenter',
          text: 'Align Center',
          onclick: function() {
            tinyMCE.execCommand('JustifyCenter');
            this.parent().parent().icon('aligncenter');
            this.parent().parent().value = 'JustifyCenter';
          }
        }, {
          icon: 'alignjustify',
          text: 'Justify',
          onclick: function() {
            tinyMCE.execCommand('JustifyFull');
            this.parent().parent().icon('alignjustify');
            this.parent().parent().value = 'JustifyFull';
          }
        }
      ],
      onPostRender: function() {
        // Select the first item by default
        this.value ='JustifyLeft';
      }
    });
  }
});

Note: If you re-select an alignment option on content that is already aligned that way, TinyMCE toggles the alignment formatting off. 注意:如果在已经以该方式对齐的内容上重新选择对齐选项,则TinyMCE会关闭对齐格式。 This is default TinyMCE behaviour, but you would need to indicate that the section already has that formatting via a toggle state on the button for this to make more sense to the user. 这是默认的TinyMCE行为,但您需要通过按钮上的切换状态指示该部分已经具有该格式,以便对用户更有意义。 This has not been implemented above. 这还没有在上面实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM