From 9616d5631f813a344d954efe00adf06bb59638a9 Mon Sep 17 00:00:00 2001 From: ryrun Date: Wed, 7 Dec 2011 14:23:28 +0100 Subject: [PATCH] Speed up line numbering of huge pads. Tested on chrome and firefox. Pad with more than 6000 lines will be loaded fast. It refresh first all current line numbers, then creates new with DocumentFragment and style.height info. Reflow(rebuild and rendering of dom) will be reduced to one for new ones, instead for each ones. --- static/js/ace2_inner.js | 58 +++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/static/js/ace2_inner.js b/static/js/ace2_inner.js index 6c8ec60e..bb45ac5f 100644 --- a/static/js/ace2_inner.js +++ b/static/js/ace2_inner.js @@ -5657,31 +5657,17 @@ function OUTER(gscope) { var newNumLines = rep.lines.length(); if (newNumLines < 1) newNumLines = 1; - if (newNumLines != lineNumbersShown) - { - var container = sideDivInner; - var odoc = outerWin.document; - while (lineNumbersShown < newNumLines) - { - lineNumbersShown++; - var n = lineNumbersShown; - var div = odoc.createElement("DIV"); - div.appendChild(odoc.createTextNode(String(n))); - container.appendChild(div); - } - while (lineNumbersShown > newNumLines) - { - container.removeChild(container.lastChild); - lineNumbersShown--; - } - } - + //update height of all current line numbers if (currentCallStack && currentCallStack.domClean) { var a = sideDivInner.firstChild; var b = doc.body.firstChild; + var n = 0; while (a && b) { + if(n > lineNumbersShown) //all updated, break + break; + var h = (b.clientHeight || b.offsetHeight); if (b.nextSibling) { @@ -5695,10 +5681,42 @@ function OUTER(gscope) if (h) { var hpx = h + "px"; - if (a.style.height != hpx) a.style.height = hpx; + if (a.style.height != hpx) { + a.style.height = hpx; + } } a = a.nextSibling; b = b.nextSibling; + n++; + } + } + + if (newNumLines != lineNumbersShown) + { + var container = sideDivInner; + var odoc = outerWin.document; + var fragment = odoc.createDocumentFragment(); + while (lineNumbersShown < newNumLines) + { + lineNumbersShown++; + var n = lineNumbersShown; + var div = odoc.createElement("DIV"); + //calculate height for new line number + var h = (b.clientHeight || b.offsetHeight); + if (b.nextSibling) + h = b.nextSibling.offsetTop - b.offsetTop; + if(h) // apply style to div + div.style.height = h +"px"; + + div.appendChild(odoc.createTextNode(String(n))); + fragment.appendChild(div); + b = b.nextSibling; + } + container.appendChild(fragment); + while (lineNumbersShown > newNumLines) + { + container.removeChild(container.lastChild); + lineNumbersShown--; } } }