-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (48 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict'
const Promise = require('promise')
const jsdom = require('jsdom')
const quill = require.resolve('quill')
const mutationObserver = require.resolve('mutation-observer')
function _render (delta, options, scripts, formats, callback) {
jsdom.env('<div id="editor"></div>', scripts, (err, window) => {
if (err) return callback(err)
window.document.getSelection = function () {
return {
getRangeAt: function () {}
}
}
// register custom format
for (let name in formats) {
window.Quill.register(name, formats[name])
}
let quill = new window.Quill('#editor', options)
quill.setContents(delta)
callback(err, window.document.querySelector('.ql-editor').innerHTML)
window.close()
})
}
module.exports = function render (delta, options, callback) {
// option is optional
let args = []
for (let i = 0; i < arguments.length; i++) {
args.push(arguments[i])
}
delta = args.shift()
callback = (typeof args[args.length - 1] === 'function') ? args.pop() : undefined
if (args.length > 0) options = args.shift()
else options = {}
let scripts = [quill, mutationObserver]
if (options.scripts !== undefined) scripts = scripts.concat(options.scripts)
let formats = {}
if (options.formats !== undefined) formats = options.formats
if (typeof callback === 'function') { // Using challback
return _render(delta, options, scripts, formats, callback)
} else { // Using promise
return new Promise(function (resolve, reject) {
return _render(delta, options, scripts, formats, function (err, output) {
if (err) return reject(err)
return resolve(output)
})
})
}
}