diff --git a/edit/codemirror-default.js b/edit/codemirror-default.js index cd507db6..d5cad7b9 100644 --- a/edit/codemirror-default.js +++ b/edit/codemirror-default.js @@ -126,17 +126,18 @@ /** * Sets cursor and centers it in view if `pos` was out of view * @param {CodeMirror.Pos} pos + * @param {CodeMirror.Pos} [end] - will set a selection from `pos` to `end` */ - jumpToPos(pos) { + jumpToPos(pos, end = pos) { const {curOp} = this; if (!curOp) this.startOperation(); - const coords = this.cursorCoords(pos, 'page'); + 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); } - this.setCursor(pos); + CodeMirror.prototype.setSelection.call(this, pos, end); if (!curOp) this.endOperation(); }, }); diff --git a/edit/global-search.js b/edit/global-search.js index ff18616f..3470daf2 100644 --- a/edit/global-search.js +++ b/edit/global-search.js @@ -23,7 +23,6 @@ onDOMready().then(() => { const ANNOTATE_SCROLLBAR_DELAY = 350; const ANNOTATE_SCROLLBAR_OPTIONS = {maxMatches: 10e3}; const STORAGE_UPDATE_DELAY = 500; - const SCROLL_REVEAL_MIN_PX = 50; const DIALOG_SELECTOR = '#search-replace-dialog'; const DIALOG_STYLE_SELECTOR = '#search-replace-dialog-style'; @@ -80,8 +79,8 @@ onDOMready().then(() => { const cm = target.CodeMirror; (cm || target).focus(); if (cm) { - const pos = cm.state.search.searchPos; - cm.setSelection(pos.from, pos.to); + const {from, to} = cm.state.search.searchPos; + cm.jumpToPos(from, to); } } destroyDialog({restoreFocus: !found}); @@ -784,21 +783,20 @@ onDOMready().then(() => { function makeMatchVisible(cm, searchCursor) { const canFocus = !state.firstRun && (!state.dialog || !state.dialog.contains(document.activeElement)); state.cm = cm; - // scroll within the editor + const pos = searchCursor.pos; Object.assign(getStateSafe(cm), { cursorPos: { from: cm.getCursor('from'), to: cm.getCursor('to'), }, - searchPos: searchCursor.pos, + searchPos: pos, unclosedOp: !cm.curOp, }); if (!cm.curOp) cm.startOperation(); if (!state.firstRun) { editor.scrollToEditor(cm); - cm.setSelection(searchCursor.pos.from, searchCursor.pos.to); - cm.scrollIntoView(searchCursor.pos, SCROLL_REVEAL_MIN_PX); + cm.jumpToPos(pos.from, pos.to); } // focus or expose as the current search target clearMarker(); @@ -808,7 +806,6 @@ onDOMready().then(() => { } else { makeTargetVisible(cm.display.wrapper); // mark the match - const pos = searchCursor.pos; state.marker = cm.state.search.marker = cm.markText(pos.from, pos.to, { className: MATCH_CLASS, clearOnEnter: true, diff --git a/edit/linter-report.js b/edit/linter-report.js index b3aea52b..da2ad952 100644 --- a/edit/linter-report.js +++ b/edit/linter-report.js @@ -156,6 +156,6 @@ Object.assign(linter, (() => { function gotoLintIssue(cm, anno) { editor.scrollToEditor(cm); cm.focus(); - cm.setSelection(anno.from); + cm.jumpToPos(anno.from); } })());