From 252a98b231eac7b94581cc640a7e61f043729d60 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 31 Oct 2017 13:55:40 -0600 Subject: [PATCH] Prevent the browser from auto-scrolling the scroll container on spacebar --- src/text-editor-component.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/text-editor-component.js b/src/text-editor-component.js index b67b45f83..4c639e532 100644 --- a/src/text-editor-component.js +++ b/src/text-editor-component.js @@ -1616,11 +1616,23 @@ class TextEditorComponent { if (this.isInputEnabled()) { event.stopPropagation() - // WARNING: If we call preventDefault on the input of a space character, - // then the browser interprets the spacebar keypress as a page-down command, - // causing spaces to scroll elements containing editors. This is impossible - // to test. - if (event.data !== ' ') event.preventDefault() + // WARNING: If we call preventDefault on the input of a space + // character, then the browser interprets the spacebar keypress as a + // page-down command, causing spaces to scroll elements containing + // editors. This means typing space will actually change the contents + // of the hidden input, which will cause the browser to autoscroll the + // scroll container to reveal the input if it is off screen (See + // https://github.com/atom/atom/issues/16046). To correct for this + // situation, we automatically reset the scroll position to 0,0 after + // typing a space. None of this can really be tested. + if (event.data === ' ') { + window.setImmediate(() => { + this.refs.scrollContainer.scrollTop = 0 + this.refs.scrollContainer.scrollLeft = 0 + }) + } else { + event.preventDefault() + } // If the input event is fired while the accented character menu is open it // means that the user has chosen one of the accented alternatives. Thus, we