-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathscript.js
More file actions
218 lines (171 loc) · 6.57 KB
/
Copy pathscript.js
File metadata and controls
218 lines (171 loc) · 6.57 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
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
/* VocaMac site behaviour.
Every feature here is an enhancement: the page is complete without it. */
(function () {
"use strict";
var reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
/* ---------- Sticky header hairline ---------- */
var header = document.querySelector("[data-header]");
if (header) {
var setScrolled = function () {
header.classList.toggle("is-scrolled", window.scrollY > 8);
};
setScrolled();
window.addEventListener("scroll", setScrolled, { passive: true });
}
/* ---------- Mobile navigation ---------- */
var toggle = document.querySelector("[data-nav-toggle]");
var mobileNav = document.querySelector("[data-mobile-nav]");
if (toggle && mobileNav) {
var setOpen = function (open) {
toggle.setAttribute("aria-expanded", open ? "true" : "false");
mobileNav.hidden = !open;
};
setOpen(false);
toggle.addEventListener("click", function () {
setOpen(toggle.getAttribute("aria-expanded") !== "true");
});
mobileNav.addEventListener("click", function (event) {
if (event.target.closest("a")) { setOpen(false); }
});
document.addEventListener("keydown", function (event) {
if (event.key === "Escape" && toggle.getAttribute("aria-expanded") === "true") {
setOpen(false);
toggle.focus();
}
});
document.addEventListener("click", function (event) {
if (toggle.getAttribute("aria-expanded") !== "true") { return; }
if (toggle.contains(event.target) || mobileNav.contains(event.target)) { return; }
setOpen(false);
});
window.addEventListener("resize", function () {
if (window.innerWidth > 920) { setOpen(false); }
});
}
/* ---------- Copy buttons ---------- */
var canCopy = !!(navigator.clipboard && navigator.clipboard.writeText);
var copyBlocks = document.querySelectorAll("[data-copy]");
var copyStatus = null;
if (canCopy && copyBlocks.length) {
copyStatus = document.createElement("div");
copyStatus.className = "sr-only";
copyStatus.setAttribute("aria-live", "polite");
copyStatus.setAttribute("aria-atomic", "true");
document.body.appendChild(copyStatus);
}
var announceCopy = function (message) {
if (!copyStatus) { return; }
copyStatus.textContent = "";
copyStatus.textContent = message;
};
copyBlocks.forEach(function (block) {
var source = block.querySelector("code");
if (!source || !canCopy) { return; }
var button = document.createElement("button");
button.type = "button";
button.className = "copy-btn";
button.textContent = "Copy";
button.setAttribute("aria-label", "Copy commands to clipboard");
button.addEventListener("click", function () {
// Drop the shell prompts so the paste is runnable.
var text = Array.prototype.map
.call(source.childNodes, function (node) {
return node.classList && node.classList.contains("prompt") ? "" : node.textContent;
})
.join("")
.trim();
var restore = function () {
button.textContent = "Copy";
button.classList.remove("is-copied");
announceCopy("");
};
navigator.clipboard.writeText(text).then(
function () {
button.textContent = "Copied";
button.classList.add("is-copied");
announceCopy("Copied");
setTimeout(restore, 2000);
},
function () {
button.textContent = "Press ⌘C";
announceCopy("Press ⌘C");
setTimeout(restore, 2000);
}
);
});
block.appendChild(button);
});
/* ---------- Optional reveals ---------- */
var revealItems = document.querySelectorAll(".reveal");
if (revealItems.length) {
var reveal = function (element) { element.classList.add("is-visible"); };
if (reduceMotion.matches || !("IntersectionObserver" in window)) {
revealItems.forEach(reveal);
} else {
document.documentElement.classList.add("js-reveal");
var safetyNet;
var observer = new IntersectionObserver(
function (entries) {
// The observer reports, so the blanket fallback is no longer needed:
// without this, everything below the fold un-hid after 800ms and the
// reveal never actually played on scroll.
clearTimeout(safetyNet);
entries.forEach(function (entry) {
if (!entry.isIntersecting) { return; }
reveal(entry.target);
observer.unobserve(entry.target);
});
},
{ rootMargin: "0px 0px -8% 0px", threshold: 0.05 }
);
revealItems.forEach(function (element) { observer.observe(element); });
// Safety net: never leave content hidden if the observer never fires.
safetyNet = setTimeout(function () { revealItems.forEach(reveal); }, 800);
}
}
/* ---------- Table of contents highlighting ---------- */
var toc = document.querySelector("[data-toc]");
if (toc && "IntersectionObserver" in window) {
var tocLinks = {};
var headings = [];
toc.querySelectorAll('a[href^="#"]').forEach(function (link) {
var heading = document.getElementById(decodeURIComponent(link.hash.slice(1)));
if (!heading) { return; }
tocLinks[heading.id] = link;
headings.push(heading);
});
if (headings.length) {
var current = null;
var setCurrent = function (id) {
if (id === current) { return; }
if (current && tocLinks[current]) { tocLinks[current].classList.remove("is-current"); }
current = id;
if (current && tocLinks[current]) { tocLinks[current].classList.add("is-current"); }
};
var visible = {};
var tocObserver = new IntersectionObserver(
function (entries) {
entries.forEach(function (entry) {
visible[entry.target.id] = entry.isIntersecting;
});
var first = headings.filter(function (heading) { return visible[heading.id]; })[0];
if (first) { setCurrent(first.id); }
},
{ rootMargin: "-12% 0px -70% 0px", threshold: 0 }
);
headings.forEach(function (heading) { tocObserver.observe(heading); });
setCurrent(headings[0].id);
}
}
/* ---------- FAQ convenience ---------- */
var faq = document.querySelector("[data-faq]");
if (faq) {
faq.addEventListener("toggle", function (event) {
var target = event.target;
if (target.tagName !== "DETAILS" || !target.open) { return; }
faq.querySelectorAll("details[open]").forEach(function (other) {
if (other !== target) { other.open = false; }
});
}, true);
}
})();