Skip to content

Instantly share code, notes, and snippets.

@milesich
Last active July 5, 2017 22:42
Show Gist options
  • Save milesich/60e5f660c2420410a2c58707d67000e7 to your computer and use it in GitHub Desktop.
Save milesich/60e5f660c2420410a2c58707d67000e7 to your computer and use it in GitHub Desktop.
runkit file
var jsdom = require("jsdom")
exports.endpoint = function(request, response) {
if (request.method == 'POST') {
var jsonString = '';
request.on('data', function (data) {
jsonString += data;
});
request.on('end', function () {
renderQuill(jsonString, function (html, text) {
var out = JSON.stringify({"html": html, "text": text});
response.writeHead(200, {
'Content-Length': Buffer.byteLength(out, 'utf8'),
'Content-Type': 'application/json'}
);
response.end(out);
});
});
}
}
function renderQuill(deltas, callback) {
jsdom.env({
html: '<div id="editor-container"></div>',
scripts: [
'https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/0.7.24/MutationObserver.min.js',
'https://cdn.quilljs.com/1.2.6/quill.min.js'],
onload: function (window) {
var document = window.document;
// fake getSelection
// https://github.com/tmpvar/jsdom/issues/317
document.getSelection = function() {
return {
getRangeAt: function() {}
};
};
var container = document.getElementById("editor-container");
var quill = new window.Quill(container, {});
/*
var delta = {
ops: [
{ insert: 'Gandalf', attributes: { bold: true } },
{ insert: ' the ' },
{ insert: 'Grey', attributes: { color: '#ccc' } }
]
};*/
quill.setContents(JSON.parse(deltas));
callback(document.querySelector(".ql-editor").innerHTML, quill.getText());
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment