-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs.js
More file actions
85 lines (75 loc) · 3.34 KB
/
docs.js
File metadata and controls
85 lines (75 loc) · 3.34 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
/* docs.js — Interactivity for the API documentation page */
(function () {
'use strict';
// ─── Code example tabs ──────────────────────────────────────────
document.querySelectorAll('.example-tabs').forEach(function (tabGroup) {
var buttons = tabGroup.querySelectorAll('[data-lang]');
var container = tabGroup.closest('.example-block');
if (!container) return;
buttons.forEach(function (btn) {
btn.addEventListener('click', function () {
var lang = btn.getAttribute('data-lang');
// Deactivate all tabs in this group
buttons.forEach(function (b) { b.classList.remove('active'); });
btn.classList.add('active');
// Show matching panel, hide others
container.querySelectorAll('.example-panel[data-lang]').forEach(function (panel) {
panel.classList.toggle('active', panel.getAttribute('data-lang') === lang);
});
});
});
});
// ─── Copy-to-clipboard ──────────────────────────────────────────
document.querySelectorAll('.copy-btn').forEach(function (btn) {
btn.addEventListener('click', function () {
var wrapper = btn.closest('.code-wrapper');
if (!wrapper) return;
var code = wrapper.querySelector('code');
if (!code) return;
navigator.clipboard.writeText(code.textContent).then(function () {
var original = btn.textContent;
btn.textContent = 'Copied!';
btn.classList.add('copied');
setTimeout(function () {
btn.textContent = original;
btn.classList.remove('copied');
}, 2000);
});
});
});
// ─── Smooth scroll for anchor links ─────────────────────────────
document.querySelectorAll('a[href^="#"]').forEach(function (link) {
link.addEventListener('click', function (e) {
var target = document.querySelector(link.getAttribute('href'));
if (!target) return;
e.preventDefault();
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
history.pushState(null, '', link.getAttribute('href'));
});
});
// ─── Sidebar active section tracking ────────────────────────────
var sidebarLinks = document.querySelectorAll('.sidebar a[href^="#"]');
if (sidebarLinks.length === 0) return;
var sections = [];
sidebarLinks.forEach(function (link) {
var id = link.getAttribute('href').slice(1);
var el = document.getElementById(id);
if (el) sections.push({ id: id, el: el, link: link });
});
if (!('IntersectionObserver' in window)) return;
var currentActive = null;
var observer = new IntersectionObserver(
function (entries) {
entries.forEach(function (entry) {
if (!entry.isIntersecting) return;
var match = sections.find(function (s) { return s.el === entry.target; });
if (!match) return;
if (currentActive) currentActive.classList.remove('active');
match.link.classList.add('active');
currentActive = match.link;
});
},
{ rootMargin: '-80px 0px -60% 0px', threshold: 0 }
);
sections.forEach(function (s) { observer.observe(s.el); });
})();