summaryrefslogtreecommitdiff
path: root/public/js/syncscroll.js
diff options
context:
space:
mode:
authorWu Cheng-Han2015-09-25 18:15:44 +0800
committerWu Cheng-Han2015-09-25 18:15:44 +0800
commitfe3950ec9c92a97ae2238d0e5ed57f57baa594f3 (patch)
treef6f3c28b28d1b169c89d102614f22757a02e0f26 /public/js/syncscroll.js
parent36451ddbe9264236a818c66c4f72455ed5b44278 (diff)
Optimized buildMap, now can parse whole doc below 10ms
Diffstat (limited to 'public/js/syncscroll.js')
-rw-r--r--public/js/syncscroll.js26
1 files changed, 3 insertions, 23 deletions
diff --git a/public/js/syncscroll.js b/public/js/syncscroll.js
index 66d68e64..30ff43ce 100644
--- a/public/js/syncscroll.js
+++ b/public/js/syncscroll.js
@@ -177,23 +177,7 @@ var buildMap = _.throttle(buildMapInner, buildMapThrottle);
// Optimizations are required only for big texts.
function buildMapInner(syncBack) {
var i, offset, nonEmptyList, pos, a, b, _lineHeightMap, linesCount,
- acc, sourceLikeDiv, textarea = ui.area.codemirror,
- wrap = $('.CodeMirror-wrap pre'),
- _scrollMap;
-
- sourceLikeDiv = $('<div />').css({
- position: 'absolute',
- visibility: 'hidden',
- height: 'auto',
- width: wrap.width(),
- 'font-size': textarea.css('font-size'),
- 'font-family': textarea.css('font-family'),
- 'line-height': textarea.css('line-height'),
- 'word-wrap': wrap.css('word-wrap'),
- 'white-space': wrap.css('white-space'),
- 'word-break': wrap.css('word-break'),
- 'tab-size': '38px'
- }).appendTo('body');
+ acc, _scrollMap;
offset = ui.area.view.scrollTop() - ui.area.view.offset().top;
_scrollMap = [];
@@ -202,11 +186,9 @@ function buildMapInner(syncBack) {
acc = 0;
var lines = editor.getValue().split('\n');
- var lineHeight = parseFloat(sourceLikeDiv.css('line-height'));
- var div = sourceLikeDiv[0];
+ var lineHeight = editor.defaultTextHeight();
for (i = 0; i < lines.length; i++) {
var str = lines[i];
- var h, lh;
_lineHeightMap.push(acc);
@@ -215,11 +197,9 @@ function buildMapInner(syncBack) {
continue;
}
- sourceLikeDiv.text(str);
- h = parseFloat(div.clientHeight);
+ var h = editor.heightAtLine(i + 1) - editor.heightAtLine(i);
acc += Math.round(h / lineHeight);
}
- sourceLikeDiv.remove();
_lineHeightMap.push(acc);
linesCount = acc;