1 | import { __read } from "tslib";
|
2 | import { 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';
|
3 | export var tablePrefixSelector = 'pm-table';
|
4 | export var tableCellSelector = tablePrefixSelector + "-cell-content-wrap";
|
5 | export var tableHeaderSelector = tablePrefixSelector + "-header-content-wrap";
|
6 | export var tableCellContentWrapperSelector = tablePrefixSelector + "-cell-nodeview-wrapper";
|
7 | export var tableCellContentDomSelector = tablePrefixSelector + "-cell-nodeview-content-dom";
|
8 | var DEFAULT_TABLE_HEADER_CELL_BACKGROUND = N20.toLocaleLowerCase();
|
9 | var 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 | };
|
29 | export 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 |
|
46 |
|
47 |
|
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 | };
|
68 | export var tableBackgroundColorPalette = new Map();
|
69 | export var tableBackgroundBorderColor = hexToRgba(N800, 0.12) || N0;
|
70 | export 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 |
|
99 | export 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 | };
|
129 | export 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 | }); };
|
137 | export var tableRow = {
|
138 | content: '(tableCell | tableHeader)+',
|
139 | tableRole: 'row',
|
140 | parseDOM: [{ tag: 'tr' }],
|
141 | toDOM: function () {
|
142 | return ['tr', 0];
|
143 | },
|
144 | };
|
145 | var cellAttrs = {
|
146 | colspan: { default: 1 },
|
147 | rowspan: { default: 1 },
|
148 | colwidth: { default: null },
|
149 | background: { default: null },
|
150 | };
|
151 | export 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 |
|
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 | };
|
170 | export 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 | }); };
|
178 | export 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 | };
|
194 | export var toJSONTableHeader = toJSONTableCell;
|
195 |
|
\ | No newline at end of file |