/
Helpers.ts
321 lines (275 loc) · 9.41 KB
/
Helpers.ts
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
*/
import { HTMLElement } from '@ephox/dom-globals';
import { Arr, Fun, Obj, Strings } from '@ephox/katamari';
import { Css, Element } from '@ephox/sugar';
import Editor from 'tinymce/core/api/Editor';
import Tools from 'tinymce/core/api/util/Tools';
import Styles from '../actions/Styles';
import { shouldStyleWithCss, getDefaultStyles, getDefaultAttributes } from '../api/Settings';
import { Types } from '@ephox/bridge';
/**
* @class tinymce.table.ui.Helpers
* @private
*/
const buildListItems = (inputList, itemCallback, startItems?): Types.SelectBox.ExternalSelectBoxItem[] => {
const appendItems = (values, output?: Types.SelectBox.ExternalSelectBoxItem[]) => {
output = output || [];
Tools.each(values, (item) => {
const menuItem: any = { text: item.text || item.title };
// TODO TINY-2236 - this implies table_class_list supports nested menus, but bridge doesn't, so this either needs to be supported or deleted
if (item.menu) {
menuItem.menu = appendItems(item.menu);
} else {
menuItem.value = item.value;
if (itemCallback) {
itemCallback(menuItem);
}
}
output.push(menuItem);
});
return output;
};
return appendItems(inputList, startItems || []);
};
const extractAdvancedStyles = (dom, elm) => {
const rgbToHex = (value: string) => Strings.startsWith(value, 'rgb') ? dom.toHex(value) : value;
const borderStyle = Css.getRaw(Element.fromDom(elm), 'border-style').getOr('');
const borderColor = Css.getRaw(Element.fromDom(elm), 'border-color').map(rgbToHex).getOr('');
const bgColor = Css.getRaw(Element.fromDom(elm), 'background-color').map(rgbToHex).getOr('');
return {
borderstyle: borderStyle,
bordercolor: borderColor,
backgroundcolor: bgColor
};
};
const getSharedValues = (data) => {
// Mutates baseData to return an object that contains only the values
// that were the same across all objects in data
const baseData = data[0];
const comparisonData = data.slice(1);
const keys = Obj.keys(baseData);
Arr.each(comparisonData, (items) => {
Arr.each(keys, (key) => {
Obj.each(items, (itemValue, itemKey) => {
const comparisonValue = baseData[key];
if (comparisonValue !== '' && key === itemKey) {
if (comparisonValue !== itemValue) {
baseData[key] = '';
}
}
});
});
});
return baseData;
};
const getAdvancedTab = () => {
const items: Types.Dialog.BodyComponentApi[] = [
{
name: 'borderstyle',
type: 'selectbox',
label: 'Border style',
items: [
{ text: 'Select...', value: '' },
{ text: 'Solid', value: 'solid' },
{ text: 'Dotted', value: 'dotted' },
{ text: 'Dashed', value: 'dashed' },
{ text: 'Double', value: 'double' },
{ text: 'Groove', value: 'groove' },
{ text: 'Ridge', value: 'ridge' },
{ text: 'Inset', value: 'inset' },
{ text: 'Outset', value: 'outset' },
{ text: 'None', value: 'none' },
{ text: 'Hidden', value: 'hidden' }
]
},
{
name: 'bordercolor',
type: 'colorinput',
label: 'Border color'
},
{
name: 'backgroundcolor',
type: 'colorinput',
label: 'Background color'
}
];
return {
title: 'Advanced',
name: 'advanced',
items
};
};
// The extractDataFrom... functions are in this file partly for code reuse and partly so we can test them,
// because some of these are crazy complicated
const getAlignment = (alignments, formatName, dataName, editor, elm) => {
const alignmentData = {};
Tools.each(alignments.split(' '), (name) => {
if (editor.formatter.matchNode(elm, formatName + name)) {
alignmentData[dataName] = name;
}
});
if (!alignmentData[dataName]) {
// TODO: Note, this isn't a real value. But maybe that is OK?
alignmentData[dataName] = '';
}
return alignmentData;
};
const getHAlignment = Fun.curry(getAlignment, 'left center right');
const getVAlignment = Fun.curry(getAlignment, 'top middle bottom');
export interface TableData {
height: string;
width: string;
cellspacing: string;
cellpadding: string;
caption: boolean;
class?: string;
align: string;
border: string;
cols?: string;
rows?: string;
borderstyle?: string;
bordercolor?: string;
backgroundcolor?: string;
}
const extractDataFromSettings = (editor, hasAdvTableTab): TableData => {
const style = getDefaultStyles(editor);
const attrs = getDefaultAttributes(editor);
const extractAdvancedStyleData = (dom) => {
const rgbToHex = (value: string) => Strings.startsWith(value, 'rgb') ? dom.toHex(value) : value;
const borderStyle = Obj.get(style, 'border-style').getOr('');
const borderColor = Obj.get(style, 'border-color').getOr('');
const bgColor = Obj.get(style, 'background-color').getOr('');
return {
borderstyle: borderStyle,
bordercolor: rgbToHex(borderColor),
backgroundcolor: rgbToHex(bgColor)
};
};
const defaultData: TableData = {
height: '',
width: '100%',
cellspacing: '',
cellpadding: '',
caption: false,
class: '',
align: '',
border: ''
};
const getBorder = () => {
const borderWidth = style['border-width'];
if (shouldStyleWithCss(editor) && borderWidth) {
return { border: borderWidth };
}
return Obj.get(attrs, 'border').fold( () => ({}), (border) => ({ border }));
};
const dom = editor.dom;
const advStyle = (hasAdvTableTab ? extractAdvancedStyleData(dom) : {});
const getCellPaddingCellSpacing = () => {
const spacing = Obj.get(style, 'border-spacing').or(Obj.get(attrs, 'cellspacing')).fold( () => ({}), (cellspacing) => ({ cellspacing }));
const padding = Obj.get(style, 'border-padding').or(Obj.get(attrs, 'cellpadding')).fold( () => ({}), (cellpadding) => ({ cellpadding }));
return {
...spacing,
...padding
};
};
const data = {
...defaultData,
...style,
...attrs,
...advStyle,
...getBorder(),
...getCellPaddingCellSpacing()
};
return data;
};
const extractDataFromTableElement = (editor: Editor, elm, hasAdvTableTab: boolean): TableData => {
const getBorder = (dom, elm) => {
// Cases (in order to check):
// 1. shouldStyleWithCss - extract border-width style if it exists
// 2. !shouldStyleWithCss && border attribute - set border attribute as value
// 3. !shouldStyleWithCss && nothing on the table - grab styles from the first th or td
const optBorderWidth = Css.getRaw(Element.fromDom(elm), 'border-width');
if (shouldStyleWithCss(editor) && optBorderWidth.isSome()) {
return optBorderWidth.getOr('');
}
return dom.getAttrib(elm, 'border') || Styles.getTDTHOverallStyle(editor.dom, elm, 'border-width')
|| Styles.getTDTHOverallStyle(editor.dom, elm, 'border');
};
const dom = editor.dom;
const data: any = {
width: dom.getStyle(elm, 'width') || dom.getAttrib(elm, 'width'),
height: dom.getStyle(elm, 'height') || dom.getAttrib(elm, 'height'),
cellspacing: dom.getStyle(elm, 'border-spacing') || dom.getAttrib(elm, 'cellspacing'),
cellpadding: dom.getAttrib(elm, 'cellpadding') || Styles.getTDTHOverallStyle(editor.dom, elm, 'padding'),
border: getBorder(dom, elm),
caption: !!dom.select('caption', elm)[0],
class: dom.getAttrib(elm, 'class', ''),
...getHAlignment('align', 'align', editor, elm),
...(hasAdvTableTab ? extractAdvancedStyles(dom, elm) : {})
};
return data;
};
export interface RowData {
height: string;
scope: string;
class: string;
align: string;
type: string;
borderstyle?: string;
bordercolor?: string;
backgroundcolor?: string;
}
const extractDataFromRowElement = (editor: Editor, elm: HTMLElement, hasAdvancedRowTab: boolean): RowData => {
const dom = editor.dom;
const data: RowData = {
height: dom.getStyle(elm, 'height') || dom.getAttrib(elm, 'height'),
scope: dom.getAttrib(elm, 'scope'),
class: dom.getAttrib(elm, 'class', ''),
align: '',
type: elm.parentNode.nodeName.toLowerCase(),
...getHAlignment('align', 'align', editor, elm),
...(hasAdvancedRowTab ? extractAdvancedStyles(dom, elm) : {})
};
return data;
};
export interface CellData {
width: string;
height: string;
scope: string;
celltype: string;
class: string;
halign: string;
valign: string;
borderstyle?: string;
bordercolor?: string;
backgroundcolor?: string;
}
const extractDataFromCellElement = (editor: Editor, elm: HTMLElement, hasAdvancedCellTab: boolean): CellData => {
const dom = editor.dom;
const data: any = {
width: dom.getStyle(elm, 'width') || dom.getAttrib(elm, 'width'),
height: dom.getStyle(elm, 'height') || dom.getAttrib(elm, 'height'),
scope: dom.getAttrib(elm, 'scope'),
celltype: elm.nodeName.toLowerCase(),
class: dom.getAttrib(elm, 'class', ''),
...getHAlignment('align', 'halign', editor, elm),
...getVAlignment('valign', 'valign', editor, elm),
...(hasAdvancedCellTab ? extractAdvancedStyles(dom, elm) : {})
};
return data;
};
export default {
buildListItems,
extractAdvancedStyles,
getSharedValues,
getAdvancedTab,
extractDataFromTableElement,
extractDataFromRowElement,
extractDataFromCellElement,
extractDataFromSettings
};