<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jens</title>
    <description>The latest articles on DEV Community by Jens (@halfbloody).</description>
    <link>https://dev.to/halfbloody</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F834704%2F34f91287-6bf0-4d4d-acab-333bc82028aa.png</url>
      <title>DEV Community: Jens</title>
      <link>https://dev.to/halfbloody</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/halfbloody"/>
    <language>en</language>
    <item>
      <title>How to Debug Javascript in a Rails Gem</title>
      <dc:creator>Jens</dc:creator>
      <pubDate>Fri, 16 Feb 2024 11:08:38 +0000</pubDate>
      <link>https://dev.to/halfbloody/how-to-debug-javascript-in-a-rails-gem-4bm3</link>
      <guid>https://dev.to/halfbloody/how-to-debug-javascript-in-a-rails-gem-4bm3</guid>
      <description>&lt;p&gt;I encountered the issue when trying to get the morph feature of Turbo 8 to work. The page just kept refreshing and I didn't understand why. There were no good tutorials yet so I had to dig into the gem.&lt;/p&gt;

&lt;p&gt;First, I opened it with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle open turbo-rails
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is see that the gem has 3 javascript files:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app/assets/javascripts/turbo.js&lt;/code&gt;&lt;br&gt;
&lt;code&gt;app/assets/javascripts/turbo.min.js&lt;/code&gt;&lt;br&gt;
&lt;code&gt;app/assets/javascripts/turbo.min.js.map&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I added some log statements in turbo.js but realized that I have to change the minified version &lt;code&gt;turbo.min.js&lt;/code&gt;. For convenience, I changed the turbo.js file as it's easier to read and recompiled the min.js file with terser.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install terser -g
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terser turbo.js -o turbo.min.js --compress --mangle 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that I was able to add log statements to the gem's javascript and figure out the issue.&lt;/p&gt;

&lt;p&gt;After you play with a gem don't forget to reset its code.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>debugging</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Replacing Trix with Tinymce in Rails 6.1 ActionText</title>
      <dc:creator>Jens</dc:creator>
      <pubDate>Wed, 03 Aug 2022 12:51:00 +0000</pubDate>
      <link>https://dev.to/halfbloody/replacing-trix-with-tinymce-in-rails-61-actiontext-3e75</link>
      <guid>https://dev.to/halfbloody/replacing-trix-with-tinymce-in-rails-61-actiontext-3e75</guid>
      <description>&lt;p&gt;Summary:&lt;br&gt;
If you want to replace Trix you should not use ActionText. It works kind of but adds a lot of Trix-specific content (like the "trix-content" blocks around your edit text which you don't need.&lt;/p&gt;

&lt;p&gt;The easiest way is to just add a &lt;code&gt;text&lt;/code&gt;-field to your model and use &lt;code&gt;&amp;lt;%= sanitize content %&amp;gt;&lt;/code&gt; in your view.&lt;/p&gt;

&lt;p&gt;Tinymce was very easy to install. See my summary here.&lt;/p&gt;

&lt;p&gt;As Trix is very limited in scope and doesn't allow you to add attributes to elements (for example you can't add target="_blank" to a link to open it in a new tab) and it doesn't allow you to add classes and in general has a lot of problems as it's seems not to be actively maintained it makes sense to look for other options.&lt;/p&gt;

&lt;p&gt;After looking around I started to experiment to replace Trix with Tinymce, but was wondering if it makes sense to keep ActionText.&lt;/p&gt;

&lt;p&gt;It was surprisingly easy to replace Trix with TinyMCE which was recommended to me in a Reddit Thread.&lt;/p&gt;

&lt;p&gt;My Stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rails 6.1.4&lt;/li&gt;
&lt;li&gt;Ruby 3.0&lt;/li&gt;
&lt;li&gt;jsbundling-rails with esbuild&lt;/li&gt;
&lt;li&gt;Turbo and Stimulus&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You just add tinymce to your package.json and run &lt;code&gt;yarn install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You want to wrap Tinymce in a Stimulus controller to easily be able to add it to any textfield.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails generate stimulus tinymce&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will make every textfield a Tinymce editor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import tinymce from 'tinymce/tinymce'
import 'tinymce/icons/default/icons'
import 'tinymce/themes/silver/theme';
import 'tinymce/skins/ui/oxide/skin.min.css';
import 'tinymce/models/dom';

import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="tinymce"
export default class extends Controller {
  connect() {
    tinymce.init({
      selector: 'textarea',  // change this value according to your HTML
    });
  }

  disconnect () {
    tinymce.remove()
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that you just add the stimulus controller to the encapsulating block and change the rich_text_area to a text_area (here my views using HAML and Tailwind)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    .flex.flex-col.gap-3.mb-3
      = f.label :add_text_block, class: "font-bold text-xl"
      = f.rich_text_area :text, class: "", id: "editor_#{block.id}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    .flex.flex-col.gap-3.mb-3{ data: { controller: "tinymce" } }
      = f.label :add_text_block, class: "font-bold text-xl"
      = f.text_area :text, class: "", id: "editor_#{block.id}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your view will show Tinymce Editor instead of Trix editor. And the best part is that all changes get automatically saved. So &lt;code&gt;has_rich_text :text&lt;/code&gt; in the model works also with other editors.&lt;/p&gt;

&lt;p&gt;Now I just need to figure out how to add links and images to Tinymce and how to customize the elements with attributes and classes. The main reason Trix didn't work for me.&lt;/p&gt;

&lt;p&gt;So, the next step is to figure out how plugins work with Tinymce. &lt;/p&gt;

&lt;p&gt;Plugins are also quite easy to add. Just import:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'tinymce/plugins/link';
import 'tinymce/plugins/code';
import 'tinymce/plugins/image';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And customize it in the intializer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      plugins: 'link image',  // note the comma at the end of the line!
      toolbar: 'h2 link image',
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
