Skip to content

Commit

Permalink
Uploading existing images WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gavvvr committed Nov 15, 2021
1 parent 6a65af7 commit 583e1e5
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/ImgurPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/* eslint-disable no-param-reassign */
/* eslint-disable no-underscore-dangle */
import { Editor, MarkdownView, Notice, Plugin } from "obsidian";
import {
Editor,
MarkdownView,
Menu,
Notice,
parseLinktext,
Plugin,
View,
} from "obsidian";
import * as CodeMirror from "codemirror";
import ImageUploader from "./uploader/ImageUploader";
// eslint-disable-next-line import/no-cycle
Expand Down Expand Up @@ -222,6 +230,48 @@ export default class ImgurPlugin extends Plugin {
}
};
});

this.registerEvent(
this.app.workspace.on(
"editor-menu",
(menu: Menu, editor: Editor, view: MarkdownView) => {
menu.addItem((item) => {
item
.setTitle("Upload to Imgur")
.setIcon("wand")
.onClick(async () => {
const clickable = editor.getClickableTokenAt(
editor.getCursor()
);

const lt = parseLinktext(clickable.text);
const file = this.app.metadataCache.getFirstLinkpathDest(
lt.path,
view.file.path
);

const arrayBuffer = await this.app.vault.readBinary(file);

const fileToUpload = new File([arrayBuffer], file.name);

// console.log(blob);

this.uploadFileAndEmbedImgurImage(fileToUpload)
.then(() => view.app.vault.trash(file, true))
.catch((ee) => console.log(ee));

// console.log(file);
// console.log(this.app.metadataCache.getFileCache(file.path));

// this.app.vault
// .trash(file, true)
// .then(() => {})
// .catch(() => {});
});
});
}
)
);
}

private static composeNewDragEvent(
Expand Down

0 comments on commit 583e1e5

Please sign in to comment.