352 lines
8.1 KiB
Svelte
352 lines
8.1 KiB
Svelte
<script lang="ts">
|
|
import SafeMedia from '$lib/components/SafeMedia.svelte';
|
|
import Icon from '$lib/components/ui/Icon.svelte';
|
|
import Spinner from '$lib/components/ui/Spinner.svelte';
|
|
import { t } from '$lib/i18n.svelte';
|
|
import { exportLastFrame } from '$lib/media/lastFrame';
|
|
|
|
interface Props {
|
|
src: string | null;
|
|
alt?: string;
|
|
onClose: () => void;
|
|
/** Optional filmstrip — enables prev/next buttons and arrow keys. */
|
|
images?: string[];
|
|
index?: number;
|
|
onnavigate?: (i: number) => void;
|
|
caption?: string;
|
|
/** Render a video player instead of a zoomable image. */
|
|
kind?: 'image' | 'video';
|
|
}
|
|
|
|
let {
|
|
src,
|
|
alt = '',
|
|
onClose,
|
|
images,
|
|
index = 0,
|
|
onnavigate,
|
|
caption,
|
|
kind = 'image',
|
|
}: Props = $props();
|
|
|
|
let zoomed = $state(false);
|
|
|
|
const isVideo = $derived(kind === 'video');
|
|
|
|
$effect(() => {
|
|
void src;
|
|
zoomed = false;
|
|
});
|
|
|
|
const hasNav = $derived(Boolean(images && images.length > 1 && onnavigate));
|
|
|
|
function step(delta: number) {
|
|
if (!images || images.length === 0 || !onnavigate) return;
|
|
onnavigate((index + delta + images.length) % images.length);
|
|
}
|
|
|
|
function onKeydown(e: KeyboardEvent) {
|
|
if (e.key === 'Escape') onClose();
|
|
else if (hasNav && e.key === 'ArrowLeft') step(-1);
|
|
else if (hasNav && e.key === 'ArrowRight') step(1);
|
|
}
|
|
|
|
const downloadName = $derived.by(() => {
|
|
if (!src) return 'media';
|
|
try {
|
|
const query = src.split('?')[1] ?? '';
|
|
const path = new URLSearchParams(query).get('path');
|
|
const name = path ? decodeURIComponent(path).split(/[\\/]/).pop() : null;
|
|
if (name) return name;
|
|
} catch {
|
|
// fall through to the generic name
|
|
}
|
|
return alt ? `${alt.trim().replace(/\s+/g, '-').toLowerCase()}.png` : 'media';
|
|
});
|
|
|
|
let frameBusy = $state(false);
|
|
async function exportTail() {
|
|
if (!src || frameBusy) return;
|
|
frameBusy = true;
|
|
try {
|
|
await exportLastFrame(src);
|
|
} finally {
|
|
frameBusy = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<svelte:window onkeydown={src ? onKeydown : undefined} />
|
|
|
|
{#if src}
|
|
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
|
|
<div
|
|
class="backdrop"
|
|
role="dialog"
|
|
aria-modal="true"
|
|
aria-label={isVideo ? t('lightbox.videoPreview') : t('lightbox.imagePreview')}
|
|
tabindex="-1"
|
|
onclick={onClose}
|
|
>
|
|
<div class="toolbar">
|
|
{#if hasNav}
|
|
<span class="counter">{index + 1} / {images?.length ?? 0}</span>
|
|
{/if}
|
|
{#if !isVideo}
|
|
<button
|
|
class="tool"
|
|
type="button"
|
|
aria-label={zoomed ? t('lightbox.fitToScreen') : t('lightbox.zoom100')}
|
|
title={zoomed ? t('lightbox.fitToScreen') : t('lightbox.zoom100')}
|
|
onclick={() => (zoomed = !zoomed)}
|
|
>
|
|
<Icon name="zoom-in" size={17} />
|
|
</button>
|
|
{/if}
|
|
{#if isVideo}
|
|
<button
|
|
class="tool"
|
|
type="button"
|
|
aria-label={frameBusy ? t('lastFrame.exporting') : t('lastFrame.export')}
|
|
title={frameBusy
|
|
? t('lastFrame.exporting')
|
|
: `${t('lastFrame.export')} · ${t('lastFrame.hint')}`}
|
|
disabled={frameBusy}
|
|
aria-busy={frameBusy}
|
|
onclick={(e) => {
|
|
e.stopPropagation();
|
|
exportTail();
|
|
}}
|
|
>
|
|
{#if frameBusy}
|
|
<Spinner size="sm" />
|
|
{:else}
|
|
<Icon name="image" size={17} />
|
|
{/if}
|
|
</button>
|
|
{/if}
|
|
<a
|
|
class="tool"
|
|
href={src}
|
|
download={downloadName}
|
|
rel="external"
|
|
aria-label={t('lightbox.downloadMedia')}
|
|
title={t('lightbox.download')}
|
|
onclick={(e) => e.stopPropagation()}
|
|
>
|
|
<Icon name="download" size={17} />
|
|
</a>
|
|
<button
|
|
class="tool"
|
|
type="button"
|
|
aria-label={t('lightbox.closePreview')}
|
|
title={t('lightbox.closeEsc')}
|
|
onclick={onClose}
|
|
>
|
|
<Icon name="close" size={17} />
|
|
</button>
|
|
</div>
|
|
|
|
{#if hasNav}
|
|
<button
|
|
class="nav prev"
|
|
type="button"
|
|
aria-label={t('lightbox.prev')}
|
|
onclick={(e) => {
|
|
e.stopPropagation();
|
|
step(-1);
|
|
}}
|
|
>
|
|
<Icon name="chevron-left" size={22} />
|
|
</button>
|
|
<button
|
|
class="nav next"
|
|
type="button"
|
|
aria-label={t('lightbox.next')}
|
|
onclick={(e) => {
|
|
e.stopPropagation();
|
|
step(1);
|
|
}}
|
|
>
|
|
<Icon name="chevron-right" size={22} />
|
|
</button>
|
|
{/if}
|
|
|
|
{#if isVideo}
|
|
<!-- Clicks on the player (play/pause/seek) must not close the modal -->
|
|
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
|
|
<div class="player-frame" onclick={(e) => e.stopPropagation()}>
|
|
<SafeMedia
|
|
class="player"
|
|
{src}
|
|
{alt}
|
|
kind="video"
|
|
label={t('lightbox.videoUnavailable')}
|
|
controls
|
|
preload="metadata"
|
|
/>
|
|
</div>
|
|
{:else}
|
|
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
|
|
<div
|
|
class="frame"
|
|
class:zoomed
|
|
title={zoomed ? t('lightbox.clickToFit') : t('lightbox.clickToZoom')}
|
|
onclick={(e) => {
|
|
e.stopPropagation();
|
|
zoomed = !zoomed;
|
|
}}
|
|
>
|
|
<SafeMedia class="preview" {src} {alt} label={t('lightbox.imageUnavailable')} />
|
|
</div>
|
|
{/if}
|
|
{#if caption}
|
|
<p class="caption">{caption}</p>
|
|
{/if}
|
|
<p class="hint">
|
|
{#if isVideo}
|
|
{t('lightbox.closeHint')} ·
|
|
{:else}
|
|
{t('lightbox.clickToZoom')} · {t('lightbox.closeHint')} ·
|
|
{/if}
|
|
<a href={src} target="_blank" rel="noopener external" onclick={(e) => e.stopPropagation()}
|
|
>{t('lightbox.openOriginal')}</a
|
|
>
|
|
</p>
|
|
</div>
|
|
{/if}
|
|
|
|
<style>
|
|
.backdrop {
|
|
position: fixed;
|
|
inset: 0;
|
|
z-index: 10000;
|
|
background: rgba(0, 0, 0, 0.88);
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 48px 24px 32px;
|
|
gap: 12px;
|
|
}
|
|
.toolbar {
|
|
position: absolute;
|
|
top: 12px;
|
|
right: 16px;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
.counter {
|
|
font-size: 12px;
|
|
color: color-mix(in srgb, var(--on-media) 65%, transparent);
|
|
margin-right: 4px;
|
|
font-variant-numeric: tabular-nums;
|
|
}
|
|
.tool {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 40px;
|
|
height: 40px;
|
|
border: none;
|
|
border-radius: 999px;
|
|
background: color-mix(in srgb, var(--on-media) 12%, transparent);
|
|
color: var(--on-media);
|
|
cursor: pointer;
|
|
transition: background 0.15s;
|
|
}
|
|
.tool:hover {
|
|
background: color-mix(in srgb, var(--on-media) 22%, transparent);
|
|
}
|
|
.tool:focus-visible {
|
|
outline: 2px solid var(--accent);
|
|
outline-offset: 2px;
|
|
}
|
|
.nav {
|
|
position: absolute;
|
|
top: 50%;
|
|
transform: translateY(-50%);
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 44px;
|
|
height: 44px;
|
|
border: none;
|
|
border-radius: 999px;
|
|
background: color-mix(in srgb, var(--on-media) 10%, transparent);
|
|
color: var(--on-media);
|
|
cursor: pointer;
|
|
transition: background 0.15s;
|
|
}
|
|
.nav:hover {
|
|
background: color-mix(in srgb, var(--on-media) 22%, transparent);
|
|
}
|
|
.nav:focus-visible {
|
|
outline: 2px solid var(--accent);
|
|
outline-offset: 2px;
|
|
}
|
|
.nav.prev {
|
|
left: 16px;
|
|
}
|
|
.nav.next {
|
|
right: 16px;
|
|
}
|
|
.frame {
|
|
max-width: min(96vw, 1600px);
|
|
max-height: calc(100vh - 140px);
|
|
display: flex;
|
|
overflow: auto;
|
|
cursor: zoom-in;
|
|
}
|
|
.frame.zoomed {
|
|
cursor: zoom-out;
|
|
}
|
|
.frame :global(.preview) {
|
|
max-width: min(96vw, 1600px);
|
|
max-height: calc(100vh - 140px);
|
|
width: auto;
|
|
height: auto;
|
|
object-fit: contain;
|
|
border-radius: 4px;
|
|
box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
|
|
min-height: 120px;
|
|
min-width: 200px;
|
|
margin: auto;
|
|
}
|
|
.frame.zoomed :global(.preview) {
|
|
max-width: none;
|
|
max-height: none;
|
|
}
|
|
/* ── Video player mode ───────────────────────────────────── */
|
|
.player-frame {
|
|
max-width: min(96vw, 1600px);
|
|
width: 100%;
|
|
display: flex;
|
|
justify-content: center;
|
|
}
|
|
.player-frame :global(.player) {
|
|
max-width: min(96vw, 1600px);
|
|
max-height: calc(100vh - 160px);
|
|
width: 100%;
|
|
border-radius: 6px;
|
|
background: #000;
|
|
box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
|
|
}
|
|
.caption {
|
|
margin: 0;
|
|
font-size: 13px;
|
|
color: color-mix(in srgb, var(--on-media) 85%, transparent);
|
|
max-width: min(96vw, 720px);
|
|
text-align: center;
|
|
}
|
|
.hint {
|
|
margin: 0;
|
|
font-size: 12px;
|
|
color: color-mix(in srgb, var(--on-media) 55%, transparent);
|
|
}
|
|
.hint a {
|
|
color: color-mix(in srgb, var(--on-media) 85%, transparent);
|
|
}
|
|
</style>
|