Add: scroll the editor when cursor is outside of the editor

This commit is contained in:
eight 2017-09-15 07:47:44 +08:00
parent 553cc94bc8
commit 5a9ac5ce7d

View File

@ -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;