'Paste rich text as plain text without HTML markup formatting in Trix editor

I am using Rich Text (Trix editor) with Rails and I want to be able to convert everything that user pastes to sanitized plain text instead of having formatted elements. Based on Trix documentation I am using this code to convert pasted elements to string.

const element = document.querySelector("trix-editor")
  element.addEventListener("trix-paste", function(e) {
    element.editor.getDocument().toString()
    console.log(element.editor.getDocument().toString())
  })

In console, it shows correct plain text, but in the editor, all the elements are still formatted.

How can I replace text in the editor to this sanitized text?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source