695 lines
17 KiB
Svelte
695 lines
17 KiB
Svelte
<script lang="ts">
|
||
import { resolve } from '$app/paths';
|
||
import { workflowFamily } from './workflowFamily';
|
||
import { isWorkflowRunnable } from '$lib/workflowState';
|
||
import { onMount, untrack } from 'svelte';
|
||
import { goto } from '$app/navigation';
|
||
import { createMutation, useQueryClient } from '@tanstack/svelte-query';
|
||
import { workflows, apiErrorMessage } from '$lib/api';
|
||
import type { WorkflowMeta } from '$lib/comfy/types';
|
||
import { usageLabel } from '$lib/components/generate/schema';
|
||
import { spotlight } from '$lib/actions/spotlight';
|
||
import { toast } from '$lib/toast';
|
||
import Icon from '$lib/components/ui/Icon.svelte';
|
||
let {
|
||
kind,
|
||
initialUsage = 'all',
|
||
metas,
|
||
selectedId,
|
||
pendingCount,
|
||
onPick,
|
||
onKind,
|
||
onDetails,
|
||
disabled = false,
|
||
historyCollapsed = false,
|
||
onToggleHistory,
|
||
}: {
|
||
disabled?: boolean;
|
||
historyCollapsed?: boolean;
|
||
onToggleHistory: () => void;
|
||
kind: 'image' | 'video';
|
||
initialUsage?: string;
|
||
metas: WorkflowMeta[];
|
||
selectedId: number | null;
|
||
pendingCount: number;
|
||
onPick: (id: number) => void;
|
||
onKind: (kind: 'image' | 'video') => void;
|
||
onDetails: () => void;
|
||
} = $props();
|
||
const closedGroups = $state<Record<string, boolean>>({});
|
||
function groupKey(m: WorkflowMeta) {
|
||
return pins.includes(m.id) ? 'pinned' : (m.model_family ?? 'local');
|
||
}
|
||
let searchInput: HTMLInputElement;
|
||
let searchOrigin: HTMLElement | null = null;
|
||
function searchKeydown(e: KeyboardEvent) {
|
||
if (e.defaultPrevented || e.isComposing || e.keyCode === 229) return;
|
||
if (
|
||
[
|
||
...document.querySelectorAll('.shell-pop, .modal, [role="dialog"]'),
|
||
].some((el) => el.getClientRects().length)
|
||
)
|
||
return;
|
||
if (e.key === 'Escape' && e.target === searchInput) {
|
||
e.preventDefault();
|
||
const origin = searchOrigin?.isConnected
|
||
? searchOrigin
|
||
: searchInput.closest('aside')?.querySelector('button');
|
||
if (origin instanceof HTMLElement) origin.focus();
|
||
else searchInput.blur();
|
||
return;
|
||
}
|
||
const el = e.target instanceof HTMLElement ? e.target : null;
|
||
if (
|
||
e.key !== '/' ||
|
||
e.ctrlKey ||
|
||
e.metaKey ||
|
||
e.altKey ||
|
||
el?.isContentEditable ||
|
||
el?.closest(
|
||
'input, textarea, select, [contenteditable]:not([contenteditable="false"])',
|
||
) ||
|
||
!searchInput?.getClientRects().length
|
||
)
|
||
return;
|
||
e.preventDefault();
|
||
searchInput.focus();
|
||
}
|
||
const client = useQueryClient();
|
||
let search = $state(''),
|
||
usage = $state(untrack(() => initialUsage)),
|
||
family = $state('all'),
|
||
favOnly = $state(false),
|
||
runnableOnly = $state(true),
|
||
limit = $state(48),
|
||
pins = $state<number[]>([]);
|
||
onMount(() => {
|
||
try {
|
||
pins = JSON.parse(
|
||
localStorage.getItem('calliope.reference.workflowPins') || '[]',
|
||
);
|
||
} catch {
|
||
pins = [];
|
||
}
|
||
});
|
||
const families = $derived([
|
||
...new Set(metas.map((m) => m.model_family).filter(Boolean)),
|
||
]);
|
||
const usages = $derived([
|
||
...new Set(metas.map((m) => m.usage).filter(Boolean)),
|
||
]);
|
||
const filtered = $derived(
|
||
metas
|
||
.filter(
|
||
(m) =>
|
||
(!runnableOnly || runnable(m)) &&
|
||
(!favOnly || m.favorite) &&
|
||
(usage === 'all' || m.usage === usage) &&
|
||
(family === 'all' || m.model_family === family) &&
|
||
[
|
||
m.name,
|
||
m.description,
|
||
m.source_path,
|
||
m.usage ? usageLabel(m.usage) : null,
|
||
m.model_family,
|
||
...(m.tags ?? []),
|
||
]
|
||
.join(' ')
|
||
.toLowerCase()
|
||
.includes(search.trim().toLowerCase()),
|
||
)
|
||
.sort(
|
||
(a, b) =>
|
||
Number(pins.includes(b.id)) - Number(pins.includes(a.id)) ||
|
||
(a.model_family ?? '本地工作流').localeCompare(
|
||
b.model_family ?? '本地工作流',
|
||
'zh',
|
||
) ||
|
||
Number(b.favorite) - Number(a.favorite) ||
|
||
a.name.localeCompare(b.name, 'zh'),
|
||
),
|
||
);
|
||
$effect(() => {
|
||
void search;
|
||
void usage;
|
||
void family;
|
||
void favOnly;
|
||
limit = 48;
|
||
});
|
||
$effect(() => {
|
||
const index = filtered.findIndex((m) => m.id === selectedId);
|
||
if (index >= limit) limit = index + 24;
|
||
});
|
||
function runnable(m: WorkflowMeta) {
|
||
return isWorkflowRunnable(m);
|
||
}
|
||
function choose(m: WorkflowMeta) {
|
||
if (runnable(m)) onPick(m.id);
|
||
else void goto(resolve(`/workflows?manage=1&wf=${m.id}`));
|
||
}
|
||
function pin(id: number) {
|
||
pins = pins.includes(id) ? pins.filter((p) => p !== id) : [...pins, id];
|
||
try {
|
||
localStorage.setItem(
|
||
'calliope.reference.workflowPins',
|
||
JSON.stringify(pins),
|
||
);
|
||
} catch {
|
||
/* Pins remain usable this visit. */
|
||
}
|
||
}
|
||
const favorite = createMutation({
|
||
mutationFn: (m: WorkflowMeta) =>
|
||
workflows.update(m.id, { favorite: !m.favorite }),
|
||
onSuccess: () => client.invalidateQueries({ queryKey: ['workflows-meta'] }),
|
||
onError: (e) => toast.error(apiErrorMessage(e)),
|
||
});
|
||
</script>
|
||
|
||
<svelte:window onkeydown={searchKeydown} />
|
||
|
||
<aside class="workflow-rail" aria-label="创作工作流列表">
|
||
<header>
|
||
<strong
|
||
><span class="desk-glyph"><Icon name="nav-create" size={18} /></span
|
||
>创作台</strong
|
||
><button
|
||
class="history-toggle a-btn ghost sm"
|
||
onclick={onToggleHistory}
|
||
aria-label={historyCollapsed ? '展开历史栏' : '收起历史栏'}
|
||
aria-expanded={!historyCollapsed}>◫</button
|
||
>
|
||
</header>
|
||
<div class="rail-tools">
|
||
<div class="kind-tabs" role="tablist" aria-label="创作类型">
|
||
<button
|
||
role="tab"
|
||
aria-selected={kind === 'image'}
|
||
{disabled}
|
||
onclick={() => onKind('image')}
|
||
><Icon name="project-image" size={14} />图片</button
|
||
>
|
||
<button
|
||
role="tab"
|
||
aria-selected={kind === 'video'}
|
||
{disabled}
|
||
onclick={() => onKind('video')}
|
||
><Icon name="project-film" size={14} />视频</button
|
||
>
|
||
</div>
|
||
<div class="rail-search">
|
||
<Icon name="search" size={14} />
|
||
<input
|
||
type="search"
|
||
bind:this={searchInput}
|
||
onfocus={(e) => {
|
||
searchOrigin =
|
||
e.relatedTarget instanceof HTMLElement ? e.relatedTarget : null;
|
||
}}
|
||
bind:value={search}
|
||
aria-label="搜索工作流"
|
||
placeholder="搜用途、模型或文件名"
|
||
/>
|
||
<kbd>/</kbd>
|
||
</div>
|
||
<div class="filters" role="group" aria-label="筛选工作流用途">
|
||
<button aria-pressed={usage === 'all'} onclick={() => (usage = 'all')}
|
||
>全部</button
|
||
>
|
||
{#each usages as u (u)}<button
|
||
aria-pressed={usage === u}
|
||
onclick={() => (usage = u!)}>{usageLabel(u!)}</button
|
||
>{/each}
|
||
<button aria-pressed={favOnly} onclick={() => (favOnly = !favOnly)}
|
||
>☆ 收藏</button
|
||
>
|
||
</div>
|
||
{#if families.length}<div
|
||
class="filters"
|
||
role="group"
|
||
aria-label="筛选模型家族"
|
||
>
|
||
<button aria-pressed={family === 'all'} onclick={() => (family = 'all')}
|
||
>全部模型</button
|
||
>
|
||
{#each families as f (f)}<button
|
||
aria-pressed={family === f}
|
||
onclick={() => (family = f!)}>{f}</button
|
||
>{/each}
|
||
</div>{/if}
|
||
<div class="list-meta">
|
||
<span title="目录接口不提供仓库文件总数"
|
||
>{filtered.length} 个 · 仓库文件数未知</span
|
||
>
|
||
<label class="runnable-filter"
|
||
><input type="checkbox" bind:checked={runnableOnly} />仅可运行</label
|
||
>
|
||
</div>
|
||
</div>
|
||
<div class="rail-list">
|
||
{#each filtered.slice(0, limit) as m, index (m.id)}
|
||
{#if index === 0 || groupKey(m) !== groupKey(filtered[index - 1])}<button
|
||
class="family-label"
|
||
aria-expanded={!closedGroups[groupKey(m)]}
|
||
onclick={() =>
|
||
(closedGroups[groupKey(m)] = !closedGroups[groupKey(m)])}
|
||
>
|
||
<Icon
|
||
name={closedGroups[groupKey(m)] ? 'chevron-right' : 'chevron-down'}
|
||
size={14}
|
||
/>
|
||
<Icon name={pins.includes(m.id) ? 'pin' : 'agent-cpu'} size={14} />
|
||
<span
|
||
>{pins.includes(m.id)
|
||
? '置顶'
|
||
: `本地 ComfyUI · ${m.model_family || '未标注模型'}`}</span
|
||
>
|
||
<small
|
||
>{filtered.filter((item) => groupKey(item) === groupKey(m))
|
||
.length}</small
|
||
>
|
||
</button>{/if}
|
||
{#if !closedGroups[groupKey(m)]}
|
||
<div
|
||
class="workflow-row reference-option"
|
||
use:spotlight
|
||
class:selected={selectedId === m.id}
|
||
>
|
||
<button
|
||
class="workflow-pick"
|
||
{disabled}
|
||
onclick={() => choose(m)}
|
||
aria-pressed={selectedId === m.id}
|
||
title={m.name}
|
||
><i style:background={workflowFamily(m.model_family).color}
|
||
>{workflowFamily(m.model_family).label}</i
|
||
><span
|
||
><b>{m.name}</b>{#if !runnable(m)}<small class="pending"
|
||
>待配置 · 映射 / 测试 →</small
|
||
>{/if}<small
|
||
>{m.description ||
|
||
(m.usage ? usageLabel(m.usage) : '本地 ComfyUI')}</small
|
||
></span
|
||
></button
|
||
>
|
||
<div class="row-actions">
|
||
<button
|
||
class:pressed={pins.includes(m.id)}
|
||
aria-pressed={pins.includes(m.id)}
|
||
aria-label={(pins.includes(m.id) ? '取消置顶' : '置顶') + m.name}
|
||
onclick={() => pin(m.id)}><Icon name="pin" size={14} /></button
|
||
><button
|
||
aria-pressed={!!m.favorite}
|
||
aria-label={(m.favorite ? '取消收藏' : '收藏') + m.name}
|
||
disabled={$favorite.isPending}
|
||
onclick={() => $favorite.mutate(m)}
|
||
>{m.favorite ? '★' : '☆'}</button
|
||
>
|
||
</div>
|
||
</div>{/if}{/each}
|
||
{#if !filtered.length}<p class="empty">
|
||
没有符合条件的工作流。可以清空搜索或更改筛选。
|
||
</p>{/if}
|
||
{#if filtered.length > limit}<button
|
||
class="more"
|
||
onclick={() => (limit += 48)}
|
||
>再显示 {Math.min(filtered.length - limit, 48)} 个</button
|
||
>{/if}
|
||
</div>
|
||
<footer>
|
||
{#if pendingCount}<a href={resolve('/workflows?manage=1')}
|
||
>{pendingCount} 个工作流待配置 →</a
|
||
>{/if}<button class="manage" data-workflow-details onclick={onDetails}
|
||
><span>+</span>
|
||
<div>工作流管理<small>输入要求 / 导入 / 分类</small></div>
|
||
<b>›</b></button
|
||
><a href={resolve('/models')}>云端接口 ↗</a>
|
||
</footer>
|
||
</aside>
|
||
|
||
<style>
|
||
.workflow-rail {
|
||
box-sizing: border-box;
|
||
width: var(--list);
|
||
flex-shrink: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
min-height: 0;
|
||
background: var(--bg-1);
|
||
border-right: 1px solid var(--line);
|
||
}
|
||
.workflow-rail > header {
|
||
padding: 12px 12px 0;
|
||
height: var(--desk-rail-title-height);
|
||
box-sizing: border-box;
|
||
flex-shrink: 0;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
}
|
||
header strong {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
font-size: 14px;
|
||
font-weight: 600;
|
||
}
|
||
.desk-glyph {
|
||
width: var(--desk-glyph-size);
|
||
height: var(--desk-glyph-size);
|
||
display: grid;
|
||
place-items: center;
|
||
border-radius: var(--desk-slot-radius);
|
||
color: var(--scene);
|
||
background: var(--desk-glyph-bg);
|
||
box-shadow: var(--desk-glyph-shadow);
|
||
}
|
||
.rail-tools {
|
||
padding: var(--desk-rail-gap) 12px;
|
||
border-bottom: 1px solid var(--line);
|
||
}
|
||
.kind-tabs {
|
||
display: flex;
|
||
gap: 2px;
|
||
padding: 3px;
|
||
height: 36px;
|
||
box-sizing: border-box;
|
||
border: 1px solid var(--line);
|
||
border-radius: var(--desk-seg-radius);
|
||
background: var(--well);
|
||
}
|
||
.kind-tabs button {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
gap: 6px;
|
||
flex: 1;
|
||
height: 28px;
|
||
padding: 0 12px;
|
||
font-size: 12.5px;
|
||
border: 0;
|
||
border-radius: var(--desk-seg-selected-radius);
|
||
background: transparent;
|
||
color: var(--tx-2);
|
||
cursor: pointer;
|
||
}
|
||
.kind-tabs [aria-selected='true'] {
|
||
background: var(--bg-3);
|
||
box-shadow: var(--sh-1);
|
||
color: var(--tx-1);
|
||
}
|
||
.kind-tabs button:disabled {
|
||
cursor: wait;
|
||
}
|
||
.rail-search {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
margin-top: var(--desk-rail-gap);
|
||
height: var(--desk-search-height);
|
||
box-sizing: border-box;
|
||
font-size: var(--desk-search-size);
|
||
padding: 0 10px;
|
||
background: var(--well);
|
||
border: 1px solid var(--line-2);
|
||
border-radius: var(--desk-search-radius);
|
||
color: var(--text);
|
||
}
|
||
.rail-search input {
|
||
min-width: 0;
|
||
flex: 1;
|
||
padding: 0;
|
||
border: 0;
|
||
background: none;
|
||
color: inherit;
|
||
font: inherit;
|
||
outline: none;
|
||
}
|
||
.rail-search kbd {
|
||
font: var(--desk-search-key-font);
|
||
padding: var(--desk-search-key-padding);
|
||
border-radius: var(--desk-search-key-radius);
|
||
border: 1px solid var(--line-2);
|
||
color: var(--tx-3);
|
||
background: var(--bg-1);
|
||
}
|
||
.rail-search:focus-within {
|
||
border-color: var(--scene);
|
||
}
|
||
.filters {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 6px;
|
||
margin-top: 8px;
|
||
}
|
||
.filters button {
|
||
height: var(--desk-filter-height);
|
||
padding: 0 9px;
|
||
font-size: var(--desk-filter-size);
|
||
border-radius: var(--desk-filter-radius);
|
||
border: 1px solid var(--line);
|
||
background: var(--bg-2);
|
||
color: var(--tx-2);
|
||
cursor: pointer;
|
||
}
|
||
.filters button[aria-pressed='true'] {
|
||
color: var(--tx-1);
|
||
border-color: color-mix(in srgb, var(--scene) 60%, transparent);
|
||
background: color-mix(in srgb, var(--scene) 13%, var(--bg-2));
|
||
}
|
||
.runnable-filter {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 6px;
|
||
white-space: nowrap;
|
||
cursor: pointer;
|
||
}
|
||
.runnable-filter input {
|
||
margin: 0;
|
||
appearance: none;
|
||
width: var(--desk-switch-width);
|
||
height: var(--desk-switch-height);
|
||
border: 0;
|
||
border-radius: var(--r-pill);
|
||
background: var(--bg-4);
|
||
position: relative;
|
||
cursor: pointer;
|
||
flex-shrink: 0;
|
||
}
|
||
.runnable-filter input::after {
|
||
content: '';
|
||
position: absolute;
|
||
left: 2px;
|
||
top: 2px;
|
||
width: var(--desk-switch-knob);
|
||
height: var(--desk-switch-knob);
|
||
border-radius: 50%;
|
||
background: var(--tx-2);
|
||
transition: transform var(--t-base);
|
||
}
|
||
.runnable-filter input:checked {
|
||
background: color-mix(in srgb, var(--scene) 55%, var(--bg-4));
|
||
}
|
||
.runnable-filter input:checked::after {
|
||
transform: translateX(11px);
|
||
background: var(--on-media);
|
||
}
|
||
.pending {
|
||
color: var(--warn);
|
||
}
|
||
.list-meta {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 6px;
|
||
padding: 8px 2px 0;
|
||
font-size: var(--desk-filter-size);
|
||
color: var(--tx-3);
|
||
}
|
||
.family-label {
|
||
width: 100%;
|
||
border: 0;
|
||
background: var(--bg-1);
|
||
gap: 6px;
|
||
text-align: left;
|
||
cursor: pointer;
|
||
font-size: 11px;
|
||
color: var(--tx-3);
|
||
padding: 0 6px;
|
||
height: var(--desk-group-height);
|
||
display: flex;
|
||
align-items: center;
|
||
margin-top: 8px;
|
||
}
|
||
.family-label span {
|
||
min-width: 0;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
.family-label small {
|
||
margin-left: auto;
|
||
font: 10.5px var(--mono);
|
||
}
|
||
.rail-list {
|
||
flex: 1;
|
||
min-height: 0;
|
||
overflow: auto;
|
||
padding: 4px 8px 12px;
|
||
scrollbar-gutter: auto;
|
||
}
|
||
.workflow-row {
|
||
border: 1px solid transparent;
|
||
position: relative;
|
||
display: flex;
|
||
align-items: center;
|
||
border-radius: var(--desk-row-radius);
|
||
margin: 2px 0;
|
||
}
|
||
.workflow-row.selected {
|
||
border-color: color-mix(in srgb, var(--scene) 35%, transparent);
|
||
background: linear-gradient(
|
||
90deg,
|
||
color-mix(in srgb, var(--scene) 12%, var(--bg-3)),
|
||
var(--bg-3)
|
||
);
|
||
}
|
||
.workflow-pick {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: var(--desk-row-gap);
|
||
padding: var(--desk-row-padding);
|
||
min-height: var(--desk-row-height);
|
||
width: 100%;
|
||
min-width: 0;
|
||
text-align: left;
|
||
border: 0;
|
||
background: none;
|
||
color: var(--text);
|
||
cursor: pointer;
|
||
}
|
||
.workflow-pick i {
|
||
display: grid;
|
||
place-items: center;
|
||
width: var(--desk-family-size);
|
||
height: var(--desk-family-size);
|
||
flex-shrink: 0;
|
||
border-radius: 9px;
|
||
border: 1px solid var(--line-2);
|
||
color: var(--desk-family-ink);
|
||
background: var(--scene);
|
||
font: var(--desk-family-font);
|
||
font-style: normal;
|
||
}
|
||
.workflow-pick > span {
|
||
min-width: 0;
|
||
flex: 1;
|
||
}
|
||
.workflow-pick b {
|
||
display: block;
|
||
font-size: var(--desk-row-name-size);
|
||
font-weight: var(--desk-row-name-weight);
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
margin-bottom: 1px;
|
||
}
|
||
.workflow-pick small {
|
||
display: block;
|
||
font-size: var(--desk-row-detail-size);
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
color: var(--text-3);
|
||
}
|
||
.row-actions {
|
||
display: flex;
|
||
gap: 2px;
|
||
padding-right: 4px;
|
||
flex-shrink: 0;
|
||
}
|
||
.row-actions button {
|
||
opacity: 0;
|
||
padding: 2px;
|
||
height: 24px;
|
||
width: 24px;
|
||
border: 0;
|
||
color: var(--text-3);
|
||
background: none;
|
||
font-size: 12px;
|
||
cursor: pointer;
|
||
}
|
||
.row-actions button[aria-pressed='true'] {
|
||
color: var(--warn);
|
||
opacity: 1;
|
||
}
|
||
.workflow-row:hover .row-actions button,
|
||
.workflow-row:focus-within .row-actions button {
|
||
opacity: 1;
|
||
}
|
||
.workflow-row:hover {
|
||
background-color: var(--bg-2);
|
||
}
|
||
.empty {
|
||
color: var(--text-3);
|
||
font-size: 11px;
|
||
padding: 12px;
|
||
}
|
||
.more {
|
||
width: 100%;
|
||
padding: 8px;
|
||
background: none;
|
||
border: 1px solid var(--line);
|
||
color: var(--text-3);
|
||
border-radius: 6px;
|
||
font-size: 11px;
|
||
cursor: pointer;
|
||
}
|
||
footer {
|
||
padding: 8px;
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 6px;
|
||
border-top: 1px solid var(--line);
|
||
}
|
||
footer a,
|
||
.manage {
|
||
font-size: 12px;
|
||
color: var(--tx-2);
|
||
text-decoration: none;
|
||
}
|
||
footer a,
|
||
.manage {
|
||
flex: 1;
|
||
white-space: nowrap;
|
||
border: 1px solid var(--line-2);
|
||
border-radius: var(--desk-action-radius);
|
||
background: var(--bg-3);
|
||
}
|
||
.manage {
|
||
text-align: left;
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 7px;
|
||
padding: 0 9px;
|
||
height: 26px;
|
||
cursor: pointer;
|
||
}
|
||
.manage small {
|
||
display: none;
|
||
}
|
||
.manage b {
|
||
margin-left: auto;
|
||
}
|
||
button:disabled {
|
||
opacity: 0.5;
|
||
cursor: wait;
|
||
}
|
||
@media (max-width: 1100px) {
|
||
.workflow-rail {
|
||
width: var(--list);
|
||
}
|
||
}
|
||
</style>
|