Yshmarov
Yshmarov

Reputation: 3749

action_text - enable/disable attachments in different fields

How can I disable action_text attachments for one field, but enable it for another?

I have a post model with 2 fields - description_without_attachments and content_with_attachments.

Model Post.rb:

  has_rich_text :description_without_attachments
  has_rich_text :content_with_attachments

I found that the following code if added into packs/application.js can block all attachments for trix action_text:

window.addEventListener("trix-file-accept", function(event) {
  event.preventDefault()
  alert("File attachment not supported!")
})

however I want to block only for some specific field. I would imagine it working like:

  has_rich_text :description_without_attachments, attachments: false

Upvotes: 1

Views: 609

Answers (1)

Richard Michael
Richard Michael

Reputation: 1624

To do this in my project, I added the event listener on the editor specific to the field, not window.

In your case,

let editor = document.querySelector('trix-editor#post_description_without_attachments');
editor.addEventListener("trix-file-accept", e => e.preventDefault());

(Although unnecessary due to the unique id attribute, I think using the trix-editor element name in the query selector better documents the code.)

Upvotes: 4

Related Questions