loop-index

loop-index

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.

Showing Posts 1 to 2

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:

loop-index

loop-index OP

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:

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews