first image and entry upload working hell yeah
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
</script>
|
||||
|
||||
{#if edit}
|
||||
<Editor newEntryDate={date} edit={true}/>
|
||||
<Editor newEntryDate={date} bind:edit />
|
||||
{:else}
|
||||
<div class="flex flex-col items-center justify-center">
|
||||
<button
|
||||
|
||||
@@ -2,22 +2,39 @@
|
||||
import ImageTarget from "./imageTarget.svelte";
|
||||
import TextEditor from "./textEditor.svelte";
|
||||
import { formatDate } from "$lib/date.ts";
|
||||
import { createEntry } from "$lib/upload.ts";
|
||||
|
||||
let { newEntryDate, entry = $bindable(), edit = $bindable(false) } = $props();
|
||||
|
||||
let newEntry = $state({ date: newEntryDate, image: "", content: "" });
|
||||
|
||||
let editModeText = $derived(edit ? "done" : "edit")
|
||||
</script>
|
||||
|
||||
<div class="flex flex-row mb-2">
|
||||
<p class="text-3xl font-bold">{entry ? formatDate(entry.date) : formatDate(newEntryDate)}</p>
|
||||
<button
|
||||
class="bg-white/20 px-3 rounded-lg ml-auto"
|
||||
onclick={() => edit = !edit}
|
||||
>
|
||||
{editModeText}
|
||||
</button>
|
||||
|
||||
{#if edit}
|
||||
<button
|
||||
class="bg-white/10 px-3 rounded-lg ml-auto"
|
||||
onclick={() => edit = false}
|
||||
>
|
||||
cancel
|
||||
</button>
|
||||
<button
|
||||
class="bg-white/20 px-3 rounded-lg ml-1"
|
||||
onclick={() => { createEntry(newEntry); edit = false }}
|
||||
>
|
||||
done
|
||||
</button>
|
||||
{:else}
|
||||
<button
|
||||
class="bg-white/20 px-3 rounded-lg ml-auto"
|
||||
onclick={() => edit = true}
|
||||
>
|
||||
edit
|
||||
</button>
|
||||
{/if}
|
||||
|
||||
</div>
|
||||
|
||||
{#if entry}
|
||||
|
||||
42
src/lib/upload.ts
Normal file
42
src/lib/upload.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
function dataURLtoFile(dataurl: string, filename: string) {
|
||||
var arr = dataurl.split(','),
|
||||
mime = arr[0].match(/:(.*?);/)[1],
|
||||
bstr = atob(arr[arr.length - 1]),
|
||||
n = bstr.length,
|
||||
u8arr = new Uint8Array(n);
|
||||
while(n--){
|
||||
u8arr[n] = bstr.charCodeAt(n);
|
||||
}
|
||||
return new File([u8arr], filename, {type:mime});
|
||||
}
|
||||
|
||||
export async function uploadImage(b64: string) {
|
||||
const data = new FormData()
|
||||
|
||||
// unable to tell from the b64 whether it's a jpg or png but defaulting to jpg seems to work fine enough
|
||||
const file = dataURLtoFile(b64, 'image.jpg')
|
||||
data.append('image', file)
|
||||
|
||||
const r = await fetch('/api/image', {
|
||||
method: 'POST',
|
||||
body: data
|
||||
})
|
||||
|
||||
const url = (await r.json()).url
|
||||
return url
|
||||
}
|
||||
|
||||
export async function createEntry(newEntry) {
|
||||
if (newEntry.image) {
|
||||
const url = await uploadImage(newEntry.image)
|
||||
newEntry.image = url
|
||||
}
|
||||
|
||||
await fetch("/api/entry/new", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(newEntry),
|
||||
});
|
||||
}
|
||||
@@ -24,6 +24,8 @@
|
||||
selectedDate = data as string
|
||||
}
|
||||
}
|
||||
|
||||
// $effect(() => {selectedEntry = ''})
|
||||
</script>
|
||||
|
||||
<Calendar entries={data.all} dayClickCallback={selectEntry} />
|
||||
|
||||
Reference in New Issue
Block a user