javascriptreactjsbasecamptrix

"new Trix is not defined" when using Basecamp / Trix Editor


var attachment = new Trix.Attachment({ content: '<span class="mention">@trix</span>' })
element.editor.insertAttachment(attachment)

I used this snippet but it throwed error: Trix is not defined?? How can i handle it? Thank you!


Solution

  • This is probably because the script

    var attachment = new Trix.Attachment({ content: '<span class="mention">@trix</span>' })
    element.editor.insertAttachment(attachment)
    

    is executing before the trix.js or the npm package(however you are implementing it). For now I haven't found a listener when Trix is ready so a quick solution is to use a CDN instead of those.

    <script src="https://cdnjs.cloudflare.com/ajax/libs/trix/1.3.0/trix.js"></script>
    

    These should work if you don't have a problem with the use of CDN