Skip to content

Commit

Permalink
fix(camera): Append change listener only once (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Jun 17, 2021
1 parent cdc1835 commit 5b7021e
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions camera/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,48 @@ export class CameraWeb extends WebPlugin implements CameraPlugin {
input.type = 'file';
input.hidden = true;
document.body.appendChild(input);
input.addEventListener('change', (_e: any) => {
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,
} as Photo);
} else if (options.resultType === 'base64') {
const b64 = (reader.result as string).split(',')[1];
resolve({
base64String: b64,
format,
} as Photo);
}

cleanup();
});

reader.readAsDataURL(file);
} else {
resolve({
webPath: URL.createObjectURL(file),
format: format,
});
cleanup();
}
});
}

input.accept = 'image/*';
Expand All @@ -80,46 +122,6 @@ export class CameraWeb extends WebPlugin implements CameraPlugin {
(input as any).capture = 'environment';
}

input.addEventListener('change', (_e: any) => {
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,
} as Photo);
} else if (options.resultType === 'base64') {
const b64 = (reader.result as string).split(',')[1];
resolve({
base64String: b64,
format,
} as Photo);
}

cleanup();
});

reader.readAsDataURL(file);
} else {
resolve({
webPath: URL.createObjectURL(file),
format: format,
});
cleanup();
}
});

input.click();
}

Expand Down

0 comments on commit 5b7021e

Please sign in to comment.