Last active
March 20, 2024 16:27
-
-
Save fxmontigny/58ffb86108aba8e80c606adcc3341036 to your computer and use it in GitHub Desktop.
Insert and get html content with quilljs editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const editor = $('.editor'); | |
const quill = new Quill(editor); | |
// set html content | |
quill.setHTML = (html) => { | |
editor.root.innerHTML = html; | |
}; | |
// get html content | |
quill.getHTML = () => { | |
return editor.root.innerHTML; | |
}; | |
// usages | |
quill.setHTML('<b>Hello my friend</b>'); | |
quill.on('text-change', () => { | |
console.log('get html', quill.getHTML()); | |
}); |
If you aren't getting the desired output. It could be because your html content is encoded.
Use this to convert it.
var Title = $('<textarea />').html("<p><strong>Hello</strong></p><p><br></p>").text();
console.log(Title);
This code will output
<p><strong>Hello</strong></p>
After this you can use this command to set the html content in quill editor
quill.root.innerHTML = realHTML;
or even this:
let initialContent = quill.clipboard.convert(realHTML);
quill.setContents(initialContent, 'silent');
Its proper your html is in the real html format before setting the value on quill. Else the html tags would be displayed verbally.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This worked for me too, specially to get the editor styles inlined instead of getting
ql-...
class names on the HTML which wouldn't work in contexts where QuillJS styles aren't loaded.It uses the built in function to inject HTML into the editor (
pastHTML
) which should be a better route to implement this in case the selectors above don't work the next version due to internal implementation changes on the editor. It also usesquill-delta-to-html
which helps in getting the css syles inlined and is a more structured way to convert Delta to HTML and follows the design guidelines of QuillJS.