From def93bce74c58d55ab942eff080546db211ada0a Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 11 Dec 2024 16:49:33 +0800 Subject: [PATCH] Add support for cancel event in file selection input --- dev/example.html | 2 +- src/utils.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/dev/example.html b/dev/example.html index 7c62757..8d06ca3 100644 --- a/dev/example.html +++ b/dev/example.html @@ -206,7 +206,7 @@ progressBar.classList.add('hidden'); - message.innerText = '❌ Error ' + response.code; + message.innerText = '❌ Error ' + (typeof response === 'string' ? response : response.code); message.classList.remove('hidden'); spinner.classList.add('hidden'); diff --git a/src/utils.js b/src/utils.js index 2022b60..e958f24 100644 --- a/src/utils.js +++ b/src/utils.js @@ -169,6 +169,26 @@ module.exports = class Utils { document.body.removeChild(inputElement); }, false); + /** + * Detect if the browser supports the `cancel` event + */ + if ('oncancel' in inputElement) { + /** + * Add oncancel listener for «choose file» pop-up + */ + inputElement.addEventListener('cancel', () => { + /** + * Reject promise if user canceled file selection + */ + reject('User canceled file selection'); + + /** + * Remove element from a DOM + */ + document.body.removeChild(inputElement); + }); + } + /** * Fire click event on «input file» field */