optimize longest line calculation
This commit is contained in:
parent
f8642b9339
commit
9315c374e6
2 changed files with 44 additions and 26 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
function TextDocument(text) {
|
function TextDocument(text)
|
||||||
|
{
|
||||||
this.lines = this._split(text);
|
this.lines = this._split(text);
|
||||||
|
this.modified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
TextDocument.prototype =
|
TextDocument.prototype =
|
||||||
|
|
@ -8,6 +10,22 @@ TextDocument.prototype =
|
||||||
return text.split(/[\n\r]/)
|
return text.split(/[\n\r]/)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getWidth : function()
|
||||||
|
{
|
||||||
|
if (this.modified)
|
||||||
|
{
|
||||||
|
this.modified = false;
|
||||||
|
|
||||||
|
var lines = this.lines;
|
||||||
|
var longestLine = 0;
|
||||||
|
for (var i=0; i < lines.length; i++) {
|
||||||
|
longestLine = Math.max(longestLine, lines[i].length);
|
||||||
|
}
|
||||||
|
this.width = longestLine;
|
||||||
|
}
|
||||||
|
return this.width;
|
||||||
|
},
|
||||||
|
|
||||||
getLine : function(row) {
|
getLine : function(row) {
|
||||||
return this.lines[row] || "";
|
return this.lines[row] || "";
|
||||||
},
|
},
|
||||||
|
|
@ -72,8 +90,24 @@ TextDocument.prototype =
|
||||||
return this.lines.length;
|
return this.lines.length;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getTextRange : function(range)
|
||||||
|
{
|
||||||
|
if (range.start.row == range.end.row) {
|
||||||
|
return this.lines[range.start.row].substring(range.start.column, range.end.column);
|
||||||
|
} else {
|
||||||
|
var lines = [];
|
||||||
|
lines.push(this.lines[range.start.row].substring(range.start.column));
|
||||||
|
lines.push.apply(lines, this.lines.slice(range.start.row+1, range.end.row+1));
|
||||||
|
lines.push(this.lines[range.end.row].substring(0, range.end.column));
|
||||||
|
|
||||||
|
return lines.join("\n");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
insert : function(position, text)
|
insert : function(position, text)
|
||||||
{
|
{
|
||||||
|
this.modified = true;
|
||||||
|
|
||||||
var newLines = this._split(text);
|
var newLines = this._split(text);
|
||||||
|
|
||||||
if (text == "\n")
|
if (text == "\n")
|
||||||
|
|
@ -117,23 +151,11 @@ TextDocument.prototype =
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getTextRange : function(range)
|
|
||||||
{
|
|
||||||
if (range.start.row == range.end.row) {
|
|
||||||
return this.lines[range.start.row].substring(range.start.column, range.end.column);
|
|
||||||
} else {
|
|
||||||
var lines = [];
|
|
||||||
lines.push(this.lines[range.start.row].substring(range.start.column));
|
|
||||||
lines.push.apply(lines, this.lines.slice(range.start.row+1, range.end.row+1));
|
|
||||||
lines.push(this.lines[range.end.row].substring(0, range.end.column));
|
|
||||||
|
|
||||||
return lines.join("\n");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
remove : function(range)
|
remove : function(range)
|
||||||
{
|
{
|
||||||
|
this.modified = true;
|
||||||
|
|
||||||
var firstRow = range.start.row;
|
var firstRow = range.start.row;
|
||||||
var lastRow = range.end.row;
|
var lastRow = range.end.row;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ function VirtualRenderer(containerId)
|
||||||
VirtualRenderer.prototype.setDocument = function(doc)
|
VirtualRenderer.prototype.setDocument = function(doc)
|
||||||
{
|
{
|
||||||
this.lines = doc.lines;
|
this.lines = doc.lines;
|
||||||
|
this.doc = doc;
|
||||||
this.textLayer.setDocument(doc);
|
this.textLayer.setDocument(doc);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -32,23 +33,18 @@ VirtualRenderer.prototype.getContainerElement = function() {
|
||||||
return this.container;
|
return this.container;
|
||||||
};
|
};
|
||||||
|
|
||||||
VirtualRenderer.prototype.getLongestLineWidth = function(lines)
|
|
||||||
{
|
|
||||||
var longestLine = this.container.clientWidth;
|
|
||||||
for (var i=0; i < lines.length; i++) {
|
|
||||||
longestLine = Math.max(longestLine, (lines[i].length * this.characterWidth));
|
|
||||||
}
|
|
||||||
return longestLine;
|
|
||||||
};
|
|
||||||
|
|
||||||
VirtualRenderer.prototype.draw = function()
|
VirtualRenderer.prototype.draw = function()
|
||||||
{
|
{
|
||||||
var lines = this.lines;
|
var lines = this.lines;
|
||||||
|
|
||||||
var offset = this.scrollTop % this.lineHeight;
|
var offset = this.scrollTop % this.lineHeight;
|
||||||
var minHeight = this.container.clientHeight + offset;
|
var minHeight = this.scroller.clientHeight + offset;
|
||||||
|
|
||||||
|
var longestLine = Math.max(
|
||||||
|
this.scroller.clientWidth,
|
||||||
|
this.doc.getWidth() * this.characterWidth
|
||||||
|
);
|
||||||
|
|
||||||
var longestLine = this.getLongestLineWidth(lines);
|
|
||||||
var lineCount = Math.ceil(minHeight / this.lineHeight);
|
var lineCount = Math.ceil(minHeight / this.lineHeight);
|
||||||
var firstRow = Math.round((this.scrollTop - offset) / this.lineHeight);
|
var firstRow = Math.round((this.scrollTop - offset) / this.lineHeight);
|
||||||
var lastRow = Math.min(lines.length, firstRow+lineCount);
|
var lastRow = Math.min(lines.length, firstRow+lineCount);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue