From 7b1b146620b574c643623a4eee724da2e9817427 Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 19 Feb 2016 13:41:03 -0800 Subject: [PATCH] Save, then restore scroll position in Chrome textareas on remarkup assist Summary: Fixes T10396. Seems like this has been around for a while (references from 2011): http://stackoverflow.com/questions/4002312/chrome-resets-the-textarea-scroll-bar-scrolltop-when-focus-is-called https://bugs.chromium.org/p/chromium/issues/detail?id=75072 Commenting out this `focus()` seemed to fix the issue locally, at the cost of not focusing. Saving, focusing, then restoring seems to produce the correct behavior everywhere. Test Plan: - In Safari, Firefox and Chrome, typed a ton of text into a remarkup area (more than the height of the area, so it has a scrollbar). - Selected some text near the top. - Clicked "B" to bold the text. - Scroll position remained the same in all browsers (previously: in Chrome, it changed). Reviewers: chad Reviewed By: chad Maniphest Tasks: T10396 Differential Revision: https://secure.phabricator.com/D15313 --- resources/celerity/map.php | 16 ++++++++-------- webroot/rsrc/js/core/TextAreaUtils.js | 7 +++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/resources/celerity/map.php b/resources/celerity/map.php index bfac438b60..9a9d5cd527 100644 --- a/resources/celerity/map.php +++ b/resources/celerity/map.php @@ -8,7 +8,7 @@ return array( 'names' => array( 'core.pkg.css' => '7935f211', - 'core.pkg.js' => '298d5888', + 'core.pkg.js' => '7d8faf57', 'darkconsole.pkg.js' => 'e7393ebb', 'differential.pkg.css' => '2de124c9', 'differential.pkg.js' => 'd0cd0df6', @@ -462,7 +462,7 @@ return array( 'rsrc/js/core/Notification.js' => 'ccf1cbf8', 'rsrc/js/core/Prefab.js' => 'e67df814', 'rsrc/js/core/ShapedRequest.js' => '7cbe244b', - 'rsrc/js/core/TextAreaUtils.js' => '9e54692d', + 'rsrc/js/core/TextAreaUtils.js' => '5813016a', 'rsrc/js/core/Title.js' => 'df5e11d2', 'rsrc/js/core/ToolTip.js' => '6323f942', 'rsrc/js/core/behavior-active-nav.js' => 'e379b58e', @@ -775,7 +775,7 @@ return array( 'phabricator-slowvote-css' => 'da0afb1b', 'phabricator-source-code-view-css' => 'cbeef983', 'phabricator-standard-page-view' => 'e709f6d0', - 'phabricator-textareautils' => '9e54692d', + 'phabricator-textareautils' => '5813016a', 'phabricator-title' => 'df5e11d2', 'phabricator-tooltip' => '6323f942', 'phabricator-ui-example-css' => '528b19de', @@ -1275,6 +1275,11 @@ return array( 'javelin-request', 'javelin-util', ), + '5813016a' => array( + 'javelin-install', + 'javelin-dom', + 'javelin-vector', + ), '59a7976a' => array( 'javelin-install', 'javelin-dom', @@ -1621,11 +1626,6 @@ return array( 'phabricator-phtize', 'changeset-view-manager', ), - '9e54692d' => array( - 'javelin-install', - 'javelin-dom', - 'javelin-vector', - ), '9f36c42d' => array( 'javelin-behavior', 'javelin-stratcom', diff --git a/webroot/rsrc/js/core/TextAreaUtils.js b/webroot/rsrc/js/core/TextAreaUtils.js index f8c84c97a7..8f8a81067b 100644 --- a/webroot/rsrc/js/core/TextAreaUtils.js +++ b/webroot/rsrc/js/core/TextAreaUtils.js @@ -33,7 +33,14 @@ JX.install('TextAreaUtils', { setSelectionRange : function(area, start, end) { if ('setSelectionRange' in area) { + + // Chrome scrolls the textarea to the bottom as a side effect of + // calling focus(), so save the scroll position, focus, then restore + // the scroll position. + var scroll_top = area.scrollTop; area.focus(); + area.scrollTop = scroll_top; + area.setSelectionRange(start, end); } },