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">
|
<script lang="ts">
|
||||||
import { uploadMedia } from '$lib/pb';
|
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;
|
url?: string;
|
||||||
alt?: string;
|
alt?: string;
|
||||||
onChange?: (value: { url: string; alt: string }) => void;
|
onChange?: (value: { url: string; alt: string }) => void;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
let uploading = $state(false);
|
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) {
|
async function handleFile(event: Event) {
|
||||||
const file = (event.currentTarget as HTMLInputElement).files?.[0];
|
const file = (event.currentTarget as HTMLInputElement).files?.[0];
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
uploading = true;
|
uploading = true;
|
||||||
|
error = '';
|
||||||
try {
|
try {
|
||||||
const result = await uploadMedia(file);
|
const result = await uploadMedia(file);
|
||||||
url = result.url;
|
localUrl = result.url;
|
||||||
onChange({ url, alt });
|
emitChange();
|
||||||
|
} catch (e) {
|
||||||
|
error = e instanceof Error ? e.message : 'Upload failed';
|
||||||
|
console.error('Image upload error:', e);
|
||||||
} finally {
|
} finally {
|
||||||
uploading = false;
|
uploading = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function removeImage() {
|
||||||
|
localUrl = '';
|
||||||
|
localAlt = '';
|
||||||
|
emitChange();
|
||||||
|
if (fileInput) fileInput.value = '';
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="space-y-3">
|
<div class="grid grid-cols-[1fr_1.5fr] gap-4">
|
||||||
{#if url}
|
<!-- Drop zone / upload area -->
|
||||||
<img class="max-h-56 rounded-box object-cover" src={url} alt={alt} />
|
<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}
|
{/if}
|
||||||
<input class="file-input file-input-bordered w-full" type="file" accept="image/*" onchange={handleFile} disabled={uploading} />
|
</div>
|
||||||
<label class="form-control">
|
|
||||||
<span class="label-text">Image URL</span>
|
{#if error}
|
||||||
<input class="input input-bordered" bind:value={url} oninput={() => onChange({ url, alt })} />
|
<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>
|
||||||
<label class="form-control">
|
<label class="form-control w-full">
|
||||||
<span class="label-text">Alt text</span>
|
<div class="label"><span class="label-text">Alt text</span></div>
|
||||||
<input class="input input-bordered" bind:value={alt} oninput={() => onChange({ url, alt })} />
|
<input class="input input-bordered w-full" bind:value={localAlt} oninput={() => emitChange()} />
|
||||||
</label>
|
</label>
|
||||||
{#if uploading}<span class="loading loading-spinner"></span>{/if}
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,14 @@
|
||||||
if (url) exec('createLink', url);
|
if (url) exec('createLink', url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clearFormatting() {
|
||||||
|
if (!browser) return;
|
||||||
|
editorEl?.focus();
|
||||||
|
document.execCommand('formatBlock', false, '<p>');
|
||||||
|
document.execCommand('removeFormat');
|
||||||
|
emit();
|
||||||
|
}
|
||||||
|
|
||||||
function handleInput() {
|
function handleInput() {
|
||||||
emit();
|
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('formatBlock', '<h3>')} type="button">H3</button>
|
||||||
<button class="btn btn-xs" onclick={() => exec('insertUnorderedList')} type="button">List</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={setLink} type="button">Link</button>
|
||||||
|
<button class="btn btn-xs" onclick={clearFormatting} type="button">Clear</button>
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
bind:this={editorEl}
|
bind:this={editorEl}
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,10 @@ export async function uploadMedia(file: File) {
|
||||||
formData.append('file', file);
|
formData.append('file', file);
|
||||||
formData.append('alt', file.name.replace(/\.[^/.]+$/, '').replace(/[-_]/g, ' '));
|
formData.append('alt', file.name.replace(/\.[^/.]+$/, '').replace(/[-_]/g, ' '));
|
||||||
|
|
||||||
const record = await pb.collection('media').create(formData);
|
const res = await fetch('/admin/api/upload', { method: 'POST', body: formData });
|
||||||
return {
|
if (!res.ok) {
|
||||||
record,
|
const err = await res.json().catch(() => ({}));
|
||||||
url: getFileUrl(record.collectionId, record.id, record.file)
|
throw new Error(err?.message || `Upload failed (${res.status})`);
|
||||||
};
|
}
|
||||||
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
<div class="flex-none gap-2">
|
<div class="flex-none gap-2">
|
||||||
<a class="btn btn-ghost" href="/admin/pages">Pages</a>
|
<a class="btn btn-ghost" href="/admin/pages">Pages</a>
|
||||||
<a class="btn btn-ghost" href="/admin/globals">Globals</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>
|
<button class="btn btn-outline" type="submit">Logout</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</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)} />
|
<input type="hidden" name="data" value={JSON.stringify(footer)} />
|
||||||
<h2 class="mb-4 text-2xl font-bold">Footer</h2>
|
<h2 class="mb-4 text-2xl font-bold">Footer</h2>
|
||||||
{#each ['businessName', 'tagline', 'address', 'phone', 'email', 'copyrightText'] as field}
|
{#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}
|
{/each}
|
||||||
<h3 class="mt-6 font-bold">Social links</h3>
|
<h3 class="mt-6 font-bold">Social links</h3>
|
||||||
{#each footer.socialLinks || [] as link, i}
|
{#each footer.socialLinks || [] as link, i}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ services:
|
||||||
ports:
|
ports:
|
||||||
- "8090:80"
|
- "8090:80"
|
||||||
volumes:
|
volumes:
|
||||||
- ./pocketbase/pb_data:/pb/pb_data
|
- pocketbase:/pb/pb_data
|
||||||
|
|
||||||
app:
|
app:
|
||||||
build:
|
build:
|
||||||
|
|
@ -22,3 +22,7 @@ services:
|
||||||
ORIGIN: http://localhost:3000
|
ORIGIN: http://localhost:3000
|
||||||
ports:
|
ports:
|
||||||
- "3000:3000"
|
- "3000:3000"
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
pocketbase:
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue