Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll suppose the DOM in your example is an effective example of the actual complexity of your DOM, and that a "line-boxe" is just a line of text.</p> <p>1-2) For every <code>&lt;span&gt;</code> inside the <code>&lt;div&gt;</code>, you can count the number of lines they span with something like this:</p> <pre><code>var spans = div.getElementsByTagName("span"), spandata = []; for (var i = 0; i &lt; spans.length; i++) { var rects = spans[i].getClientRects(); if (i &gt; 0) if (rects[0].bottom &gt; obj.rects[obj.rects - 1].bottom) var inirow = obj.lastRow + 1; else var inirow = obj.lastRow; var obj = { element: spans[i], rects: rects, iniRow: inirow, lastRow: inirow + rects.length - 1 }; spandata.push(obj); } </code></pre> <p>Now <code>spandata</code> is a list of all the data you want about the <code>&lt;span&gt;</code> elements. I'm also supposing that each one of them may span through more than one line.</p> <p>Keep in mind that <code>getClientRects</code> has some issues in IE&lt;8.</p> <p>3) In modern browsers, the <code>getSelection</code> method can help you:</p> <pre><code>var sel = window.getSelection(); if (sel.type === "Caret") var span = sel.anchorNode.parentNode; </code></pre> <p>About the line position, I must say it's not an easy task. You can't easily get the page position of the caret. The simplest thing you can do is to place a dummy inline element in the place of the caret:</p> <pre><code>var text = sel.anchorNode.nodeValue; sel.anchorNode.nodeValue = text.substring(0, sel.anchorOffset); var dummy = document.createElement("i"); span.appendChild(dummy); var pos = dummy.getBoundingClientRect(); sel.anchorNode.nodeValue = text; span.removeChild(dummy); </code></pre> <p><code>pos</code> contains the info of the position of the caret. Now you have to compare them with the rect infos about the span:</p> <pre><code>var rects = span.getClientRects(); for (var i = 0; i &lt; rects.length; i++) if (rects[i]].bottom === pos.bottom) break; if (i &lt; rects.length) { for (var i = 0; i &lt; spandata.length; i++) { if (spandata[i].element === span) { var line = spandata[i].iniRow + i; break; } } } </code></pre> <p>In the end, if <code>line != null</code>, it contains the line of the caret.</p> <p>Man, that was complicated...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload