Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
163 lines (147 loc) · 5 KB
/
Copy pathscript.js
File metadata and controls
163 lines (147 loc) · 5 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
const downloadLabel = document.querySelector(".version");
const navbar = document.querySelector(".navbar");
const starsCount = document.getElementById("stars-count");
const commitTime = document.getElementById("commit-time");
const versionsCount = document.getElementById("versions-count");
function fetchGitHub(url) {
return fetch(url).then((res) => {
if (!res.ok) {
throw new Error(`GitHub API request failed (${res.status})`);
}
return res.json();
});
}
function timeAgo(date) {
const time = new Date(date).getTime();
if (Number.isNaN(time)) return "-";
const seconds = Math.floor((Date.now() - time) / 1000);
const intervals = [
{ label: "yr", seconds: 31536000 },
{ label: "mo", seconds: 2592000 },
{ label: "wk", seconds: 604800 },
{ label: "d", seconds: 86400 },
{ label: "hr", seconds: 3600 },
{ label: "min", seconds: 60 },
];
for (const interval of intervals) {
const count = Math.floor(seconds / interval.seconds);
if (count >= 1) {
return `${count} ${interval.label}${count > 1 ? "s" : ""} ago`;
}
}
return "just now";
}
fetchGitHub("https://api.github.com/repos/JereIDE/JereIDE/releases/latest")
.then((data) => {
if (!data || typeof data.tag_name !== "string") {
throw new Error("Unexpected release response");
}
const tag = data.tag_name;
const version = `v${tag.replace(/^v/i, "")}`;
downloadLabel.textContent = `${version} · macOS 12+ · Windows 10+`;
downloadLabel.href = data.html_url;
})
.catch(() => {
downloadLabel.textContent = "macOS 12+ · Windows 10+";
});
// Fetch repo data for stars and last commit
fetchGitHub("https://api.github.com/repos/JereIDE/JereIDE")
.then((repoData) => {
if (!repoData || !Number.isFinite(repoData.stargazers_count)) {
throw new Error("Unexpected repository response");
}
animateCount(starsCount, repoData.stargazers_count);
commitTime.textContent = timeAgo(repoData.pushed_at);
})
.catch(() => {
starsCount.textContent = "-";
commitTime.textContent = "-";
});
function animateCount(el, target) {
if (!Number.isFinite(target) || target <= 0) {
el.textContent = "0";
return;
}
let current = 0;
const increment = target > 100 ? Math.ceil(target / 50) : 1;
const duration = 1500;
const stepTime = Math.max(16, Math.floor(duration / (target / increment)));
const timer = setInterval(() => {
current += increment;
if (current >= target) {
current = target;
clearInterval(timer);
}
el.textContent = current;
}, stepTime);
}
// Fetch release count for "Versions Released"
fetchGitHub("https://api.github.com/repos/JereIDE/JereIDE/releases")
.then((releases) => {
if (!Array.isArray(releases)) {
throw new Error("Unexpected releases response");
}
animateCount(versionsCount, releases.length);
})
.catch(() => {
versionsCount.textContent = "-";
});
window.addEventListener(
"scroll",
() => {
const threshold = 250;
if (window.scrollY > threshold) {
if (!navbar.classList.contains("navbar-fixed")) {
navbar.classList.add("navbar-fixed");
document.body.style.paddingTop = navbar.offsetHeight + "px";
}
} else {
if (navbar.classList.contains("navbar-fixed")) {
navbar.classList.remove("navbar-fixed");
document.body.style.paddingTop = "";
}
}
},
{ passive: true },
);
// Grow the screenshot as the user scrolls through its section
const screenshotSection = document.querySelector(".screenshot");
const screenshotImg = document.querySelector(".screenshot-img");
if (screenshotSection && screenshotImg) {
const sticky = screenshotSection.querySelector(".screenshot-sticky");
let ticking = false;
const updateScreenshotScale = () => {
ticking = false;
const rect = screenshotSection.getBoundingClientRect();
const travel = rect.height - sticky.offsetHeight;
const progress =
travel > 0 ? Math.min(Math.max(-rect.top / travel, 0), 1) : 0;
const eased = progress * progress * (3 - 2 * progress);
screenshotImg.style.setProperty(
"--screenshot-scale",
(1 + eased * 0.6).toFixed(3),
);
screenshotImg.style.setProperty(
"--screenshot-rise",
`${((1 - eased) * 32).toFixed(1)}px`,
);
// Fade out once 15% of the photo is hidden above the viewport,
// and be fully gone when only the bottom 10% is still visible.
const imgRect = screenshotImg.getBoundingClientRect();
const covered = Math.min(Math.max(-imgRect.top / imgRect.height, 0), 1);
const fade = Math.min(Math.max((covered - 0.15) / (0.9 - 0.15), 0), 1);
screenshotImg.style.setProperty(
"--screenshot-opacity",
(1 - fade).toFixed(3),
);
};
const requestScreenshotUpdate = () => {
if (!ticking) {
ticking = true;
requestAnimationFrame(updateScreenshotScale);
}
};
updateScreenshotScale();
window.addEventListener("scroll", requestScreenshotUpdate, { passive: true });
window.addEventListener("resize", requestScreenshotUpdate, { passive: true });
}