Editor: don't scroll sections into view on click

This commit is contained in:
tophf 2015-03-14 23:07:35 +03:00
parent 31a9be7c2a
commit 603da2736f

51
edit.js
View File

@ -173,35 +173,6 @@ function setupCodeMirror(textarea) {
cm.lastChange = cm.changeGeneration(); cm.lastChange = cm.changeGeneration();
cm.on("change", indicateCodeChange); 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)) {
lockScroll = null;
if (bounds.top < 0) {
window.scrollBy(0, bounds.top - 1);
} else {
window.scrollBy(0, bounds.bottom - window.innerHeight + 1);
}
// prevent possible double fire of selection change event induced by window.scrollBy
var selectionChangeCount = 0, selection;
function beforeSelectionChange(cm, obj) {
if (++selectionChangeCount == 1) {
selection = obj.ranges;
} else {
obj.update(selection);
cm.off("beforeSelectionChange", beforeSelectionChange);
}
}
cm.on("beforeSelectionChange", beforeSelectionChange);
setTimeout(function() {
cm.off("beforeSelectionChange", beforeSelectionChange)
}, 200);
}
});
// ensure the section doesn't jump when clicking selected text // ensure the section doesn't jump when clicking selected text
cm.on("cursorActivity", function(cm) { cm.on("cursorActivity", function(cm) {
setTimeout(function() { setTimeout(function() {
@ -363,6 +334,19 @@ function removeSection(event) {
setCleanItem(section, !section.defaultValue); setCleanItem(section, !section.defaultValue);
} }
function makeSectionVisible(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)) {
lockScroll = null;
if (bounds.top < 0) {
window.scrollBy(0, bounds.top - 1);
} else {
window.scrollBy(0, bounds.bottom - window.innerHeight + 1);
}
}
}
function setupGlobalSearch() { function setupGlobalSearch() {
var originalCommand = { var originalCommand = {
find: CodeMirror.commands.find, find: CodeMirror.commands.find,
@ -426,6 +410,7 @@ function setupGlobalSearch() {
var searchCursor = cm.getSearchCursor(state.query, pos, shouldIgnoreCase(state.query)); var searchCursor = cm.getSearchCursor(state.query, pos, shouldIgnoreCase(state.query));
if (searchCursor.find(reverse) || editors.length == 1) { if (searchCursor.find(reverse) || editors.length == 1) {
if (editors.length > 1) { if (editors.length > 1) {
makeSectionVisible(cm);
cm.focus(); cm.focus();
} }
// speedup the original findNext // speedup the original findNext
@ -456,10 +441,14 @@ function jumpToLine(cm) {
} }
function nextBuffer(cm) { function nextBuffer(cm) {
editors[(editors.indexOf(cm) + 1) % editors.length].focus(); var cm = editors[(editors.indexOf(cm) + 1) % editors.length];
makeSectionVisible(cm);
cm.focus();
} }
function prevBuffer(cm) { function prevBuffer(cm) {
editors[(editors.indexOf(cm) - 1 + editors.length) % editors.length].focus(); var cm = editors[(editors.indexOf(cm) - 1 + editors.length) % editors.length];
makeSectionVisible(cm);
cm.focus();
} }
window.addEventListener("load", init, false); window.addEventListener("load", init, false);