Drag & drop now takes filename from event
This commit is contained in:
parent
c7545bf350
commit
24be260a34
@ -1,8 +1,6 @@
|
|||||||
import { EditorView, ViewPlugin, ViewUpdate } from "@codemirror/view";
|
import { EditorView, ViewPlugin, ViewUpdate } from "@codemirror/view";
|
||||||
import { safeRun } from "@plugos/plugos/util";
|
import { safeRun } from "@plugos/plugos/util";
|
||||||
import { Space } from "@silverbulletmd/common/spaces/space";
|
|
||||||
import { maximumAttachmentSize } from "@silverbulletmd/common/types";
|
import { maximumAttachmentSize } from "@silverbulletmd/common/types";
|
||||||
import { createImportSpecifier } from "typescript";
|
|
||||||
import { Editor } from "./editor";
|
import { Editor } from "./editor";
|
||||||
|
|
||||||
const urlRegexp =
|
const urlRegexp =
|
||||||
@ -57,7 +55,10 @@ export function attachmentExtension(editor: Editor) {
|
|||||||
// TODO: This doesn't take into account the target cursor position,
|
// TODO: This doesn't take into account the target cursor position,
|
||||||
// it just drops the attachment wherever the cursor was last.
|
// it just drops the attachment wherever the cursor was last.
|
||||||
if (event.dataTransfer) {
|
if (event.dataTransfer) {
|
||||||
let payload = [...event.dataTransfer.items];
|
let payload = [...event.dataTransfer.files];
|
||||||
|
if (!payload.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
safeRun(async () => {
|
safeRun(async () => {
|
||||||
await processFileTransfer(payload);
|
await processFileTransfer(payload);
|
||||||
});
|
});
|
||||||
@ -65,22 +66,41 @@ export function attachmentExtension(editor: Editor) {
|
|||||||
},
|
},
|
||||||
paste: (event: ClipboardEvent) => {
|
paste: (event: ClipboardEvent) => {
|
||||||
let payload = [...event.clipboardData!.items];
|
let payload = [...event.clipboardData!.items];
|
||||||
|
if (!payload.length || payload.length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
safeRun(async () => {
|
safeRun(async () => {
|
||||||
await processFileTransfer(payload);
|
await processItemTransfer(payload);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
async function processFileTransfer(payload: DataTransferItem[]) {
|
async function processFileTransfer(payload: File[]) {
|
||||||
if (!payload.length || payload.length === 0) {
|
let data = await payload[0].arrayBuffer();
|
||||||
return false;
|
await saveFile(data!, payload[0].name, payload[0].type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function processItemTransfer(payload: DataTransferItem[]) {
|
||||||
let file = payload.find((item) => item.kind === "file");
|
let file = payload.find((item) => item.kind === "file");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const fileType = file.type;
|
const fileType = file.type;
|
||||||
|
let ext = fileType.split("/")[1];
|
||||||
|
let fileName = new Date()
|
||||||
|
.toISOString()
|
||||||
|
.split(".")[0]
|
||||||
|
.replace("T", "_")
|
||||||
|
.replaceAll(":", "-");
|
||||||
let data = await file!.getAsFile()?.arrayBuffer();
|
let data = await file!.getAsFile()?.arrayBuffer();
|
||||||
|
await saveFile(data!, `${fileName}.${ext}`, fileType);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveFile(
|
||||||
|
data: ArrayBuffer,
|
||||||
|
suggestedName: string,
|
||||||
|
mimeType: string
|
||||||
|
) {
|
||||||
if (data!.byteLength > maximumAttachmentSize) {
|
if (data!.byteLength > maximumAttachmentSize) {
|
||||||
editor.flashNotification(
|
editor.flashNotification(
|
||||||
`Attachment is too large, maximum is ${
|
`Attachment is too large, maximum is ${
|
||||||
@ -90,22 +110,17 @@ export function attachmentExtension(editor: Editor) {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let ext = fileType.split("/")[1];
|
|
||||||
let fileName = new Date()
|
|
||||||
.toISOString()
|
|
||||||
.split(".")[0]
|
|
||||||
.replace("T", "_")
|
|
||||||
.replaceAll(":", "-");
|
|
||||||
let finalFileName = prompt(
|
let finalFileName = prompt(
|
||||||
"File name for pasted attachment",
|
"File name for pasted attachment",
|
||||||
`${fileName}.${ext}`
|
suggestedName
|
||||||
);
|
);
|
||||||
if (!finalFileName) {
|
if (!finalFileName) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await editor.space.writeAttachment(finalFileName, data!);
|
await editor.space.writeAttachment(finalFileName, data!);
|
||||||
let attachmentMarkdown = `[${finalFileName}](attachment/${finalFileName})`;
|
let attachmentMarkdown = `[${finalFileName}](attachment/${finalFileName})`;
|
||||||
if (fileType.startsWith("image/")) {
|
if (mimeType.startsWith("image/")) {
|
||||||
attachmentMarkdown = `![](attachment/${finalFileName})`;
|
attachmentMarkdown = `![](attachment/${finalFileName})`;
|
||||||
}
|
}
|
||||||
editor.editorView!.dispatch({
|
editor.editorView!.dispatch({
|
||||||
|
Loading…
Reference in New Issue
Block a user