2016-02-22 00:58:06 +00:00
|
|
|
// Inject line numbers for sync scroll.
|
|
|
|
|
|
|
|
function addPart(tokens, idx) {
|
|
|
|
if (tokens[idx].map && tokens[idx].level === 0) {
|
|
|
|
var startline = tokens[idx].map[0] + 1;
|
|
|
|
var endline = tokens[idx].map[1];
|
|
|
|
tokens[idx].attrJoin('class', 'part');
|
|
|
|
tokens[idx].attrJoin('data-startline', startline);
|
|
|
|
tokens[idx].attrJoin('data-endline', endline);
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2016-02-22 00:58:06 +00:00
|
|
|
}
|
2015-05-15 04:58:13 +00:00
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
md.renderer.rules.blockquote_open = function (tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('class', 'raw');
|
|
|
|
addPart(tokens, idx);
|
|
|
|
return self.renderToken.apply(self, arguments);
|
2015-05-15 04:58:13 +00:00
|
|
|
};
|
2016-02-22 00:58:06 +00:00
|
|
|
md.renderer.rules.table_open = function (tokens, idx, options, env, self) {
|
|
|
|
addPart(tokens, idx);
|
|
|
|
return self.renderToken.apply(self, arguments);
|
2015-05-15 04:58:13 +00:00
|
|
|
};
|
2016-02-22 00:58:06 +00:00
|
|
|
md.renderer.rules.bullet_list_open = function (tokens, idx, options, env, self) {
|
|
|
|
addPart(tokens, idx);
|
|
|
|
return self.renderToken.apply(self, arguments);
|
2015-09-25 10:10:36 +00:00
|
|
|
};
|
2016-02-22 00:58:06 +00:00
|
|
|
md.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('class', 'raw');
|
2016-03-04 15:20:17 +00:00
|
|
|
if (tokens[idx].map) {
|
|
|
|
var startline = tokens[idx].map[0] + 1;
|
|
|
|
var endline = tokens[idx].map[1];
|
|
|
|
tokens[idx].attrJoin('class', 'part');
|
|
|
|
tokens[idx].attrJoin('data-startline', startline);
|
|
|
|
tokens[idx].attrJoin('data-endline', endline);
|
|
|
|
}
|
2016-02-22 00:58:06 +00:00
|
|
|
return self.renderToken.apply(self, arguments);
|
2015-05-15 04:58:13 +00:00
|
|
|
};
|
2016-02-22 00:58:06 +00:00
|
|
|
md.renderer.rules.ordered_list_open = function (tokens, idx, options, env, self) {
|
|
|
|
addPart(tokens, idx);
|
|
|
|
return self.renderToken.apply(self, arguments);
|
2015-05-15 04:58:13 +00:00
|
|
|
};
|
2016-02-22 00:58:06 +00:00
|
|
|
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
|
|
|
|
addPart(tokens, idx);
|
|
|
|
return self.renderToken.apply(self, arguments);
|
2015-05-15 04:58:13 +00:00
|
|
|
};
|
2016-02-22 00:58:06 +00:00
|
|
|
md.renderer.rules.paragraph_open = function (tokens, idx, options, env, self) {
|
|
|
|
addPart(tokens, idx);
|
|
|
|
return self.renderToken.apply(self, arguments);
|
|
|
|
};
|
|
|
|
md.renderer.rules.heading_open = function (tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('class', 'raw');
|
|
|
|
addPart(tokens, idx);
|
|
|
|
return self.renderToken.apply(self, arguments);
|
2015-05-15 04:58:13 +00:00
|
|
|
};
|
|
|
|
md.renderer.rules.fence = function (tokens, idx, options, env, self) {
|
2016-02-22 00:58:06 +00:00
|
|
|
var token = tokens[idx],
|
|
|
|
info = token.info ? md.utils.unescapeAll(token.info).trim() : '',
|
|
|
|
langName = '',
|
|
|
|
highlighted;
|
|
|
|
|
|
|
|
if (info) {
|
|
|
|
langName = info.split(/\s+/g)[0];
|
|
|
|
token.attrJoin('class', options.langPrefix + langName.replace(/\=$|\=\d+$|\=\+$/, ''));
|
|
|
|
token.attrJoin('class', 'hljs');
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.highlight) {
|
2016-02-22 00:58:06 +00:00
|
|
|
highlighted = options.highlight(token.content, langName) || md.utils.escapeHtml(token.content);
|
2015-05-15 04:58:13 +00:00
|
|
|
} else {
|
2016-02-22 00:58:06 +00:00
|
|
|
highlighted = md.utils.escapeHtml(token.content);
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
if (highlighted.indexOf('<pre') === 0) {
|
|
|
|
return highlighted + '\n';
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2016-02-22 00:58:06 +00:00
|
|
|
|
|
|
|
if (tokens[idx].map && tokens[idx].level === 0) {
|
|
|
|
var startline = tokens[idx].map[0] + 1;
|
|
|
|
var endline = tokens[idx].map[1];
|
|
|
|
return '<pre class="part" data-startline="' + startline + '" data-endline="' + endline + '"><code' + self.renderAttrs(token) + '>'
|
|
|
|
+ highlighted
|
|
|
|
+ '</code></pre>\n';
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
return '<pre><code' + self.renderAttrs(token) + '>'
|
|
|
|
+ highlighted
|
|
|
|
+ '</code></pre>\n';
|
|
|
|
};
|
|
|
|
md.renderer.rules.code_block = function (tokens, idx, options, env, self) {
|
|
|
|
if (tokens[idx].map && tokens[idx].level === 0) {
|
|
|
|
var startline = tokens[idx].map[0] + 1;
|
|
|
|
var endline = tokens[idx].map[1];
|
|
|
|
return '<pre class="part" data-startline="' + startline + '" data-endline="' + endline + '"><code>' + md.utils.escapeHtml(tokens[idx].content) + '</code></pre>\n';
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2016-02-22 00:58:06 +00:00
|
|
|
return '<pre><code>' + md.utils.escapeHtml(tokens[idx].content) + '</code></pre>\n';
|
2015-05-15 04:58:13 +00:00
|
|
|
};
|
2016-03-15 02:56:53 +00:00
|
|
|
function renderContainer(tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('role', 'alert');
|
|
|
|
tokens[idx].attrJoin('class', 'alert');
|
|
|
|
tokens[idx].attrJoin('class', 'alert-' + tokens[idx].info.trim());
|
|
|
|
addPart(tokens, idx);
|
|
|
|
return self.renderToken.apply(self, arguments);
|
|
|
|
}
|
|
|
|
md.use(window.markdownitContainer, 'success', { render: renderContainer });
|
|
|
|
md.use(window.markdownitContainer, 'info', { render: renderContainer });
|
|
|
|
md.use(window.markdownitContainer, 'warning', { render: renderContainer });
|
|
|
|
md.use(window.markdownitContainer, 'danger', { render: renderContainer });
|
2015-05-15 04:58:13 +00:00
|
|
|
|
2015-09-25 10:05:50 +00:00
|
|
|
var preventSyncScroll = false;
|
|
|
|
|
2015-06-01 10:04:25 +00:00
|
|
|
//var editorScrollThrottle = 100;
|
|
|
|
var buildMapThrottle = 100;
|
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
var viewScrolling = false;
|
|
|
|
var viewScrollingDelay = 200;
|
|
|
|
var viewScrollingTimer = null;
|
|
|
|
|
2015-06-01 10:04:25 +00:00
|
|
|
//editor.on('scroll', _.throttle(syncScrollToView, editorScrollThrottle));
|
2016-04-20 08:16:07 +00:00
|
|
|
if (editor.getOption('scrollbarStyle') === 'native') {
|
|
|
|
ui.area.codemirrorScroll.on('scroll', syncScrollToView);
|
|
|
|
} else {
|
|
|
|
editor.on('scroll', syncScrollToView);
|
|
|
|
}
|
2015-05-15 04:58:13 +00:00
|
|
|
ui.area.view.on('scroll', function () {
|
|
|
|
viewScrolling = true;
|
|
|
|
clearTimeout(viewScrollingTimer);
|
|
|
|
viewScrollingTimer = setTimeout(function () {
|
|
|
|
viewScrolling = false;
|
|
|
|
}, viewScrollingDelay);
|
|
|
|
});
|
|
|
|
//editor.on('scroll', _.debounce(syncScrollToView, syncScrollDelay));
|
|
|
|
//ui.area.view.on('scroll', _.debounce(syncScrollToEdit, 50));
|
|
|
|
|
2016-02-11 20:36:05 +00:00
|
|
|
var scrollMap, lineHeightMap, viewTop, viewBottom;
|
2015-05-15 04:58:13 +00:00
|
|
|
|
|
|
|
viewAjaxCallback = clearMap;
|
|
|
|
|
|
|
|
function clearMap() {
|
|
|
|
scrollMap = null;
|
|
|
|
lineHeightMap = null;
|
2016-02-11 20:36:05 +00:00
|
|
|
viewTop = null;
|
|
|
|
viewBottom = null;
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 10:04:25 +00:00
|
|
|
var buildMap = _.throttle(buildMapInner, buildMapThrottle);
|
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
// 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.
|
2015-06-01 10:04:25 +00:00
|
|
|
function buildMapInner(syncBack) {
|
2015-05-15 04:58:13 +00:00
|
|
|
var i, offset, nonEmptyList, pos, a, b, _lineHeightMap, linesCount,
|
2015-09-25 10:15:44 +00:00
|
|
|
acc, _scrollMap;
|
2015-05-15 04:58:13 +00:00
|
|
|
|
|
|
|
offset = ui.area.view.scrollTop() - ui.area.view.offset().top;
|
|
|
|
_scrollMap = [];
|
|
|
|
nonEmptyList = [];
|
|
|
|
_lineHeightMap = [];
|
2016-02-11 20:36:05 +00:00
|
|
|
viewTop = 0;
|
|
|
|
viewBottom = ui.area.view[0].scrollHeight - ui.area.view.height();
|
2015-05-15 04:58:13 +00:00
|
|
|
|
|
|
|
acc = 0;
|
2015-06-01 10:04:25 +00:00
|
|
|
var lines = editor.getValue().split('\n');
|
2015-09-25 10:15:44 +00:00
|
|
|
var lineHeight = editor.defaultTextHeight();
|
2015-06-01 10:04:25 +00:00
|
|
|
for (i = 0; i < lines.length; i++) {
|
|
|
|
var str = lines[i];
|
2015-05-15 04:58:13 +00:00
|
|
|
|
|
|
|
_lineHeightMap.push(acc);
|
|
|
|
|
|
|
|
if (str.length === 0) {
|
|
|
|
acc++;
|
2015-06-01 10:04:25 +00:00
|
|
|
continue;
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:15:44 +00:00
|
|
|
var h = editor.heightAtLine(i + 1) - editor.heightAtLine(i);
|
2015-07-02 12:30:43 +00:00
|
|
|
acc += Math.round(h / lineHeight);
|
2015-06-01 10:04:25 +00:00
|
|
|
}
|
2015-05-15 04:58:13 +00:00
|
|
|
_lineHeightMap.push(acc);
|
|
|
|
linesCount = acc;
|
|
|
|
|
|
|
|
for (i = 0; i < linesCount; i++) {
|
|
|
|
_scrollMap.push(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
nonEmptyList.push(0);
|
2016-02-11 20:36:05 +00:00
|
|
|
// make the first line go top
|
|
|
|
_scrollMap[0] = viewTop;
|
2015-05-15 04:58:13 +00:00
|
|
|
|
2015-06-01 10:04:25 +00:00
|
|
|
var parts = ui.area.markdown.find('.part').toArray();
|
|
|
|
for (i = 0; i < parts.length; i++) {
|
|
|
|
var $el = $(parts[i]),
|
|
|
|
t = $el.attr('data-startline') - 1;
|
2015-05-15 04:58:13 +00:00
|
|
|
if (t === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
t = _lineHeightMap[t];
|
2016-01-12 13:47:02 +00:00
|
|
|
if (t !== 0 && t !== nonEmptyList[nonEmptyList.length - 1]) {
|
2015-05-15 04:58:13 +00:00
|
|
|
nonEmptyList.push(t);
|
|
|
|
}
|
2015-09-25 10:17:08 +00:00
|
|
|
_scrollMap[t] = Math.round($el.offset().top + offset - 10);
|
2015-06-01 10:04:25 +00:00
|
|
|
}
|
2015-05-15 04:58:13 +00:00
|
|
|
|
|
|
|
nonEmptyList.push(linesCount);
|
|
|
|
_scrollMap[linesCount] = ui.area.view[0].scrollHeight;
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
for (i = 1; i < linesCount; i++) {
|
|
|
|
if (_scrollMap[i] !== -1) {
|
|
|
|
pos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = nonEmptyList[pos];
|
|
|
|
b = nonEmptyList[pos + 1];
|
|
|
|
_scrollMap[i] = Math.round((_scrollMap[b] * (i - a) + _scrollMap[a] * (b - i)) / (b - a));
|
|
|
|
}
|
|
|
|
|
|
|
|
_scrollMap[0] = 0;
|
|
|
|
|
|
|
|
scrollMap = _scrollMap;
|
|
|
|
lineHeightMap = _lineHeightMap;
|
2015-06-01 10:04:25 +00:00
|
|
|
|
2015-07-02 12:30:43 +00:00
|
|
|
if (loaded && syncBack)
|
2015-06-01 10:04:25 +00:00
|
|
|
syncScrollToView();
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPartByEditorLineNo(lineNo) {
|
|
|
|
var part = null;
|
|
|
|
ui.area.markdown.find('.part').each(function (n, el) {
|
|
|
|
if (part) return;
|
|
|
|
var $el = $(el),
|
|
|
|
t = $el.data('startline') - 1,
|
|
|
|
f = $el.data('endline') - 1;
|
|
|
|
if (t === '' || f === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (lineNo >= t && lineNo <= f) {
|
|
|
|
part = $el;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (part)
|
|
|
|
return {
|
|
|
|
startline: part.data('startline') - 1,
|
|
|
|
endline: part.data('endline') - 1,
|
|
|
|
linediff: Math.abs(part.data('endline') - part.data('startline')) + 1,
|
|
|
|
element: part
|
|
|
|
};
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEditorLineNoByTop(top) {
|
|
|
|
for (var i = 0; i < lineHeightMap.length; i++)
|
|
|
|
if (lineHeightMap[i] * editor.defaultTextHeight() > top)
|
|
|
|
return i;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-06-01 10:04:25 +00:00
|
|
|
function syncScrollToView(event, _lineNo) {
|
|
|
|
if (currentMode != modeType.both) return;
|
2015-09-25 10:05:50 +00:00
|
|
|
if (preventSyncScroll) {
|
2016-05-16 15:02:59 +00:00
|
|
|
if (typeof preventSyncScroll === 'number') {
|
|
|
|
preventSyncScroll--;
|
|
|
|
} else {
|
|
|
|
preventSyncScroll = false;
|
|
|
|
}
|
2015-09-25 10:05:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-15 04:58:13 +00:00
|
|
|
var lineNo, posTo;
|
|
|
|
var scrollInfo = editor.getScrollInfo();
|
|
|
|
if (!scrollMap || !lineHeightMap) {
|
2015-06-01 10:04:25 +00:00
|
|
|
buildMap(true);
|
|
|
|
return;
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2015-06-01 10:04:25 +00:00
|
|
|
if (!_lineNo) {
|
2015-05-15 04:58:13 +00:00
|
|
|
var topDiffPercent, posToNextDiff;
|
|
|
|
var textHeight = editor.defaultTextHeight();
|
|
|
|
lineNo = Math.floor(scrollInfo.top / textHeight);
|
2016-02-11 20:36:05 +00:00
|
|
|
// 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);
|
2015-05-15 04:58:13 +00:00
|
|
|
} else {
|
|
|
|
topDiffPercent = (scrollInfo.top % textHeight) / textHeight;
|
|
|
|
posTo = scrollMap[lineNo];
|
|
|
|
posToNextDiff = (scrollMap[lineNo + 1] - posTo) * topDiffPercent;
|
|
|
|
posTo += Math.floor(posToNextDiff);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (viewScrolling) return;
|
|
|
|
posTo = scrollMap[lineHeightMap[_lineNo]];
|
|
|
|
}
|
|
|
|
var posDiff = Math.abs(ui.area.view.scrollTop() - posTo);
|
2015-06-01 10:04:25 +00:00
|
|
|
var duration = posDiff / 50;
|
|
|
|
ui.area.view.stop(true, true).animate({
|
|
|
|
scrollTop: posTo
|
|
|
|
}, duration >= 100 ? duration : 100, "linear");
|
|
|
|
/*
|
2015-05-15 04:58:13 +00:00
|
|
|
if (posDiff > scrollInfo.clientHeight / 5) {
|
|
|
|
var duration = posDiff / 50;
|
2015-06-01 10:04:25 +00:00
|
|
|
ui.area.view.stop(true, true).animate({
|
2015-05-15 04:58:13 +00:00
|
|
|
scrollTop: posTo
|
2015-06-01 10:04:25 +00:00
|
|
|
}, duration >= 100 ? duration : 100, "linear");
|
2015-05-15 04:58:13 +00:00
|
|
|
} else {
|
2015-06-01 10:04:25 +00:00
|
|
|
ui.area.view.stop(true, true).scrollTop(posTo);
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2015-06-01 10:04:25 +00:00
|
|
|
*/
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|