| 1 |
/* |
| 2 |
* File uploads via Drag and Drop on elements with class "droptarget" |
| 3 |
* and attribute "data-account-name". |
| 4 |
*/ |
| 5 |
|
| 6 |
import type { Writable } from "svelte/store"; |
| 7 |
import { writable } from "svelte/store"; |
| 8 |
|
| 9 |
import { put } from "./api"; |
| 10 |
import { todayAsString } from "./format"; |
| 11 |
import { delegate } from "./lib/events"; |
| 12 |
import { basename, documentHasAccount } from "./lib/paths"; |
| 13 |
import { notify, notify_err } from "./notifications"; |
| 14 |
|
| 15 |
/** |
| 16 |
* Handle a dragenter or dragover event. |
| 17 |
* |
| 18 |
* We want to allow a drop if the dragged thing is either a file that could be |
| 19 |
* dragged from a file manager or a URL (as dragged from a document link in Fava). |
| 20 |
*/ |
| 21 |
function dragover(event: Event, closestTarget: Element): void { |
| 22 |
if (!(event instanceof DragEvent)) { |
| 23 |
return; |
| 24 |
} |
| 25 |
const types = event.dataTransfer?.types ?? []; |
| 26 |
if (types.includes("Files") || types.includes("text/uri-list")) { |
| 27 |
closestTarget.classList.add("dragover"); |
| 28 |
event.preventDefault(); |
| 29 |
} |
| 30 |
} |
| 31 |
delegate(document, "dragenter", ".droptarget", dragover); |
| 32 |
delegate(document, "dragover", ".droptarget", dragover); |
| 33 |
|
| 34 |
function dragleave(event: Event, closestTarget: Element): void { |
| 35 |
if (!(event instanceof DragEvent)) { |
| 36 |
return; |
| 37 |
} |
| 38 |
closestTarget.classList.remove("dragover"); |
| 39 |
event.preventDefault(); |
| 40 |
} |
| 41 |
delegate(document, "dragleave", ".droptarget", dragleave); |
| 42 |
|
| 43 |
/* Stores that the Svelte component accesses. */ |
| 44 |
export const account = writable(""); |
| 45 |
export const hash = writable(""); |
| 46 |
|
| 47 |
interface DroppedFile { |
| 48 |
dataTransferFile: File; |
| 49 |
name: string; |
| 50 |
} |
| 51 |
export const files: Writable<DroppedFile[]> = writable([]); |
| 52 |
|
| 53 |
function drop(event: Event, target: Element): void { |
| 54 |
if (!(event instanceof DragEvent)) { |
| 55 |
return; |
| 56 |
} |
| 57 |
target.classList.remove("dragover"); |
| 58 |
event.preventDefault(); |
| 59 |
event.stopPropagation(); |
| 60 |
if (!event.dataTransfer) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
// Account name that the document should be attached to. |
| 65 |
const targetAccount = target.getAttribute("data-account-name"); |
| 66 |
// Hash of the entry that the document should be attached to. |
| 67 |
const targetEntry = target.getAttribute("data-entry"); |
| 68 |
|
| 69 |
if (event.dataTransfer.types.includes("Files")) { |
| 70 |
// Files are being dropped. |
| 71 |
const date = target.getAttribute("data-entry-date") ?? todayAsString(); |
| 72 |
const uploadedFiles: DroppedFile[] = []; |
| 73 |
for (const dataTransferFile of event.dataTransfer.files) { |
| 74 |
let { name } = dataTransferFile; |
| 75 |
if (!/^\d{4}-\d{2}-\d{2}/.test(name)) { |
| 76 |
name = `${date} ${name}`; |
| 77 |
} |
| 78 |
uploadedFiles.push({ dataTransferFile, name }); |
| 79 |
} |
| 80 |
account.set(targetAccount ?? ""); |
| 81 |
hash.set(targetEntry ?? ""); |
| 82 |
files.set(uploadedFiles); |
| 83 |
} else if (event.dataTransfer.types.includes("text/uri-list")) { |
| 84 |
// Links are being dropped |
| 85 |
const url = event.dataTransfer.getData("URL"); |
| 86 |
// Try to extract the filename from the URL. |
| 87 |
let filename = new URL(url).searchParams.get("filename"); |
| 88 |
if (filename != null && targetEntry != null) { |
| 89 |
if ( |
| 90 |
targetAccount != null && |
| 91 |
documentHasAccount(filename, targetAccount) |
| 92 |
) { |
| 93 |
filename = basename(filename); |
| 94 |
} |
| 95 |
put("attach_document", { filename, entry_hash: targetEntry }).then( |
| 96 |
notify, |
| 97 |
(error: unknown) => { |
| 98 |
notify_err( |
| 99 |
error, |
| 100 |
(e) => `Adding document metadata failed: ${e.message}`, |
| 101 |
); |
| 102 |
}, |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
delegate(document, "drop", ".droptarget", drop); |