TinyMCE:初始化后如何绑定事件

问题描述:

我已经搜索了很多但是通过 google-fu 来获取任何结果:(

I already searched a lot but by google-fu'ing don't get me any results :(

我已经有了初始化 tinyMCE 编辑器我无法控制哪个初始化过程,所以下面的代码根本不起作用:

I have an already initialized tinyMCE editor which initialization process I cannot control, so code like the following don't work at all:

tinyMCE.init({
   ...
   setup : function(ed) {
          ed.onChange.add(function(ed, l) {
                  console.debug('Editor contents was modified. Contents: ' + l.content);
          });
   }
});

由于未定义jQuery tinymce插件,因此以下代码无效:

Code like the following doesn't work either since the jQuery tinymce plugin isn't defined:

$('textarea').tinymce({
    setup: function(ed) {
        ed.onClick.add(function(ed, e) {
            alert('Editor was clicked: ' + e.target.nodeName);
        });
    }
});

我的意思是,它必须使用 tinymce.something 语法。

I mean, it has to be using the tinymce.something syntax.

如何在tinyMCE初始化之后将回调函数绑定到任何tinyMCE事件?

在使用console.log()侵入tinymce对象后,我找到了一个可行的解决方案:

After hacking into the tinymce object with console.log(), I found a working solution:

setTimeout(function () {
       for (var i = 0; i < tinymce.editors.length; i++) {
             tinymce.editors[i].onChange.add(function (ed, e) {
             // Update HTML view textarea (that is the one used to send the data to server).
         ed.save();
       });
            }
}, 1000);

在回调函数中,可以设置任何想要的事件绑定。

Inside that callback function one can set the whatever event binding one wants.

setTimeout 调用是为了克服 tinymce 和 jQuery ,因为当调用 tinymce.editors [i] .onChange.add()时,tinymce尚未初始化。

The setTimeout call is to overcome the racing condition of tinymce and jQuery, since when the call to tinymce.editors[i].onChange.add() is made tinymce wasn't initialized yet.