Skip to content

Commit 6fe5bcd

Browse files
authored
feat(entry-file): added file upload source (#736)
* feat(entry-file): added file upload source * feat: added method getSourceTypes * feat: added export UploadSource, ExternalUploadSource * feat: added types and handler fallback * build(ssr): added check object and return stubbedExports with obj * feat(types): added SourceTypes in the OutputFileEntry * test(types): added test for source type in the file
1 parent 649db3b commit 6fe5bcd

12 files changed

+48
-25
lines changed

abstract/UploaderBlock.js

+11-18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { TypedCollection } from './TypedCollection.js';
1515
import { UploaderPublicApi } from './UploaderPublicApi.js';
1616
import { ValidationManager } from './ValidationManager.js';
1717
import { uploadEntrySchema } from './uploadEntrySchema.js';
18+
import { ExternalUploadSource, UploadSource } from '../blocks/utils/UploadSource.js';
1819

1920
export class UploaderBlock extends ActivityBlock {
2021
/** @protected */
@@ -421,26 +422,18 @@ export class UploaderBlock extends ActivityBlock {
421422
}
422423
}
423424

424-
/** @enum {String} */
425+
/**
426+
* @deprecated Use list sources ExternalUploadSource from from blocks/utils/UploadSource.js
427+
* @enum {String}
428+
*/
425429
UploaderBlock.extSrcList = Object.freeze({
426-
FACEBOOK: 'facebook',
427-
DROPBOX: 'dropbox',
428-
GDRIVE: 'gdrive',
429-
GPHOTOS: 'gphotos',
430-
INSTAGRAM: 'instagram',
431-
FLICKR: 'flickr',
432-
VK: 'vk',
433-
EVERNOTE: 'evernote',
434-
BOX: 'box',
435-
ONEDRIVE: 'onedrive',
436-
HUDDLE: 'huddle',
430+
...ExternalUploadSource,
437431
});
438432

439-
/** @enum {String} */
433+
/**
434+
* @deprecated Use list sources UploadSource from from blocks/utils/UploadSource.js
435+
* @enum {String}
436+
*/
440437
UploaderBlock.sourceTypes = Object.freeze({
441-
LOCAL: 'local',
442-
URL: 'url',
443-
CAMERA: 'camera',
444-
DRAW: 'draw',
445-
...UploaderBlock.extSrcList,
438+
...UploadSource,
446439
});

abstract/UploaderPublicApi.js

+1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ export class UploaderPublicApi {
240240
isRemoved: status === 'removed',
241241
errors: /** @type {import('../types/exported.js').OutputFileEntry['errors']} */ (uploadEntryData.errors),
242242
status,
243+
source: uploadEntryData?.source,
243244
};
244245

245246
return /** @type {import('../types/exported.js').OutputFileEntry<TStatus>} */ (outputItem);

abstract/uploadEntrySchema.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { UploadcareFile } from '@uploadcare/upload-client';
2727
* @property {string | null} fullPath
2828
* @property {import('@uploadcare/upload-client').Metadata | null} metadata
2929
* @property {boolean} isRemoved
30+
* @property {String} source
3031
*/
3132

3233
/**

blocks/DropArea/DropArea.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class DropArea extends UploaderBlock {
151151
this.subConfigValue('sourceList', (value) => {
152152
const list = stringToArray(value);
153153
// Enable drop area if local files are allowed
154-
this.$.isEnabled = list.includes(UploaderBlock.sourceTypes.LOCAL);
154+
this.$.isEnabled = list.includes(UploadSource.LOCAL);
155155
// Show drop area if it's enabled or default slot is overrided
156156
this.$.isVisible = this.$.isEnabled || !this.querySelector('[data-default-slot]');
157157
});

blocks/FileItem/FileItem.js

+1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ export class FileItem extends UploaderBlock {
397397
cdnUrl: entry.getValue('cdnUrl') ?? fileInfo.cdnUrl,
398398
cdnUrlModifiers: entry.getValue('cdnUrlModifiers') ?? '',
399399
uploadProgress: 100,
400+
source: entry.getValue('source') ?? null,
400401
});
401402

402403
if (entry === this._entry) {

blocks/SourceBtn/SourceBtn.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22
import { UploaderBlock } from '../../abstract/UploaderBlock.js';
33
import { ActivityBlock } from '../../abstract/ActivityBlock.js';
4+
import { ExternalUploadSource, UploadSource } from '../utils/UploadSource.js';
45

56
const L10N_PREFIX = 'src-type-';
67

@@ -37,19 +38,19 @@ export class SourceBtn extends UploaderBlock {
3738

3839
initTypes() {
3940
this.registerType({
40-
type: UploaderBlock.sourceTypes.LOCAL,
41+
type: UploadSource.LOCAL,
4142
activate: () => {
4243
this.api.openSystemDialog();
4344
return false;
4445
},
4546
});
4647
this.registerType({
47-
type: UploaderBlock.sourceTypes.URL,
48+
type: UploadSource.URL,
4849
activity: ActivityBlock.activities.URL,
4950
textKey: 'from-url',
5051
});
5152
this.registerType({
52-
type: UploaderBlock.sourceTypes.CAMERA,
53+
type: UploadSource.CAMERA,
5354
activity: ActivityBlock.activities.CAMERA,
5455
activate: () => {
5556
const supportsCapture = 'capture' in document.createElement('input');
@@ -65,7 +66,7 @@ export class SourceBtn extends UploaderBlock {
6566
icon: 'edit-draw',
6667
});
6768

68-
for (let externalSourceType of Object.values(UploaderBlock.extSrcList)) {
69+
for (let externalSourceType of Object.values(ExternalUploadSource)) {
6970
this.registerType({
7071
type: externalSourceType,
7172
activity: ActivityBlock.activities.EXTERNAL,

blocks/UrlSource/UrlSource.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class UrlSource extends UploaderBlock {
1313
e.preventDefault();
1414

1515
let url = this.ref.input['value'];
16-
this.api.addFileFromUrl(url, { source: UploadSource.URL_TAB });
16+
this.api.addFileFromUrl(url, { source: UploadSource.URL });
1717
this.$['*currentActivity'] = ActivityBlock.activities.UPLOAD_LIST;
1818
},
1919
onCancel: () => {

blocks/utils/UploadSource.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1+
// @ts-check
2+
export const ExternalUploadSource = Object.freeze({
3+
FACEBOOK: 'facebook',
4+
DROPBOX: 'dropbox',
5+
GDRIVE: 'gdrive',
6+
GPHOTOS: 'gphotos',
7+
INSTAGRAM: 'instagram',
8+
FLICKR: 'flickr',
9+
VK: 'vk',
10+
EVERNOTE: 'evernote',
11+
BOX: 'box',
12+
ONEDRIVE: 'onedrive',
13+
HUDDLE: 'huddle',
14+
});
15+
116
export const UploadSource = Object.freeze({
217
LOCAL: 'local',
318
DROP_AREA: 'drop-area',
4-
URL_TAB: 'url-tab',
519
CAMERA: 'camera',
620
EXTERNAL: 'external',
721
API: 'js-api',
22+
URL: 'url',
23+
DRAW: 'draw',
24+
...ExternalUploadSource,
825
});
26+
27+
/** @typedef {(typeof UploadSource)[keyof typeof UploadSource]} SourceTypes */

build-ssr-stubs.js

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ const stubbedExports = Object.fromEntries(
7272
newValue = '() => {}';
7373
} else if (typeof value === 'string') {
7474
newValue = `\`${value}\``;
75+
} else if (typeof value === 'object') {
76+
newValue = JSON.stringify(value);
7577
} else {
7678
throw new Error(`Unexpected export type: ${typeof value}`);
7779
}

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export { defineComponents } from './abstract/defineComponents.js';
4444
export { defineLocale } from './abstract/localeRegistry.js';
4545
export { loadFileUploaderFrom } from './abstract/loadFileUploaderFrom.js';
4646
export { toKebabCase } from './utils/toKebabCase.js';
47+
export { UploadSource, ExternalUploadSource } from './blocks/utils/UploadSource.js';
4748

4849
export * from './env.js';
4950

types/exported.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type SecureUploadsSignatureResolver = () => Promise<SecureUploadsSignatur
2121
export type IconHrefResolver = (iconName: string) => string;
2222
export type FileValidators = FuncFileValidator[];
2323
export type CollectionValidators = FuncCollectionValidator[];
24+
export type SourceTypes = import('../blocks/utils/UploadSource').SourceTypes
2425

2526
export type ConfigType = {
2627
pubkey: string;
@@ -168,6 +169,7 @@ export type OutputFileEntry<TStatus extends OutputFileStatus = OutputFileStatus>
168169
externalUrl: string | null;
169170
uploadProgress: number;
170171
fullPath: string | null;
172+
source: SourceTypes | null;
171173
} & (
172174
| {
173175
status: 'success';

types/test/uc-upload-ctx-provider.test-d.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
OutputFileErrorType,
1212
UploadCtxProvider
1313
} from '../../index.js';
14+
import { SourceTypes } from '../../blocks/utils/UploadSource.js';
1415

1516
const instance = new UploadCtxProvider();
1617
instance.uploadCollection.size;
@@ -96,6 +97,7 @@ instance.addEventListener('file-added', (e) => {
9697
expectType<null>(state.cdnUrlModifiers);
9798
expectType<null>(state.uuid);
9899
expectType<null>(state.fileInfo);
100+
expectType<SourceTypes | null>(state.source);
99101
});
100102

101103
instance.addEventListener('file-removed', (e) => {

0 commit comments

Comments
 (0)