Skip to content

Commit

Permalink
feat(external-sources): add files to the upload list after done butto…
Browse files Browse the repository at this point in the history
…n click
  • Loading branch information
nd0ut committed Sep 27, 2023
1 parent 26eda66 commit 0089370
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions blocks/ExternalSource/ExternalSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,30 @@ export class ExternalSource extends UploaderBlock {
...this.init$,
activityIcon: '',
activityCaption: '',
selectedList: [],
counter: 0,
onDone: () => {
for (const message of this.$.selectedList) {
const url = (() => {
if (message.alternatives) {
const preferredTypes = stringToArray(this.cfg.externalSourcesPreferredTypes);
for (const preferredType of preferredTypes) {
const regexp = wildcardRegexp(preferredType);
for (const [type, typeUrl] of Object.entries(message.alternatives)) {
if (regexp.test(type)) {
return typeUrl;
}
}
}
}
return message.url;
})();

const { filename } = message;
const { externalSourceType } = this.activityParams;
this.addFileFromUrl(url, { fileName: filename, source: externalSourceType });
}

this.$['*currentActivity'] = ActivityBlock.activities.UPLOAD_LIST;
},
onCancel: () => {
Expand All @@ -67,7 +89,6 @@ export class ExternalSource extends UploaderBlock {
activityIcon: externalSourceType,
});

this.$.counter = 0;
this.mountIframe();
},
});
Expand All @@ -76,6 +97,9 @@ export class ExternalSource extends UploaderBlock {
this.unmountIframe();
}
});
this.sub('selectedList', (list) => {
this.$.counter = list.length;
});
}

/**
Expand All @@ -91,27 +115,7 @@ export class ExternalSource extends UploaderBlock {
* @param {SelectedFileMessage} message
*/
async handleFileSelected(message) {
console.log(message);
this.$.counter = this.$.counter + 1;

const url = (() => {
if (message.alternatives) {
const preferredTypes = stringToArray(this.cfg.externalSourcesPreferredTypes);
for (const preferredType of preferredTypes) {
const regexp = wildcardRegexp(preferredType);
for (const [type, typeUrl] of Object.entries(message.alternatives)) {
if (regexp.test(type)) {
return typeUrl;
}
}
}
}
return message.url;
})();

let { filename } = message;
let { externalSourceType } = this.activityParams;
this.addFileFromUrl(url, { fileName: filename, source: externalSourceType });
this.$.selectedList = [...this.$.selectedList, message];
}

/** @private */
Expand Down Expand Up @@ -193,13 +197,16 @@ export class ExternalSource extends UploaderBlock {
registerMessage('file-selected', iframe.contentWindow, this.handleFileSelected.bind(this));

this._iframe = iframe;
this.$.selectedList = [];
}

/** @private */
unmountIframe() {
this._iframe && unregisterMessage('file-selected', this._iframe.contentWindow);
this.ref.iframeWrapper.innerHTML = '';
this._iframe = null;
this.$.selectedList = [];
this.$.counter = 0;
}
}

Expand Down

0 comments on commit 0089370

Please sign in to comment.