Skip to content

Commit

Permalink
use editor from event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanhandy committed Nov 18, 2021
1 parent 082ca5f commit 465b125
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
18 changes: 5 additions & 13 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2650,26 +2650,18 @@ var DEFAULT_SETTINGS = {
folder: null
};
var CloudinaryUploader = class extends import_obsidian2.Plugin {
getEditor() {
const mdView = this.app.workspace.getActiveViewOfType(import_obsidian2.MarkdownView);
if (mdView) {
return mdView.editor;
} else {
return null;
}
}
setupPasteHandler() {
this.registerEvent(this.app.workspace.on("editor-paste", async (evt) => {
this.registerEvent(this.app.workspace.on("editor-paste", async (evt, editor) => {
const {files} = evt.clipboardData;
if (files.length == 0 || !files[0].type.startsWith("image")) {
this.getEditor().replaceSelection("Clipboard data is not an image\n");
editor.replaceSelection("Clipboard data is not an image\n");
} else if (this.settings.cloudName && this.settings.uploadPreset) {
for (let file of files) {
evt.preventDefault();
const randomString = (Math.random() * 10086).toString(36).substr(0, 8);
const pastePlaceText = `![uploading...](${randomString})
`;
this.getEditor().replaceSelection(pastePlaceText);
editor.replaceSelection(pastePlaceText);
const formData = new FormData();
formData.append("file", file);
formData.append("upload_preset", this.settings.uploadPreset);
Expand All @@ -2681,15 +2673,15 @@ var CloudinaryUploader = class extends import_obsidian2.Plugin {
}).then((res) => {
const url = import_object_path.default.get(res.data, "secure_url");
const imgMarkdownText = `![](${url})`;
this.replaceText(this.getEditor(), pastePlaceText, imgMarkdownText);
this.replaceText(editor, pastePlaceText, imgMarkdownText);
}, (err) => {
new import_obsidian2.Notice(err, 5e3);
console.log(err);
});
}
} else {
new import_obsidian2.Notice("Cloudinary Image Uploader: Please check the image hosting settings.");
this.getEditor().replaceSelection("Please check settings for upload");
editor.replaceSelection("Please check settings for upload");
}
}));
}
Expand Down
19 changes: 5 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Notice,
Plugin,
MarkdownView,
Editor,
} from "obsidian";

Expand All @@ -27,28 +26,20 @@ const DEFAULT_SETTINGS: CloudinarySettings = {
export default class CloudinaryUploader extends Plugin {
settings: CloudinarySettings;

private getEditor() {
const mdView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (mdView) {
return mdView.editor;
} else {
return null;
}
}

setupPasteHandler(): void {
this.registerEvent(this.app.workspace.on('editor-paste',async (evt: ClipboardEvent)=>{
this.registerEvent(this.app.workspace.on('editor-paste',async (evt: ClipboardEvent, editor: Editor)=>{
const { files } = evt.clipboardData;
if (files.length == 0 || !files[0].type.startsWith("image")) {
this.getEditor().replaceSelection("Clipboard data is not an image\n");
editor.replaceSelection("Clipboard data is not an image\n");
}
else if (this.settings.cloudName && this.settings.uploadPreset) {
for (let file of files) {
evt.preventDefault();

const randomString = (Math.random() * 10086).toString(36).substr(0, 8)
const pastePlaceText = `![uploading...](${randomString})\n`
this.getEditor().replaceSelection(pastePlaceText)
editor.replaceSelection(pastePlaceText)

const formData = new FormData();
formData.append('file',file);
Expand All @@ -62,7 +53,7 @@ export default class CloudinaryUploader extends Plugin {
}).then(res => {
const url = objectPath.get(res.data, 'secure_url')
const imgMarkdownText = `![](${url})`
this.replaceText(this.getEditor(), pastePlaceText, imgMarkdownText)
this.replaceText(editor, pastePlaceText, imgMarkdownText)
}, err => {
new Notice(err, 5000)
console.log(err)
Expand All @@ -71,7 +62,7 @@ export default class CloudinaryUploader extends Plugin {
}
else {
new Notice("Cloudinary Image Uploader: Please check the image hosting settings.");
this.getEditor().replaceSelection("Please check settings for upload");
editor.replaceSelection("Please check settings for upload");
}

}))
Expand Down

0 comments on commit 465b125

Please sign in to comment.