2011-03-26 14:10:41 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function makeVirtualLineView(lineNode)
|
|
|
|
{
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
// how much to jump forward or backward at once in a charSeeker before
|
|
|
|
// constructing a DOM node and checking the coordinates (which takes a
|
|
|
|
// significant fraction of a millisecond). From the
|
|
|
|
// coordinates and the approximate line height we can estimate how
|
|
|
|
// many lines we have moved. We risk being off if the number of lines
|
|
|
|
// we move is on the order of the line height in pixels. Fortunately,
|
|
|
|
// when the user boosts the font-size they increase both.
|
|
|
|
var maxCharIncrement = 20;
|
|
|
|
var seekerAtEnd = null;
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function getNumChars()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return lineNode.textContent.length;
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
function getNumVirtualLines()
|
|
|
|
{
|
|
|
|
if (!seekerAtEnd)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var seeker = makeCharSeeker();
|
|
|
|
seeker.forwardByWhile(maxCharIncrement);
|
|
|
|
seekerAtEnd = seeker;
|
|
|
|
}
|
|
|
|
return seekerAtEnd.getVirtualLine() + 1;
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function getVLineAndOffsetForChar(lineChar)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var seeker = makeCharSeeker();
|
|
|
|
seeker.forwardByWhile(maxCharIncrement, null, lineChar);
|
|
|
|
var theLine = seeker.getVirtualLine();
|
2011-07-07 19:59:34 +02:00
|
|
|
seeker.backwardByWhile(8, function()
|
|
|
|
{
|
|
|
|
return seeker.getVirtualLine() == theLine;
|
|
|
|
});
|
|
|
|
seeker.forwardByWhile(1, function()
|
|
|
|
{
|
|
|
|
return seeker.getVirtualLine() != theLine;
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
var lineStartChar = seeker.getOffset();
|
2011-07-07 19:59:34 +02:00
|
|
|
return {
|
|
|
|
vline: theLine,
|
|
|
|
offset: (lineChar - lineStartChar)
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function getCharForVLineAndOffset(vline, offset)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
// returns revised vline and offset as well as absolute char index within line.
|
|
|
|
// if offset is beyond end of line, for example, will give new offset at end of line.
|
|
|
|
var seeker = makeCharSeeker();
|
|
|
|
// go to start of line
|
2011-07-07 19:59:34 +02:00
|
|
|
seeker.binarySearch(function()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return seeker.getVirtualLine() >= vline;
|
|
|
|
});
|
|
|
|
var lineStart = seeker.getOffset();
|
|
|
|
var theLine = seeker.getVirtualLine();
|
|
|
|
// go to offset, overshooting the virtual line only if offset is too large for it
|
2011-07-07 19:59:34 +02:00
|
|
|
seeker.forwardByWhile(maxCharIncrement, null, lineStart + offset);
|
2011-03-26 14:10:41 +01:00
|
|
|
// get back into line
|
2011-07-07 19:59:34 +02:00
|
|
|
seeker.backwardByWhile(1, function()
|
|
|
|
{
|
|
|
|
return seeker.getVirtualLine() != theLine;
|
|
|
|
}, lineStart);
|
2011-03-26 14:10:41 +01:00
|
|
|
var lineChar = seeker.getOffset();
|
|
|
|
var theOffset = lineChar - lineStart;
|
|
|
|
// handle case of last virtual line; should be able to be at end of it
|
2011-07-07 19:59:34 +02:00
|
|
|
if (theOffset < offset && theLine == (getNumVirtualLines() - 1))
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var lineLen = getNumChars();
|
2011-07-07 19:59:34 +02:00
|
|
|
theOffset += lineLen - lineChar;
|
2011-03-26 14:10:41 +01:00
|
|
|
lineChar = lineLen;
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
vline: theLine,
|
|
|
|
offset: theOffset,
|
|
|
|
lineChar: lineChar
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
return {
|
|
|
|
getNumVirtualLines: getNumVirtualLines,
|
|
|
|
getVLineAndOffsetForChar: getVLineAndOffsetForChar,
|
|
|
|
getCharForVLineAndOffset: getCharForVLineAndOffset,
|
|
|
|
makeCharSeeker: function()
|
|
|
|
{
|
|
|
|
return makeCharSeeker();
|
|
|
|
}
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function deepFirstChildTextNode(nd)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
nd = nd.firstChild;
|
|
|
|
while (nd && nd.firstChild) nd = nd.firstChild;
|
|
|
|
if (nd.data) return nd;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function makeCharSeeker( /*lineNode*/ )
|
|
|
|
{
|
|
|
|
|
|
|
|
function charCoords(tnode, i)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var container = tnode.parentNode;
|
|
|
|
|
|
|
|
// treat space specially; a space at the end of a virtual line
|
|
|
|
// will have weird coordinates
|
|
|
|
var isSpace = (tnode.nodeValue.charAt(i) === " ");
|
2011-07-07 19:59:34 +02:00
|
|
|
if (isSpace)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
if (container.previousSibling && deepFirstChildTextNode(container.previousSibling))
|
|
|
|
{
|
|
|
|
tnode = deepFirstChildTextNode(container.previousSibling);
|
|
|
|
i = tnode.length - 1;
|
|
|
|
container = tnode.parentNode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
top: container.offsetTop,
|
|
|
|
left: container.offsetLeft
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i--; // use previous char
|
|
|
|
}
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var charWrapper = document.createElement("SPAN");
|
|
|
|
|
|
|
|
// wrap the character
|
|
|
|
var tnodeText = tnode.nodeValue;
|
|
|
|
var frag = document.createDocumentFragment();
|
|
|
|
frag.appendChild(document.createTextNode(tnodeText.substring(0, i)));
|
|
|
|
charWrapper.appendChild(document.createTextNode(tnodeText.substr(i, 1)));
|
|
|
|
frag.appendChild(charWrapper);
|
2011-07-07 19:59:34 +02:00
|
|
|
frag.appendChild(document.createTextNode(tnodeText.substring(i + 1)));
|
2011-03-26 14:10:41 +01:00
|
|
|
container.replaceChild(frag, tnode);
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
var result = {
|
|
|
|
top: charWrapper.offsetTop,
|
|
|
|
left: charWrapper.offsetLeft + (isSpace ? charWrapper.offsetWidth : 0),
|
|
|
|
height: charWrapper.offsetHeight
|
|
|
|
};
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
while (container.firstChild) container.removeChild(container.firstChild);
|
|
|
|
container.appendChild(tnode);
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
var lineText = lineNode.textContent;
|
|
|
|
var lineLength = lineText.length;
|
|
|
|
|
|
|
|
var curNode = null;
|
|
|
|
var curChar = 0;
|
|
|
|
var curCharWithinNode = 0
|
|
|
|
var curTop;
|
|
|
|
var curLeft;
|
|
|
|
var approxLineHeight;
|
|
|
|
var whichLine = 0;
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function nextNode()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var n = curNode;
|
2011-07-07 19:59:34 +02:00
|
|
|
if (!n) n = lineNode.firstChild;
|
2011-03-26 14:10:41 +01:00
|
|
|
else n = n.nextSibling;
|
2011-07-07 19:59:34 +02:00
|
|
|
while (n && !deepFirstChildTextNode(n))
|
|
|
|
{
|
|
|
|
n = n.nextSibling;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
function prevNode()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var n = curNode;
|
2011-07-07 19:59:34 +02:00
|
|
|
if (!n) n = lineNode.lastChild;
|
2011-03-26 14:10:41 +01:00
|
|
|
else n = n.previousSibling;
|
2011-07-07 19:59:34 +02:00
|
|
|
while (n && !deepFirstChildTextNode(n))
|
|
|
|
{
|
|
|
|
n = n.previousSibling;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
var seeker;
|
2011-07-07 19:59:34 +02:00
|
|
|
if (lineLength > 0)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
curNode = nextNode();
|
|
|
|
var firstCharData = charCoords(deepFirstChildTextNode(curNode), 0);
|
|
|
|
approxLineHeight = firstCharData.height;
|
|
|
|
curTop = firstCharData.top;
|
|
|
|
curLeft = firstCharData.left;
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function updateCharData(tnode, i)
|
|
|
|
{
|
|
|
|
var coords = charCoords(tnode, i);
|
|
|
|
whichLine += Math.round((coords.top - curTop) / approxLineHeight);
|
|
|
|
curTop = coords.top;
|
|
|
|
curLeft = coords.left;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
seeker = {
|
2011-07-07 19:59:34 +02:00
|
|
|
forward: function(numChars)
|
|
|
|
{
|
|
|
|
var oldChar = curChar;
|
|
|
|
var newChar = curChar + numChars;
|
|
|
|
if (newChar > (lineLength - 1)) newChar = lineLength - 1;
|
|
|
|
while (curChar < newChar)
|
|
|
|
{
|
|
|
|
var curNodeLength = deepFirstChildTextNode(curNode).length;
|
|
|
|
var toGo = curNodeLength - curCharWithinNode;
|
|
|
|
if (curChar + toGo > newChar || !nextNode())
|
|
|
|
{
|
|
|
|
// going to next node would be too far
|
|
|
|
var n = newChar - curChar;
|
|
|
|
if (n >= toGo) n = toGo - 1;
|
|
|
|
curChar += n;
|
|
|
|
curCharWithinNode += n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// go to next node
|
|
|
|
curChar += toGo;
|
|
|
|
curCharWithinNode = 0;
|
|
|
|
curNode = nextNode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateCharData(deepFirstChildTextNode(curNode), curCharWithinNode);
|
|
|
|
return curChar - oldChar;
|
|
|
|
},
|
|
|
|
backward: function(numChars)
|
|
|
|
{
|
|
|
|
var oldChar = curChar;
|
|
|
|
var newChar = curChar - numChars;
|
|
|
|
if (newChar < 0) newChar = 0;
|
|
|
|
while (curChar > newChar)
|
|
|
|
{
|
|
|
|
if (curChar - curCharWithinNode <= newChar || !prevNode())
|
|
|
|
{
|
|
|
|
// going to prev node would be too far
|
|
|
|
var n = curChar - newChar;
|
|
|
|
if (n > curCharWithinNode) n = curCharWithinNode;
|
|
|
|
curChar -= n;
|
|
|
|
curCharWithinNode -= n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// go to prev node
|
|
|
|
curChar -= curCharWithinNode + 1;
|
|
|
|
curNode = prevNode();
|
|
|
|
curCharWithinNode = deepFirstChildTextNode(curNode).length - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateCharData(deepFirstChildTextNode(curNode), curCharWithinNode);
|
|
|
|
return oldChar - curChar;
|
|
|
|
},
|
|
|
|
getVirtualLine: function()
|
|
|
|
{
|
|
|
|
return whichLine;
|
|
|
|
},
|
|
|
|
getLeftCoord: function()
|
|
|
|
{
|
|
|
|
return curLeft;
|
|
|
|
}
|
2011-03-26 14:10:41 +01:00
|
|
|
};
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
else
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
curLeft = lineNode.offsetLeft;
|
2011-07-07 19:59:34 +02:00
|
|
|
seeker = {
|
|
|
|
forward: function(numChars)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
backward: function(numChars)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
getVirtualLine: function()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
getLeftCoord: function()
|
|
|
|
{
|
|
|
|
return curLeft;
|
|
|
|
}
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
seeker.getOffset = function()
|
|
|
|
{
|
|
|
|
return curChar;
|
|
|
|
};
|
|
|
|
seeker.getLineLength = function()
|
|
|
|
{
|
|
|
|
return lineLength;
|
|
|
|
};
|
|
|
|
seeker.toString = function()
|
|
|
|
{
|
|
|
|
return "seeker[curChar: " + curChar + "(" + lineText.charAt(curChar) + "), left: " + seeker.getLeftCoord() + ", vline: " + seeker.getVirtualLine() + "]";
|
2011-03-26 14:10:41 +01:00
|
|
|
};
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function moveByWhile(isBackward, amount, optCondFunc, optCharLimit)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var charsMovedLast = null;
|
|
|
|
var hasCondFunc = ((typeof optCondFunc) == "function");
|
|
|
|
var condFunc = optCondFunc;
|
|
|
|
var hasCharLimit = ((typeof optCharLimit) == "number");
|
|
|
|
var charLimit = optCharLimit;
|
2011-07-07 19:59:34 +02:00
|
|
|
while (charsMovedLast !== 0 && ((!hasCondFunc) || condFunc()))
|
|
|
|
{
|
|
|
|
var toMove = amount;
|
|
|
|
if (hasCharLimit)
|
|
|
|
{
|
|
|
|
var untilLimit = (isBackward ? curChar - charLimit : charLimit - curChar);
|
|
|
|
if (untilLimit < toMove) toMove = untilLimit;
|
|
|
|
}
|
|
|
|
if (toMove < 0) break;
|
|
|
|
charsMovedLast = (isBackward ? seeker.backward(toMove) : seeker.forward(toMove));
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
seeker.forwardByWhile = function(amount, optCondFunc, optCharLimit)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
moveByWhile(false, amount, optCondFunc, optCharLimit);
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
seeker.backwardByWhile = function(amount, optCondFunc, optCharLimit)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
moveByWhile(true, amount, optCondFunc, optCharLimit);
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
seeker.binarySearch = function(condFunc)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
// returns index of boundary between false chars and true chars;
|
|
|
|
// positions seeker at first true char, or else last char
|
|
|
|
var trueFunc = condFunc;
|
2011-07-07 19:59:34 +02:00
|
|
|
var falseFunc = function()
|
|
|
|
{
|
|
|
|
return !condFunc();
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
seeker.forwardByWhile(20, falseFunc);
|
|
|
|
seeker.backwardByWhile(20, trueFunc);
|
|
|
|
seeker.forwardByWhile(10, falseFunc);
|
|
|
|
seeker.backwardByWhile(5, trueFunc);
|
|
|
|
seeker.forwardByWhile(1, falseFunc);
|
|
|
|
return seeker.getOffset() + (condFunc() ? 0 : 1);
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
return seeker;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|