Fixed scrollSpy might not set properly on updateView or windowResize

This commit is contained in:
Wu Cheng-Han 2015-09-25 18:29:01 +08:00
parent 4725415acd
commit a879b5b5fd

View file

@ -470,7 +470,8 @@ function windowResizeInner() {
clearMap(); clearMap();
syncScrollToView(); syncScrollToView();
editor.setOption('viewportMargin', viewportMargin); editor.setOption('viewportMargin', viewportMargin);
}, windowResizeDelay); updateScrollspy();
}, 100);
} }
} }
@ -803,6 +804,32 @@ function generateScrollspy() {
ui.area.view.scroll(); 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 //fix for wrong autofocus
$('#clipboardModal').on('shown.bs.modal', function () { $('#clipboardModal').on('shown.bs.modal', function () {
$('#clipboardModal').blur(); $('#clipboardModal').blur();
@ -1736,6 +1763,7 @@ function updateViewInner() {
generateToc('toc'); generateToc('toc');
generateToc('toc-affix'); generateToc('toc-affix');
generateScrollspy(); generateScrollspy();
updateScrollspy();
smoothHashScroll(); smoothHashScroll();
isDirty = false; isDirty = false;
clearMap(); clearMap();