Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(entry-file): added file upload source #736

Merged
merged 7 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions abstract/UploaderBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { TypedCollection } from './TypedCollection.js';
import { UploaderPublicApi } from './UploaderPublicApi.js';
import { ValidationManager } from './ValidationManager.js';
import { uploadEntrySchema } from './uploadEntrySchema.js';
import { ExternalUploadSource, UploadSource } from '../blocks/utils/UploadSource.js';

export class UploaderBlock extends ActivityBlock {
/** @protected */
Expand Down Expand Up @@ -421,26 +422,18 @@ export class UploaderBlock extends ActivityBlock {
}
}

/** @enum {String} */
/**
* @deprecated Use list sources ExternalUploadSource from from blocks/utils/UploadSource.js
* @enum {String}
*/
UploaderBlock.extSrcList = Object.freeze({
egordidenko marked this conversation as resolved.
Show resolved Hide resolved
FACEBOOK: 'facebook',
DROPBOX: 'dropbox',
GDRIVE: 'gdrive',
GPHOTOS: 'gphotos',
INSTAGRAM: 'instagram',
FLICKR: 'flickr',
VK: 'vk',
EVERNOTE: 'evernote',
BOX: 'box',
ONEDRIVE: 'onedrive',
HUDDLE: 'huddle',
...ExternalUploadSource,
});

/** @enum {String} */
/**
* @deprecated Use list sources UploadSource from from blocks/utils/UploadSource.js
* @enum {String}
*/
UploaderBlock.sourceTypes = Object.freeze({
LOCAL: 'local',
URL: 'url',
CAMERA: 'camera',
DRAW: 'draw',
...UploaderBlock.extSrcList,
...UploadSource,
});
1 change: 1 addition & 0 deletions abstract/UploaderPublicApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export class UploaderPublicApi {
isRemoved: status === 'removed',
errors: /** @type {import('../types/exported.js').OutputFileEntry['errors']} */ (uploadEntryData.errors),
status,
source: uploadEntryData?.source,
egordidenko marked this conversation as resolved.
Show resolved Hide resolved
};

return /** @type {import('../types/exported.js').OutputFileEntry<TStatus>} */ (outputItem);
Expand Down
1 change: 1 addition & 0 deletions abstract/uploadEntrySchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { UploadcareFile } from '@uploadcare/upload-client';
* @property {string | null} fullPath
* @property {import('@uploadcare/upload-client').Metadata | null} metadata
* @property {boolean} isRemoved
* @property {String} source
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion blocks/DropArea/DropArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class DropArea extends UploaderBlock {
this.subConfigValue('sourceList', (value) => {
const list = stringToArray(value);
// Enable drop area if local files are allowed
this.$.isEnabled = list.includes(UploaderBlock.sourceTypes.LOCAL);
this.$.isEnabled = list.includes(UploadSource.LOCAL);
// Show drop area if it's enabled or default slot is overrided
this.$.isVisible = this.$.isEnabled || !this.querySelector('[data-default-slot]');
});
Expand Down
1 change: 1 addition & 0 deletions blocks/FileItem/FileItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ export class FileItem extends UploaderBlock {
cdnUrl: entry.getValue('cdnUrl') ?? fileInfo.cdnUrl,
cdnUrlModifiers: entry.getValue('cdnUrlModifiers') ?? '',
uploadProgress: 100,
source: entry.getValue('source') ?? null,
});

