From 8e860c53f3bb3a671bef22f928c0eb68ed3a2bc9 Mon Sep 17 00:00:00 2001 From: Adrian Heine <mail@adrianheine.de> Date: Wed, 26 Apr 2017 10:42:24 +0200 Subject: [PATCH] Make calculateScrollPos private, introduce scrollToCoordsRange --- src/display/scrolling.js | 21 +++++++++++++-------- src/edit/methods.js | 10 ++-------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/display/scrolling.js b/src/display/scrolling.js index 12a13b454..9a84b7a99 100644 --- a/src/display/scrolling.js +++ b/src/display/scrolling.js @@ -69,7 +69,7 @@ export function scrollIntoView(cm, rect) { // rectangle into view. Returns an object with scrollTop and // scrollLeft properties. When these are undefined, the // vertical/horizontal position does not need to be adjusted. -export function calculateScrollPos(cm, rect) { +function calculateScrollPos(cm, rect) { let display = cm.display, snapMargin = textHeight(cm.display) if (rect.top < 0) rect.top = 0 let screentop = cm.curOp && cm.curOp.scrollTop != null ? cm.curOp.scrollTop : display.scroller.scrollTop @@ -128,15 +128,20 @@ export function resolveScrollToPos(cm) { if (range) { cm.curOp.scrollToPos = null let from = estimateCoords(cm, range.from), to = estimateCoords(cm, range.to) - let sPos = calculateScrollPos(cm, { - left: Math.min(from.left, to.left), - top: Math.min(from.top, to.top) - range.margin, - right: Math.max(from.right, to.right), - bottom: Math.max(from.bottom, to.bottom) + range.margin - }) - cm.scrollTo(sPos.scrollLeft, sPos.scrollTop) + scrollToCoordsRange(cm, from, to, range.margin) } } + +export function scrollToCoordsRange(cm, from, to, margin) { + let sPos = calculateScrollPos(cm, { + left: Math.min(from.left, to.left), + top: Math.min(from.top, to.top) - margin, + right: Math.max(from.right, to.right), + bottom: Math.max(from.bottom, to.bottom) + margin + }) + cm.scrollTo(sPos.scrollLeft, sPos.scrollTop) +} + // Sync the scrollable area and scrollbars, ensure the viewport // covers the visible area. export function updateScrollTop(cm, val) { diff --git a/src/edit/methods.js b/src/edit/methods.js index b8dd343d0..5262c4f9c 100644 --- a/src/edit/methods.js +++ b/src/edit/methods.js @@ -14,7 +14,7 @@ import { clipLine, clipPos, equalCursorPos, Pos } from "../line/pos" import { charCoords, charWidth, clearCaches, clearLineMeasurementCache, coordsChar, cursorCoords, displayHeight, displayWidth, estimateLineHeights, fromCoordSystem, intoCoordSystem, scrollGap, textHeight } from "../measurement/position_measurement" import { Range } from "../model/selection" import { replaceOneSelection, skipAtomic } from "../model/selection_updates" -import { addToScrollPos, calculateScrollPos, ensureCursorVisible, resolveScrollToPos, scrollIntoView } from "../display/scrolling" +import { addToScrollPos, ensureCursorVisible, resolveScrollToPos, scrollIntoView, scrollToCoordsRange } from "../display/scrolling" import { heightAtLine } from "../line/spans" import { updateGutterSpace } from "../display/update_display" import { indexOf, insertSorted, isWordChar, sel_dontScroll, sel_move } from "../util/misc" @@ -388,13 +388,7 @@ export default function(CodeMirror) { resolveScrollToPos(this) this.curOp.scrollToPos = range } else { - let sPos = calculateScrollPos(this, { - left: Math.min(range.from.left, range.to.left), - top: Math.min(range.from.top, range.to.top) - range.margin, - right: Math.max(range.from.right, range.to.right), - bottom: Math.max(range.from.bottom, range.to.bottom) + range.margin - }) - this.scrollTo(sPos.scrollLeft, sPos.scrollTop) + scrollToCoordsRange(this, range.from, range.to, range.margin) } }), -- GitLab