loop-index

loop-index

TipTap editor in FormComponent not working

I am trying to integrate a TipTap editor into my edit FormComponent. Right now I have a separate LiveView component for the editor:

@impl true
  def render(%{value: %Phoenix.HTML.FormField{} = field} = assigns) do
    errors = if Phoenix.Component.used_input?(field), do: field.errors, else: []

    assigns
    |> assign(:errors, Enum.map(errors, &translate_error(&1)))

    ~H"""
    <div phx-hook="TextEditor" class="tiptap-editor" id={"editor-#{@name}"} phx-update="ignore" data-class={@class} data-value={@value.value}>
      <div data-editor={@name}></div>
      <.input type="text" name={@name} field={@value} data-editor-hidden={@name} phx-change="validate" />
      <.error :for={msg <- @errors}><%= msg %></.error>
    </div>
    """
  end

Called from the FormComponent:

<.simple_form
        for={@form}
        id="project-form"
        phx-target={@myself}
        phx-change="validate"
        phx-submit="save"
      >
        <.input field={@form[:title]} type="text" label="Title" />
        <.live_component
          module={FeedWeb.ProjectLive.EditorComponent}
          id="tiptap-editor"
          name="body"
          class="w-full p-2 mt-2 border rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6 border-zinc-300 focus:border-zinc-400"
          value={@form[:body]}
        />
        <%!-- <.input field={@form[:body]} type="textarea" rows="12" label="Body" /> --%>
        <:actions>
          <.button phx-disable-with="Saving..." class="px-2 py-1 text-white bg-blue-500 rounded-md">
            Save Project
          </.button>
        </:actions>
      </.simple_form>

And on app.js, some code to hydrate the editor:

const TextEditor = {
    editor: null,
    content: null,
    buttonSetup: {
        bold: { run: (editor) => editor.chain().focus().toggleBold().run(), check: (editor) => editor.isActive("bold") },
        italic: { run: (editor) => editor.chain().focus().toggleItalic().run(), check: (editor) => editor.isActive("italic") },
        h1: {
            run: (editor) => editor.chain().focus().toggleHeading({ level: 1 }).run(),
            check: (editor) => editor.isActive("heading", { level: 1 }),
        },
    },

    initialValue() {
        return this.el.dataset.value || "";
    },
    classes() {
        return this.el.dataset.class || "";
    },
    mounted() {
        const element = this.el.querySelector("[data-editor]");
        const hidden = this.el.querySelector("[data-editor-hidden]");
        const controlButtons = this.el.closest("form").querySelectorAll("[data-editor-control]");

        controlButtons.forEach((btn) => {
            btn.addEventListener("click", (e) => {
                const config = this.buttonSetup[e.target.dataset.editorControl];
                if (config && typeof config.run === "function") {
                    config.run(this.editor);
                }
            });
        });

        this.editor = new Editor({
            element,
            extensions: [
                StarterKit,
                Typography,
                Table.configure({
                    resizable: false,
                }),
                TableCell,
                TableHeader,
                TableRow,
            ],
            onUpdate: ({ editor }) => {
                this.content = editor.getHTML();
                hidden.value = this.content;
                // this.pushEventTo(this.el, "change", { value: this.content });
                // console.log("content", this.content);
            },
            content: this.initialValue(),
            editorProps: {
                attributes: {
                    class: this.classes()
                }
            },
            onTransaction: ({ editor }) => {
                controlButtons.forEach((btn) => {
                    const config = this.buttonSetup[btn.dataset.editorControl];
                    if (config && typeof config.check === "function") {
                        if (config.check(this.editor)) {
                            btn.classList.add("editor-active");
                        } else {
                            btn.classList.remove("editor-active");
                        }
                    }
                });
            },
        });
    },
};

(this would be put into the hooks param)

The problem is when I hit save, whatever typed into the editor is not saved. I’m guessing since I’m wrapping the entire editor component in phx-update=“ignore” the value of the hidden input is not being synced to the server and hence not submitted to the form. I have tried to use pushEventTo on editor change, but somehow that unfocuses the editor on every keystroke.

I’d appreciate it if someone can explain for me how this works and how I should fix it! Would really love to understand how this should work.

Marked As Solved

loop-index

loop-index

Thank you for responding! I have looked at that thread before, but the setup for TipTap seems more complex than Trix so I wasn’t able to figure out anything new from there.

I was able to solve my problem by just putting the editor and the hidden input in the top level form component instead of extracting it to a separate component. I guess maybe passing a form field down into a child component doesn’t track the changes too well.

<.simple_form
        for={@form}
        id="project-form"
        phx-target={@myself}
        phx-change="validate"
        phx-submit="save"
      >
        <.input field={@form[:title]} type="text" label="Title" />

         <%!-- TipTap editor --%>
        <div
          phx-hook="TextEditor"
          id="editor-wrapper"
          data-value={@form[:body].value}
          data-class="w-full p-2 mt-2 border rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6 border-zinc-300 focus:border-zinc-400"
        >
          <div data-editor="body" id="editor-component" phx-update="ignore"></div>
          <.input type="hidden" field={@form[:body]} data-editor-hidden="body" />
        </div>
        <:actions>
          <.button phx-disable-with="Saving..." class="px-2 py-1 text-white bg-blue-500 rounded-md">
            Save Project
          </.button>
        </:actions>
      </.simple_form>

This works fine now, I’d love to extract it out into a reuseable component but this will work for now.

My TipTap integration was based on this guide if anybody wants to do the same:

Also Liked

codeanpeace

codeanpeace

Welcome to elixirforum!

Take a look at this example of a WYIWYG JS editor populating a hidden input form field via a LiveView client side hook:

Where Next?

Trending in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement