This repository was archived by the owner on May 22, 2018. It is now read-only.
forked from UmbraEngineering/quilljs-renderer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdoc.js
133 lines (111 loc) · 3.49 KB
/
doc.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var dom = require('./dom');
module.exports = Doc;
function Doc(formats, options) {
this.formats = formats;
this.document = options.document;
this.blockTag = options && options.blockTag || 'div';
this.inlineTag = options && options.inlineTag || 'span';
this.root = this.document.createElement('div');
}
Doc.prototype.getHTML = function() {
return this.root.innerHTML;
};
Doc.prototype.writeOp = function(op) {
if (op.insert === null || op.insert === undefined) {
throw new Error('Cannot convert delta with non-insert operations');
}
var attrs = op.attributes || {};
var text = (typeof op.insert === 'string') ?
op.insert.replace(/\r\n?/g, '\n') : '!';
var index = text.indexOf('\n');
while (index >= 0) {
this.writeText(text.slice(0, index), attrs);
this.formatLine(attrs);
this.line = null;
text = text.slice(index + 1);
index = text.indexOf('\n');
}
if (text.length > 0) {
this.writeText(text, attrs);
}
return this;
};
Doc.prototype.writeText = function(text, attrs) {
if (!this.line) {
this.line = this.document.createElement(this.blockTag);
this.root.appendChild(this.line);
}
if (!text.length) { return }
var node = this.document.createTextNode(text);
this.line.appendChild(node);
// TODO: loop through in priority order
// TODO: each format function returns a promise
for (var key in attrs) {
if (!attrs.hasOwnProperty(key)) { continue }
var format = this.formats[key];
if (format && format.type !== 'line') {
node = this.applyFormat(node, format, attrs[key]);
}
}
// TODO: optimize line?
this.line = this.root.lastChild;
};
Doc.prototype.applyFormat = function(node, format, value) {
if (format.tag) {
if (format.type === 'line') {
node = dom(node).switchTag(format.tag).get();
} else {
if (dom.VOID_TAGS[format.tag]) {
node = dom(node).replaceWith(this.document.createElement(format.tag)).get();
} else {
node = dom(node).wrap(format.tag).get();
}
}
}
if (format.parentTag) {
node = dom(node).wrap(format.parentTag).get();
if (node.previousSibling && node.tagName === node.previousSibling.tagName) {
dom(node.previousSibling).merge(node);
}
if (node.nextSibling && node.tagName === node.nextSibling.tagName) {
dom(node).merge(node.nextSibling);
}
}
if (format.attribute || format.class || format.style) {
if (dom(node).isTextNode()) {
node = dom(node).wrap(this.inlineTag).get();
}
if (format.attribute) {
node.setAttribute(format.attribute, value);
}
if (format.class) {
if (typeof node.classList === 'undefined') {
var newClass = format.class + value;
var className = node.className;
node.className = className.length ? (className + ' ' + newClass) : (className + newClass);
} else {
node.classList.add(format.class + value);
}
}
if (format.style && value !== format.default) {
node.style[format.style] = value;
}
}
if (typeof format.add === 'function') {
node = format.add(node, value, dom);
}
return node;
};
Doc.prototype.formatLine = function(attrs) {
var line = this.line;
// TODO: loop through in priority order
// TODO: each format function returns a promise
for (var name in attrs) {
if (!attrs.hasOwnProperty(name)) { continue }
var format = this.formats[name];
if (format && format.type === 'line') {
line = this.applyFormat(line, format, attrs[name]);
}
}
this.line = line;
};