切换工具栏按钮状态的最简单方法是什么(就像使用默认的粗体按钮一样)?我不能"得到"对于那个类我的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 */
}
});
});
答案 0 :(得分: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
答案 1 :(得分: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"
});
答案 2 :(得分: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"
});
答案 3 :(得分:0)
命令中的第一个fire statechange:
editor.fire('FullscreenStateChanged', {state: fullscreenState});
按钮onPostRender更改按钮状态:
onPostRender: function() {
var self = this;
editor.on('FullscreenStateChanged', function(e) {
self.active(e.state);
});
}