mirror of
https://github.com/moku-project/Moku.git
synced 2026-06-13 09:19:56 -05:00
Chore: Restructure Repository for SvelteKit
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
<script lang="ts">
|
||||
export interface MenuItem {
|
||||
label: string;
|
||||
icon?: any;
|
||||
onClick: () => void;
|
||||
danger?: boolean;
|
||||
disabled?: boolean;
|
||||
separator?: never;
|
||||
children?: MenuEntry[];
|
||||
}
|
||||
export interface MenuSeparator { separator: true }
|
||||
export type MenuEntry = MenuItem | MenuSeparator;
|
||||
|
||||
interface Props {
|
||||
x: number;
|
||||
y: number;
|
||||
items: MenuEntry[];
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
let { x, y, items, onClose }: Props = $props();
|
||||
|
||||
let focused = $state(-1);
|
||||
let el = $state<HTMLDivElement | undefined>(undefined);
|
||||
let measured = $state(false);
|
||||
let pos = $state({ left: 0, top: 0 });
|
||||
let subOpen = $state(-1);
|
||||
let subEls = $state<(HTMLDivElement | null)[]>([]);
|
||||
|
||||
const actionable = $derived(
|
||||
items
|
||||
.map((_, i) => i)
|
||||
.filter((i) => !("separator" in items[i]) && !(items[i] as MenuItem).disabled)
|
||||
);
|
||||
|
||||
$effect(() => { if (actionable.length && focused === -1) focused = actionable[0]; });
|
||||
|
||||
function getZoom(): number {
|
||||
const raw = parseFloat(document.documentElement.style.zoom || "1") || 1;
|
||||
return raw > 10 ? raw / 100 : raw;
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (!el) return;
|
||||
const zoom = getZoom();
|
||||
const style = getComputedStyle(document.documentElement);
|
||||
const sidebarW = parseFloat(style.getPropertyValue('--sidebar-width')) || 52;
|
||||
const titlebarH = parseFloat(style.getPropertyValue('--titlebar-height')) || 36;
|
||||
const vw = window.innerWidth / zoom;
|
||||
const vh = window.innerHeight / zoom;
|
||||
const sx = x / zoom - sidebarW / zoom;
|
||||
const sy = y / zoom - titlebarH / zoom;
|
||||
const menuW = el.offsetWidth;
|
||||
const menuH = el.offsetHeight;
|
||||
pos = {
|
||||
left: Math.max(4, sx + menuW > vw ? sx - menuW : sx),
|
||||
top: Math.max(4, sy + menuH > vh ? sy - menuH : sy),
|
||||
};
|
||||
measured = true;
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (subOpen < 0) return;
|
||||
const sub = subEls[subOpen];
|
||||
if (!sub) return;
|
||||
requestAnimationFrame(() => {
|
||||
const zoom = getZoom();
|
||||
const vw = window.innerWidth / zoom;
|
||||
const rect = sub.getBoundingClientRect();
|
||||
if (rect.right / zoom > vw) sub.classList.add("sub-flip");
|
||||
else sub.classList.remove("sub-flip");
|
||||
});
|
||||
});
|
||||
|
||||
function onMouseDown(e: MouseEvent) {
|
||||
const inMain = el?.contains(e.target as Node);
|
||||
const inSub = subOpen >= 0 && subEls[subOpen]?.contains(e.target as Node);
|
||||
if (!inMain && !inSub) onClose();
|
||||
}
|
||||
|
||||
function onTouchStartOutside(e: TouchEvent) {
|
||||
const inMain = el?.contains(e.target as Node);
|
||||
const inSub = subOpen >= 0 && subEls[subOpen]?.contains(e.target as Node);
|
||||
if (!inMain && !inSub) onClose();
|
||||
}
|
||||
|
||||
function onKey(e: KeyboardEvent) {
|
||||
if (e.key === "Escape") {
|
||||
e.stopPropagation();
|
||||
if (subOpen >= 0) { subOpen = -1; } else { onClose(); }
|
||||
return;
|
||||
}
|
||||
if (e.key === "ArrowDown") {
|
||||
e.preventDefault();
|
||||
const cur = actionable.indexOf(focused);
|
||||
focused = actionable[(cur + 1) % actionable.length] ?? actionable[0];
|
||||
return;
|
||||
}
|
||||
if (e.key === "ArrowUp") {
|
||||
e.preventDefault();
|
||||
const cur = actionable.indexOf(focused);
|
||||
focused = actionable[(cur - 1 + actionable.length) % actionable.length] ?? actionable[0];
|
||||
return;
|
||||
}
|
||||
if (e.key === "ArrowRight" && focused >= 0) {
|
||||
const item = items[focused] as MenuItem;
|
||||
if (item?.children?.length) { subOpen = focused; return; }
|
||||
}
|
||||
if (e.key === "ArrowLeft") { subOpen = -1; return; }
|
||||
if (e.key === "Enter" && focused >= 0) {
|
||||
e.preventDefault();
|
||||
const item = items[focused] as MenuItem;
|
||||
if (item?.children?.length) { subOpen = focused; return; }
|
||||
if (item && !item.disabled) { item.onClick(); onClose(); }
|
||||
}
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
document.addEventListener("mousedown", onMouseDown, true);
|
||||
document.addEventListener("touchstart", onTouchStartOutside, true);
|
||||
document.addEventListener("keydown", onKey, true);
|
||||
return () => {
|
||||
document.removeEventListener("mousedown", onMouseDown, true);
|
||||
document.removeEventListener("touchstart", onTouchStartOutside, true);
|
||||
document.removeEventListener("keydown", onKey, true);
|
||||
};
|
||||
});
|
||||
</script>
|
||||
|
||||
<div bind:this={el} class="menu" role="menu" tabindex="-1"
|
||||
style="left:{pos.left}px;top:{pos.top}px;visibility:{measured ? 'visible' : 'hidden'}"
|
||||
oncontextmenu={(e) => e.preventDefault()}>
|
||||
{#each items as item, i}
|
||||
{#if "separator" in item}
|
||||
<div class="sep"></div>
|
||||
{:else}
|
||||
{@const mi = item as MenuItem}
|
||||
{@const hasSub = !!mi.children?.length}
|
||||
<div class="item-wrap">
|
||||
<button
|
||||
class="item"
|
||||
class:danger={mi.danger}
|
||||
class:disabled={mi.disabled}
|
||||
class:focused={focused === i}
|
||||
class:has-sub={hasSub}
|
||||
disabled={mi.disabled}
|
||||
onclick={() => {
|
||||
if (mi.disabled) return;
|
||||
if (hasSub) { subOpen = subOpen === i ? -1 : i; return; }
|
||||
mi.onClick(); onClose();
|
||||
}}
|
||||
onmouseenter={() => { if (!mi.disabled) { focused = i; subOpen = hasSub ? i : -1; } }}
|
||||
onmouseleave={() => { focused = -1; }}
|
||||
>
|
||||
<span class="icon" class:icon-danger={mi.danger}>
|
||||
{#if mi.icon}<mi.icon size={13} weight="light" />{/if}
|
||||
</span>
|
||||
<span class="label">{mi.label}</span>
|
||||
{#if hasSub}<span class="sub-arrow">›</span>{/if}
|
||||
</button>
|
||||
{#if hasSub && subOpen === i}
|
||||
<div bind:this={subEls[i]} class="menu submenu" role="menu" tabindex="-1"
|
||||
onmouseenter={() => { subOpen = i; }}>
|
||||
{#each mi.children as child}
|
||||
{#if "separator" in child}
|
||||
<div class="sep"></div>
|
||||
{:else}
|
||||
{@const sc = child as MenuItem}
|
||||
<button
|
||||
class="item"
|
||||
class:danger={sc.danger}
|
||||
class:disabled={sc.disabled}
|
||||
disabled={sc.disabled}
|
||||
onclick={() => { if (!sc.disabled) { sc.onClick(); onClose(); } }}
|
||||
>
|
||||
<span class="icon" class:icon-danger={sc.danger}>
|
||||
{#if sc.icon}<sc.icon size={13} weight="light" />{/if}
|
||||
</span>
|
||||
<span class="label">{sc.label}</span>
|
||||
</button>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.menu {
|
||||
position: fixed; z-index: 200;
|
||||
background: var(--bg-raised); border: 1px solid var(--border-base);
|
||||
border-radius: var(--radius-lg); padding: var(--sp-1); min-width: 190px;
|
||||
box-shadow: 0 0 0 1px rgba(0,0,0,0.08), 0 4px 12px rgba(0,0,0,0.35), 0 16px 40px rgba(0,0,0,0.25);
|
||||
animation: scaleIn 0.1s ease both; transform-origin: top left;
|
||||
}
|
||||
.item-wrap { position: relative; }
|
||||
.submenu {
|
||||
position: absolute;
|
||||
left: 100%;
|
||||
top: 0;
|
||||
z-index: 201;
|
||||
animation: scaleIn 0.08s ease both;
|
||||
transform-origin: top left;
|
||||
}
|
||||
:global(.submenu.sub-flip) {
|
||||
left: auto;
|
||||
right: 100%;
|
||||
transform-origin: top right;
|
||||
}
|
||||
.item {
|
||||
display: flex; align-items: center; gap: var(--sp-2);
|
||||
width: 100%; padding: 5px var(--sp-2); border-radius: var(--radius-md);
|
||||
font-size: var(--text-sm); color: var(--text-secondary);
|
||||
text-align: left; cursor: pointer; background: none; border: none; outline: none;
|
||||
transition: background var(--t-fast), color var(--t-fast);
|
||||
position: relative;
|
||||
}
|
||||
.item:hover:not(.disabled), .item.focused:not(.disabled) { background: var(--bg-overlay); color: var(--text-primary); }
|
||||
.item.danger { color: var(--color-error); }
|
||||
.item.danger:hover:not(.disabled), .item.danger.focused:not(.disabled) { background: var(--color-error-bg); }
|
||||
.item.disabled { opacity: 0.3; cursor: default; pointer-events: none; }
|
||||
.icon { display: flex; align-items: center; justify-content: center; width: 18px; height: 18px; flex-shrink: 0; color: var(--text-faint); border-radius: var(--radius-sm); }
|
||||
.icon-danger { color: var(--color-error); opacity: 0.7; }
|
||||
.label { flex: 1; line-height: 1.3; }
|
||||
.sub-arrow { font-size: 14px; color: var(--text-faint); line-height: 1; margin-left: auto; padding-left: var(--sp-1); }
|
||||
.sep { height: 1px; background: var(--border-dim); margin: 3px var(--sp-1); }
|
||||
|
||||
@keyframes scaleIn { from { opacity: 0; transform: scale(0.97) } to { opacity: 1; transform: scale(1) } }
|
||||
</style>
|
||||
@@ -0,0 +1,130 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte";
|
||||
import { MagnifyingGlass, CircleNotch, CaretDown, CaretRight } from "phosphor-svelte";
|
||||
import { gql } from "@api/client";
|
||||
import Thumbnail from "@shared/manga/Thumbnail.svelte";
|
||||
import { GET_SOURCES } from "@api/queries/extensions";
|
||||
import { store } from "@store/state.svelte";
|
||||
import type { Source } from "@types/index";
|
||||
|
||||
let sources: Source[] = $state([]);
|
||||
let loading = $state(true);
|
||||
let lang = $state("all");
|
||||
let search = $state("");
|
||||
let expanded = $state(new Set<string>());
|
||||
|
||||
onMount(() => {
|
||||
gql<{ sources: { nodes: Source[] } }>(GET_SOURCES)
|
||||
.then((d) => { sources = d.sources.nodes; })
|
||||
.catch(console.error)
|
||||
.finally(() => { loading = false; });
|
||||
});
|
||||
|
||||
const langs = $derived(["all", ...Array.from(new Set(sources.map((s) => s.lang))).sort()]);
|
||||
const filtered = $derived(sources.filter((src) => {
|
||||
if (src.id === "0") return false;
|
||||
const matchLang = lang === "all" || src.lang === lang;
|
||||
const matchSearch = src.name.toLowerCase().includes(search.toLowerCase())
|
||||
|| src.displayName.toLowerCase().includes(search.toLowerCase());
|
||||
return matchLang && matchSearch;
|
||||
}));
|
||||
|
||||
const groups = $derived.by(() => {
|
||||
const map = new Map<string, { name: string; icon: string; sources: Source[] }>();
|
||||
for (const src of filtered) {
|
||||
if (!map.has(src.name)) map.set(src.name, { name: src.name, icon: src.iconUrl, sources: [] });
|
||||
map.get(src.name)!.sources.push(src);
|
||||
}
|
||||
return Array.from(map.values());
|
||||
});
|
||||
|
||||
function toggleGroup(name: string) {
|
||||
const next = new Set(expanded);
|
||||
next.has(name) ? next.delete(name) : next.add(name);
|
||||
expanded = next;
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="root">
|
||||
<div class="header">
|
||||
<h1 class="heading">Sources</h1>
|
||||
<div class="search-wrap">
|
||||
<MagnifyingGlass size={12} class="search-icon" weight="light" />
|
||||
<input class="search" placeholder="Search" bind:value={search} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<div class="lang-row">
|
||||
{#each langs as l}
|
||||
<button class="lang-btn" class:active={lang === l} onclick={() => lang = l}>
|
||||
{l === "all" ? "All" : l.toUpperCase()}
|
||||
</button>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
{#if loading}
|
||||
<div class="empty"><CircleNotch size={16} weight="light" class="anim-spin" style="color:var(--text-faint)" /></div>
|
||||
{:else if groups.length === 0}
|
||||
<div class="empty">No sources found.</div>
|
||||
{:else}
|
||||
<div class="list">
|
||||
{#each groups as g}
|
||||
{@const single = g.sources.length === 1}
|
||||
{@const open = expanded.has(g.name)}
|
||||
<div>
|
||||
<button class="row" onclick={() => single ? store.activeSource = g.sources[0] : toggleGroup(g.name)}>
|
||||
<Thumbnail src={g.icon} alt={g.name} class="icon" onerror={(e) => ((e.target as HTMLImageElement).style.display = "none")} />
|
||||
<div class="info">
|
||||
<span class="name">{g.name}</span>
|
||||
<span class="meta">{single ? `${g.sources[0].lang.toUpperCase()}${g.sources[0].isNsfw ? " · NSFW" : ""}` : `${g.sources.length} languages`}</span>
|
||||
</div>
|
||||
<span class="arrow">
|
||||
{#if single}→{:else if open}<CaretDown size={12} weight="light" />{:else}<CaretRight size={12} weight="light" />{/if}
|
||||
</span>
|
||||
</button>
|
||||
{#if !single && open}
|
||||
{#each g.sources as src}
|
||||
<button class="row row-indented" onclick={() => store.activeSource = src}>
|
||||
<div class="indent-spacer"></div>
|
||||
<div class="info"><span class="name">{src.lang.toUpperCase()}{src.isNsfw ? " · NSFW" : ""}</span></div>
|
||||
<span class="arrow">→</span>
|
||||
</button>
|
||||
{/each}
|
||||
{/if}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.root { display: flex; flex-direction: column; height: 100%; overflow: hidden; animation: fadeIn 0.14s ease both; }
|
||||
.content { flex: 1; overflow-y: auto; padding: var(--sp-4) var(--sp-6) var(--sp-6); display: flex; flex-direction: column; gap: var(--sp-3); }
|
||||
.header { display: flex; align-items: center; justify-content: space-between; padding: var(--sp-4) var(--sp-6); border-bottom: 1px solid var(--border-dim); flex-shrink: 0; }
|
||||
.heading { font-family: var(--font-ui); font-size: var(--text-xs); font-weight: var(--weight-normal); color: var(--text-faint); letter-spacing: var(--tracking-wider); text-transform: uppercase; }
|
||||
.search-wrap { position: relative; display: flex; align-items: center; }
|
||||
.search-wrap :global(.search-icon) { position: absolute; left: 9px; color: var(--text-faint); pointer-events: none; }
|
||||
.search { background: var(--bg-raised); border: 1px solid var(--border-dim); border-radius: var(--radius-md); padding: 5px 10px 5px 26px; color: var(--text-primary); font-size: var(--text-sm); width: 180px; outline: none; transition: border-color var(--t-base); }
|
||||
.search::placeholder { color: var(--text-faint); }
|
||||
.search:focus { border-color: var(--border-strong); }
|
||||
.lang-row { display: flex; flex-wrap: wrap; gap: var(--sp-1); }
|
||||
.lang-btn { font-family: var(--font-ui); font-size: var(--text-2xs); letter-spacing: var(--tracking-wider); padding: 3px 8px; border-radius: var(--radius-sm); border: 1px solid var(--border-dim); background: none; color: var(--text-faint); cursor: pointer; transition: color var(--t-base), border-color var(--t-base), background var(--t-base); }
|
||||
.lang-btn:hover { color: var(--text-muted); border-color: var(--border-strong); }
|
||||
.lang-btn.active { background: var(--accent-muted); border-color: var(--accent-dim); color: var(--accent-fg); }
|
||||
.list { display: flex; flex-direction: column; gap: 1px; }
|
||||
.row { display: flex; align-items: center; gap: var(--sp-3); padding: 9px var(--sp-3); border-radius: var(--radius-md); border: 1px solid transparent; background: none; text-align: left; width: 100%; cursor: pointer; transition: background var(--t-fast), border-color var(--t-fast); }
|
||||
.row:hover { background: var(--bg-raised); border-color: var(--border-dim); }
|
||||
.row-indented { padding-left: var(--sp-5); }
|
||||
.indent-spacer { width: 32px; flex-shrink: 0; }
|
||||
:global(.icon) { width: 32px; height: 32px; border-radius: var(--radius-md); object-fit: cover; flex-shrink: 0; background: var(--bg-raised); }
|
||||
.info { flex: 1; display: flex; flex-direction: column; gap: 2px; overflow: hidden; }
|
||||
.name { font-size: var(--text-base); font-weight: var(--weight-medium); color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||
.meta { font-family: var(--font-ui); font-size: var(--text-2xs); color: var(--text-faint); letter-spacing: var(--tracking-wide); }
|
||||
.arrow { display: flex; align-items: center; font-family: var(--font-ui); font-size: var(--text-xs); color: var(--text-faint); flex-shrink: 0; opacity: 0; transition: opacity var(--t-base); }
|
||||
.row:hover .arrow { opacity: 1; }
|
||||
.empty { display: flex; align-items: center; justify-content: center; height: 160px; color: var(--text-faint); font-family: var(--font-ui); font-size: var(--text-xs); letter-spacing: var(--tracking-wide); }
|
||||
|
||||
@keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }
|
||||
</style>
|
||||
Reference in New Issue
Block a user