156 lines
3.8 KiB
Svelte
156 lines
3.8 KiB
Svelte
<script lang="ts">
|
|
import { tick } from 'svelte';
|
|
import ComfyDynamicForm from '$lib/components/ComfyDynamicForm.svelte';
|
|
import type { ComfyDynamicInput } from '$lib/comfy/types';
|
|
import { validateForm, classifySchema, type FormValues } from './schema';
|
|
let {
|
|
inputs,
|
|
values,
|
|
anchor,
|
|
onClose,
|
|
onApply,
|
|
}: {
|
|
inputs: ComfyDynamicInput[];
|
|
values: FormValues;
|
|
anchor: HTMLElement | null;
|
|
onClose: () => void;
|
|
onApply: (values: FormValues) => void;
|
|
} = $props();
|
|
let draft = $state<FormValues>({});
|
|
let showErrors = $state(false);
|
|
let panel = $state<HTMLDivElement | null>(null);
|
|
let x = $state(0),
|
|
y = $state(0);
|
|
const errors = $derived(
|
|
validateForm(inputs, classifySchema(inputs, 'image'), draft),
|
|
);
|
|
$effect(() => {
|
|
if (!anchor || !inputs.length) return;
|
|
const source = anchor;
|
|
draft = Object.fromEntries(
|
|
inputs.map((i) => [i.nodeId, values[i.nodeId] ?? i.defaultValue ?? '']),
|
|
);
|
|
showErrors = false;
|
|
const rect = source.getBoundingClientRect();
|
|
x = Math.max(12, Math.min(rect.left, innerWidth - 312));
|
|
y = Math.max(12, innerHeight - rect.top + 8);
|
|
void tick().then(() =>
|
|
panel
|
|
?.querySelector<HTMLElement>('input,select,textarea,button')
|
|
?.focus(),
|
|
);
|
|
const closeOutside = (e: PointerEvent) => {
|
|
if (
|
|
!panel?.contains(e.target as Node) &&
|
|
!source.contains(e.target as Node)
|
|
)
|
|
onClose();
|
|
};
|
|
const keys = (e: KeyboardEvent) => {
|
|
if (e.key === 'Escape') {
|
|
e.preventDefault();
|
|
onClose();
|
|
}
|
|
if (e.key === 'Tab' && panel) {
|
|
const all = [
|
|
...panel.querySelectorAll<HTMLElement>(
|
|
'input,select,textarea,button',
|
|
),
|
|
].filter(
|
|
(e) => !e.hasAttribute('disabled') && e.getClientRects().length,
|
|
);
|
|
if (e.shiftKey && document.activeElement === all[0]) {
|
|
e.preventDefault();
|
|
all.at(-1)?.focus();
|
|
} else if (!e.shiftKey && document.activeElement === all.at(-1)) {
|
|
e.preventDefault();
|
|
all[0]?.focus();
|
|
}
|
|
}
|
|
};
|
|
document.addEventListener('pointerdown', closeOutside);
|
|
document.addEventListener('keydown', keys, true);
|
|
window.addEventListener('resize', onClose);
|
|
return () => {
|
|
document.removeEventListener('pointerdown', closeOutside);
|
|
document.removeEventListener('keydown', keys, true);
|
|
window.removeEventListener('resize', onClose);
|
|
if (source.isConnected) source.focus();
|
|
};
|
|
});
|
|
function save() {
|
|
if (Object.keys(errors).length) {
|
|
showErrors = true;
|
|
return;
|
|
}
|
|
onApply(draft);
|
|
onClose();
|
|
}
|
|
</script>
|
|
|
|
{#if inputs.length && anchor}
|
|
<div
|
|
bind:this={panel}
|
|
class="parameter-popover"
|
|
role="dialog"
|
|
aria-label={inputs.map((i) => i.label).join(' / ')}
|
|
style:left={`${x}px`}
|
|
style:bottom={`${y}px`}
|
|
>
|
|
<header>{inputs.map((i) => i.label).join(' / ')}</header>
|
|
<ComfyDynamicForm
|
|
{inputs}
|
|
values={draft as Record<string, string | number>}
|
|
quiet
|
|
{errors}
|
|
{showErrors}
|
|
onChange={(v) => (draft = { ...draft, ...v })}
|
|
/>
|
|
<footer>
|
|
<button class="a-btn ghost sm" onclick={onClose}>取消</button><button
|
|
class="a-btn secondary sm"
|
|
onclick={save}>应用参数</button
|
|
>
|
|
</footer>
|
|
</div>
|
|
{/if}
|
|
|
|
<style>
|
|
.parameter-popover {
|
|
position: fixed;
|
|
z-index: 800;
|
|
width: 300px;
|
|
box-sizing: border-box;
|
|
max-height: calc(100dvh - 36px);
|
|
overflow: auto;
|
|
padding: 16px;
|
|
background: var(--float-bg);
|
|
border: 1px solid var(--line-2);
|
|
border-radius: 12px;
|
|
box-shadow: var(--shadow-float);
|
|
transform-origin: bottom left;
|
|
animation: appear var(--d-pop) var(--ease-out);
|
|
}
|
|
header {
|
|
font-size: 12px;
|
|
color: var(--text-2);
|
|
margin-bottom: 14px;
|
|
}
|
|
footer {
|
|
display: flex;
|
|
gap: 8px;
|
|
justify-content: flex-end;
|
|
margin-top: 12px;
|
|
}
|
|
@keyframes appear {
|
|
from {
|
|
opacity: 0;
|
|
transform: translateY(4px) scale(0.98);
|
|
}
|
|
to {
|
|
opacity: 1;
|
|
transform: none;
|
|
}
|
|
}
|
|
</style>
|