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
* @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();
},
});

View File

@ -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,

View File

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