<!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>[28994] trunk/src/wp-includes/js: TinyMCE: improve the way wpViews work.</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 { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { 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/28994">28994</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2014-07-04 03:58:20 +0000 (Fri, 04 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>TinyMCE: improve the way wpViews work. Add two paragraphs and capture the caret in them on clicking before/after/left/right of a view or moving the caret with the arrow keys, then show a "fake" caret.

This makes it much more "natural" to move the caret with the arrow keys and to add paragraphs before a view when it is the first element or after a view when it's last.

Props avryl, see <a href="http://core.trac.wordpress.org/ticket/28595">#28595</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesjsmceviewjs">trunk/src/wp-includes/js/mce-view.js</a></li>
<li><a href="#trunksrcwpincludesjstinymcepluginswpviewpluginjs">trunk/src/wp-includes/js/tinymce/plugins/wpview/plugin.js</a></li>
<li><a href="#trunksrcwpincludesjstinymceskinswordpresswpcontentcss">trunk/src/wp-includes/js/tinymce/skins/wordpress/wp-content.css</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesjsmceviewjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/js/mce-view.js (28993 => 28994)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/js/mce-view.js     2014-07-04 03:38:33 UTC (rev 28993)
+++ trunk/src/wp-includes/js/mce-view.js        2014-07-04 03:58:20 UTC (rev 28994)
</span><span class="lines">@@ -39,45 +39,47 @@
</span><span class="cx">          getHtml: function() {},
</span><span class="cx">          render: function() {
</span><span class="cx">                  this.setContent(
</span><del>-                               '<div class="toolbar">' +
-                                       ( _.isFunction( views[ this.type ].edit ) ? '<div class="dashicons dashicons-edit edit"></div>' : '' ) +
-                                       '<div class="dashicons dashicons-no-alt remove"></div>' +
</del><ins>+                                '<p class="wpview-selection-before">\u00a0</p>' +
+                               '<div class="wpview-body" contenteditable="false">' +
+                                       '<div class="toolbar">' +
+                                               ( _.isFunction( views[ this.type ].edit ) ? '<div class="dashicons dashicons-edit edit"></div>' : '' ) +
+                                               '<div class="dashicons dashicons-no-alt remove"></div>' +
+                                       '</div>' +
+                                       '<div class="wpview-content wpview-type-' + this.type + '">' +
+                                               this.getHtml() +
+                                       '</div>' +
+                                       ( this.overlay ? '<div class="wpview-overlay"></div>' : '' ) +
</ins><span class="cx">                           '</div>' +
</span><del>-                               '<div class="wpview-content">' +
-                                       this.getHtml() +
-                               '</div>' +
-                               ( this.overlay ? '<div class="wpview-overlay"></div>' : '' ) +
-                               // The <ins> is used to mark the end of the wrapper div (has to be the last child node).
-                               // Needed when comparing the content as string for preventing extra undo levels.
-                               '<ins data-wpview-end="1"></ins>',
</del><ins>+                                '<p class="wpview-selection-after">\u00a0</p>',
</ins><span class="cx">                           function( self, editor, node ) {
</span><span class="cx">                                  $( self ).trigger( 'ready', [ editor, node ] );
</span><del>-                               }
</del><ins>+                                },
+                               'wrap'
</ins><span class="cx">                   );
</span><span class="cx">          },
</span><span class="cx">          unbind: function() {},
</span><del>-               setContent: function( html, callback, replace ) {
</del><ins>+                setContent: function( html, callback, option ) {
</ins><span class="cx">                   _.each( tinymce.editors, function( editor ) {
</span><span class="cx">                          var self = this;
</span><span class="cx">                          if ( editor.plugins.wpview ) {
</span><span class="cx">                                  $( editor.getBody() )
</span><span class="cx">                                  .find( '[data-wpview-text="' + this.encodedText + '"]' )
</span><span class="cx">                                  .each( function ( i, element ) {
</span><del>-                                               var contentWrap = $( element ).children( '.wpview-content' ),
</del><ins>+                                                var contentWrap = $( element ).find( '.wpview-content' ),
</ins><span class="cx">                                                   wrap = element;
</span><span class="cx"> 
</span><del>-                                               if ( contentWrap.length ) {
</del><ins>+                                                if ( contentWrap.length && option !== 'wrap' ) {
</ins><span class="cx">                                                   element = contentWrap = contentWrap[0];
</span><span class="cx">                                          }
</span><span class="cx"> 
</span><span class="cx">                                          if ( _.isString( html ) ) {
</span><del>-                                                       if ( replace ) {
</del><ins>+                                                        if ( option === 'replace' ) {
</ins><span class="cx">                                                           element = editor.dom.replace( editor.dom.createFragment( html ), wrap );
</span><span class="cx">                                                  } else {
</span><span class="cx">                                                          editor.dom.setHTML( element, html );
</span><span class="cx">                                                  }
</span><span class="cx">                                          } else {
</span><del>-                                                       if ( replace ) {
</del><ins>+                                                        if ( option === 'replace' ) {
</ins><span class="cx">                                                           element = editor.dom.replace( html, wrap );
</span><span class="cx">                                                  } else {
</span><span class="cx">                                                          element.appendChild( html );
</span><span class="lines">@@ -85,7 +87,7 @@
</span><span class="cx">                                          }
</span><span class="cx"> 
</span><span class="cx">                                          if ( _.isFunction( callback ) ) {
</span><del>-                                                       callback( self, editor, $( element ).children( '.wpview-content' )[0] );
</del><ins>+                                                        callback( self, editor, $( element ).find( '.wpview-content' )[0] );
</ins><span class="cx">                                           }
</span><span class="cx">                                  } );
</span><span class="cx">                          }
</span><span class="lines">@@ -267,10 +269,9 @@
</span><span class="cx">                          tag: 'div',
</span><span class="cx"> 
</span><span class="cx">                          attrs: {
</span><del>-                                       'class': 'wpview-wrap wpview-type-' + viewType,
</del><ins>+                                        'class': 'wpview-wrap',
</ins><span class="cx">                                   'data-wpview-text': encodedText,
</span><del>-                                       'data-wpview-type': viewType,
-                                       'contenteditable': 'false'
</del><ins>+                                        'data-wpview-type': viewType
</ins><span class="cx">                           },
</span><span class="cx"> 
</span><span class="cx">                          content: '\u00a0'
</span><span class="lines">@@ -742,7 +743,7 @@
</span><span class="cx"> 
</span><span class="cx">                                          self.setError( response.message, 'admin-media' );
</span><span class="cx">                                  } else {
</span><del>-                                               self.setContent( '<p>' + self.original + '</p>', null, true );
</del><ins>+                                                self.setContent( '<p>' + self.original + '</p>', null, 'replace' );
</ins><span class="cx">                                   }
</span><span class="cx">                          } else if ( response && response.statusText ) {
</span><span class="cx">                                  self.setError( response.statusText, 'admin-media' );
</span></span></pre></div>
<a id="trunksrcwpincludesjstinymcepluginswpviewpluginjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/js/tinymce/plugins/wpview/plugin.js (28993 => 28994)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/js/tinymce/plugins/wpview/plugin.js        2014-07-04 03:38:33 UTC (rev 28993)
+++ trunk/src/wp-includes/js/tinymce/plugins/wpview/plugin.js   2014-07-04 03:58:20 UTC (rev 28994)
</span><span class="lines">@@ -4,41 +4,29 @@
</span><span class="cx">  */
</span><span class="cx"> tinymce.PluginManager.add( 'wpview', function( editor ) {
</span><span class="cx">  var selected,
</span><ins>+               Env = tinymce.Env,
</ins><span class="cx">           VK = tinymce.util.VK,
</span><span class="cx">          TreeWalker = tinymce.dom.TreeWalker,
</span><del>-               toRemove = false;
</del><ins>+                toRemove = false,
+               cursorInterval, lastKeyDownNode, setViewCursorTries;
</ins><span class="cx"> 
</span><del>-       function getParentView( node ) {
-               while ( node && node.nodeName !== 'BODY' ) {
-                       if ( isView( node ) ) {
-                               return node;
-                       }
-
-                       node = node.parentNode;
-               }
</del><ins>+        function getView( node ) {
+               return editor.dom.getParent( node, function( node ) {
+                       return editor.dom.hasClass( node, 'wpview-wrap' );
+               });
</ins><span class="cx">   }
</span><span class="cx"> 
</span><del>-       function isView( node ) {
-               return node && /\bwpview-wrap\b/.test( node.className );
-       }
-
-       function createPadNode() {
-               return editor.dom.create( 'p', { 'data-wpview-pad': 1 },
-                       ( tinymce.Env.ie && tinymce.Env.ie < 11 ) ? '' : '<br data-mce-bogus="1" />' );
-       }
-
</del><span class="cx">   /**
</span><span class="cx">   * Get the text/shortcode string for a view.
</span><span class="cx">   *
</span><del>-        * @param view The view wrapper's HTML id or node
</del><ins>+         * @param view The view wrapper's node
</ins><span class="cx">    * @returns string The text/shoercode string of the view
</span><span class="cx">   */
</span><span class="cx">  function getViewText( view ) {
</span><del>-               view = getParentView( typeof view === 'string' ? editor.dom.get( view ) : view );
-
-               if ( view ) {
</del><ins>+                if ( view = getView( view ) ) {
</ins><span class="cx">                   return window.decodeURIComponent( editor.dom.getAttrib( view, 'data-wpview-text' ) || '' );
</span><span class="cx">          }
</span><ins>+
</ins><span class="cx">           return '';
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -49,12 +37,13 @@
</span><span class="cx">   * @param text The text string to be set
</span><span class="cx">   */
</span><span class="cx">  function setViewText( view, text ) {
</span><del>-               view = getParentView( typeof view === 'string' ? editor.dom.get( view ) : view );
</del><ins>+                view = getView( view );
</ins><span class="cx"> 
</span><span class="cx">          if ( view ) {
</span><span class="cx">                  editor.dom.setAttrib( view, 'data-wpview-text', window.encodeURIComponent( text || '' ) );
</span><span class="cx">                  return true;
</span><span class="cx">          }
</span><ins>+
</ins><span class="cx">           return false;
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -62,6 +51,41 @@
</span><span class="cx">          event.stopPropagation();
</span><span class="cx">  }
</span><span class="cx"> 
</span><ins>+       function setViewCursor( before, view ) {
+               var location = before ? 'before' : 'after',
+                       offset = before ? 0 : 1;
+               editor.selection.setCursorLocation( editor.dom.select( '.wpview-selection-' + location, view )[0], offset );
+               editor.nodeChanged();
+       }
+
+       function handleEnter( view, before ) {
+               var dom = editor.dom,
+                       padNode;
+
+               if ( ! before && view.nextSibling && dom.isEmpty( view.nextSibling ) && view.nextSibling.nodeName === 'P' ) {
+                       padNode = view.nextSibling;
+               } else if ( before && view.previousSibling && dom.isEmpty( view.previousSibling ) && view.previousSibling.nodeName === 'P' ) {
+                       padNode = view.previousSibling;
+               } else {
+                       padNode = dom.create( 'p' );
+
+                       if ( ! ( Env.ie && Env.ie < 11 ) ) {
+                               padNode.innerHTML = '<br data-mce-bogus="1">';
+                       }
+
+                       if ( before ) {
+                               view.parentNode.insertBefore( padNode, view );
+                       } else {
+                               dom.insertAfter( padNode, view );
+                       }
+               }
+
+               deselect();
+               editor.getBody().focus();
+               editor.selection.setCursorLocation( padNode, 0 );
+               editor.nodeChanged();
+       }
+
</ins><span class="cx">   function select( viewNode ) {
</span><span class="cx">          var clipboard,
</span><span class="cx">                  dom = editor.dom;
</span><span class="lines">@@ -80,8 +104,7 @@
</span><span class="cx">                  'contenteditable': 'true'
</span><span class="cx">          }, getViewText( viewNode ) );
</span><span class="cx"> 
</span><del>-               // Prepend inside the wrapper
-               viewNode.insertBefore( clipboard, viewNode.firstChild );
</del><ins>+                editor.dom.select( '.wpview-body', viewNode )[0].appendChild( clipboard );
</ins><span class="cx"> 
</span><span class="cx">          // Both of the following are necessary to prevent manipulating the selection/focus
</span><span class="cx">          dom.bind( clipboard, 'beforedeactivate focusin focusout', _stop );
</span><span class="lines">@@ -116,27 +139,6 @@
</span><span class="cx">          selected = null;
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       function selectSiblingView( node, direction ) {
-               var body = editor.getBody(),
-                       sibling = direction === 'previous' ? 'previousSibling' : 'nextSibling';
-
-               while ( node && node.parentNode !== body ) {
-                       if ( node[sibling] ) {
-                               // The caret will be in another element
-                               return false;
-                       }
-
-                       node = node.parentNode;
-               }
-
-               if ( isView( node[sibling] ) ) {
-                       select( node[sibling] );
-                       return true;
-               }
-
-               return false;
-       }
-
</del><span class="cx">   // Check if the `wp.mce` API exists.
</span><span class="cx">  if ( typeof wp === 'undefined' || ! wp.mce ) {
</span><span class="cx">          return;
</span><span class="lines">@@ -144,8 +146,12 @@
</span><span class="cx"> 
</span><span class="cx">  // Remove the content of view wrappers from HTML string
</span><span class="cx">  function emptyViews( content ) {
</span><del>-               return content.replace(/(<div[^>]+wpview-wrap[^>]+>)[\s\S]+?data-wpview-end[^>]*><\/ins><\/div>/g, '$1</div>' );
</del><ins>+                return content.replace(/<div[^>]+data-wpview-text=\"([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>(?:&nbsp;|\u00a0)*<\/p><\/div>/g, '$1' );
</ins><span class="cx">   }
</span><ins>+       
+       window.emptyViews = function() {
+               return emptyViews( editor.getContent({format : 'raw'}) );
+       };
</ins><span class="cx"> 
</span><span class="cx">  // Prevent adding undo levels on changes inside a view wrapper
</span><span class="cx">  editor.on( 'BeforeAddUndo', function( event ) {
</span><span class="lines">@@ -181,77 +187,61 @@
</span><span class="cx"> 
</span><span class="cx">  // When the editor's content has been updated and the DOM has been
</span><span class="cx">  // processed, render the views in the document.
</span><del>-       editor.on( 'SetContent', function( event ) {
-               var body, padNode;
-
</del><ins>+        editor.on( 'SetContent', function() {
</ins><span class="cx">           wp.mce.views.render();
</span><del>-
-               // Add padding <p> if the noneditable node is last
-               if ( event.load || ! event.set ) {
-                       body = editor.getBody();
-
-                       if ( isView( body.lastChild ) ) {
-                               padNode = createPadNode();
-                               body.appendChild( padNode );
-
-                               if ( ! event.initial ) {
-                                       editor.selection.setCursorLocation( padNode, 0 );
-                               }
-                       }
-               }
</del><span class="cx">   });
</span><span class="cx"> 
</span><del>-       // Detect mouse down events that are adjacent to a view when a view is the first view or the last view
</del><ins>+        // Set the cursor before or after a view when clicking next to it.
</ins><span class="cx">   editor.on( 'click', function( event ) {
</span><del>-               var body = editor.getBody(),
-                       doc = editor.getDoc(),
-                       scrollTop = doc.documentElement.scrollTop || body.scrollTop || 0,
-                       x, y, firstNode, lastNode, padNode;
</del><ins>+                var x = event.clientX,
+                       y = event.clientY,
+                       body = editor.getBody(),
+                       bodyRect = body.getBoundingClientRect(),
+                       first = body.firstChild,
+                       firstRect = first.getBoundingClientRect(),
+                       last = body.lastChild,
+                       lastRect = last.getBoundingClientRect(),
+                       view;
</ins><span class="cx"> 
</span><del>-               if ( event.target.nodeName === 'HTML' && ! event.metaKey && ! event.ctrlKey ) {
-                       firstNode = body.firstChild;
-                       lastNode = body.lastChild;
-                       x = event.clientX;
-                       y = event.clientY;
</del><ins>+                if ( y < firstRect.top && ( view = getView( first ) ) ) {
+                       setViewCursor( true, view );
+                       event.preventDefault();
+               } else if ( y > lastRect.bottom && ( view = getView( last ) ) ) {
+                       setViewCursor( false, view );
+                       event.preventDefault();
+               } else {
+                       tinymce.each( editor.dom.select( '.wpview-wrap' ), function( view ) {
+                               var rect = view.getBoundingClientRect();
</ins><span class="cx"> 
</span><del>-                       // Detect clicks above or to the left if the first node is a wpview
-                       if ( isView( firstNode ) && ( ( x < firstNode.offsetLeft && y < ( firstNode.offsetHeight - scrollTop ) ) ||
-                               y < firstNode.offsetTop ) ) {
-
-                               padNode = createPadNode();
-                               body.insertBefore( padNode, firstNode );
-
-                       // Detect clicks to the right and below the last view
-                       } else if ( isView( lastNode ) && ( x > ( lastNode.offsetLeft + lastNode.offsetWidth ) ||
-                               ( ( scrollTop + y ) - ( lastNode.offsetTop + lastNode.offsetHeight ) ) > 0 ) ) {
-
-                               padNode = createPadNode();
-                               body.appendChild( padNode );
-                       }
-
-                       if ( padNode ) {
-                               // Make sure that a selected view is deselected so that focus and selection are handled properly
-                               deselect();
-                               editor.getBody().focus();
-                               editor.selection.setCursorLocation( padNode, 0 );
-                       }
</del><ins>+                                if ( y >= rect.top && y <= rect.bottom ) {
+                                       if ( x < bodyRect.left ) {
+                                               setViewCursor( true, view );
+                                               event.preventDefault();
+                                       } else if ( x > bodyRect.right ) {
+                                               setViewCursor( false, view );
+                                               event.preventDefault();
+                                       }
+                                       return;
+                               }
+                       });
</ins><span class="cx">           }
</span><span class="cx">  });
</span><span class="cx"> 
</span><span class="cx">  editor.on( 'init', function() {
</span><span class="cx">          var selection = editor.selection;
</span><ins>+
</ins><span class="cx">           // When a view is selected, ensure content that is being pasted
</span><span class="cx">          // or inserted is added to a text node (instead of the view).
</span><span class="cx">          editor.on( 'BeforeSetContent', function() {
</span><span class="cx">                  var walker, target,
</span><del>-                               view = getParentView( selection.getNode() );
</del><ins>+                                view = getView( selection.getNode() );
</ins><span class="cx"> 
</span><span class="cx">                  // If the selection is not within a view, bail.
</span><span class="cx">                  if ( ! view ) {
</span><span class="cx">                          return;
</span><span class="cx">                  }
</span><span class="cx"> 
</span><del>-                       if ( ! view.nextSibling || isView( view.nextSibling ) ) {
</del><ins>+                        if ( ! view.nextSibling || getView( view.nextSibling ) ) {
</ins><span class="cx">                           // If there are no additional nodes or the next node is a
</span><span class="cx">                          // view, create a text node after the current view.
</span><span class="cx">                          target = editor.getDoc().createTextNode('');
</span><span class="lines">@@ -267,9 +257,7 @@
</span><span class="cx">                  selection.collapse( true );
</span><span class="cx">          });
</span><span class="cx"> 
</span><del>-               // When the selection's content changes, scan any new content
-               // for matching views.
-               //
</del><ins>+                // When the selection's content changes, scan any new content for matching views.
</ins><span class="cx">           // Runs on paste and on inserting nodes/html.
</span><span class="cx">          editor.on( 'SetContent', function( e ) {
</span><span class="cx">                  if ( ! e.context ) {
</span><span class="lines">@@ -286,7 +274,7 @@
</span><span class="cx">          });
</span><span class="cx"> 
</span><span class="cx">          editor.dom.bind( editor.getBody().parentNode, 'mousedown mouseup click', function( event ) {
</span><del>-                       var view = getParentView( event.target ),
</del><ins>+                        var view = getView( event.target ),
</ins><span class="cx">                           deselectEventType;
</span><span class="cx"> 
</span><span class="cx">                  // Contain clicks inside the view wrapper
</span><span class="lines">@@ -294,7 +282,7 @@
</span><span class="cx">                          event.stopPropagation();
</span><span class="cx"> 
</span><span class="cx">                          // Hack to try and keep the block resize handles from appearing. They will show on mousedown and then be removed on mouseup.
</span><del>-                               if ( tinymce.Env.ie <= 10 ) {
</del><ins>+                                if ( Env.ie <= 10 ) {
</ins><span class="cx">                                   deselect();
</span><span class="cx">                          }
</span><span class="cx"> 
</span><span class="lines">@@ -307,14 +295,14 @@
</span><span class="cx">                                          editor.dom.remove( view );
</span><span class="cx">                                  }
</span><span class="cx">                          }
</span><ins>+
</ins><span class="cx">                           // Returning false stops the ugly bars from appearing in IE11 and stops the view being selected as a range in FF.
</span><span class="cx">                          // Unfortunately, it also inhibits the dragging of views to a new location.
</span><span class="cx">                          return false;
</span><span class="cx">                  } else {
</span><del>-
</del><span class="cx">                           // Fix issue with deselecting a view in IE8. Without this hack, clicking content above the view wouldn't actually deselect it
</span><span class="cx">                          // and the caret wouldn't be placed at the mouse location
</span><del>-                               if ( tinymce.Env.ie && tinymce.Env.ie <= 8 ) {
</del><ins>+                                if ( Env.ie && Env.ie <= 8 ) {
</ins><span class="cx">                                   deselectEventType = 'mouseup';
</span><span class="cx">                          } else {
</span><span class="cx">                                  deselectEventType = 'mousedown';
</span><span class="lines">@@ -328,19 +316,8 @@
</span><span class="cx">  });
</span><span class="cx"> 
</span><span class="cx">  editor.on( 'PreProcess', function( event ) {
</span><del>-               var dom = editor.dom;
-
-               // Remove empty padding nodes
-               tinymce.each( dom.select( 'p[data-wpview-pad]', event.node ), function( node ) {
-                       if ( dom.isEmpty( node ) ) {
-                               dom.remove( node );
-                       } else {
-                               dom.setAttrib( node, 'data-wpview-pad', null );
-                       }
-               });
-
</del><span class="cx">           // Replace the wpview node with the wpview string/shortcode?
</span><del>-               tinymce.each( dom.select( 'div[data-wpview-text]', event.node ), function( node ) {
</del><ins>+                tinymce.each( editor.dom.select( 'div[data-wpview-text]', event.node ), function( node ) {
</ins><span class="cx">                   // Empty the wrap node
</span><span class="cx">                  if ( 'textContent' in node ) {
</span><span class="cx">                          node.textContent = '\u00a0';
</span><span class="lines">@@ -361,157 +338,226 @@
</span><span class="cx">          }
</span><span class="cx">  });
</span><span class="cx"> 
</span><ins>+       // (De)select views when arrow keys are used to navigate the content of the editor.
</ins><span class="cx">   editor.on( 'keydown', function( event ) {
</span><ins>+               if ( event.metaKey || event.ctrlKey || ( keyCode >= 112 && keyCode <= 123 ) ) {
+                       return;
+               }
+
+               if ( selected ) {
+                       return;
+               }
+
</ins><span class="cx">           var keyCode = event.keyCode,
</span><del>-                       body = editor.getBody(),
-                       view, padNode;
</del><ins>+                        dom = editor.dom,
+                       selection = editor.selection,
+                       node = selection.getNode(),
+                       view = getView( node ),
+                       cursorBefore, cursorAfter;
</ins><span class="cx"> 
</span><ins>+               lastKeyDownNode = node;
+
+               if ( ! view ) {
+                       return;
+               }
+
+               if ( ! ( ( cursorBefore = dom.hasClass( view, 'wpview-selection-before' ) ) ||
+                               ( cursorAfter = dom.hasClass( view, 'wpview-selection-after' ) ) ) ) {
+                       return;
+               }
+
+               if ( ( cursorAfter && keyCode === VK.UP ) || ( cursorBefore && keyCode === VK.BACKSPACE ) ) {
+                       if ( view.previousSibling ) {
+                               if ( getView( view.previousSibling ) ) {
+                                       setViewCursor( false, view.previousSibling );
+                               } else {
+                                       if ( dom.isEmpty( view.previousSibling ) && keyCode === VK.BACKSPACE ) {
+                                               dom.remove( view.previousSibling );
+                                       } else {
+                                               selection.select( view.previousSibling, true );
+                                               selection.collapse();
+                                       }
+                               }
+                       } else {
+                               handleEnter( view, true );
+                       }
+                       event.preventDefault();
+               } else if ( cursorAfter && ( keyCode === VK.DOWN || keyCode === VK.RIGHT ) ) {
+                       if ( view.nextSibling ) {
+                               if ( getView( view.nextSibling ) ) {
+                                       setViewCursor( false, view.nextSibling );
+                               } else {
+                                       selection.setCursorLocation( view.nextSibling, 0 );
+                               }
+                       } else {
+                               handleEnter( view );
+                       }
+                       event.preventDefault();
+               } else if ( cursorBefore && ( keyCode === VK.UP || keyCode ===  VK.LEFT ) ) {
+                       if ( view.previousSibling ) {
+                               if ( getView( view.previousSibling ) ) {
+                                       setViewCursor( true, view.previousSibling );
+                               } else {
+                                       selection.select( view.previousSibling, true );
+                                       selection.collapse();
+                               }
+                       } else {
+                               handleEnter( view, true );
+                       }
+                       event.preventDefault();
+               } else if ( cursorBefore && keyCode === VK.DOWN ) {
+                       if ( view.nextSibling ) {
+                               if ( getView( view.nextSibling ) ) {
+                                       setViewCursor( true, view.nextSibling );
+                               } else {
+                                       selection.setCursorLocation( view.nextSibling, 0 );
+                               }
+                       } else {
+                               handleEnter( view );
+                       }
+                       event.preventDefault();
+               } else if ( ( cursorAfter && keyCode === VK.LEFT ) || ( cursorBefore && keyCode === VK.RIGHT ) ) {
+                       select( view );
+                       event.preventDefault();
+                       event.stopImmediatePropagation();
+               } else if ( cursorAfter && keyCode === VK.BACKSPACE ) {
+                       dom.remove( view );
+                       event.preventDefault();
+               } else if ( cursorAfter ) {
+                       handleEnter( view );
+               } else if ( cursorBefore ) {
+                       handleEnter( view , true);
+               }
+
+               if ( keyCode === VK.ENTER ) {
+                       event.preventDefault();
+               }
+       });
+
+       // Handle key presses for selected views.
+       editor.on( 'keydown', function( event ) {
+               var dom = editor.dom,
+                       keyCode = event.keyCode,
+                       selection = editor.selection,
+                       view;
+
</ins><span class="cx">           // If a view isn't selected, let the event go on its merry way.
</span><span class="cx">          if ( ! selected ) {
</span><span class="cx">                  return;
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               // Let keypresses that involve the command or control keys through.
</del><ins>+                // Let key presses that involve the command or control keys through.
</ins><span class="cx">           // Also, let any of the F# keys through.
</span><span class="cx">          if ( event.metaKey || event.ctrlKey || ( keyCode >= 112 && keyCode <= 123 ) ) {
</span><del>-                       if ( ( event.metaKey || event.ctrlKey ) && keyCode === 88 ) {
-                               toRemove = selected;
</del><ins>+                        // But remove the view when cmd/ctrl + x/backspace are pressed.
+                       if ( ( event.metaKey || event.ctrlKey ) && ( keyCode === 88 || keyCode === VK.BACKSPACE ) ) {
+                               // We'll remove a cut view on keyup, otherwise the browser can't copy the content.
+                               if ( keyCode === 88 ) {
+                                       toRemove = selected;
+                               } else {
+                                       editor.dom.remove( selected );
+                               }
</ins><span class="cx">                   }
</span><span class="cx">                  return;
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               view = getParentView( editor.selection.getNode() );
</del><ins>+                view = getView( selection.getNode() );
</ins><span class="cx"> 
</span><del>-               // If the caret is not within the selected view, deselect the
-               // view and bail.
</del><ins>+                // If the caret is not within the selected view, deselect the view and bail.
</ins><span class="cx">           if ( view !== selected ) {
</span><span class="cx">                  deselect();
</span><span class="cx">                  return;
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               // Deselect views with the arrow keys
</del><span class="cx">           if ( keyCode === VK.LEFT || keyCode === VK.UP ) {
</span><ins>+                       setViewCursor( true, view );
</ins><span class="cx">                   deselect();
</span><del>-                       // Handle case where two views are stacked on top of one another
-                       if ( isView( view.previousSibling ) ) {
-                               select( view.previousSibling );
-                       // Handle case where view is the first node
-                       } else if ( ! view.previousSibling ) {
-                               padNode = createPadNode();
-                               body.insertBefore( padNode, body.firstChild );
-                               editor.selection.setCursorLocation( body.firstChild, 0 );
-                       // Handle default case
-                       } else {
-                               editor.selection.select( view.previousSibling, true );
-                               editor.selection.collapse();
-                       }
</del><span class="cx">           } else if ( keyCode === VK.RIGHT || keyCode === VK.DOWN ) {
</span><ins>+                       setViewCursor( false, view );
</ins><span class="cx">                   deselect();
</span><del>-                       // Handle case where the next node is another wpview
-                       if ( isView( view.nextSibling ) ) {
-                               select( view.nextSibling );
-                       // Handle case were the view is that last node
-                       } else if ( ! view.nextSibling ) {
-                               padNode = createPadNode();
-                               body.appendChild( padNode );
-                               editor.selection.setCursorLocation( body.lastChild, 0 );
-                       // Handle default case where the next node is a non-wpview
-                       } else {
-                               editor.selection.setCursorLocation( view.nextSibling, 0 );
-                       }
</del><ins>+                } else if ( keyCode === VK.ENTER ) {
+                       handleEnter( view );
</ins><span class="cx">           } else if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
</span><del>-                       // If delete or backspace is pressed, delete the view.
-                       editor.dom.remove( selected );
</del><ins>+                        dom.remove( selected );
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          event.preventDefault();
</span><span class="cx">  });
</span><span class="cx"> 
</span><del>-       // Select views when arrow keys are used to navigate the content of the editor.
</del><ins>+        // Make sure we don't eat any content.
</ins><span class="cx">   editor.on( 'keydown', function( event ) {
</span><del>-               var keyCode = event.keyCode,
-                       dom = editor.dom,
-                       range = editor.selection.getRng(),
-                       startNode = range.startContainer,
-                       body = editor.getBody(),
-                       node, container;
</del><ins>+                var selection = editor.selection,
+                       node, range, view;
</ins><span class="cx"> 
</span><del>-               if ( ! startNode || startNode === body || event.metaKey || event.ctrlKey ) {
-                       return;
-               }
</del><ins>+                if ( event.keyCode === VK.BACKSPACE ) {
+                       node = selection.getNode();
</ins><span class="cx"> 
</span><del>-               if ( keyCode === VK.UP || keyCode === VK.LEFT ) {
-                       if ( keyCode === VK.LEFT && ( ! range.collapsed || range.startOffset !== 0 ) ) {
-                               // Not at the beginning of the current range
-                               return;
-                       }
-
-                       if ( ! ( node = dom.getParent( startNode, dom.isBlock ) ) ) {
-                               return;
-                       }
-
-                       if ( selectSiblingView( node, 'previous' ) ) {
-                               event.preventDefault();
-                       }
-               } else if ( keyCode === VK.DOWN || keyCode === VK.RIGHT ) {
-                       if ( ! ( node = dom.getParent( startNode, dom.isBlock ) ) ) {
-                               return;
-                       }
-
-                       if ( keyCode === VK.RIGHT ) {
-                               container = range.endContainer;
-
-                               if ( ! range.collapsed || ( range.startOffset === 0 && container.length ) ||
-                                       container.nextSibling ||
-                                       ( container.nodeType === 3 && range.startOffset !== container.length ) ) { // Not at the end of the current range
-
-                                       return;
</del><ins>+                        if ( editor.dom.isEmpty( node ) ) {
+                               if ( view = getView( node.previousSibling ) ) {
+                                       setViewCursor( false, view );
+                                       editor.dom.remove( node );
+                                       event.preventDefault();
</ins><span class="cx">                           }
</span><del>-
-                               // In a child element
-                               while ( container && container !== node && container !== body ) {
-                                       if ( container.nextSibling ) {
-                                               return;
-                                       }
-                                       container = container.parentNode;
-                               }
-                       }
-
-                       if ( selectSiblingView( node, 'next' ) ) {
</del><ins>+                        } else if ( ( range = selection.getRng() ) &&
+                                       range.startOffset === 0 &&
+                                       range.endOffset === 0 &&
+                                       ( view = getView( node.previousSibling ) ) ) {
+                               setViewCursor( false, view );
</ins><span class="cx">                           event.preventDefault();
</span><span class="cx">                  }
</span><span class="cx">          }
</span><span class="cx">  });
</span><span class="cx"> 
</span><del>-       editor.on( 'keyup', function( event ) {
-               var padNode,
-                       keyCode = event.keyCode,
-                       body = editor.getBody(),
-                       range;
-
</del><ins>+        editor.on( 'keyup', function() {
</ins><span class="cx">           if ( toRemove ) {
</span><span class="cx">                  editor.dom.remove( toRemove );
</span><span class="cx">                  toRemove = false;
</span><span class="cx">          }
</span><ins>+       });
</ins><span class="cx"> 
</span><del>-               if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
-                       // Make sure there is padding if the last element is a view
-                       if ( isView( body.lastChild ) ) {
-                               padNode = createPadNode();
-                               body.appendChild( padNode );
</del><ins>+        editor.on( 'nodechange', function( event ) {
+               var dom = editor.dom,
+                       views = editor.dom.select( '.wpview-wrap' ),
+                       className = event.element.className,
+                       view = getView( event.element ),
+                       lKDN = lastKeyDownNode;
</ins><span class="cx"> 
</span><del>-                               if ( body.childNodes.length === 2 ) {
-                                       editor.selection.setCursorLocation( padNode, 0 );
</del><ins>+                lastKeyDownNode = false;
+
+               clearInterval( cursorInterval );
+
+               dom.removeClass( views, 'wpview-selection-before' );
+               dom.removeClass( views, 'wpview-selection-after' );
+               dom.removeClass( views, 'wpview-cursor-hide' );
+
+               if ( view ) {
+                       if ( className === 'wpview-selection-before' || className === 'wpview-selection-after' ) {
+                               setViewCursorTries = 0;
+
+                               // Make sure the cursor arrived in the right node.
+                               // This is necessary for Firefox.
+                               if ( lKDN === view.previousSibling ) {
+                                       setViewCursor( true, view );
+                                       return;
+                               } else if ( lKDN === view.nextSibling ) {
+                                       setViewCursor( false, view );
+                                       return;
</ins><span class="cx">                           }
</span><del>-                       }
</del><span class="cx"> 
</span><del>-                       range = editor.selection.getRng();
</del><ins>+                                dom.addClass( view, className );
</ins><span class="cx"> 
</span><del>-                       // Allow an initial element in the document to be removed when it is before a view
-                       if ( body.firstChild === range.startContainer && range.collapsed === true &&
-                                       isView( range.startContainer.nextSibling ) && range.startOffset === 0 ) {
-
-                               editor.dom.remove( range.startContainer );
</del><ins>+                                cursorInterval = setInterval( function() {
+                                       if ( dom.hasClass( view, 'wpview-cursor-hide' ) ) {
+                                               dom.removeClass( view, 'wpview-cursor-hide' );
+                                       } else {
+                                               dom.addClass( view, 'wpview-cursor-hide' );
+                                       }
+                               }, 500 );
+                       // If the cursor happens to be anywhere around the view, then set the cursor properly.
+                       // Only try this once to prevent a loop. (You never know.)
+                       } else if ( ! selected && ! setViewCursorTries ) {
+                               setViewCursorTries++;
+                               setViewCursor( true, view );
</ins><span class="cx">                   }
</span><span class="cx">          }
</span><span class="cx">  });
</span></span></pre></div>
<a id="trunksrcwpincludesjstinymceskinswordpresswpcontentcss"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/js/tinymce/skins/wordpress/wp-content.css (28993 => 28994)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/js/tinymce/skins/wordpress/wp-content.css  2014-07-04 03:38:33 UTC (rev 28993)
+++ trunk/src/wp-includes/js/tinymce/skins/wordpress/wp-content.css     2014-07-04 03:58:20 UTC (rev 28994)
</span><span class="lines">@@ -210,7 +210,8 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /* hide the shortcode content, but allow the content to still be selected */
</span><del>-.wpview-wrap .wpview-clipboard {
</del><ins>+.wpview-wrap .wpview-clipboard,
+.wpview-wrap > p {
</ins><span class="cx">   position: absolute;
</span><span class="cx">  top: 0;
</span><span class="cx">  left: 0;
</span><span class="lines">@@ -225,14 +226,47 @@
</span><span class="cx">  height: 1px;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/* An ugly box will appear when this is focussed in IE, so we'll move it outside the window. */
+.wpview-wrap.wpview-selection-before > p,
+.wpview-wrap.wpview-selection-after > p {
+       left: -10000px;
+}
+
</ins><span class="cx"> .wpview-wrap .wpview-clipboard,
</span><del>-.wpview-wrap .wpview-clipboard * {
</del><ins>+.wpview-wrap .wpview-clipboard *,
+.wpview-wrap > p {
</ins><span class="cx">   -moz-user-select: text;
</span><span class="cx">  -webkit-user-select: text;
</span><span class="cx">  -ms-user-select: text;
</span><span class="cx">  user-select: text;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+.wpview-wrap.wpview-selection-before:before,
+.wpview-wrap.wpview-selection-after:before {
+       content: '';
+       margin: 0;
+       padding: 0;
+       position: absolute;
+       top: -2px;
+       left: -3px;
+       bottom: -2px;
+       width: 1px;
+       background-color: black;
+       -webkit-transition: opacity 0.15s ease-out;
+       -moz-transition: opacity 0.15s ease-out;
+       transition: opacity 0.15s ease-out;
+       opacity: 1;
+}
+
+.wpview-wrap.wpview-selection-after:before {
+       left: auto;
+       right: -3px;
+}
+
+.wpview-wrap.wpview-cursor-hide:before {
+       opacity: 0;
+}
+
</ins><span class="cx"> /**
</span><span class="cx">  * Media previews
</span><span class="cx">  */
</span><span class="lines">@@ -348,16 +382,16 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /* Audio player is short; therefore let's put the toolbar above */
</span><del>-.wpview-type-audio .toolbar {
</del><ins>+.wpview-wrap[data-wpview-type="audio"] .toolbar {
</ins><span class="cx">   top: auto;
</span><span class="cx">  bottom: -34px;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-.wpview-type-audio .toolbar div {
</del><ins>+.wpview-wrap[data-wpview-type="audio"] .toolbar div {
</ins><span class="cx">   margin-top: 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-.wpview-type-audio .toolbar div:first-child {
</del><ins>+.wpview-wrap[data-wpview-type="audio"] .toolbar div:first-child {
</ins><span class="cx">   margin-left: 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>