Skip to content

Commit

Permalink
Only remove attachment upload button after trix has initialized (#3464)
Browse files Browse the repository at this point in the history
* Only remove attachment upload button after trix has initialized

I'm finding that when attachments are disabled the trix field controller is trying to remove the attachments button before trix has created the toolbar. This crashes the controller inside the connect method and so none of the other event listeners are set up.

* Update app/javascript/js/controllers/fields/trix_field_controller.js

* fix syntax

* fix disable_attachments logic

---------

Co-authored-by: Paul Bob <[email protected]>
Co-authored-by: Paul Bob <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2024
1 parent 85d957f commit f7c53f7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export default class extends Controller {
connect() {
if (this.attachmentsDisabledValue) {
// Remove the attachments button
this.controllerTarget.querySelector('.trix-button-group--file-tools').remove()
window.addEventListener('trix-initialize', (event) => {
if (event.target === this.editorTarget) {
this.controllerTarget.querySelector('.trix-button-group--file-tools').remove()
}
})
}

window.addEventListener('trix-file-accept', (event) => {
Expand Down
8 changes: 4 additions & 4 deletions lib/avo/fields/trix_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def is_action_text?
private

def disable_attachments?(args)
# If we don't have an attachment_key, we disable attachments. There's no point in having
# attachments if we can't store them.
return false if args[:attachment_key].present?
# Return the value of attachments_disabled if explicitly provided
return args[:attachments_disabled] unless args[:attachments_disabled].nil?

args[:attachments_disabled] == true
# Disable attachments if attachment_key is not present
args[:attachment_key].blank?
end
end
end
Expand Down

0 comments on commit f7c53f7

Please sign in to comment.