From 0ff96f9caf6fab2fc8ca91233b5ee67199e010f7 Mon Sep 17 00:00:00 2001 From: BurningTreeC Date: Thu, 15 Nov 2018 15:58:32 +0100 Subject: [PATCH] [pagescroller] add callback function option (#3473) this allows using the pagescroller for scrolling elements into view where the rect gets calculated somewhere else Example: CodeMirror has the `cm.cursorCoords()` function that returns the rect of the textselection (or cursor coordinates) this scrolls the cursor or text selection into view using tiddlywikis pagescroller: `$tw.pageScroller.scrollIntoView(undefined, function() { return self.cm.cursorCoords(true,"window"); });` --- core/modules/utils/dom/scroller.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/modules/utils/dom/scroller.js b/core/modules/utils/dom/scroller.js index e34e6b032..80afa848b 100644 --- a/core/modules/utils/dom/scroller.js +++ b/core/modules/utils/dom/scroller.js @@ -53,10 +53,10 @@ PageScroller.prototype.handleEvent = function(event) { /* Handle a scroll event hitting the page document */ -PageScroller.prototype.scrollIntoView = function(element) { +PageScroller.prototype.scrollIntoView = function(element,callback) { var self = this, duration = $tw.utils.getAnimationDuration(), - srcWindow = element.ownerDocument.defaultView; + srcWindow = element ? element.ownerDocument.defaultView : window; // Now get ready to scroll the body this.cancelScroll(srcWindow); this.startTime = Date.now(); @@ -68,8 +68,8 @@ PageScroller.prototype.scrollIntoView = function(element) { } // Get the client bounds of the element and adjust by the scroll position var getBounds = function() { - var clientBounds = element.getBoundingClientRect(), - scrollPosition = $tw.utils.getScrollPosition(srcWindow); + var clientBounds = typeof callback === 'function' ? callback() : element.getBoundingClientRect(), + scrollPosition = $tw.utils.getScrollPosition(); return { left: clientBounds.left + scrollPosition.x, top: clientBounds.top + scrollPosition.y - offset,