diff --git a/calliope-backend/tests/test_playground_uploads.py b/calliope-backend/tests/test_playground_uploads.py index f8cd9598..23c0390f 100644 --- a/calliope-backend/tests/test_playground_uploads.py +++ b/calliope-backend/tests/test_playground_uploads.py @@ -78,3 +78,55 @@ def test_upload_extension_map(): assert _kind_for_ext(".JPG") == "image" # case-insensitive assert _kind_for_ext(".exe") is None assert _kind_for_ext("") is None + + +def test_upload_assigned_to_character_sheet(client): + proj = client.post("/api/projects", json={"title": "Reel"}).json() + char = client.post( + f"/api/projects/{proj['id']}/characters", + json={"name": "Hero"}, + ).json() + up = client.post( + "/api/playground/uploads", + files={"file": ("hero.png", PNG_BYTES, "image/png")}, + ) + assert up.status_code == 200 + path = up.json()["path"] + + patched = client.patch( + f"/api/projects/{proj['id']}/characters/{char['id']}", + json={"sheet_path": path}, + ) + assert patched.status_code == 200 + assert patched.json()["sheet_path"] == path + + loc = client.post( + f"/api/projects/{proj['id']}/locations", + json={"name": "Dock"}, + ).json() + loc_up = client.post( + "/api/playground/uploads", + files={"file": ("dock.png", PNG_BYTES, "image/png")}, + ).json() + loc_patched = client.patch( + f"/api/projects/{proj['id']}/locations/{loc['id']}", + json={"reference_image_path": loc_up["path"]}, + ) + assert loc_patched.status_code == 200 + assert loc_patched.json()["reference_image_path"] == loc_up["path"] + + item = client.post( + f"/api/projects/{proj['id']}/items", + json={"name": "Key"}, + ).json() + item_up = client.post( + "/api/playground/uploads", + files={"file": ("key.png", PNG_BYTES, "image/png")}, + ).json() + item_patched = client.patch( + f"/api/projects/{proj['id']}/items/{item['id']}", + json={"reference_image_path": item_up["path"]}, + ) + assert item_patched.status_code == 200 + assert item_patched.json()["reference_image_path"] == item_up["path"] + diff --git a/calliope-web/src/lib/components/AssetsStage.svelte b/calliope-web/src/lib/components/AssetsStage.svelte index fc39a7f2..5a7531ea 100644 --- a/calliope-web/src/lib/components/AssetsStage.svelte +++ b/calliope-web/src/lib/components/AssetsStage.svelte @@ -5,6 +5,7 @@ import { assetUrl, jobsApi, + playgroundApi, projects, workflows, type Character, @@ -72,6 +73,9 @@ type DeleteTarget = { kind: 'character' | 'location' | 'item'; id: number; name: string }; let deleteTarget = $state(null); let deleteOpen = $state(false); + let fileInput: HTMLInputElement | null = $state(null); + let uploadTarget = $state(null); + let uploadingKey = $state(null); const assetsQuery = createQuery( toStore(() => ({ @@ -502,14 +506,73 @@ function goToStory() { goto('?stage=story', { keepFocus: true, noScroll: true }); } + + function uploadKey(kind: DeleteTarget['kind'], id: number) { + if (kind === 'character') return charKey(id); + if (kind === 'location') return locKey(id); + return itemKey(id); + } + + function openOwnImage(target: DeleteTarget) { + uploadTarget = target; + fileInput?.click(); + } + + async function onOwnImageChosen(e: Event) { + const input = e.currentTarget as HTMLInputElement; + const file = input.files?.[0]; + input.value = ''; + const target = uploadTarget; + uploadTarget = null; + if (!file || !target) return; + if (!file.type.startsWith('image/')) { + toast.error('Please choose an image file (png, jpg, webp, gif)'); + return; + } + const key = uploadKey(target.kind, target.id); + uploadingKey = key; + try { + const uploaded = await playgroundApi.upload(file); + if (uploaded.kind !== 'image') { + toast.error('That file is not an image'); + return; + } + if (target.kind === 'character') { + await projects.updateCharacter(projectId, target.id, { sheet_path: uploaded.path }); + } else if (target.kind === 'location') { + await projects.updateLocation(projectId, target.id, { + reference_image_path: uploaded.path, + }); + } else { + await projects.updateItem(projectId, target.id, { reference_image_path: uploaded.path }); + } + await client.invalidateQueries({ queryKey: ['assets', projectId] }); + await client.invalidateQueries({ queryKey: ['project', projectId] }); + await client.invalidateQueries({ queryKey: ['projects'] }); + toast.success(`Image added for ${target.name}`); + } catch (err) { + toast.error(err instanceof Error ? err.message : 'Could not add image'); + } finally { + uploadingKey = null; + } + } + +

2. Characters, Environments & Items

- Pick a workflow below. Sheet/environment/item prompts use the built-in template by - default — expand Edit prompt only if you need to tweak text. + Pick a workflow below, or upload your own image on any card. Sheet/environment/item + prompts use the built-in template by default — expand Edit prompt only if you need to + tweak text.

@@ -730,6 +793,18 @@ >View {char.name} sheet full size + {#if jstate === 'failed' && job} + {#if jstate === 'failed' && job} + {#if jstate === 'failed' && job}