./assets/js/hooks/textEditHook.js
import Quill from 'quill';
export let TextEditor = {
mounted() {
console.log('Mounting');
let quill = new Quill(this.el, {
theme: 'snow'
});
quill.on('text-change', (delta, oldDelta, source) => {
if (source == 'api') {
console.log("An API call triggered this change.");
} else if (source == 'user') {
console.log(this);
this.pushEventTo(this.el.phxHookId, "text-editor", {"text_content": quill.getContents()})
}
});
},
updated(){
console.log('U');
}
}
./assets/js/app.js
import { TextEditor } from './hooks/textEditHook' //Put this at the top
let Hooks = {}
// WYSIWYG
Hooks.TextEditor = TextEditor
let liveSocket = new LiveSocket("/live", Socket, {hooks: Hooks, params: {_csrf_token: csrfToken}})
Note!! This must be at the top level of the map; Not within params #IDidThisByAccident
<div id="editor" phx-hook="TextEditor" phx-target={@myself} />
Note: I’ve set the phx-target to itself since I’m placing the handle-event function in there
<link href="<https://cdn.quilljs.com/1.3.6/quill.snow.css>" rel="stylesheet">