fix image upload
visual improvements
This commit is contained in:
parent
94c1e0b679
commit
fe5d77443d
7 changed files with 134 additions and 24 deletions
|
|
@ -1,39 +1,105 @@
|
|||
<script lang="ts">
|
||||
import { uploadMedia } from '$lib/pb';
|
||||
|
||||
let { url = '', alt = '', onChange = (value: { url: string; alt: string }) => {} } = $props<{
|
||||
let { url: urlProp = '', alt: altProp = '', onChange = (value: { url: string; alt: string }) => {} } = $props<{
|
||||
url?: string;
|
||||
alt?: string;
|
||||
onChange?: (value: { url: string; alt: string }) => void;
|
||||
}>();
|
||||
|
||||
let uploading = $state(false);
|
||||
let error = $state('');
|
||||
let fileInput = $state<HTMLInputElement>();
|
||||
|
||||
// Local state synced from props so reactivity works reliably in Svelte 5
|
||||
let localUrl = $state(urlProp);
|
||||
let localAlt = $state(altProp);
|
||||
|
||||
$effect(() => { localUrl = urlProp; });
|
||||
$effect(() => { localAlt = altProp; });
|
||||
|
||||
function emitChange() {
|
||||
onChange({ url: localUrl, alt: localAlt });
|
||||
}
|
||||
|
||||
async function handleFile(event: Event) {
|
||||
const file = (event.currentTarget as HTMLInputElement).files?.[0];
|
||||
if (!file) return;
|
||||
uploading = true;
|
||||
error = '';
|
||||
try {
|
||||
const result = await uploadMedia(file);
|
||||
url = result.url;
|
||||
onChange({ url, alt });
|
||||
localUrl = result.url;
|
||||
emitChange();
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : 'Upload failed';
|
||||
console.error('Image upload error:', e);
|
||||
} finally {
|
||||
uploading = false;
|
||||
}
|
||||
}
|
||||
|
||||
function removeImage() {
|
||||
localUrl = '';
|
||||
localAlt = '';
|
||||
emitChange();
|
||||
if (fileInput) fileInput.value = '';
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="space-y-3">
|
||||
{#if url}
|
||||
<img class="max-h-56 rounded-box object-cover" src={url} alt={alt} />
|
||||
{/if}
|
||||
<input class="file-input file-input-bordered w-full" type="file" accept="image/*" onchange={handleFile} disabled={uploading} />
|
||||
<label class="form-control">
|
||||
<span class="label-text">Image URL</span>
|
||||
<input class="input input-bordered" bind:value={url} oninput={() => onChange({ url, alt })} />
|
||||
</label>
|
||||
<label class="form-control">
|
||||
<span class="label-text">Alt text</span>
|
||||
<input class="input input-bordered" bind:value={alt} oninput={() => onChange({ url, alt })} />
|
||||
</label>
|
||||
{#if uploading}<span class="loading loading-spinner"></span>{/if}
|
||||
<div class="grid grid-cols-[1fr_1.5fr] gap-4">
|
||||
<!-- Drop zone / upload area -->
|
||||
<div
|
||||
class="flex min-h-28 cursor-pointer flex-col items-center justify-center rounded-box border-2 border-dashed p-3 transition-colors hover:border-primary {localUrl ? 'border-success/40' : 'border-base-300'}"
|
||||
role="button"
|
||||
tabindex="0"
|
||||
onclick={() => fileInput?.click()}
|
||||
onkeydown={(e) => e.key === 'Enter' && fileInput?.click()}
|
||||
>
|
||||
{#if uploading}
|
||||
<span class="loading loading-spinner loading-lg text-primary"></span>
|
||||
<p class="mt-3 text-sm text-base-content/60">Uploading…</p>
|
||||
{:else if localUrl}
|
||||
<div class="relative w-full">
|
||||
<img class="max-h-40 w-full rounded-box object-cover shadow" src={localUrl} alt={localAlt} />
|
||||
<button
|
||||
class="btn btn-circle btn-error btn-outline btn-xs absolute right-1 top-1"
|
||||
type="button"
|
||||
onclick={(e) => { e.stopPropagation(); removeImage(); }}
|
||||
title="Remove image"
|
||||
>✕</button>
|
||||
</div>
|
||||
{:else}
|
||||
<svg class="mb-2 size-8 text-base-content/40" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5m-13.5-9L12 3m0 0 4.5 4.5M12 3v13.5" />
|
||||
</svg>
|
||||
<p class="text-center text-sm font-medium text-base-content/70">Upload image</p>
|
||||
<p class="text-xs text-base-content/40">PNG, JPG, WebP</p>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if error}
|
||||
<div class="alert alert-error col-span-full text-sm">{error}</div>
|
||||
{/if}
|
||||
|
||||
<input
|
||||
class="hidden"
|
||||
type="file"
|
||||
accept="image/*"
|
||||
onchange={handleFile}
|
||||
disabled={uploading}
|
||||
bind:this={fileInput}
|
||||
/>
|
||||
|
||||
<!-- URL & Alt fields -->
|
||||
<div class="space-y-3">
|
||||
<label class="form-control w-full">
|
||||
<div class="label"><span class="label-text">Image URL</span></div>
|
||||
<input class="input input-bordered w-full" bind:value={localUrl} oninput={() => emitChange()} />
|
||||
</label>
|
||||
<label class="form-control w-full">
|
||||
<div class="label"><span class="label-text">Alt text</span></div>
|
||||
<input class="input input-bordered w-full" bind:value={localAlt} oninput={() => emitChange()} />
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -39,6 +39,14 @@
|
|||
if (url) exec('createLink', url);
|
||||
}
|
||||
|
||||
function clearFormatting() {
|
||||
if (!browser) return;
|
||||
editorEl?.focus();
|
||||
document.execCommand('formatBlock', false, '<p>');
|
||||
document.execCommand('removeFormat');
|
||||
emit();
|
||||
}
|
||||
|
||||
function handleInput() {
|
||||
emit();
|
||||
}
|
||||
|
|
@ -53,6 +61,7 @@
|
|||
<button class="btn btn-xs" onclick={() => exec('formatBlock', '<h3>')} type="button">H3</button>
|
||||
<button class="btn btn-xs" onclick={() => exec('insertUnorderedList')} type="button">List</button>
|
||||
<button class="btn btn-xs" onclick={setLink} type="button">Link</button>
|
||||
<button class="btn btn-xs" onclick={clearFormatting} type="button">Clear</button>
|
||||
</div>
|
||||
<div
|
||||
bind:this={editorEl}
|
||||
|
|
|
|||
|
|
@ -28,9 +28,10 @@ export async function uploadMedia(file: File) {
|
|||
formData.append('file', file);
|
||||
formData.append('alt', file.name.replace(/\.[^/.]+$/, '').replace(/[-_]/g, ' '));
|
||||
|
||||
const record = await pb.collection('media').create(formData);
|
||||
return {
|
||||
record,
|
||||
url: getFileUrl(record.collectionId, record.id, record.file)
|
||||
};
|
||||
const res = await fetch('/admin/api/upload', { method: 'POST', body: formData });
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(err?.message || `Upload failed (${res.status})`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
<div class="flex-none gap-2">
|
||||
<a class="btn btn-ghost" href="/admin/pages">Pages</a>
|
||||
<a class="btn btn-ghost" href="/admin/globals">Globals</a>
|
||||
<form method="POST" action="/admin/login?/logout">
|
||||
<form class="inline" method="POST" action="/admin/login?/logout">
|
||||
<button class="btn btn-outline" type="submit">Logout</button>
|
||||
</form>
|
||||
</div>
|
||||
|
|
|
|||
27
app/src/routes/admin/api/upload/+server.ts
Normal file
27
app/src/routes/admin/api/upload/+server.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { env } from '$env/dynamic/public';
|
||||
import { error, json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
const formData = await request.formData();
|
||||
const file = formData.get('file') as File | null;
|
||||
const alt = formData.get('alt') as string | null;
|
||||
|
||||
if (!file) {
|
||||
throw error(400, 'No file provided');
|
||||
}
|
||||
|
||||
const uploadForm = new FormData();
|
||||
uploadForm.append('file', file);
|
||||
if (alt) uploadForm.append('alt', alt);
|
||||
|
||||
try {
|
||||
const record = await locals.pb.collection('media').create(uploadForm);
|
||||
const publicUrl = env.PUBLIC_POCKETBASE_URL || 'http://localhost:8090';
|
||||
const url = `${publicUrl}/api/files/${record.collectionId}/${record.id}/${record.file}`;
|
||||
return json({ record, url });
|
||||
} catch (e) {
|
||||
console.error('Media upload error:', e);
|
||||
throw error(500, 'Upload failed');
|
||||
}
|
||||
};
|
||||
|
|
@ -42,7 +42,10 @@
|
|||
<input type="hidden" name="data" value={JSON.stringify(footer)} />
|
||||
<h2 class="mb-4 text-2xl font-bold">Footer</h2>
|
||||
{#each ['businessName', 'tagline', 'address', 'phone', 'email', 'copyrightText'] as field}
|
||||
<label class="form-control"><span class="label-text">{field}</span><input class="input input-bordered" bind:value={footer[field]} /></label>
|
||||
<label class="form-control w-full">
|
||||
<div class="label"><span class="label-text">{field}</span></div>
|
||||
<input class="input input-bordered w-full" bind:value={footer[field]} />
|
||||
</label>
|
||||
{/each}
|
||||
<h3 class="mt-6 font-bold">Social links</h3>
|
||||
{#each footer.socialLinks || [] as link, i}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ services:
|
|||
ports:
|
||||
- "8090:80"
|
||||
volumes:
|
||||
- ./pocketbase/pb_data:/pb/pb_data
|
||||
- pocketbase:/pb/pb_data
|
||||
|
||||
app:
|
||||
build:
|
||||
|
|
@ -22,3 +22,7 @@ services:
|
|||
ORIGIN: http://localhost:3000
|
||||
ports:
|
||||
- "3000:3000"
|
||||
|
||||
volumes:
|
||||
pocketbase:
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue