Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom notifications in config #159

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -63,6 +64,7 @@ import Uploader from './uploader';
* @property {object} [uploader] - optional custom uploader
* @property {function(File): Promise.<UploadResponseFormat>} [uploader.uploadByFile] - method that upload image by File
* @property {function(string): Promise.<UploadResponseFormat>} [uploader.uploadByUrl] - method that upload image by URL
* @property {function(string): Promise} [notifier] - method that shows notification
*/

/**
Expand Down Expand Up @@ -122,6 +124,7 @@ export default class ImageTool {
buttonContent: config.buttonContent || '',
uploader: config.uploader || undefined,
actions: config.actions || [],
notifier: config.notifier || undefined,
};

/**
Expand Down Expand Up @@ -359,10 +362,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(this.config.notifier) {
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');
}
} else {
this.api.notifier.show({
message: this.api.i18n.t('Couldn’t upload image. Please try another.'),
style: 'error',
});
}
this.ui.hidePreloader();
}

Expand Down
2 changes: 1 addition & 1 deletion src/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}