How to Use Trix Editor With Phoenix LiveView: Difference between revisions

From ElixirBlocks
Jump to: navigation, search
No edit summary
Line 11: Line 11:
         _csrf_token: csrfToken
         _csrf_token: csrfToken
     },
     },




Line 22: Line 23:
                 this.ele.addEventListener("trix-change", (e) => {
                 this.ele.addEventListener("trix-change", (e) => {
                     console.log(e.target.value)
                     console.log(e.target.value)
                     this.pushEvent('zap', {
                     this.pushEvent('transmit-text', {
                         payload: e.target.value
                         payload: e.target.value
                     });
                     });

Revision as of 17:16, 21 June 2023

This page is in progress

To integrate Trix editor we use a JavaScript Hook

JavaScript Hook

let liveSocket = new LiveSocket("/live", Socket, {
    params: {
        _csrf_token: csrfToken
    },



    hooks: {
        TrixEditor: {
            mounted() {

                let element = document.querySelector("trix-editor");
                this.ele = element
                this.ele.value = "data in the value attribute";
                this.ele.addEventListener("trix-change", (e) => {
                    console.log(e.target.value)
                    this.pushEvent('transmit-text', {
                        payload: e.target.value
                    });
                });

            }
        }
    }


})

LiveView


    <div phx-hook="TrixEditor" id="xyz">
      <div id="richtext" phx-update="ignore">
        <trix-editor class="trix-content"></trix-editor>
      </div>
    </div>