From 795e4a1db5de127bd42d93d9fa29f822610a6113 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 6 Jan 2017 14:13:44 +0000 Subject: [PATCH] Docs: Add documentation for `wp-admin/js/editor.js`. Props terwdan, ireneyoast. Fixes #38933. git-svn-id: https://develop.svn.wordpress.org/trunk@39738 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/js/editor.js | 326 ++++++++++++++++++++++++++++++++++---- 1 file changed, 296 insertions(+), 30 deletions(-) diff --git a/src/wp-admin/js/editor.js b/src/wp-admin/js/editor.js index e3fbaab945..002cf4a11f 100644 --- a/src/wp-admin/js/editor.js +++ b/src/wp-admin/js/editor.js @@ -1,14 +1,45 @@ ( function( $ ) { + /** + * @summary Creates the tinyMCE editors. + * + * Creates the tinyMCE editor and binds all events used for switching + * from visual to text mode. + * + * @since 4.3.0 + * + * @class + */ function SwitchEditors() { var tinymce, $$, exports = {}; + /** + * @summary Initializes the event binding for switching editors. + * + * @since 4.3.0 + * + * @returns {void} + */ function init() { if ( ! tinymce && window.tinymce ) { tinymce = window.tinymce; $$ = tinymce.$; + /** + * @summary Handles onclick events for the editor buttons. + * + * @since 4.3.0 + * + * Handles an onclick event on the document. + * Switches the editor between visual and text, + * if the clicked element has the 'wp-switch-editor' class. + * If the class name is switch-html switches to the HTML editor, + * if the class name is switch-tmce + * switches to the TMCE editor. + * + * @returns {void} + */ $$( document ).on( 'click', function( event ) { var id, mode, target = $$( event.target ); @@ -22,6 +53,16 @@ } } + /** + * @summary Retrieves the height of the toolbar based on the container the + * editor is placed in. + * + * @since 3.9.0 + * + * @param {Object} editor The tinyMCE editor. + * @returns {number} If the height is between 10 and 200 return the height, + * else return 30. + */ function getToolbarHeight( editor ) { var node = $$( '.mce-toolbar-grp', editor.getContainer() )[0], height = node && node.clientHeight; @@ -33,6 +74,20 @@ return 30; } + /** + * @summary Switches the editor between visual and text. + * + * @since 4.3.0 + * + * @memberof switchEditors + * + * @param {string} id The id of the editor you want to change the editor mode for. + * If no id is given, it defaults to content. + * @param {string} mode The mode you want to switch to. + * If an undefined mode is given, it defaults to toggle. + * + * @returns {void} + */ function switchEditor( id, mode ) { id = id || 'content'; mode = mode || 'toggle'; @@ -43,6 +98,7 @@ $textarea = $$( '#' + id ), textarea = $textarea[0]; + // Toggle the mode between visual and textual representation. if ( 'toggle' === mode ) { if ( editor && ! editor.isHidden() ) { mode = 'html'; @@ -51,11 +107,20 @@ } } + + // If the mode is tmce or tinymce, show the editor. if ( 'tmce' === mode || 'tinymce' === mode ) { + + /* + * If the editor isn't hidden we are already in tmce mode + * and we don't need to switch. + * Return false to stop event bubbling. + */ if ( editor && ! editor.isHidden() ) { return false; } + // Close the QuickTags toolbars if they are visible. if ( typeof( window.QTags ) !== 'undefined' ) { window.QTags.closeAllTags( id ); } @@ -65,12 +130,12 @@ if ( editor ) { editor.show(); - // No point resizing the iframe in iOS + // Don't resize the iframe in iOS. if ( ! tinymce.Env.iOS && editorHeight ) { toolbarHeight = getToolbarHeight( editor ); editorHeight = editorHeight - toolbarHeight + 14; - // height cannot be under 50 or over 5000 + // Height must be between 50 and 5000. if ( editorHeight > 50 && editorHeight < 5000 ) { editor.theme.resizeTo( null, editorHeight ); } @@ -83,12 +148,21 @@ $textarea.attr( 'aria-hidden', true ); window.setUserSetting( 'editor', 'tinymce' ); + // Hide the editor if mode is html. } else if ( 'html' === mode ) { + + /* + * If the editor is hidden we are already in html mode and + * we don't need to switch. + * Return false to stop event bubbling. + */ if ( editor && editor.isHidden() ) { return false; } if ( editor ) { + + // Don't resize the iframe in iOS. if ( ! tinymce.Env.iOS ) { iframe = editor.iframeElement; editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0; @@ -97,7 +171,7 @@ toolbarHeight = getToolbarHeight( editor ); editorHeight = editorHeight + toolbarHeight - 14; - // height cannot be under 50 or over 5000 + // Height must be between 50 and 5000. if ( editorHeight > 50 && editorHeight < 5000 ) { textarea.style.height = editorHeight + 'px'; } @@ -106,7 +180,7 @@ editor.hide(); } else { - // The TinyMCE instance doesn't exist, show the textarea + // The TinyMCE instance doesn't exist, show the textarea. $textarea.css({ 'display': '', 'visibility': '' }); } @@ -116,7 +190,22 @@ } } - // Replace paragraphs with double line breaks + /** + * @summary Replaces all paragraphs with double line breaks. + * + * Replaces all paragraphs with double line breaks. Taking into account + * the elements where the

