From 650c95fb783810b883c26e8bb832ab4adc6bc42f Mon Sep 17 00:00:00 2001 From: agent Date: Mon, 26 Oct 2026 14:21:22 +0000 Subject: [PATCH] Image picker: pick from the uploaded images; labels beside the fields "Pick from library" opens the images already in `media` (newest first, searchable by alt text or file name, 24 at a time) so one can be reused without uploading it again; it lists them through the new admin GET /api/media. The layout follows the sketch: box on the left, Alt text and Image URL with their labels beside them, and Pick from library / Paste as a full-width row underneath. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW --- AGENTS.md | 2 +- .../src/lib/admin/editors/ImagePicker.svelte | 116 ++++++++++-------- .../src/lib/admin/editors/MediaLibrary.svelte | 98 +++++++++++++++ app-admin/src/routes/api/media/+server.ts | 29 +++++ 4 files changed, 190 insertions(+), 55 deletions(-) create mode 100644 app-admin/src/lib/admin/editors/MediaLibrary.svelte create mode 100644 app-admin/src/routes/api/media/+server.ts diff --git a/AGENTS.md b/AGENTS.md index 96d0823..c9590ee 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -146,7 +146,7 @@ To add an email block type: add to `EmailBlockType` in `packages/shared/src/type - Section card: `rounded-box bg-base-100 p-5 shadow` with an `h2.text-2xl.font-bold` title (see Site design / globals). - Form field: ``; hints in ``. - Setting rows in editors: `SegmentedControl` (choices) and `ColorField` (colours) — label column on the left, like the block Background picker. - - Images: always `$lib/admin/editors/ImagePicker.svelte` (choose, drag in or paste an image; address and alt text). It uploads to `media` through `/api/upload`; `withUrl={false}` + `altHint=""` + the `id` from `onChange` where a field stores a media relation (Services & products). Store only `url`/`alt` from `onChange` elsewhere. + - Images: always `$lib/admin/editors/ImagePicker.svelte` (choose, drag in or paste an image, or pick an uploaded one from the library: `MediaLibrary.svelte`, `/api/media`; address and alt text). It uploads to `media` through `/api/upload`; `withUrl={false}` + `altHint=""` + the `id` from `onChange` where a field stores a media relation (Services & products). Store only `url`/`alt` from `onChange` elsewhere. - Create/edit forms open in `$lib/admin/Modal.svelte`; destructive submits use `confirmSubmit()` from `$lib/admin/confirm` (returning `false` from a handler does not cancel). - Server actions wrap PocketBase writes in `attempt()` from `$lib/server/actions` so failures show as an error banner, not a 500. - Editors with unsaved state use `use:enhance` + `warnOnUnsavedChanges()`. diff --git a/app-admin/src/lib/admin/editors/ImagePicker.svelte b/app-admin/src/lib/admin/editors/ImagePicker.svelte index 953e832..0a9e8b2 100644 --- a/app-admin/src/lib/admin/editors/ImagePicker.svelte +++ b/app-admin/src/lib/admin/editors/ImagePicker.svelte @@ -1,13 +1,16 @@ + + + search(e.currentTarget.value)} + onkeydown={(e) => e.key === 'Enter' && e.preventDefault()} + /> + + {#if error} + + {/if} + + {#if items.length} +
+ {#each items as media (media.id)} + + {/each} +
+ {#if page < totalPages} +
+ +
+ {/if} + {:else if loading} +
+ {:else if !error} +

{query ? 'No images match that search.' : 'No images have been uploaded yet.'}

+ {/if} +
diff --git a/app-admin/src/routes/api/media/+server.ts b/app-admin/src/routes/api/media/+server.ts new file mode 100644 index 0000000..14ab5a6 --- /dev/null +++ b/app-admin/src/routes/api/media/+server.ts @@ -0,0 +1,29 @@ +import { env } from '$env/dynamic/public'; +import { error, json } from '@sveltejs/kit'; +import type { RequestHandler } from './$types'; + +const PER_PAGE = 24; + +// Uploaded images for the image picker's library, newest first; `q` searches alt text and file name. +export const GET: RequestHandler = async ({ url, locals }) => { + const page = Math.max(1, Number(url.searchParams.get('page')) || 1); + const q = (url.searchParams.get('q') || '').trim(); + try { + const list = await locals.pb.collection('media').getList(page, PER_PAGE, { + sort: '-created', + filter: q ? locals.pb.filter('alt ~ {:q} || file ~ {:q}', { q }) : '' + }); + return json({ + page: list.page, + totalPages: list.totalPages, + items: list.items.map((record) => ({ + id: record.id, + alt: record.alt || '', + url: `${env.PUBLIC_POCKETBASE_URL}/api/files/${record.collectionId}/${record.id}/${record.file}` + })) + }); + } catch (e) { + console.error('Media list error:', e); + throw error(500, 'Couldn’t load the uploaded images'); + } +};