Files
aigc-home/calliope-web/src/lib/components/generate/SlotTray.svelte
T

229 lines
7.1 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts">
import MediaSlot from '$lib/components/studio/MediaSlot.svelte';
import RefImageGrid from '$lib/components/studio/RefImageGrid.svelte';
import { mediaFileProblem } from '$lib/comfy/imageFile';
import { createUploadManager } from '$lib/comfy/useUpload.svelte';
import type { ComfyDynamicInput } from '$lib/comfy/types';
import { toast } from '$lib/toast';
import { t } from '$lib/i18n.svelte';
import { h3Warnings, listKind, listPaths, type DeskSchema, type FormValue, type FormValues, type SlotDef } from './schema';
/**
* 输入槽 (spec-v2 §3.3): exactly the media inputs the workflow JSON wires —
* text-to-image shows none, image-to-image one input, image-to-video a
* first frame, ref-to-video the 1–N list. Owns uploads and rejects;
* values flow back through `onSet`.
*/
interface Props {
ds: DeskSchema;
values: FormValues;
showErrors: boolean;
errors: Record<string, string>;
/** True while any upload runs (generate waits for it). */
busy?: boolean;
onSet: (nodeId: string, value: FormValue) => void;
onFromResults: (nodeId: string) => void;
}
let { ds, values, showErrors, errors, busy = $bindable(false), onSet, onFromResults }: Props = $props();
const uploads = createUploadManager();
type Reject = { name: string; message: string };
let rejects = $state<Record<string, Reject[]>>({});
let fileSizes = $state<Record<string, number>>({});
let replacingKey = $state('');
let replacingIndex = $state(-1);
$effect(() => {
busy = Object.keys(uploads.uploading).length > 0;
});
// A different workflow starts with no stale rejects.
$effect(() => {
void ds;
rejects = {};
});
const warns = $derived(h3Warnings(ds, values));
function setRejects(nodeId: string, list: Reject[]) {
rejects = { ...rejects, [nodeId]: list };
}
async function upload(nodeId: string, file: File): Promise<string | null> {
const path = await uploads.uploadSafe(nodeId, file);
if (path) fileSizes = { ...fileSizes, [path]: file.size };
return path;
}
async function slotFile(s: SlotDef, file: File) {
const problem = mediaFileProblem(s.mediaKind, file);
if (problem) {
setRejects(s.inp.nodeId, [{ name: file.name, message: problem }]);
return;
}
setRejects(s.inp.nodeId, []);
const path = await upload(s.inp.nodeId, file);
if (path) onSet(s.inp.nodeId, path);
}
/** Add picked/dropped files in order; a batch that would pass `max` is refused whole. */
async function groupAdd(entry: ComfyDynamicInput, files: File[]) {
const key = entry.nodeId;
const mk = listKind(entry);
const max = entry.max ?? 9;
const ok = files.filter((f) => !mediaFileProblem(mk, f));
setRejects(
key,
files.filter((f) => mediaFileProblem(mk, f)).map((f) => ({ name: f.name, message: mediaFileProblem(mk, f)! })),
);
const start = listPaths(values, entry);
if (start.length + ok.length > max) {
toast.error(t('studio.errRefMax', { max, n: start.length, k: ok.length }));
return;
}
for (const f of ok) {
const path = await upload(key, f);
if (!path) break;
onSet(key, [...listPaths(values, entry), path]);
}
}
async function groupReplace(entry: ComfyDynamicInput, index: number, file: File) {
const key = entry.nodeId;
const problem = mediaFileProblem(listKind(entry), file);
if (problem) {
setRejects(key, [{ name: file.name, message: problem }]);
return;
}
setRejects(key, []);
replacingKey = key;
replacingIndex = index;
try {
const path = await upload(key, file);
if (path) onSet(key, listPaths(values, entry).map((p, i) => (i === index ? path : p)));
} finally {
replacingKey = '';
replacingIndex = -1;
}
}
function fileKind(f: File): 'image' | 'video' | 'audio' {
if (f.type.startsWith('video/')) return 'video';
if (f.type.startsWith('audio/')) return 'audio';
return 'image';
}
/** Composer-wide drop: each file goes to the first wired slot of its kind with room. */
export function dropFiles(files: File[]) {
const pending = new Map<string, File[]>();
const taken = new Set<string>();
let unplaced = 0;
for (const f of files) {
const mk = fileKind(f);
const slot = ds.slots.find(
(s) =>
s.mediaKind === mk &&
!s.mask &&
!taken.has(s.inp.nodeId) &&
!String(values[s.inp.nodeId] ?? ''),
);
if (slot) {
taken.add(slot.inp.nodeId);
void slotFile(slot, f);
continue;
}
const list = ds.lists.find((g) => listKind(g) === mk);
if (list) {
pending.set(list.nodeId, [...(pending.get(list.nodeId) ?? []), f]);
continue;
}
unplaced += 1;
}
for (const [nodeId, batch] of pending) {
const entry = ds.lists.find((g) => g.nodeId === nodeId);
if (entry) void groupAdd(entry, batch);
}
if (unplaced) toast.error(t('desk.dropNoSlot', { n: unplaced }));
}
</script>
{#if ds.slots.length || ds.lists.length}
<div class="tray">
{#each ds.slots as s (s.inp.nodeId)}
{@const reject = rejects[s.inp.nodeId]?.[0]}
<MediaSlot
label={s.label}
hint={s.lastFrame && !s.inp.required ? t('studio.slotOptional') : undefined}
required={s.inp.required}
value={String(values[s.inp.nodeId] ?? '')}
kind={s.mediaKind}
size={fileSizes[String(values[s.inp.nodeId] ?? '')]}
uploading={uploads.isUploading(s.inp.nodeId)}
uploadName={uploads.uploading[s.inp.nodeId]}
need={showErrors && !!errors[s.inp.nodeId]}
error={reject?.message ?? (showErrors ? errors[s.inp.nodeId] : undefined)}
allowFromResults={s.fromResults}
anchorId={`slot-${s.inp.nodeId}`}
onFile={(f) => slotFile(s, f)}
onFromResults={() => onFromResults(s.inp.nodeId)}
onClear={() => {
setRejects(s.inp.nodeId, []);
onSet(s.inp.nodeId, '');
}}
/>
{/each}
{#each ds.lists as entry (entry.nodeId)}
<RefImageGrid
label={entry.label}
mediaKind={listKind(entry)}
paths={listPaths(values, entry)}
max={entry.max ?? 9}
sizes={fileSizes}
uploadingName={uploads.uploading[entry.nodeId]}
uploadingIndex={replacingKey === entry.nodeId ? replacingIndex : -1}
rejects={rejects[entry.nodeId] ?? []}
need={showErrors && !!errors[entry.nodeId]}
error={showErrors ? errors[entry.nodeId] : undefined}
allowFromResults={entry.kind === 'image_list'}
anchorId={`slot-${entry.nodeId}`}
onAdd={(files) => groupAdd(entry, files)}
onReplace={(i, file) => groupReplace(entry, i, file)}
onRemove={(i) => onSet(entry.nodeId, listPaths(values, entry).filter((_, j) => j !== i))}
onDismissReject={(i) => setRejects(entry.nodeId, (rejects[entry.nodeId] ?? []).filter((_, j) => j !== i))}
onFromResults={entry.kind === 'image_list' ? () => onFromResults(entry.nodeId) : undefined}
/>
{/each}
</div>
{#if showErrors && errors.firstLast}
<p class="a-slot-hint err note">{errors.firstLast}</p>
{/if}
{#if ds.hasH3Hint}
<div class="note" role="note">
<p class="a-slot-hint">{t('wfMedia.h3Limits')}</p>
{#each warns as w (w)}
<p class="a-slot-hint warn">{w}</p>
{/each}
</div>
{/if}
{/if}
<style>
.tray {
display: flex;
flex-wrap: wrap;
gap: var(--sp-3) var(--sp-6);
padding: var(--sp-4) var(--sp-5) 0;
}
.note {
margin: 0;
padding: var(--sp-2) var(--sp-5) 0;
}
.note p {
margin: 0;
}
.warn {
color: var(--warn);
}
</style>