-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.js
93 lines (85 loc) · 2.56 KB
/
utils.js
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
export const nbsp = "\u00a0";
export function getRangeForCursor(e) {
let range=null;
if (document.caretRangeFromPoint) {
// edge, chrome, android
range = document.caretRangeFromPoint(e.clientX, e.clientY)
} else {
// firefox
var pos = [e.rangeParent, e.rangeOffset]
range = document.createRange()
range.setStart(...pos);
range.setEnd(...pos);
}
return range;
}
export function mergeNodeAfter(target, source) {
let first = source.childNodes[0];
let documentFragment = document.createDocumentFragment();
while (source.childNodes.length > 0) {
documentFragment.appendChild(source.childNodes[0]);
}
target.append(documentFragment);
if (first != null) {
console.log("Setting cursor before:", first);
setCursorBefore(first);
}
source.remove();
}
export function findAncestor(p, nodeName) {
while (p != null) {
if (p.nodeName === nodeName) {
return p;
}
p = p.parentNode;
}
}
export function removeAllChildren(node) {
// while(node.childNodes.length > 0) {
// node.removeChild(node.childNodes[0]);
// }
node.replaceChildren();
}
export function loadXMLDoc(filename)
{
let xhttp = window.ActiveXObject ?
new ActiveXObject("Msxml2.XMLHTTP")
: new XMLHttpRequest();
xhttp.open("GET", filename, false);
try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}
export function setCursorBefore(node) {
// Why not: document.getSelection().collapse(node); ?
let newr=document.createRange();
newr.setStartBefore(node);
newr.setEndBefore(node);
document.getSelection().removeAllRanges();
document.getSelection().addRange(newr);
console.log("New selection:", document.getSelection(), newr);
}
export function setCursorAfter(node) {
if (node.nextSibling) {
setCursorBefore(node.nextSibling)
} else {
let newr=document.createRange();
if (node.nodeName=='#text') {
newr.setStart(node, node.nodeValue.length - 1)
newr.setEnd(node, node.nodeValue.length - 1)
}
// newr.setStartBefore(node);
// newr.setEndBefore(node);
document.getSelection().removeAllRanges();
document.getSelection().addRange(newr);
console.log("New selection:", document.getSelection(), newr);
}
// Why not: document.getSelection().collapse(node); ?
//
// let newr=document.createRange();
// newr.setStartBefore(node);
// newr.setEndBefore(node);
// document.getSelection().removeAllRanges();
// document.getSelection().addRange(newr);
// console.log("New selection:", document.getSelection(), newr);
}