/* global getUserSetting, setUserSetting */
( function( tinymce ) {
// Set the minimum value for the modals z-index higher than #wpadminbar (100000)
if ( ! tinymce.ui.FloatPanel.zIndex || tinymce.ui.FloatPanel.zIndex < 100100 ) {
tinymce.ui.FloatPanel.zIndex = 100100;
}
tinymce.PluginManager.add( 'wordpress', function( editor ) {
var wpAdvButton, style,
DOM = tinymce.DOM,
each = tinymce.each,
__ = editor.editorManager.i18n.translate,
$ = window.jQuery,
wp = window.wp,
hasWpautop = ( wp && wp.editor && wp.editor.autop && editor.getParam( 'wpautop', true ) );
if ( $ ) {
$( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] );
}
function toggleToolbars( state ) {
var initial, toolbars,
pixels = 0,
classicBlockToolbar = tinymce.$( '.block-library-classic__toolbar' );
if ( state === 'hide' ) {
initial = true;
} else if ( classicBlockToolbar.length && ! classicBlockToolbar.hasClass( 'has-advanced-toolbar' ) ) {
// Show the second, third, etc. toolbar rows in the Classic block instance.
classicBlockToolbar.addClass( 'has-advanced-toolbar' );
state = 'show';
}
if ( editor.theme.panel ) {
toolbars = editor.theme.panel.find('.toolbar:not(.menubar)');
}
if ( toolbars && toolbars.length > 1 ) {
if ( ! state && toolbars[1].visible() ) {
state = 'hide';
}
each( toolbars, function( toolbar, i ) {
if ( i > 0 ) {
if ( state === 'hide' ) {
toolbar.hide();
pixels += 30;
} else {
toolbar.show();
pixels -= 30;
}
}
});
}
// Resize editor iframe, not needed for iOS and inline instances.
if ( pixels && ! tinymce.Env.iOS && editor.iframeElement ) {
DOM.setStyle( editor.iframeElement, 'height', editor.iframeElement.clientHeight + pixels );
}
if ( ! initial ) {
if ( state === 'hide' ) {
setUserSetting( 'hidetb', '0' );
wpAdvButton && wpAdvButton.active( false );
} else {
setUserSetting( 'hidetb', '1' );
wpAdvButton && wpAdvButton.active( true );
}
}
editor.fire( 'wp-toolbar-toggle' );
}
// Add the kitchen sink button :)
editor.addButton( 'wp_adv', {
tooltip: 'Toolbar Toggle',
cmd: 'WP_Adv',
onPostRender: function() {
wpAdvButton = this;
wpAdvButton.active( getUserSetting( 'hidetb' ) === '1' );
}
});
// Hide the toolbars after loading
editor.on( 'PostRender', function() {
if ( editor.getParam( 'wordpress_adv_hidden', true ) && getUserSetting( 'hidetb', '0' ) === '0' ) {
toggleToolbars( 'hide' );
} else {
tinymce.$( '.block-library-classic__toolbar' ).addClass( 'has-advanced-toolbar' );
}
});
editor.addCommand( 'WP_Adv', function() {
toggleToolbars();
});
editor.on( 'focus', function() {
window.wpActiveEditor = editor.id;
});
editor.on( 'BeforeSetContent', function( event ) {
var title;
if ( event.content ) {
if ( event.content.indexOf( '/g, function( match, moretext ) {
return '';
});
}
if ( event.content.indexOf( '' ) !== -1 ) {
title = __( 'Page break' );
event.content = event.content.replace( //g,
'
' );
}
if ( event.load && event.format !== 'raw' ) {
if ( hasWpautop ) {
event.content = wp.editor.autop( event.content );
} else {
// Prevent creation of paragraphs out of multiple HTML comments.
event.content = event.content.replace( /-->\s+';
} else if ( image.indexOf( 'data-wp-more="nextpage"' ) !== -1 ) {
string = '';
} else if ( image.indexOf( 'data-wp-preserve' ) !== -1 ) {
if ( match = image.match( / data-wp-preserve="([^"]+)"/ ) ) {
string = decodeURIComponent( match[1] );
}
}
return string || image;
});
}
});
// Display the tag name instead of img in element path
editor.on( 'ResolveName', function( event ) {
var attr;
if ( event.target.nodeName === 'IMG' && ( attr = editor.dom.getAttrib( event.target, 'data-wp-more' ) ) ) {
event.name = attr;
}
});
// Register commands
editor.addCommand( 'WP_More', function( tag ) {
var parent, html, title,
classname = 'wp-more-tag',
dom = editor.dom,
node = editor.selection.getNode(),
rootNode = editor.getBody();
tag = tag || 'more';
classname += ' mce-wp-' + tag;
title = tag === 'more' ? 'Read more...' : 'Next page';
title = __( title );
html = '
';
// Most common case
if ( node === rootNode || ( node.nodeName === 'P' && node.parentNode === rootNode ) ) {
editor.insertContent( html );
return;
}
// Get the top level parent node
parent = dom.getParent( node, function( found ) {
if ( found.parentNode && found.parentNode === rootNode ) {
return true;
}
return false;
}, editor.getBody() );
if ( parent ) {
if ( parent.nodeName === 'P' ) {
parent.appendChild( dom.create( 'p', null, html ).firstChild );
} else {
dom.insertAfter( dom.create( 'p', null, html ), parent );
}
editor.nodeChanged();
}
});
editor.addCommand( 'WP_Code', function() {
editor.formatter.toggle('code');
});
editor.addCommand( 'WP_Page', function() {
editor.execCommand( 'WP_More', 'nextpage' );
});
editor.addCommand( 'WP_Help', function() {
var access = tinymce.Env.mac ? __( 'Ctrl + Alt + letter:' ) : __( 'Shift + Alt + letter:' ),
meta = tinymce.Env.mac ? __( 'Cmd + letter:' ) : __( 'Ctrl + letter:' ),
table1 = [],
table2 = [],
row1 = {},
row2 = {},
i1 = 0,
i2 = 0,
labels = editor.settings.wp_shortcut_labels,
header, html, dialog, $wrap;
if ( ! labels ) {
return;
}
function tr( row, columns ) {
var out = '
' + __( 'To move focus to other buttons use Tab or the arrow keys. To return focus to the editor press Escape or use one of the buttons.' ) + '
'; html += '\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)( [^>]*)?>/gi, '<$1$2>' )
.replace( /<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)>\s*<\/p>/gi, '$1>' );
}
});
if ( $ ) {
$( document ).triggerHandler( 'tinymce-editor-init', [editor] );
}
if ( window.tinyMCEPreInit && window.tinyMCEPreInit.dragDropUpload ) {
dom.bind( doc, 'dragstart dragend dragover drop', function( event ) {
if ( $ ) {
// Trigger the jQuery handlers.
$( document ).trigger( new $.Event( event ) );
}
});
}
if ( editor.getParam( 'wp_paste_filters', true ) ) {
editor.on( 'PastePreProcess', function( event ) {
// Remove trailing
added by WebKit browsers to the clipboard
event.content = event.content.replace( /
/gi, '' );
// In WebKit this is handled by removeWebKitStyles()
if ( ! tinymce.Env.webkit ) {
// Remove all inline styles
event.content = event.content.replace( /(<[^>]+) style="[^"]*"([^>]*>)/gi, '$1$2' );
// Put back the internal styles
event.content = event.content.replace(/(<[^>]+) data-mce-style=([^>]+>)/gi, '$1 style=$2' );
}
});
editor.on( 'PastePostProcess', function( event ) {
// Remove empty paragraphs
editor.$( 'p', event.node ).each( function( i, node ) {
if ( dom.isEmpty( node ) ) {
dom.remove( node );
}
});
if ( tinymce.isIE ) {
editor.$( 'a', event.node ).find( 'font, u' ).each( function( i, node ) {
dom.remove( node, true );
});
}
});
}
if ( editor.settings.wp_shortcut_labels && editor.theme.panel ) {
var labels = {};
var access = 'Shift+Alt+';
var meta = 'Ctrl+';
// For Mac: ctrl = \u2303, cmd = \u2318, alt = \u2325
if ( tinymce.Env.mac ) {
access = '\u2303\u2325';
meta = '\u2318';
}
each( editor.settings.wp_shortcut_labels, function( value, name ) {
labels[ name ] = value.replace( 'access', access ).replace( 'meta', meta );
} );
each( editor.theme.panel.find('button'), function( button ) {
if ( button && button.settings.tooltip && labels.hasOwnProperty( button.settings.tooltip ) ) {
// Need to translate now. We are changing the string so it won't match and cannot be translated later.
button.settings.tooltip = editor.translate( button.settings.tooltip ) + ' (' + labels[ button.settings.tooltip ] + ')';
}
} );
// listbox for the "blocks" drop-down
each( editor.theme.panel.find('listbox'), function( listbox ) {
if ( listbox && listbox.settings.text === 'Paragraph' ) {
each( listbox.settings.values, function( item ) {
if ( item.text && labels.hasOwnProperty( item.text ) ) {
item.shortcut = '(' + labels[ item.text ] + ')';
}
} );
}
} );
}
});
editor.on( 'SaveContent', function( event ) {
// If editor is hidden, we just want the textarea's value to be saved
if ( ! editor.inline && editor.isHidden() ) {
event.content = event.element.value;
return;
}
// Keep empty paragraphs :(
event.content = event.content.replace( /
(?:
|\u00a0|\uFEFF| )*<\/p>/g, '
' ); if ( hasWpautop ) { event.content = wp.editor.removep( event.content ); } else { // Restore formatting of block boundaries. event.content = event.content.replace( /-->\s*\n\n