Skip to content

Commit

Permalink
fix(camera): Make web use source options (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Jun 23, 2021
1 parent c39ead4 commit 7870e6b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
2 changes: 1 addition & 1 deletion camera/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Request camera and photo album permissions
| **`direction`** | <code><a href="#cameradirection">CameraDirection</a></code> | iOS and Web only: The camera direction. | <code>: CameraDirection.Rear</code> | 1.0.0 |
| **`presentationStyle`** | <code>'fullscreen' \| 'popover'</code> | iOS only: The presentation style of the Camera. | <code>: 'fullscreen'</code> | 1.0.0 |
| **`webUseInput`** | <code>boolean</code> | Web only: Whether to use the PWA Element experience or file input. The default is to use PWA Elements if installed and fall back to file input. To always use file input, set this to `true`. Learn more about PWA Elements: https://capacitorjs.com/docs/pwa-elements | | 1.0.0 |
| **`promptLabelHeader`** | <code>string</code> | Text value to use when displaying the prompt. iOS and Android only: The title of the action sheet. | <code>: 'Photo'</code> | 1.0.0 |
| **`promptLabelHeader`** | <code>string</code> | Text value to use when displaying the prompt. | <code>: 'Photo'</code> | 1.0.0 |
| **`promptLabelCancel`** | <code>string</code> | Text value to use when displaying the prompt. iOS only: The label of the 'cancel' button. | <code>: 'Cancel'</code> | 1.0.0 |
| **`promptLabelPhoto`** | <code>string</code> | Text value to use when displaying the prompt. The label of the button to select a saved image. | <code>: 'From Photos'</code> | 1.0.0 |
| **`promptLabelPicture`** | <code>string</code> | Text value to use when displaying the prompt. The label of the button to open the camera. | <code>: 'Take Picture'</code> | 1.0.0 |
Expand Down
1 change: 0 additions & 1 deletion camera/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export interface ImageOptions {

/**
* Text value to use when displaying the prompt.
* iOS and Android only: The title of the action sheet.
* @default: 'Photo'
*
* @since 1.0.0
Expand Down
88 changes: 58 additions & 30 deletions camera/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,71 @@ export class CameraWeb extends WebPlugin implements CameraPlugin {
async getPhoto(options: ImageOptions): Promise<Photo> {
// eslint-disable-next-line no-async-promise-executor
return new Promise<Photo>(async (resolve, reject) => {
if (options.webUseInput) {
if (options.webUseInput || options.source === CameraSource.Photos) {
this.fileInputExperience(options, resolve);
} else {
if (customElements.get('pwa-camera-modal')) {
const cameraModal: any = document.createElement('pwa-camera-modal');
document.body.appendChild(cameraModal);
try {
await cameraModal.componentOnReady();
cameraModal.addEventListener('onPhoto', async (e: any) => {
const photo = e.detail;

if (photo === null) {
reject(new CapacitorException('User cancelled photos app'));
} else if (photo instanceof Error) {
reject(photo);
} else {
resolve(await this._getCameraPhoto(photo, options));
}

cameraModal.dismiss();
document.body.removeChild(cameraModal);
});

cameraModal.present();
} catch (e) {
} else if (options.source === CameraSource.Prompt) {
let actionSheet: any = document.querySelector('pwa-action-sheet');
if (!actionSheet) {
actionSheet = document.createElement('pwa-action-sheet');
document.body.appendChild(actionSheet);
}
actionSheet.header = options.promptLabelHeader || 'Photo';
actionSheet.cancelable = false;
actionSheet.options = [
{ title: options.promptLabelPhoto || 'From Photos' },
{ title: options.promptLabelPicture || 'Take Picture' },
];
actionSheet.addEventListener('onSelection', async (e: any) => {
const selection = e.detail;
if (selection === 0) {
this.fileInputExperience(options, resolve);
} else {
this.cameraExperience(options, resolve, reject);
}
} else {
console.error(
`Unable to load PWA Element 'pwa-camera-modal'. See the docs: https://capacitorjs.com/docs/pwa-elements.`,
);
this.fileInputExperience(options, resolve);
}
});
} else {
this.cameraExperience(options, resolve, reject);
}
});
}

private async cameraExperience(
options: ImageOptions,
resolve: any,
reject: any,
) {
if (customElements.get('pwa-camera-modal')) {
const cameraModal: any = document.createElement('pwa-camera-modal');
document.body.appendChild(cameraModal);
try {
await cameraModal.componentOnReady();
cameraModal.addEventListener('onPhoto', async (e: any) => {
const photo = e.detail;

if (photo === null) {
reject(new CapacitorException('User cancelled photos app'));
} else if (photo instanceof Error) {
reject(photo);
} else {
resolve(await this._getCameraPhoto(photo, options));
}

cameraModal.dismiss();
document.body.removeChild(cameraModal);
});

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

private fileInputExperience(options: ImageOptions, resolve: any) {
let input = document.querySelector(
'#_capacitor-camera-input',
Expand Down

0 comments on commit 7870e6b

Please sign in to comment.