241 lines
5.6 KiB
Svelte
241 lines
5.6 KiB
Svelte
<script lang="ts">
|
||
import Icon from '$lib/components/ui/Icon.svelte';
|
||
import { assetUrl } from '$lib/api';
|
||
import {
|
||
IMAGE_SLOT_ACCEPT,
|
||
formatBytes,
|
||
uploadBaseName,
|
||
} from '$lib/comfy/imageFile';
|
||
import { t } from '$lib/i18n.svelte';
|
||
|
||
/**
|
||
* Single media input slot as a composer tile (spec-v2 §3.3 输入槽):
|
||
* empty = dashed + "+", drag-hover = gold, uploading = ring, filled = thumb
|
||
* (click to replace) with a ✕ on hover/focus. `need` = required but empty
|
||
* after a blocked 生成 (amber); `error` = rejected file / invalid (red).
|
||
*/
|
||
interface Props {
|
||
label: string;
|
||
caption?: boolean;
|
||
/** Hint after the label, e.g. 必填 · 1 张. */
|
||
hint?: string;
|
||
required?: boolean;
|
||
/** Current value: a server-side path ('' = empty). */
|
||
value: string;
|
||
kind: 'image' | 'video' | 'audio';
|
||
/** Byte size of the current file when known. */
|
||
size?: number;
|
||
uploading?: boolean;
|
||
uploadName?: string;
|
||
need?: boolean;
|
||
error?: string;
|
||
/** Show the 从结果选 action (image slots fed from finished takes). */
|
||
allowFromResults?: boolean;
|
||
/** Anchor id so 「还缺:…」 can jump focus here. */
|
||
anchorId?: string;
|
||
onFile: (file: File) => void;
|
||
onFromResults?: () => void;
|
||
onFromScene?: () => void;
|
||
onClear: () => void;
|
||
}
|
||
|
||
let {
|
||
label,
|
||
caption = false,
|
||
hint,
|
||
required = false,
|
||
value,
|
||
kind,
|
||
size,
|
||
uploading = false,
|
||
uploadName,
|
||
need = false,
|
||
error,
|
||
allowFromResults = false,
|
||
anchorId,
|
||
onFile,
|
||
onFromResults,
|
||
onFromScene,
|
||
onClear,
|
||
}: Props = $props();
|
||
|
||
let fileInput = $state<HTMLInputElement | null>(null);
|
||
let dragDepth = $state(0);
|
||
let broken = $state(false);
|
||
let dims = $state('');
|
||
$effect(() => {
|
||
void value;
|
||
broken = false;
|
||
dims = '';
|
||
});
|
||
|
||
const filled = $derived(!!value && !broken);
|
||
const meta = $derived(
|
||
[uploadBaseName(value), dims, size != null ? formatBytes(size) : '']
|
||
.filter(Boolean)
|
||
.join(' · '),
|
||
);
|
||
|
||
function accept(): string {
|
||
if (kind === 'video') return 'video/*,.mp4,.webm,.mov,.mkv';
|
||
if (kind === 'audio') return 'audio/*';
|
||
return IMAGE_SLOT_ACCEPT;
|
||
}
|
||
|
||
function noteDims(e: Event) {
|
||
const img = e.currentTarget as HTMLImageElement;
|
||
if (img.naturalWidth) dims = `${img.naturalWidth}×${img.naturalHeight}`;
|
||
}
|
||
|
||
function pick() {
|
||
if (uploading || !fileInput) return;
|
||
fileInput.value = '';
|
||
fileInput.click();
|
||
}
|
||
|
||
function onDrop(e: DragEvent) {
|
||
e.preventDefault();
|
||
e.stopPropagation();
|
||
dragDepth = 0;
|
||
if (uploading) return;
|
||
const file = e.dataTransfer?.files?.[0];
|
||
if (file) onFile(file);
|
||
}
|
||
|
||
const dragHandlers = {
|
||
ondragenter: (e: DragEvent) => {
|
||
e.preventDefault();
|
||
dragDepth += 1;
|
||
},
|
||
ondragleave: () => (dragDepth = Math.max(0, dragDepth - 1)),
|
||
ondragover: (e: DragEvent) => e.preventDefault(),
|
||
ondrop: onDrop,
|
||
};
|
||
</script>
|
||
|
||
<div class="a-slot">
|
||
{#if !caption}<div class="a-slot-label">
|
||
<span>{label}</span>
|
||
{#if required}<span class="req" aria-hidden="true">*</span>{/if}
|
||
{#if hint}<span>{hint}</span>{/if}
|
||
{#if onFromScene}<button
|
||
type="button"
|
||
class="a-btn ghost xs"
|
||
onclick={onFromScene}>从三维搭景截取</button
|
||
>{/if}
|
||
{#if allowFromResults && onFromResults}
|
||
<button type="button" class="a-btn ghost xs" onclick={onFromResults}>
|
||
{t('studio.slotFromResultsShort')}
|
||
</button>
|
||
{/if}
|
||
</div>{/if}
|
||
|
||
<input
|
||
bind:this={fileInput}
|
||
type="file"
|
||
class="sr-file"
|
||
tabindex="-1"
|
||
aria-hidden="true"
|
||
accept={accept()}
|
||
onchange={(e) => {
|
||
const f = e.currentTarget.files?.[0];
|
||
if (f) onFile(f);
|
||
}}
|
||
/>
|
||
|
||
<div class="a-slot-row">
|
||
{#if filled}
|
||
<div
|
||
class="a-tile"
|
||
class:drag={dragDepth > 0}
|
||
class:bad={!!error}
|
||
{...dragHandlers}
|
||
>
|
||
<button
|
||
type="button"
|
||
id={anchorId}
|
||
class="a-tile-hit"
|
||
title={`${t('studio.refReplace')} · ${meta}`}
|
||
aria-label={`${t('studio.refReplace')} ${label}`}
|
||
disabled={uploading}
|
||
onclick={pick}
|
||
>
|
||
{#if kind === 'image'}
|
||
<img
|
||
src={assetUrl(value)}
|
||
alt=""
|
||
onload={noteDims}
|
||
onerror={() => (broken = true)}
|
||
/>
|
||
{:else if kind === 'video'}
|
||
<video src={assetUrl(value)} preload="metadata" muted playsinline
|
||
></video>
|
||
{:else}
|
||
<Icon name="music" size={18} />
|
||
{/if}
|
||
</button>
|
||
{#if caption}<span class="a-slot-caption"
|
||
>{label}{hint ? ` · ${hint}` : ''}</span
|
||
>{/if}
|
||
{#if uploading}
|
||
<span class="a-tile-ring" title={uploadName}><i></i></span>
|
||
{:else}
|
||
<button
|
||
type="button"
|
||
class="a-tile-x"
|
||
aria-label={`${t('studio.deleteBtn')} ${label}`}
|
||
onclick={onClear}
|
||
>
|
||
<Icon name="close" size={10} />
|
||
</button>
|
||
{/if}
|
||
</div>
|
||
{:else}
|
||
<button
|
||
type="button"
|
||
id={anchorId}
|
||
class="a-tile add"
|
||
class:drag={dragDepth > 0}
|
||
class:need={need && !error}
|
||
class:bad={!!error}
|
||
title={t('studio.slotPick')}
|
||
aria-label={`${label} · ${t('studio.slotPick')}`}
|
||
aria-busy={uploading || undefined}
|
||
disabled={uploading}
|
||
onclick={pick}
|
||
{...dragHandlers}
|
||
>
|
||
{#if uploading}
|
||
<span class="a-tile-ring" title={uploadName}><i></i></span>
|
||
{:else}
|
||
<Icon
|
||
name={kind === 'video'
|
||
? 'film'
|
||
: kind === 'audio'
|
||
? 'music'
|
||
: 'plus'}
|
||
size={18}
|
||
/>
|
||
{#if caption}<span class="a-slot-caption"
|
||
>{label}{hint ? ` · ${hint}` : ''}</span
|
||
>{/if}
|
||
{/if}
|
||
</button>
|
||
{/if}
|
||
</div>
|
||
{#if error?.trim()}<span class="a-slot-hint err" role="alert">{error}</span
|
||
>{/if}
|
||
</div>
|
||
|
||
<style>
|
||
.sr-file {
|
||
position: absolute;
|
||
width: 1px;
|
||
height: 1px;
|
||
overflow: hidden;
|
||
clip: rect(0 0 0 0);
|
||
white-space: nowrap;
|
||
border: 0;
|
||
}
|
||
</style>
|