Skip to content

Commit 8f0b911

Browse files
MatheusRichbradrees
andcommitted
Dispatch direct-upload events on attachment uploads
When using Action Text's rich textarea, it's possible to attach files to the editor. Previously, that action didn't dispatch any events, which made it hard to react to the file uploads. For instance, if an upload failed, there was no way to notify the user about it, or remove the attachment from the editor. This commits adds new events - `direct-upload:start`, `direct-upload:progress`, and `direct-upload:end` - similar to how Active Storage's direct uploads work. Closes rails#37793 Supersedes rails#37794 Co-authored-by: Brad Rees <[email protected]>
1 parent 10924d3 commit 8f0b911

File tree

8 files changed

+102
-20
lines changed

8 files changed

+102
-20
lines changed

actiontext/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* Dispatch direct-upload events on attachment uploads
2+
3+
When using Action Text's rich textarea, it's possible to attach files to the
4+
editor. Previously, that action didn't dispatch any events, which made it hard
5+
to react to the file uploads. For instance, if an upload failed, there was no
6+
way to notify the user about it, or remove the attachment from the editor.
7+
8+
This commits adds new events - `direct-upload:start`, `direct-upload:progress`,
9+
and `direct-upload:end` - similar to how Active Storage's direct uploads work.
10+
11+
*Matheus Richard*, *Brad Rees*
12+
113
* Add `store_if_blank` option to `has_rich_text`
214

315
Pass `store_if_blank: false` to not create `ActionText::RichText` records when saving with a blank attribute, such as from an optional form parameter.

actiontext/app/assets/javascripts/actiontext.esm.js

Lines changed: 27 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

actiontext/app/assets/javascripts/actiontext.js

Lines changed: 27 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

actiontext/app/javascript/actiontext/attachment_upload.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DirectUpload } from "@rails/activestorage"
1+
import { DirectUpload, dispatchEvent } from "@rails/activestorage"
22

33
export class AttachmentUpload {
44
constructor(attachment, element) {
@@ -9,24 +9,29 @@ export class AttachmentUpload {
99

1010
start() {
1111
this.directUpload.create(this.directUploadDidComplete.bind(this))
12+
this.dispatch("start")
1213
}
1314

1415
directUploadWillStoreFileWithXHR(xhr) {
1516
xhr.upload.addEventListener("progress", event => {
1617
const progress = event.loaded / event.total * 100
1718
this.attachment.setUploadProgress(progress)
19+
if (progress) {
20+
this.dispatch("progress", { progress: progress })
21+
}
1822
})
1923
}
2024

2125
directUploadDidComplete(error, attributes) {
2226
if (error) {
23-
throw new Error(`Direct upload failed: ${error}`)
27+
this.dispatchError(error)
28+
} else {
29+
this.attachment.setAttributes({
30+
sgid: attributes.attachable_sgid,
31+
url: this.createBlobUrl(attributes.signed_id, attributes.filename)
32+
})
33+
this.dispatch("end")
2434
}
25-
26-
this.attachment.setAttributes({
27-
sgid: attributes.attachable_sgid,
28-
url: this.createBlobUrl(attributes.signed_id, attributes.filename)
29-
})
3035
}
3136

3237
createBlobUrl(signedId, filename) {
@@ -35,6 +40,18 @@ export class AttachmentUpload {
3540
.replace(":filename", encodeURIComponent(filename))
3641
}
3742

43+
dispatch(name, detail = {}) {
44+
detail.attachment = this.attachment
45+
return dispatchEvent(this.element, `direct-upload:${name}`, { detail })
46+
}
47+
48+
dispatchError(error) {
49+
const event = this.dispatch("error", { error })
50+
if (!event.defaultPrevented) {
51+
alert(error);
52+
}
53+
}
54+
3855
get directUploadUrl() {
3956
return this.element.dataset.directUploadUrl
4057
}

activestorage/app/assets/javascripts/activestorage.esm.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

activestorage/app/assets/javascripts/activestorage.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

activestorage/app/javascript/activestorage/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { start } from "./ujs"
22
import { DirectUpload } from "./direct_upload"
33
import { DirectUploadController } from "./direct_upload_controller"
44
import { DirectUploadsController } from "./direct_uploads_controller"
5-
export { start, DirectUpload, DirectUploadController, DirectUploadsController }
5+
import { dispatchEvent } from "./helpers"
6+
export { start, DirectUpload, DirectUploadController, DirectUploadsController, dispatchEvent }
67

78
function autostart() {
89
if (window.ActiveStorage) {

guides/source/action_text_overview.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ To customize the HTML rendered for embedded images and other attachments (known
233233
as blobs), edit the `app/views/active_storage/blobs/_blob.html.erb` template
234234
created by the installer:
235235

236-
237236
```html+erb
238237
<%# app/views/active_storage/blobs/_blob.html.erb %>
239238
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
@@ -270,6 +269,14 @@ encounter when working with Action Text and Active Storage is that images do not
270269
render correctly in the editor. This is usually due to the `libvips` dependency
271270
not being installed.
272271

272+
#### Attachment Direct Upload JavaScript Events
273+
274+
| Event name | Event target | Event data (`event.detail`) | Description |
275+
| --- | --- | --- | --- |
276+
| `direct-upload:start` | `<input>` | `{id, file}` | A direct upload is starting. |
277+
| `direct-upload:progress` | `<input>` | `{id, file, progress}` | As requests to store files progress. |
278+
| `direct-upload:error` | `<input>` | `{id, file, error}` | An error occurred. An `alert` will display unless this event is canceled. |
279+
| `direct-upload:end` | `<input>` | `{id, file}` | A direct upload has ended. |
273280

274281
### Signed GlobalID
275282

0 commit comments

Comments
 (0)