use jumpToPos more, fix coords calc

This commit is contained in:
tophf 2020-11-26 16:56:33 +03:00
parent 6451eb533c
commit cb64a6bac9
3 changed files with 10 additions and 12 deletions

View File

@ -126,17 +126,18 @@
/** /**
* Sets cursor and centers it in view if `pos` was out of view * Sets cursor and centers it in view if `pos` was out of view
* @param {CodeMirror.Pos} pos * @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; const {curOp} = this;
if (!curOp) this.startOperation(); if (!curOp) this.startOperation();
const coords = this.cursorCoords(pos, 'page'); const coords = this.cursorCoords(pos, 'window');
const b = this.display.wrapper.getBoundingClientRect(); const b = this.display.wrapper.getBoundingClientRect();
if (coords.top < Math.max(0, b.top + this.defaultTextHeight() * 2) || if (coords.top < Math.max(0, b.top + this.defaultTextHeight() * 2) ||
coords.bottom > Math.min(window.innerHeight, b.bottom - 100)) { coords.bottom > Math.min(window.innerHeight, b.bottom - 100)) {
this.scrollIntoView(pos, b.height / 2); this.scrollIntoView(pos, b.height / 2);
} }
this.setCursor(pos); CodeMirror.prototype.setSelection.call(this, pos, end);
if (!curOp) this.endOperation(); if (!curOp) this.endOperation();
}, },
}); });

View File

@ -23,7 +23,6 @@ onDOMready().then(() => {
const ANNOTATE_SCROLLBAR_DELAY = 350; const ANNOTATE_SCROLLBAR_DELAY = 350;
const ANNOTATE_SCROLLBAR_OPTIONS = {maxMatches: 10e3}; const ANNOTATE_SCROLLBAR_OPTIONS = {maxMatches: 10e3};
const STORAGE_UPDATE_DELAY = 500; const STORAGE_UPDATE_DELAY = 500;
const SCROLL_REVEAL_MIN_PX = 50;
const DIALOG_SELECTOR = '#search-replace-dialog'; const DIALOG_SELECTOR = '#search-replace-dialog';
const DIALOG_STYLE_SELECTOR = '#search-replace-dialog-style'; const DIALOG_STYLE_SELECTOR = '#search-replace-dialog-style';
@ -80,8 +79,8 @@ onDOMready().then(() => {
const cm = target.CodeMirror; const cm = target.CodeMirror;
(cm || target).focus(); (cm || target).focus();
if (cm) { if (cm) {
const pos = cm.state.search.searchPos; const {from, to} = cm.state.search.searchPos;
cm.setSelection(pos.from, pos.to); cm.jumpToPos(from, to);
} }
} }
destroyDialog({restoreFocus: !found}); destroyDialog({restoreFocus: !found});
@ -784,21 +783,20 @@ onDOMready().then(() => {
function makeMatchVisible(cm, searchCursor) { function makeMatchVisible(cm, searchCursor) {
const canFocus = !state.firstRun && (!state.dialog || !state.dialog.contains(document.activeElement)); const canFocus = !state.firstRun && (!state.dialog || !state.dialog.contains(document.activeElement));
state.cm = cm; state.cm = cm;
// scroll within the editor // scroll within the editor
const pos = searchCursor.pos;
Object.assign(getStateSafe(cm), { Object.assign(getStateSafe(cm), {
cursorPos: { cursorPos: {
from: cm.getCursor('from'), from: cm.getCursor('from'),
to: cm.getCursor('to'), to: cm.getCursor('to'),
}, },
searchPos: searchCursor.pos, searchPos: pos,
unclosedOp: !cm.curOp, unclosedOp: !cm.curOp,
}); });
if (!cm.curOp) cm.startOperation(); if (!cm.curOp) cm.startOperation();
if (!state.firstRun) { if (!state.firstRun) {
editor.scrollToEditor(cm); editor.scrollToEditor(cm);
cm.setSelection(searchCursor.pos.from, searchCursor.pos.to); cm.jumpToPos(pos.from, pos.to);
cm.scrollIntoView(searchCursor.pos, SCROLL_REVEAL_MIN_PX);
} }
// focus or expose as the current search target // focus or expose as the current search target
clearMarker(); clearMarker();
@ -808,7 +806,6 @@ onDOMready().then(() => {
} else { } else {
makeTargetVisible(cm.display.wrapper); makeTargetVisible(cm.display.wrapper);
// mark the match // mark the match
const pos = searchCursor.pos;
state.marker = cm.state.search.marker = cm.markText(pos.from, pos.to, { state.marker = cm.state.search.marker = cm.markText(pos.from, pos.to, {
className: MATCH_CLASS, className: MATCH_CLASS,
clearOnEnter: true, clearOnEnter: true,

View File

@ -156,6 +156,6 @@ Object.assign(linter, (() => {
function gotoLintIssue(cm, anno) { function gotoLintIssue(cm, anno) {
editor.scrollToEditor(cm); editor.scrollToEditor(cm);
cm.focus(); cm.focus();
cm.setSelection(anno.from); cm.jumpToPos(anno.from);
} }
})()); })());