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
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 ?
Most helpful comment
Just now :) https://github.com/basecamp/trix/releases/tag/0.10.1