Skip to content

Commit 30fe30d

Browse files
committed
Customize Attachment Preview URL
Related to [#1154][] First, document the existing Attachment previewing process, including _which_ content types are supported out of the box. Next, resolve some `ManagedAttachment` to `Attachment` proxying issues. The `ManagedAttachment` class is what gets dispatched as part of `trix-attachment-add` events. It does not inherit from `Attachment`, but instead proxies method calls and property access. Prior to this commit, there were some proxy definition gaps. For example, the `ManagedAttachment` [declares a proxy for the `setAttribute` method][setAttribute]. Unfortunately, an `Attachment.setAttribute` method did not exist prior to these changes. This commit remedies that. Next, this commit adds proxy definitions for `Attachment.setPreviewURL` and `Attachment.getPreviewURL` so that event handlers can customize the value from the event-level `ManagedAttachment` instance, without deeply reaching into the `Attachment` instance (via `event.attachment.attachment`). [#1154]: #1154 [setAttribute]: https://github.com/basecamp/trix/blob/5db0ea49180de97f27b0becf47440690a1eaa39c/src/trix/models/managed_attachment.js#L22
1 parent 5db0ea4 commit 30fe30d

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,27 @@ To store attachments, listen for the `trix-attachment-add` event. Upload the att
300300
301301
If you don’t want to accept dropped or pasted files, call `preventDefault()` on the `trix-file-accept` event, which Trix dispatches just before the `trix-attachment-add` event.
302302
303+
## Previewing Attached Files
304+
305+
Trix automatically previews attached image files. To determine whether or not to preview an attached file, Trix compares the file's content type against the [Trix.Attachment.previewablePattern](./src/trix/models/attachment.js#L7). By default, Trix will preview the following content types:
306+
307+
* `image/gif`
308+
* `image/png`
309+
* `image/webp`
310+
* `image/jpg`
311+
* `image/jpeg`
312+
313+
To customize an attachment's preview, listen for the `trix-attachment-add` event. When handling the event, set the attachment's `previewable` attribute, then change its preview URL by calling `setPreviewURL`:
314+
315+
```js
316+
addEventListener("trix-attachment-add", (event) => {
317+
if (event.attachment.file instanceof File) {
318+
event.attachment.setAttribute("previewable", true)
319+
event.attachment.setPreviewURL("...")
320+
}
321+
})
322+
```
323+
303324
# Editing Text Programmatically
304325
305326
You can manipulate a Trix editor programmatically through the `Trix.Editor` interface, available on each `<trix-editor>` element through its `editor` property.

src/test/system/attachment_test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,29 @@ testGroup("Attachments", { template: "editor_with_image" }, () => {
156156
assert.textAttributes([ 1, 2 ], {})
157157
expectDocument(`${OBJECT_REPLACEMENT_CHARACTER}${OBJECT_REPLACEMENT_CHARACTER}\n`)
158158
})
159+
160+
test("setting previewURL makes an Attachment previewable", async () => {
161+
getEditorElement().addEventListener("trix-attachment-add", async ({ attachment }) => {
162+
attachment.setAttribute("previewable", true)
163+
attachment.setPreviewURL("/loading.png")
164+
165+
await delay(50)
166+
167+
attachment.setPreviewURL("/loaded.png")
168+
}, { once: true })
169+
170+
getComposition().insertFile(createFile())
171+
172+
const loadingImg = getEditorElement().querySelector("img")
173+
174+
assert.ok(/loading.png$/.test(loadingImg.src), "sets previewURL to loading image")
175+
176+
await delay(50)
177+
178+
const loadedImg = getEditorElement().querySelector("img")
179+
180+
assert.ok(/loaded.png$/.test(loadedImg.src), "sets previewURL to loaded image")
181+
})
159182
})
160183
})
161184

src/trix/models/attachment.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export default class Attachment extends TrixObject {
3232
this.didChangeAttributes()
3333
}
3434

35+
setAttribute(attribute, value) {
36+
this.setAttributes({ [attribute]: value })
37+
}
38+
3539
getAttribute(attribute) {
3640
return this.attributes.get(attribute)
3741
}

src/trix/models/managed_attachment.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ ManagedAttachment.proxyMethod("attachment.setAttributes")
2323
ManagedAttachment.proxyMethod("attachment.isPending")
2424
ManagedAttachment.proxyMethod("attachment.isPreviewable")
2525
ManagedAttachment.proxyMethod("attachment.getURL")
26+
ManagedAttachment.proxyMethod("attachment.getPreviewURL")
27+
ManagedAttachment.proxyMethod("attachment.setPreviewURL")
2628
ManagedAttachment.proxyMethod("attachment.getHref")
2729
ManagedAttachment.proxyMethod("attachment.getFilename")
2830
ManagedAttachment.proxyMethod("attachment.getFilesize")

0 commit comments

Comments
 (0)