consistent window scrolling in scrollToEditor and jumpToPos

This commit is contained in:
tophf 2020-12-08 22:54:29 +03:00
parent 9d8f9ec5a2
commit 72330b480b
3 changed files with 15 additions and 17 deletions

View File

@ -144,12 +144,16 @@ define(require => {
jumpToPos(pos, end = pos) { jumpToPos(pos, end = pos) {
const {curOp} = this; const {curOp} = this;
if (!curOp) this.startOperation(); if (!curOp) this.startOperation();
const coords = this.cursorCoords(pos, 'window'); const y = this.cursorCoords(pos, 'window').top;
const b = this.display.wrapper.getBoundingClientRect(); const rect = this.display.wrapper.getBoundingClientRect();
if (coords.top < Math.max(0, b.top + this.defaultTextHeight() * 2) || // case 1) outside of CM viewport or too close to edge so tell CM to render a new viewport
coords.bottom > Math.min(window.innerHeight, b.bottom - 100)) { if (y < rect.top + 50 || y > rect.bottom - 100) {
this.scrollIntoView(pos, b.height / 2); this.scrollIntoView(pos, rect.height / 2);
// case 2) inside CM viewport but outside of window viewport so just scroll the window
} else if (y < 0 || y > innerHeight) {
editor.scrollToEditor(this);
} }
// Using prototype since our bookmark patch sets cm.setSelection to jumpToPos
CodeMirror.prototype.setSelection.call(this, pos, end); CodeMirror.prototype.setSelection.call(this, pos, end);
if (!curOp) this.endOperation(); if (!curOp) this.endOperation();
}, },

View File

@ -797,7 +797,6 @@ define(require => {
}); });
if (!cm.curOp) cm.startOperation(); if (!cm.curOp) cm.startOperation();
if (!state.firstRun) { if (!state.firstRun) {
editor.scrollToEditor(cm);
cm.jumpToPos(pos.from, pos.to); cm.jumpToPos(pos.from, pos.to);
} }
// focus or expose as the current search target // focus or expose as the current search target

View File

@ -126,17 +126,12 @@ define(require => function SectionsEditor() {
}, },
scrollToEditor(cm) { scrollToEditor(cm) {
const section = sections.find(s => s.cm === cm).el; const {el} = sections.find(s => s.cm === cm);
const bounds = section.getBoundingClientRect(); const r = el.getBoundingClientRect();
if ( const h = window.innerHeight;
(bounds.bottom > window.innerHeight && bounds.top > 0) || if (r.bottom > h && r.top > 0 ||
(bounds.top < 0 && bounds.bottom < window.innerHeight) r.bottom < h && r.top < 0) {
) { window.scrollBy(0, (r.top + r.bottom - h) / 2 | 0);
if (bounds.top < 0) {
window.scrollBy(0, bounds.top - 1);
} else {
window.scrollBy(0, bounds.bottom - window.innerHeight + 1);
}
} }
}, },
}); });