Trix: "trix-attachment-add" event called twice on calling updateAttributesForAttachment

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

document.addEventListener("trix-attachment-add", function(event) {
      console.log(event);
      var attachment;
      attachment = event.attachment;
      if (attachment.file) {
        attachment.attachmentManager.delegate.composition.updateAttributesForAttachment({'caption': '<Insert Caption>'}, attachment);
        return uploadAttachment(attachment);
      }
    });

I want to pre-add caption on uploaded attachment and I used the above code. I noticed that this line is causing the event to be fired twice

attachment.attachmentManager.delegate.composition.updateAttributesForAttachment({'caption': '<Insert Caption>'}, attachment);

I got a hold of this way to update the caption from https://github.com/basecamp/trix/issues/156

Steps to Reproduce
  1. Use the above code to change caption of the attachment
  2. trix-attachment-add is fired twice
Details
  • Trix version: 0.10.0
  • Browser name and version: Chrome, Version 53.0.2785.116 (64-bit)
  • Operating system: Ubuntu 14.04

Most helpful comment

All 4 comments

Thanks! Also, here's how you can add a caption using the editor API more naturally:

addEventListener("trix-attachment-add", function(event) {
  var attachment = event.attachment

  if (attachment.file) {
    var editor = event.target.editor
    var originalRange = editor.getSelectedRange()
    var attachmentRange = editor.getDocument().getRangeOfAttachment(attachment)

    editor.setSelectedRange(attachmentRange)
    editor.activateAttribute("caption", "<Insert Caption>")
    editor.setSelectedRange(originalRange)
  }
})

Thanks @javan

Any information on when this fix will be released ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marpstar picture marpstar  路  5Comments

javan picture javan  路  4Comments

acapro picture acapro  路  3Comments

madikarizma picture madikarizma  路  5Comments

pars0097 picture pars0097  路  4Comments