New plugin: VoiceMessages (#1380)

Co-authored-by: V <vendicated@riseup.net>
Co-authored-by: Justice Almanzar <superdash993@gmail.com>
This commit is contained in:
V 2023-07-26 01:27:04 +02:00 committed by GitHub
parent 198b35ffdc
commit 8620a1d86d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 660 additions and 37 deletions

View file

@ -16,6 +16,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Prompts the user to save a file to their system
* @param file The file to save
*/
export function saveFile(file: File) {
const a = document.createElement("a");
a.href = URL.createObjectURL(file);
@ -28,3 +32,24 @@ export function saveFile(file: File) {
document.body.removeChild(a);
});
}
/**
* Prompts the user to choose a file from their system
* @param mimeTypes A comma separated list of mime types to accept, see https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept#unique_file_type_specifiers
* @returns A promise that resolves to the chosen file or null if the user cancels
*/
export function chooseFile(mimeTypes: string) {
return new Promise<File | null>(resolve => {
const input = document.createElement("input");
input.type = "file";
input.style.display = "none";
input.accept = mimeTypes;
input.onchange = async () => {
resolve(input.files?.[0] ?? null);
};
document.body.appendChild(input);
input.click();
setImmediate(() => document.body.removeChild(input));
});
}