Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quill.js how to use setText with basic formatting

I'm having trouble understanding how I would populate the Quill editor with some basic HTML text using plain js.

I'm passing text with basic formatting (just bold, italic, underline and header tags) so there aren't any advanced RTF features to worry about.

So for example: My javascript might be something like

var postContent = "<h2>My short post</h2><p>This is a <strong>really, really</strong> short post.</p>";

quill.setText(postContent, "api");

I thought I'd be able to use quill.setText(postContent, "api") but that seems to display the HTML tags instead of rendering the formatted text.

Do I actually need to parse the code into a blot? Seems like there must be a way to get Quill to render that basic HTML, as it seems to work if I paste simple formatted text directly into Quill.

One thing I've tried is simply doing:

quill.root.innerHTML = postContent;

Which seems to work visibly, but I'm worried that I'm now doing something a little hacky and I might not actually be using Quill as intended. Shouldn't there be a way to accomplish this via the Quill API?

Any insight? Thanks.

like image 864
Bangkokian Avatar asked Oct 16 '25 13:10

Bangkokian


1 Answers

There is unsafe deprecated function dangerouslyPasteHTML: https://quilljs.com/docs/modules/clipboard/#dangerouslypastehtml

It's better to use setContents or updateContents with Delata of ops with attributes: https://quilljs.com/docs/api/#updatecontents

like image 110
Ben Browitt Avatar answered Oct 19 '25 07:10

Ben Browitt