/
context-menu.js
242 lines (202 loc) · 5.95 KB
/
context-menu.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var ContextMenu = new (class ContextMenu {
constructor() {
this.menu = document.createElement("ul");
this.menu.className = this.menu.id = "context-menu";
this.menu.tabIndex = 0;
this.menu.style.display = "none";
document.body.appendChild(this.menu);
this.menu.addEventListener("mousedown", function (event) {
// Prevent clicks on the menu to propagate
// to the window, so that the menu is not
// removed and links will be followed.
event.stopPropagation();
});
window.addEventListener("mousedown", () => this.hide());
window.addEventListener("pageshow", () => this.hide());
window.addEventListener("click", () => this.tryShowOnClick(event));
}
fmt(s, data) {
data = data
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
return s.replace("_", data);
}
tryShowOnClick(event) {
// Don't display the context menu if there's a selection.
// User could be trying to select something and the context menu will undo it.
if (!window.getSelection().isCollapsed) {
return;
}
if (!event.target.closest("code")) {
return;
}
let tree = document.getElementById("data").getAttribute("data-tree");
let menuItems = [];
let index = event.target.closest("[data-i]");
if (index) {
index = index.getAttribute("data-i");
// Comes from the generated page.
let [jumps, searches] = ANALYSIS_DATA[index];
for (let { sym, pretty } of jumps) {
menuItems.push({
html: this.fmt("Go to definition of _", pretty),
href: `/${tree}/define?q=${encodeURIComponent(sym)}&redirect=false`,
icon: "search",
});
}
for (let { sym, pretty } of searches) {
menuItems.push({
html: this.fmt("Search for _", pretty),
href: `/${tree}/search?q=symbol:${encodeURIComponent(
sym
)}&redirect=false`,
icon: "search",
});
}
}
let word = getTargetWord();
if (word) {
// A word was clicked on.
menuItems.push({
html: this.fmt("Search for the substring <strong>_</strong>", word),
href: `/${tree}/search?q=${encodeURIComponent(word)}&redirect=false`,
icon: "search",
});
}
let token = event.target.closest("[data-symbols]");
if (token) {
let symbols = token.getAttribute("data-symbols");
let visibleToken = token.textContent;
menuItems.push({
html: "Sticky highlight",
href: `javascript:Hover.stickyHighlight('${symbols}', '${visibleToken}')`,
});
}
if (!menuItems.length) {
return;
}
this.menu.innerHTML = "";
for (let item of menuItems) {
let li = document.createElement("li");
let link = li.appendChild(document.createElement("a"));
link.href = item.href;
link.classList.add("icon");
if (item.icon) {
link.classList.add(item.icon);
}
link.innerHTML = item.html;
this.menu.appendChild(li);
}
let top = event.clientY + window.scrollY;
let left = event.clientX + window.scrollX;
this.menu.style.top = top + "px";
this.menu.style.left = left + "px";
this.menu.style.display = "";
this.menu.focus();
}
hide() {
this.menu.style.display = "none";
}
get active() {
return this.menu.style.display != "none";
}
})();
var Hover = new (class Hover {
constructor() {
this.items = [];
this.sticky = false;
window.addEventListener("mousedown", () => {
if (this.sticky) {
this.deactivate();
}
});
window.addEventListener("mousemove", event => this._handleMouseMove(event));
}
_handleMouseMove(event) {
if (ContextMenu.active || this.sticky) {
return;
}
let symbols = event.target.closest("[data-symbols]");
if (!symbols) {
return this.deactivate();
}
this.activate(symbols.getAttribute("data-symbols"), symbols.textContent);
}
deactivate() {
for (let item of this.items) {
item.classList.remove("hovered");
}
this.items = [];
this.sticky = false;
}
activate(symbols, visibleToken) {
this.deactivate();
this.items = this.findReferences(symbols, visibleToken);
for (let item of this.items) {
item.classList.add("hovered");
}
}
findReferences(symbols, visibleToken) {
function symbolsFromString(symbols) {
if (!symbols || symbols == "?") {
// XXX why the `?` special-case?
return [];
}
return symbols.split(",");
}
symbols = symbolsFromString(symbols);
if (!symbols.length) {
return [];
}
symbols = new Set(symbols);
return [...document.querySelectorAll("span[data-symbols]")].filter(span => {
// XXX The attribute check is cheaper, probably should be before.
return (
span.textContent == visibleToken &&
symbolsFromString(span.getAttribute("data-symbols")).some(symbol =>
symbols.has(symbol)
)
);
});
}
stickyHighlight(symbols, visibleToken) {
ContextMenu.hide();
this.activate(symbols, visibleToken);
this.sticky = true;
}
})();
function getTargetWord() {
let selection = window.getSelection();
if (!selection.isCollapsed) {
return null;
}
let offset = selection.focusOffset;
let node = selection.anchorNode;
let string = node.nodeValue;
if (!string.length) {
return null;
}
function isWordChar(character) {
// TODO: this could be more non-ascii friendly.
return /[A-Z0-9_]/i.test(character);
}
if (offset < string.length && !isWordChar(string[offset])) {
// Not really in a word.
return null;
}
let start = offset;
let end = offset;
while (start > 0 && isWordChar(string[start - 1])) {
--start;
}
while (end < string.length && isWordChar(string[end])) {
++end;
}
if (end <= start) {
return null;
}
return string.substring(start, end);
}