@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
202 lines • 8.43 kB
JavaScript
import { MediaPicker, Popup, Browser, Dropzone, Clipboard, BinaryUploader, } from 'mediapicker';
var PickerFacade = (function () {
function PickerFacade(pickerType, uploadParams, contextConfig, stateManager, errorReporter, mediaPickerFactory) {
var _this = this;
this.stateManager = stateManager;
this.onStartListeners = [];
this.handleUploadStart = function (event) {
var file = event.file;
var tempId = "temporary:" + file.id;
var state = {
id: tempId,
status: 'uploading',
fileName: file.name,
fileSize: file.size,
fileMimeType: file.type,
};
_this.stateManager.updateState(tempId, state);
_this.onStartListeners.forEach(function (cb) { return cb.call(cb, state); });
};
this.handleUploadStatusUpdate = function (event) {
var file = event.file, progress = event.progress;
var tempId = "temporary:" + file.id;
var currentState = _this.stateManager.getState(tempId);
var currentStatus = currentState && currentState.status ? currentState.status : 'unknown';
_this.stateManager.updateState(tempId, {
id: tempId,
status: currentStatus === 'unknown' ? 'uploading' : currentStatus,
progress: progress ? progress.portion : undefined,
fileName: file.name,
fileSize: file.size,
fileMimeType: file.type,
});
};
this.handleUploadProcessing = function (event) {
var file = event.file;
var tempId = "temporary:" + file.id;
_this.stateManager.updateState(tempId, {
id: tempId,
publicId: file.publicId,
status: 'processing',
fileName: file.name,
fileSize: file.size,
fileMimeType: file.type,
});
};
this.handleUploadFinalizeReady = function (event) {
var file = event.file;
var finalize = event.finalize;
var tempId = "temporary:" + file.id;
if (!finalize) {
throw new Error('Editor: Media: Picker emitted finalize-ready event but didn\'t provide finalize callback');
}
_this.stateManager.updateState(tempId, {
id: tempId,
publicId: file.publicId,
finalizeCb: finalize,
status: 'unfinalized',
fileName: file.name,
fileSize: file.size,
fileMimeType: file.type,
});
};
this.handleUploadError = function (_a) {
var error = _a.error;
if (!error || !error.fileId) {
var err = new Error("Media: unknown upload-error received from Media Picker: " + (error && error.name));
_this.errorReporter.captureException(err);
return;
}
var tempId = "temporary:" + error.fileId;
_this.stateManager.updateState(tempId, {
id: tempId,
status: 'error',
error: error ? { description: error.description, name: error.name } : undefined,
});
};
this.handleUploadEnd = function (event) {
var file = event.file;
var tempId = "temporary:" + file.id;
_this.stateManager.updateState(tempId, {
id: tempId,
publicId: file.publicId,
status: 'ready',
fileName: file.name,
fileSize: file.size,
fileMimeType: file.type,
});
};
this.handleUploadPreviewUpdate = function (event) {
var tempId = "temporary:" + event.file.id;
if (event.preview !== undefined) {
_this.stateManager.updateState(tempId, {
id: tempId,
thumbnail: event.preview
});
}
};
this.errorReporter = errorReporter;
this.uploadParams = uploadParams;
if (!mediaPickerFactory) {
mediaPickerFactory = MediaPicker;
}
var picker = this.picker = mediaPickerFactory(pickerType, this.buildPickerConfigFromContext(contextConfig), pickerType === 'dropzone' ? { container: this.getDropzoneContainer() } : undefined);
picker.on('upload-start', this.handleUploadStart);
picker.on('upload-preview-update', this.handleUploadPreviewUpdate);
picker.on('upload-status-update', this.handleUploadStatusUpdate);
picker.on('upload-processing', this.handleUploadProcessing);
picker.on('upload-finalize-ready', this.handleUploadFinalizeReady);
picker.on('upload-error', this.handleUploadError);
picker.on('upload-end', this.handleUploadEnd);
if (picker instanceof Dropzone || picker instanceof Clipboard) {
picker.activate();
}
}
PickerFacade.prototype.destroy = function () {
var picker = this.picker;
if (!picker) {
return;
}
picker.removeAllListeners('upload-start');
picker.removeAllListeners('upload-preview-update');
picker.removeAllListeners('upload-status-update');
picker.removeAllListeners('upload-processing');
picker.removeAllListeners('upload-finalize-ready');
picker.removeAllListeners('upload-error');
picker.removeAllListeners('upload-end');
try {
if (picker instanceof Dropzone || picker instanceof Clipboard) {
picker.deactivate();
}
if (picker instanceof Popup || picker instanceof Browser) {
picker.teardown();
}
}
catch (ex) {
this.errorReporter.captureException(ex);
}
};
PickerFacade.prototype.setUploadParams = function (params) {
this.uploadParams = params;
this.picker.setUploadParams(params);
};
PickerFacade.prototype.show = function () {
if (this.picker instanceof Popup) {
try {
this.picker.show();
}
catch (ex) {
this.errorReporter.captureException(ex);
}
}
};
PickerFacade.prototype.cancel = function (tempId) {
if (this.picker instanceof Popup) {
var state = this.stateManager.getState(tempId);
if (!state || (state.status === 'cancelled')) {
return;
}
try {
this.picker.cancel(tempId);
}
catch (e) {
// We're deliberately consuming a known Media Picker exception, as it seems that
// the picker has problems cancelling uploads before the popup picker has been shown
// TODO: remove after fixing https://jira.atlassian.com/browse/FIL-4161
if (!/((popupIframe|cancelUpload).*?undefined)|(undefined.*?(popupIframe|cancelUpload))/.test("" + e)) {
throw e;
}
}
this.stateManager.updateState(tempId, {
id: tempId,
status: 'cancelled',
});
}
};
PickerFacade.prototype.upload = function (url, fileName) {
if (this.picker instanceof BinaryUploader) {
this.picker.upload(url, fileName);
}
};
PickerFacade.prototype.onNewMedia = function (cb) {
this.onStartListeners.push(cb);
};
PickerFacade.prototype.buildPickerConfigFromContext = function (context) {
var _this = this;
return {
uploadParams: this.uploadParams,
apiUrl: context.serviceHost,
apiClientId: context.clientId,
tokenSource: { getter: function (reject, resolve) {
context.tokenProvider(_this.uploadParams.collection).then(resolve, reject);
} },
};
};
PickerFacade.prototype.getDropzoneContainer = function () {
var dropzoneContainer = this.uploadParams.dropzoneContainer;
return dropzoneContainer ? dropzoneContainer : document.body;
};
return PickerFacade;
}());
export default PickerFacade;
//# sourceMappingURL=picker-facade.js.map