MCPcopy Create free account
hub / github.com/SamyPesse/draft-js-code / getLineAnchorForOffset

Function getLineAnchorForOffset

lib/utils/getLineAnchorForOffset.js:28–46  ·  view source on GitHub ↗

* Return an anchor of a cursor in a block as a {line,offset} object * * @param {String} text * @param {Number} offset * @param {String} sep (optional) * @return {LineAnchor}

(text, offset, sep)

Source from the content-addressed store, hash-verified

26 * @return {LineAnchor}
27 */
28function getLineAnchorForOffset(text, offset, sep) {
29 sep = sep || getNewLine(text);
30
31 var lineIndex = 0;
32 var nextLineIndex = 0;
33 var lastLineIndex = 0;
34
35 while (nextLineIndex >= 0 && nextLineIndex < offset) {
36 lineIndex++;
37
38 lastLineIndex = nextLineIndex;
39 nextLineIndex = text.indexOf(sep, nextLineIndex + sep.length);
40 }
41
42 return new LineAnchor({
43 line: lineIndex - 1,
44 offset: offset - lastLineIndex
45 });
46}
47
48module.exports = getLineAnchorForOffset;

Callers 3

insertNewLineFunction · 0.85
removeIndentFunction · 0.85

Calls 1

getNewLineFunction · 0.85

Tested by

no test coverage detected