Skip to content

Instantly share code, notes, and snippets.

@rahulsprajapati
Created March 2, 2017 18:07
Show Gist options
  • Save rahulsprajapati/b1363acee3864cf344f4fec10d8cbc18 to your computer and use it in GitHub Desktop.
Save rahulsprajapati/b1363acee3864cf344f4fec10d8cbc18 to your computer and use it in GitHub Desktop.
Tinymce Editor for Shortcode UI plugin.
if ( ! function_exists( 'shortcode_enqueue_editor_scripts' ) ) {
/**
* Enqueue our editor area scripts and styles.
*/
function shortcode_enqueue_editor_scripts() {
wp_register_script( 'shortcode_editor_script', get_template_directory_uri() . '/js/admin/editor-script.js', array( 'shortcode-ui' ), false, true );
wp_enqueue_script( 'shortcode_editor_script' );
}
/**
* Enqueue shortcode ui editor script after shortcode UI assets have been enqueued.
*
* @see Shortcode_UI::enqueue();
*/
add_action( 'enqueue_shortcode_ui', 'shortcode_enqueue_editor_scripts' );
}
/**
* Script for wp_enqueue_editor.
*/
(function ( $, wp ) {
'use strict';
// Boxout shortcode inner_content textarea selector.
var boxOutEditor = 'textarea#inner_content';
var editorLoaded = false;
window.ShortCodeTinyMceEditor = {
// Check if boxout editor is initialized or not.
boxOutEditorInitialized: false,
// Function to load TinyMCE Editor to selector.
loadEditor: function ( selector ) {
var _this = this;
if ( $( selector ).length ) {
// Check if editor is already initialized.
if ( _this.boxOutEditorInitialized ) {
// Destroy/Remove the tinyMCE object for the selector before initialized.
tinymce.remove( selector );
// Remove all controls from tinyMCE object for the selector.
tinymce.execCommand('mceRemoveControl', true, selector);
}
// Initialize tinymce editor to selector textarea.
tinyMCE.init({
selector: selector,
skin: 'wordpress',
paste_retain_style_properties: '',
menubar: false,
height : 200,
toolbar: "undo,redo,bold,italic,strikethrough,bullist,numlist,link",
plugins: ['paste', 'wordpress', 'wplink'],
init_instance_callback : function( editor ) {
_this.boxOutEditorInitialized = true;
},
});
return true;
} else {
return false;
}
},
// Function to unload TinyMCE Editor to selector.
unloadEditor: function ( selector ) {
var _this = this;
if ( $( selector ).length ) {
var editorContent = tinymce.activeEditor.getContent();
$( selector )
.text( editorContent )
.trigger( 'input' );
// Remove tinymce editor for selector.
tinymce.remove( selector );
// Remove all controls from tinyMCE object for the selector.
tinymce.execCommand('mceRemoveControl', true, selector);
_this.boxOutEditorInitialized = false;
return true;
} else {
return false;
}
}
};
// Shortcode ui plugin render_edit shortcode action.
wp.shortcake.hooks.addAction( 'shortcode-ui.render_edit', function() {
editorLoaded = ShortCodeTinyMceEditor.loadEditor( boxOutEditor );
} );
// Shortcode ui plugin render_new shortcode action.
wp.shortcake.hooks.addAction( 'shortcode-ui.render_new', function() {
editorLoaded = ShortCodeTinyMceEditor.loadEditor( boxOutEditor );
} );
// Shortcode ui plugin render_destroy shortcode action.
wp.shortcake.hooks.addAction( 'shortcode-ui.render_destroy', function() {
if ( editorLoaded ) {
ShortCodeTinyMceEditor.unloadEditor( boxOutEditor );
}
} );
})( jQuery, window.wp );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment