Skip to content

Commit

Permalink
fix: system file dialog on safari (#730)
Browse files Browse the repository at this point in the history
Co-authored-by: nd0ut <[email protected]>
  • Loading branch information
nd0ut and nd0ut authored Aug 26, 2024
1 parent 90531f2 commit cfdd43a
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions abstract/UploaderPublicApi.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check
import { ActivityBlock } from './ActivityBlock.js';

import { Data } from '@symbiotejs/symbiote';
import { applyStyles, Data } from '@symbiotejs/symbiote';
import { EventType } from '../blocks/UploadCtxProvider/EventEmitter.js';
import { UploadSource } from '../blocks/utils/UploadSource.js';
import { serializeCsv } from '../blocks/utils/comma-separated.js';
Expand Down Expand Up @@ -138,7 +138,9 @@ export class UploaderPublicApi {

/** @param {{ captureCamera?: boolean }} options */
openSystemDialog = (options = {}) => {
let accept = serializeCsv(mergeFileTypes([this.cfg.accept ?? '', ...(this.cfg.imgOnly ? IMAGE_ACCEPT_LIST : [])]));
const accept = serializeCsv(
mergeFileTypes([this.cfg.accept ?? '', ...(this.cfg.imgOnly ? IMAGE_ACCEPT_LIST : [])]),
);

if (this.cfg.accept && !!this.cfg.imgOnly) {
console.warn(
Expand All @@ -147,7 +149,16 @@ export class UploaderPublicApi {
'The value of `accept` will be concatenated with the internal image mime types list.',
);
}

const INPUT_ATTR_NAME = 'uploadcare-file-input';
const fileInput = document.createElement('input');
fileInput.setAttribute(INPUT_ATTR_NAME, '');
applyStyles(fileInput, {
opacity: 0,
height: 0,
width: 0,
visibility: 'hidden',
});
fileInput.type = 'file';
fileInput.multiple = this.cfg.multiple;
if (options.captureCamera) {
Expand All @@ -156,18 +167,33 @@ export class UploaderPublicApi {
} else {
fileInput.accept = accept;
}
fileInput.addEventListener(
'change',
() => {
if (!fileInput.files) {
return;
}
[...fileInput.files].forEach((file) =>
this.addFileFromObject(file, { source: options.captureCamera ? UploadSource.CAMERA : UploadSource.LOCAL }),
);
// To call uploadTrigger UploadList should draw file items first:
this._ctx.$['*currentActivity'] = ActivityBlock.activities.UPLOAD_LIST;
this._ctx.setOrAddState('*modalActive', true);
fileInput.remove();
},
{
once: true,
},
);

document.querySelectorAll(`[${INPUT_ATTR_NAME}]`).forEach((el) => el.remove());

/**
* Some browsers (e.g. Safari) require the file input to be in the DOM to work properly. Without it the file input
* will open system dialog but won't trigger the change event sometimes.
*/
document.body.appendChild(fileInput);
fileInput.dispatchEvent(new MouseEvent('click'));
fileInput.onchange = () => {
// @ts-ignore TODO: fix this
[...fileInput['files']].forEach((file) =>
this.addFileFromObject(file, { source: options.captureCamera ? UploadSource.CAMERA : UploadSource.LOCAL }),
);
// To call uploadTrigger UploadList should draw file items first:
this._ctx.$['*currentActivity'] = ActivityBlock.activities.UPLOAD_LIST;
this._ctx.setOrAddState('*modalActive', true);
// @ts-ignore TODO: fix this
fileInput['value'] = '';
};
};

/**
Expand Down

0 comments on commit cfdd43a

Please sign in to comment.