plugin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. var capacitorCamera = (function (exports, core) {
  2. 'use strict';
  3. exports.CameraSource = void 0;
  4. (function (CameraSource) {
  5. /**
  6. * Prompts the user to select either the photo album or take a photo.
  7. */
  8. CameraSource["Prompt"] = "PROMPT";
  9. /**
  10. * Take a new photo using the camera.
  11. */
  12. CameraSource["Camera"] = "CAMERA";
  13. /**
  14. * Pick an existing photo from the gallery or photo album.
  15. */
  16. CameraSource["Photos"] = "PHOTOS";
  17. })(exports.CameraSource || (exports.CameraSource = {}));
  18. exports.CameraDirection = void 0;
  19. (function (CameraDirection) {
  20. CameraDirection["Rear"] = "REAR";
  21. CameraDirection["Front"] = "FRONT";
  22. })(exports.CameraDirection || (exports.CameraDirection = {}));
  23. exports.CameraResultType = void 0;
  24. (function (CameraResultType) {
  25. CameraResultType["Uri"] = "uri";
  26. CameraResultType["Base64"] = "base64";
  27. CameraResultType["DataUrl"] = "dataUrl";
  28. })(exports.CameraResultType || (exports.CameraResultType = {}));
  29. class CameraWeb extends core.WebPlugin {
  30. async getPhoto(options) {
  31. // eslint-disable-next-line no-async-promise-executor
  32. return new Promise(async (resolve, reject) => {
  33. if (options.webUseInput || options.source === exports.CameraSource.Photos) {
  34. this.fileInputExperience(options, resolve, reject);
  35. }
  36. else if (options.source === exports.CameraSource.Prompt) {
  37. let actionSheet = document.querySelector('pwa-action-sheet');
  38. if (!actionSheet) {
  39. actionSheet = document.createElement('pwa-action-sheet');
  40. document.body.appendChild(actionSheet);
  41. }
  42. actionSheet.header = options.promptLabelHeader || 'Photo';
  43. actionSheet.cancelable = false;
  44. actionSheet.options = [
  45. { title: options.promptLabelPhoto || 'From Photos' },
  46. { title: options.promptLabelPicture || 'Take Picture' },
  47. ];
  48. actionSheet.addEventListener('onSelection', async (e) => {
  49. const selection = e.detail;
  50. if (selection === 0) {
  51. this.fileInputExperience(options, resolve, reject);
  52. }
  53. else {
  54. this.cameraExperience(options, resolve, reject);
  55. }
  56. });
  57. }
  58. else {
  59. this.cameraExperience(options, resolve, reject);
  60. }
  61. });
  62. }
  63. async pickImages(_options) {
  64. // eslint-disable-next-line no-async-promise-executor
  65. return new Promise(async (resolve, reject) => {
  66. this.multipleFileInputExperience(resolve, reject);
  67. });
  68. }
  69. async cameraExperience(options, resolve, reject) {
  70. if (customElements.get('pwa-camera-modal')) {
  71. const cameraModal = document.createElement('pwa-camera-modal');
  72. cameraModal.facingMode =
  73. options.direction === exports.CameraDirection.Front ? 'user' : 'environment';
  74. document.body.appendChild(cameraModal);
  75. try {
  76. await cameraModal.componentOnReady();
  77. cameraModal.addEventListener('onPhoto', async (e) => {
  78. const photo = e.detail;
  79. if (photo === null) {
  80. reject(new core.CapacitorException('User cancelled photos app'));
  81. }
  82. else if (photo instanceof Error) {
  83. reject(photo);
  84. }
  85. else {
  86. resolve(await this._getCameraPhoto(photo, options));
  87. }
  88. cameraModal.dismiss();
  89. document.body.removeChild(cameraModal);
  90. });
  91. cameraModal.present();
  92. }
  93. catch (e) {
  94. this.fileInputExperience(options, resolve, reject);
  95. }
  96. }
  97. else {
  98. console.error(`Unable to load PWA Element 'pwa-camera-modal'. See the docs: https://capacitorjs.com/docs/web/pwa-elements.`);
  99. this.fileInputExperience(options, resolve, reject);
  100. }
  101. }
  102. fileInputExperience(options, resolve, reject) {
  103. let input = document.querySelector('#_capacitor-camera-input');
  104. const cleanup = () => {
  105. var _a;
  106. (_a = input.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(input);
  107. };
  108. if (!input) {
  109. input = document.createElement('input');
  110. input.id = '_capacitor-camera-input';
  111. input.type = 'file';
  112. input.hidden = true;
  113. document.body.appendChild(input);
  114. input.addEventListener('change', (_e) => {
  115. const file = input.files[0];
  116. let format = 'jpeg';
  117. if (file.type === 'image/png') {
  118. format = 'png';
  119. }
  120. else if (file.type === 'image/gif') {
  121. format = 'gif';
  122. }
  123. if (options.resultType === 'dataUrl' ||
  124. options.resultType === 'base64') {
  125. const reader = new FileReader();
  126. reader.addEventListener('load', () => {
  127. if (options.resultType === 'dataUrl') {
  128. resolve({
  129. dataUrl: reader.result,
  130. format,
  131. });
  132. }
  133. else if (options.resultType === 'base64') {
  134. const b64 = reader.result.split(',')[1];
  135. resolve({
  136. base64String: b64,
  137. format,
  138. });
  139. }
  140. cleanup();
  141. });
  142. reader.readAsDataURL(file);
  143. }
  144. else {
  145. resolve({
  146. webPath: URL.createObjectURL(file),
  147. format: format,
  148. });
  149. cleanup();
  150. }
  151. });
  152. input.addEventListener('cancel', (_e) => {
  153. reject(new core.CapacitorException('User cancelled photos app'));
  154. cleanup();
  155. });
  156. }
  157. input.accept = 'image/*';
  158. input.capture = true;
  159. if (options.source === exports.CameraSource.Photos ||
  160. options.source === exports.CameraSource.Prompt) {
  161. input.removeAttribute('capture');
  162. }
  163. else if (options.direction === exports.CameraDirection.Front) {
  164. input.capture = 'user';
  165. }
  166. else if (options.direction === exports.CameraDirection.Rear) {
  167. input.capture = 'environment';
  168. }
  169. input.click();
  170. }
  171. multipleFileInputExperience(resolve, reject) {
  172. let input = document.querySelector('#_capacitor-camera-input-multiple');
  173. const cleanup = () => {
  174. var _a;
  175. (_a = input.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(input);
  176. };
  177. if (!input) {
  178. input = document.createElement('input');
  179. input.id = '_capacitor-camera-input-multiple';
  180. input.type = 'file';
  181. input.hidden = true;
  182. input.multiple = true;
  183. document.body.appendChild(input);
  184. input.addEventListener('change', (_e) => {
  185. const photos = [];
  186. // eslint-disable-next-line @typescript-eslint/prefer-for-of
  187. for (let i = 0; i < input.files.length; i++) {
  188. const file = input.files[i];
  189. let format = 'jpeg';
  190. if (file.type === 'image/png') {
  191. format = 'png';
  192. }
  193. else if (file.type === 'image/gif') {
  194. format = 'gif';
  195. }
  196. photos.push({
  197. webPath: URL.createObjectURL(file),
  198. format: format,
  199. });
  200. }
  201. resolve({ photos });
  202. cleanup();
  203. });
  204. input.addEventListener('cancel', (_e) => {
  205. reject(new core.CapacitorException('User cancelled photos app'));
  206. cleanup();
  207. });
  208. }
  209. input.accept = 'image/*';
  210. input.click();
  211. }
  212. _getCameraPhoto(photo, options) {
  213. return new Promise((resolve, reject) => {
  214. const reader = new FileReader();
  215. const format = photo.type.split('/')[1];
  216. if (options.resultType === 'uri') {
  217. resolve({
  218. webPath: URL.createObjectURL(photo),
  219. format: format,
  220. saved: false,
  221. });
  222. }
  223. else {
  224. reader.readAsDataURL(photo);
  225. reader.onloadend = () => {
  226. const r = reader.result;
  227. if (options.resultType === 'dataUrl') {
  228. resolve({
  229. dataUrl: r,
  230. format: format,
  231. saved: false,
  232. });
  233. }
  234. else {
  235. resolve({
  236. base64String: r.split(',')[1],
  237. format: format,
  238. saved: false,
  239. });
  240. }
  241. };
  242. reader.onerror = e => {
  243. reject(e);
  244. };
  245. }
  246. });
  247. }
  248. async checkPermissions() {
  249. if (typeof navigator === 'undefined' || !navigator.permissions) {
  250. throw this.unavailable('Permissions API not available in this browser');
  251. }
  252. try {
  253. // https://developer.mozilla.org/en-US/docs/Web/API/Permissions/query
  254. // the specific permissions that are supported varies among browsers that implement the
  255. // permissions API, so we need a try/catch in case 'camera' is invalid
  256. const permission = await window.navigator.permissions.query({
  257. name: 'camera',
  258. });
  259. return {
  260. camera: permission.state,
  261. photos: 'granted',
  262. };
  263. }
  264. catch (_a) {
  265. throw this.unavailable('Camera permissions are not available in this browser');
  266. }
  267. }
  268. async requestPermissions() {
  269. throw this.unimplemented('Not implemented on web.');
  270. }
  271. async pickLimitedLibraryPhotos() {
  272. throw this.unavailable('Not implemented on web.');
  273. }
  274. async getLimitedLibraryPhotos() {
  275. throw this.unavailable('Not implemented on web.');
  276. }
  277. }
  278. new CameraWeb();
  279. const Camera = core.registerPlugin('Camera', {
  280. web: () => new CameraWeb(),
  281. });
  282. exports.Camera = Camera;
  283. return exports;
  284. })({}, capacitorExports);
  285. //# sourceMappingURL=plugin.js.map