Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
188 lines (162 loc) · 6.55 KB
/
Copy pathscript.js
File metadata and controls
188 lines (162 loc) · 6.55 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
const notesGrid = document.getElementById('notesGrid');
const searchInput = document.getElementById('search');
const resultCount = document.getElementById('resultCount');
const topicsCount = document.getElementById('topicsCount');
const featuredNotes = document.getElementById('featuredNotes');
const featuredCount = document.getElementById('featuredCount');
const themeToggle = document.getElementById('themeToggle');
const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Performance: Cache card metadata to avoid repeated DOM queries
let cards = [];
let cardMetadata = [];
let searchDebounceTimer = null;
const SEARCH_DEBOUNCE_MS = 300;
let lastScrollY = 0;
function applyTheme(theme) {
document.documentElement.classList.toggle('light', theme === 'light');
if (themeToggle) {
themeToggle.classList.toggle('light', theme === 'light');
themeToggle.setAttribute('aria-label', theme === 'light' ? 'Switch to dark mode' : 'Switch to light mode');
}
localStorage.setItem('site-theme', theme);
}
function loadTheme() {
const savedTheme = localStorage.getItem('site-theme');
const initialTheme = savedTheme === 'light' ? 'light' : 'dark';
applyTheme(initialTheme);
}
function updateTopicsCount() {
if (topicsCount) {
topicsCount.textContent = notes.length;
}
}
function toggleTheme() {
const currentTheme = document.documentElement.classList.contains('light') ? 'light' : 'dark';
applyTheme(currentTheme === 'light' ? 'dark' : 'light');
if (themeToggle && !reducedMotion) {
themeToggle.classList.add('active');
setTimeout(() => themeToggle.classList.remove('active'), 420);
}
}
if (themeToggle) {
themeToggle.addEventListener('click', toggleTheme);
}
function updateResultCount(visibleCount, totalCount) {
if (!resultCount) return;
resultCount.textContent = visibleCount === totalCount
? `Showing ${totalCount} notes`
: `Showing ${visibleCount} of ${totalCount} notes`;
}
function getFeaturedNotes() {
return notes.filter(note => note.isFeatured);
}
function renderFeaturedNotes() {
if (!featuredNotes || !featuredCount) return;
const featuredItems = getFeaturedNotes();
featuredCount.textContent = `${featuredItems.length} featured`;
featuredNotes.innerHTML = featuredItems.map(note => `
<a href="${note.href}" class="featured-note-card">
<div class="featured-note-card__top">
<span class="featured-note-card__label">${note.label}</span>
<span class="featured-note-card__badge ${note.isNew ? 'featured-note-card__badge--new' : ''}">
${note.isNew ? 'New' : 'Featured'}
</span>
</div>
<h3>${note.title}</h3>
<p>${note.description}</p>
<div class="featured-note-card__tags">
${note.tags.map(tag => `<span>${tag}</span>`).join('')}
</div>
</a>
`).join('');
}
function renderNotes() {
if (!notesGrid) return;
renderFeaturedNotes();
notesGrid.innerHTML = notes.map(note => {
const newBadge = note.isNew ? '<span class="card-new">New</span>' : '';
const featuredClass = note.isFeatured ? ' featured' : '';
return `
<a href="${note.href}" class="card${featuredClass}">
<div class="card-meta">
<span class="card-label">${note.label}</span>
${newBadge}
</div>
<h3>${note.title}</h3>
<p>${note.description}</p>
</a>
`;
}).join('');
cards = Array.from(document.querySelectorAll('.card'));
// Performance: Pre-cache card metadata to avoid repeated DOM queries during filtering
cardMetadata = cards.map(card => ({
element: card,
title: (card.querySelector('h3')?.textContent || '').toLowerCase(),
description: (card.querySelector('p')?.textContent || '').toLowerCase(),
label: (card.querySelector('.card-label')?.textContent || '').toLowerCase()
}));
}
// Performance: Debounce search to avoid excessive filtering on every keystroke
function debounceFilter() {
if (searchDebounceTimer) clearTimeout(searchDebounceTimer);
searchDebounceTimer = setTimeout(filterNotes, SEARCH_DEBOUNCE_MS);
}
function filterNotes() {
if (!searchInput || cardMetadata.length === 0) return;
const query = searchInput.value.trim().toLowerCase();
let visible = 0;
// Performance: Batch DOM updates using visibility toggling instead of style.display
cardMetadata.forEach(({ element, title, description, label }) => {
const matches = !query || title.includes(query) || description.includes(query) || label.includes(query);
element.classList.toggle('hidden', !matches);
if (matches) visible++;
});
updateResultCount(visible, cards.length);
}
function animateCards() {
// Performance: Skip animations if reduced-motion is preferred
if (reducedMotion) return;
// Performance: Use requestAnimationFrame to batch animations and avoid layout thrashing
requestAnimationFrame(() => {
cards.forEach((card, index) => {
card.animate(
[
{ opacity: 0, transform: 'translateY(20px)' },
{ opacity: 1, transform: 'translateY(0)' }
],
{
duration: 420,
delay: index * 70,
fill: 'forwards',
easing: 'ease-out'
}
);
});
});
}
loadTheme();
updateTopicsCount();
renderNotes();
animateCards();
if (searchInput) {
// Performance: Use debounced filter instead of filtering on every keystroke
searchInput.addEventListener('input', debounceFilter);
}
filterNotes();
// Hide/show toggle button on scroll
window.addEventListener('scroll', () => {
const currentScrollY = window.scrollY;
const isScrollingDown = currentScrollY > lastScrollY;
if (currentScrollY > 100 && isScrollingDown) {
// Scrolling down and past top - hide button
themeToggle?.style.setProperty('pointer-events', 'none');
themeToggle?.style.setProperty('opacity', '0');
themeToggle?.style.setProperty('transform', 'scale(0.8)');
} else {
// At top or scrolling up - show button
themeToggle?.style.setProperty('pointer-events', 'auto');
themeToggle?.style.setProperty('opacity', '1');
themeToggle?.style.setProperty('transform', 'scale(1)');
}
lastScrollY = currentScrollY;
}, { passive: true });