Scroll the clicked section into view

This commit is contained in:
tophf 2015-03-07 03:10:19 +03:00
parent 514a1227e4
commit cd8d11b137

14
edit.js
View File

@ -29,6 +29,20 @@ function setupCodeMirror(textarea) {
});
cm.lastChange = cm.changeGeneration();
cm.on("change", indicateCodeChange);
// ensure the entire section is visible on focus
cm.on("focus", function(cm) {
var section = cm.display.wrapper.parentNode;
var bounds = section.getBoundingClientRect();
if ((bounds.bottom > window.innerHeight && bounds.top > 0) || (bounds.top < 0 && bounds.bottom < window.innerHeight)) {
if (bounds.top > window.innerHeight || bounds.top < 0) {
section.scrollIntoView();
} else {
window.scrollBy(0, bounds.bottom - window.innerHeight + 1);
}
}
});
editors.push(cm);
}