Browse Source

Demo: js cleanup

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
357ce955a8
  1. 54
      demo/assets/index.js

54
demo/assets/index.js

@ -1,7 +1,7 @@
(function () { (function () {
'use strict'; 'use strict';
var mdHtml, mdSrc, permalink, scrollMap, escapeHtml; var mdHtml, mdSrc, permalink, scrollMap;
var defaults = { var defaults = {
html: false, // Enable HTML tags in source html: false, // Enable HTML tags in source
@ -90,19 +90,28 @@
}; };
} }
function setHighlightedlContent(selector, content, lang) {
if (window.hljs) {
$(selector).html(window.hljs.highlight(lang, content).value);
} else {
$(selector).text(content);
}
}
function updateResult() { function updateResult() {
var source = $('.source').val(), var source = $('.source').val();
out;
// Update only active view to avoid slowdowns // Update only active view to avoid slowdowns
// (debug & src view with highlighting are a bit slow) // (debug & src view with highlighting are a bit slow)
if (defaults._view === 'src') { if (defaults._view === 'src') {
out = mdSrc.render(source); setHighlightedlContent('.result-src-content', mdSrc.render(source), 'html');
$('.result-src-content').html(window.hljs ? window.hljs.highlight('html', out).value : escapeHtml(out));
} else if (defaults._view === 'debug') { } else if (defaults._view === 'debug') {
out = JSON.stringify(mdSrc.parse(source, { references: {} }), null, 2); setHighlightedlContent(
$('.result-debug-content').html(window.hljs ? window.hljs.highlight('json', out).value : escapeHtml(out)); '.result-debug-content',
JSON.stringify(mdSrc.parse(source, { references: {} }), null, 2),
'json'
);
} else { /*defaults._view === 'html'*/ } else { /*defaults._view === 'html'*/
$('.result-html').html(mdHtml.render(source)); $('.result-html').html(mdHtml.render(source));
@ -131,7 +140,8 @@
// Optimizations are required only for big texts. // Optimizations are required only for big texts.
function buildScrollMap() { 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'),
_scrollMap;
sourceLikeDiv = $('<div />').css({ sourceLikeDiv = $('<div />').css({
position: 'absolute', position: 'absolute',
@ -145,7 +155,7 @@
}).appendTo('body'); }).appendTo('body');
offset = $('.result-html').scrollTop() - $('.result-html').offset().top; offset = $('.result-html').scrollTop() - $('.result-html').offset().top;
scrollMap = []; _scrollMap = [];
nonEmptyList = []; nonEmptyList = [];
lineHeightMap = []; lineHeightMap = [];
@ -169,52 +179,54 @@
lineHeightMap.push(acc); lineHeightMap.push(acc);
linesCount = acc; linesCount = acc;
for (i = 0; i < linesCount; i++) { scrollMap.push(-1); } for (i = 0; i < linesCount; i++) { _scrollMap.push(-1); }
nonEmptyList.push(0); nonEmptyList.push(0);
scrollMap[0] = 0; _scrollMap[0] = 0;
$('.line').each(function(n, el) { $('.line').each(function(n, el) {
var $el = $(el), t = $el.data('line'); var $el = $(el), t = $el.data('line');
if (t === '') { return; } if (t === '') { return; }
t = lineHeightMap[t]; t = lineHeightMap[t];
if (t !== 0) { nonEmptyList.push(t); } if (t !== 0) { nonEmptyList.push(t); }
scrollMap[t] = Math.round($el.offset().top + offset); _scrollMap[t] = Math.round($el.offset().top + offset);
}); });
nonEmptyList.push(linesCount); nonEmptyList.push(linesCount);
scrollMap[linesCount] = $('.result-html')[0].scrollHeight; _scrollMap[linesCount] = $('.result-html')[0].scrollHeight;
pos = 0; pos = 0;
for (i = 1; i < linesCount; i++) { for (i = 1; i < linesCount; i++) {
if (scrollMap[i] !== -1) { if (_scrollMap[i] !== -1) {
pos++; pos++;
continue; continue;
} }
a = nonEmptyList[pos]; a = nonEmptyList[pos];
b = nonEmptyList[pos + 1]; b = nonEmptyList[pos + 1];
scrollMap[i] = Math.round((scrollMap[b] * (i - a) + scrollMap[a] * (b - i)) / (b - a)); _scrollMap[i] = Math.round((_scrollMap[b] * (i - a) + _scrollMap[a] * (b - i)) / (b - a));
} }
return scrollMap; return _scrollMap;
} }
function syncScroll() { function syncScroll() {
var textarea = $('.source'), var textarea = $('.source'),
lineHeight = parseFloat(textarea.css('line-height')), lineHeight = parseFloat(textarea.css('line-height')),
lineNo; lineNo, posTo;
lineNo = Math.floor(textarea.scrollTop() / lineHeight); lineNo = Math.floor(textarea.scrollTop() / lineHeight);
if (!scrollMap) { buildScrollMap(); } if (!scrollMap) { scrollMap = buildScrollMap(); }
posTo = scrollMap[lineNo];
$('.result-html').stop(true).animate({ $('.result-html').stop(true).animate({
scrollTop: scrollMap[lineNo] scrollTop: posTo
}, 100, 'linear'); }, 100, 'linear');
} }
//////////////////////////////////////////////////////////////////////////////
// Init on page load
//
$(function() { $(function() {
escapeHtml = window.Remarkable.utils.escapeHtml;
// highlight snippet // highlight snippet
if (window.hljs) { if (window.hljs) {
$('pre.code-sample code').each(function(i, block) { $('pre.code-sample code').each(function(i, block) {

Loading…
Cancel
Save