X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/61343b82c4f0da4c68e4c6373daafff4a81efdd1..138998bbd8f7a1ac38b2f1eacbdf7cd522be4b13:/wp-admin/js/editor.js
diff --git a/wp-admin/js/editor.js b/wp-admin/js/editor.js
index e54e836c..8602b140 100644
--- a/wp-admin/js/editor.js
+++ b/wp-admin/js/editor.js
@@ -1,245 +1,370 @@
-var switchEditors = {
-
- switchto: function(el) {
- var aid = el.id, l = aid.length, id = aid.substr(0, l - 5), mode = aid.substr(l - 4);
+( function( $ ) {
+ function SwitchEditors() {
+ var tinymce, $$,
+ exports = {};
+
+ function init() {
+ if ( ! tinymce && window.tinymce ) {
+ tinymce = window.tinymce;
+ $$ = tinymce.$;
+
+ $$( document ).on( 'click', function( event ) {
+ var id, mode,
+ target = $$( event.target );
+
+ if ( target.hasClass( 'wp-switch-editor' ) ) {
+ id = target.attr( 'data-wp-editor-id' );
+ mode = target.hasClass( 'switch-tmce' ) ? 'tmce' : 'html';
+ switchEditor( id, mode );
+ }
+ });
+ }
+ }
- this.go(id, mode);
- },
+ function getToolbarHeight( editor ) {
+ var node = $$( '.mce-toolbar-grp', editor.getContainer() )[0],
+ height = node && node.clientHeight;
- go: function(id, mode) { // mode can be 'html', 'tmce', or 'toggle'; 'html' is used for the "Text" editor tab.
- id = id || 'content';
- mode = mode || 'toggle';
+ if ( height && height > 10 && height < 200 ) {
+ return parseInt( height, 10 );
+ }
- var t = this, ed = tinyMCE.get(id), wrap_id, txtarea_el, dom = tinymce.DOM;
+ return 30;
+ }
- wrap_id = 'wp-'+id+'-wrap';
- txtarea_el = dom.get(id);
+ function switchEditor( id, mode ) {
+ id = id || 'content';
+ mode = mode || 'toggle';
+
+ var editorHeight, toolbarHeight, iframe,
+ editor = tinymce.get( id ),
+ wrap = $$( '#wp-' + id + '-wrap' ),
+ $textarea = $$( '#' + id ),
+ textarea = $textarea[0];
+
+ if ( 'toggle' === mode ) {
+ if ( editor && ! editor.isHidden() ) {
+ mode = 'html';
+ } else {
+ mode = 'tmce';
+ }
+ }
- if ( 'toggle' == mode ) {
- if ( ed && !ed.isHidden() )
- mode = 'html';
- else
- mode = 'tmce';
+ if ( 'tmce' === mode || 'tinymce' === mode ) {
+ if ( editor && ! editor.isHidden() ) {
+ return false;
+ }
+
+ if ( typeof( window.QTags ) !== 'undefined' ) {
+ window.QTags.closeAllTags( id );
+ }
+
+ editorHeight = parseInt( textarea.style.height, 10 ) || 0;
+
+ if ( editor ) {
+ editor.show();
+
+ // No point resizing the iframe in iOS
+ if ( ! tinymce.Env.iOS && editorHeight ) {
+ toolbarHeight = getToolbarHeight( editor );
+ editorHeight = editorHeight - toolbarHeight + 14;
+
+ // height cannot be under 50 or over 5000
+ if ( editorHeight > 50 && editorHeight < 5000 ) {
+ editor.theme.resizeTo( null, editorHeight );
+ }
+ }
+ } else {
+ tinymce.init( window.tinyMCEPreInit.mceInit[id] );
+ }
+
+ wrap.removeClass( 'html-active' ).addClass( 'tmce-active' );
+ $textarea.attr( 'aria-hidden', true );
+ window.setUserSetting( 'editor', 'tinymce' );
+
+ } else if ( 'html' === mode ) {
+ if ( editor && editor.isHidden() ) {
+ return false;
+ }
+
+ if ( editor ) {
+ if ( ! tinymce.Env.iOS ) {
+ iframe = editor.iframeElement;
+ editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0;
+
+ if ( editorHeight ) {
+ toolbarHeight = getToolbarHeight( editor );
+ editorHeight = editorHeight + toolbarHeight - 14;
+
+ // height cannot be under 50 or over 5000
+ if ( editorHeight > 50 && editorHeight < 5000 ) {
+ textarea.style.height = editorHeight + 'px';
+ }
+ }
+ }
+
+ editor.hide();
+ } else {
+ // The TinyMCE instance doesn't exist, show the textarea
+ $textarea.css({ 'display': '', 'visibility': '' });
+ }
+
+ wrap.removeClass( 'tmce-active' ).addClass( 'html-active' );
+ $textarea.attr( 'aria-hidden', false );
+ window.setUserSetting( 'editor', 'html' );
+ }
}
- if ( 'tmce' == mode || 'tinymce' == mode ) {
- if ( ed && ! ed.isHidden() )
- return false;
+ // Replace paragraphs with double line breaks
+ function removep( html ) {
+ var blocklist = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset',
+ blocklist1 = blocklist + '|div|p',
+ blocklist2 = blocklist + '|pre',
+ preserve_linebreaks = false,
+ preserve_br = false;
+
+ if ( ! html ) {
+ return '';
+ }
+
+ // Protect pre|script tags
+ if ( html.indexOf( '
]*>[\s\S]+?<\/\1>/g, function( a ) {
+ a = a.replace( /
(\r\n|\n)?/g, '' );
+ a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '' );
+ return a.replace( /\r?\n/g, '' );
+ });
+ }
+
+ // keep
tags inside captions and remove line breaks
+ if ( html.indexOf( '[caption' ) !== -1 ) {
+ preserve_br = true;
+ html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
+ return a.replace( /
]*)>/g, '' ).replace( /[\r\n\t]+/, '' );
+ });
+ }
+
+ // Pretty it up for the source editor
+ html = html.replace( new RegExp( '\\s*(' + blocklist1 + ')>\\s*', 'g' ), '$1>\n' );
+ html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' );
+
+ // Mark if it has any attributes.
+ html = html.replace( /(]+>.*?)<\/p>/g, '$1
' );
- if ( typeof(QTags) != 'undefined' )
- QTags.closeAllTags(id);
+ // Separate containing
+ html = html.replace( /
]*)?>\s*
/gi, '
\n\n' );
- if ( tinyMCEPreInit.mceInit[id] && tinyMCEPreInit.mceInit[id].wpautop )
- txtarea_el.value = t.wpautop( txtarea_el.value );
+ // Remove
and
+ html = html.replace( /\s*
/gi, '' );
+ html = html.replace( /\s*<\/p>\s*/gi, '\n\n' );
+ html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' );
+ html = html.replace( /\s*
\s*/gi, '\n' );
- if ( ed ) {
- ed.show();
- } else {
- ed = new tinymce.Editor(id, tinyMCEPreInit.mceInit[id]);
- ed.render();
+ // Fix some block element newline issues
+ html = html.replace( /\s*
\s*/g, '
\n' );
+ html = html.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' );
+ html = html.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' );
+
+ html = html.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' );
+ html = html.replace( new RegExp('\\s*(' + blocklist2 + ')>\\s*', 'g' ), '$1>\n' );
+ html = html.replace( /
]*)>/g, '\t' );
+
+ if ( html.indexOf( '