/* global getUserSetting, setUserSetting */ ( function( tinymce ) { // Set the minimum value for the modals z-index higher than #wpadminbar (100000) 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 iframe, initial, toolbars, pixels = 0; initial = ( state === 'hide' ); if ( editor.theme.panel ) { toolbars = editor.theme.panel.find('.toolbar:not(.menubar)'); } if ( ! toolbars || toolbars.length < 2 || ( state === 'hide' && ! toolbars[1].visible() ) ) { return; } 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; } } }); if ( pixels && ! initial ) { // Resize iframe, not needed in iOS if ( ! tinymce.Env.iOS ) { iframe = editor.getContentAreaContainer().firstChild; DOM.setStyle( iframe, 'height', iframe.clientHeight + pixels ); } 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' ? true : false ); } }); // Hide the toolbars after loading editor.on( 'PostRender', function() { if ( editor.getParam( 'wordpress_adv_hidden', true ) && getUserSetting( 'hidetb', '0' ) === '0' ) { toggleToolbars( 'hide' ); } }); editor.addCommand( 'WP_Adv', function() { toggleToolbars(); }); editor.on( 'focus', function() { window.wpActiveEditor = editor.id; }); // Replace Read More/Next Page tags with images editor.on( 'BeforeSetContent', function( event ) { var title, paragraph = tinymce.Env.webkit ? '
(?: |\u00a0|\uFEFF|\s)+<\/p>/gi, paragraph );
if ( event.load && event.format !== 'raw' && hasWpautop ) {
event.content = wp.editor.autop( event.content );
}
}
});
// Replace images with tags
editor.on( 'PostProcess', function( e ) {
if ( e.get ) {
e.content = e.content.replace(/]+>/g, function( image ) {
var match, moretext = '';
if ( image.indexOf( 'data-wp-more="more"' ) !== -1 ) {
if ( match = image.match( /data-wp-more-text="([^"]+)"/ ) ) {
moretext = match[1];
}
image = '';
} else if ( image.indexOf( 'data-wp-more="nextpage"' ) !== -1 ) {
image = '';
}
return 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();
tag = tag || 'more';
classname += ' mce-wp-' + tag;
title = tag === 'more' ? 'Read more...' : 'Next page';
title = __( title );
html = '
';
// Most common case
if ( node.nodeName === 'BODY' || ( node.nodeName === 'P' && node.parentNode.nodeName === 'BODY' ) ) {
editor.insertContent( html );
return;
}
// Get the top level parent node
parent = dom.getParent( node, function( found ) {
if ( found.parentNode && found.parentNode.nodeName === 'BODY' ) {
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 = [],
header, html, dialog, $wrap;
each( [
{ c: 'Copy', x: 'Cut' },
{ v: 'Paste', a: 'Select all' },
{ z: 'Undo', y: 'Redo' },
{ b: 'Bold', i: 'Italic' },
{ u: 'Underline', k: 'Insert/edit link' }
], function( row ) {
table1.push( tr( row ) );
} );
each( [
{ 1: 'Heading 1', 2: 'Heading 2' },
{ 3: 'Heading 3', 4: 'Heading 4' },
{ 5: 'Heading 5', 6: 'Heading 6' },
{ l: 'Align left', c: 'Align center' },
{ r: 'Align right', j: 'Justify' },
{ d: 'Strikethrough', q: 'Blockquote' },
{ u: 'Bullet list', o: 'Numbered list' },
{ a: 'Insert/edit link', s: 'Remove link' },
{ m: 'Insert/edit image', t: 'Insert Read More tag' },
{ h: 'Keyboard Shortcuts', x: 'Code' },
{ p: 'Insert Page Break tag', w: 'Distraction-free writing mode' }
], function( row ) {
table2.push( tr( row ) );
} );
function tr( row ) {
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 ) ) { if ( ! tinymce.Env.webkit ) { // In WebKit handled by removeWebKitStyles() editor.on( 'PastePreProcess', function( event ) { // 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 each( dom.select( 'p', event.node ), function( node ) { if ( dom.isEmpty( node ) ) { dom.remove( node ); } }); }); } }); 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 ); } }); editor.on( 'preInit', function() { var validElementsSetting = '@[id|accesskey|class|dir|lang|style|tabindex|' + 'title|contenteditable|draggable|dropzone|hidden|spellcheck|translate],' + // Global attributes. 'i,' + // Don't replace with and with and don't remove them when empty. 'b,' + 'script[src|async|defer|type|charset|crossorigin|integrity]'; // Add support for