Files
aigc-home/calliope-web/src/lib/canvas/ArtifactNode.svelte
T
施工员2号andmultica-agent 433fd719f1 feat(HEL-884): locate canvas occupancy, accurate card handling, close the delete loop
- GET /api/library/media/usage: read-only occupancy report per file —
  every live canvas card with canvas title, project/session scope, and
  the real consequence of deleting that card (deletes_file / keep reason)
- _path_on_canvas joins canvas: orphan nodes (canvas row gone, FK-cascade
  dead metadata) no longer count as occupancy; they are reported as
  invalid references with totals instead of blocking deletion
- canvas deleteNode: keep_file=true release-only variant (card removed,
  file always preserved); artifact cards with a live pending/running job
  refuse deletion (409) so completion events cannot resurrect them
- library UI: occupancy modal on kept(on_canvas) with locate / delete
  card (accurate impact text) / release-only actions, per-path free-file
  delete closing the loop, invalid-reference note, offset paging for
  large counts, library selection persisted across canvas round-trips
- canvas UI: ?locate=<node> deep-link centers the card (computed
  viewport, no store.fitView — its nodes write-back looped the graph
  rebuild effect), gold ring highlight + dismiss, back-to-library chip
- i18n keys for all 7 languages; e2e runner + 32-check real-browser
  regression on isolated data

Co-authored-by: multica-agent <github@multica.ai>
2026-10-26 23:48:15 +08:00

129 lines
3.6 KiB
Svelte

<script lang="ts">
import { type Node, type NodeProps } from '@xyflow/svelte';
import { untrack } from 'svelte';
import { reduceMotion } from '$lib/reduceMotion.svelte';
import { finishAnimations } from '$lib/actions/finishAnimations';
import SafeMedia from '$lib/components/SafeMedia.svelte';
import { assetUrl } from '$lib/api';
import { t } from '$lib/i18n.svelte';
interface ArtifactNodeData extends Record<string, unknown> {
canvasNodeId: number;
jobId: number | null;
title: string;
/** Generated media file (image or video) — absolute path served via /assets. */
artifactPath: string | null;
kind: 'image' | 'video';
status: string;
/** HEL-884 locate deep-link: this card is the one being looked for. */
located?: boolean;
/** Called on media click: opens the large preview (no navigation). */
onOpenMedia?: (kind: 'image' | 'video') => void;
}
type ArtifactNode = Node<ArtifactNodeData, 'image' | 'video'>;
let { data, selected }: NodeProps<ArtifactNode> = $props();
const mediaSrc = $derived(
data.artifactPath ? assetUrl(data.artifactPath) : null,
);
// '#t=0.1' fragment paints the first frame (same trick as ClipMonitor).
const videoSrc = $derived(
data.kind === 'video' && mediaSrc ? mediaSrc + '#t=0.1' : null,
);
const running = $derived(
data.status === 'running' || data.status === 'pending',
);
let arriving = $state(false);
let previous: { id: number | null; status: string } | null = null;
$effect(() => {
void reduceMotion.mode;
void reduceMotion.loopRevision;
arriving = false;
});
$effect(() => {
const id = data.jobId,
status = data.status;
if (!id || previous?.id !== id || status !== 'done') arriving = false;
else if (previous.status === 'pending' || previous.status === 'running')
arriving = !untrack(() => reduceMotion.mode === 'off');
previous = { id, status };
});
$effect(() => {
if (!arriving) return;
const skip = () => (arriving = false);
window.addEventListener('pointerdown', skip, true);
return () => window.removeEventListener('pointerdown', skip, true);
});
function openPreview() {
if (!mediaSrc) return;
data.onOpenMedia?.(data.kind);
}
</script>
<div
class="canvas-card artifact-node a-viewfinder"
class:a-in-frame={selected}
class:located={data.located}
data-kind={data.kind}
data-testid={data.located ? 'located-card' : undefined}
>
<header>
<span class="type-chip">{data.kind}</span>
<span class="title" title={data.title}>{data.title}</span>
</header>
<div
class="media"
use:finishAnimations
onanimationend={() => (arriving = false)}
>
{#if videoSrc}
<button
type="button"
class="media-btn"
class:a-result-develop={arriving}
onclick={openPreview}
title={t('canvas.openPreview')}
>
<video
class="media-el"
src={videoSrc}
muted
playsinline
preload="metadata"
></video>
</button>
{:else if mediaSrc}
<button
type="button"
class="media-btn"
class:a-result-develop={arriving}
onclick={openPreview}
title={t('canvas.openPreview')}
>
<SafeMedia
class="media-el"
src={mediaSrc}
alt={data.title}
kind="image"
controls={false}
/>
</button>
{:else if data.status === 'running'}
<span class="placeholder">{t('canvas.generating')}</span>
{:else}
<span class="placeholder">{t('canvas.noMedia')}</span>
{/if}
<span class="out-kind">{data.kind}</span>
</div>
<footer>
{#if running}<span class="status running-badge">{t('canvas.running')}</span
>{:else if data.status === 'failed'}<span class="status failed-badge"
>{t('canvas.failed')}</span
>{/if}<span class="node-id">#{data.canvasNodeId}</span>
</footer>
</div>