|
| 1 | +<script lang="ts"> |
| 2 | + import CarbonGeneratePdf from "~icons/carbon/generate-pdf"; |
| 3 | + import EosIconsLoading from "~icons/eos-icons/loading"; |
| 4 | + let fileList: File[]; |
| 5 | + $: fileList = []; |
| 6 | +
|
| 7 | + $: file_error = false; |
| 8 | + $: file_error_message = ""; |
| 9 | +
|
| 10 | + export let value = ""; |
| 11 | + export let onDrag = false; |
| 12 | +
|
| 13 | + async function handleFileUpload(file: File) { |
| 14 | + // file to blob |
| 15 | + let pdf_blob = new Blob([file], { type: "application/pdf" }); |
| 16 | +
|
| 17 | + const formData = new FormData(); |
| 18 | + formData.append("file", pdf_blob); |
| 19 | +
|
| 20 | + try { |
| 21 | + const response = await fetch("/convertPdf", { |
| 22 | + method: "POST", |
| 23 | + body: formData, |
| 24 | + }); |
| 25 | +
|
| 26 | + if (!response.ok) { |
| 27 | + throw new Error("Failed to convert PDF"); |
| 28 | + } |
| 29 | +
|
| 30 | + let { result } = await response.json(); |
| 31 | + return result; |
| 32 | + } catch (error) { |
| 33 | + console.error("PDF File conversion error:", error); |
| 34 | + return ""; |
| 35 | + } |
| 36 | + } |
| 37 | +
|
| 38 | + async function dropHandle(event: DragEvent) { |
| 39 | + event.preventDefault(); |
| 40 | +
|
| 41 | + if (event.dataTransfer && event.dataTransfer.items) { |
| 42 | + // Use DataTransferItemList interface to access the file(s) |
| 43 | + for (let i = 0; i < event.dataTransfer.items.length; i++) { |
| 44 | + // If dropped items aren't files, reject them |
| 45 | + if (event.dataTransfer.items[i].kind === "file") { |
| 46 | + const file = event.dataTransfer.items[i].getAsFile(); |
| 47 | + if (file) { |
| 48 | + fileList = [...fileList, file]; |
| 49 | + if (event.dataTransfer.items[i].type !== "application/pdf") { |
| 50 | + console.log("Only PDF files are supported"); |
| 51 | + file_error = true; |
| 52 | + file_error_message = "Only PDF files are supported"; |
| 53 | + break; |
| 54 | + } |
| 55 | + try { |
| 56 | + let mardown_conversion = await handleFileUpload(file); |
| 57 | + value += mardown_conversion; |
| 58 | + } catch (error) { |
| 59 | + file_error = true; |
| 60 | + file_error_message = "Failed to convert PDF: " + error; |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +
|
| 68 | + if (file_error === true) { |
| 69 | + // Sleep for 2 sec |
| 70 | + await new Promise((r) => setTimeout(r, 1000)); |
| 71 | + } |
| 72 | + fileList = []; |
| 73 | + onDrag = false; |
| 74 | + } |
| 75 | +</script> |
| 76 | + |
| 77 | +<div |
| 78 | + id="dropzone" |
| 79 | + on:drop={dropHandle} |
| 80 | + class="relative flex w-full max-w-4xl flex-col items-center rounded-xl border bg-gray-100 focus-within:border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:focus-within:border-gray-500" |
| 81 | +> |
| 82 | + <div class="items-center object-center" /> |
| 83 | + <div class="object-center"> |
| 84 | + {#if fileList.length === 0} |
| 85 | + <div class="mt-3 flex justify-center"> |
| 86 | + <CarbonGeneratePdf class="text-5xl text-gray-500 dark:text-gray-400" /> |
| 87 | + </div> |
| 88 | + <p class="mb-3 mt-3 text-sm text-gray-500 dark:text-gray-400"> |
| 89 | + Drag and drop your <span class="font-semibold">PDF</span> file here |
| 90 | + </p> |
| 91 | + {:else if file_error === true} |
| 92 | + <p class="mb-3 mt-3 text-sm text-gray-500 dark:text-gray-400"> |
| 93 | + Error {file_error_message} |
| 94 | + </p> |
| 95 | + {:else} |
| 96 | + {#each fileList as file} |
| 97 | + <p class="mb-3 mt-3 flex items-center text-sm text-gray-500 dark:text-gray-400"> |
| 98 | + <EosIconsLoading class="mr-2 text-3xl" /> |
| 99 | + <span class="font-semibold">{file.name}</span>: {file.size} bytes is being converted |
| 100 | + </p> |
| 101 | + {/each} |
| 102 | + {/if} |
| 103 | + </div> |
| 104 | +</div> |
0 commit comments