Way to check whether TinyMCE is active in WordPress

こ雲淡風輕ζ 提交于 2021-02-06 15:23:01

问题


I'm working on a plugin that, when TinyMCE is in use as the Visual editor, uses TinyMCE commands to insert text into body content editing area. Currently, it works by just running the command. If it works, then TinyMCE is active and if not, then I have custom JS for working with the HTML editor.

My question, however: is there any way to check whether TinyMCE is active or not instead of just running the command and having it fail when it isn't?


回答1:


And... I've answered the question for myself. The conditional you want to test for is as follows:

is_tinyMCE_active = false;
if (typeof(tinyMCE) != "undefined") {
  if (tinyMCE.activeEditor == null || tinyMCE.activeEditor.isHidden() != false) {
    is_tinyMCE_active = true;
  }
}

The trick is that tinyMCE.activeEditor returns null when TinyMCE isn't activated. You can use the isHidden() method to make sure it isn't executing when you've switched back to HTML editor mode.

This is poorly documented on the TinyMCE website and forums.




回答2:


Yes, I saw that code on wordpress: ABSPATH/wp-includes/js/autosave.js file

// (bool) is rich editor enabled and active
var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();



回答3:


As this questions ranked in search engines. I think I should extend Daniels answers a little bit to support when we have multiple instances of wordpress editor in a single page.

    var is_editor_active = function(editor_id){

        if(typeof tinyMCE == 'undefined'){
            return false;
        }

        if( typeof editor_id == 'undefined' ){
            editor = tinyMCE.activeEditor;
        }else{
            editor = tinyMCE.EditorManager.get(editor_id);
        }

        if(editor == null){
            return false;
        }

         return !editor.isHidden();

    };

Uses

When you only need to check the active editor

if(is_editor_active()){
    // do stuff
}

If an editor with certain id needed to be checked

if(is_editor_active('mycontent'){
    // do stuff
}

I hope this little function will be helpful to others :)




回答4:


According to WordPress editor.js we can easily check that :

    if ( window.wpActiveEditor ) {
       // editor active
    }else {
       // not active
    }


来源:https://stackoverflow.com/questions/1179136/way-to-check-whether-tinymce-is-active-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!