From cd8d11b1373817ce4096d1aca2cca744707d5ba0 Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 7 Mar 2015 03:10:19 +0300 Subject: [PATCH] Scroll the clicked section into view --- edit.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/edit.js b/edit.js index 35f2d1e7..58e651ac 100644 --- a/edit.js +++ b/edit.js @@ -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); }