I use this code to create a custom tinymce button that changes a class of image. It's in the setup block.
ed.addButton('cust_setimgaspreview', {
title : 'Set image as a preview image',
image : 'ikony/previews.png',
onclick : function() {
if(ed.selection.getNode().tagName == 'IMG')
{
ed.selection.getNode().className = 'preview';
} else {
alert('You need to select an image.');
}
}
});
As you can see, I use an "ugly approach" to disable the class change on other elements than image. How can I disable/enable the button in the same way as tinymce does with its default buttons (like edit image or edit link)? I guess I need to somehow catch selection change and then change the button state depending on the selection, but I have no ide how to do that.
I use this code to create a custom tinymce button that changes a class of image. It's in the setup block.
ed.addButton('cust_setimgaspreview', {
title : 'Set image as a preview image',
image : 'ikony/previews.png',
onclick : function() {
if(ed.selection.getNode().tagName == 'IMG')
{
ed.selection.getNode().className = 'preview';
} else {
alert('You need to select an image.');
}
}
});
As you can see, I use an "ugly approach" to disable the class change on other elements than image. How can I disable/enable the button in the same way as tinymce does with its default buttons (like edit image or edit link)? I guess I need to somehow catch selection change and then change the button state depending on the selection, but I have no ide how to do that.
Share Improve this question edited Sep 2, 2016 at 10:34 Amunak asked Jul 24, 2012 at 12:52 AmunakAmunak 4665 silver badges19 bronze badges2 Answers
Reset to default 7Just figured it out - it's quite simple. I've just edited my setup function and added "onNodeChange" handler.
setup : function(ed) {
ed.onNodeChange.add(function(ed, cm, node) {
cm.setDisabled('cust_setimgaspreview', !(node.tagName == 'IMG'))
});
ed.addButton('cust_setimgaspreview', {
title : 'Set image as a preview image',
image : 'ikony/previews.png',
onclick : function() {
ed.selection.getNode().className = 'preview';
}
});
}
You may use the functions provided by the control manager.
You may use i.e.
state = !state;
ed.controlManager.setActive('my_button_id', state);