Skip to content

Commit

Permalink
Merge branch 'clone-wysiwyg'
Browse files Browse the repository at this point in the history
  • Loading branch information
Anh committed Mar 4, 2016
2 parents e057297 + ffec55c commit f8bbf52
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 122 deletions.
5 changes: 4 additions & 1 deletion css/wysiwyg.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.rwmb-field .mceIframeContainer {
background: #fff;
}
}
.rwmb-wysiwyg-clone {
padding-top: 20px;
}
24 changes: 6 additions & 18 deletions inc/fields/wysiwyg.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* WYSIWYG (editor) field class.
*/
Expand All @@ -15,7 +16,8 @@ class RWMB_Wysiwyg_Field extends RWMB_Field
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'rwmb-meta-box-wysiwyg', RWMB_CSS_URL . 'wysiwyg.css', array(), RWMB_VER );
wp_enqueue_style( 'rwmb-wysiwyg', RWMB_CSS_URL . 'wysiwyg.css', array(), RWMB_VER );
wp_enqueue_script( 'rwmb-wysiwyg', RWMB_JS_URL . 'wysiwyg.js', array( 'jquery' ), RWMB_VER, true );
}

/**
Expand Down Expand Up @@ -58,18 +60,12 @@ static function html( $meta, $field )
ob_start();

$field['options']['textarea_name'] = $field['field_name'];
$attributes = self::get_attributes( $field );

// Use new wp_editor() since WP 3.3
wp_editor( $meta, $field['id'], $field['options'] );
wp_editor( $meta, $attributes['id'], $field['options'] );

$editor = ob_get_clean();
if ( $field['clone'] )
{
self::$cloneable_editors[$field['id']] = $editor;
add_action( 'admin_print_footer_scripts', array( __CLASS__, 'footer_scripts' ), 51 );
}

return $editor;
return ob_get_clean();
}

/**
Expand Down Expand Up @@ -107,12 +103,4 @@ static function normalize( $field )

return $field;
}

/**
* Display list of editors' IDs in the footer for clone.
*/
static function footer_scripts()
{
echo '<script>var rwmb_cloneable_editors = ', wp_json_encode( self::$cloneable_editors ), ';</script>';
}
}
104 changes: 1 addition & 103 deletions js/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,116 +158,14 @@ jQuery( function ( $ )
$button.toggle( isNaN( maxClone ) || ( maxClone && numClone < maxClone ) );
}

/**
* Clone WYSIWYG field
* @param $container
* @return void
*/
function cloneWYSIWYG( $container )
{
var $clone_first = $container.find( '.rwmb-clone:first' ),
$clone_last = $container.find( '.rwmb-clone:last' ),
$clone = $( '<div />' ).addClass( 'rwmb-clone' ),
field_name = $clone_last.find( 'textarea.wp-editor-area' ).attr( 'name' ),
field_id = field_name.replace( /\[(\d+)]/, '' );

//Create some global vars
var new_index = 0;
var new_name = field_name.replace( /\[(\d+)]/, function ( match, p1 )
{
new_index = ( parseInt( p1, 10 ) + 1 );
return '[' + new_index + ']';
} );

if ( typeof rwmb_cloneable_editors !== 'undefined' && typeof rwmb_cloneable_editors[field_id] !== 'undefined' )
{
//Get HTML of editor from global object
var cloned_editor = $( rwmb_cloneable_editors[field_id] );

//Fill new clone with html form global object
$clone.append( cloned_editor );

//Add remove button to clone
$clone.append( $clone_last.find( '.remove-clone' ).clone() );

//Add new clone after the last clone
$clone.insertAfter( $clone_last );

//Replace ID of field with new ID
var new_id = cloned_editor.attr( 'id' ).replace( /\[(\d+)]/, '[' + new_index + ']' );
cloned_editor.attr( 'id', new_id );

//Replace all IDs within cloned field
cloned_editor.find( '[id*="' + field_id + '"]' ).each( function ()
{
var id = $( this ).attr( 'id' ).replace( /\[(\d+)]/, '[' + new_index + ']' );
$( this ).attr( 'id', id );
} );

//Get the new textarea element
var textarea = $( cloned_editor ).find( 'textarea.wp-editor-area' );

// Update the "name" attribute
textarea.attr( 'name', new_name );

//Empty the textarea
textarea.html( '' );

//Update editor link, so we can add media to the new editor
cloned_editor.find( '#insert-media-button' ).data( 'editor', new_name );


//Get TinyMCE setting for our fields
var tmceinit = tinyMCEPreInit.mceInit[$clone_first.find( 'textarea.wp-editor-area' ).attr( 'name' )];
var tmceqtinit = tinyMCEPreInit.qtInit[$clone_first.find( 'textarea.wp-editor-area' ).attr( 'name' )];

//Replace id & elements with new created field names
tmceinit.elements = new_name;
tmceqtinit.id = new_name;

//Initialize TinyMCE
try
{
tinymce.init( tmceinit );
}
catch ( e )
{
}
if ( typeof(QTags) === 'function' )
{
try
{
quicktags( tmceqtinit );
}
catch ( e )
{
}
}

// Toggle remove buttons
toggleRemoveButtons( $clone );

//Trigger custom clone event
textarea.trigger( 'clone' );
}
}

