<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[22012] trunk/wp-includes: First pass on TinyMCE attachment in-editor UI.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/22012">22012</a></dd>
<dt>Author</dt> <dd>koopersmith</dd>
<dt>Date</dt> <dd>2012-09-26 14:12:54 +0000 (Wed, 26 Sep 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>First pass on TinyMCE attachment in-editor UI.

* Adds in-editor UI for image attachments. Most of this UI should be able to migrate to all images in a future commit.
* Removes the wpeditimage TinyMCE plugin from the default plugins array.
* Add `wp.media.fit`, a helper method to constrain dimensions based upon a maximum width and/or height.
* Add html attribute parsing and string generation utilities (currently stored in mce-views).
* Calling `remove` on a TinyMCE views now ensures that the the parent and references are removed as well.
* Fixes a bug where we weren't sending the full array of results to matches in wp.mce.view.

see <a href="http://core.trac.wordpress.org/ticket/21390">#21390</a>, <a href="http://core.trac.wordpress.org/ticket/21812">#21812</a>, <a href="http://core.trac.wordpress.org/ticket/21813">#21813</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesclasswpeditorphp">trunk/wp-includes/class-wp-editor.php</a></li>
<li><a href="#trunkwpincludesjsmceviewjs">trunk/wp-includes/js/mce-view.js</a></li>
<li><a href="#trunkwpincludesjsmediamodelsjs">trunk/wp-includes/js/media-models.js</a></li>
<li><a href="#trunkwpincludesjstinymcethemesadvancedskinswp_themecontentcss">trunk/wp-includes/js/tinymce/themes/advanced/skins/wp_theme/content.css</a></li>
<li><a href="#trunkwpincludesmediaphp">trunk/wp-includes/media.php</a></li>
<li><a href="#trunkwpincludesscriptloaderphp">trunk/wp-includes/script-loader.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesclasswpeditorphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-editor.php (22011 => 22012)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-editor.php        2012-09-26 12:32:54 UTC (rev 22011)
+++ trunk/wp-includes/class-wp-editor.php        2012-09-26 14:12:54 UTC (rev 22012)
</span><span class="lines">@@ -191,7 +191,7 @@
</span><span class="cx">                                 self::$baseurl = includes_url('js/tinymce');
</span><span class="cx">                                 self::$mce_locale = $mce_locale = ( '' == get_locale() ) ? 'en' : strtolower( substr(get_locale(), 0, 2) ); // only ISO 639-1
</span><span class="cx">                                 $no_captions = (bool) apply_filters( 'disable_captions', '' );
</span><del>-                                $plugins = array( 'inlinepopups', 'spellchecker', 'tabfocus', 'paste', 'media', 'fullscreen', 'wordpress', 'wpeditimage', 'wpgallery', 'wplink', 'wpdialogs', 'wpview' );
</del><ins>+                                $plugins = array( 'inlinepopups', 'spellchecker', 'tabfocus', 'paste', 'media', 'fullscreen', 'wordpress', 'wpgallery', 'wplink', 'wpdialogs', 'wpview' );
</ins><span class="cx">                                 $first_run = true;
</span><span class="cx">                                 $ext_plugins = '';
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesjsmceviewjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/mce-view.js (22011 => 22012)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/mce-view.js        2012-09-26 12:32:54 UTC (rev 22011)
+++ trunk/wp-includes/js/mce-view.js        2012-09-26 14:12:54 UTC (rev 22012)
</span><span class="lines">@@ -39,7 +39,7 @@
</span><span class="cx">                                         content: match[0],
</span><span class="cx">                                         options: {
</span><span class="cx">                                                 original: match[0],
</span><del>-                                                results:  _.toArray( arguments )
</del><ins>+                                                results:  match
</ins><span class="cx">                                         }
</span><span class="cx">                                 };
</span><span class="cx">                         }
</span><span class="lines">@@ -92,19 +92,53 @@
</span><span class="cx">                 // * `text` is a method that accepts an instance of the `view`
</span><span class="cx">                 // constructor and transforms it into a text representation.
</span><span class="cx">                 add: function( id, options ) {
</span><ins>+                        var parent, remove, base, properties;
+
</ins><span class="cx">                         // Fetch the parent view or the default options.
</span><del>-                        var parent = options.extend ? wp.mce.view.get( options.extend ) : wp.mce.view.defaults;
</del><ins>+                        parent = options.extend ? wp.mce.view.get( options.extend ) : wp.mce.view.defaults;
</ins><span class="cx"> 
</span><span class="cx">                         // Extend the `options` object with the parent's properties.
</span><span class="cx">                         _.defaults( options, parent );
</span><span class="cx">                         options.id = id;
</span><span class="cx"> 
</span><del>-                        // If the `view` provided was an object, automatically create
-                        // a new `Backbone.View` constructor, using the parent's `view`
-                        // constructor as a base.
-                        if ( ! _.isFunction( options.view ) )
-                                options.view = parent.view.extend( options.view );
</del><ins>+                        // 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();
</ins><span class="cx"> 
</span><ins>+                                        // 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 &amp;&amp; ! 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
+                        });
+
</ins><span class="cx">                         views[ id ] = options;
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="lines">@@ -234,7 +268,187 @@
</span><span class="cx"> 
</span><span class="cx">                                 return instance &amp;&amp; view ? view.text( instance ) : '';
</span><span class="cx">                         });
</span><del>-                }
</del><ins>+                },
+
+                // Link any localized strings.
+                l10n: _.isUndefined( _wpMceViewL10n ) ? {} : _wpMceViewL10n
</ins><span class="cx">         };
</span><span class="cx"> 
</span><span class="cx"> }(jQuery));
</span><ins>+
+// Default TinyMCE Views
+// ---------------------
+(function($){
+        var mceview = wp.mce.view,
+                attrs;
+
+        wp.html = _.extend( wp.html || {}, {
+                // ### Parse HTML attributes.
+                //
+                // Converts `content` to a set of parsed HTML attributes.
+                // Utilizes `wp.shortcode.attrs( content )`, which is a valid superset of
+                // the HTML attribute specification. Reformats the attributes into an
+                // object that contains the `attrs` with `key:value` mapping, and a record
+                // of the attributes that were entered using `empty` attribute syntax (i.e.
+                // with no value).
+                attrs: function( content ) {
+                        var result, attrs;
+
+                        // If `content` ends in a slash, strip it.
+                        if ( '/' === content[ content.length - 1 ] )
+                                content = content.slice( 0, -1 );
+
+                        result = wp.shortcode.attrs( content );
+                        attrs  = result.named;
+
+                        _.each( result.numeric, function( key ) {
+                                if ( /\s/.test( key ) )
+                                        return;
+
+                                attrs[ key ] = '';
+                        });
+
+                        return attrs;
+                },
+
+                string: function( options ) {
+                        var text = '&lt;' + options.tag,
+                                content = options.content || '';
+
+                        _.each( options.attrs, function( value, attr ) {
+                                text += ' ' + attr;
+
+                                // Use empty attribute notation where possible.
+                                if ( '' === value )
+                                        return;
+
+                                // Convert boolean values to strings.
+                                if ( _.isBoolean( value ) )
+                                        value = value ? 'true' : 'false';
+
+                                text += '=&quot;' + value + '&quot;';
+                        });
+
+                        // Return the result if it is a self-closing tag.
+                        if ( options.single )
+                                return text + ' /&gt;';
+
+                        // Complete the opening tag.
+                        text += '&gt;';
+
+                        // If `content` is an object, recursively call this function.
+                        text += _.isObject( content ) ? wp.html.string( content ) : content;
+
+                        return text + '&lt;/' + options.tag + '&gt;';
+                }
+        });
+
+        mceview.add( 'attachment', {
+                pattern: new RegExp( '(?:&lt;a([^&gt;]*)&gt;)?&lt;img([^&gt;]*class=(?:&quot;[^&quot;]*|\'[^\']*)\\bwp-image-(\\d+)[^&gt;]*)&gt;(?:&lt;/a&gt;)?' ),
+
+                text: function( instance ) {
+                        var img     = _.clone( instance.img ),
+                                classes = img['class'].split(/\s+/),
+                                options;
+
+                        // Update `img` classes.
+                        if ( instance.align )
+                                classes.push( 'align' + instance.align );
+
+                        if ( instance.size )
+                                classes.push( 'size-' + instance.size );
+
+                        classes.push( 'wp-image-' + instance.model.id );
+
+                        img['class'] = _.compact( classes ).join(' ');
+
+                        // Generate `img` tag options.
+                        options = {
+                                tag:    'img',
+                                attrs:  img,
+                                single: true
+                        };
+
+                        // Generate the `a` element options, if they exist.
+                        if ( instance.anchor ) {
+                                options = {
+                                        tag:     'a',
+                                        attrs:   instance.anchor,
+                                        content: options
+                                };
+                        }
+
+                        return wp.html.string( options );
+                },
+
+                view: {
+                        className: 'editor-attachment',
+                        template:  media.template('editor-attachment'),
+
+                        events: {
+                                'click .close': 'remove'
+                        },
+
+                        initialize: function() {
+                                var view    = this,
+                                        results = this.options.results,
+                                        id      = results[3],
+                                        className;
+
+                                this.model = wp.media.model.Attachment.get( id );
+
+                                if ( results[1] )
+                                        this.anchor = wp.html.attrs( results[1] );
+
+                                this.img = wp.html.attrs( results[2] );
+                                className = this.img['class'];
+
+                                // Strip ID class.
+                                className = className.replace( /(?:^|\s)wp-image-\d+/, '' );
+
+                                // Calculate thumbnail `size` and remove class.
+                                className = className.replace( /(?:^|\s)size-(\S+)/, function( match, size ) {
+                                        view.size = size;
+                                        return '';
+                                });
+
+                                // Calculate `align` and remove class.
+                                className = className.replace( /(?:^|\s)align(left|center|right|none)(?:\s|$)/, function( match, align ) {
+                                        view.align = align;
+                                        return '';
+                                });
+
+                                this.img['class'] = className;
+
+                                this.$el.addClass('spinner');
+                                this.model.fetch().done( _.bind( this.render, this ) );
+                        },
+
+                        render: function() {
+                                var attachment = this.model.toJSON(),
+                                        options;
+
+                                // If we don't have the attachment data, bail.
+                                if ( ! attachment.url )
+                                        return;
+
+                                options = {
+                                        url: 'image' === attachment.type ? attachment.url : attachment.icon,
+                                        uploading: attachment.uploading
+                                };
+
+                                _.extend( options, wp.media.fit({
+                                        width:    attachment.width,
+                                        height:   attachment.height,
+                                        maxWidth: mceview.l10n.contentWidth
+                                }) );
+
+                                // Use the specified size if it exists.
+                                if ( this.size &amp;&amp; attachment.sizes &amp;&amp; attachment.sizes[ this.size ] )
+                                        _.extend( options, _.pick( attachment.sizes[ this.size ], 'url', 'width', 'height' ) );
+
+                                this.$el.html( this.template( options ) );
+                        }
+                }
+        });
+}(jQuery));
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkwpincludesjsmediamodelsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-models.js (22011 => 22012)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-models.js        2012-09-26 12:32:54 UTC (rev 22011)
+++ trunk/wp-includes/js/media-models.js        2012-09-26 14:12:54 UTC (rev 22012)
</span><span class="lines">@@ -121,6 +121,45 @@
</span><span class="cx">                                         deferred.rejectWith( this, arguments );
</span><span class="cx">                                 });
</span><span class="cx">                         }).promise();
</span><ins>+                },
+
+                // Scales a set of dimensions to fit within bounding dimensions.
+                fit: function( dimensions ) {
+                        var width     = dimensions.width,
+                                height    = dimensions.height,
+                                maxWidth  = dimensions.maxWidth,
+                                maxHeight = dimensions.maxHeight,
+                                constraint;
+
+                        // Compare ratios between the two values to determine which
+                        // max to constrain by. If a max value doesn't exist, then the
+                        // opposite side is the constraint.
+                        if ( ! _.isUndefined( maxWidth ) &amp;&amp; ! _.isUndefined( maxHeight ) ) {
+                                constraint = ( width / height &gt; maxWidth / maxHeight ) ? 'width' : 'height';
+                        } else if ( _.isUndefined( maxHeight ) ) {
+                                constraint = 'width';
+                        } else if (  _.isUndefined( maxWidth ) &amp;&amp; height &gt; maxHeight ) {
+                                constraint = 'height';
+                        }
+
+                        // If the value of the constrained side is larger than the max,
+                        // then scale the values. Otherwise return the originals; they fit.
+                        if ( 'width' === constraint &amp;&amp; width &gt; maxWidth ) {
+                                return {
+                                        width : maxWidth,
+                                        height: maxWidth * height / width
+                                };
+                        } else if ( 'height' === constraint &amp;&amp; height &gt; maxHeight ) {
+                                return {
+                                        width : maxHeight * width / height,
+                                        height: maxHeight
+                                };
+                        } else {
+                                return {
+                                        width : width,
+                                        height: height
+                                };
+                        }
</ins><span class="cx">                 }
</span><span class="cx">         });
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesjstinymcethemesadvancedskinswp_themecontentcss"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/tinymce/themes/advanced/skins/wp_theme/content.css (22011 => 22012)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/tinymce/themes/advanced/skins/wp_theme/content.css        2012-09-26 12:32:54 UTC (rev 22011)
+++ trunk/wp-includes/js/tinymce/themes/advanced/skins/wp_theme/content.css        2012-09-26 14:12:54 UTC (rev 22012)
</span><span class="lines">@@ -146,3 +146,47 @@
</span><span class="cx"> div.wp-view {
</span><span class="cx">         display: inline-block;
</span><span class="cx"> }
</span><ins>+
+.spinner {
+        background: #fff url(&quot;../../../../../../../wp-admin/images/wpspin_light.gif&quot;) no-repeat center center;
+        border: 1px solid #dfdfdf;
+        margin-top: 10px;
+        margin-right: 10px;
+}
+
+.editor-attachment {
+        position: relative;
+        padding: 5px;
+}
+
+.editor-attachment,
+.editor-attachment img {
+        min-height: 100px;
+        min-width: 100px;
+}
+
+.editor-attachment img {
+        display: block;
+        border: 0;
+        padding: 0;
+        margin: 0;
+}
+
+.close {
+        display: none;
+        position: absolute;
+        top: 0;
+        right: 0;
+        height: 26px;
+        width: 26px;
+        font-size: 26px;
+        line-height: 22px;
+        text-align: center;
+        cursor: pointer;
+        color: #464646;
+        background: #fff;
+}
+
+.editor-attachment:hover .close {
+        display: block;
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkwpincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/media.php (22011 => 22012)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/media.php        2012-09-26 12:32:54 UTC (rev 22011)
+++ trunk/wp-includes/media.php        2012-09-26 14:12:54 UTC (rev 22012)
</span><span class="lines">@@ -1414,5 +1414,17 @@
</span><span class="cx">                 &lt;/div&gt;
</span><span class="cx">                 &lt;a class=&quot;clear-selection&quot; href=&quot;#&quot;&gt;&lt;?php _e('Clear selection'); ?&gt;&lt;/a&gt;
</span><span class="cx">         &lt;/script&gt;
</span><ins>+
+        &lt;script type=&quot;text/html&quot; id=&quot;tmpl-editor-attachment&quot;&gt;
+                &lt;% if ( url ) { %&gt;
+                        &lt;img src=&quot;&lt;%- url %&gt;&quot; width=&quot;&lt;%- width %&gt;&quot; height=&quot;&lt;%- height %&gt;&quot; draggable=&quot;false&quot; /&gt;
+                &lt;% } %&gt;
+
+                &lt;% if ( uploading ) { %&gt;
+                        &lt;div class=&quot;media-progress-bar&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;
+                &lt;% } %&gt;
+                &lt;div class=&quot;close&quot;&gt;&amp;times;&lt;/div&gt;
+                &lt;div class=&quot;describe&quot;&gt;&lt;/div&gt;
+        &lt;/script&gt;
</ins><span class="cx">         &lt;?php
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkwpincludesscriptloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/script-loader.php (22011 => 22012)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/script-loader.php        2012-09-26 12:32:54 UTC (rev 22011)
+++ trunk/wp-includes/script-loader.php        2012-09-26 14:12:54 UTC (rev 22012)
</span><span class="lines">@@ -323,7 +323,10 @@
</span><span class="cx">         ) );
</span><span class="cx"> 
</span><span class="cx">         $scripts-&gt;add( 'shortcode', &quot;/wp-includes/js/shortcode$suffix.js&quot;, array( 'underscore' ), false, 1 );
</span><del>-        $scripts-&gt;add( 'mce-view', &quot;/wp-includes/js/mce-view$suffix.js&quot;, array( 'shortcode', 'backbone', 'jquery' ), false, 1 );
</del><ins>+        $scripts-&gt;add( 'mce-view', &quot;/wp-includes/js/mce-view$suffix.js&quot;, array( 'shortcode', 'media-models' ), false, 1 );
+        did_action( 'init' ) &amp;&amp; $scripts-&gt;localize( 'mce-view', '_wpMceViewL10n', array(
+                'contentWidth' =&gt; isset( $GLOBALS['content_width'] ) ? $GLOBALS['content_width'] : 800,
+        ) );
</ins><span class="cx"> 
</span><span class="cx">         if ( is_admin() ) {
</span><span class="cx">                 $scripts-&gt;add( 'ajaxcat', &quot;/wp-admin/js/cat$suffix.js&quot;, array( 'wp-lists' ) );
</span></span></pre>
</div>
</div>

</body>
</html>