obsidian-custom-attachment-location icon indicating copy to clipboard operation
obsidian-custom-attachment-location copied to clipboard

[Bug] Drag & drop does not use custom attachment name

Open Elaws opened this issue 2 years ago • 2 comments

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

Elaws avatar Dec 04 '22 21:12 Elaws

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 :

  1. It works for .png, .jpeg, .jpg and .bmp images and .wav, .mp3 audio files.
  2. It works on desktop and mobile.
  3. For this to work, you need to add a setting renameDraggedFiles and set it to true.
  4. For now, only relative paths work (= attachment folder begins with ./). For absolute paths to work, it is needed to stop using node.js Path 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.
  5. handlePaste should be rewritten a bit like handleDrop so that it also works on mobile (but only with relative paths as written in 4.).

Elaws avatar Dec 08 '22 14:12 Elaws

Update : Fix does not work with Canvas.

Related : https://github.com/RainCat1998/obsidian-custom-attachment-location/issues/39

Elaws avatar Apr 27 '23 18:04 Elaws

Added corresponding setting in https://github.com/RainCat1998/obsidian-custom-attachment-location/releases/tag/2.1.0

mnaoumov avatar Jul 28 '24 21:07 mnaoumov