Supplementary information for my answer to the Stack Overflow question about an Integrated Markdown WYSIWYG text editor.
It’s all implemented by a single wysiwyg.js file, plus its three dependencies to-markdown.js
, showdown.js
, and hallo.js
. Hallo itself depeds on jQueryUI and Rangy - these dependencies can be satisfied with the jquery-ui-rails
and rangy-rails
gems.
I place the four files in a new app/assets/javascripts/wysiwyg
directory.
$ ls app/assets/javascripts/wysiwyg
hallo.js showdown.js to-markdown.js wysiwyg.js
Just add to the Javascript assets:
//= require jquery-ui
//= require rangy-core
//= require_tree ./wysiwyg
The below is a rails view partial, a form containing a wysiwyg text input field:
<div id=item-cell-edit>
<p><%= operation.model.title %></p>
<%= simple_form_for operation.contract, remote: true do |f| %>
<%= f.input :title %>
<%= f.input :description, as: :text, input_html: {class: 'wysiwyg'} %>
<%= f.submit %>
<% end %>
(if that looks rails-wierd, take a look at trailblazer)
The [wysisyg.js] is triggered when the page is ready. It selects each <textarea>
on the page with a CSS class id of wysiwyg
, appends a new <div>
element after it and then runs hallo
on the <div>
(hallo
populates the <div>
). So, all you should need in your code is:
<textarea class='wysiwyg'>
...
</textarea>
(plus, of course loading the Javascript).
An optional div.wysiwyg
CSS class can be used to customise styling around the WYSIWYG <div>
. For example, the following is useful for Bootstrap:
div.wysiwyg {
height:auto !important;
min-height: 34px;
}