X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/6c8f14c09105d0afa4c1574215c59b5021040e76..b22765f41bf0b2021b9beb9120ee0ac91fa89292:/wp-includes/js/mce-view.js diff --git a/wp-includes/js/mce-view.js b/wp-includes/js/mce-view.js index 912c4c7c..3d2616d7 100644 --- a/wp-includes/js/mce-view.js +++ b/wp-includes/js/mce-view.js @@ -1,184 +1,105 @@ -// Ensure the global `wp` object exists. -window.wp = window.wp || {}; +/* global tinymce */ + +/* + * The TinyMCE view API. + * + * Note: this API is "experimental" meaning that it will probably change + * in the next few releases based on feedback from 3.9.0. + * If you decide to use it, please follow the development closely. + * + * Diagram + * + * |- registered view constructor (type) + * | |- view instance (unique text) + * | | |- editor 1 + * | | | |- view node + * | | | |- view node + * | | | |- ... + * | | |- editor 2 + * | | | |- ... + * | |- view instance + * | | |- ... + * |- registered view + * | |- ... + */ +( function( window, wp, shortcode, $ ) { + 'use strict'; -(function($){ var views = {}, instances = {}; - // Create the `wp.mce` object if necessary. wp.mce = wp.mce || {}; - // wp.mce.view - // ----------- - // A set of utilities that simplifies adding custom UI within a TinyMCE editor. - // At its core, it serves as a series of converters, transforming text to a - // custom UI, and back again. - wp.mce.view = { - // ### defaults - defaults: { - // The default properties used for objects with the `pattern` key in - // `wp.mce.view.add()`. - pattern: { - view: Backbone.View, - text: function( instance ) { - return instance.options.original; - }, - - toView: function( content ) { - if ( ! this.pattern ) - return; - - this.pattern.lastIndex = 0; - var match = this.pattern.exec( content ); - - if ( ! match ) - return; - - return { - index: match.index, - content: match[0], - options: { - original: match[0], - results: match - } - }; - } - }, - - // The default properties used for objects with the `shortcode` key in - // `wp.mce.view.add()`. - shortcode: { - view: Backbone.View, - text: function( instance ) { - return instance.options.shortcode.string(); - }, - - toView: function( content ) { - var match = wp.shortcode.next( this.shortcode, content ); - - if ( ! match ) - return; - - return { - index: match.index, - content: match.content, - options: { - shortcode: match.shortcode - } - }; - } - } + /** + * wp.mce.views + * + * A set of utilities that simplifies adding custom UI within a TinyMCE editor. + * At its core, it serves as a series of converters, transforming text to a + * custom UI, and back again. + */ + wp.mce.views = { + + /** + * Registers a new view type. + * + * @param {String} type The view type. + * @param {Object} extend An object to extend wp.mce.View.prototype with. + */ + register: function( type, extend ) { + views[ type ] = wp.mce.View.extend( _.extend( extend, { type: type } ) ); }, - // ### add( id, options ) - // Registers a new TinyMCE view. - // - // Accepts a unique `id` and an `options` object. - // - // `options` accepts the following properties: - // - // * `pattern` is the regular expression used to scan the content and - // detect matching views. - // - // * `view` is a `Backbone.View` constructor. If a plain object is - // provided, it will automatically extend the parent constructor - // (usually `Backbone.View`). Views are instantiated when the `pattern` - // is successfully matched. The instance's `options` object is provided - // with the `original` matched value, the match `results` including - // capture groups, and the `viewType`, which is the constructor's `id`. - // - // * `extend` an existing view by passing in its `id`. The current - // view will inherit all properties from the parent view, and if - // `view` is set to a plain object, it will extend the parent `view` - // constructor. - // - // * `text` is a method that accepts an instance of the `view` - // constructor and transforms it into a text representation. - add: function( id, options ) { - var parent, remove, base, properties; - - // Fetch the parent view or the default options. - if ( options.extend ) - parent = wp.mce.view.get( options.extend ); - else if ( options.shortcode ) - parent = wp.mce.view.defaults.shortcode; - else - parent = wp.mce.view.defaults.pattern; - - // Extend the `options` object with the parent's properties. - _.defaults( options, parent ); - options.id = id; - - // Create properties used to enhance the view for use in TinyMCE. - properties = { - // Ensure the wrapper element and references to the view are - // removed. Otherwise, removed views could randomly restore. - remove: function() { - delete instances[ this.el.id ]; - this.$el.parent().remove(); - - // Trigger the inherited `remove` method. - if ( remove ) - remove.apply( this, arguments ); - - return this; - } - }; - - // If the `view` provided was an object, use the parent's - // `view` constructor as a base. If a `view` constructor - // was provided, treat that as the base. - if ( _.isFunction( options.view ) ) { - base = options.view; - } else { - base = parent.view; - remove = options.view.remove; - _.defaults( properties, options.view ); - } - - // If there's a `remove` method on the `base` view that wasn't - // created by this method, inherit it. - if ( ! remove && ! base._mceview ) - remove = base.prototype.remove; - - // Automatically create the new `Backbone.View` constructor. - options.view = base.extend( properties, { - // Flag that the new view has been created by `wp.mce.view`. - _mceview: true - }); - - views[ id ] = options; + /** + * Unregisters a view type. + * + * @param {String} type The view type. + */ + unregister: function( type ) { + delete views[ type ]; }, - // ### get( id ) - // Returns a TinyMCE view options object. - get: function( id ) { - return views[ id ]; + /** + * Returns the settings of a view type. + * + * @param {String} type The view type. + * + * @return {Function} The view constructor. + */ + get: function( type ) { + return views[ type ]; }, - // ### remove( id ) - // Unregisters a TinyMCE view. - remove: function( id ) { - delete views[ id ]; + /** + * Unbinds all view nodes. + * Runs before removing all view nodes from the DOM. + */ + unbind: function() { + _.each( instances, function( instance ) { + instance.unbind(); + } ); }, - // ### toViews( content ) - // Scans a `content` string for each view's pattern, replacing any - // matches with wrapper elements, and creates a new view instance for - // every match. - // - // To render the views, call `wp.mce.view.render( scope )`. - toViews: function( content ) { + /** + * Scans a given string for each view's pattern, + * replacing any matches with markers, + * and creates a new instance for every match. + * + * @param {String} content The string to scan. + * + * @return {String} The string with markers. + */ + setMarkers: function( content ) { var pieces = [ { content: content } ], - current; + self = this, + instance, current; - _.each( views, function( view, viewType ) { + _.each( views, function( view, type ) { current = pieces.slice(); pieces = []; _.each( current, function( piece ) { var remaining = piece.content, - result; + result, text; // Ignore processed pieces, but retain their location. if ( piece.processed ) { @@ -188,162 +109,817 @@ window.wp = window.wp || {}; // Iterate through the string progressively matching views // and slicing the string as we go. - while ( remaining && (result = view.toView( remaining )) ) { + while ( remaining && ( result = view.prototype.match( remaining ) ) ) { // Any text before the match becomes an unprocessed piece. - if ( result.index ) - pieces.push({ content: remaining.substring( 0, result.index ) }); + if ( result.index ) { + pieces.push( { content: remaining.substring( 0, result.index ) } ); + } + + instance = self.createInstance( type, result.content, result.options ); + text = instance.loader ? '.' : instance.text; // Add the processed piece for the match. - pieces.push({ - content: wp.mce.view.toView( viewType, result.options ), + pieces.push( { + content: instance.ignore ? text : '
' + text + '
', processed: true - }); + } ); // Update the remaining content. remaining = remaining.slice( result.index + result.content.length ); } - // There are no additional matches. If any content remains, - // add it as an unprocessed piece. - if ( remaining ) - pieces.push({ content: remaining }); - }); - }); + // There are no additional matches. + // If any content remains, add it as an unprocessed piece. + if ( remaining ) { + pieces.push( { content: remaining } ); + } + } ); + } ); + + content = _.pluck( pieces, 'content' ).join( '' ); + return content.replace( /\s*
' ); + }, + + /** + * Create a view instance. + * + * @param {String} type The view type. + * @param {String} text The textual representation of the view. + * @param {Object} options Options. + * @param {Boolean} force Recreate the instance. Optional. + * + * @return {wp.mce.View} The view instance. + */ + createInstance: function( type, text, options, force ) { + var View = this.get( type ), + encodedText, + instance; + + text = tinymce.DOM.decode( text ); + + if ( ! force ) { + instance = this.getInstance( text ); + + if ( instance ) { + return instance; + } + } + + encodedText = encodeURIComponent( text ); + + options = _.extend( options || {}, { + text: text, + encodedText: encodedText + } ); + + return instances[ encodedText ] = new View( options ); + }, + + /** + * Get a view instance. + * + * @param {(String|HTMLElement)} object The textual representation of the view or the view node. + * + * @return {wp.mce.View} The view instance or undefined. + */ + getInstance: function( object ) { + if ( typeof object === 'string' ) { + return instances[ encodeURIComponent( object ) ]; + } + + return instances[ $( object ).attr( 'data-wpview-text' ) ]; + }, + + /** + * Given a view node, get the view's text. + * + * @param {HTMLElement} node The view node. + * + * @return {String} The textual representation of the view. + */ + getText: function( node ) { + return decodeURIComponent( $( node ).attr( 'data-wpview-text' ) || '' ); + }, - return _.pluck( pieces, 'content' ).join(''); + /** + * Renders all view nodes that are not yet rendered. + * + * @param {Boolean} force Rerender all view nodes. + */ + render: function( force ) { + _.each( instances, function( instance ) { + instance.render( force ); + } ); + }, + + /** + * Update the text of a given view node. + * + * @param {String} text The new text. + * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in. + * @param {HTMLElement} node The view node to update. + * @param {Boolean} force Recreate the instance. Optional. + */ + update: function( text, editor, node, force ) { + var instance = this.getInstance( node ); + + if ( instance ) { + instance.update( text, editor, node, force ); + } }, - toView: function( viewType, options ) { - var view = wp.mce.view.get( viewType ), - instance, id; + /** + * Renders any editing interface based on the view type. + * + * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in. + * @param {HTMLElement} node The view node to edit. + */ + edit: function( editor, node ) { + var instance = this.getInstance( node ); + + if ( instance && instance.edit ) { + instance.edit( instance.text, function( text, force ) { + instance.update( text, editor, node, force ); + } ); + } + }, - if ( ! view ) - return ''; + /** + * Remove a given view node from the DOM. + * + * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in. + * @param {HTMLElement} node The view node to remove. + */ + remove: function( editor, node ) { + var instance = this.getInstance( node ); + + if ( instance ) { + instance.remove( editor, node ); + } + } + }; + + /** + * A Backbone-like View constructor intended for use when rendering a TinyMCE View. + * The main difference is that the TinyMCE View is not tied to a particular DOM node. + * + * @param {Object} options Options. + */ + wp.mce.View = function( options ) { + _.extend( this, options ); + this.initialize(); + }; + + wp.mce.View.extend = Backbone.View.extend; + + _.extend( wp.mce.View.prototype, { + + /** + * The content. + * + * @type {*} + */ + content: null, + + /** + * Whether or not to display a loader. + * + * @type {Boolean} + */ + loader: true, + + /** + * Runs after the view instance is created. + */ + initialize: function() {}, + + /** + * Retuns the content to render in the view node. + * + * @return {*} + */ + getContent: function() { + return this.content; + }, + + /** + * Renders all view nodes tied to this view instance that are not yet rendered. + * + * @param {String} content The content to render. Optional. + * @param {Boolean} force Rerender all view nodes tied to this view instance. Optional. + */ + render: function( content, force ) { + if ( content != null ) { + this.content = content; + } + + content = this.getContent(); + + // If there's nothing to render an no loader needs to be shown, stop. + if ( ! this.loader && ! content ) { + return; + } - // Create a new view instance. - instance = new view.view( _.extend( options || {}, { - viewType: viewType - }) ); + // We're about to rerender all views of this instance, so unbind rendered views. + force && this.unbind(); - // Use the view's `id` if it already exists. Otherwise, - // create a new `id`. - id = instance.el.id = instance.el.id || _.uniqueId('__wpmce-'); - instances[ id ] = instance; + // Replace any left over markers. + this.replaceMarkers(); - // Create a dummy `$wrapper` property to allow `$wrapper` to be - // called in the view's `render` method without a conditional. - instance.$wrapper = $(); + if ( content ) { + this.setContent( content, function( editor, node ) { + $( node ).data( 'rendered', true ); + this.bindNode.call( this, editor, node ); + }, force ? null : false ); + } else { + this.setLoader(); + } + }, - return wp.html.string({ - // If the view is a span, wrap it in a span. - tag: 'span' === instance.tagName ? 'span' : 'div', + /** + * Binds a given node after its content is added to the DOM. + */ + bindNode: function() {}, + + /** + * Unbinds a given node before its content is removed from the DOM. + */ + unbindNode: function() {}, + + /** + * Unbinds all view nodes tied to this view instance. + * Runs before their content is removed from the DOM. + */ + unbind: function() { + this.getNodes( function( editor, node ) { + this.unbindNode.call( this, editor, node ); + }, true ); + }, - attrs: { - 'class': 'wp-view-wrap wp-view-type-' + viewType, - 'data-wp-view': id, - 'contenteditable': false + /** + * Gets all the TinyMCE editor instances that support views. + * + * @param {Function} callback A callback. + */ + getEditors: function( callback ) { + _.each( tinymce.editors, function( editor ) { + if ( editor.plugins.wpview ) { + callback.call( this, editor ); } - }); + }, this ); }, - // ### render( scope ) - // Renders any view instances inside a DOM node `scope`. - // - // View instances are detected by the presence of wrapper elements. - // To generate wrapper elements, pass your content through - // `wp.mce.view.toViews( content )`. - render: function( scope ) { - $( '.wp-view-wrap', scope ).each( function() { - var wrapper = $(this), - view = wp.mce.view.instance( this ); - - if ( ! view ) - return; + /** + * Gets all view nodes tied to this view instance. + * + * @param {Function} callback A callback. + * @param {Boolean} rendered Get (un)rendered view nodes. Optional. + */ + getNodes: function( callback, rendered ) { + this.getEditors( function( editor ) { + var self = this; + + $( editor.getBody() ) + .find( '[data-wpview-text="' + self.encodedText + '"]' ) + .filter( function() { + var data; + + if ( rendered == null ) { + return true; + } + + data = $( this ).data( 'rendered' ) === true; - // Link the real wrapper to the view. - view.$wrapper = wrapper; - // Render the view. - view.render(); - // Detach the view element to ensure events are not unbound. - view.$el.detach(); - - // Empty the wrapper, attach the view element to the wrapper, - // and add an ending marker to the wrapper to help regexes - // scan the HTML string. - wrapper.empty().append( view.el ).append(''); - }); + return rendered ? data : ! data; + } ) + .each( function() { + callback.call( self, editor, this, this /* back compat */ ); + } ); + } ); }, - // ### toText( content ) - // Scans an HTML `content` string and replaces any view instances with - // their respective text representations. - toText: function( content ) { - return content.replace( /<(?:div|span)[^>]+data-wp-view="([^"]+)"[^>]*>.*?]+data-wp-view-end[^>]*><\/span><\/(?:div|span)>/g, function( match, id ) { - var instance = instances[ id ], - view; + /** + * Gets all marker nodes tied to this view instance. + * + * @param {Function} callback A callback. + */ + getMarkers: function( callback ) { + this.getEditors( function( editor ) { + var self = this; + + $( editor.getBody() ) + .find( '[data-wpview-marker="' + this.encodedText + '"]' ) + .each( function() { + callback.call( self, editor, this ); + } ); + } ); + }, - if ( instance ) - view = wp.mce.view.get( instance.options.viewType ); + /** + * Replaces all marker nodes tied to this view instance. + */ + replaceMarkers: function() { + this.getMarkers( function( editor, node ) { + var $viewNode; - return instance && view ? view.text( instance ) : ''; - }); + if ( ! this.loader && $( node ).text() !== this.text ) { + editor.dom.setAttrib( node, 'data-wpview-marker', null ); + return; + } + + $viewNode = editor.$( + '' + ); + + editor.$( node ).replaceWith( $viewNode ); + } ); }, - // ### Remove internal TinyMCE attributes. - removeInternalAttrs: function( attrs ) { - var result = {}; - _.each( attrs, function( value, attr ) { - if ( -1 === attr.indexOf('data-mce') ) - result[ attr ] = value; - }); - return result; + /** + * Removes all marker nodes tied to this view instance. + */ + removeMarkers: function() { + this.getMarkers( function( editor, node ) { + editor.dom.setAttrib( node, 'data-wpview-marker', null ); + } ); }, - // ### Parse an attribute string and removes internal TinyMCE attributes. - attrs: function( content ) { - return wp.mce.view.removeInternalAttrs( wp.html.attrs( content ) ); + /** + * Sets the content for all view nodes tied to this view instance. + * + * @param {*} content The content to set. + * @param {Function} callback A callback. Optional. + * @param {Boolean} rendered Only set for (un)rendered nodes. Optional. + */ + setContent: function( content, callback, rendered ) { + if ( _.isObject( content ) && content.body.indexOf( ''; + } + + editor.undoManager.transact( function() { + node.innerHTML = ''; + node.appendChild( _.isString( content ) ? editor.dom.createFragment( content ) : content ); + editor.dom.add( node, 'span', { 'class': 'wpview-end' } ); + } ); + + callback && callback.call( this, editor, node ); + }, rendered ); + } }, - // ### instance( scope ) - // - // Accepts a MCE view wrapper `node` (i.e. a node with the - // `wp-view-wrap` class). - instance: function( node ) { - var id = $( node ).data('wp-view'); + /** + * Sets the content in an iframe for all view nodes tied to this view instance. + * + * @param {String} head HTML string to be added to the head of the document. + * @param {String} body HTML string to be added to the body of the document. + * @param {Function} callback A callback. Optional. + * @param {Boolean} rendered Only set for (un)rendered nodes. Optional. + */ + setIframes: function( head, body, callback, rendered ) { + var self = this; + + this.getNodes( function( editor, node ) { + var dom = editor.dom, + styles = '', + bodyClasses = editor.getBody().className || '', + editorHead = editor.getDoc().getElementsByTagName( 'head' )[0]; + + tinymce.each( dom.$( 'link[rel="stylesheet"]', editorHead ), function( link ) { + if ( link.href && link.href.indexOf( 'skins/lightgray/content.min.css' ) === -1 && + link.href.indexOf( 'skins/wordpress/wp-content.css' ) === -1 ) { + + styles += dom.getOuterHTML( link ); + } + } ); + + if ( self.iframeHeight ) { + dom.add( node, 'span', { + 'data-mce-bogus': 1, + style: { + display: 'block', + width: '100%', + height: self.iframeHeight + } + }, '\u200B' ); + } + + // Seems the browsers need a bit of time to insert/set the view nodes, + // or the iframe will fail especially when switching Text => Visual. + setTimeout( function() { + var iframe, iframeWin, iframeDoc, MutationObserver, observer, i, block; + + editor.undoManager.transact( function() { + node.innerHTML = ''; + + iframe = dom.add( node, 'iframe', { + /* jshint scripturl: true */ + src: tinymce.Env.ie ? 'javascript:""' : '', + frameBorder: '0', + allowTransparency: 'true', + scrolling: 'no', + 'class': 'wpview-sandbox', + style: { + width: '100%', + display: 'block' + }, + height: self.iframeHeight + } ); + + dom.add( node, 'span', { 'class': 'mce-shim' } ); + dom.add( node, 'span', { 'class': 'wpview-end' } ); + } ); + + // Bail if the iframe node is not attached to the DOM. + // Happens when the view is dragged in the editor. + // There is a browser restriction when iframes are moved in the DOM. They get emptied. + // The iframe will be rerendered after dropping the view node at the new location. + if ( ! iframe.contentWindow ) { + return; + } + + iframeWin = iframe.contentWindow; + iframeDoc = iframeWin.document; + iframeDoc.open(); + + iframeDoc.write( + '' + + '' + + '
' + + '' + + head + + styles + + '' + + '' + + '' + + body + + '' + + '' + ); + + iframeDoc.close(); + + function resize() { + var $iframe; + + if ( block ) { + return; + } + + // Make sure the iframe still exists. + if ( iframe.contentWindow ) { + $iframe = $( iframe ); + self.iframeHeight = $( iframeDoc.body ).height(); + + if ( $iframe.height() !== self.iframeHeight ) { + $iframe.height( self.iframeHeight ); + editor.nodeChanged(); + } + } + } + + if ( self.iframeHeight ) { + block = true; + + setTimeout( function() { + block = false; + resize(); + }, 3000 ); + } + + $( iframeWin ).on( 'load', resize ); + + MutationObserver = iframeWin.MutationObserver || iframeWin.WebKitMutationObserver || iframeWin.MozMutationObserver; + + if ( MutationObserver ) { + observer = new MutationObserver( _.debounce( resize, 100 ) ); + + observer.observe( iframeDoc.body, { + attributes: true, + childList: true, + subtree: true + } ); + } else { + for ( i = 1; i < 6; i++ ) { + setTimeout( resize, i * 700 ); + } + } - if ( id ) - return instances[ id ]; + callback && callback.call( self, editor, node ); + }, 50 ); + }, rendered ); }, - // ### Select a view. - // - // Accepts a MCE view wrapper `node` (i.e. a node with the - // `wp-view-wrap` class). - select: function( node ) { - var $node = $(node); + /** + * Sets a loader for all view nodes tied to this view instance. + */ + setLoader: function() { + this.setContent( + '' + message + '
' + + ')(https?:\/\/[^\s"]+?)(<\/p>\s*|$)/gi, + match = re.exec( content ); + + if ( match ) { + return { + index: match.index + match[1].length, + content: match[2], + options: { + url: true + } + }; + } + } + } ) ); +} )( window, window.wp.mce.views, window.wp.media, window.jQuery );