forked from zenoamaro/react-quill
-
Notifications
You must be signed in to change notification settings - Fork 11
/
toolbar.js
174 lines (152 loc) · 4.42 KB
/
toolbar.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
var React = require('react'),
ReactDOMServer = require('react-dom/server'),
T = React.PropTypes;
var defaultColors = [
'rgb( 0, 0, 0)', 'rgb(230, 0, 0)', 'rgb(255, 153, 0)',
'rgb(255, 255, 0)', 'rgb( 0, 138, 0)', 'rgb( 0, 102, 204)',
'rgb(153, 51, 255)', 'rgb(255, 255, 255)', 'rgb(250, 204, 204)',
'rgb(255, 235, 204)', 'rgb(255, 255, 204)', 'rgb(204, 232, 204)',
'rgb(204, 224, 245)', 'rgb(235, 214, 255)', 'rgb(187, 187, 187)',
'rgb(240, 102, 102)', 'rgb(255, 194, 102)', 'rgb(255, 255, 102)',
'rgb(102, 185, 102)', 'rgb(102, 163, 224)', 'rgb(194, 133, 255)',
'rgb(136, 136, 136)', 'rgb(161, 0, 0)', 'rgb(178, 107, 0)',
'rgb(178, 178, 0)', 'rgb( 0, 97, 0)', 'rgb( 0, 71, 178)',
'rgb(107, 36, 178)', 'rgb( 68, 68, 68)', 'rgb( 92, 0, 0)',
'rgb(102, 61, 0)', 'rgb(102, 102, 0)', 'rgb( 0, 55, 0)',
'rgb( 0, 41, 102)', 'rgb( 61, 20, 10)',
].map(function(color){ return { value: color } });
var defaultItems = [
{ label:'Formats', type:'group', items: [
{ label:'Font', type:'font', items: [
{ label:'Sans Serif', value:'sans-serif', selected:true },
{ label:'Serif', value:'serif' },
{ label:'Monospace', value:'monospace' }
]},
{ label:'Size', type:'size', items: [
{ label:'Small', value:'10px' },
{ label:'Normal', value:'13px', selected:true },
{ label:'Large', value:'18px' },
{ label:'Huge', value:'32px' }
]},
{ label:'Alignment', type:'align', items: [
{ label:'', value:'', selected:true },
{ label:'', value:'center' },
{ label:'', value:'right' },
{ label:'', value:'justify' }
]}
]},
{ label:'Text', type:'group', items: [
{ type:'bold', label:'Bold' },
{ type:'italic', label:'Italic' },
{ type:'strike', label:'Strike' },
{ type:'underline', label:'Underline' },
{ type:'color', label:'Color', items:defaultColors },
{ type:'background', label:'Background color', items:defaultColors },
{ type:'link', label:'Link' }
]},
{ label:'Blocks', type:'group', items: [
{ type:'list', value:'bullet' },
{ type:'list', value:'ordered' }
]},
{ label:'Blocks', type:'group', items: [
{ type:'image', label:'Image' }
]}
];
var QuillToolbar = React.createClass({
displayName: 'Quill Toolbar',
propTypes: {
id: T.string,
className: T.string,
items: T.array
},
getDefaultProps: function(){
return {
items: defaultItems
};
},
renderGroup: function(item, key) {
return React.DOM.span({
key: item.label || key,
className:'ql-formats' },
item.items.map(this.renderItem)
);
},
renderChoiceItem: function(item, key) {
return React.DOM.option({
key: item.label || item.value || key,
value:item.value },
item.label
);
},
renderChoices: function(item, key) {
var attrs = {
key: item.label || key,
title: item.label,
className: 'ql-'+item.type
};
var self = this;
var choiceItems = item.items.map(function(item, key) {
if (item.selected) {
attrs.defaultValue = item.value;
}
return self.renderChoiceItem(item, key);
})
return React.DOM.select(attrs, choiceItems);
},
renderButton: function(item, key) {
return React.DOM.button({
type: 'button',
key: item.label || item.value || key,
value: item.value,
className: 'ql-'+item.type,
title: item.label },
item.children
);
},
renderAction: function(item, key) {
return React.DOM.span({
key: item.label || item.value || key,
className: 'ql-'+item.type,
title: item.label },
item.children
);
},
renderItem: function(item, key) {
switch (item.type) {
case 'group':
return this.renderGroup(item, key);
case 'font':
case 'align':
case 'size':
case 'color':
case 'background':
return this.renderChoices(item, key);
case 'bold':
case 'italic':
case 'underline':
case 'strike':
case 'link':
case 'list':
case 'image':
return this.renderButton(item, key);
default:
return this.renderAction(item, key);
}
},
getClassName: function() {
return 'quill-toolbar ' + (this.props.className||'');
},
render: function() {
var children = this.props.items.map(this.renderItem);
var html = children.map(ReactDOMServer.renderToStaticMarkup).join('');
return React.DOM.div({
className: this.getClassName(),
style: this.props.style || {},
dangerouslySetInnerHTML: { __html:html }
});
}
});
module.exports = QuillToolbar;
QuillToolbar.defaultItems = defaultItems;
QuillToolbar.defaultColors = defaultColors;