Skip to content

Commit

Permalink
fix multi paste handler issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanhandy committed May 23, 2024
1 parent fe855f3 commit 69301cf
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const DEFAULT_SETTINGS: CloudinarySettings = {
export default class CloudinaryUploader extends Plugin {
settings: CloudinarySettings;

private clearHandlers(){
this.app.workspace.off('editor-paste',this.pasteHandler);
this.app.workspace.off('editor-drop',this.dropHandler);
}

private setupHandlers(){
if(this.settings.clipboardUpload){
this.registerEvent(this.app.workspace.on('editor-paste',this.pasteHandler));
Expand All @@ -64,12 +69,17 @@ export default class CloudinaryUploader extends Plugin {
}

private uploadFiles = async (files: FileList,event,editor) => {
// On paste event, get "files" from clipbaord data

// On paste event, get "files" from clipbaord or drag data
// If files contain image, move to API call
// if Files empty or does not contain image, throw error
if(files.length > 0){
if((this.settings.audioUpload && files[0].type.startsWith("audio")) ||
(this.settings.videoUpload && files[0].type.startsWith('video')) ||
(this.settings.imageUpload && files[0].type.startsWith('image'))){
event.preventDefault(); // Prevent default paste behaviour

if (this.settings.cloudName && this.settings.uploadPreset) {
event.preventDefault(); // Prevent default paste behaviour
for (let file of files) {
const randomString = (Math.random() * 10086).toString(36).substr(0, 8)
const pastePlaceText = `![uploading...](${randomString})\n`
Expand Down Expand Up @@ -125,6 +135,7 @@ export default class CloudinaryUploader extends Plugin {
}
}
}
}
}
// Function to replace text
private replaceText(editor: Editor, target: string, replacement: string): void {
Expand All @@ -148,6 +159,7 @@ export default class CloudinaryUploader extends Plugin {
async onload(): Promise<void> {
console.log("loading Cloudinary Uploader");
await this.loadSettings();
this.clearHandlers();
this.setupHandlers();
//this.setupPasteHandler();
this.addSettingTab(new CloudinaryUploaderSettingTab(this.app, this));
Expand All @@ -156,16 +168,18 @@ export default class CloudinaryUploader extends Plugin {
// Plugin shutdown steps
onunload(): void {
console.log("unloading Cloudinary Uploader");
this.clearHandlers();

}
// Load settings infromation
// Load settings information
async loadSettings(): Promise<void> {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

// When saving settings
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
this.clearHandlers();
this.setupHandlers();
}
}

0 comments on commit 69301cf

Please sign in to comment.