Hey guys I’ve made a guide on how to connect Quill to Phoenix.
For the sake of formatting it might be easier to view it on my Notion Doc Here
How to connect Quill with Phoenix
- Make a hook file ex
./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');
}
}
- Import it to your
./assets/js/app.js
import { TextEditor } from './hooks/textEditHook' //Put this at the top
- Add hooks and assign it to Hooks.TextEditor (call this whatever but you must assign to Hooks
let Hooks = {}
// WYSIWYG
Hooks.TextEditor = TextEditor
- Assign the hooks to your LiveSocket
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
- Assign the phx-hook to where you want the element replaced
<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
- Import the CSS whatever way you want. For my lazy butt I just did an inline CSS import at the top
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
- Load that page and give it a shot
My code
My HEEX
<div>
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<h2><%= @title %></h2>
<.form
let={f}
for={@changeset}
id="profile-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save">
<%= label f, :name %>
<%= text_input f, :name %>
<%= error_tag f, :name %>
<%= label f, :description %>
<div id="editor" phx-hook="TextEditor" phx-target={@myself} />
<%= error_tag f, :description %>
<%= label f, :birthdate %>
<%= date_select f, :birthdate %>
<%= error_tag f, :birthdate %>
<%= inputs_for f, :links, fn fl -> %>
<div class="flex flex-row space-x-6">
<div id="link-title">
<p>Title</p>
<%= text_input fl, :title %>
</div>
<div id="link-url">
<p>url</p>
<%= text_input fl, :url %>
</div>
</div>
<% end %>
<a phx-click="add-link" phx-target={@myself} >Add Link</a>
<%= inputs_for f, :pics, [multipart: true], fn fl -> %>
<div class="flex flex-row space-x-6">
<div id="link-title">
<p>Caption</p>
<%= text_input fl, :alt %>
</div>
<div id="link-url">
<p>url</p>
<%= text_input fl, :url %>
</div>
</div>
<% end %>
<a phx-click="add-pic" phx-target={@myself} >Add Pic</a>
<div>
<%= submit "Save", phx_disable_with: "Saving..." %>
</div>
</.form>
</div>
My App.js
// Svelte
import 'svonix'
// We import the CSS which is extracted to its own file by esbuild.
// Remove this line if you add a your own CSS build pipeline (e.g postcss).
import "../css/app.css"
import { TextEditor } from './hooks/textEditHook'
let Hooks = {}
// WYSIWYG
Hooks.TextEditor = TextEditor
// If you want to use Phoenix channels, run `mix help phx.gen.channel`
// to get started and then uncomment the line below.
// import "./user_socket.js"
// You can include dependencies in two ways.
//
// The simplest option is to put them in assets/vendor and
// import them using relative paths:
//
// import "../vendor/some-package.js"
//
// Alternatively, you can `npm install some-package --prefix assets` and import
// them using a path starting with the package name:
//
// import "some-package"
//
// Include phoenix_html to handle method=PUT/DELETE in forms and buttons.
import "phoenix_html"
// Establish Phoenix Socket and LiveView configuration.
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {hooks: Hooks, params: {_csrf_token: csrfToken}})
// Show progress bar on live navigation and form submits
topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
window.addEventListener("phx:page-loading-start", info => topbar.show())
window.addEventListener("phx:page-loading-stop", info => topbar.hide())
// connect if there are any LiveViews on the page
liveSocket.connect()
// expose liveSocket on window for web console debug logs and latency simulation:
// >> liveSocket.enableDebug()
// >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session
// >> liveSocket.disableLatencySim()
window.liveSocket = liveSocket
My Hook (with comments)
import Quill from 'quill';
export let TextEditor = {
mounted() {
console.log('Mounting');
let quill = new Quill(this.el, {
theme: 'snow'
});
// Read more on what .on('events') you can put here
// https://quilljs.com/docs/api/#events
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 sends the event of
// def handle_event("text-editor", %{"text_content" => content}, socket) do
this.pushEventTo(this.el.phxHookId, "text-editor", {"text_content": quill.getContents()})
}
});
},
updated(){
console.log('U');
}
}
My handle-event
def handle_event("text-editor", %{"text_content" => content}, socket) do
IO.inspect(content)
{:noreply, socket}
end
Things to know
How to push event to desired live view controller…
Set the phx-target in phoenix
<div id="editor" phx-hook="TextEditor" phx-target={@myself} >
<%= text_input f, :description%>
</div>
In your js file reference it like this…
this.el.phxHookId
You will see above in the code snippet how this works together
Note: I’m using arrow functions so I don’t have to rebind this
#ShitJSDoes