Reputation: 825
I am having real trouble understanding how to work with quill...
Saving the data is not a problem as that pretty straight forward :)
I am a little stuck on two points
can anyone offer any advice how to load the delta
{"ops":[{"insert":"this is a test bit of text\n"}]}
back into the quill editor and how to parse the output to create a page?
Thanks in advance for any replies!
Upvotes: 23
Views: 28012
Reputation: 12198
I was setting root.innerHTML
, but it messes up the actual HTML, the pasteHTML
method seems to be working fine, like so:
quill.pasteHTML(YOUR_HTML_HERE, 'silent');
I also got a code editor, and using following method:
let delta = quill.editor.getDelta();
delta.insert(VALUE_HERE, { 'code-block': true });
quill.updateContents(delta, 'silent');
You may also need to clear your contents before using the getDelta
method above, like so:
quill.setContents([
{ insert: '\n', attributes: { 'code-block': true } }
], 'silent');
I use to silent
the Source so this change is not triggered on my own binded event listeners.
Upvotes: 1
Reputation: 2718
Quill parses content by its own method.
If you are using HTML directly, use dangerouslyPasteHTML like the following:
quill.clipboard.dangerouslyPasteHTML("<p>here is some <strong>awesome</strong> text</p>");
This will sanitize the HTML by its own method and load HTML.
Upvotes: 2
Reputation: 800
// parse String
let stringToParse = String.raw`{"ops":[{"insert":"this is a test bit of text\n"}]}`;
// set quill editor instance to Delta state.
quill.setContents(JSON.parse());
setContents from Quill to set editor to Delta.
String.raw to avoid expanding '\t'
to ' '
. (see also JSON.parse throws error when parsing...)
Upvotes: 2
Reputation: 1082
I write this for people who is using Laravel. I did it like this:
To store, before submit my form:
form.submit(function(){
var description = document.querySelector('input[name=description]');
description.value = editor.root.innerHTML;
})
To edit my form, I load the content:
var editor = new Quill('#quill-editor', {
modules: {
toolbar: toolbarOptions
},
placeholder: 'type something',
theme: 'snow'
});
.root.innerHTML = '{!! !empty($quill_editor) ? $quill_editor : '' !!} ';
And if finally you want to use is as html, you can load it like this in your template:
{!! $description !!}
I hope is useful for someone
Upvotes: 5
Reputation: 1115
This works for me. May it will helps some one.
editor.root.innerHTML = "<p><strong class=\"ql-size-large\"><em><s><u>This would be the text that we are going to show in the editor with pre-formatting.<\/u><\/s><\/em><\/strong><\/p>";
Here editor
will be your quill instance.
Thanks to this LINK
Upvotes: 21
Reputation: 894
var quill = new Quill('#editor-container', { modules: { toolbar: [] }, readOnly: true, theme: 'bubble'} );
var $target = $('#editor-container');
var $content = JSON.parse($target[0].innerText);
quill.setContents($content);
Upvotes: 3
Reputation: 825
So I have managed to find a work around, not sure if it is the correct method, but it works.
Turns out it was javascript escaping the data passed to it.
Essentially when the form fails to submit the errored data is added back to the hidden input field and then javascript reads it from there...
HTML Form:
<input name="post" id="post" type="hidden" data-post-id="{{ old('post') }}">
Javascript:
var postId = $('#post').data("post-id");
quill.setContents(postId);
Upvotes: -1
Reputation: 1872
Use setContents to insert a delta:
quill.setContents({
"ops":[
{"insert":"this is a test bit of text\n"}
]
});
http://codepen.io/anon/pen/VKWZLd
You can access the raw HTML with:
var html = document.querySelector(".ql-editor").innerHTML
If you are working with raw HTML, you need to sanitize it before you use it.
Upvotes: 24