Reputation: 11
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?
Upvotes: 1
Views: 1438
Reputation: 109
try this: https://github.com/basecamp/trix/issues/148
document.addEventListener('trix-before-paste', function (e) {
if (e.paste.hasOwnProperty('html')){
let div = document.createElement("div");
div.innerHTML = e.paste.html;
e.paste.html = div.textContent;
}
});
Upvotes: 1