From 6c8ce85ea97850ed75dcf1bc4b15a2e761f05c91 Mon Sep 17 00:00:00 2001 From: roipoussiere Date: Thu, 7 Mar 2019 20:41:45 +0100 Subject: [PATCH] Add errorCallback option --- README.md | 4 ++-- src/js/easymde.js | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a00e12e..18de629 100644 --- a/README.md +++ b/README.md @@ -159,12 +159,12 @@ easyMDE.value('New input for **EasyMDE**'); - **sbProgress**: Status message displayed to show uploading progress. Defaults to `Uploading #file_name#: #progress#%`. - **sbOnUploaded**: Status message displayed when the image has been uploaded. Defaults to `Uploaded #image_name#`. - **sizeUnits**: A comma-separated list of units used to display messages with human-readable file sizes. Defaults to `b,Kb,Mb`. -- **errorMessages**: Errors displayed to the user, mainly on alert popups, where `#image_name#`, `#image_size#` and `#image_max_size#` will replaced by their respective values, that can be used for customization or internationalization: +- **errorMessages**: Errors displayed to the user, using the `errorCallback` option, where `#image_name#`, `#image_size#` and `#image_max_size#` will replaced by their respective values, that can be used for customization or internationalization: - **noFileGiven**: The server did not receive any file from the user. Defaults to `You must select a file.`. - **imageTypeNotAllowed**: The user send a file type which doesn't match the `imageAccept` list, or the server returned this error code. Defaults to `This image type is not allowed.`. - **imageTooLarge**: The size of the image being imported is bigger than the `imageMaxSize`, or if the server returned this error code. Defaults to `Image #image_name# is too big (#image_size#).\nMaximum file size is #image_max_size#.`. - **imageImportError**: An unexpected error occurred when uploading the image. Defaults to `Something went wrong when uploading the image #image_name#.`. - +- **errorCallback**: A callback function used to define how to display an error message. Defaults to `function(errorMessage) {alert(errorMessage);};`. - **renderingConfig**: Adjust settings for parsing the Markdown during previewing (not editing). - **codeSyntaxHighlighting**: If set to `true`, will highlight using [highlight.js](https://github.com/isagalaev/highlight.js). Defaults to `false`. To use this feature you must include highlight.js on your page or pass in using the `hljs` option. For example, include the script and the CSS files like:
``
`` - **hljs**: An injectible instance of [highlight.js](https://github.com/isagalaev/highlight.js). If you don't want to rely on the global namespace (`window.hljs`), you can provide an instance here. Defaults to `undefined`. diff --git a/src/js/easymde.js b/src/js/easymde.js index 51fd564..17a58d3 100644 --- a/src/js/easymde.js +++ b/src/js/easymde.js @@ -1426,7 +1426,7 @@ var imageTexts = { }; /** - * Errors displayed to the user, mainly on alert popups. Can be used for + * Errors displayed to the user, using the `errorCallback` option. Can be used for * customization or internationalization. */ var errorMessages = { @@ -1547,6 +1547,9 @@ function EasyMDE(options) { options.minHeight = options.minHeight || '300px'; + options.errorCallback = options.errorCallback || function(errorMessage) { + alert(errorMessage); + }; // Import-image default configuration options.uploadImage = options.uploadImage || false; @@ -1621,7 +1624,7 @@ EasyMDE.prototype.uploadImages = function(files) { this.uploadImage(files[i], function onSuccess(imageUrl) { afterImageUploaded(self, imageUrl); }, function onFailure(error) { - alert(error); + self.options.errorCallback(error); }); } this.updateStatusBar('upload-image', self.options.imageTexts.sbOnDrop.replace('#images_names#', names.join(', '))); @@ -1831,22 +1834,22 @@ EasyMDE.prototype.autosave = function () { console.log('EasyMDE: You must set a uniqueId to use the autosave feature'); return; } - + if(this.options.autosave.binded !== true) { if (easyMDE.element.form != null && easyMDE.element.form != undefined) { easyMDE.element.form.addEventListener('submit', function () { clearTimeout(easyMDE.autosaveTimeoutId); easyMDE.autosaveTimeoutId = undefined; - + localStorage.removeItem('smde_' + easyMDE.options.autosave.uniqueId); - + // Restart autosaving in case the submit will be cancelled down the line setTimeout(function() { easyMDE.autosave(); }, easyMDE.options.autosave.delay || 10000); }); } - + this.options.autosave.binded = true; }