Trix: Programmatically Remove Attachments

Created on 12 Oct 2018  路  3Comments  路  Source: basecamp/trix

I see that there is a way to programmatically insert a file/attachment, but is there a way to programmatically remove attachments? Being able to remove attachments by id would be awesome.

Most helpful comment

If you want to venture into not-technically-public APIs, this will work too:

const attachment = editor.getDocument().getAttachmentById(id)
editor.composition.removeAttachment(attachment)

All 3 comments

If you still have a reference to an attachment from an event, you can call its remove() method:

addEventListener("trix-attachment-add", event => {
  const { attachment } = event
  setTimeout(() => {
    attachment.remove()
  }, 1000)
})

Otherwise, you can use the editor.setSelectedRange() and editor.deleteInDirection() APIs.

If you want to venture into not-technically-public APIs, this will work too:

const attachment = editor.getDocument().getAttachmentById(id)
editor.composition.removeAttachment(attachment)

This works great, thank you so much for the response.

Was this page helpful?
0 / 5 - 0 ratings