且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测TinyMCE中的变化?

更新时间:2023-02-26 14:53:50

对于Tinymce 4它适用于我,

For Tinymce 4 it works for me,

        editor.on('keyup', function(e) {
            alert('keyup occured');
            //console.log('init event', e);
            console.log('Editor contents was modified. Contents: ' + editor.getContent());
            check_submit(); //another function calling
        });

请注意,键不会捕获所有案例.例如,menu中的copy/paste/cut,而不是keyboard

Note that keyup won't capture all cases. for example copy/paste/cut from menu and not from keyboard

如果需要,可以使用

editor.on('paste', function(e) {
                    console.debug('paste event');

                });

editor.on('cut', function(e) {
                    console.debug('cut event');

                });

注意 如果从tinymce捕获cutpaste,则可能不应该从keyup处理这些事件.我要做的是仅在键不是cut&的键时保存. paste即:

NOTE if you capture cut and paste from tinymce you should probably not process those event from keyup. What I did is to do save only if the keys are not keys for cut & paste i.e :

 /**
 * MCE keyup callback, update the master model selected attribute
 * 
 * @method tinyMCEKeyup
 */
 tinyMCEKeyUp : function(e) {
        console.log('tinymce:keyup');


         var ctrlDown = false;
         var ctrlKey = 17, vKey = 86, xKey = 88;


         if ((e.ctrlKey) && (e.keyCode === vKey)) {
             console.log('paste from keyboard')
             /* got here. do nothing. let paste event handle this one  */
             return;
         } else if ((e.ctrlKey) && (e.keyCode === xKey)) {
             console.log('cut from keyboard')
             /* got here. do nothing. let paste event handle this one  */
             return;
         }

         this.doSave();

},

,然后从keyup事件中调用此函数.这样一来,您就可以节省两次剪切和剪切动作.粘贴

and call this function from the keyup event. This way you will save yourself do some actions twice on cut & paste

注意,您很快就会发现,menu中发生的任何style changes也会触发这些事件.

NOTE soon you will figure out that any style changes that happens from menu will NOT trigger those event as well..

我仍在寻找解决样式变化的答案.

I'm still looking for an answer to capture style change.