esm-BJMrlu_7.js

9.5 KB · raw

1import{a as e,n as t,r as n}from"./dist-Cgg5UYwv.js";var r;(function(e){e.Prompt=`PROMPT`,e.Camera=`CAMERA`,e.Photos=`PHOTOS`})(r||={});var i;(function(e){e.Rear=`REAR`,e.Front=`FRONT`})(i||={});var a;(function(e){e.Uri=`uri`,e.Base64=`base64`,e.DataUrl=`dataUrl`})(a||={});var o;(function(e){e[e.Photo=0]=`Photo`,e[e.Video=1]=`Video`})(o||={});var s;(function(e){e[e.Photo=0]=`Photo`,e[e.Video=1]=`Video`,e[e.All=2]=`All`})(s||={});var c;(function(e){e[e.JPEG=0]=`JPEG`,e[e.PNG=1]=`PNG`})(c||={});var l;(function(e){e.CameraPermissionDenied=`OS-PLUG-CAMR-0003`,e.GalleryPermissionDenied=`OS-PLUG-CAMR-0005`,e.NoCameraAvailable=`OS-PLUG-CAMR-0007`,e.TakePhotoCancelled=`OS-PLUG-CAMR-0006`,e.TakePhotoFailed=`OS-PLUG-CAMR-0010`,e.TakePhotoInvalidArguments=`OS-PLUG-CAMR-0014`,e.InvalidImageData=`OS-PLUG-CAMR-0008`,e.EditPhotoFailed=`OS-PLUG-CAMR-0009`,e.EditPhotoCancelled=`OS-PLUG-CAMR-0013`,e.EditPhotoEmptyUri=`OS-PLUG-CAMR-0024`,e.ImageNotFound=`OS-PLUG-CAMR-0011`,e.ProcessImageFailed=`OS-PLUG-CAMR-0012`,e.ChooseMediaFailed=`OS-PLUG-CAMR-0018`,e.ChooseMediaCancelled=`OS-PLUG-CAMR-0020`,e.MediaPathError=`OS-PLUG-CAMR-0021`,e.FetchImageFromUriFailed=`OS-PLUG-CAMR-0028`,e.RecordVideoFailed=`OS-PLUG-CAMR-0016`,e.RecordVideoCancelled=`OS-PLUG-CAMR-0017`,e.VideoNotFound=`OS-PLUG-CAMR-0025`,e.PlayVideoFailed=`OS-PLUG-CAMR-0023`,e.EncodeResultFailed=`OS-PLUG-CAMR-0019`,e.FileNotFound=`OS-PLUG-CAMR-0027`,e.InvalidArgument=`OS-PLUG-CAMR-0031`,e.GeneralError=`OS-PLUG-CAMR-0026`})(l||={});var u=class extends n{async takePhoto(e){return new Promise(async(t,n)=>{e.webUseInput?this.takePhotoCameraInputExperience(e,t,n):this.takePhotoCameraExperience(e,t,n)})}async recordVideo(e){throw this.unimplemented(`recordVideo is not implemented on Web.`)}async playVideo(e){throw this.unimplemented(`playVideo is not implemented on Web.`)}async chooseFromGallery(e){return new Promise(async(t,n)=>{this.galleryInputExperience(e,t,n)})}async editPhoto(e){throw this.unimplemented(`editPhoto is not implemented on Web.`)}async editURIPhoto(e){throw this.unimplemented(`editURIPhoto is not implemented on Web.`)}async getPhoto(e){return new Promise(async(t,n)=>{if(e.webUseInput||e.source===r.Photos)this.fileInputExperience(e,t,n);else if(e.source===r.Prompt){let r=document.querySelector(`pwa-action-sheet`);r||(r=document.createElement(`pwa-action-sheet`),document.body.appendChild(r)),r.header=e.promptLabelHeader||`Photo`,r.cancelable=!1,r.options=[{title:e.promptLabelPhoto||`From Photos`},{title:e.promptLabelPicture||`Take Picture`}],r.addEventListener(`onSelection`,async r=>{r.detail===0?this.fileInputExperience(e,t,n):this.cameraExperience(e,t,n)})}else this.cameraExperience(e,t,n)})}async pickImages(e){return new Promise(async(e,t)=>{this.multipleFileInputExperience(e,t)})}async cameraExperience(e,t,n){await this._setupPWACameraModal(e.direction,t=>this._getCameraPhoto(t,e),()=>this.fileInputExperience(e,t,n),t,n)}fileInputExperience(e,n,a){let o=document.querySelector(`#_capacitor-camera-input`),s=()=>{var e;(e=o.parentNode)==null||e.removeChild(o)};o||(o=document.createElement(`input`),o.id=`_capacitor-camera-input`,o.type=`file`,o.hidden=!0,document.body.appendChild(o),o.addEventListener(`change`,t=>{let r=o.files[0],i=`jpeg`;if(r.type===`image/png`?i=`png`:r.type===`image/gif`&&(i=`gif`),e.resultType===`dataUrl`||e.resultType===`base64`){let t=new FileReader;t.addEventListener(`load`,()=>{if(e.resultType===`dataUrl`)n({dataUrl:t.result,format:i});else if(e.resultType===`base64`){let e=t.result.split(`,`)[1];n({base64String:e,format:i})}s()}),t.readAsDataURL(r)}else n({webPath:URL.createObjectURL(r),format:i}),s()}),o.addEventListener(`cancel`,e=>{a(new t(`User cancelled photos app`)),s()})),o.accept=`image/*`,o.capture=!0,e.source===r.Photos||e.source===r.Prompt?o.removeAttribute(`capture`):e.direction===i.Front?o.capture=`user`:e.direction===i.Rear&&(o.capture=`environment`),o.click()}multipleFileInputExperience(e,n){let r=document.querySelector(`#_capacitor-camera-input-multiple`),i=()=>{var e;(e=r.parentNode)==null||e.removeChild(r)};r||(r=document.createElement(`input`),r.id=`_capacitor-camera-input-multiple`,r.type=`file`,r.hidden=!0,r.multiple=!0,document.body.appendChild(r),r.addEventListener(`change`,t=>{let n=[];for(let e=0;e<r.files.length;e++){let t=r.files[e],i=`jpeg`;t.type===`image/png`?i=`png`:t.type===`image/gif`&&(i=`gif`),n.push({webPath:URL.createObjectURL(t),format:i})}e({photos:n}),i()}),r.addEventListener(`cancel`,e=>{n(new t(`User cancelled photos app`)),i()})),r.accept=`image/*`,r.click()}_getCameraPhoto(e,t){return new Promise((n,r)=>{let i=new FileReader,a=this._getFileFormat(e);t.resultType===`uri`?n({webPath:URL.createObjectURL(e),format:a,saved:!1}):(i.readAsDataURL(e),i.onloadend=()=>{let e=i.result;t.resultType===`dataUrl`?n({dataUrl:e,format:a,saved:!1}):n({base64String:e.split(`,`)[1],format:a,saved:!1})},i.onerror=e=>{r(e)})})}async takePhotoCameraExperience(e,t,n){await this._setupPWACameraModal(e.cameraDirection,t=>this._buildPhotoMediaResult(t,e.includeMetadata??!1),()=>this.takePhotoCameraInputExperience(e,t,n),t,n)}takePhotoCameraInputExperience(e,n,r){let a=this._createFileInput(`_capacitor-camera-input-takephoto`),o=()=>{var e;(e=a.parentNode)==null||e.removeChild(a)};a.onchange=async t=>{if(!this._validateFileInput(a,r,o))return;let i=a.files[0];n(await this._buildPhotoMediaResult(i,e.includeMetadata??!1)),o()},a.oncancel=()=>{r(new t(`User cancelled photos app`)),o()},a.accept=`image/*`,e.cameraDirection===i.Front?a.capture=`user`:a.capture=`environment`,a.click()}galleryInputExperience(e,n,r){let i=this._createFileInput(`_capacitor-camera-input-gallery`);i.multiple=e.allowMultipleSelection??!1;let a=()=>{var e;(e=i.parentNode)==null||e.removeChild(i)};i.onchange=async t=>{if(!this._validateFileInput(i,r,a))return;let s=[];for(let t=0;t<i.files.length;t++){let n=i.files[t];if(n.type.startsWith(`image/`))s.push(await this._buildPhotoMediaResult(n,e.includeMetadata??!1));else if(n.type.startsWith(`video/`)){let t=this._getFileFormat(n),r,i,a;try{let t=await this._getVideoMetadata(n);r=t.thumbnail,e.includeMetadata&&(i=t.resolution,a=t.duration)}catch(e){console.warn(`Failed to get video metadata:`,e)}let c={type:o.Video,thumbnail:r,webPath:URL.createObjectURL(n),saved:!1};e.includeMetadata&&(c.metadata={format:t,resolution:i,size:n.size,creationDate:new Date(n.lastModified).toISOString(),duration:a}),s.push(c)}}n({results:s}),a()},i.oncancel=()=>{r(new t(`User cancelled photos app`)),a()};let c=e.mediaType??s.Photo;c===s.Photo?i.accept=`image/*`:c===s.Video?i.accept=`video/*`:i.accept=`image/*,video/*`,i.click()}_getFileFormat(e){return e.type===`image/png`?`png`:e.type===`image/gif`?`gif`:e.type.startsWith(`video/`)?e.type.split(`/`)[1]:e.type.startsWith(`image/`)?`jpeg`:e.type.split(`/`)[1]||`jpeg`}async _buildPhotoMediaResult(e,t){let n=this._getFileFormat(e),r=await this._getBase64FromFile(e),i={type:o.Photo,thumbnail:r,webPath:URL.createObjectURL(e),saved:!1};return t&&(i.metadata={format:n,resolution:await this._getImageResolution(e),size:e.size,creationDate:`lastModified`in e?new Date(e.lastModified).toISOString():new Date().toISOString()}),i}_validateFileInput(e,n,r){return!e.files||e.files.length===0?(n(new t(e.multiple?`No files selected`:`No file selected`)),r(),!1):!0}async _setupPWACameraModal(e,n,r,a,o){if(customElements.get(`pwa-camera-modal`)){let s=document.createElement(`pwa-camera-modal`);s.facingMode=e===i.Front?`user`:`environment`,document.body.appendChild(s);try{await s.componentOnReady(),s.addEventListener(`onPhoto`,async e=>{let r=e.detail;r===null?o(new t(`User cancelled photos app`)):r instanceof Error?o(r):a(await n(r)),s.dismiss(),document.body.removeChild(s)}),s.present()}catch{r()}}else console.error(`Unable to load PWA Element 'pwa-camera-modal'. See the docs: https://capacitorjs.com/docs/web/pwa-elements.`),r()}_createFileInput(e){let t=document.querySelector(`#${e}`);return t||(t=document.createElement(`input`),t.id=e,t.type=`file`,t.hidden=!0,document.body.appendChild(t)),t}async _getImageResolution(e){try{let t=await createImageBitmap(e),n=`${t.width}x${t.height}`;return t.close(),n}catch(e){console.warn(`Failed to get image resolution:`,e);return}}_getBase64FromFile(e){return new Promise((t,n)=>{let r=new FileReader;r.onloadend=()=>{let e=r.result.split(`,`)[1];t(e)},r.onerror=e=>{n(e)},r.readAsDataURL(e)})}_getVideoMetadata(e){return new Promise(t=>{let n=document.createElement(`video`);n.preload=`metadata`,n.muted=!0,n.onloadedmetadata=()=>{n.currentTime=Math.min(1,n.duration*.1)},n.onseeked=()=>{let e={resolution:`${n.videoWidth}x${n.videoHeight}`,duration:n.duration};try{let t=document.createElement(`canvas`);t.width=n.videoWidth,t.height=n.videoHeight;let r=t.getContext(`2d`);r&&(r.drawImage(n,0,0,t.width,t.height),e.thumbnail=t.toDataURL(`image/jpeg`,.8).split(`,`)[1])}catch(e){console.warn(`Failed to generate video thumbnail:`,e)}URL.revokeObjectURL(n.src),t(e)},n.onerror=()=>{URL.revokeObjectURL(n.src),t({})},n.src=URL.createObjectURL(e)})}async checkPermissions(){if(typeof navigator>`u`||!navigator.permissions)throw this.unavailable(`Permissions API not available in this browser`);try{return{camera:(await window.navigator.permissions.query({name:`camera`})).state,photos:`granted`}}catch{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 u;var d=e(`Camera`,{web:()=>new u});export{d as Camera,a as CameraResultType,r as CameraSource};