gpt4 book ai didi

javascript - CKEDITOR 脚本何时加载并准备好使用?

转载 作者:数据小太阳 更新时间:2023-10-29 04:53:08 30 4
gpt4 key购买 nike

是否有更好的方法来确定何时加载 CKEDITOR 以及某些功能可用?我在下面包含了我们当前正在使用的代码,但它依赖于 setTimeouts 和计数器来经常检查 CKEDITOR 是否准备就绪。有时代码到达第一个 if 语句并且 CKEDITOR 可用(不为空)但事件处理尚未加载,因此 CKEDITOR.on 不是函数。我本质上是在寻找一种依赖于事件处理(可能是 CKEDITOR 在准备就绪时触发的事件?)而不是 setTimeouts 的解决方案。

TL;DR:CKEDITOR.on 还不能作为函数使用

只有某些页面需要 CKEDITOR,因此它会根据页面内容按需加载。

版本:使用 CKEDITOR 4.8

  Main.Script.getScript(javascript('external/ckeditor/ckeditor.js'), function() {
var count = 0,
init = function() {
count++;
if (CKEDITOR && CKEDITOR.on) {
/*
According to https://docs.ckeditor.com/ckeditor4/latest/api/CKEDITOR_editor.html#event-loaded,
the "loaded" event is fired when editor components (configuration, languages, and plugins)
are fully loaded and initialized.
*/
CKEDITOR.on('loaded', function() {
// Trigger _ckeditorReady event for custom event handling (CKEDITOR.on('instanceReady'), etc.)
$(window).trigger('_ckeditorReady');
});
} else {
if (count < 50) {
$(script).ready(function() {
setTimeout(init, 20);
});
}
}
};
$(script).ready(function() {
setTimeout(init, 20);
});
});

最佳答案

instanceReady事件,在加载编辑器并准备好进行交互时触发。使用方法示例:

CKEDITOR.replace( 'editor', {
on: {
'instanceReady': function( evt ) {
var editor = evt.editor;
console.log( 'Hello world' );
console.log( JSON.stringify( CKEDITOR.tools.objectKeys( editor.plugins ) ) );
}
}
} );

实现同一事件的另一种方法:

var myeditor = CKEDITOR.replace( 'editor' );

myeditor.on( 'instanceReady', function( evt ) {
var editor = evt.editor;
console.log( 'Hello world' );
console.log( JSON.stringify( CKEDITOR.tools.objectKeys( editor.plugins ) ) );
} );

来源:https://codepen.io/msamsel/pen/LQBLxw?editors=1111

关于javascript - CKEDITOR 脚本何时加载并准备好使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48934472/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com