-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnm-hover.js
More file actions
109 lines (97 loc) · 2.97 KB
/
Copy pathnm-hover.js
File metadata and controls
109 lines (97 loc) · 2.97 KB
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
window.addEventListener("load", () => {
let items = [].slice.call(
document.querySelectorAll(".code-chunk.collapsible"),
);
items.forEach((item) => {
item.addEventListener("click", () => {
item.classList.toggle("collapsed");
});
});
const node = document.createElement("div");
document.body.appendChild(node);
Object.assign(node.style, {
position: "fixed",
backgroundColor: "white",
boxShadow: "0 1px 3px rgba(100, 100, 100, 0.1)",
whiteSpace: "pre",
padding: "5px 10px",
fontFamily: "monospace",
display: "none",
top: 0,
left: 0,
});
const size = ({ start, end }) => {
return end.chars - start.chars;
};
const before = ({ line, col }, l2, c2) =>
line < l2 || (line === l2 && col <= c2);
const after = ({ line, col }, l2, c2) =>
line > l2 || (line === l2 && col >= c2);
const findType = (types, lno, start, end) => {
// console.log('looking', lno, start, end)
// console.log(types.filter(t => t.start.line === lno + 1 && t.end.line === lno + 1).map(t => t.type))
return types.filter(
(t) => before(t.start, lno + 1, start) && after(t.end, lno + 1, end),
);
};
const xoff = 10;
const yoff = 20;
let showing = null;
const getMarker = (container, { line, col }) => {
// console.log('looking for', line, col)
return container.querySelector(`.marker[data-marker="${line - 1},${col}"]`);
};
let chunks = [].slice.call(document.querySelectorAll(".reason-source"));
chunks.forEach((chunk) => {
const name = chunk.getAttribute("data-file-name");
if (!name || name === "null" || name === "undefined") return;
if (!TYPES[name]) return console.warn("types not found for " + name);
const all_types = TYPES[name];
all_types.sort((a, b) => {
let d = size(a) - size(b);
if (d == 0) return a.depth - b.depth;
return d;
});
chunk.addEventListener("mousemove", (evt) => {
node.style.left = evt.clientX + xoff + "px";
node.style.top = evt.clientY + yoff + "px";
});
chunk.addEventListener("mouseout", (evt) => {
node.style.display = "none";
if (showing) {
showing.forEach((node) => {
node.style.display = "none";
node.classList.remove("left");
node.classList.remove("right");
});
showing = null;
}
});
chunk.addEventListener("mouseover", (evt) => {
const postxt = evt.target.getAttribute("data-pos");
if (!postxt) return;
const [lno, start, end] = postxt.split(",").map(Number);
const types = findType(all_types, lno, start, end);
if (!types.length) {
return;
}
const t = types[0];
node.style.display = "flex";
node.textContent = t.type;
node.style.left = evt.clientX + xoff + "px";
node.style.top = evt.clientY + yoff + "px";
showing = [];
const sstart = getMarker(chunk, t.start);
const send = getMarker(chunk, t.end);
if (sstart) {
showing.push(sstart);
sstart.classList.add("left");
}
if (send) {
showing.push(send);
send.classList.add("right");
}
showing.forEach((node) => (node.style.display = "inline-block"));
});
});
});