aboutsummaryrefslogtreecommitdiffstats
path: root/app/javascript/activetext/index.js
diff options
context:
space:
mode:
authorJavan Makhmali <javan@javan.us>2018-02-12 15:50:42 -0500
committerJavan Makhmali <javan@javan.us>2018-02-12 15:50:42 -0500
commit986d6a6da4a42a9e92e8c0cc96b7edca35e778de (patch)
tree16dee1f3a9922c421cfc5a6b0c27d8bb808ff63d /app/javascript/activetext/index.js
parent2f13b3c9fe21debec4bd9afe4601125f4fe29ce2 (diff)
downloadrails-986d6a6da4a42a9e92e8c0cc96b7edca35e778de.tar.gz
rails-986d6a6da4a42a9e92e8c0cc96b7edca35e778de.tar.bz2
rails-986d6a6da4a42a9e92e8c0cc96b7edca35e778de.zip
Add initial ActiveStorage integration
Diffstat (limited to 'app/javascript/activetext/index.js')
-rw-r--r--app/javascript/activetext/index.js32
1 files changed, 32 insertions, 0 deletions
diff --git a/app/javascript/activetext/index.js b/app/javascript/activetext/index.js
index c77ed0ab26..03cb9960cb 100644
--- a/app/javascript/activetext/index.js
+++ b/app/javascript/activetext/index.js
@@ -1 +1,33 @@
import * as Trix from "trix"
+import { DirectUpload } from "activestorage"
+
+// FIXME: Hard coded routes
+const directUploadsURL = "/rails/active_storage/direct_uploads"
+const blobsURL = "/rails/active_storage/blobs"
+
+addEventListener("trix-attachment-add", event => {
+ const { attachment } = event
+ if (!attachment.file) return
+
+ const delegate = {
+ directUploadWillStoreFileWithXHR: (xhr) => {
+ xhr.upload.addEventListener("progress", event => {
+ const progress = event.loaded / event.total * 100
+ attachment.setUploadProgress(progress)
+ })
+ }
+ }
+
+ const directUpload = new DirectUpload(attachment.file, directUploadsURL, delegate)
+
+ directUpload.create((error, attributes) => {
+ if (error) {
+ console.warn("Failed to store file for attachment", attachment, error)
+ } else {
+ console.log("Created blob for attachment", attributes, attachment)
+ const { signed_id } = attributes
+ const url = `${blobsURL}/${signed_id}/${encodeURIComponent(attachment.file.name)}`
+ attachment.setAttributes({ url, signed_id })
+ }
+ })
+})