Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
69 lines (64 loc) · 2.71 KB
/
Copy pathmain.js
File metadata and controls
69 lines (64 loc) · 2.71 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
const toggle = document.querySelector("#review-toggle")
const review = document.querySelector(".review")
const arrow = toggle.querySelector("svg").outerHTML
toggle.addEventListener("click", () => {
const compacted = toggle.getAttribute("aria-pressed") !== "true"
toggle.setAttribute("aria-pressed", String(compacted))
review.classList.toggle("is-compacted", compacted)
toggle.innerHTML = `${compacted ? "Show full example" : "Show retained context"}${arrow}`
document.querySelector("#review-state").textContent = compacted ? "Retained checkpoint" : "Before compaction"
document.querySelector("#review-count").textContent = compacted ? "4 illustrated entries retained" : "6 illustrated entries"
document.querySelector(".full-output").hidden = compacted
document.querySelector(".short-output").hidden = !compacted
})
const tabs = [...document.querySelectorAll('[role="tab"]')]
const panels = tabs.map(tab => document.getElementById(tab.getAttribute("aria-controls")))
document.querySelector(".backend-tabs").hidden = false
panels.forEach(panel => {
panel.setAttribute("role", "tabpanel")
panel.setAttribute("aria-labelledby", tabs[panels.indexOf(panel)].id)
panel.tabIndex = 0
})
function activateTab(index, focus = false) {
tabs.forEach((tab, current) => {
const active = current === index
tab.setAttribute("aria-selected", String(active))
tab.tabIndex = active ? 0 : -1
panels[current].hidden = !active
})
if (focus) tabs[index].focus()
}
tabs.forEach((tab, index) => {
tab.addEventListener("click", () => activateTab(index))
tab.addEventListener("keydown", event => {
let next
if (event.key === "ArrowRight") next = (index + 1) % tabs.length
if (event.key === "ArrowLeft") next = (index + tabs.length - 1) % tabs.length
if (event.key === "Home") next = 0
if (event.key === "End") next = tabs.length - 1
if (next !== undefined) {
event.preventDefault()
activateTab(next, true)
}
})
})
activateTab(0)
const copyStatus = document.querySelector("#copy-status")
let statusTimeout
document.querySelectorAll("[data-copy]").forEach(button => {
button.addEventListener("click", async () => {
button.disabled = true
try {
const value = document.getElementById(button.dataset.copy).textContent.trim()
if (!navigator.clipboard?.writeText) throw new Error("Clipboard unavailable")
await navigator.clipboard.writeText(value)
copyStatus.textContent = "Copied to clipboard."
} catch {
copyStatus.textContent = "Clipboard unavailable. Select the command and copy it manually."
} finally {
button.disabled = false
clearTimeout(statusTimeout)
statusTimeout = setTimeout(() => { copyStatus.textContent = "" }, 5000)
}
})
})