rhino-editor
Version:
A custom element wrapped rich text editor
145 lines (142 loc) • 5.03 kB
JavaScript
import {
LOADING_STATES
} from "./chunk-S67QH4NY.js";
import {
BaseEvent
} from "./chunk-A753ZYET.js";
// src/exports/attachment-upload.ts
import { DirectUpload } from "@rails/activestorage";
var _AttachmentUploadStartEvent = class _AttachmentUploadStartEvent extends BaseEvent {
constructor(attachmentUpload, options) {
super(_AttachmentUploadStartEvent.eventName, options);
this.attachmentUpload = attachmentUpload;
this.attachmentUpload = attachmentUpload;
}
};
_AttachmentUploadStartEvent.eventName = "rhino-direct-upload:start";
var AttachmentUploadStartEvent = _AttachmentUploadStartEvent;
var _AttachmentUploadProgressEvent = class _AttachmentUploadProgressEvent extends BaseEvent {
constructor(attachmentUpload, options) {
super(_AttachmentUploadProgressEvent.eventName, options);
this.attachmentUpload = attachmentUpload;
this.attachmentUpload = attachmentUpload;
}
};
_AttachmentUploadProgressEvent.eventName = "rhino-direct-upload:progress";
var AttachmentUploadProgressEvent = _AttachmentUploadProgressEvent;
var _AttachmentUploadErrorEvent = class _AttachmentUploadErrorEvent extends BaseEvent {
constructor(attachmentUpload, options) {
super(_AttachmentUploadErrorEvent.eventName, options);
this.attachmentUpload = attachmentUpload;
this.attachmentUpload = attachmentUpload;
}
};
_AttachmentUploadErrorEvent.eventName = "rhino-direct-upload:error";
var AttachmentUploadErrorEvent = _AttachmentUploadErrorEvent;
var _AttachmentUploadSucceedEvent = class _AttachmentUploadSucceedEvent extends BaseEvent {
constructor(attachmentUpload, options) {
super(_AttachmentUploadSucceedEvent.eventName, options);
this.attachmentUpload = attachmentUpload;
this.attachmentUpload = attachmentUpload;
}
};
_AttachmentUploadSucceedEvent.eventName = "rhino-direct-upload:succeed";
var AttachmentUploadSucceedEvent = _AttachmentUploadSucceedEvent;
var _AttachmentUploadCompleteEvent = class _AttachmentUploadCompleteEvent extends BaseEvent {
constructor(attachmentUpload, options) {
super(_AttachmentUploadCompleteEvent.eventName, options);
this.attachmentUpload = attachmentUpload;
this.attachmentUpload = attachmentUpload;
}
};
_AttachmentUploadCompleteEvent.eventName = "rhino-direct-upload:complete";
var AttachmentUploadCompleteEvent = _AttachmentUploadCompleteEvent;
var AttachmentUpload = class {
constructor(attachment, element) {
this.progress = 0;
this.attachment = attachment;
this.element = element;
if (this.attachment.file == null) throw "No file found for direct upload";
this.directUpload = new DirectUpload(
this.attachment.file,
this.directUploadUrl,
this
);
}
start() {
this.directUpload.create(this.directUploadDidComplete.bind(this));
this.element.dispatchEvent(new AttachmentUploadStartEvent(this));
}
directUploadWillStoreFileWithXHR(xhr) {
const maxPossibleProgress = 90;
xhr.upload.addEventListener("progress", (event) => {
const progress = Math.min(
event.loaded / event.total * 100,
maxPossibleProgress
);
this.progress = progress;
this.setUploadProgress();
this.element.dispatchEvent(new AttachmentUploadProgressEvent(this));
});
}
handleError(error) {
this.progress = 0;
if (this.attachment.content == null) {
this.attachment.setNodeMarkup({
progress: 0,
loadingState: LOADING_STATES.error
});
}
this.element.dispatchEvent(new AttachmentUploadErrorEvent(this));
this.element.dispatchEvent(new AttachmentUploadCompleteEvent(this));
if (error) {
throw Error(`Direct upload failed: ${error}`);
}
}
directUploadDidComplete(error, blob) {
if (error) {
this.handleError(error);
return;
}
if (blob.attachable_sgid) {
const blobUrl = this.createBlobUrl(blob.signed_id, blob.filename);
this.attachment.directUpload = this;
this.attachment.setAttributes({
sgid: blob.attachable_sgid,
url: blobUrl
});
}
}
setUploadProgress() {
if (this.progress >= 100) {
this.progress = 100;
this.attachment.setUploadProgress(100);
return;
}
this.attachment.setUploadProgress(this.progress);
}
createBlobUrl(signedId, filename) {
if (this.blobUrlTemplate == null) return "";
return this.blobUrlTemplate.replace(":signed_id", signedId).replace(":filename", encodeURIComponent(filename));
}
get directUploadUrl() {
if (this.element.dataset.directUploadUrl == null) {
throw Error(
`No "data-direct-upload-url" attribute is set on ${this.element}`
);
}
return this.element.dataset.directUploadUrl;
}
get blobUrlTemplate() {
return this.element.dataset.blobUrlTemplate;
}
};
export {
AttachmentUploadStartEvent,
AttachmentUploadProgressEvent,
AttachmentUploadErrorEvent,
AttachmentUploadSucceedEvent,
AttachmentUploadCompleteEvent,
AttachmentUpload
};
//# sourceMappingURL=chunk-UMN5Y2FY.js.map