Skip to content

Commit fe3950e

Browse files
committed
Optimized buildMap, now can parse whole doc below 10ms
1 parent 36451dd commit fe3950e

1 file changed

Lines changed: 3 additions & 23 deletions

File tree

public/js/syncscroll.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,7 @@ var buildMap = _.throttle(buildMapInner, buildMapThrottle);
177177
// Optimizations are required only for big texts.
178178
function buildMapInner(syncBack) {
179179
var i, offset, nonEmptyList, pos, a, b, _lineHeightMap, linesCount,
180-
acc, sourceLikeDiv, textarea = ui.area.codemirror,
181-
wrap = $('.CodeMirror-wrap pre'),
182-
_scrollMap;
183-
184-
sourceLikeDiv = $('<div />').css({
185-
position: 'absolute',
186-
visibility: 'hidden',
187-
height: 'auto',
188-
width: wrap.width(),
189-
'font-size': textarea.css('font-size'),
190-
'font-family': textarea.css('font-family'),
191-
'line-height': textarea.css('line-height'),
192-
'word-wrap': wrap.css('word-wrap'),
193-
'white-space': wrap.css('white-space'),
194-
'word-break': wrap.css('word-break'),
195-
'tab-size': '38px'
196-
}).appendTo('body');
180+
acc, _scrollMap;
197181

198182
offset = ui.area.view.scrollTop() - ui.area.view.offset().top;
199183
_scrollMap = [];
@@ -202,11 +186,9 @@ function buildMapInner(syncBack) {
202186

203187
acc = 0;
204188
var lines = editor.getValue().split('\n');
205-
var lineHeight = parseFloat(sourceLikeDiv.css('line-height'));
206-
var div = sourceLikeDiv[0];
189+
var lineHeight = editor.defaultTextHeight();
207190
for (i = 0; i < lines.length; i++) {
208191
var str = lines[i];
209-
var h, lh;
210192

211193
_lineHeightMap.push(acc);
212194

@@ -215,11 +197,9 @@ function buildMapInner(syncBack) {
215197
continue;
216198
}
217199

218-
sourceLikeDiv.text(str);
219-
h = parseFloat(div.clientHeight);
200+
var h = editor.heightAtLine(i + 1) - editor.heightAtLine(i);
220201
acc += Math.round(h / lineHeight);
221202
}
222-
sourceLikeDiv.remove();
223203
_lineHeightMap.push(acc);
224204
linesCount = acc;
225205

0 commit comments

Comments
 (0)