Rec*_*oil 3 tinymce toolbar button toggle
切换工具栏按钮状态的最简单方法是什么(就像使用默认的粗体按钮一样)?我无法"获得"那个将我从默认变为选中的按钮外观的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 */
}
});
});
Run Code Online (Sandbox Code Playgroud)
小智 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)
});
Run Code Online (Sandbox Code Playgroud)
或者您可以使用"nodechange"事件使用自定义逻辑
editor.addButton('SomeButton', {
text: 'My button',
onPostRender: function() {
var ctrl = this;
ed.on('NodeChange', function(e) {
ctrl.active(e.element.nodeName == 'A');
});
}
});
Run Code Online (Sandbox Code Playgroud)
参考:https: //www.tinymce.com/docs/advanced/migration-guide-from-3.x/#controlstates
小智 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"
});
Run Code Online (Sandbox Code Playgroud)