summaryrefslogtreecommitdiff
path: root/src/static/js/virtual_lines.js
blob: 2bcf5ed631285755da6b945987219f42b9ba3e82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
 * This code is mostly from the old Etherpad. Please help us to comment this code. 
 * This helps other people to understand this code better and helps them to improve it.
 * TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
 */

/**
 * Copyright 2009 Google Inc.
 *
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

function makeVirtualLineView(lineNode)
{

  // 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;

  function getNumChars()
  {
    return lineNode.textContent.length;
  }

  function getNumVirtualLines()
  {
    if (!seekerAtEnd)
    {
      var seeker = makeCharSeeker();
      seeker.forwardByWhile(maxCharIncrement);
      seekerAtEnd = seeker;
    }
    return seekerAtEnd.getVirtualLine() + 1;
  }

  function getVLineAndOffsetForChar(lineChar)
  {
    var seeker = makeCharSeeker();
    seeker.forwardByWhile(maxCharIncrement, null, lineChar);
    var theLine = seeker.getVirtualLine();
    seeker.backwardByWhile(8, function()
    {
      return seeker.getVirtualLine() == theLine;
    });
    seeker.forwardByWhile(1, function()
    {
      return seeker.getVirtualLine() != theLine;
    });
    var lineStartChar = seeker.getOffset();
    return {
      vline: theLine,
      offset: (lineChar - lineStartChar)
    };
  }

  function getCharForVLineAndOffset(vline, offset)
  {
    // 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
    seeker.binarySearch(function()
    {
      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
    seeker.forwardByWhile(maxCharIncrement, null, lineStart + offset);
    // get back into line
    seeker.backwardByWhile(1, function()
    {
      return seeker.getVirtualLine() != theLine;
    }, lineStart);
    var lineChar = seeker.getOffset();
    var theOffset = lineChar - lineStart;
    // handle case of last virtual line; should be able to be at end of it
    if (theOffset < offset && theLine == (getNumVirtualLines() - 1))
    {
      var lineLen = getNumChars();
      theOffset += lineLen - lineChar;
      lineChar = lineLen;
    }

    return {
      vline: theLine,
      offset: theOffset,
      lineChar: lineChar
    };
  }

  return {
    getNumVirtualLines: getNumVirtualLines,
    getVLineAndOffsetForChar: getVLineAndOffsetForChar,
    getCharForVLineAndOffset: getCharForVLineAndOffset,
    makeCharSeeker: function()
    {
      return makeCharSeeker();
    }
  };

  function deepFirstChildTextNode(nd)
  {
    nd = nd.firstChild;
    while (nd && nd.firstChild) nd = nd.firstChild;
    if (nd.data) return nd;
    return null;
  }

  function makeCharSeeker( /*lineNode*/ )
  {

    function charCoords(tnode, i)
    {
      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) === " ");
      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
        }
      }


      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);
      frag.appendChild(document.createTextNode(tnodeText.substring(i + 1)));
      container.replaceChild(frag, tnode);

      var result = {
        top: charWrapper.offsetTop,
        left: charWrapper.offsetLeft + (isSpace ? charWrapper.offsetWidth : 0),
        height: charWrapper.offsetHeight
      };

      while (container.firstChild) container.removeChild(container.firstChild);
      container.appendChild(tnode);

      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;

    function nextNode()
    {
      var n = curNode;
      if (!n) n = lineNode.firstChild;
      else n = n.nextSibling;
      while (n && !deepFirstChildTextNode(n))
      {
        n = n.nextSibling;
      }
      return n;
    }

    function prevNode()
    {
      var n = curNode;
      if (!n) n = lineNode.lastChild;
      else n = n.previousSibling;
      while (n && !deepFirstChildTextNode(n))
      {
        n = n.previousSibling;
      }
      return n;
    }

    var seeker;
    if (lineLength > 0)
    {
      curNode = nextNode();
      var firstCharData = charCoords(deepFirstChildTextNode(curNode), 0);
      approxLineHeight = firstCharData.height;
      curTop = firstCharData.top;
      curLeft = firstCharData.left;

      function updateCharData(tnode, i)
      {
        var coords = charCoords(tnode, i);
        whichLine += Math.round((coords.top - curTop) / approxLineHeight);
        curTop = coords.top;
        curLeft = coords.left;
      }

      seeker = {
        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;
        }
      };
    }
    else
    {
      curLeft = lineNode.offsetLeft;
      seeker = {
        forward: function(numChars)
        {
          return 0;
        },
        backward: function(numChars)
        {
          return 0;
        },
        getVirtualLine: function()
        {
          return 0;
        },
        getLeftCoord: function()
        {
          return curLeft;
        }
      };
    }
    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() + "]";
    };

    function moveByWhile(isBackward, amount, optCondFunc, optCharLimit)
    {
      var charsMovedLast = null;
      var hasCondFunc = ((typeof optCondFunc) == "function");
      var condFunc = optCondFunc;
      var hasCharLimit = ((typeof optCharLimit) == "number");
      var charLimit = optCharLimit;
      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));
      }
    }

    seeker.forwardByWhile = function(amount, optCondFunc, optCharLimit)
    {
      moveByWhile(false, amount, optCondFunc, optCharLimit);
    }
    seeker.backwardByWhile = function(amount, optCondFunc, optCharLimit)
    {
      moveByWhile(true, amount, optCondFunc, optCharLimit);
    }
    seeker.binarySearch = function(condFunc)
    {
      // returns index of boundary between false chars and true chars;
      // positions seeker at first true char, or else last char
      var trueFunc = condFunc;
      var falseFunc = function()
        {
          return !condFunc();
          };
      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);
    }

    return seeker;
  }

}

exports.makeVirtualLineView = makeVirtualLineView;