diff --git a/edit/codemirror-default.js b/edit/codemirror-default.js index 87fb59f8..7a4f00a3 100644 --- a/edit/codemirror-default.js +++ b/edit/codemirror-default.js @@ -144,12 +144,16 @@ define(require => { jumpToPos(pos, end = pos) { const {curOp} = this; if (!curOp) this.startOperation(); - const coords = this.cursorCoords(pos, 'window'); - const b = this.display.wrapper.getBoundingClientRect(); - if (coords.top < Math.max(0, b.top + this.defaultTextHeight() * 2) || - coords.bottom > Math.min(window.innerHeight, b.bottom - 100)) { - this.scrollIntoView(pos, b.height / 2); + const y = this.cursorCoords(pos, 'window').top; + const rect = this.display.wrapper.getBoundingClientRect(); + // case 1) outside of CM viewport or too close to edge so tell CM to render a new viewport + if (y < rect.top + 50 || y > rect.bottom - 100) { + 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); if (!curOp) this.endOperation(); }, diff --git a/edit/global-search.js b/edit/global-search.js index 37b84d96..76269715 100644 --- a/edit/global-search.js +++ b/edit/global-search.js @@ -797,7 +797,6 @@ define(require => { }); if (!cm.curOp) cm.startOperation(); if (!state.firstRun) { - editor.scrollToEditor(cm); cm.jumpToPos(pos.from, pos.to); } // focus or expose as the current search target diff --git a/edit/sections-editor.js b/edit/sections-editor.js index 90d767ff..b4eb7573 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -126,17 +126,12 @@ define(require => function SectionsEditor() { }, scrollToEditor(cm) { - const section = sections.find(s => s.cm === cm).el; - const bounds = section.getBoundingClientRect(); - if ( - (bounds.bottom > window.innerHeight && bounds.top > 0) || - (bounds.top < 0 && bounds.bottom < window.innerHeight) - ) { - if (bounds.top < 0) { - window.scrollBy(0, bounds.top - 1); - } else { - window.scrollBy(0, bounds.bottom - window.innerHeight + 1); - } + const {el} = sections.find(s => s.cm === cm); + const r = el.getBoundingClientRect(); + const h = window.innerHeight; + if (r.bottom > h && r.top > 0 || + r.bottom < h && r.top < 0) { + window.scrollBy(0, (r.top + r.bottom - h) / 2 | 0); } }, });