Enable image pasting in markdown textarea

This commit is contained in:
thiloho
2024-08-19 19:31:41 +02:00
parent ec23a6188a
commit 5cc329c2f1
5 changed files with 99 additions and 3 deletions

View File

@@ -18,7 +18,7 @@ export const load: PageServerLoad = async ({ parent, params, cookies, fetch }) =
};
export const actions: Actions = {
default: async ({ fetch, cookies, request, params }) => {
editArticle: async ({ fetch, cookies, request, params }) => {
const data = await request.formData();
const coverFile = data.get("cover-image") as File;
@@ -63,5 +63,30 @@ export const actions: Actions = {
}
return { success: true, message: "Successfully updated article" };
},
pasteImage: async ({ request, fetch, cookies, params }) => {
const data = await request.formData();
const file = data.get("file") as File;
const fileData = await fetch(`${API_BASE_PREFIX}/rpc/upload_file`, {
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
Authorization: `Bearer ${cookies.get("session_token")}`,
Accept: "application/vnd.pgrst.object+json",
"X-Website-Id": params.websiteId,
"X-Mimetype": file.type,
"X-Original-Filename": file.name
},
body: await file.arrayBuffer()
});
const fileJSON = await fileData.json();
if (!fileData.ok) {
return { success: false, message: fileJSON.message };
}
return { fileId: fileJSON.file_id };
}
};

View File

@@ -5,7 +5,7 @@
import SuccessOrError from "$lib/components/SuccessOrError.svelte";
import type { ActionData, PageServerData } from "./$types";
import Modal from "$lib/components/Modal.svelte";
import { API_BASE_PREFIX } from "$lib/utils";
import { API_BASE_PREFIX, handleImagePaste } from "$lib/utils";
const { data, form } = $props<{ data: PageServerData; form: ActionData }>();
@@ -17,6 +17,11 @@
const { scrollTop, scrollHeight, clientHeight } = mainContentTextarea;
textareaScrollTop = (scrollTop / (scrollHeight - clientHeight)) * 100;
};
const handlePaste = async (event: ClipboardEvent) => {
const newContent = await handleImagePaste(event);
previewContent = newContent;
};
</script>
<SuccessOrError success={form?.success} message={form?.message} />
@@ -33,6 +38,7 @@
<form
method="POST"
action="?/editArticle"
enctype="multipart/form-data"
use:enhance={() => {
return async ({ update }) => {
@@ -94,6 +100,7 @@
bind:value={previewContent}
bind:this={mainContentTextarea}
onscroll={updateScrollPercentage}
onpaste={handlePaste}
required>{data.article.main_content}</textarea
>
</label>