TinyMCE 4 toggle toolbar button state

后端 未结 4 1374
轻奢々
轻奢々 2021-01-03 08:39

What is the simplest way to toggle the state of a toolbar button (like it works with the default bold-button)? I can\'t \"get\" to that class i Tinymce that changes the butt

相关标签:
4条回答
  • 2021-01-03 09:17

    First fire statechange in your command:

    editor.fire('FullscreenStateChanged', {state: fullscreenState});
    

    On button onPostRender change button state:

    onPostRender: function() {
        var self = this;
    
        editor.on('FullscreenStateChanged', function(e) {
            self.active(e.state);
        });
    }
    
    0 讨论(0)
  • 2021-01-03 09:21

    After some fiddling I concluded that the onclick won't do the job here. After hours I ended up with this solution which seams to work nicely in Tinymce 4.x:

    /* In the timymce > plugins, name your pluginfolder "my_crazy_plugin" and
    plugin file as "plugin.min.js" */
    
    /* Your plugin file: plugin.min.js */
    tinymce.PluginManager.add('my_crazy_plugin', function(editor) {
       var state;
    
       /* Actions to do on button click */
       function my_action() {
            state = !state; /* Switching state */
            editor.fire('mybutton', {state: state});
    
            if (state){
                alert(state); /* Do your true-stuff here */
            }
            else {
                alert(state); /* Do your false-stuff here */
            }
        }
    
        function toggleState_MyButton() {
            var self = this;
            editor.on('mybutton', function(e) {
                self.active(e.state);
            });
        }
    
        /* Adding the button & command */
        editor.addCommand('cmd_mybutton', my_action);
    
        editor.addButton('mybutton', {
            image: 'tinymce/plugins/my_crazy_plugin/img/some16x16icon.png',
            title: 'That Bubble Help text',
            cmd: 'cmd_mybutton',
            onPostRender: toggleState_MyButton
        });
    });
    
    
    /* Your file with the tinymce init section: */
    tinymce.init({
        plugins: [
            "my_crazy_plugin"
            ],
        toolbar: "mybutton"
    });
    
    0 讨论(0)
  • 2021-01-03 09:25

    In case anyone else finds this article for this issue - I found a bit of a simpler way to do it, using the onclick in 4.0.16:

    /* In the timymce > plugins, name your pluginfolder "my_crazy_plugin" and
    plugin file as "plugin.min.js" */
    
    /* Your plugin file: plugin.min.js */
    tinymce.PluginManager.add('my_crazy_plugin', function(editor) {
       /* Actions to do on button click */
       function my_action() {
            this.active( !this.active() );
            var state = this.active();
    
            if (state){
                alert(state); /* Do your true-stuff here */
            }
            else {
                alert(state); /* Do your false-stuff here */
            }
        }
    
        editor.addButton('mybutton', {
            image: 'tinymce/plugins/my_crazy_plugin/img/some16x16icon.png',
            title: 'That Bubble Help text',
            onclick: my_action
        });
    });
    
    
    /* Your file with the tinymce init section: */
    tinymce.init({
        plugins: [
            "my_crazy_plugin"
            ],
        toolbar: "mybutton"
    });
    
    0 讨论(0)
  • 2021-01-03 09:33

    In TinyMCE 4 you can use the simpler stateSelector setting:

    editor.addButton('SomeButton', {
        text: 'My button',
        stateSelector: '.class-of-node' // or use an element (an id would probably work as well, but I haven't tried it myself)
    });
    

    Or you can use custom logic using the "nodechange" event

    editor.addButton('SomeButton', {
        text: 'My button',
        onPostRender: function() {
            var ctrl = this;
    
            ed.on('NodeChange', function(e) {
                ctrl.active(e.element.nodeName == 'A');
            });
        }
    });
    

    Reference: https://www.tinymce.com/docs/advanced/migration-guide-from-3.x/#controlstates

    0 讨论(0)
提交回复
热议问题