diff options
author | Cheng-Han, Wu | 2016-02-11 14:36:05 -0600 |
---|---|---|
committer | Cheng-Han, Wu | 2016-02-11 14:36:05 -0600 |
commit | 6967dac0ffaa7ff26c6ec4a1e42e81fffa687693 (patch) | |
tree | d9ec315918b41c5b0621ffb169396ed02466fb7a | |
parent | 8db142554fbe1c85aa8ffcdf0900b2855645939b (diff) |
Updated syncscroll to lerp on last line
-rw-r--r-- | public/js/syncscroll.js | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/public/js/syncscroll.js b/public/js/syncscroll.js index abc9a274..0d858db7 100644 --- a/public/js/syncscroll.js +++ b/public/js/syncscroll.js @@ -161,13 +161,15 @@ ui.area.view.on('scroll', function () { //editor.on('scroll', _.debounce(syncScrollToView, syncScrollDelay)); //ui.area.view.on('scroll', _.debounce(syncScrollToEdit, 50)); -var scrollMap, lineHeightMap; +var scrollMap, lineHeightMap, viewTop, viewBottom; viewAjaxCallback = clearMap; function clearMap() { scrollMap = null; lineHeightMap = null; + viewTop = null; + viewBottom = null; } var buildMap = _.throttle(buildMapInner, buildMapThrottle); @@ -183,6 +185,8 @@ function buildMapInner(syncBack) { _scrollMap = []; nonEmptyList = []; _lineHeightMap = []; + viewTop = 0; + viewBottom = ui.area.view[0].scrollHeight - ui.area.view.height(); acc = 0; var lines = editor.getValue().split('\n'); @@ -208,7 +212,8 @@ function buildMapInner(syncBack) { } nonEmptyList.push(0); - _scrollMap[0] = 0; + // make the first line go top + _scrollMap[0] = viewTop; var parts = ui.area.markdown.find('.part').toArray(); for (i = 0; i < parts.length; i++) { @@ -296,9 +301,13 @@ function syncScrollToView(event, _lineNo) { var topDiffPercent, posToNextDiff; var textHeight = editor.defaultTextHeight(); lineNo = Math.floor(scrollInfo.top / textHeight); - //if reach bottom, then scroll to end - if (scrollInfo.height > scrollInfo.clientHeight && scrollInfo.top + scrollInfo.clientHeight >= scrollInfo.height - textHeight) { - posTo = ui.area.view[0].scrollHeight - ui.area.view.height(); + // if reach the last line, will start lerp to the bottom + var diffToBottom = (scrollInfo.top + scrollInfo.clientHeight) - (scrollInfo.height - textHeight); + if (scrollInfo.height > scrollInfo.clientHeight && diffToBottom > 0) { + topDiffPercent = diffToBottom / textHeight; + posTo = scrollMap[lineNo + 1]; + posToNextDiff = (viewBottom - posTo) * topDiffPercent; + posTo += Math.floor(posToNextDiff); } else { topDiffPercent = (scrollInfo.top % textHeight) / textHeight; posTo = scrollMap[lineNo]; |