UNPKG

7.29 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.stateToStyle = exports.generateConfigState = exports.generateConfig = undefined;
7
8var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
9
10var _objHasObj = require('obj-has-obj');
11
12var _objHasObj2 = _interopRequireDefault(_objHasObj);
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16var fontSizeList = [9, 10, 11, 12, 13, 14, 18, 24, 30, 36, 48, 64, 72].map(function (font) {
17 return {
18 type: 'font-' + font,
19 label: font,
20 style: {
21 fontSize: font + 'px'
22 }
23 };
24});
25
26var lineHeightList = [1.0, 1.15, 1.5, 2.0, 2.5, 3.0].map(function (height) {
27 return {
28 type: 'line-height-' + height,
29 label: height,
30 style: {
31 lineHeight: height
32 }
33 };
34});
35
36var toolbarConfig = {
37 display: ['INLINE_STYLE_BUTTONS', 'FONT_HIGHLIGHT_BUTTON', 'FONT_COLOR_BUTTON', 'FONT_SIZE_DROPDOWN', 'FONT_FAMILY_DROPDOWN', 'LINE_HEIGHT_DROPDOWN', 'ALIGN_DROPDOWN'],
38 INLINE_STYLE_BUTTONS: [{
39 type: 'bold',
40 style: {
41 fontWeight: 'bold'
42 }
43 }, {
44 type: 'italic',
45 style: {
46 fontStyle: 'italic'
47 }
48 }, {
49 type: 'underline',
50 style: {
51 textDecoration: 'underline'
52 }
53 }],
54 FONT_SIZE_DROPDOWN: fontSizeList,
55 LINE_HEIGHT_DROPDOWN: lineHeightList,
56 FONT_FAMILY_DROPDOWN: [{
57 label: 'Helvetica neue',
58 style: {
59 fontFamily: 'Helvetica neue'
60 }
61 }, {
62 label: "Arial",
63 style: {
64 fontFamily: "Arial"
65 }
66 }, {
67 label: "Arial Narrow",
68 style: {
69 fontFamily: "Arial Narrow"
70 }
71 }, {
72 label: "Arial Black",
73 style: {
74 fontFamily: "Arial Black"
75 }
76 }, {
77 label: "Courier New",
78 style: {
79 fontFamily: "Courier New"
80 }
81 }, {
82 label: "Georgia",
83 style: {
84 fontFamily: "Georgia"
85 }
86 }, {
87 label: "Lucida Console",
88 style: {
89 fontFamily: "Lucida Console"
90 }
91 }, {
92 label: "Tahoma",
93 style: {
94 fontFamily: "Tahoma"
95 }
96 }, {
97 label: "Times New Roman",
98 style: {
99 fontFamily: "Times New Roman"
100 }
101 }, {
102 label: "Verdana",
103 style: {
104 fontFamily: "Verdana"
105 }
106 }, {
107 label: "微軟正黑體",
108 style: {
109 fontFamily: "Microsoft JhengHei, 微軟正黑體"
110 }
111 }, {
112 label: "標楷體",
113 style: {
114 fontFamily: "DFKai-sb, 標楷體"
115 }
116 }],
117 ALIGN_DROPDOWN: [{
118 label: 'Align Left',
119 style: {
120 textAlign: 'left'
121 }
122 }, {
123 label: 'Align Center',
124 style: {
125 textAlign: 'center'
126 }
127 }, {
128 label: 'Align Right',
129 style: {
130 textAlign: 'right'
131 }
132 }]
133};
134
135var aryObjToObj = function aryObjToObj(ary, key) {
136 var obj = {};
137 var _iteratorNormalCompletion = true;
138 var _didIteratorError = false;
139 var _iteratorError = undefined;
140
141 try {
142 for (var _iterator = ary[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
143 var item = _step.value;
144
145 obj[item[key]] = item;
146 }
147 } catch (err) {
148 _didIteratorError = true;
149 _iteratorError = err;
150 } finally {
151 try {
152 if (!_iteratorNormalCompletion && _iterator.return) {
153 _iterator.return();
154 }
155 } finally {
156 if (_didIteratorError) {
157 throw _iteratorError;
158 }
159 }
160 }
161
162 return obj;
163};
164
165var generateConfig = exports.generateConfig = function generateConfig() {
166 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
167
168 // change array of object to object
169 config = Object.assign({}, toolbarConfig, config);
170 config.inlineStyleObj = aryObjToObj(config.INLINE_STYLE_BUTTONS, 'type');
171 config.fontSizeObj = aryObjToObj(config.FONT_SIZE_DROPDOWN, 'label');
172 config.lineHeightObj = aryObjToObj(config.LINE_HEIGHT_DROPDOWN, 'label');
173 config.alignObj = aryObjToObj(config.ALIGN_DROPDOWN, 'label');
174 config.fontFamilyObj = aryObjToObj(config.FONT_FAMILY_DROPDOWN, 'label');
175 return config;
176};
177
178var generateConfigState = exports.generateConfigState = function generateConfigState(config) {
179 var defaultStyle = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
180
181 var activeInlineStyle = void 0;
182 var activeColor = defaultStyle.color || '#000';
183 var activeBackgroundColor = defaultStyle.backgroundColor || 'none';
184 var activeFontSize = void 0;
185 var activeLineHeight = void 0;
186 var activeAlign = 'Align Left'; // default
187 var activeFontFamily = config.FONT_FAMILY_DROPDOWN[0].label;
188 if (config.INLINE_STYLE_BUTTONS) {
189 activeInlineStyle = Object.values(config.INLINE_STYLE_BUTTONS).reduce(function (obj, item) {
190 obj[item.type] = false;
191 if ((0, _objHasObj2.default)(defaultStyle, item.style)) {
192 obj[item.type] = true;
193 }
194 return obj;
195 }, {});
196 }
197 if (config.ALIGN_DROPDOWN && defaultStyle.textAlign) {
198 Object.values(config.ALIGN_DROPDOWN).forEach(function (item) {
199 if (item.style.textAlign === defaultStyle.textAlign) {
200 activeAlign = item.label;
201 }
202 });
203 }
204 if (config.FONT_FAMILY_DROPDOWN && defaultStyle.fontFamily) {
205 Object.values(config.FONT_FAMILY_DROPDOWN).forEach(function (item) {
206 if (item.style.fontFamily === defaultStyle.fontFamily) {
207 activeFontFamily = item.label;
208 }
209 });
210 }
211 if (config.FONT_SIZE_DROPDOWN && config.FONT_SIZE_DROPDOWN[0]) {
212 activeFontSize = defaultStyle.fontSize ? defaultStyle.fontSize.replace('px', '') : config.FONT_SIZE_DROPDOWN[0].label;
213 }
214 if (config.LINE_HEIGHT_DROPDOWN && config.LINE_HEIGHT_DROPDOWN[0]) {
215 activeLineHeight = defaultStyle.lineHeight ? defaultStyle.lineHeight : config.LINE_HEIGHT_DROPDOWN[0].label;
216 }
217 return {
218 activeInlineStyle: activeInlineStyle,
219 activeColor: activeColor,
220 activeBackgroundColor: activeBackgroundColor,
221 activeFontSize: activeFontSize,
222 activeLineHeight: activeLineHeight,
223 activeFontFamily: activeFontFamily,
224 activeAlign: activeAlign
225 };
226};
227
228var stateToStyle = exports.stateToStyle = function stateToStyle(_ref) {
229 var activeInlineStyle = _ref.activeInlineStyle,
230 activeColor = _ref.activeColor,
231 activeBackgroundColor = _ref.activeBackgroundColor,
232 activeFontSize = _ref.activeFontSize,
233 activeLineHeight = _ref.activeLineHeight,
234 activeAlign = _ref.activeAlign,
235 activeFontFamily = _ref.activeFontFamily,
236 config = _ref.config;
237 var inlineStyleObj = config.inlineStyleObj,
238 fontSizeObj = config.fontSizeObj,
239 lineHeightObj = config.lineHeightObj,
240 alignObj = config.alignObj,
241 fontFamilyObj = config.fontFamilyObj;
242
243 var style = _extends({
244 color: activeColor,
245 backgroundColor: activeBackgroundColor
246 }, fontSizeObj[activeFontSize].style, lineHeightObj[activeLineHeight].style, alignObj[activeAlign].style, fontFamilyObj[activeFontFamily].style);
247
248 // inline style
249 Object.keys(activeInlineStyle).map(function (type) {
250 if (activeInlineStyle[type]) {
251 style = _extends({}, style, inlineStyleObj[type].style);
252 }
253 });
254 return style;
255};
256
257exports.default = toolbarConfig;
\No newline at end of file