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

fix(camera): reject promise on web input cancel event #1958

Merged
merged 4 commits into from
Dec 14, 2023
Merged
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
28 changes: 20 additions & 8 deletions camera/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// eslint-disable-next-line no-async-promise-executor
return new Promise<Photo>(async (resolve, reject) => {
if (options.webUseInput || options.source === CameraSource.Photos) {
this.fileInputExperience(options, resolve);
this.fileInputExperience(options, resolve, reject);
} else if (options.source === CameraSource.Prompt) {
let actionSheet: any = document.querySelector('pwa-action-sheet');
if (!actionSheet) {
Expand All @@ -31,7 +31,7 @@
actionSheet.addEventListener('onSelection', async (e: any) => {
const selection = e.detail;
if (selection === 0) {
this.fileInputExperience(options, resolve);
this.fileInputExperience(options, resolve, reject);
} else {
this.cameraExperience(options, resolve, reject);
}
Expand All @@ -42,10 +42,10 @@
});
}

async pickImages(_options: GalleryImageOptions): Promise<GalleryPhotos> {

Check warning on line 45 in camera/src/web.ts

View workflow job for this annotation

GitHub Actions / lint (camera)

'_options' is defined but never used
// eslint-disable-next-line no-async-promise-executor
return new Promise<GalleryPhotos>(async resolve => {
this.multipleFileInputExperience(resolve);
return new Promise<GalleryPhotos>(async (resolve, reject) => {
this.multipleFileInputExperience(resolve, reject);
});
}

Expand Down Expand Up @@ -78,17 +78,21 @@

cameraModal.present();
} catch (e) {
this.fileInputExperience(options, resolve);
this.fileInputExperience(options, resolve, reject);
}
} else {
console.error(
`Unable to load PWA Element 'pwa-camera-modal'. See the docs: https://capacitorjs.com/docs/web/pwa-elements.`,
);
this.fileInputExperience(options, resolve);
this.fileInputExperience(options, resolve, reject);
}
}

private fileInputExperience(options: ImageOptions, resolve: any) {
private fileInputExperience(
options: ImageOptions,
resolve: any,
reject: any,
) {
let input = document.querySelector(
'#_capacitor-camera-input',
) as HTMLInputElement;
Expand All @@ -103,8 +107,8 @@
input.type = 'file';
input.hidden = true;
document.body.appendChild(input);
input.addEventListener('change', (_e: any) => {

Check warning on line 110 in camera/src/web.ts

View workflow job for this annotation

GitHub Actions / lint (camera)

'_e' is defined but never used
const file = input.files![0];

Check warning on line 111 in camera/src/web.ts

View workflow job for this annotation

GitHub Actions / lint (camera)

Forbidden non-null assertion
let format = 'jpeg';

if (file.type === 'image/png') {
Expand Down Expand Up @@ -145,6 +149,10 @@
cleanup();
}
});
input.addEventListener('cancel', (_e: any) => {

Check warning on line 152 in camera/src/web.ts

View workflow job for this annotation

GitHub Actions / lint (camera)

'_e' is defined but never used
reject(new CapacitorException('User cancelled photos app'));
cleanup();
});
}

input.accept = 'image/*';
Expand All @@ -164,7 +172,7 @@
input.click();
}

private multipleFileInputExperience(resolve: any) {
private multipleFileInputExperience(resolve: any, reject: any) {
let input = document.querySelector(
'#_capacitor-camera-input-multiple',
) as HTMLInputElement;
Expand All @@ -180,11 +188,11 @@
input.hidden = true;
input.multiple = true;
document.body.appendChild(input);
input.addEventListener('change', (_e: any) => {

Check warning on line 191 in camera/src/web.ts

View workflow job for this annotation

GitHub Actions / lint (camera)

'_e' is defined but never used
const photos = [];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < input.files!.length; i++) {

Check warning on line 194 in camera/src/web.ts

View workflow job for this annotation

GitHub Actions / lint (camera)

Forbidden non-null assertion
const file = input.files![i];

Check warning on line 195 in camera/src/web.ts

View workflow job for this annotation

GitHub Actions / lint (camera)

Forbidden non-null assertion
let format = 'jpeg';

if (file.type === 'image/png') {
Expand All @@ -200,6 +208,10 @@
resolve({ photos });
cleanup();
});
input.addEventListener('cancel', (_e: any) => {

Check warning on line 211 in camera/src/web.ts

View workflow job for this annotation

GitHub Actions / lint (camera)

'_e' is defined but never used
reject(new CapacitorException('User cancelled photos app'));
cleanup();
});
}

input.accept = 'image/*';
Expand Down
Loading