From d7c0b2aa227e11eee7a5a4b1c6f43aff8d640012 Mon Sep 17 00:00:00 2001 From: ramil Date: Mon, 23 Aug 2021 11:27:47 +0500 Subject: [PATCH 1/4] Add notifier overload --- src/index.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 20389702..bd90ae08 100644 --- a/src/index.js +++ b/src/index.js @@ -63,6 +63,7 @@ import Uploader from './uploader'; * @property {object} [uploader] - optional custom uploader * @property {function(File): Promise.} [uploader.uploadByFile] - method that upload image by File * @property {function(string): Promise.} [uploader.uploadByUrl] - method that upload image by URL + * @property {function(string): Promise} [notifier] - method that shows notification */ /** @@ -122,6 +123,7 @@ export default class ImageTool { buttonContent: config.buttonContent || '', uploader: config.uploader || undefined, actions: config.actions || [], + notifier: config.notifier || undefined, }; /** @@ -359,10 +361,18 @@ export default class ImageTool { uploadingFailed(errorText) { console.log('Image Tool: uploading failed because of', errorText); - this.api.notifier.show({ - message: this.api.i18n.t('Couldn’t upload image. Please try another.'), - style: 'error', - }); + if(notifier && typeof notifier === 'function') { + notification = notifier(this.api.i18n.t('Couldn’t upload image. Please try another.')) + + if (!isPromise(notification)) { + console.warn('Custom notification method should return a Promise'); + } + } else { + this.api.notifier.show({ + message: this.api.i18n.t('Couldn’t upload image. Please try another.'), + style: 'error', + }); + } this.ui.hidePreloader(); } From 483af2d9472c9171e5077d71ecdb60d03e5cee92 Mon Sep 17 00:00:00 2001 From: ramil Date: Mon, 23 Aug 2021 18:58:53 +0500 Subject: [PATCH 2/4] fix name --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index bd90ae08..3bcba0db 100644 --- a/src/index.js +++ b/src/index.js @@ -361,9 +361,9 @@ export default class ImageTool { uploadingFailed(errorText) { console.log('Image Tool: uploading failed because of', errorText); - if(notifier && typeof notifier === 'function') { - notification = notifier(this.api.i18n.t('Couldn’t upload image. Please try another.')) - + if(this.config.notifier) { + notification = this.config.notifier(this.api.i18n.t('Couldn’t upload image. Please try another.')) + if (!isPromise(notification)) { console.warn('Custom notification method should return a Promise'); } From 0b7c785eb02315d70de498a22c24a7a60500fac5 Mon Sep 17 00:00:00 2001 From: ramil Date: Mon, 23 Aug 2021 19:40:07 +0500 Subject: [PATCH 3/4] add let word i forgot --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3bcba0db..0cc1af2c 100644 --- a/src/index.js +++ b/src/index.js @@ -362,7 +362,7 @@ export default class ImageTool { console.log('Image Tool: uploading failed because of', errorText); if(this.config.notifier) { - notification = this.config.notifier(this.api.i18n.t('Couldn’t upload image. Please try another.')) + let notification = this.config.notifier(this.api.i18n.t('Couldn’t upload image. Please try another.')) if (!isPromise(notification)) { console.warn('Custom notification method should return a Promise'); From 6d5b4c1a62eee956119394d6e4106f97df698bee Mon Sep 17 00:00:00 2001 From: ramil Date: Mon, 23 Aug 2021 21:30:46 +0500 Subject: [PATCH 4/4] Add isPromise import i forgot to add AGAIN --- src/index.js | 1 + src/uploader.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 0cc1af2c..1ece1b35 100644 --- a/src/index.js +++ b/src/index.js @@ -47,6 +47,7 @@ import Ui from './ui'; import Tunes from './tunes'; import ToolboxIcon from './svg/toolbox.svg'; import Uploader from './uploader'; +import { isPromise } from './uploader'; /** * @typedef {object} ImageConfig diff --git a/src/uploader.js b/src/uploader.js index 98752a41..200cc2e8 100644 --- a/src/uploader.js +++ b/src/uploader.js @@ -182,6 +182,6 @@ export default class Uploader { * @param {*} object - object to check * @returns {boolean} */ -function isPromise(object) { +export function isPromise(object) { return object && typeof object.then === "function"; }