Files
aigc-home/calliope-web/src/lib/components/generate/Composer.svelte
T
2026-10-24 21:05:08 +08:00

764 lines
18 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 { resolve } from '$app/paths';
import Modal from '$lib/components/ui/Modal.svelte';
import { tick } from 'svelte';
import ParameterPopover from './ParameterPopover.svelte';
import Icon from '$lib/components/ui/Icon.svelte';
import type { ComfyDynamicInput, Workflow } from '$lib/comfy/types';
import { t } from '$lib/i18n.svelte';
import ComfyDynamicForm from '$lib/components/ComfyDynamicForm.svelte';
import type { Snippet } from 'svelte';
import SlotTray from './SlotTray.svelte';
import {
type BlockReason,
type DeskSchema,
type FormValue,
type FormValues,
} from './schema';
/** Scrollable creative inputs with a separate, always visible submission footer. */
interface Props {
header?: Snippet;
dock?: boolean;
ds: DeskSchema;
values: FormValues;
current: Workflow | null;
endpoints: Array<{ id: string; name: string }>;
endpointId: string;
conn: { state: 'ok' | 'fail' | 'untested' | 'dry'; ms?: number | null };
errors: Record<string, string>;
showErrors: boolean;
block: BlockReason | null;
changedCount: number;
submitting: boolean;
/** Shows 已提交 on the button for --d-flash. */
sent: boolean;
slotBusy?: boolean;
promptEl?: HTMLTextAreaElement | null;
onSet: (nodeId: string, value: FormValue) => void;
onOpenBay: () => void;
onPickEndpoint: (id: string) => void;
onSubmit: () => void;
onReveal: () => void;
onOpenParams: () => void;
onRestoreAll: () => void;
onFromResults: (nodeId: string) => void;
}
let {
header,
dock = false,
ds,
values,
current,
endpoints,
endpointId,
conn,
errors,
showErrors,
block,
changedCount,
submitting,
sent,
slotBusy = $bindable(false),
promptEl = $bindable(null),
onSet,
onPickEndpoint,
onSubmit,
onReveal,
onOpenParams,
onRestoreAll,
onFromResults,
}: Props = $props();
let mediaOpen = $state(false);
let inlineBusy = $state(false);
let editorBusy = $state(false);
$effect(() => {
slotBusy = inlineBusy || editorBusy;
});
let expanded = $state(false);
let popoverInputs = $state<ComfyDynamicInput[]>([]);
let popoverAnchor = $state<HTMLElement | null>(null);
export function openMedia() {
mediaOpen = true;
}
function openParameter(inp: ComfyDynamicInput, anchor: HTMLElement) {
popoverInputs =
inp === ds.width || inp === ds.height
? [ds.width, ds.height].filter((i): i is ComfyDynamicInput => !!i)
: inp === ds.ratio
? [ds.ratio, ds.megapixels].filter((i): i is ComfyDynamicInput => !!i)
: [inp];
popoverAnchor = anchor;
}
let tray = $state<ReturnType<typeof SlotTray> | null>(null);
let root = $state<HTMLElement | null>(null);
let dragDepth = $state(0);
const hasSlots = $derived(ds.slots.length + ds.lists.length > 0);
const commonInputs = $derived(
ds.megapixels && !ds.common.some((i) => i.nodeId === ds.megapixels?.nodeId)
? [...ds.common, ds.megapixels]
: ds.common,
);
const advancedInputs = $derived(
ds.advanced.filter((i) => !commonInputs.some((c) => c.nodeId === i.nodeId)),
);
const promptVal = $derived(
ds.prompt ? String(values[ds.prompt.nodeId] ?? '') : '',
);
const promptErr = $derived(
showErrors && ds.prompt ? errors[ds.prompt.nodeId] : undefined,
);
const endpointName = $derived(
endpoints.find((e) => e.id === endpointId)?.name ?? t('desk.noConn'),
);
function patch(
inputs: ComfyDynamicInput[],
next: Record<string, string | number>,
) {
for (const inp of inputs) {
const v = next[inp.nodeId];
if (v !== undefined && v !== values[inp.nodeId]) onSet(inp.nodeId, v);
}
}
async function onDrop(e: DragEvent) {
e.preventDefault();
dragDepth = 0;
const files = Array.from(e.dataTransfer?.files ?? []);
if (files.length) {
if (dock) {
mediaOpen = true;
await tick();
}
tray?.dropFiles(files);
}
}
function hasFiles(e: DragEvent): boolean {
return Array.from(e.dataTransfer?.types ?? []).includes('Files');
}
</script>
<div
class="composer-wrap"
class:dock
class:expanded
class:paired-frames={ds.lists.length === 0 &&
ds.slots.length > 0 &&
ds.slots.every((s) => s.firstFrame || s.lastFrame)}
>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
bind:this={root}
class="composer"
class:drag={dragDepth > 0}
ondragenter={(e) => {
if (hasFiles(e)) {
e.preventDefault();
dragDepth += 1;
}
}}
ondragleave={() => (dragDepth = Math.max(0, dragDepth - 1))}
ondragover={(e) => hasFiles(e) && e.preventDefault()}
ondrop={onDrop}
>
<div class="composer-scroll" aria-label="创作输入">
{@render header?.()}
{#if current}
<section
class="input-section prompt-section"
class:has-slots={dock && hasSlots}
>
<h3>2 · 准备这次创作</h3>
{#if hasSlots && !dock}<div class="slots">
<SlotTray
allowScene={current?.kind === 'video'}
bind:this={tray}
bind:busy={inlineBusy}
{ds}
{values}
{showErrors}
{errors}
{onSet}
{onFromResults}
/>
</div>{/if}
{#if dock && hasSlots}<div class="media-summary">
<SlotTray
compact
allowScene={current?.kind === 'video'}
bind:busy={inlineBusy}
{ds}
{values}
{showErrors}
{errors}
{onSet}
{onFromResults}
/>
<button class="expand-prompt manage-media" onclick={openMedia}
>管理素材</button
>
</div>{/if}
<div class="prompt-pair" class:dual={dock && !!ds.negative}>
{#if ds.prompt}<div class="prompt-field">
<div class="prompt-label">
<label for="desk-prompt"
>{ds.negative ? '正向提示词' : '提示词'}
{#if ds.prompt.required}<span>必填</span>{/if}</label
>
<button
class="expand-prompt"
aria-expanded={expanded}
onclick={() => (expanded = !expanded)}
>{expanded ? '收起提示词' : '展开提示词'}</button
>
</div>
<textarea
bind:this={promptEl}
id="desk-prompt"
class="prompt"
class:invalid={!!promptErr}
rows="4"
value={promptVal}
placeholder="描述你想要的画面、动作和镜头…"
aria-label={t('studio.promptLabel')}
aria-invalid={!!promptErr || undefined}
oninput={(e) =>
onSet(ds.prompt!.nodeId, e.currentTarget.value)}
></textarea>
<div class="prompt-counter">
<span>Enter 换行 · Ctrl + Enter 生成</span><span
>{promptVal.length} 字</span
>
</div>
{#if promptErr}<p class="error" role="alert">
{promptErr}
</p>{/if}
</div>{:else}<p class="note">
这个工具不需要填写提示词,请准备它需要的素材。
</p>{/if}
{#if dock && ds.negative}<div class="prompt-field negative">
<label for="desk-negative"
>反向提示词 {#if ds.negative.required}<span>必填</span
>{/if}</label
><textarea
id="desk-negative"
class="prompt"
rows="4"
value={String(values[ds.negative.nodeId] ?? '')}
placeholder="填写希望避免的内容…"
aria-label="反向提示词"
aria-invalid={(showErrors && !!errors[ds.negative.nodeId]) ||
undefined}
oninput={(e) =>
onSet(ds.negative!.nodeId, e.currentTarget.value)}
></textarea>
<div class="prompt-counter">
<span>避免的内容</span><span
>{String(values[ds.negative.nodeId] ?? '').length} 字</span
>
</div>
{#if showErrors && errors[ds.negative.nodeId]}<p
class="error"
role="alert"
>
{errors[ds.negative.nodeId]}
</p>{/if}
</div>{/if}
</div>
</section>
{#if !dock && commonInputs.length}<section class="input-section">
<h3>画面与生成设置</h3>
<ComfyDynamicForm
inputs={commonInputs}
values={values as Record<string, string | number>}
quiet
{errors}
{showErrors}
onChange={(v) => patch(commonInputs, v)}
/>
</section>{/if}
{#if !dock && changedCount > 0}<button
class="a-btn ghost sm"
onclick={onRestoreAll}>恢复工具默认参数</button
>{/if}
{#if !dock && advancedInputs.length}<details class="advanced">
<summary>更多参数 · {advancedInputs.length} 项</summary>
<ComfyDynamicForm
inputs={advancedInputs}
values={values as Record<string, string | number>}
quiet
{errors}
{showErrors}
onChange={(v) => patch(advancedInputs, v)}
/>
</details>{/if}
{/if}
{#if !dock}<details
class="runtime"
open={conn.state === 'fail' || !endpointId}
>
<summary id="desk-conn">运行设置 · {endpointName}</summary>
<label for="comfy-endpoint">使用哪台 ComfyUI</label>
<select
id="comfy-endpoint"
class="a-select"
value={endpointId}
onchange={(e) => onPickEndpoint(e.currentTarget.value)}
>
{#if !endpoints.length}<option value="">尚未配置服务</option>{/if}
{#each endpoints as ep (ep.id)}<option value={ep.id}
>{ep.name}</option
>{/each}
</select><a
href={resolve('/connections')}
target="_blank"
rel="noreferrer">配置或检查连接 ↗</a
>
</details>{/if}
</div>
<footer class="submit">
{#if dock}<div class="dock-chips">
<button id="desk-conn" class="parameter" onclick={onOpenParams}
>✦ {endpointName}⌄</button
>{#each commonInputs
.filter((i) => i !== ds.height)
.slice(0, 3) as inp (inp.nodeId)}<button
class="parameter"
onclick={(e) => openParameter(inp, e.currentTarget)}
>{inp.label}: {String(
inp === ds.width && ds.height
? String(values[inp.nodeId]) +
' × ' +
String(values[ds.height.nodeId])
: (values[inp.nodeId] ?? inp.defaultValue ?? '—'),
)}⌄</button
>{/each}<button class="parameter" onclick={onOpenParams}
>创作设置⌄</button
>
</div>{/if}
{#if block}<button
type="button"
class="why"
class:err={block.tone === 'err'}
onclick={onReveal}>{block.text}</button
>
{:else}<p class="ready">素材已准备好,可以开始生成。</p>{/if}
<button
type="button"
class="a-btn primary gen"
disabled={!!block || submitting || slotBusy}
onclick={onSubmit}
>
<Icon name="sparkle" size={14} />{submitting
? '正在提交…'
: slotBusy
? '正在上传素材…'
: sent
? '已加入生成队列'
: '开始生成'}
</button>
</footer>
</div>
</div>
{#if dock}
<Modal
bind:open={mediaOpen}
size="lg"
title="本次创作素材"
dismissible={!slotBusy}
>
<div class="media-editor">
<SlotTray
allowScene={current?.kind === 'video'}
bind:this={tray}
bind:busy={editorBusy}
{ds}
{values}
{showErrors}
{errors}
{onSet}
{onFromResults}
/>
</div>
{#snippet footer()}<button
class="a-btn secondary"
disabled={slotBusy}
onclick={() => (mediaOpen = false)}
>{slotBusy ? '正在上传…' : '完成'}</button
>{/snippet}
</Modal>
{/if}
<ParameterPopover
inputs={popoverInputs}
{values}
anchor={popoverAnchor}
onClose={() => {
popoverInputs = [];
popoverAnchor = null;
}}
onApply={(v) => {
for (const [id, value] of Object.entries(v)) onSet(id, value);
}}
/>
<style>
.composer-wrap,
.composer {
height: 100%;
min-height: 0;
display: flex;
flex-direction: column;
}
.composer-scroll {
flex: 1;
min-height: 0;
overflow: auto;
padding: 18px;
overscroll-behavior: contain;
scrollbar-gutter: stable;
}
.composer.drag {
outline: 2px dashed var(--accent);
outline-offset: -4px;
}
.input-section {
padding: 18px 0;
border-top: 1px solid var(--line);
}
h3 {
font-size: 13px;
margin: 0 0 14px;
font-weight: 650;
}
label {
display: block;
font-size: 12px;
margin-bottom: 8px;
color: var(--text-2);
}
label span {
font-size: 10px;
color: var(--text-3);
margin-left: 6px;
}
.prompt {
display: block;
width: 100%;
min-height: 100px;
max-height: 260px;
resize: vertical;
box-sizing: border-box;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--surface);
padding: 12px;
font: inherit;
font-size: 13px;
line-height: 1.6;
color: var(--text);
}
.prompt:focus {
outline: 2px solid var(--accent);
outline-offset: 1px;
}
.slots {
margin-bottom: 16px;
}
.slots :global(.tray) {
padding: 0;
gap: 14px;
}
.slots :global(.note) {
padding: 8px 0 0;
}
.advanced,
.runtime {
border-top: 1px solid var(--line);
padding: 14px 0;
}
summary {
font-size: 12px;
cursor: pointer;
padding: 4px 0;
margin-bottom: 8px;
line-height: 1.5;
overflow-wrap: anywhere;
}
.runtime .a-select {
width: 100%;
margin-bottom: 10px;
}
.runtime a {
font-size: 12px;
}
.submit {
flex-shrink: 0;
padding: 12px 18px 16px;
border-top: 1px solid var(--line);
background: var(--surface);
display: grid;
gap: 10px;
box-shadow: var(--sh-1);
}
.gen {
width: 100%;
min-height: 44px;
justify-content: center;
gap: 8px;
font-size: 14px;
}
.why {
padding: 0;
border: 0;
background: none;
text-align: left;
font: inherit;
font-size: 12px;
line-height: 1.5;
color: var(--warn);
cursor: pointer;
overflow-wrap: anywhere;
}
.why.err,
.error {
color: var(--err);
}
.ready,
.note,
.error {
margin: 0;
font-size: 12px;
line-height: 1.5;
color: var(--text-3);
}
.error {
color: var(--err);
}
.dock {
height: auto;
min-height: 0;
}
.dock .composer {
height: auto;
min-height: 0;
border: 1px solid var(--line-2);
border-radius: var(--desk-dock-radius);
background: var(--composer-bg);
overflow: hidden;
box-shadow: var(--desk-dock-shadow);
}
.dock .composer:focus-within {
border-color: color-mix(in srgb, var(--gold) 45%, transparent);
box-shadow: var(--desk-dock-focus);
}
.dock .composer-scroll {
scrollbar-gutter: auto;
padding: 0;
overflow: auto;
display: flex;
flex-direction: column;
}
.dock .input-section {
padding: 0;
border: 0;
margin: 0;
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
}
.dock .input-section > h3 {
display: none;
}
.media-summary {
position: relative;
display: flex;
align-items: center;
gap: 6px;
min-height: 0;
margin: 0;
flex-shrink: 0;
overflow: auto;
}
.paired-frames {
--desk-slot-column: var(--desk-first-last-column);
}
.dock .input-section.has-slots {
display: grid;
grid-template-columns: minmax(0, var(--desk-slot-column)) minmax(0, 1fr);
gap: 0;
}
.has-slots .media-summary {
align-self: stretch;
flex-direction: column;
align-items: stretch;
margin: 0;
padding: 10px 12px 8px;
border-right: 1px solid var(--line);
}
.manage-media {
position: absolute;
bottom: 6px;
right: 12px;
}
.expand-prompt {
margin-left: auto;
flex-shrink: 0;
border: 0;
background: none;
color: var(--tx-2);
font-size: 11px;
cursor: pointer;
}
.dock .prompt-pair {
display: grid;
grid-template-columns: minmax(0, 1fr);
min-height: 0;
flex: 1;
gap: 0;
}
.dock .prompt-pair.dual {
grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
}
.dock .prompt-field {
padding: 10px 14px 6px;
display: flex;
flex-direction: column;
min-height: 0;
min-width: 0;
}
.dock .negative {
border-left: 1px solid var(--line);
}
.dock .prompt-field label {
font-size: 11.5px;
height: 20px;
color: var(--text-3);
margin: 0;
display: flex;
gap: 6px;
}
.prompt-label {
display: flex;
align-items: center;
justify-content: space-between;
height: 20px;
}
.dock .prompt-field:first-child label {
color: var(--tx-2);
}
.dock .prompt {
background: none;
border: 0;
border-radius: 0;
padding: 2px 0;
font-size: var(--fs-body);
line-height: 1.6;
resize: none;
min-height: 0;
max-height: none;
height: var(--prompt-height);
flex: none;
overflow: auto;
}
.dock.expanded .prompt {
height: var(--prompt-expanded);
}
.dock .prompt:focus {
outline: 0;
}
.prompt-counter {
display: flex;
justify-content: space-between;
font-size: 9px;
color: var(--text-3);
margin-top: 3px;
}
.dock .prompt-counter {
height: 22px;
align-items: center;
font-size: 11px;
margin-top: 4px;
}
.dock .submit {
display: flex;
flex-wrap: nowrap;
align-items: center;
gap: 8px;
padding: 8px 10px 10px 12px;
height: 57px;
box-sizing: border-box;
background: none;
border-top: 1px solid var(--line);
position: relative;
}
.dock-chips {
display: flex;
gap: var(--desk-slot-gap);
align-items: center;
flex: 1;
min-width: 0;
overflow: auto;
}
.parameter {
background: var(--desk-chip-bg);
border: 1px solid var(--line);
border-radius: var(--desk-chip-radius);
height: var(--desk-chip-height);
padding: var(--desk-chip-padding);
font-size: var(--desk-chip-size);
color: var(--tx-2);
cursor: pointer;
white-space: nowrap;
max-width: 180px;
overflow: hidden;
text-overflow: ellipsis;
flex-shrink: 0;
}
.parameter:hover {
border-color: var(--line-3);
}
.dock .gen {
width: var(--desk-gen-width);
height: var(--desk-gen-height);
min-height: var(--desk-gen-height);
font-size: 14px;
font-weight: var(--desk-gen-weight);
padding: 0 16px;
border: 0;
border-radius: var(--desk-gen-radius);
margin-left: auto;
flex-shrink: 0;
}
.dock .why {
position: absolute;
bottom: calc(100% + 3px);
right: 14px;
max-width: 65%;
padding: 3px 8px;
border-radius: 5px;
background: var(--bg-2);
font-size: 10px;
color: var(--warn);
z-index: 1;
}
.dock .why.err {
color: var(--err);
background: var(--bg-2);
}
.dock .ready {
display: none;
}
.dock .error {
font-size: 10px;
}
</style>