123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- var capacitorCamera = (function (exports, core) {
- 'use strict';
- exports.CameraSource = void 0;
- (function (CameraSource) {
- /**
- * Prompts the user to select either the photo album or take a photo.
- */
- CameraSource["Prompt"] = "PROMPT";
- /**
- * Take a new photo using the camera.
- */
- CameraSource["Camera"] = "CAMERA";
- /**
- * Pick an existing photo from the gallery or photo album.
- */
- CameraSource["Photos"] = "PHOTOS";
- })(exports.CameraSource || (exports.CameraSource = {}));
- exports.CameraDirection = void 0;
- (function (CameraDirection) {
- CameraDirection["Rear"] = "REAR";
- CameraDirection["Front"] = "FRONT";
- })(exports.CameraDirection || (exports.CameraDirection = {}));
- exports.CameraResultType = void 0;
- (function (CameraResultType) {
- CameraResultType["Uri"] = "uri";
- CameraResultType["Base64"] = "base64";
- CameraResultType["DataUrl"] = "dataUrl";
- })(exports.CameraResultType || (exports.CameraResultType = {}));
- class CameraWeb extends core.WebPlugin {
- async getPhoto(options) {
- // eslint-disable-next-line no-async-promise-executor
- return new Promise(async (resolve, reject) => {
- if (options.webUseInput || options.source === exports.CameraSource.Photos) {
- this.fileInputExperience(options, resolve, reject);
- }
- else if (options.source === exports.CameraSource.Prompt) {
- let actionSheet = 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) => {
- const selection = e.detail;
- if (selection === 0) {
- this.fileInputExperience(options, resolve, reject);
- }
- else {
- this.cameraExperience(options, resolve, reject);
- }
- });
- }
- else {
- this.cameraExperience(options, resolve, reject);
- }
- });
- }
- async pickImages(_options) {
- // eslint-disable-next-line no-async-promise-executor
- return new Promise(async (resolve, reject) => {
- this.multipleFileInputExperience(resolve, reject);
- });
- }
- async cameraExperience(options, resolve, reject) {
- if (customElements.get('pwa-camera-modal')) {
- const cameraModal = document.createElement('pwa-camera-modal');
- cameraModal.facingMode =
- options.direction === exports.CameraDirection.Front ? 'user' : 'environment';
- document.body.appendChild(cameraModal);
- try {
- await cameraModal.componentOnReady();
- cameraModal.addEventListener('onPhoto', async (e) => {
- const photo = e.detail;
- if (photo === null) {
- reject(new core.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, 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, reject);
- }
- }
- fileInputExperience(options, resolve, reject) {
- let input = document.querySelector('#_capacitor-camera-input');
- const cleanup = () => {
- var _a;
- (_a = input.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(input);
- };
- if (!input) {
- input = document.createElement('input');
- input.id = '_capacitor-camera-input';
- input.type = 'file';
- input.hidden = true;
- document.body.appendChild(input);
- input.addEventListener('change', (_e) => {
- const file = input.files[0];
- let format = 'jpeg';
- if (file.type === 'image/png') {
- format = 'png';
- }
- else if (file.type === 'image/gif') {
- format = 'gif';
- }
- if (options.resultType === 'dataUrl' ||
- options.resultType === 'base64') {
- const reader = new FileReader();
- reader.addEventListener('load', () => {
- if (options.resultType === 'dataUrl') {
- resolve({
- dataUrl: reader.result,
- format,
- });
- }
- else if (options.resultType === 'base64') {
- const b64 = reader.result.split(',')[1];
- resolve({
- base64String: b64,
- format,
- });
- }
- cleanup();
- });
- reader.readAsDataURL(file);
- }
- else {
- resolve({
- webPath: URL.createObjectURL(file),
- format: format,
- });
- cleanup();
- }
- });
- input.addEventListener('cancel', (_e) => {
- reject(new core.CapacitorException('User cancelled photos app'));
- cleanup();
- });
- }
- input.accept = 'image/*';
- input.capture = true;
- if (options.source === exports.CameraSource.Photos ||
- options.source === exports.CameraSource.Prompt) {
- input.removeAttribute('capture');
- }
- else if (options.direction === exports.CameraDirection.Front) {
- input.capture = 'user';
- }
- else if (options.direction === exports.CameraDirection.Rear) {
- input.capture = 'environment';
- }
- input.click();
- }
- multipleFileInputExperience(resolve, reject) {
- let input = document.querySelector('#_capacitor-camera-input-multiple');
- const cleanup = () => {
- var _a;
- (_a = input.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(input);
- };
- if (!input) {
- input = document.createElement('input');
- input.id = '_capacitor-camera-input-multiple';
- input.type = 'file';
- input.hidden = true;
- input.multiple = true;
- document.body.appendChild(input);
- input.addEventListener('change', (_e) => {
- const photos = [];
- // eslint-disable-next-line @typescript-eslint/prefer-for-of
- for (let i = 0; i < input.files.length; i++) {
- const file = input.files[i];
- let format = 'jpeg';
- if (file.type === 'image/png') {
- format = 'png';
- }
- else if (file.type === 'image/gif') {
- format = 'gif';
- }
- photos.push({
- webPath: URL.createObjectURL(file),
- format: format,
- });
- }
- resolve({ photos });
- cleanup();
- });
- input.addEventListener('cancel', (_e) => {
- reject(new core.CapacitorException('User cancelled photos app'));
- cleanup();
- });
- }
- input.accept = 'image/*';
- input.click();
- }
- _getCameraPhoto(photo, options) {
- return new Promise((resolve, reject) => {
- const reader = new FileReader();
- const format = photo.type.split('/')[1];
- if (options.resultType === 'uri') {
- resolve({
- webPath: URL.createObjectURL(photo),
- format: format,
- saved: false,
- });
- }
- else {
- reader.readAsDataURL(photo);
- reader.onloadend = () => {
- const r = reader.result;
- if (options.resultType === 'dataUrl') {
- resolve({
- dataUrl: r,
- format: format,
- saved: false,
- });
- }
- else {
- resolve({
- base64String: r.split(',')[1],
- format: format,
- saved: false,
- });
- }
- };
- reader.onerror = e => {
- reject(e);
- };
- }
- });
- }
- async checkPermissions() {
- if (typeof navigator === 'undefined' || !navigator.permissions) {
- throw this.unavailable('Permissions API not available in this browser');
- }
- try {
- // https://developer.mozilla.org/en-US/docs/Web/API/Permissions/query
- // the specific permissions that are supported varies among browsers that implement the
- // permissions API, so we need a try/catch in case 'camera' is invalid
- const permission = await window.navigator.permissions.query({
- name: 'camera',
- });
- return {
- camera: permission.state,
- photos: 'granted',
- };
- }
- catch (_a) {
- throw this.unavailable('Camera permissions are not available in this browser');
- }
- }
- async requestPermissions() {
- throw this.unimplemented('Not implemented on web.');
- }
- async pickLimitedLibraryPhotos() {
- throw this.unavailable('Not implemented on web.');
- }
- async getLimitedLibraryPhotos() {
- throw this.unavailable('Not implemented on web.');
- }
- }
- new CameraWeb();
- const Camera = core.registerPlugin('Camera', {
- web: () => new CameraWeb(),
- });
- exports.Camera = Camera;
- return exports;
- })({}, capacitorExports);
- //# sourceMappingURL=plugin.js.map
|