我正在开发一个 CKEditor 插件,用于注释文本和添加边距注释,但我希望仅当用户已经选择了一系列文本时才启用一些自定义工具栏按钮。每当用户打字或光标位于单个点(而不是范围)时,按钮(及其关联的命令)都应该被禁用。
我是一位经验丰富的插件作者,我花了相当多的时间寻找核心API文档,但我还没有找到任何看起来有帮助的东西。
你的情况有点棘手,因为选择更改事件没有很好地跨浏览器实现,FF是主要问题。
在您的情况下,您将需要非常频繁地检查选择更改,因为您对所有选择更改感兴趣,因此 CKEditorselectionChange不适合它。
幸运的是有一个selectionCheck
编辑器中的事件触发频率更高,并且是为 FF 实现的。
解决方案:
在这里你有init
我模拟了一个插件的方法来解决你的问题。它将禁用/启用Source按照你解释的方式按钮。
我已经在这个功能中添加了节流功能,这样机器较小的客户就可以欣赏你的功能了:)
init: function( editor ) {
// Funciton depending on editor selection (taken from the scope) will set the state of our command.
function RefreshState() {
var editable = editor.editable(),
// Command that we want to control.
command = editor.getCommand( 'source' ),
range,
commandState;
if ( !editable ) {
// It might be a case that editable is not yet ready.
return;
}
// We assume only one range.
range = editable.getDocument().getSelection().getRanges()[ 0 ];
// The state we're about to set for the command.
commandState = ( range && !range.collapsed ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
command.setState( commandState );
}
// We'll use throttled function calls, because this event can be fired very, very frequently.
var throttledFunction = CKEDITOR.tools.eventsBuffer( 250, RefreshState );
// Now this is the event that detects all the selection changes.
editor.on( 'selectionCheck', throttledFunction.input );
// You'll most likely also want to execute this function as soon as editor is ready.
editor.on( 'instanceReady', function( evt ) {
// Also do state refresh on instanceReady.
RefreshState();
} );
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)