Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close the quill editor when done with editing

I am dynamically instantiating the QuillJS editor on a click event of an element in the DOM. So my requirement is that once the user is done with the editing for that element, he/she should be able to close the editor. Currently, I do not see any close method in the quill API. The enable/disable API methods won't work for me as I want to close the editor completely and show the user the same view he/she had before seeing the quill editor but of course with the saved changes.

The demo of this can be seen here
https://codepen.io/curiousdj/pen/eEjbPK

const options = { theme: "snow" };
var divs = document.getElementsByTagName("div");
var initializeQuill = function (e){
     if(!this.quill){
        console.log(e);
        this.target = event.currentTarget;
        this.quill = new Quill(this.target, options);
        this.target.children[0].onclick = function(et) { et.preventDefault(); };
        this.target.children[0].onblur = function(l) {
               l.target.parentElement.quill.close;
         };
        }
        this.quill.focus();
        e.stopPropagation();
        e.preventDefault();
}
for(var i = 0; i < divs.length; i++){
    divs[i].onclick = initializeQuill;
}
like image 350
Dhananjay Pal Avatar asked Aug 25 '17 08:08

Dhananjay Pal


People also ask

How do I turn off Quill editor?

quill. disable() should do the trick.

How do I turn off react Quill editor?

in React Quill 1.3. 5, you can add the readOnly prop as true. It will disable your editor like input tag when disabled.

What is quill rich 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.

How to disable the Quill editor?

'click #editNote': (e, t) -> note = t.find '.container' console.log note basicEditor = new Quill (note) Then once the user clicks save I want to be able to disable the Quill editor.

How to turn a template into an editable Quill editor?

When the user clicks edit I turn the template into an editable Quill editor like so: 'click #editNote': (e, t) -> note = t.find '.container' console.log note basicEditor = new Quill (note)

How do I set up Quill?

Quill requires a container where the editor will be appended. You can pass in either a CSS selector or a DOM object. To configure Quill, pass in an options object: The following keys are recognized: DOM Element or a CSS selector for a DOM Element, within which the editor’s ui elements (i.e. tooltips, etc.) should be confined.

What is quilljs text editor?

QuillJS is a beautiful open-source text editor for web applications. In this tutorial I’m going to show you how to configure it, and in the second step we will get the typed text.


Video Answer


2 Answers

I'd advise either:

  1. Copy the contents of the quill instance, destroy the DOM element that the instance is bound to, create a new DOM element and paste the contents back in
  2. Use disable via the API as you've already tried, but also style disabled Quill instances to not show any differentiation between Quill's formatting and your own.

I've updated your pen here to give an example of the second option with basic changes

.ql-editor {
  padding:0;
  line-height:inherit;
}

.ql-editor p {
  margin-bottom:10px;
}

.ql-toolbar {
  display:none;
}

.ql-container.ql-snow {
  border:none;
  font-family:inherit;
  font-size:inherit;
}
like image 66
Joe Attard-Owen Avatar answered Oct 07 '22 00:10

Joe Attard-Owen


This work for me:

function destory_editor(selector){
    if($(selector)[0])
    {
        var content = $(selector).find('.ql-editor').html();
        $(selector).html(content);

        $(selector).siblings('.ql-toolbar').remove();
        $(selector + " *[class*='ql-']").removeClass (function (index, css) {
           return (css.match (/(^|\s)ql-\S+/g) || []).join(' ');
        });

        $(selector + "[class*='ql-']").removeClass (function (index, css) {
           return (css.match (/(^|\s)ql-\S+/g) || []).join(' ');
        });
    }
    else
    {
        console.error('editor not exists');
    }
}

To use it just call:

destory_editor('.editor');
like image 1
Mohamad Hamouday Avatar answered Oct 06 '22 22:10

Mohamad Hamouday