Trix: No way to prevent non-file attachments

Created on 4 Jan 2017  路  6Comments  路  Source: basecamp/trix

In several other issues (#216 #279 and #330) the following snippet is supplied as a way "to prevent non-file attachments like pasted images":

document.addEventListener("trix-attachment-add", function(event) {
  event.attachment.remove()
});

This does not work. The image still gets pasted in and a js error is generated: "Uncaught TypeError: Cannot read property 'remove' of undefined" which leads me to believe that this isn't supposed to be called on the event object.

My desired outcome here is preventing users from pasting images from desktop word processor documents (e.g. Word, Libreoffice, etc).

Steps to Reproduce
  1. Add the snippet
  2. Paste an image from a desktop word processing document
  3. The image gets pasted -> I expect that it would not be pasted
Details
  • Trix version: 0.9.10
  • Browser name and version: Chrome 56 (beta)
  • Operating system: Ubuntu 16.04

Most helpful comment

With jQuery, you need to access the original, native event:

$(document).on("trix-attachment-add", function(event) {
  event.originalEvent.attachment.remove();
});

jQuery passes the handler an Event object it can use to analyze and change the status of the event. This object is a normalized subset of data provided by the browser; the browser's unmodified native event object is available in event.originalEvent.
https://api.jquery.com/on/#event-handler

All 6 comments

I've been unable to reproduce this issue. Here's what I tried:
remove-attachment

The image attachment is added in the first paste as expected. Then it gets removed in the second paste after adding an event handler like you have above.

Okay eating my own words here, but maybe there is still a bug of some kind here. I committed a cardinal sin of the bug reporter - I reported a bug with code I wasn't actually running. I was using jQuery's on to bind the handler and not the exact same as the snippet above, which does appear to work. My apologies on that!

My question for you is this: why does window.event have attachment but the event handler argument does not?

$(document).on("trix-attachment-add", function(e) {
  e.attachment.remove(); // error
});

$(document).on("trix-attachment-add", function() {
  event.attachment.remove(); // fine
});

I appreciate you taking the time to look into this btw, thanks so much @javan !

With jQuery, you need to access the original, native event:

$(document).on("trix-attachment-add", function(event) {
  event.originalEvent.attachment.remove();
});

jQuery passes the handler an Event object it can use to analyze and change the status of the event. This object is a normalized subset of data provided by the browser; the browser's unmodified native event object is available in event.originalEvent.
https://api.jquery.com/on/#event-handler

When using jQuery, I usually name that arg $event instead of event as a reminder:

$(document).on("trix-attachment-add", function($event) {
  $event.originalEvent.attachment.remove();
});

the originalEvent just return null...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benzkji picture benzkji  路  3Comments

lcsqlpete picture lcsqlpete  路  3Comments

madikarizma picture madikarizma  路  5Comments

marpstar picture marpstar  路  5Comments

pars0097 picture pars0097  路  4Comments