Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QuillJS doesn't work with textarea

I am trying to use QuillJS on a specific form field in my Django 1.10 template as follows:

<link href="https://cdn.quilljs.com/1.1.3/quill.snow.css" rel="stylesheet">

<script src="https://cdn.quilljs.com/1.1.3/quill.js"></script>
<!-- Initialize Quill editor -->
<script>
  var quill = new Quill('#id_text', {
    theme: 'snow'
  });
</script>

The problem is that Django renders the form field I want to use Quill on as a textarea instead of a div, and Quill doesn't seem to work on it: any effects applied to text don't register either visually or in the resulting output, and when I try to edit an existing record the initial text doesn't show up in the editor, even though it is there in the source HTML between the textarea tags.

Is it a known issue that Quill doesn't work with textarea or is there something else that could be wrong?

like image 729
voodoo-burger Avatar asked Nov 04 '16 22:11

voodoo-burger


People also ask

Can I use Quill with textarea?

Quill doesn't work directly with textarea. The example show you how to get the delta contents from Quill and add it to a form field. Sorry, something went wrong. Sorry, something went wrong. Quill doesn't work directly with textarea, any one help me on this.

What does gettext return when Quill is empty?

Note even when Quill is empty, there is still a blank line in the editor, so in these cases getText will return ‘ ’. The length parameter defaults to the length of the remaining document. Insert embedded content into the editor, returning a Delta representing the change. Source may be "user", "api", or "silent".

What is Quill rich text editor?

Quill Rich Text Editor. Quill is a free, open source WYSIWYG editor built for the modern web. With its modular architecture and expressive API, it is completely customizable to fit any need.

When are changes to Quill's editor content considered'user'or'user'?

Emitted when the contents of Quill have changed. Details of the change, representation of the editor contents before the change, along with the source of the change are provided. The source will be "user" if it originates from the users.


3 Answers

The following example works with jQuery, but it can be easily changed to run in plain javascript.

Step 1:
Add a class to your textarea, for example quill-editor:

<textarea name="input" placeholder="Textarea" class="form-control quill-editor">
    <p>Hello World!</p>
    <p>Some initial <strong>bold</strong> text</p>
    <p><br></p>
</textarea>

Step 2:

Add this javascript code after the textarea field:

$('.quill-editor').each(function(i, el) {
    var el = $(this), id = 'quilleditor-' + i, val = el.val(), editor_height = 200;
    var div = $('<div/>').attr('id', id).css('height', editor_height + 'px').html(val);
    el.addClass('d-none');
    el.parent().append(div);

    var quill = new Quill('#' + id, {
        modules: { toolbar: true },
        theme: 'snow'
    });
    quill.on('text-change', function() {
        el.html(quill.getContents());
    });
});

It will allow you to add as many editors as you like and it will update its coresponding textarea. No other code is required.

How it works:

$('.quill-editor').each(function(i, el) {
//...
});

for every quill-editor class it finds,

var el = $(this), id = 'quilleditor-' + i, val = el.val(), editor_height = 200;
var div = $('<div/>').attr('id', id).css('height', editor_height + 'px').html(val);
el.hide();
el.parent().append(div);

it will create a div after the textarea field with a unique id and a fixed height, which will be used by the quill editor instance. the original textarea will get hidden.

var quill = new Quill('#' + id, {
    modules: { toolbar: true },
    theme: 'snow'
});

a new Quill instance is started,

quill.on('text-change', function() {
    el.html(quill.getContents());
});

when its content will be changed, the textarea field will get updated.

like image 150
Binar Web Avatar answered Oct 06 '22 05:10

Binar Web


You can use Quill with a div and sync the editor's content(Delta) with a hidden input field in the form.

There is a Quill Form Submit example.

like image 27
Ben Browitt Avatar answered Oct 06 '22 04:10

Ben Browitt


Add this in module to work

['link']

Here is working perfectly.

like image 42
Lucas William Avatar answered Oct 06 '22 05:10

Lucas William