obsidian-custom-attachment-location
obsidian-custom-attachment-location copied to clipboard
[Bug] Drag & drop does not use custom attachment name
Hello,
On desktop and mobile (iPad), drag and drop still does not change the image name to custom name (here ${filename}). I've tested with .png
, .jpeg
and .jpg
files.
Wasn't the latest release supposed to solve that issue ?
Thank you !
Environment
Obsidian 1.0.3 Plugin 0.0.9 Windows 11 iPad OS 15.7
Please find below a proposition of modification of handleDrop
, so that drag & dropped files work :
handleDrop(event, editor, view) {
return __async(this, null, function* () {
console.log("Handle Drop");
let mdFileName = view.file.basename;
// Uncomment to handle absolute path (here relative because attachment folder begins with "./"), but rewrite without node.js "Path".
// let mdFolderPath = Path.dirname(view.file.path);
let path = this.getAttachmentFolderPath(mdFileName);
// Uncomment to handle absolute path, but rewrite without node.js "Path".
// let fullPath = this.getAttachmentFolderFullPath(mdFolderPath, mdFileName);
// Uncomment to handle absolute path.
//if (!this.useRelativePath && !(yield this.adapter.exists(fullPath)))
// yield this.app.vault.createFolder(fullPath);
this.updateAttachmentFolderConfig(path);
if(this.settings.renameDraggedFiles)
{
let dataTransferItems = event.dataTransfer.items;
for (let i in dataTransferItems) {
let item = dataTransferItems[i];
if (item.kind !== "file")
continue;
if (!(item.type === "image/png" || item.type === "image/jpeg" || item.type === "image/bmp" || item.type === "audio/mpeg" || item.type === "audio/wav"))
continue;
let droppedFile = item.getAsFile();
if (!droppedFile)
continue;
let extension = "";
switch (item.type) {
case "image/png":
extension = "png"
break;
case "image/jpeg":
extension = "jpeg"
break;
case "image/bmp":
extension = "bmp"
break;
case "audio/wav":
extension = "wav"
break;
case "audio/mpeg":
extension = "mp3"
break;
default:
console.log(`Type "${item.type}" is not handled !`);
}
event.preventDefault();
let file = yield blobToArrayBuffer(droppedFile);
let name = this.getPastedImageFileName(mdFileName);
let savedFile = yield this.app.saveAttachment(name, extension, file);
let markdownLink = yield this.app.fileManager.generateMarkdownLink(savedFile, view.file.path);
markdownLink += "\n\n";
editor.replaceSelection(markdownLink);
}
}
});
}
Please note the following :
- It works for
.png
,.jpeg
,.jpg
and.bmp
images and.wav
,.mp3
audio files. - It works on desktop and mobile.
- For this to work, you need to add a setting
renameDraggedFiles
and set it to true. - For now, only relative paths work (= attachment folder begins with
./
). For absolute paths to work, it is needed to stop using node.jsPath
and use Obsidian API instead (see commented lines in the script). It's probably not difficult to do, but I've not looked at it yet. -
handlePaste
should be rewritten a bit likehandleDrop
so that it also works on mobile (but only with relative paths as written in4.
).
Update : Fix does not work with Canvas.
Related : https://github.com/RainCat1998/obsidian-custom-attachment-location/issues/39
Added corresponding setting in https://github.com/RainCat1998/obsidian-custom-attachment-location/releases/tag/2.1.0