Enable CKEditor toolbar button only with valid text selection?

前端 未结 3 1637
醉梦人生
醉梦人生 2021-01-21 13:04

I\'m working on a CKEditor plugin for annotating text and adding margin comments, but I\'d like some of my custom toolbar buttons to be enabled only when the user has already se

3条回答
  •  [愿得一人]
    2021-01-21 13:26

    If you are working on a plugin, I guess that you are registering commands with the ckeditor.

    In that case, you should provide a refresh method, which will be called by the CKEditor to update the state of the button when needed:

    Defined by the command definition, a function to determine the command state. It will be invoked when the editor has its states or selection changed.

    You can see examples of implementation in several of the plugins developed by the CKEditor team. Here is one taken from the source code of the Link plugin:

    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 );
    }
    

提交回复
热议问题