summaryrefslogtreecommitdiff
path: root/public
diff options
context:
space:
mode:
authorWu Cheng-Han2015-09-25 18:29:01 +0800
committerWu Cheng-Han2015-09-25 18:29:01 +0800
commita879b5b5fda434e7581fd3efa4515caab45abf7b (patch)
tree2ca38a4fbf9ace90837f21ba7545e54f7d65931e /public
parent4725415acd5171a73e5444a479cc3bbdc332bb4a (diff)
Fixed scrollSpy might not set properly on updateView or windowResize
Diffstat (limited to '')
-rw-r--r--public/js/index.js30
1 files changed, 29 insertions, 1 deletions
diff --git a/public/js/index.js b/public/js/index.js
index 16857d0d..b3fc372f 100644
--- a/public/js/index.js
+++ b/public/js/index.js
@@ -470,7 +470,8 @@ function windowResizeInner() {
clearMap();
syncScrollToView();
editor.setOption('viewportMargin', viewportMargin);
- }, windowResizeDelay);
+ updateScrollspy();
+ }, 100);
}
}
@@ -803,6 +804,32 @@ function generateScrollspy() {
ui.area.view.scroll();
}
+function updateScrollspy() {
+ var headers = ui.area.markdown.find('h1, h2, h3').toArray();
+ var headerMap = [];
+ for (var i = 0; i < headers.length; i++) {
+ headerMap.push($(headers[i]).offset().top - parseInt($(headers[i]).css('margin-top')));
+ }
+ applyScrollspyActive($(window).scrollTop(), headerMap, headers,
+ $('.scrollspy-body'), 0);
+ var offset = ui.area.view.scrollTop() - ui.area.view.offset().top;
+ applyScrollspyActive(ui.area.view.scrollTop(), headerMap, headers,
+ $('.scrollspy-view'), offset - 10);
+}
+
+function applyScrollspyActive(top, headerMap, headers, target, offset) {
+ var index = 0;
+ for(var i = headerMap.length - 1; i >= 0; i--) {
+ if(top >= (headerMap[i] + offset) && headerMap[i + 1] && top < (headerMap[i + 1] + offset)) {
+ index = i;
+ break;
+ }
+ }
+ var header = $(headers[index]);
+ var active = target.find('a[href="#' + header.attr('id') + '"]');
+ active.closest('li').addClass('active').parent().closest('li').addClass('active').parent().closest('li').addClass('active');
+}
+
//fix for wrong autofocus
$('#clipboardModal').on('shown.bs.modal', function () {
$('#clipboardModal').blur();
@@ -1736,6 +1763,7 @@ function updateViewInner() {
generateToc('toc');
generateToc('toc-affix');
generateScrollspy();
+ updateScrollspy();
smoothHashScroll();
isDirty = false;
clearMap();