Optimized buildMap, now can parse whole doc below 10ms

This commit is contained in:
Wu Cheng-Han 2015-09-25 18:15:44 +08:00
parent 36451ddbe9
commit fe3950ec9c

View file

@ -177,23 +177,7 @@ var buildMap = _.throttle(buildMapInner, buildMapThrottle);
// Optimizations are required only for big texts. // Optimizations are required only for big texts.
function buildMapInner(syncBack) { function buildMapInner(syncBack) {
var i, offset, nonEmptyList, pos, a, b, _lineHeightMap, linesCount, var i, offset, nonEmptyList, pos, a, b, _lineHeightMap, linesCount,
acc, sourceLikeDiv, textarea = ui.area.codemirror, acc, _scrollMap;
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');
offset = ui.area.view.scrollTop() - ui.area.view.offset().top; offset = ui.area.view.scrollTop() - ui.area.view.offset().top;
_scrollMap = []; _scrollMap = [];
@ -202,11 +186,9 @@ function buildMapInner(syncBack) {
acc = 0; acc = 0;
var lines = editor.getValue().split('\n'); var lines = editor.getValue().split('\n');
var lineHeight = parseFloat(sourceLikeDiv.css('line-height')); var lineHeight = editor.defaultTextHeight();
var div = sourceLikeDiv[0];
for (i = 0; i < lines.length; i++) { for (i = 0; i < lines.length; i++) {
var str = lines[i]; var str = lines[i];
var h, lh;
_lineHeightMap.push(acc); _lineHeightMap.push(acc);
@ -215,11 +197,9 @@ function buildMapInner(syncBack) {
continue; continue;
} }
sourceLikeDiv.text(str); var h = editor.heightAtLine(i + 1) - editor.heightAtLine(i);
h = parseFloat(div.clientHeight);
acc += Math.round(h / lineHeight); acc += Math.round(h / lineHeight);
} }
sourceLikeDiv.remove();
_lineHeightMap.push(acc); _lineHeightMap.push(acc);
linesCount = acc; linesCount = acc;