Table of Contents
Let’s create a basic text editor using Quill
index.html
Example
Let’s customize the appearance of the text editor
Record the contents of the text editor in the console
Output
in conclusion
Home Web Front-end JS Tutorial Building a text editor with Quill.js

Building a text editor with Quill.js

Aug 23, 2023 pm 11:29 PM

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.

Let’s create a basic text editor using 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>
Copy after login

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>
Copy after login

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

tag with id="editor" and add some simple styles that specify the height. Please refer to the tag shown below.

<body>
   <div id="editor" style="height: 250px"></div>
</body>
Copy after login
In the above code, we create a
tag with the id "editor" and provide a simple style with a specified height of 250px

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. </p> <p>Consider the <b><script>tag</b> shown below. </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;script&gt; var quill = new Quill('#editor', { theme: 'snow' }); &lt;/script&gt; </pre><div class="contentsignin">Copy after login</div></div> <p>The above <script> tag should be placed at the end of the <body> tag, that is, before the <body> tag is closed. </p> <h3 id="index-html">index.html</h3> <p>The entire HTML code is shown below. </p> <h3 id="Example">Example</h3> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><!DOCTYPE html> <html lang="en"> &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt; &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt; &lt;title&gt;Quill Text Editor&lt;/title&gt; &lt;link href=&quot;https://cdn.quilljs.com/1.3.7/quill.snow.css&quot; rel=&quot;stylesheet&quot;&gt; &lt;script src=&quot;https://cdn.quilljs.com/1.3.7/quill.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; <body> <div id="editor" style="height: 200px"></div> <script> var quill = new Quill('#editor', { theme: 'snow' }); </script>

Copy after login

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.

Let’s customize the appearance of 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. </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;script&gt; let toolbarOptions = [ ['bold', 'italic', 'underline'] ] let quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); &lt;/script&gt; </pre><div class="contentsignin">Copy after login</div></div> <p> 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. </p> <p><b>index.html</b></p> <p> Shown below is the updated index.html file. </p> <h3 id="Example">Example</h3> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><!DOCTYPE html> <html lang="en"> &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt; &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt; &lt;title&gt;Quill Text Editor&lt;/title&gt; &lt;link href=&quot;https://cdn.quilljs.com/1.3.7/quill.snow.css&quot; rel=&quot;stylesheet&quot;&gt; &lt;script src=&quot;https://cdn.quilljs.com/1.3.7/quill.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; <body> <div id="editor" style="height: 200px"></div> <script> var toolbarOptions = [ ['bold', 'italic', 'underline'] ] var quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); </script>

Copy after login

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.

Record the contents of the text editor in the console

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>
Copy after login

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. </p> <p>Consider the updated <script> tag shown below. </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;script&gt; 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); } &lt;/script&gt; </pre><div class="contentsignin">Copy after login</div></div> 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</b></p> <p>The updated <b>index.html</b> code is as follows. </p> <h3 id="Example">Example</h3> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><!DOCTYPE html> <html lang="en"> &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt; &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt; &lt;title&gt;Quill Text Editor&lt;/title&gt; &lt;link href=&quot;https://cdn.quilljs.com/1.3.7/quill.snow.css&quot; rel=&quot;stylesheet&quot;&gt; &lt;script src=&quot;https://cdn.quilljs.com/1.3.7/quill.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; <body> <div id="editor" style="height: 200px"></div> &lt;button onclick=&quot;consoleHTMLContent()&quot;&gt;Print in Console&lt;/button&gt; <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>

Copy after login

If we run the above code in the browser, we will see a text editor. Once we enter some text in the editor and click the button, the root object of the quill text editor will be printed in the console. .

Output

I tried writing a simple line of code in the editor and then clicked the button and this is the output I got in the browser console.

<p>Hi There <strong>Inside HTML </strong><em>Is this italic?</em></p>
Copy after login

in conclusion

In this tutorial, we demonstrate how to use Quill.js to create a text editor with different toolbar options. Through multiple examples, we explain how to add or remove toolbars and how to control the root element in the Quill text editor.

The above is the detailed content of Building a text editor with Quill.js. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

Can PowerPoint run JavaScript? Can PowerPoint run JavaScript? Apr 01, 2025 pm 05:17 PM

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

See all articles