views:

516

answers:

3

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?

+4  A: 

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

if (tinyMCE.activeEditor != null && tinyMCE.activeEditor.isHidden() == false) {
// Run this TinyMCE function 
} else {
// Use your custom Javascript to manipulate text within the HTML editor
}

The trick is that tinyMCE.activeEditor returns null when TinyMCE isn't the 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.

Daniel Bachhuber
Thaks. This worked well. :)
Haris
A: 

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();
Tici
A: 

Cool !!!

I have looking for that isHidden() for hours because was refusing to insert text when toggling between HTML/Editor...

function insertHtml(myValue) { if (window.tinyMCE.activeEditor != null && window.tinyMCE.activeEditor.isHidden() == false) { tinyMCE.activeEditor.focus(); tinyMCE.execCommand("mceInsertContent", false, myValue); } else { insertAtCursor(myValue); } window.close(); }

TheBigBoss