3

切换工具栏按钮状态的最简单方法是什么(就像它与默认粗体按钮一起使用)?我无法“获得”将按钮外观从默认更改为选中的 Tinymce 类。这是我的插件代码(简化):

tinymce.PluginManager.add('myplugin', function (editor) {
    editor.addButton('mybutton', {
        text: false,
        image: 'someimage.png',

        onclick: function () {
            /* Toggle this toolbar button state to selected (like with the tinymce bold-button)*/
            /* and of course some other code goes here */
        }
    });
});
4

4 回答 4

6

在 TinyMCE 4 中,您可以使用更简单的 stateSelector 设置:

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)
});

或者您可以使用“nodechange”事件使用自定义逻辑

editor.addButton('SomeButton', {
    text: 'My button',
    onPostRender: function() {
        var ctrl = this;

        ed.on('NodeChange', function(e) {
            ctrl.active(e.element.nodeName == 'A');
        });
    }
});

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

于 2016-04-07T20:28:38.100 回答
5

万一其他人发现这篇文章解决了这个问题——我发现了一种更简单的方法,使用 4.0.16 中的 onclick:

/* 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"
});
于 2014-02-26T18:02:30.793 回答
4

经过一番摆弄后,我得出结论,onclick 不会在这里完成这项工作。几个小时后,我最终得到了这个解决方案,它在 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"
});
于 2013-11-10T11:26:39.913 回答
0

在您的命令中首先触发 statechange:

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

在按钮 onPostRender 更改按钮状态:

onPostRender: function() {
    var self = this;

    editor.on('FullscreenStateChanged', function(e) {
        self.active(e.state);
    });
}
于 2016-06-19T02:02:39.940 回答