LostCore
2/5/2015 - 12:50 PM

Some TinyMCE helper functions for Wordpress.

Some TinyMCE helper functions for Wordpress.

jQuery.noConflict()(function($) {
  "use strict";
  
  /**
   * Checks if tmce is active
   */
  function is_tinymce_active() {
      /*if (!_.isNull(tinyMCE.activeEditor)) { return true; } return false;*/
      if (typeof tinyMCE !== "undefined" && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden()) {
          return true;
      }
      return false;
  }
  
  /**
   * Get the focus of the editor
   */
  function get_tinymce_focus(){
    window.switchEditors.go('content', 'tmce'); //WP only (content is the id of the editor)
    /*window.tinymce.get("content").focus();
    window.tinymce.activeEditor.focus();*/
    window.tinymce.execCommand('mceFocus',false,'content');
  }
  
  /**
   * Returns the editor content
   */
  function get_tinymce_content() {
    if (is_tinymce_active()) {
        return tinyMCE.activeEditor.getContent({
            format: 'raw'
        });
    } else {
        return jQuery('#content').val();
    }
  }

  /**
   * Set the editor content
   */
  function set_tinymce_content(content) {
      if (is_tinymce_active()) {
          //tinyMCE.activeEditor.setContent(content, {format: 'raw'});
          tinyMCE.get('content').setContent(content, {
              format: 'raw'
          });
          jQuery('#content').val(content);
      } else {
          jQuery('#content').val(content);
      }
  }
});