diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2018-04-13 16:23:04 -0700 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2018-04-13 16:23:04 -0700 |
commit | f1d74871e7f00e8bbde3501a759487ac8cc4c3fc (patch) | |
tree | 2f1fa680bd1e6a356256fdc59cab5a6af8d74da7 /app/javascript/actiontext | |
parent | 3bc244abc1800c7617cbfbbe1dd2597053a638c9 (diff) | |
download | rails-f1d74871e7f00e8bbde3501a759487ac8cc4c3fc.tar.gz rails-f1d74871e7f00e8bbde3501a759487ac8cc4c3fc.tar.bz2 rails-f1d74871e7f00e8bbde3501a759487ac8cc4c3fc.zip |
Rename from Active Text to Action Text
This is more like Action View than Active Model.
Diffstat (limited to 'app/javascript/actiontext')
-rw-r--r-- | app/javascript/actiontext/attachment_upload.js | 45 | ||||
-rw-r--r-- | app/javascript/actiontext/index.js | 11 |
2 files changed, 56 insertions, 0 deletions
diff --git a/app/javascript/actiontext/attachment_upload.js b/app/javascript/actiontext/attachment_upload.js new file mode 100644 index 0000000000..a716f1f589 --- /dev/null +++ b/app/javascript/actiontext/attachment_upload.js @@ -0,0 +1,45 @@ +import { DirectUpload } from "activestorage" + +export class AttachmentUpload { + constructor(attachment, element) { + this.attachment = attachment + this.element = element + this.directUpload = new DirectUpload(attachment.file, this.directUploadUrl, this) + } + + start() { + this.directUpload.create(this.directUploadDidComplete.bind(this)) + } + + directUploadWillStoreFileWithXHR(xhr) { + xhr.upload.addEventListener("progress", event => { + const progress = event.loaded / event.total * 100 + this.attachment.setUploadProgress(progress) + }) + } + + directUploadDidComplete(error, attributes) { + if (error) { + throw new Error(`Direct upload failed: ${error}`) + } + + this.attachment.setAttributes({ + sgid: attributes.attachable_sgid, + url: this.createBlobUrl(attributes.signed_id, attributes.filename) + }) + } + + createBlobUrl(signedId, filename) { + return this.blobUrlTemplate + .replace(":signed_id", signedId) + .replace(":filename", encodeURIComponent(filename)) + } + + get directUploadUrl() { + return this.element.dataset.directUploadUrl + } + + get blobUrlTemplate() { + return this.element.dataset.blobUrlTemplate + } +} diff --git a/app/javascript/actiontext/index.js b/app/javascript/actiontext/index.js new file mode 100644 index 0000000000..c149eda952 --- /dev/null +++ b/app/javascript/actiontext/index.js @@ -0,0 +1,11 @@ +import * as Trix from "trix" +import { AttachmentUpload } from "./attachment_upload" + +addEventListener("trix-attachment-add", event => { + const { attachment, target } = event + + if (attachment.file) { + const upload = new AttachmentUpload(attachment, target) + upload.start() + } +}) |