最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to enabledisable custom button on selection change with tinymce - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 7

Just 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);
发布评论

评论列表(0)

  1. 暂无评论