Browse Source

Optimized result tabs update & added comments

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
c42fc6bbfe
  1. 43
      demo/assets/index.js

43
demo/assets/index.js

@ -64,6 +64,13 @@
return '<table class="table table-striped">\n'; return '<table class="table table-striped">\n';
}; };
//
// Inject line numbers for sync scroll. Notes:
//
// - We track only headings and paragraphs on first level. That's enougth.
// - Footnotes content causes jumps. Level limit filter it automatically.
//
mdHtml.renderer.rules.paragraph_open = function (tokens, idx) { mdHtml.renderer.rules.paragraph_open = function (tokens, idx) {
var line; var line;
if (tokens[idx].lines && tokens[idx].level === 0) { if (tokens[idx].lines && tokens[idx].level === 0) {
@ -84,14 +91,24 @@
} }
function updateResult() { function updateResult() {
var source = $('.source').val(); var source = $('.source').val(),
dump;
$('.result-html').html(mdHtml.render(source)); // Update only active view to avoid slowdowns
$('.result-src-content').html(window.hljs.highlight('html', mdSrc.render(source)).value); // (debug & src view with highlighting are a bit slow)
scrollMap = null; if (defaults._view === 'src') {
$('.result-src-content').html(window.hljs.highlight('html', mdSrc.render(source)).value);
} else if (defaults._view === 'debug') {
dump = JSON.stringify(mdSrc.parse(source, { references: {} }), null, 2);
$('.result-debug-content').html(window.hljs.highlight('json', dump).value);
} else { /*defaults._view === 'html'*/
$('.result-html').html(mdHtml.render(source));
}
var dump = JSON.stringify(mdSrc.parse(source, { references: {} }), null, 2); // reset lines mapping cache on content update
$('.result-debug-content').html(window.hljs.highlight('json', dump).value); scrollMap = null;
try { try {
if (source) { if (source) {
@ -108,7 +125,10 @@
} }
} }
function recomputeScroll() { // Build offsets for each line (lines can be wrapped)
// That's a bit dirty to process each line everytime, but ok for demo.
// Optimizations are required only for big texts.
function buildScrollMap() {
var i, offset, nonEmptyList, pos, a, b, lineHeightMap, linesCount, var i, offset, nonEmptyList, pos, a, b, lineHeightMap, linesCount,
acc, sourceLikeDiv, textarea = $('.source'); acc, sourceLikeDiv, textarea = $('.source');
@ -185,7 +205,7 @@
lineNo; lineNo;
lineNo = Math.floor(textarea.scrollTop() / lineHeight); lineNo = Math.floor(textarea.scrollTop() / lineHeight);
if (!scrollMap) { recomputeScroll(); } if (!scrollMap) { buildScrollMap(); }
$('.result-html').stop(true).animate({ $('.result-html').stop(true).animate({
scrollTop: scrollMap[lineNo] scrollTop: scrollMap[lineNo]
}, 100, 'linear'); }, 100, 'linear');
@ -280,7 +300,7 @@
permalink = document.getElementById('permalink'); permalink = document.getElementById('permalink');
// Setup listeners // Setup listeners
$('.source').on('keyup paste cut mouseup', updateResult); $('.source').on('keyup paste cut mouseup', _.debounce(updateResult, 300, { maxWait: 500 }));
$('.source').on('scroll', _.debounce(syncScroll, 50, { maxWait: 50 })); $('.source').on('scroll', _.debounce(syncScroll, 50, { maxWait: 50 }));
$('.source-clear').on('click', function (event) { $('.source-clear').on('click', function (event) {
@ -299,6 +319,11 @@
} }
}); });
// Need to recalculate line positions on window resize
$(window).on('resize', function () {
scrollMap = null;
});
updateResult(); updateResult();
}); });
})(); })();

Loading…
Cancel
Save