X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/6c8f14c09105d0afa4c1574215c59b5021040e76..4feeb71a9d812a9ae371c28a3d8b442a4394ded7:/wp-includes/js/mce-view.js diff --git a/wp-includes/js/mce-view.js b/wp-includes/js/mce-view.js index 912c4c7c..b532cb97 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,825 @@ 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' ) || '' );
+ },
+
+ /**
+ * 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 );
+ }
+ },
- return _.pluck( pieces, 'content' ).join('');
+ /**
+ * 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 );
+ } );
+ }
},
- toView: function( viewType, options ) {
- var view = wp.mce.view.get( viewType ),
- instance, id;
+ /**
+ * 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();
+ };
- if ( ! view )
- return '';
+ 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;
+ }
- // Create a new view instance.
- instance = new view.view( _.extend( options || {}, {
- viewType: viewType
- }) );
+ content = this.getContent();
- // 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;
+ // If there's nothing to render an no loader needs to be shown, stop.
+ if ( ! this.loader && ! content ) {
+ return;
+ }
- // Create a dummy `$wrapper` property to allow `$wrapper` to be
- // called in the view's `render` method without a conditional.
- instance.$wrapper = $();
+ // We're about to rerender all views of this instance, so unbind rendered views.
+ force && this.unbind();
- return wp.html.string({
- // If the view is a span, wrap it in a span.
- tag: 'span' === instance.tagName ? 'span' : 'div',
+ // Replace any left over markers.
+ this.replaceMarkers();
- attrs: {
- 'class': 'wp-view-wrap wp-view-type-' + viewType,
- 'data-wp-view': id,
- 'contenteditable': false
+ if ( content ) {
+ this.setContent( content, function( editor, node, contentNode ) {
+ $( node ).data( 'rendered', true );
+ this.bindNode.call( this, editor, node, contentNode );
+ }, force ? null : false );
+ } else {
+ this.setLoader();
+ }
+ },
+
+ /**
+ * 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, contentNode ) {
+ this.unbindNode.call( this, editor, node, contentNode );
+ $( node ).trigger( 'wp-mce-view-unbind' );
+ }, true );
+ },
+
+ /**
+ * 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;
+ }
- // 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('');
- });
+ data = $( this ).data( 'rendered' ) === true;
+
+ return rendered ? data : ! data;
+ } )
+ .each( function() {
+ callback.call( self, editor, this, $( this ).find( '.wpview-content' ).get( 0 ) );
+ } );
+ } );
+ },
+
+ /**
+ * 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 );
+ } );
+ } );
},
- // ### 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;
+ /**
+ * Replaces all marker nodes tied to this view instance.
+ */
+ replaceMarkers: function() {
+ this.getMarkers( function( editor, node ) {
+ var selected = node === editor.selection.getNode(),
+ $viewNode;
- if ( instance )
- view = wp.mce.view.get( instance.options.viewType );
+ if ( ! this.loader && $( node ).text() !== this.text ) {
+ editor.dom.setAttrib( node, 'data-wpview-marker', null );
+ return;
+ }
- return instance && view ? view.text( instance ) : '';
- });
+ $viewNode = editor.$(
+ ' \u00a0 \u00a0