summaryrefslogtreecommitdiff
path: root/src/ts/script.ts
blob: 56c6d63020feb14e2197441f2796f6412f8a1782 (plain) (blame)
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
import Prism from 'prismjs';
import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-css';
import 'prismjs/components/prism-markup';
import { initOneko } from './oneko';

// Auto-detect asset base from the bundled script's origin
(() => {
    window.ASSET_BASE = '';
    const bundleScript = document.querySelector('script[src*="bundle"]');
    if (bundleScript?.src) {
        try {
            const url = new URL(bundleScript.src, window.location.href);
            window.ASSET_BASE = url.origin;
        } catch {
            // Fall back to empty string
        }
    }
})();

(() => {
    const toggleButton = document.getElementById('theme-toggle') as HTMLInputElement;
    const html = document.documentElement;

    const sessionTheme = sessionStorage.getItem('theme');
    const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;

    const initialTheme = sessionTheme || (systemPrefersDark ? 'dark' : 'light');

    if (initialTheme === 'dark') {
        html.setAttribute('data-theme', 'dark');
        toggleButton.checked = true;
    }

    toggleButton.addEventListener('change', () => {
        const theme = html.getAttribute('data-theme');

        if (theme === 'dark') {
            html.removeAttribute('data-theme');
            sessionStorage.setItem('theme', 'light');
            toggleButton.checked = false;
        } else {
            html.setAttribute('data-theme', 'dark');
            sessionStorage.setItem('theme', 'dark');
            toggleButton.checked = true;
        }
    });
})();

(() => {
    const colors = ['#ff69b4', '#b19cd9', '#8b6f47', '#ff85c0', '#c4b5fd', '#d4a574'];
    const shapes = ['❀', '✿', '✽', '✾', '✻', '❊', '❋', '✼'];

    document.addEventListener('mousemove', (e: MouseEvent) => {
        createParticle(e.clientX, e.clientY);
    });

    const createParticle = (x: number, y: number) => {
        const particle = document.createElement('div');
        particle.className = 'fairy-dust';

        const shape = shapes[Math.floor(Math.random() * shapes.length)];
        const size = Math.random() * 8 + 6;
        const color = colors[Math.floor(Math.random() * colors.length)];
        const offsetX = (Math.random() - 0.5) * 20;
        const offsetY = (Math.random() - 0.5) * 20;
        const rotation = Math.random() * 360;

        particle.textContent = shape;
        particle.style.cssText = `
      position: fixed;
      left: ${x + offsetX}px;
      top: ${y + offsetY}px;
      font-size: ${size}px;
      color: ${color};
      opacity: 0.4;
      pointer-events: none;
      z-index: 9001; /* it's over 9000 */
      line-height: 1;
      transform: rotate(${rotation}deg);
      animation: fairy-float 0.8s ease-out forwards;
    `;

        document.body.appendChild(particle);
        setTimeout(() => particle.remove(), 800);
    };
})();

document.addEventListener('DOMContentLoaded', () => {
    Prism.highlightAll();
    initOneko();
});