if (entry === this._entry) {
Expand Down
9 changes: 5 additions & 4 deletions blocks/SourceBtn/SourceBtn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check
import { UploaderBlock } from '../../abstract/UploaderBlock.js';
import { ActivityBlock } from '../../abstract/ActivityBlock.js';
import { ExternalUploadSource, UploadSource } from '../utils/UploadSource.js';

const L10N_PREFIX = 'src-type-';

Expand Down Expand Up @@ -37,19 +38,19 @@ export class SourceBtn extends UploaderBlock {

initTypes() {
this.registerType({
type: UploaderBlock.sourceTypes.LOCAL,
type: UploadSource.LOCAL,
activate: () => {
this.api.openSystemDialog();
return false;
},
});
this.registerType({
type: UploaderBlock.sourceTypes.URL,
type: UploadSource.URL,
activity: ActivityBlock.activities.URL,
textKey: 'from-url',
});
this.registerType({
type: UploaderBlock.sourceTypes.CAMERA,
type: UploadSource.CAMERA,
activity: ActivityBlock.activities.CAMERA,
activate: () => {
const supportsCapture = 'capture' in document.createElement('input');
Expand All @@ -65,7 +66,7 @@ export class SourceBtn extends UploaderBlock {
icon: 'edit-draw',
});

for (let externalSourceType of Object.values(UploaderBlock.extSrcList)) {
for (let externalSourceType of Object.values(ExternalUploadSource)) {
this.registerType({
type: externalSourceType,
activity: ActivityBlock.activities.EXTERNAL,
Expand Down
2 changes: 1 addition & 1 deletion blocks/UrlSource/UrlSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class UrlSource extends UploaderBlock {
e.preventDefault();

let url = this.ref.input['value'];
this.api.addFileFromUrl(url, { source: UploadSource.URL_TAB });
this.api.addFileFromUrl(url, { source: UploadSource.URL });
this.$['*currentActivity'] = ActivityBlock.activities.UPLOAD_LIST;
},
onCancel: () => {
Expand Down
21 changes: 20 additions & 1 deletion blocks/utils/UploadSource.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
// @ts-check
export const ExternalUploadSource = Object.freeze({
FACEBOOK: 'facebook',
DROPBOX: 'dropbox',
GDRIVE: 'gdrive',
GPHOTOS: 'gphotos',
INSTAGRAM: 'instagram',
FLICKR: 'flickr',
VK: 'vk',
EVERNOTE: 'evernote',
BOX: 'box',
ONEDRIVE: 'onedrive',
HUDDLE: 'huddle',
});

export const UploadSource = Object.freeze({
LOCAL: 'local',
DROP_AREA: 'drop-area',
URL_TAB: 'url-tab',
CAMERA: 'camera',
EXTERNAL: 'external',
API: 'js-api',
URL: 'url',
DRAW: 'draw',
...ExternalUploadSource,
});

/** @typedef {(typeof UploadSource)[keyof typeof UploadSource]} SourceTypes */
2 changes: 2 additions & 0 deletions build-ssr-stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const stubbedExports = Object.fromEntries(
newValue = '() => {}';
} else if (typeof value === 'string') {
newValue = `\`${value}\``;
} else if (typeof value === 'object') {
newValue = JSON.stringify(value);
} else {
throw new Error(`Unexpected export type: ${typeof value}`);
}
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export { defineComponents } from './abstract/defineComponents.js';
export { defineLocale } from './abstract/localeRegistry.js';
export { loadFileUploaderFrom } from './abstract/loadFileUploaderFrom.js';
export { toKebabCase } from './utils/toKebabCase.js';
export { UploadSource, ExternalUploadSource } from './blocks/utils/UploadSource.js';

export * from './env.js';

Expand Down
2 changes: 2 additions & 0 deletions types/exported.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type SecureUploadsSignatureResolver = () => Promise<SecureUploadsSignatur
export type IconHrefResolver = (iconName: string) => string;
export type FileValidators = FuncFileValidator[];
export type CollectionValidators = FuncCollectionValidator[];
export type SourceTypes = import('../blocks/utils/UploadSource').SourceTypes

export type ConfigType = {
pubkey: string;
Expand Down Expand Up @@ -168,6 +169,7 @@ export type OutputFileEntry<TStatus extends OutputFileStatus = OutputFileStatus>
externalUrl: string | null;
uploadProgress: number;
fullPath: string | null;
source: SourceTypes;
} & (
| {
status: 'success';
Expand Down
Loading