should be preserved. + * Unifies all whitespaces. + * Adds indenting with tabs to li, dt and dd elements. + * Trims whitespaces from beginning and end of the html input. + * + * @since 4.3.0 + * + * @memberof switchEditors + * + * @param {string} html The content from the editor. + * @return {string} The formatted html string. + */ function removep( html ) { var blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset', blocklist1 = blocklist + '|div|p', @@ -129,7 +218,10 @@ return ''; } - // Preserve script and style tags. + /* + * Protect script and style tags by replacing them with . + * Push matches into the preserve array. + */ if ( html.indexOf( ']*>[\s\S]*?<\/\1>/g, function( match ) { preserve.push( match ); @@ -137,7 +229,10 @@ } ); } - // Protect pre tags. + /* + * Protect pre tags by replacing all newlines and + *
tags with . + */ if ( html.indexOf( ']*>[\s\S]+?<\/pre>/g, function( a ) { @@ -147,7 +242,10 @@ }); } - // keep
tags inside captions and remove line breaks + /* + * Keep
tags inside captions and remove line breaks by replacing + * them with . + */ if ( html.indexOf( '[caption' ) !== -1 ) { preserve_br = true; html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { @@ -155,66 +253,96 @@ }); } - // Pretty it up for the source editor + // Format the text to be readable in the source editor. html = html.replace( new RegExp( '\\s*\\s*', 'g' ), '\n' ); html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); // Mark

if it has any attributes. html = html.replace( /(

]+>.*?)<\/p>/g, '$1' ); - // Separate

containing

+ // If the content of a container starts with a paragraph, replace the

tag with 2 newlines. html = html.replace( /]*)?>\s*

/gi, '\n\n' ); - // Remove

and
+ // Remove

and

tags. html = html.replace( /\s*

/gi, '' ); html = html.replace( /\s*<\/p>\s*/gi, '\n\n' ); + + // Remove white spaces between newlines. u00a0 is a no breaking space. html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' ); + + // Remove
tags. html = html.replace( /\s*
\s*/gi, '\n' ); - // Fix some block element newline issues + /* + * Fix some block element newline issues. + * Replace white spaces with newlines in combination with

tags. + */ html = html.replace( /\s*
\s*/g, '
\n' ); + + // Replace white spaces with newlines in combination with [caption] shortcodes. html = html.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' ); + + /* + * Limit the newlines in combination with [caption]'s to a maximum of + * two consecutive occurrences. + * . + */ html = html.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' ); + /* + * Replace white spaces with newlines in combination with + * all elements listed in blocklist2. + */ html = html.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' ); html = html.replace( new RegExp('\\s*\\s*', 'g' ), '\n' ); + + // Add indentation by adding a tab in front of
  • ,
    and
    tags. html = html.replace( /<((li|dt|dd)[^>]*)>/g, ' \t<$1>' ); + // Replace white spaces with newlines in combination with ' ); } + // Replace white spaces with 2 newlines in combination with
    tags. if ( html.indexOf( ']*)?>\s*/g, '\n\n\n\n' ); } + // Remove newlines in tags. if ( html.indexOf( '/g, function( a ) { return a.replace( /[\r\n]+/g, '' ); }); } - // Unmark special paragraph closing tags + // Unmark special paragraph closing tags. html = html.replace( /<\/p#>/g, '

    \n' ); + + + // Add a new line before

    tags when there is content inside the paragraph html = html.replace( /\s*(

    ]+>[\s\S]*?<\/p>)/g, '\n$1' ); - // Trim whitespace + /* + * Remove whitespaces at the start and end of a string. + * u00a0 is a no breaking space. + */ html = html.replace( /^\s+/, '' ); html = html.replace( /[\s\u00a0]+$/, '' ); - // put back the line breaks in pre|script + // Replace tags with a newline. if ( preserve_linebreaks ) { html = html.replace( //g, '\n' ); } - // and the
    tags in captions + // Restore the with
    tags. if ( preserve_br ) { html = html.replace( /]*)>/g, '' ); } - // Put back preserved tags. + // Restore preserved tags. if ( preserve.length ) { html = html.replace( //g, function() { return preserve.shift(); @@ -224,32 +352,52 @@ return html; } - // Similar to `wpautop()` in formatting.php + /** + * @summary Adds paragraph tags to the text. + * + * Adds paragraph tags to the text taking into account block level elements. + * Normalizes the whitespaces and newlines. + * + * Similar to `wpautop()` in formatting.php. + * + * @since 4.3.0 + * + * @memberof switchEditors + * + * @param {string} text The text input. + * @returns {string} The formatted text. + */ function autop( text ) { var preserve_linebreaks = false, preserve_br = false, + + // A list containing all block level elements. blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' + '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' + '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary'; - // Normalize line breaks + // Normalize line breaks. text = text.replace( /\r\n|\r/g, '\n' ); + // If there are no newlines, return the text. if ( text.indexOf( '\n' ) === -1 ) { return text; } if ( text.indexOf( ', remove them. text = text.replace( //g, function( a ) { return a.replace( /\n+/g, '' ); }); } + // Replace all new lines and tabs with spaces inside tags. text = text.replace( /<[^<>]+>/g, function( a ) { return a.replace( /[\n\t ]+/g, ' ' ); }); - // Protect pre|script tags + // Protect

     and