<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="178119" data-post-id="178119">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="wowlixir" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/wowlixir/120/18980_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  wowlixir
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>What a coincidence, I just visited the forum to post something else and saw your post. I’m also using this editor in my app.</p>
<p>A question: Where is your form and how are you loading the editor? Mine is in a <code>live_modal</code>. The only way I could make the editor (kinda) work was by placing a <code>&lt;script&gt;</code> tag inside a <code>phx-ignore</code> element. It doesn’t always load, though, and I don’t know why.</p>
<p>And a tip: SimpleMDE hasn’t been maintained in a long time. Use <a href="https://github.com/Ionaru/easy-markdown-editor" rel="noopener nofollow ugc">EasyMDE</a> instead!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="178119" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-add-js-loaded-textarea-with-liveview-form/32029/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-178119" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="178119"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="204107" data-post-id="204107">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="aweirdcorgi" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aweirdcorgi/120/21804_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  aweirdcorgi
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>hey <a class="mention" href="/u/wowlixir" rel="nofollow">@wowlixir</a>, were you able to make it work? how did you export the textarea in your template? i can´t get it to work</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="204107" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-add-js-loaded-textarea-with-liveview-form/32029/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-204107" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="204107"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="229118" data-post-id="229118">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="riverside" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  riverside
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hopefully this helps out anyone in the future. Based on the tip found <a href="https://forum.elixirforum.com/t/js-based-rich-text-editor-disappers-as-soon-as-page-loaded/30880/12" rel="nofollow">here</a>. For my use case, what i did was:</p>
<ol>
<li>Set up a form in a live component.</li>
</ol>
<pre data-code-wrap="elixir"><code class="lang-elixir">def render(assigns) do
  ~L"""
  &lt;div class="form_container"&gt;
    &lt;%= f = form for @changeset, 
       #,
       phx_target: @myself,
       phx_change: "validate_form" ,
       id: "richtext_form"
    %&gt;
      &lt;div 
        phx-hook="RichText"
        phx-update="ignore"
        phx-target="&lt;%= @myself %&gt;"
        id="richtext_container"
      &gt;
        &lt;%= textarea f, 
          :the_content, 
          value: @clientside_richtext_input 
        %&gt;
      &lt;/div&gt;
    &lt;/form&gt;
  &lt;/div&gt;
  """
end 
</code></pre>
<p>Reason being is i still want to validate the form input on the server side. But the pushing the params after a validation function in the returning <code>{:noreply, socket |&gt; ...}</code> kept on failing and the data being put into to the rich text wasn’t stored anywhere. Which led to:</p>
<ol start="2">
<li>Create two handle events. One for the form validation, the other to handle the clientside hook and pass the data into an assign that will be set as the value for the <code>textarea</code>.</li>
</ol>
<pre data-code-wrap="elixir"><code class="lang-elixir">def handle_event(
  "validate_form", 
  %{"richtext_form" =&gt; params}, 
  socket) do 
  {:noreply, socket |&gt; validate_form(params)}
end

def handle_event(
  "handle_clientside_richtext", 
  %{"richtext_data" =&gt; richtext_data},
  socket) do 
  {:noreply, 
    socket 
    |&gt; assign_form(:clientside_richtext_input, richtext_data)
    |&gt; push_event("richtext_event", %{richtext_data: richtext_data})}
end
</code></pre>
<p>This is so that the richtext data can be stored by cycling back and forth between the client and backend for when the live component is updated. Now for the same thing inside the JS code.</p>
<ol start="3">
<li>In a hook object do</li>
</ol>
<pre data-code-wrap="elixir"><code class="lang-elixir">const RichText = {
  mounted(){
    init(this.el.querySelector("#richtext_form_the_content"))
  },
  updated(){
    const textArea = init(this.el.querySelector("#richtext_form_the_content"));
    textArea.codemirror.on("change", ( ) =&gt; {
      this.pushEventTo(
        this.el, 
        "handle_clientside_richtext",
        {richtext_data: textArea.value()}
      );
      this.handleEvent(
        "richtext_event",
        (richtext_data) =&gt; textArea.value(richtext_data)
      )
    })
  }
}
</code></pre>
<p>The logic is in the <code>updated</code> function since mounted rich text area gets erased right away.</p>
<ol start="4">
<li>And finally to have easyMDE be usable do</li>
</ol>
<pre data-code-wrap="elixir"><code class="lang-elixir">import easyMDE from "easyMDE";

const init = (element) =&gt; new easyMDE({
  element: element,
  forceSync: true
})
</code></pre>
<p>The <code>forceSync: true</code> is for the data to be set as the <code>textarea</code> value on the clientside and play well with the backend.</p>
<p>It took a few days of looking back and forth at different examples, reading the forum, rereading the docs, looking at source code, and feeling dumb. This is the simplest solution that i can think of once it all clicked.</p>
<p>*edit typo fix: <code>phx-update: "TextEditor"</code> to <code>phx-hook: "RichText"</code></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="229118" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-add-js-loaded-textarea-with-liveview-form/32029/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-229118" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="229118"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>