Add wallpaper picker and compression script

This commit is contained in:
SirBlob committed 2026-11-01 22:05:40 -05:00
1 parent d81ec1b057
commit 8342304354
11 files changed
+392 -7

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
import { wallpapers, type Wallpaper } from '#lib/content';
const wallpaperStorageKey = 'wallpaper';
function readStoredWallpaperId(): string | null {
try {
return localStorage.getItem(wallpaperStorageKey);
} catch {
return null;
}
}
function storeWallpaperId(wallpaperId: string | null) {
try {
if (wallpaperId) localStorage.setItem(wallpaperStorageKey, wallpaperId);
else localStorage.removeItem(wallpaperStorageKey);
} catch {}
}
function findWallpaper(wallpaperId: string | null): Wallpaper | null {
return wallpapers.find((wallpaper) => wallpaper.id === wallpaperId) ?? null;
}
function applyWallpaper(wallpaper: Wallpaper | null) {
const rootStyle = document.documentElement.style;
if (wallpaper) rootStyle.setProperty('--wallpaper', `url('${wallpaper.image}')`);
else rootStyle.removeProperty('--wallpaper');
}
class WallpaperState {
currentId = $state<string | null>(null);
restore() {
const stored = findWallpaper(readStoredWallpaperId());
this.currentId = stored?.id ?? null;
applyWallpaper(stored);
}
set(wallpaperId: string | null) {
const wallpaper = findWallpaper(wallpaperId);
this.currentId = wallpaper?.id ?? null;
applyWallpaper(wallpaper);
storeWallpaperId(this.currentId);
}
shuffle() {
const choices = wallpapers.filter((wallpaper) => wallpaper.id !== this.currentId);
if (choices.length) this.set(choices[Math.floor(Math.random() * choices.length)].id);
}
}
export const wallpaperState = new WallpaperState();