From 5a9ac5ce7d825448d89e312286cd849944368a64 Mon Sep 17 00:00:00 2001 From: eight Date: Fri, 15 Sep 2017 07:47:44 +0800 Subject: [PATCH] Add: scroll the editor when cursor is outside of the editor --- edit/source-editor.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/edit/source-editor.js b/edit/source-editor.js index 6aacd0fc..1082ee41 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -41,6 +41,38 @@ function createSourceEditor(style) { initLint(); initAppliesToReport(cm); + window.addEventListener('wheel', e => { + if (e.target.closest('.CodeMirror') || parentScrollable(e.target)) { + return; + } + const DELTA_MULTIPLIER = [1, 20, window.innerHeight]; + scrollBy( + cm.getWrapperElement().querySelector('.CodeMirror-scroll'), + e.deltaY * DELTA_MULTIPLIER[e.deltaMode] + ); + }, {passive: true}); + + function parentScrollable(node) { + while (node) { + if (node.offsetHeight < node.scrollHeight) { + const {overflow} = getComputedStyle(node); + if (overflow === 'auto' || overflow === 'scroll') { + return true; + } + } + node = node.parentNode; + } + return false; + } + + function scrollBy(el, offset) { + if (el.scrollBy) { + el.scrollBy({top: offset * 40, behavior: 'smooth'}); + } else { + el.scrollTop += offset; + } + } + function initAppliesToReport(cm) { const DELAY = 500; let widgets = [], timer, fromLine, toLine, style, isInit;