Skip to content

Commit 2f8319b

Browse files
committed
feat: script
1 parent c697212 commit 2f8319b

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

web/static/script.js

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ document.addEventListener('DOMContentLoaded', () => {
77

88
const toggle = document.getElementById('cursor-toggle');
99
const sakuraContainer = document.getElementById('sakura-container');
10-
let sakuraEnabled = true;
10+
let sakuraEnabled = false; // Disabled by default
1111

1212
function createSakuraPetals(count = 15) {
13+
// Clear existing petals first
14+
sakuraContainer.innerHTML = '';
15+
1316
for (let i = 0; i < count; i++) {
1417
const petal = document.createElement('img');
1518
petal.src = '/static/sakura.png';
@@ -21,19 +24,34 @@ document.addEventListener('DOMContentLoaded', () => {
2124
}
2225
}
2326

24-
if (sakuraEnabled) {
25-
sakuraContainer.style.display = 'block';
26-
createSakuraPetals();
27-
toggle.classList.add('active');
27+
// Initialize sakura state (disabled by default)
28+
if (sakuraContainer) {
29+
sakuraContainer.style.display = 'none';
30+
}
31+
if (toggle) {
32+
toggle.classList.remove('active');
2833
}
2934

30-
toggle.onclick = () => {
31-
sakuraEnabled = !sakuraEnabled;
32-
sakuraContainer.style.display = sakuraEnabled ? 'block' : 'none';
33-
toggle.classList.toggle('active', sakuraEnabled);
35+
if (toggle) {
36+
toggle.onclick = () => {
37+
sakuraEnabled = !sakuraEnabled;
38+
39+
if (sakuraContainer) {
40+
sakuraContainer.style.display = sakuraEnabled ? 'block' : 'none';
41+
}
42+
43+
toggle.classList.toggle('active', sakuraEnabled);
3444

35-
if (sakuraEnabled) createSakuraPetals();
36-
};
45+
if (sakuraEnabled) {
46+
createSakuraPetals();
47+
} else {
48+
// Clear petals when disabled
49+
if (sakuraContainer) {
50+
sakuraContainer.innerHTML = '';
51+
}
52+
}
53+
};
54+
}
3755
});
3856

3957
function initHomePage() {
@@ -513,24 +531,26 @@ async function initPastePage() {
513531
currentMatchIndex = -1;
514532
return;
515533
}
516-
// TODO: render syntax highlighting ??
517-
codeBlock.innerHTML = originalHTML; // reset before applying highlights
518534

519-
const safeTerm = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
520-
const regex = new RegExp(safeTerm, 'gi');
521-
let matchIdx = 0;
535+
// Reset to original content first
536+
codeBlock.innerHTML = originalHTML;
522537

523-
const highlighted = codeBlock.innerHTML.replace(regex, match =>
524-
`<mark data-idx="${matchIdx++}">${match}</mark>`
525-
);
538+
// Escape special regex characters
539+
const safeTerm = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
540+
const regex = new RegExp(`(${safeTerm})`, 'gi');
541+
542+
// Replace all matches with highlighted versions
543+
let matchCount = 0;
544+
const highlighted = codeBlock.innerHTML.replace(regex, (match) => {
545+
return `<mark data-idx="${matchCount++}">${match}</mark>`;
546+
});
526547

527548
codeBlock.innerHTML = highlighted;
528549
matches = Array.from(codeBlock.querySelectorAll('mark'));
529550

530-
if (matches.length) {
551+
if (matches.length > 0) {
531552
currentMatchIndex = 0;
532553
updateActiveMatch();
533-
searchResultCount.textContent = `1/${matches.length} results`;
534554
} else {
535555
currentMatchIndex = -1;
536556
searchResultCount.textContent = '0 results';
@@ -580,12 +600,14 @@ async function initPastePage() {
580600
navigateSearch(1);
581601
}
582602
});
583-
prevMatchButton.addEventListener('click', () => navigateSearch(-1));
584-
nextMatchButton.addEventListener('click', () => navigateSearch(1));
603+
604+
// Add event listeners (remove duplicates)
585605
if (searchButton) searchButton.addEventListener('click', performSearch);
586606
if (prevMatchButton) prevMatchButton.addEventListener('click', () => navigateSearch(-1));
587607
if (nextMatchButton) nextMatchButton.addEventListener('click', () => navigateSearch(1));
608+
588609
if (searchInput) {
610+
searchInput.addEventListener('input', performSearch); // Use 'input' for real-time search
589611
searchInput.addEventListener('keyup', (e) => {
590612
if (e.key === 'Enter') performSearch();
591613
});

0 commit comments

Comments
 (0)