Use Quill as a Rich Text Editor in ASP.NET Core


Use Quill as a Rich Text Editor in ASP.NET Core

Use an existing open-source rich text editor in your ASP.NET Core web applications.

new rich text editor for our project, we encountered Quill, an open-source rich text editor written in Javascript that, unlike TinyMCE or other 'free' components out there, doesn't try to tie you in to any cloud platform or come with other odd strings attached.

Setting up Quill in our project took a little research, but we've managed to put together some fast and easy instructions which we thought would be worth posting.

First, we add in the CDN references at the top of our page:

<script src="//"></script>
<link href="//" rel="stylesheet">

Then we wire up our <form> tag like so:

<form id="form" asp-controller="OurController" asp-action="SaveData" onsubmit="handleSubmit()">

In our web form, we set up two fields: the first one will be our 'hidden' field for binding back to our ASP.NET model object, and the second field is for our rich text editor, like this:

<input type="hidden" id="jdr" asp-for="@Model.description"/>
<div id="description">

The whole point of having a rich text editor is to generate 'rich text' i.e. HTML, so we want to make sure that when we go to display it, we're displaying what we're pulling out of our data source. In our data storage we convert this to/from Markdown, but when it comes to our display we're posting HTML - hence the @Html.Raw statement.

Finally, at the bottom of our page, we have some Javascript that binds everything together:

    let jdQuill = new Quill('#description', {
        theme: 'snow'
    function handleSubmit(){'handleSubmit called');
        document.getElementById('jdr').value = jdQuill.root.innerHTML;

In our case, we have several editor instances, so we set up a more 'permanent' function for the form submit. It also took some research to find out that the quill.root.innerHTML value was what will actually give you the value of the rich text editor - it would be nice if the Quill documentation actually said anything about that, instead of finding it on Stack Overflow.

When that doesn't work

In some circumstances, you won't find any inner HTML. When this happens, you'll have to extract the editor contents by using the getContents() method.

Let's modify our method above to reflect this:

  function handleSubmit(){'handleSubmit called');'quill contents: ' + JSON.stringify(jdQuill.getContents()));
      document.getElementById('jdr').value = JSON.stringify(jdQuill.getContents());

When using this command, you're going to get a JSON document containing Quill's Delta format, which creates a sequence of JSON objects containing the content with some metadata around the formats used in the editor contents.

For example - when we create some content that looks like this:

Quill editor in ASP.NET Core

We get JSON output that looks like this:

  {"insert":"Here is the first paragraph.\n\nList item 1"},
  {"insert":"List item 2"},{"attributes":
  {"insert":"Here's another paragraph, and now an image:\n"},

Notice too that the binary contents of the (rather large) image we added in the editor was embedded directly into the message.

Fortunately, we have some tools to help us make sense of this data format. Using blushingpenguin's Quill.Delta, we can turn the Delta data into HTML, and set up a custom BeforeRender where we can modify the <img> objects to load image contents into Cloudinary where we can manipulate and manage them better.

One word of caution - some copy/paste functions copy all the style from the source, which can mess up your HTML. The Quill.Delta package will process that into your HTML output, so be mindful of where your sources come from!

Looking for help building or modernizing your web applications in the cloud? Contact us for a quote - not only is it free, but we can help you find what you're looking for at a better price than most consulting firms!