240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
xNodeToAppendTo.appendChild(xNode);
}
return xNode;
}
catch (e) {
showError(e);
}
}
};
/*
Connexion to the background
*/
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
xNodeToAppendTo.appendChild(xNode);
}
return xNode;
}
catch (e) {
showError(e);
}
},
getCaretPosition (xElement) {
// JS awfulness again.
// recepie from https://stackoverflow.com/questions/4811822/get-a-ranges-start-and-end-offsets-relative-to-its-parent-container
let nCaretOffsetStart = 0;
let nCaretOffsetEnd = 0;
let xSelection = window.getSelection();
if (xSelection.rangeCount > 0) {
let xRange = xSelection.getRangeAt(0);
let xPreCaretRange = xRange.cloneRange();
xPreCaretRange.selectNodeContents(xElement);
xPreCaretRange.setEnd(xRange.endContainer, xRange.endOffset);
nCaretOffsetStart = xPreCaretRange.toString().length;
nCaretOffsetEnd = nCaretOffsetStart + xRange.toString().length;
}
return [nCaretOffsetStart, nCaretOffsetEnd];
// for later: solution with multilines text
// https://stackoverflow.com/questions/4811822/get-a-ranges-start-and-end-offsets-relative-to-its-parent-container/4812022
},
setCaretPosition (xElement, nCaretOffsetStart, nCaretOffsetEnd) {
// JS awfulness again.
// recipie from https://stackoverflow.com/questions/6249095/how-to-set-caretcursor-position-in-contenteditable-element-div
let iChar = 0;
let xRange = document.createRange();
xRange.setStart(xElement, 0);
xRange.collapse(true);
let lNode = [xElement];
let xNode;
let bFoundStart = false;
let bStop = false;
while (!bStop && (xNode = lNode.pop())) {
if (xNode.nodeType == 3) { // Node.TEXT_NODE
let iNextChar = iChar + xNode.length;
if (!bFoundStart && nCaretOffsetStart >= iChar && nCaretOffsetStart <= iNextChar) {
xRange.setStart(xNode, nCaretOffsetStart - iChar);
bFoundStart = true;
}
if (bFoundStart && nCaretOffsetEnd >= iChar && nCaretOffsetEnd <= iNextChar) {
xRange.setEnd(xNode, nCaretOffsetEnd - iChar);
bStop = true;
}
iChar = iNextChar;
} else {
let i = xNode.childNodes.length;
while (i--) {
lNode.push(xNode.childNodes[i]);
}
}
}
let xSelection = window.getSelection();
xSelection.removeAllRanges();
xSelection.addRange(xRange);
}
};
/*
Connexion to the background
*/
|