Search code examples
htmlruby-on-railswysiwygfroalapre

Froala code snippet losing breaklines on edit


I am making a simple blog in RoR. When I edit a post I am losing all linebreakes in the code snippet. How can I make sure that when I am reloading the content of the post, the editor correctly formats the code snippets?

This is the input into the text editor:

A B

This is the source code in the froala editor generated using a custom button:

<pre><code>A B</code></pre>

This is the data inserted into the database:

"<pre><code>A\r\nB</code></pre>"

This is how it looks like on the web page:

enter image description here

This is how it looks like in the Froala editor when I try to update the post:

enter image description here

This is how the source code looks like this time:

<pre><code>AB</code></pre>

This is the code for the custom button:

customButtons: {
insertCode: {
title: 'Insert code',
  icon: {
    type: 'font',
      value: 'fa fa-code'
  },
    callback: function() {
      if (!this.selectionInEditor()) {
        this.$element.focus(); // Focus on editor if it's not.
      }

      var html = '<pre><code>' + (this.text() || '&#8203;') + '<span class="f-marker" data-type="false" data-id="0" data-fr-verified="true"></span><span class="f-marker" data-type="true" data-id="0" data-fr-verified="true"></span></code></pre>';

      this.insertHTML(html);
      this.restoreSelectionByMarkers();
      this.saveUndoStep();
    }
  }
}

-------------------------Update-----------------------------

The problem seems to be with the froala initiation function:

step 1: create a textarea with the content:

<textarea id="froalaedit" name="content">
<pre><code>A
B</code></pre>
</textarea>

step 2: add a button to initiate the froala editor:

<button onclick="myFunction()">Click me</button>

<script>
function myFunction(){
  $('textarea#froalaedit').editable({
    inlineMode: false,
    buttons: ['html', 'removeFormat', 'sep', 'undo', 'redo', 'sep', 'insertHorizontalRule', 'bold', 'italic', 'underline', 'strikeThrough', 'subscript', 'superscript', 'sep', 'fontFamily', 'fontSize', 'color', 'sep', 'formatBlock', 'blockStyle', 'align', 'sep', 'insertOrderedList', 'insertUnorderedList', 'sep', 'createLink', 'insertImage', 'insertVideo', 'table', 'uploadFile', 'fullscreen', 'insertCode'],
    minHeight: 200,
    customButtons: {
      insertCode: {
        title: 'Insert code',
        icon: {
          type: 'font',
          value: 'fa fa-code'
        },
        callback: function() {
          if (!this.selectionInEditor()) {
            this.$element.focus(); // Focus on editor if it's not.
          }

          var html = '<pre><code>' + (this.text() || '&#8203;') + '<span class="f-marker" data-type="false" data-id="0" data-fr-verified="true"></span><span class="f-marker" data-type="true" data-id="0" data-fr-verified="true"></span></code></pre>';

          this.insertHTML(html);
          this.restoreSelectionByMarkers();
          this.saveUndoStep();
        }
      }
    }
  })
};
</script>

Step 3: The behaviour is replicated when the button is pressed.

--------------------Fixed------------------------

did this:

<div id="eg-textarea"><%= @post.content.html_safe %></div>

instead of this:

<%= f.text_area :content, rows: 40, id: 'eg-textarea' %>

Solution

  • There was a problem with \n inside textarea and it was not rendered properly. You can either use a div instead of textarea or use the master version from Github which fixes the issue.