-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathscript.js
96 lines (80 loc) · 2.92 KB
/
script.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
// Nodes
iframe = document.createElement("iframe");
source = document.getElementById("source");
// Syntax Highlight
SyntaxHighlighter.highlight({
"gutter": true,
"toolbar": false,
"quick-code": false
}, source);
// Key event
function onKeyPress(event) {
if (event.charCode == 114) {
if (onKeyPress.toggle) {
iframe.parentNode ? document.body.removeChild(iframe) : (document.body.appendChild(iframe), iframe.contentDocument.write("<base href="+url+">"+source.textContent));
} else {
onKeyPress.toggle = true;
setTimeout(function () {
onKeyPress.toggle = false;
}, 300);
}
}
}
onKeyPress.toggle = false;
// Hash event
function onHashChange(event) {
Array.prototype.forEach.call(document.querySelectorAll(".highlighted"), function (element) {
element.classList.remove("highlighted");
});
location.hash.replace(/L(\d+)-*(\d*)/, function (match, line, max, line_node) {
line = parseInt(line, 10) || 1;
max = parseInt(max, 10) || line;
if (document.querySelector(".code .number"+line)) {
for (; line <= max; ++line) {
document.querySelector(".code .number"+line).classList.add("highlighted");
}
}
});
}
// Assign pointer events
function onPointerStart(event) {
if (iframe.parentNode) {
return document.body.removeChild(iframe);
}
onPointerMove(event);
window.addEventListener("mousemove", onPointerMove);
window.addEventListener("touchmove", onPointerMove);
window.addEventListener("mouseup", onPointerEnd);
window.addEventListener("touchend", onPointerEnd);
}
function onPointerMove(event) {
var
node = document.elementFromPoint(event.clientX || (event.pageX - window.pageXOffset), event.clientY || (event.pageY - window.pageYOffset)),
line = node && node.parentNode && node.parentNode.className == "gutter" && node.firstChild.nodeValue || 0,
keep = (event.shiftKey || /move/.test(event.type)) && onPointerMove.line;
if (line && line != onPointerMove.line) {
event.preventDefault();
location.hash = "L" + (keep ? Math.min(onPointerMove.line, line)+"-"+Math.max(onPointerMove.line, line) : line);
onPointerMove.line = keep ? onPointerMove.line : line;
}
}
onPointerMove.line = 0;
function onPointerEnd(event) {
window.removeEventListener("mousemove", onPointerMove);
window.removeEventListener("touchmove", onPointerMove);
window.removeEventListener("mouseup", onPointerEnd);
window.removeEventListener("touchend", onPointerEnd);
}
// Initialize all events
window.addEventListener("keypress", onKeyPress);
window.addEventListener("mousedown", onPointerStart);
window.addEventListener("touchstart", onPointerStart);
window.addEventListener("hashchange", onHashChange);
window.addEventListener("DOMContentLoaded", onHashChange);
window.addEventListener("DOMContentLoaded", function () {
location.hash.replace(/L(\d+)/, function (match, line) {
if (document.querySelector(".code .number"+line)) {
document.querySelector(".gutter .number"+Math.max(line - 10, 1)).scrollIntoView(true);
}
});
});