2016-02-22 00:58:06 +00:00
|
|
|
// Inject line numbers for sync scroll.
|
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
import markdownitContainer from 'markdown-it-container';
|
|
|
|
|
|
|
|
import { md } from './extra';
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
function addPart(tokens, idx) {
|
|
|
|
if (tokens[idx].map && tokens[idx].level === 0) {
|
2017-01-04 15:01:44 +00:00
|
|
|
const startline = tokens[idx].map[0] + 1;
|
|
|
|
const endline = tokens[idx].map[1];
|
2016-02-22 00:58:06 +00:00
|
|
|
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);
|
2017-01-04 15:01:44 +00:00
|
|
|
return self.renderToken(...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);
|
2017-01-04 15:01:44 +00:00
|
|
|
return self.renderToken(...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);
|
2017-01-04 15:01:44 +00:00
|
|
|
return self.renderToken(...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) {
|
2017-01-04 15:01:44 +00:00
|
|
|
const startline = tokens[idx].map[0] + 1;
|
|
|
|
const endline = tokens[idx].map[1];
|
2016-03-04 15:20:17 +00:00
|
|
|
tokens[idx].attrJoin('data-startline', startline);
|
|
|
|
tokens[idx].attrJoin('data-endline', endline);
|
|
|
|
}
|
2017-01-04 15:01:44 +00:00
|
|
|
return self.renderToken(...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);
|
2017-01-04 15:01:44 +00:00
|
|
|
return self.renderToken(...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);
|
2017-01-04 15:01:44 +00:00
|
|
|
return self.renderToken(...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);
|
2017-01-04 15:01:44 +00:00
|
|
|
return self.renderToken(...arguments);
|
2016-02-22 00:58:06 +00:00
|
|
|
};
|
|
|
|
md.renderer.rules.heading_open = function (tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('class', 'raw');
|
|
|
|
addPart(tokens, idx);
|
2017-01-04 15:01:44 +00:00
|
|
|
return self.renderToken(...arguments);
|
2015-05-15 04:58:13 +00:00
|
|
|
};
|
2017-01-04 15:01:44 +00:00
|
|
|
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
|
|
|
|
const token = tokens[idx];
|
|
|
|
const info = token.info ? md.utils.unescapeAll(token.info).trim() : '';
|
|
|
|
let langName = '';
|
|
|
|
let highlighted;
|
2016-02-22 00:58:06 +00:00
|
|
|
|
|
|
|
if (info) {
|
|
|
|
langName = info.split(/\s+/g)[0];
|
2016-10-14 09:35:35 +00:00
|
|
|
if (/\!$/.test(info)) token.attrJoin('class', 'wrap');
|
|
|
|
token.attrJoin('class', options.langPrefix + langName.replace(/\=$|\=\d+$|\=\+$|\!$|\=\!/, ''));
|
2016-02-22 00:58:06 +00:00
|
|
|
token.attrJoin('class', 'hljs');
|
2016-10-17 16:14:50 +00:00
|
|
|
token.attrJoin('class', 'raw');
|
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) {
|
2017-01-04 15:01:44 +00:00
|
|
|
return `${highlighted}\n`;
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
if (tokens[idx].map && tokens[idx].level === 0) {
|
2017-01-04 15:01:44 +00:00
|
|
|
const startline = tokens[idx].map[0] + 1;
|
|
|
|
const 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
|
|
|
}
|
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
return `<pre><code${self.renderAttrs(token)}>${highlighted}</code></pre>\n`;
|
2016-02-22 00:58:06 +00:00
|
|
|
};
|
2017-01-04 15:01:44 +00:00
|
|
|
md.renderer.rules.code_block = (tokens, idx, options, env, self) => {
|
2016-02-22 00:58:06 +00:00
|
|
|
if (tokens[idx].map && tokens[idx].level === 0) {
|
2017-01-04 15:01:44 +00:00
|
|
|
const startline = tokens[idx].map[0] + 1;
|
|
|
|
const 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
|
|
|
}
|
2017-01-04 15:01:44 +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');
|
2017-01-04 15:01:44 +00:00
|
|
|
tokens[idx].attrJoin('class', `alert-${tokens[idx].info.trim()}`);
|
2016-03-15 02:56:53 +00:00
|
|
|
addPart(tokens, idx);
|
2017-01-04 15:01:44 +00:00
|
|
|
return self.renderToken(...arguments);
|
2016-03-15 02:56:53 +00:00
|
|
|
}
|
2016-10-08 12:02:30 +00:00
|
|
|
|
|
|
|
md.use(markdownitContainer, 'success', { render: renderContainer });
|
|
|
|
md.use(markdownitContainer, 'info', { render: renderContainer });
|
|
|
|
md.use(markdownitContainer, 'warning', { render: renderContainer });
|
|
|
|
md.use(markdownitContainer, 'danger', { render: renderContainer });
|
2015-05-15 04:58:13 +00:00
|
|
|
|
2016-10-13 06:03:02 +00:00
|
|
|
// FIXME: expose syncscroll to window
|
|
|
|
window.syncscroll = true;
|
2016-05-26 05:17:00 +00:00
|
|
|
|
2016-10-13 16:25:08 +00:00
|
|
|
window.preventSyncScrollToEdit = false;
|
|
|
|
window.preventSyncScrollToView = false;
|
2015-09-25 10:05:50 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
const editScrollThrottle = 5;
|
|
|
|
const viewScrollThrottle = 5;
|
|
|
|
const buildMapThrottle = 100;
|
2015-06-01 10:04:25 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
let viewScrolling = false;
|
|
|
|
let editScrolling = false;
|
2016-05-25 05:25:05 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
let editArea = null;
|
|
|
|
let viewArea = null;
|
|
|
|
let markdownArea = null;
|
2016-09-18 08:51:19 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
export function setupSyncAreas(edit, view, markdown) {
|
2016-09-18 08:51:19 +00:00
|
|
|
editArea = edit;
|
|
|
|
viewArea = view;
|
|
|
|
markdownArea = markdown;
|
|
|
|
editArea.on('scroll', _.throttle(syncScrollToView, editScrollThrottle));
|
|
|
|
viewArea.on('scroll', _.throttle(syncScrollToEdit, viewScrollThrottle));
|
|
|
|
}
|
2016-05-25 05:25:05 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
let scrollMap, lineHeightMap, viewTop, viewBottom;
|
2015-05-15 04:58:13 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
export function clearMap() {
|
2015-05-15 04:58:13 +00:00
|
|
|
scrollMap = null;
|
|
|
|
lineHeightMap = null;
|
2016-02-11 20:36:05 +00:00
|
|
|
viewTop = null;
|
|
|
|
viewBottom = null;
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2017-01-04 15:01:44 +00:00
|
|
|
window.viewAjaxCallback = clearMap;
|
2015-05-15 04:58:13 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
const buildMap = _.throttle(buildMapInner, buildMapThrottle);
|
2015-06-01 10:04:25 +00:00
|
|
|
|
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.
|
2016-05-26 16:12:07 +00:00
|
|
|
function buildMapInner(callback) {
|
2016-09-18 08:51:19 +00:00
|
|
|
if (!viewArea || !markdownArea) return;
|
2017-01-04 15:01:44 +00:00
|
|
|
let i, offset, nonEmptyList, pos, a, b, _lineHeightMap, linesCount, acc, _scrollMap;
|
2015-05-15 04:58:13 +00:00
|
|
|
|
2016-09-18 08:51:19 +00:00
|
|
|
offset = viewArea.scrollTop() - viewArea.offset().top;
|
2015-05-15 04:58:13 +00:00
|
|
|
_scrollMap = [];
|
|
|
|
nonEmptyList = [];
|
|
|
|
_lineHeightMap = [];
|
2016-02-11 20:36:05 +00:00
|
|
|
viewTop = 0;
|
2016-09-18 08:51:19 +00:00
|
|
|
viewBottom = viewArea[0].scrollHeight - viewArea.height();
|
2015-05-15 04:58:13 +00:00
|
|
|
|
|
|
|
acc = 0;
|
2017-01-04 15:01:44 +00:00
|
|
|
const lines = editor.getValue().split('\n');
|
|
|
|
const lineHeight = editor.defaultTextHeight();
|
2015-06-01 10:04:25 +00:00
|
|
|
for (i = 0; i < lines.length; i++) {
|
2017-01-04 15:01:44 +00:00
|
|
|
const 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
|
|
|
}
|
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
const 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
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
const parts = markdownArea.find('.part').toArray();
|
2015-06-01 10:04:25 +00:00
|
|
|
for (i = 0; i < parts.length; i++) {
|
2017-01-04 15:01:44 +00:00
|
|
|
const $el = $(parts[i]);
|
|
|
|
let 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);
|
2016-09-18 08:51:19 +00:00
|
|
|
_scrollMap[linesCount] = viewArea[0].scrollHeight;
|
2015-05-15 04:58:13 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-05-26 16:12:07 +00:00
|
|
|
if (loaded && callback) callback();
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 16:31:43 +00:00
|
|
|
// sync view scroll progress to edit
|
2017-01-04 15:01:44 +00:00
|
|
|
let viewScrollingTimer = null;
|
2016-05-26 05:17:00 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
export function syncScrollToEdit(event, preventAnimate) {
|
2016-09-18 08:51:19 +00:00
|
|
|
if (currentMode != modeType.both || !syncscroll || !editArea) return;
|
2016-05-26 05:17:00 +00:00
|
|
|
if (preventSyncScrollToEdit) {
|
|
|
|
if (typeof preventSyncScrollToEdit === 'number') {
|
|
|
|
preventSyncScrollToEdit--;
|
|
|
|
} else {
|
|
|
|
preventSyncScrollToEdit = false;
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2016-05-26 05:17:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!scrollMap || !lineHeightMap) {
|
2017-01-04 15:01:44 +00:00
|
|
|
buildMap(() => {
|
2016-05-29 05:58:32 +00:00
|
|
|
syncScrollToEdit(event, preventAnimate);
|
|
|
|
});
|
2016-05-26 05:17:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (editScrolling) return;
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
const scrollTop = viewArea[0].scrollTop;
|
|
|
|
let lineIndex = 0;
|
2016-05-26 05:17:00 +00:00
|
|
|
for (var i = 0, l = scrollMap.length; i < l; i++) {
|
|
|
|
if (scrollMap[i] > scrollTop) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
lineIndex = i;
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2016-05-26 05:17:00 +00:00
|
|
|
}
|
2017-01-04 15:01:44 +00:00
|
|
|
let lineNo = 0;
|
|
|
|
let lineDiff = 0;
|
2016-05-26 05:17:00 +00:00
|
|
|
for (var i = 0, l = lineHeightMap.length; i < l; i++) {
|
|
|
|
if (lineHeightMap[i] > lineIndex) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
lineNo = lineHeightMap[i];
|
|
|
|
lineDiff = lineHeightMap[i + 1] - lineNo;
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
let posTo = 0;
|
|
|
|
let topDiffPercent = 0;
|
|
|
|
let posToNextDiff = 0;
|
|
|
|
const scrollInfo = editor.getScrollInfo();
|
|
|
|
const textHeight = editor.defaultTextHeight();
|
|
|
|
const preLastLineHeight = scrollInfo.height - scrollInfo.clientHeight - textHeight;
|
|
|
|
const preLastLineNo = Math.round(preLastLineHeight / textHeight);
|
|
|
|
const preLastLinePos = scrollMap[preLastLineNo];
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2016-05-26 05:17:00 +00:00
|
|
|
if (scrollInfo.height > scrollInfo.clientHeight && scrollTop >= preLastLinePos) {
|
|
|
|
posTo = preLastLineHeight;
|
|
|
|
topDiffPercent = (scrollTop - preLastLinePos) / (viewBottom - preLastLinePos);
|
2016-05-26 16:12:07 +00:00
|
|
|
posToNextDiff = textHeight * topDiffPercent;
|
2016-05-29 05:58:32 +00:00
|
|
|
posTo += Math.ceil(posToNextDiff);
|
2016-05-26 05:17:00 +00:00
|
|
|
} else {
|
|
|
|
posTo = lineNo * textHeight;
|
|
|
|
topDiffPercent = (scrollTop - scrollMap[lineNo]) / (scrollMap[lineNo + lineDiff] - scrollMap[lineNo]);
|
2016-05-26 16:12:07 +00:00
|
|
|
posToNextDiff = textHeight * lineDiff * topDiffPercent;
|
2016-05-29 05:58:32 +00:00
|
|
|
posTo += Math.ceil(posToNextDiff);
|
2016-05-26 05:17:00 +00:00
|
|
|
}
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2016-05-29 05:58:32 +00:00
|
|
|
if (preventAnimate) {
|
2016-09-18 08:51:19 +00:00
|
|
|
editArea.scrollTop(posTo);
|
2016-05-29 05:58:32 +00:00
|
|
|
} else {
|
2017-01-04 15:01:44 +00:00
|
|
|
const posDiff = Math.abs(scrollInfo.top - posTo);
|
2016-05-29 05:58:32 +00:00
|
|
|
var duration = posDiff / 50;
|
|
|
|
duration = duration >= 100 ? duration : 100;
|
2016-09-18 08:51:19 +00:00
|
|
|
editArea.stop(true, true).animate({
|
2016-05-29 05:58:32 +00:00
|
|
|
scrollTop: posTo
|
|
|
|
}, duration, "linear");
|
|
|
|
}
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2016-05-26 05:17:00 +00:00
|
|
|
viewScrolling = true;
|
2016-05-27 16:31:43 +00:00
|
|
|
clearTimeout(viewScrollingTimer);
|
2016-05-29 05:58:32 +00:00
|
|
|
viewScrollingTimer = setTimeout(viewScrollingTimeoutInner, duration * 1.5);
|
2016-05-26 16:12:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 16:31:43 +00:00
|
|
|
function viewScrollingTimeoutInner() {
|
|
|
|
viewScrolling = false;
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 16:31:43 +00:00
|
|
|
// sync edit scroll progress to view
|
2017-01-04 15:01:44 +00:00
|
|
|
let editScrollingTimer = null;
|
2016-05-27 16:31:43 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
export function syncScrollToView(event, preventAnimate) {
|
2016-09-18 08:51:19 +00:00
|
|
|
if (currentMode != modeType.both || !syncscroll || !viewArea) return;
|
2016-05-26 05:17:00 +00:00
|
|
|
if (preventSyncScrollToView) {
|
|
|
|
if (typeof preventSyncScrollToView === 'number') {
|
|
|
|
preventSyncScrollToView--;
|
2016-05-16 15:02:59 +00:00
|
|
|
} else {
|
2016-05-26 05:17:00 +00:00
|
|
|
preventSyncScrollToView = false;
|
2016-05-16 15:02:59 +00:00
|
|
|
}
|
2015-09-25 10:05:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-15 04:58:13 +00:00
|
|
|
if (!scrollMap || !lineHeightMap) {
|
2017-01-04 15:01:44 +00:00
|
|
|
buildMap(() => {
|
2016-05-29 05:58:32 +00:00
|
|
|
syncScrollToView(event, preventAnimate);
|
|
|
|
});
|
2015-06-01 10:04:25 +00:00
|
|
|
return;
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2016-05-26 05:17:00 +00:00
|
|
|
if (viewScrolling) return;
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2017-01-04 15:01:44 +00:00
|
|
|
let lineNo, posTo;
|
|
|
|
let topDiffPercent, posToNextDiff;
|
|
|
|
const scrollInfo = editor.getScrollInfo();
|
|
|
|
const textHeight = editor.defaultTextHeight();
|
2016-05-29 05:58:32 +00:00
|
|
|
lineNo = Math.floor(scrollInfo.top / textHeight);
|
|
|
|
// if reach the last line, will start lerp to the bottom
|
2017-01-04 15:01:44 +00:00
|
|
|
const diffToBottom = (scrollInfo.top + scrollInfo.clientHeight) - (scrollInfo.height - textHeight);
|
2016-05-29 05:58:32 +00:00
|
|
|
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 {
|
2016-05-29 05:58:32 +00:00
|
|
|
topDiffPercent = (scrollInfo.top % textHeight) / textHeight;
|
|
|
|
posTo = scrollMap[lineNo];
|
|
|
|
posToNextDiff = (scrollMap[lineNo + 1] - posTo) * topDiffPercent;
|
|
|
|
posTo += Math.floor(posToNextDiff);
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2016-05-29 05:58:32 +00:00
|
|
|
if (preventAnimate) {
|
2016-09-18 08:51:19 +00:00
|
|
|
viewArea.scrollTop(posTo);
|
2016-05-29 05:58:32 +00:00
|
|
|
} else {
|
2017-01-04 15:01:44 +00:00
|
|
|
const posDiff = Math.abs(viewArea.scrollTop() - posTo);
|
2016-05-29 05:58:32 +00:00
|
|
|
var duration = posDiff / 50;
|
|
|
|
duration = duration >= 100 ? duration : 100;
|
2016-09-18 08:51:19 +00:00
|
|
|
viewArea.stop(true, true).animate({
|
2016-05-29 05:58:32 +00:00
|
|
|
scrollTop: posTo
|
|
|
|
}, duration, "linear");
|
|
|
|
}
|
2016-10-08 12:02:30 +00:00
|
|
|
|
2016-05-25 05:25:05 +00:00
|
|
|
editScrolling = true;
|
2016-05-27 16:31:43 +00:00
|
|
|
clearTimeout(editScrollingTimer);
|
2016-05-29 05:58:32 +00:00
|
|
|
editScrollingTimer = setTimeout(editScrollingTimeoutInner, duration * 1.5);
|
2016-05-27 16:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function editScrollingTimeoutInner() {
|
|
|
|
editScrolling = false;
|
2016-10-08 12:02:30 +00:00
|
|
|
}
|