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

标签 javascript jquery ckeditor

是否有更好的方法来确定何时加载 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/

相关文章:

javascript - 克隆循环问题

javascript - 使用 JavaScript 打开和关闭 CKEditor 4 内联编辑

javascript - 如何在手机间隙打开图库而不是相机?

javascript - 显示:none is preventing click event in jQuery

javascript - jQuery 验证器插件 - 检查 mysql 数据库中的现有用户名/电子邮件

javascript - 监听 ckeditor 小部件事件

ruby-on-rails - 如何将 Wiris 插件添加到 CKEditor?

javascript - 在 Android 中使用谷歌地图调用路线

JavaScript 打印所有使用的 Unicode 字符

JavaScript 选项卡仅在存在单个选项卡时才有效