UNPKG

7.2 kBJavaScriptView Raw
1import { __read } from "tslib";
2import { isRgb, rgbToHex, N0, N20, N60, B50, B75, B100, T50, T75, T100, P50, P75, P100, R50, R75, R100, G50, G75, G200, Y50, Y75, Y200, hexToRgba, N800, } from '../../utils/colors';
3export var tablePrefixSelector = 'pm-table';
4export var tableCellSelector = tablePrefixSelector + "-cell-content-wrap";
5export var tableHeaderSelector = tablePrefixSelector + "-header-content-wrap";
6export var tableCellContentWrapperSelector = tablePrefixSelector + "-cell-nodeview-wrapper";
7export var tableCellContentDomSelector = tablePrefixSelector + "-cell-nodeview-content-dom";
8var DEFAULT_TABLE_HEADER_CELL_BACKGROUND = N20.toLocaleLowerCase();
9var getCellAttrs = function (dom, defaultValues) {
10 if (defaultValues === void 0) { defaultValues = {}; }
11 var widthAttr = dom.getAttribute('data-colwidth');
12 var width = widthAttr && /^\d+(,\d+)*$/.test(widthAttr)
13 ? widthAttr.split(',').map(function (str) { return Number(str); })
14 : null;
15 var colspan = Number(dom.getAttribute('colspan') || 1);
16 var backgroundColor = dom.style.backgroundColor;
17 if (backgroundColor && isRgb(backgroundColor)) {
18 backgroundColor = rgbToHex(backgroundColor);
19 }
20 return {
21 colspan: colspan,
22 rowspan: Number(dom.getAttribute('rowspan') || 1),
23 colwidth: width && width.length === colspan ? width : null,
24 background: backgroundColor && backgroundColor !== defaultValues['background']
25 ? backgroundColor
26 : null,
27 };
28};
29export var setCellAttrs = function (node, cell) {
30 var attrs = {};
31 var nodeType = node.type.name;
32 var colspan = cell ? parseInt(cell.getAttribute('colspan') || '1', 10) : 1;
33 var rowspan = cell ? parseInt(cell.getAttribute('rowspan') || '1', 10) : 1;
34 if (node.attrs.colspan !== colspan) {
35 attrs.colspan = node.attrs.colspan;
36 }
37 if (node.attrs.rowspan !== rowspan) {
38 attrs.rowspan = node.attrs.rowspan;
39 }
40 if (node.attrs.colwidth) {
41 attrs['data-colwidth'] = node.attrs.colwidth.join(',');
42 }
43 if (node.attrs.background) {
44 var background = node.attrs.background;
45 // to ensure that we don't overwrite product's style:
46 // - it clears background color for <th> if its set to gray
47 // - it clears background color for <td> if its set to white
48 var ignored = (nodeType === 'tableHeader' &&
49 background === tableBackgroundColorNames.get('light gray')) ||
50 (nodeType === 'tableCell' &&
51 background === tableBackgroundColorNames.get('white'));
52 if (ignored) {
53 attrs.style = '';
54 }
55 else {
56 var color = isRgb(background) ? rgbToHex(background) : background;
57 attrs.style = (attrs.style || '') + "background-color: " + color + ";";
58 }
59 }
60 if (nodeType === 'tableHeader') {
61 attrs.class = tableHeaderSelector;
62 }
63 else {
64 attrs.class = tableCellSelector;
65 }
66 return attrs;
67};
68export var tableBackgroundColorPalette = new Map();
69export var tableBackgroundBorderColor = hexToRgba(N800, 0.12) || N0;
70export var tableBackgroundColorNames = new Map();
71[
72 [N0, 'White'],
73 [B50, 'Light blue'],
74 [T50, 'Light teal'],
75 [G50, 'Light green'],
76 [Y50, 'Light yellow'],
77 [R50, 'Light red'],
78 [P50, 'Light purple'],
79 [N20, 'Light gray'],
80 [B75, 'Blue'],
81 [T75, 'Teal'],
82 [G75, 'Green'],
83 [Y75, 'Yellow'],
84 [R75, 'Red'],
85 [P75, 'Purple'],
86 [N60, 'Gray'],
87 [B100, 'Dark blue'],
88 [T100, 'Dark teal'],
89 [G200, 'Dark green'],
90 [Y200, 'Dark yellow'],
91 [R100, 'Dark red'],
92 [P100, 'Dark purple'],
93].forEach(function (_a) {
94 var _b = __read(_a, 2), colorValue = _b[0], colorName = _b[1];
95 tableBackgroundColorPalette.set(colorValue.toLowerCase(), colorName);
96 tableBackgroundColorNames.set(colorName.toLowerCase(), colorValue.toLowerCase());
97});
98// TODO: Fix any, potential issue. ED-5048
99export var table = {
100 content: 'tableRow+',
101 attrs: {
102 isNumberColumnEnabled: { default: false },
103 layout: { default: 'default' },
104 __autoSize: { default: false },
105 },
106 tableRole: 'table',
107 isolating: true,
108 selectable: false,
109 group: 'block',
110 parseDOM: [
111 {
112 tag: 'table',
113 getAttrs: function (dom) { return ({
114 isNumberColumnEnabled: dom.getAttribute('data-number-column') === 'true' ? true : false,
115 layout: dom.getAttribute('data-layout') || 'default',
116 __autoSize: dom.getAttribute('data-autosize') === 'true' ? true : false,
117 }); },
118 },
119 ],
120 toDOM: function (node) {
121 var attrs = {
122 'data-number-column': node.attrs.isNumberColumnEnabled,
123 'data-layout': node.attrs.layout,
124 'data-autosize': node.attrs.__autoSize,
125 };
126 return ['table', attrs, ['tbody', 0]];
127 },
128};
129export var tableToJSON = function (node) { return ({
130 attrs: Object.keys(node.attrs)
131 .filter(function (key) { return !key.startsWith('__'); })
132 .reduce(function (obj, key) {
133 obj[key] = node.attrs[key];
134 return obj;
135 }, {}),
136}); };
137export var tableRow = {
138 content: '(tableCell | tableHeader)+',
139 tableRole: 'row',
140 parseDOM: [{ tag: 'tr' }],
141 toDOM: function () {
142 return ['tr', 0];
143 },
144};
145var cellAttrs = {
146 colspan: { default: 1 },
147 rowspan: { default: 1 },
148 colwidth: { default: null },
149 background: { default: null },
150};
151export var tableCell = {
152 content: '(paragraph | panel | blockquote | orderedList | bulletList | rule | heading | codeBlock | mediaSingle | mediaGroup | decisionList | taskList | blockCard | extension | nestedExpand | unsupportedBlock)+',
153 attrs: cellAttrs,
154 tableRole: 'cell',
155 marks: 'alignment',
156 isolating: true,
157 parseDOM: [
158 // Ignore number cell copied from renderer
159 {
160 tag: '.ak-renderer-table-number-column',
161 ignore: true,
162 },
163 {
164 tag: 'td',
165 getAttrs: function (dom) { return getCellAttrs(dom); },
166 },
167 ],
168 toDOM: function (node) { return ['td', setCellAttrs(node), 0]; },
169};
170export var toJSONTableCell = function (node) { return ({
171 attrs: Object.keys(node.attrs).reduce(function (obj, key) {
172 if (cellAttrs[key].default !== node.attrs[key]) {
173 obj[key] = node.attrs[key];
174 }
175 return obj;
176 }, {}),
177}); };
178export var tableHeader = {
179 content: '(paragraph | panel | blockquote | orderedList | bulletList | rule | heading | codeBlock | mediaSingle | mediaGroup | decisionList | taskList | blockCard | extension | nestedExpand)+',
180 attrs: cellAttrs,
181 tableRole: 'header_cell',
182 isolating: true,
183 marks: 'alignment',
184 parseDOM: [
185 {
186 tag: 'th',
187 getAttrs: function (dom) {
188 return getCellAttrs(dom, { background: DEFAULT_TABLE_HEADER_CELL_BACKGROUND });
189 },
190 },
191 ],
192 toDOM: function (node) { return ['th', setCellAttrs(node), 0]; },
193};
194export var toJSONTableHeader = toJSONTableCell;
195//# sourceMappingURL=tableNodes.js.map
\No newline at end of file