Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've created a custom dropdown in tinymce like this:

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 dropdown

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 a a totally different way?

alignment buttons So basically put these buttons in the dropdown above with the toggle states for on and off too.

like image 792
MintDeparture Avatar asked Jan 09 '15 14:01

MintDeparture


People also ask

How do I add custom toolbar buttons to the editor?

Create a shortcut for an action or a series of actions that the user repeatedly does. Create a button for custom behavior. The methods for adding custom toolbar buttons are in the UI Registry part of the editor API editor.ui.registry. The API has four methods for adding toolbar buttons, one for each type of toolbar button:

Can I add a button to TinyMCE inside WordPress?

This page of the codex gives an example for adding a button to TinyMCE inside WordPress. However that codex entry might be a bit dated, so in the event of problems with the code here are some alternative code samples to work from.

What are the toolbar buttons for?

An integral part of the ToolBar control is the buttons you add to it. These can be used to provide easy access to menu commands or, alternately, they can be placed in another area of the user interface of your application to expose commands to your users that are not available in the menu structure.

How to depress or unpress a button visually using TinyMCE?

Then there's that internal TinyMCE method editor.formatter.formatChanged() that will register a callback to be called when current selection is of the specified format. The callback will take in a state as the argument, and we will use it to depress or unpress our button visually.


1 Answers

Try this setup - 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'); }}
            ]
        });
    }
});
like image 172
sbedulin Avatar answered Oct 07 '22 08:10

sbedulin