'Capture' cancel on file import
Summary: There's no explicit way of knowing if the file selection dialog was dismissed/canceled without a selection. Instead, subscribe once to the windows focus event, as it will the event will be received when this happens. Reviewed By: antonk52 Differential Revision: D48434187 fbshipit-source-id: dd20c55885bb3ef6bef423e17a2606d71ede288d
This commit is contained in:
committed by
Facebook GitHub Bot
parent
84e0eaa004
commit
b45eed0e9a
@@ -11,6 +11,7 @@ import {
|
|||||||
FlipperServer,
|
FlipperServer,
|
||||||
FlipperServerConfig,
|
FlipperServerConfig,
|
||||||
isProduction,
|
isProduction,
|
||||||
|
uuid,
|
||||||
wrapRequire,
|
wrapRequire,
|
||||||
} from 'flipper-common';
|
} from 'flipper-common';
|
||||||
import type {RenderHost} from 'flipper-ui-core';
|
import type {RenderHost} from 'flipper-ui-core';
|
||||||
@@ -61,14 +62,18 @@ export function initializeRenderHost(
|
|||||||
return new Promise<FileDescriptor | FileDescriptor[] | undefined>(
|
return new Promise<FileDescriptor | FileDescriptor[] | undefined>(
|
||||||
(resolve, reject) => {
|
(resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
|
let selectionMade = false;
|
||||||
|
|
||||||
const fileInput = document.createElement('input');
|
const fileInput = document.createElement('input');
|
||||||
fileInput.type = 'file';
|
fileInput.type = 'file';
|
||||||
|
fileInput.id = uuid();
|
||||||
if (options?.extensions) {
|
if (options?.extensions) {
|
||||||
fileInput.accept = options?.extensions.join(', ');
|
fileInput.accept = options?.extensions.join(', ');
|
||||||
}
|
}
|
||||||
fileInput.multiple = options?.multi ?? false;
|
fileInput.multiple = options?.multi ?? false;
|
||||||
|
|
||||||
fileInput.addEventListener('change', async (event) => {
|
fileInput.addEventListener('change', async (event) => {
|
||||||
|
selectionMade = true;
|
||||||
const target = event.target as HTMLInputElement | undefined;
|
const target = event.target as HTMLInputElement | undefined;
|
||||||
if (!target || !target.files) {
|
if (!target || !target.files) {
|
||||||
resolve(undefined);
|
resolve(undefined);
|
||||||
@@ -106,6 +111,18 @@ export function initializeRenderHost(
|
|||||||
resolve(options?.multi ? descriptors : descriptors[0]);
|
resolve(options?.multi ? descriptors : descriptors[0]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
window.addEventListener(
|
||||||
|
'focus',
|
||||||
|
() => {
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!selectionMade) {
|
||||||
|
resolve(undefined);
|
||||||
|
}
|
||||||
|
}, 300);
|
||||||
|
},
|
||||||
|
{once: true},
|
||||||
|
);
|
||||||
|
|
||||||
fileInput.click();
|
fileInput.click();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
|
|||||||
Reference in New Issue
Block a user