切换CKEditor插件按钮的状态

ben*_*fer 7 javascript ckeditor

根据选择切换ckeditor插件菜单按钮状态的正确方法是什么?

例如,在链接/取消链接插件中,我只想在光标位于链接中时启用取消链接.

editor.addCommand("unlink", {
    exec: function (editor) {
         //do something here
    },
    refresh: function (editor, path) {
       // never seems to get fired. Is this even the right hook?
    }
});

editor.ui.addButton("Unlink", {
    label: "Unlink",
    command: "unlink"
});
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

ole*_*leq 3

有一些CKEDITOR.commandDefinition#contextSensitive属性可以控制特定上下文中命令的状态。

例如,取消链接按钮的实际实现如下所示:

CKEDITOR.unlinkCommand.prototype = {
    exec: function( editor ) {
        ...
    },

    refresh: function( editor, path ) {     
        var element = path.lastElement && path.lastElement.getAscendant( 'a', true );

        if ( element && element.getName() == 'a' && element.getAttribute( 'href' ) && element.getChildCount() )
            this.setState( CKEDITOR.TRISTATE_OFF );
        else
            this.setState( CKEDITOR.TRISTATE_DISABLED );
    },

    contextSensitive: 1,
    ...
};
Run Code Online (Sandbox Code Playgroud)