1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00

[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"); });`
This commit is contained in:
BurningTreeC 2018-11-15 15:58:32 +01:00 committed by Jeremy Ruston
parent c82edbe6bc
commit 0ff96f9caf

View File

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