$( '#wpbody-content' )
// Add clones
.on( 'click', '.add-clone', function ( e )
{
e.preventDefault();

var $container = $( this ).closest( '.rwmb-input' );

if ( $( this ).closest( '.rwmb-field' ).hasClass( 'rwmb-wysiwyg-wrapper' ) )
{
cloneWYSIWYG( $container );
}
else
{
clone( $container );
}
clone( $container );

toggleRemoveButtons( $container );
toggleAddButton( $container );
Expand Down
100 changes: 100 additions & 0 deletions js/wysiwyg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* global tinymce, quicktags */

jQuery( function ( $ )
{
'use strict';

/**
* Update date picker element
* Used for static & dynamic added elements (when clone)
*/
function update()
{
var $this = $( this ),
$wrapper = $this.closest( '.wp-editor-wrap' ),
id = $this.attr( 'id' );

// Ignore existing editor.
if ( tinyMCEPreInit.mceInit[id] )
{
return;
}

// Get id of the original editor to get its tinyMCE and quick tags settings
var originalId = getOriginalId( $this );
if ( !originalId )
{
return;
}

// Update the DOM
$this.show();
updateDom( $wrapper, id );

// TinyMCE
var settings = tinyMCEPreInit.mceInit[originalId];
settings.selector = '#' + id;
tinymce.init( settings );

// Quick tags
if ( typeof quicktags === 'function' && tinyMCEPreInit.qtInit.hasOwnProperty( originalId ) )
{
var qtSettings = tinyMCEPreInit.qtInit[originalId];
qtSettings.id = id;
quicktags( qtSettings );
QTags._buttonsInit();
}
}

/**
* Get original ID of the textarea
* The ID will be used to reference to tinyMCE and quick tags settings
* @param $el Current cloned textarea
*/
function getOriginalId( $el )
{
var $clones = $el.closest( '.rwmb-clone' ).siblings( '.rwmb-clone' ),
id = '';
$clones.each( function ()
{
var currentId = $( this ).find( '.rwmb-wysiwyg' ).attr( 'id' );
if ( tinyMCEPreInit.mceInit.hasOwnProperty( currentId ) )
{
id = currentId;
return false; // Immediately stop the .each() loop
}
} );
return id;
}

/**
* Update id, class, [data-] attributes, ... of the cloned editor.
* @param $wrapper Editor wrapper element
* @param id Editor ID
*/
function updateDom( $wrapper, id )
{
// Wrapper div and media buttons
$wrapper.attr( 'id', 'wp-' + id + '-wrap' )
.removeClass( 'html-active' ).addClass( 'mce-active' ) // Active the visual mode by default
.find( '.mce-container' ).remove().end() // Remove rendered tinyMCE editor
.find( '.wp-editor-tools' ).attr( 'id', 'wp-' + id + '-editor-tools' )
.find( '.wp-media-buttons' ).attr( 'id', 'wp-' + id + '-media-buttons' )
.find( 'button' ).data( 'editor', id ).attr( 'data-editor', id );

// Editor tabs
$wrapper.find( '.switch-tmce' )
.attr( 'id', id + 'tmce' )
.data( 'wp-editor-id', id ).attr( 'data-wp-editor-id', id ).end()
.find( '.switch-html' )
.attr( 'id', id + 'html' )
.data( 'wp-editor-id', id ).attr( 'data-wp-editor-id', id );

// Quick tags
$wrapper.find( '.wp-editor-container' ).attr( 'id', 'wp-' + id + '-editor-container' )
.find( '.quicktags-toolbar' ).attr( 'id', 'qt_' + id + '_toolbar' ).html( '' );
}

$( ':input.rwmb-date' ).each( update );
$( '.rwmb-input' ).on( 'clone', ':input.rwmb-wysiwyg', update );
} );
Loading

0 comments on commit f8bbf52

Please sign in to comment.