Skip to content

Commit

Permalink
Copy links at once in Gyazo uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
qawatake committed Mar 19, 2022
1 parent ca74401 commit e69c53b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
76 changes: 43 additions & 33 deletions src/plugins/Gyazo/Gyazo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Gyazo extends MinimalPlugin {
id: 'gyazo-upload',
name: 'Upload to Gyazo',
callback: () => {
this.showImportDialog(this.handleImportedFile);
this.showImportDialog();
},
});
}
Expand Down Expand Up @@ -67,70 +67,80 @@ export class Gyazo extends MinimalPlugin {
});
}

private showImportDialog(cb: HandleImportedFile) {
private showImportDialog() {
const inputEl = createEl('input', { type: 'file' });

inputEl.multiple = true;
inputEl.addEventListener('change', async () => {
const files = inputEl.files;
this.handleImportedFiles(files, cb);
this.handleImportedFiles(files);

inputEl.remove();
});
inputEl.click();
}

private handleImportedFiles(
files: FileList | null,
cb: HandleImportedFile
) {
private async handleImportedFiles(files: FileList | null) {
if (files === null || files.length === 0) {
return;
}

const linksToCopy: string[] = [];
const promises: Promise<void>[] = [];
for (let i = 0; i < files.length; i++) {
const file = files[i];
if (file === undefined) {
continue;
}
cb(file);
}
}
const { api } = this;
if (!api) return;

if (!file.type.startsWith('image/')) {
new Notice(
`[ERROR in Toolbox]: ${file.name} is not an image file.`
);
return;
}

private handleImportedFile = (file: File) => {
if (!file.type.startsWith('image/')) {
new Notice(
`[ERROR in Toolbox]: ${file.name} is not an image file.`
promises.push(
api.upload(
file,
{
app: 'Obsidian',
},
this.onGyazoResponseSuccess(linksToCopy),
this.onGyazoResponseError
)
);
return;
new Notice(`${file.name} uploaded!`);
}

this.api?.upload(
file,
{
app: 'Obsidian',
},
this.onGyazoResponseSuccess,
this.onGyazoResponseError
);
new Notice(`Copy link for uploaded: ${file.name}!`);
};
await Promise.all(promises);
await navigator.clipboard.writeText(linksToCopy.join('\n'));
if (linksToCopy.length === 1) {
new Notice('Copy link!');
} else {
new Notice('Copy links!');
}
}

onGyazoResponseSuccess: GyazoResponseCallback = (result) => {
const gyazoUrl = result.permalink_url;
const imageUrl = result.url;
const embedText = `[![](${imageUrl})](${gyazoUrl})`;
navigator.clipboard.writeText(embedText);
};
private onGyazoResponseSuccess(
linksToCopy: string[]
): GyazoResponseCallback {
return (result) => {
const gyazoUrl = result.permalink_url;
const imageUrl = result.url;
const embedText = `[![](${imageUrl})](${gyazoUrl})`;
linksToCopy.push(embedText);
};
}

onGyazoResponseError: GyazoResponseErrorCallback = (err) => {
new Notice('[ERROR in Toolbox] failed to upload. See console.');
console.log('[ERROR in Toolbox] failed to upload.', err.message);
};
}

type HandleImportedFile = (file: File) => void | Promise<void>;

function isGyazoSettings(obj: unknown): obj is GyazoSettings {
if (typeof obj !== 'object' || obj === null) return false;
const { accessToken } = obj as UnknownObject<GyazoSettings>;
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/Gyazo/GyazoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface GyazoApi {
options: Record<string, string>,
onSuccess: GyazoResponseCallback,
onError: GyazoResponseErrorCallback
): void | Promise<void>;
): Promise<void>;
}

export interface GyazoApiAuth {
Expand Down Expand Up @@ -45,9 +45,9 @@ export class GyazoDirectApi implements GyazoApi {
});
const body = await res.json();
if (res.status === 200 && isGyazoResponseResult(body)) {
onSuccess(body);
await onSuccess(body);
} else if (isGyazoResponseError(body)) {
onError(body);
await onError(body);
} else {
new Notice('[ERROR in Toolbox] Unexpected error. See console.');
console.log('uncaught err:', body);
Expand Down

0 comments on commit e69c53b

Please sign in to comment.