<!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>[29495] trunk/src/wp-admin: Editor scrolling:</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/29495">29495</a></dd>
<dt>Author</dt> <dd>helen</dd>
<dt>Date</dt> <dd>2014-08-15 03:16:07 +0000 (Fri, 15 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Editor scrolling:

* Reduce the starting height of the editor to better match the height at which the top toolbar unpins.
* Pin the side metaboxes so they do not get lost when the editor content is long.
* Turn magic numbers into variables.

props stephdau, azaozz. see <a href="http://core.trac.wordpress.org/ticket/28328">#28328</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadmincsseditcss">trunk/src/wp-admin/css/edit.css</a></li>
<li><a href="#trunksrcwpadminjseditorexpandjs">trunk/src/wp-admin/js/editor-expand.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadmincsseditcss"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/css/edit.css (29494 => 29495)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/css/edit.css  2014-08-15 01:12:32 UTC (rev 29494)
+++ trunk/src/wp-admin/css/edit.css     2014-08-15 03:16:07 UTC (rev 29495)
</span><span class="lines">@@ -405,6 +405,10 @@
</span><span class="cx"> }
</span><span class="cx"> /* end editor-expand.js override */
</span><span class="cx"> 
</span><ins>+#poststuff #post-body.columns-2 #side-sortables {
+       width: 280px;
+}
+
</ins><span class="cx"> #timestampdiv select {
</span><span class="cx">  height: 21px;
</span><span class="cx">  line-height: 14px;
</span><span class="lines">@@ -1229,6 +1233,7 @@
</span><span class="cx"> 
</span><span class="cx">  #poststuff #post-body.columns-2 #side-sortables {
</span><span class="cx">          min-height: 0;
</span><ins>+               width: auto;
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  /* hide the radio buttons for column prefs */
</span></span></pre></div>
<a id="trunksrcwpadminjseditorexpandjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/js/editor-expand.js (29494 => 29495)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/js/editor-expand.js   2014-08-15 01:12:32 UTC (rev 29494)
+++ trunk/src/wp-admin/js/editor-expand.js      2014-08-15 03:16:07 UTC (rev 29495)
</span><span class="lines">@@ -6,6 +6,7 @@
</span><span class="cx">  var $window = $( window ),
</span><span class="cx">          $document = $( document ),
</span><span class="cx">          $adminBar = $( '#wpadminbar' ),
</span><ins>+               $footer = $( '#wpfooter' ),
</ins><span class="cx">           $wrap = $( '#postdivrich' ),
</span><span class="cx">          $contentWrap = $( '#wp-content-wrap' ),
</span><span class="cx">          $tools = $( '#wp-content-editor-tools' ),
</span><span class="lines">@@ -16,13 +17,23 @@
</span><span class="cx">          $textEditorClone = $( '<div id="content-textarea-clone"></div>' ),
</span><span class="cx">          $bottom = $( '#post-status-info' ),
</span><span class="cx">          $statusBar,
</span><ins>+               $sideSortables = $( '#side-sortables' ),
+               $postboxContainer = $( '#postbox-container-1' ),
+               $postBody = $('#post-body'),
</ins><span class="cx">           fullscreen = window.wp.editor && window.wp.editor.fullscreen,
</span><span class="cx">          mceEditor,
</span><span class="cx">          mceBind = function(){},
</span><span class="cx">          mceUnbind = function(){},
</span><span class="cx">          fixedTop = false,
</span><span class="cx">          fixedBottom = false,
</span><del>-               scrollTimer;
</del><ins>+                fixedSideTop = false,
+               fixedSideBottom = false,
+               scrollTimer,
+               lastScrollPosition = 0,
+               pageYOffsetAtTop = 130,
+               pinnedToolsTop = 56, // also used in CSS for the "#poststuff #post-body #postbox-container-1.pinned" selector
+               textEditorClonePaddingTop = 37,
+               autoresizeMinHeight = 300; // $window.height() - 310;
</ins><span class="cx"> 
</span><span class="cx">  $textEditorClone.insertAfter( $textEditor );
</span><span class="cx"> 
</span><span class="lines">@@ -31,7 +42,7 @@
</span><span class="cx">          'font-size': $textEditor.css( 'font-size' ),
</span><span class="cx">          'line-height': $textEditor.css( 'line-height' ),
</span><span class="cx">          'padding': $textEditor.css( 'padding' ),
</span><del>-               'padding-top': 37,
</del><ins>+                'padding-top': textEditorClonePaddingTop,
</ins><span class="cx">           'white-space': 'pre-wrap',
</span><span class="cx">          'word-wrap': 'break-word'
</span><span class="cx">  } );
</span><span class="lines">@@ -88,8 +99,8 @@
</span><span class="cx"> 
</span><span class="cx">          hiddenHeight = $textEditorClone.height();
</span><span class="cx"> 
</span><del>-               if ( hiddenHeight < 300 ) {
-                       hiddenHeight = 300;
</del><ins>+                if ( hiddenHeight < autoresizeMinHeight ) {
+                       hiddenHeight = autoresizeMinHeight;
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          if ( hiddenHeight === textEditorHeight ) {
</span><span class="lines">@@ -112,7 +123,7 @@
</span><span class="cx">          mceEditor = editor;
</span><span class="cx"> 
</span><span class="cx">          // Set the minimum height to the initial viewport height.
</span><del>-               editor.settings.autoresize_min_height = 300;
</del><ins>+                editor.settings.autoresize_min_height = autoresizeMinHeight;
</ins><span class="cx"> 
</span><span class="cx">          // Get the necessary UI elements.
</span><span class="cx">          $visualTop = $contentWrap.find( '.mce-toolbar-grp' );
</span><span class="lines">@@ -218,8 +229,12 @@
</span><span class="cx">                  adminBarHeight = windowWidth > 600 ? $adminBar.height() : 0,
</span><span class="cx">                  resize = type !== 'scroll',
</span><span class="cx">                  visual = ( mceEditor && ! mceEditor.isHidden() ),
</span><del>-                       buffer = 200,
-                       $top, $editor,
</del><ins>+                        buffer = autoresizeMinHeight + adminBarHeight,
+                       postBodyTop = $postBody.offset().top,
+                       borderWidth = 1,
+                       contentWrapWidth = $contentWrap.width(),
+                       sideSortablesHeight = $sideSortables.height(),
+                       $top, $editor, sidebarTop, footerTop,
</ins><span class="cx">                   toolsHeight, topPos, topHeight, editorPos, editorHeight, editorWidth, statusBarHeight;
</span><span class="cx"> 
</span><span class="cx">          if ( visual ) {
</span><span class="lines">@@ -238,25 +253,27 @@
</span><span class="cx">          editorWidth = $editor.outerWidth();
</span><span class="cx">          statusBarHeight = visual ? $statusBar.outerHeight() : 0;
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx">           // Maybe pin the top.
</span><span class="cx">          if ( ( ! fixedTop || resize ) &&
</span><del>-                               // Handle scrolling down.
-                               ( windowPos >= ( topPos - toolsHeight - adminBarHeight ) &&
-                               // Handle scrolling up.
-                               windowPos <= ( topPos - toolsHeight - adminBarHeight + editorHeight - buffer ) ) ) {
</del><ins>+                        // Handle scrolling down.
+                       ( windowPos >= ( topPos - toolsHeight - adminBarHeight ) &&
+                       // Handle scrolling up.
+                       windowPos <= ( topPos - toolsHeight - adminBarHeight + editorHeight - buffer ) ) ) {
+
</ins><span class="cx">                   fixedTop = true;
</span><span class="cx"> 
</span><span class="cx">                  $top.css( {
</span><span class="cx">                          position: 'fixed',
</span><span class="cx">                          top: adminBarHeight + toolsHeight,
</span><del>-                               width: $editor.parent().width() - ( $top.outerWidth() - $top.width() ),
</del><ins>+                                width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) ),
</ins><span class="cx">                           borderTop: '1px solid #e5e5e5'
</span><span class="cx">                  } );
</span><span class="cx"> 
</span><span class="cx">                  $tools.css( {
</span><span class="cx">                          position: 'fixed',
</span><span class="cx">                          top: adminBarHeight,
</span><del>-                               width: editorWidth + 2
</del><ins>+                                width: contentWrapWidth
</ins><span class="cx">                   } );
</span><span class="cx">          // Maybe unpin the top.
</span><span class="cx">          } else if ( fixedTop || resize ) {
</span><span class="lines">@@ -268,13 +285,13 @@
</span><span class="cx">                                  position: 'absolute',
</span><span class="cx">                                  top: 0,
</span><span class="cx">                                  borderTop: 'none',
</span><del>-                                       width: $editor.parent().width() - ( $top.outerWidth() - $top.width() )
</del><ins>+                                        width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
</ins><span class="cx">                           } );
</span><span class="cx"> 
</span><span class="cx">                          $tools.css( {
</span><span class="cx">                                  position: 'absolute',
</span><span class="cx">                                  top: 0,
</span><del>-                                       width: $contentWrap.width()
</del><ins>+                                        width: contentWrapWidth
</ins><span class="cx">                           } );
</span><span class="cx">                  // Handle scrolling down.
</span><span class="cx">                  } else if ( windowPos >= ( topPos - toolsHeight - adminBarHeight + editorHeight - buffer ) ) {
</span><span class="lines">@@ -282,31 +299,33 @@
</span><span class="cx"> 
</span><span class="cx">                          $top.css( {
</span><span class="cx">                                  position: 'absolute',
</span><del>-                                       top: editorHeight - buffer
</del><ins>+                                        top: editorHeight - buffer,
+                                       width: contentWrapWidth - ( borderWidth * 2 ) - ( visual ? 0 : ( $top.outerWidth() - $top.width() ) )
</ins><span class="cx">                           } );
</span><span class="cx"> 
</span><span class="cx">                          $tools.css( {
</span><span class="cx">                                  position: 'absolute',
</span><del>-                                       top: editorHeight - buffer + 1, // border
-                                       width: $contentWrap.width()
</del><ins>+                                        top: editorHeight - buffer + borderWidth, // border
+                                       width: contentWrapWidth
</ins><span class="cx">                           } );
</span><span class="cx">                  }
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="cx">          // Maybe adjust the bottom bar.
</span><span class="cx">          if ( ( ! fixedBottom || resize ) &&
</span><del>-                               // + 1 for the border around the .wp-editor-container.
-                               ( windowPos + windowHeight ) <= ( editorPos + editorHeight + bottomHeight + statusBarHeight + 1 ) ) {
</del><ins>+                        // +[n] for the border around the .wp-editor-container.
+                       ( windowPos + windowHeight ) <= ( editorPos + editorHeight + bottomHeight + statusBarHeight + borderWidth ) ) {
+
</ins><span class="cx">                   fixedBottom = true;
</span><span class="cx"> 
</span><span class="cx">                  $bottom.css( {
</span><span class="cx">                          position: 'fixed',
</span><span class="cx">                          bottom: 0,
</span><del>-                               width: editorWidth + 2,
</del><ins>+                                width: editorWidth + ( borderWidth * 2 ),
</ins><span class="cx">                           borderTop: '1px solid #dedede'
</span><span class="cx">                  } );
</span><span class="cx">          } else if ( ( fixedBottom || resize ) &&
</span><del>-                               ( windowPos + windowHeight ) > ( editorPos + editorHeight + bottomHeight + statusBarHeight - 1 ) ) {
</del><ins>+                                ( windowPos + windowHeight ) > ( editorPos + editorHeight + bottomHeight + statusBarHeight - borderWidth ) ) {
</ins><span class="cx">                   fixedBottom = false;
</span><span class="cx"> 
</span><span class="cx">                  $bottom.css( {
</span><span class="lines">@@ -317,6 +336,95 @@
</span><span class="cx">                  } );
</span><span class="cx">          }
</span><span class="cx"> 
</span><ins>+               // Sidebar pinning
+               if ( $postboxContainer.width() < 300 && windowWidth > 600 && // sidebar position is changed with @media from CSS, make sure it is on the side
+                       $document.height() > ( $sideSortables.height() + postBodyTop + 120 ) && // the sidebar is not the tallest element
+                       windowHeight < editorHeight * 0.7 ) { // the editor is taller than the viewport
+
+                       if ( sideSortablesHeight > windowHeight || fixedSideTop || fixedSideBottom ) {
+                               // Reset when scrolling to the top
+                               if ( windowPos + pinnedToolsTop <= postBodyTop ) {
+                                       $sideSortables.attr( 'style', '' );
+                                       fixedSideTop = fixedSideBottom = false;
+                               } else {
+                                       if ( windowPos > lastScrollPosition ) {
+                                               // Scrolling down
+                                               if ( fixedSideTop ) {
+                                                       // let it scroll
+                                                       fixedSideTop = false;
+                                                       sidebarTop = $sideSortables.offset().top - adminBarHeight;
+                                                       footerTop = $footer.offset().top;
+
+                                                       // don't get over the footer
+                                                       if ( footerTop < sidebarTop + sideSortablesHeight + 20 ) {
+                                                               sidebarTop = footerTop - sideSortablesHeight - 20;
+                                                       }
+
+                                                       $sideSortables.css({
+                                                               position: 'absolute',
+                                                               top: sidebarTop,
+                                                               bottom: ''
+                                                       });
+                                               } else if ( ! fixedSideBottom && sideSortablesHeight + $sideSortables.offset().top + 20 < windowPos + windowHeight ) {
+                                                       // pin the bottom
+                                                       fixedSideBottom = true;
+
+                                                       $sideSortables.css({
+                                                               position: 'fixed',
+                                                               top: 'auto',
+                                                               bottom: '20px'
+                                                       });
+                                               }
+                                       } else if ( windowPos < lastScrollPosition ) {
+                                               // Scrolling up
+                                               if ( fixedSideBottom ) {
+                                                       // let it scroll
+                                                       fixedSideBottom = false;
+                                                       sidebarTop = $sideSortables.offset().top - 20;
+                                                       footerTop = $footer.offset().top;
+
+                                                       // don't get over the footer
+                                                       if ( footerTop < sidebarTop + sideSortablesHeight + 20 ) {
+                                                               sidebarTop = footerTop - sideSortablesHeight - 20;
+                                                       }
+
+                                                       $sideSortables.css({
+                                                               position: 'absolute',
+                                                               top: sidebarTop,
+                                                               bottom: ''
+                                                       });
+                                               } else if ( ! fixedSideTop && $sideSortables.offset().top >= windowPos + pinnedToolsTop ) {
+                                                       // pin the top
+                                                       fixedSideTop = true;
+
+                                                       $sideSortables.css({
+                                                               position: 'fixed',
+                                                               top: pinnedToolsTop,
+                                                               bottom: ''
+                                                       });
+                                               }
+                                       }
+                               }
+                       } else {
+                               // if the sidebar container is smaller than the viewport, then pin/unpin the top when scrolling
+                               if ( windowPos >= ( postBodyTop - pinnedToolsTop ) ) {
+                                       $sideSortables.css( {
+                                               position: 'fixed',
+                                               top: pinnedToolsTop
+                                       } );
+                               } else {
+                                       $sideSortables.attr( 'style', '' );
+                               }
+
+                               fixedSideTop = fixedSideBottom = false;
+                       }
+
+                       lastScrollPosition = windowPos;
+               } else {
+                       $sideSortables.attr( 'style', '' );
+                       fixedSideTop = fixedSideBottom = false;
+               }
+
</ins><span class="cx">           if ( resize ) {
</span><span class="cx">                  $contentWrap.css( {
</span><span class="cx">                          paddingTop: $tools.outerHeight()
</span><span class="lines">@@ -354,7 +462,7 @@
</span><span class="cx">  function on() {
</span><span class="cx">          // Scroll to the top when triggering this from JS.
</span><span class="cx">          // Ensures toolbars are pinned properly.
</span><del>-               if ( window.pageYOffset && window.pageYOffset > 130 ) {
</del><ins>+                if ( window.pageYOffset && window.pageYOffset > pageYOffsetAtTop ) {
</ins><span class="cx">                   window.scrollTo( window.pageXOffset, 0 );
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="lines">@@ -397,7 +505,7 @@
</span><span class="cx"> 
</span><span class="cx">          // Scroll to the top when triggering this from JS.
</span><span class="cx">          // Ensures toolbars are reset properly.
</span><del>-               if ( window.pageYOffset && window.pageYOffset > 130 ) {
</del><ins>+                if ( window.pageYOffset && window.pageYOffset > pageYOffsetAtTop ) {
</ins><span class="cx">                   window.scrollTo( window.pageXOffset, 0 );
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="lines">@@ -417,10 +525,12 @@
</span><span class="cx">          fullscreen && fullscreen.pubsub.unsubscribe( 'hidden', fullscreenHide );
</span><span class="cx"> 
</span><span class="cx">          // Reset all css
</span><del>-               $.each( [ $visualTop, $textTop, $tools, $bottom, $contentWrap, $visualEditor, $textEditor ], function( i, element ) {
</del><ins>+                $.each( [ $visualTop, $textTop, $tools, $bottom, $contentWrap, $visualEditor, $textEditor, $sideSortables ], function( i, element ) {
</ins><span class="cx">                   element && element.attr( 'style', '' );
</span><span class="cx">          });
</span><span class="cx"> 
</span><ins>+               fixedTop = fixedBottom = fixedSideTop = fixedSideBottom = false;
+
</ins><span class="cx">           if ( mceEditor ) {
</span><span class="cx">                  mceEditor.settings.wp_autoresize_on = false;
</span><span class="cx">                  mceEditor.execCommand( 'wpAutoResizeOff' );
</span></span></pre>
</div>
</div>

</body>
</html>