From fd6bbc69e2d9a1ca71e4c8e56a974a65482ce369 Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Tue, 29 Dec 2015 14:59:12 -0500
Subject: [PATCH] FIX: On chrome, focusing on a recently changed textarea would
 scroll

---
 .../discourse/components/d-editor.js.es6      |  7 +++++--
 .../components/d-editor-test.js.es6           | 20 +++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6
index 2bf785d33b4..ceaad81bb52 100644
--- a/app/assets/javascripts/discourse/components/d-editor.js.es6
+++ b/app/assets/javascripts/discourse/components/d-editor.js.es6
@@ -317,12 +317,15 @@ export default Ember.Component.extend({
 
   _selectText(from, length) {
     Ember.run.scheduleOnce('afterRender', () => {
-      const textarea = this.$('textarea.d-editor-input')[0];
+      const $textarea = this.$('textarea.d-editor-input');
+      const textarea = $textarea[0];
+      const oldScrollPos = $textarea.scrollTop();
       if (!this.capabilities.isIOS) {
-        textarea.focus();
+        $textarea.focus();
       }
       textarea.selectionStart = from;
       textarea.selectionEnd = textarea.selectionStart + length;
+      $textarea.scrollTop(oldScrollPos);
     });
   },
 
diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6
index 8b7f23350b7..e43fdf686a5 100644
--- a/test/javascripts/components/d-editor-test.js.es6
+++ b/test/javascripts/components/d-editor-test.js.es6
@@ -516,6 +516,26 @@ testCase(`rule with a selection`, function(assert, textarea) {
   });
 });
 
+testCase(`doesn't jump to bottom with long text`, function(assert, textarea) {
+
+  let longText = 'hello world.';
+  for (let i=0; i<8; i++) {
+    longText = longText + longText;
+  }
+  this.set('value', longText);
+
+  andThen(() => {
+    $(textarea).scrollTop(0);
+    textarea.selectionStart = 3;
+    textarea.selectionEnd = 3;
+  });
+
+  click('button.bold');
+  andThen(() => {
+    assert.equal($(textarea).scrollTop(), 0, 'it stays scrolled up');
+  });
+});
+
 componentTest('emoji', {
   template: '{{d-editor value=value}}',
   setup() {