Home >Web Front-end >JS Tutorial >Building a text editor with Quill.js
Quill is a free and open source text editor that falls into the category of WYSIWYG editors and is primarily used on the modern web we use today. It is a highly customizable text editor with many expressive APIs. Quill is very easy to use and provides a good interface that is easy to understand even for people with only markup experience.
In this tutorial, we will use multiple examples to explain how to build a text editor using Quill.js.
Although there are many rich text editors that are WYSIWYG text editors, the most widely used one is Quill, and the gap is very large. Now, let's learn how to use Quill.
The first step in working with Quill is to be able to use it in the editor of our choice, and to do this we need to place the two CDN links shown below in the
tag of our HTML code .<link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet"> <script src="https://cdn.quilljs.com/1.3.7/quill.js"></script>
The first CDN link is Quill’s CSS style file, while the second CDN link is Quill’s JavaScript file. We need to add the two lines of code shown above to the
tag of our HTML code.Our tag should look like this.
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Quill Text Editor</title> <link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet"> <script src="https://cdn.quilljs.com/1.3.7/quill.js"></script> </head>
Now that we have added the CDN in the
tag, it is time to start working on the tag. Inside the tag, let's create a<body> <div id="editor" style="height: 250px"></div> </body>In the above code, we create a
Now all that is left is to create a <script> tag inside of which we will create an instance of the Quill class and then pass the id of the <div> we created as the first parameter and the second A parameter is basically an object whose properties we specify in a text editor. </script>
Consider the <script>tag</script> shown below.
<script> var quill = new Quill('#editor', { theme: 'snow' }); </script>
The above <script> tag should be placed at the end of the <body> tag, that is, before the <body> tag is closed. </script>
The entire HTML code is shown below.
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Quill Text Editor</title> <link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet"> <script src="https://cdn.quilljs.com/1.3.7/quill.js"></script> </head> <script> var quill = new Quill('#editor', { theme: 'snow' }); </script>
If you open the above HTML file in your browser, you will see a text editor output in your browser. In the text editor that you will see, we will have a large number of toolbar options at our disposal, any of which we can use in the text editor.
Now suppose we only want to provide two default toolbar options instead of all the options you get by default in a normal text editor. In this case we can use the <script> tag shown below. </script>
<script> let toolbarOptions = [ ['bold', 'italic', 'underline'] ] let quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); </script>
In the <script> tag above, we have provided only three options, namely bold, italic and underline, in the toolbar, so only these options will be available to the text editor. </script>
index.html
Shown below is the updated index.html file.
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Quill Text Editor</title> <link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet"> <script src="https://cdn.quilljs.com/1.3.7/quill.js"></script> </head> <script> var toolbarOptions = [ ['bold', 'italic', 'underline'] ] var quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); </script>
If you run the above file in a browser, you will only see three toolbar options in the text editor, namely the bold option, italic option and underline option.
Now suppose we want to log what we write in the text editor to the console. In order to do this, we first need to create a button in the
tag.Consider the code snippet shown below that creates a button.
<button onclick="consoleHTMLContent()">Print in Console</button>
Now let's focus on the <script> tag, where we need to create a function that will actually log the contents of the quill text editor as well as some other toolbar options. </script>
Consider the updated <script> tag shown below. </script>
<script> let toolbarOptions = [ ['bold', 'italic', 'underline'],[{ 'size': ['small', false, 'large', 'huge'] }],[{ 'color': [] }, { 'background': [] }] ] let quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); function consoleHTMLContent() { console.log(quill.root.innerHTML); } </script>In the above <script> tag, we have a function called consoleHTMLContent in which I print the content present in the root property of the quill object <p><b>index.html <p>The updated <b>index.html code is as follows. <h3>Example <pre class='brush:javascript;toolbar:false;'><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Quill Text Editor</title> <link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet"> <script src="https://cdn.quilljs.com/1.3.7/quill.js"></script> </head> <body> <div id="editor" style="height: 200px"></div> <button onclick="consoleHTMLContent()">Print in Console</button> <script> let toolbarOptions = [ ['bold', 'italic', 'underline'],[{ 'size': ['small', false, 'large', 'huge'] }],[{ 'color': [] }, { 'background': [] }] ] let quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); function consoleHTMLContent() { console.log(quill.root.innerHTML); } </script>