我向CKEditor内联添加了一个自定义插件,以执行粗体操作。插件按预期工作,但按钮的开/关状态不起作用。
执行命令时,其状态始终为TRISTATE_OFF
。
CKEDITOR.plugins.add( 'customBold', {
requires: 'toolbar',
icons: 'bold',
hidpi: false,
init: function( editor ) {
var boldCommand = {
exec: function( editor ) {
document.execCommand('bold', false, null);
}
}
editor.addCommand( 'bold', boldCommand );
editor.ui.addButton && editor.ui.addButton( 'Bold', {
label: 'bold',
command: 'bold',
toolbar: 'basic,10'
});
editor.setKeystroke( [
[ CKEDITOR.CTRL + 66 /*B*/, 'bold' ]
]);
}
});
当用户选择粗体文本时,我想在工具栏中切换粗体样式。
最佳答案
您需要调用command.setState
方法,该方法将设置命令的状态,然后该命令会自动影响相关按钮的状态。
但是,您需要知道何时调用该方法(状态更改时)。像basicstyle插件这样的CKEditor插件使用CKEditor的styles system来让它们easily listen改变样式状态:
editor.attachStyleStateChange( style, function( state ) {
!editor.readOnly && editor.getCommand( commandName ).setState( state );
} );
但是,您尝试使用本机命令,我强烈建议您不要使用。 CKEditor实现自己的样式系统和命令不是巧合。