3 EditorFormFieldDefinition, EditorFormFields,
5 EditorSelectFormFieldDefinition
6 } from "../../framework/forms";
7 import {EditorUiContext} from "../../framework/core";
8 import {EditorFormModal} from "../../framework/modals";
9 import {$getSelection} from "lexical";
11 $forEachTableCell, $getCellPaddingForTable,
12 $getTableCellColumnWidth,
13 $getTableCellsFromSelection, $getTableFromSelection,
14 $getTableRowsFromSelection,
15 $setTableCellColumnWidth
16 } from "../../../utils/tables";
17 import {formatSizeValue} from "../../../utils/dom";
18 import {TableCellNode, TableNode, TableRowNode} from "@lexical/table";
19 import {CommonBlockAlignment} from "lexical/nodes/common";
20 import {colorFieldBuilder} from "../../framework/blocks/color-field";
22 const borderStyleInput: EditorSelectFormFieldDefinition = {
23 label: 'Border style',
41 const borderColorInput: EditorFormFieldDefinition = {
42 label: 'Border color',
47 const backgroundColorInput: EditorFormFieldDefinition = {
48 label: 'Background color',
49 name: 'background_color',
53 const alignmentInput: EditorSelectFormFieldDefinition = {
65 export function $showCellPropertiesForm(cell: TableCellNode, context: EditorUiContext): EditorFormModal {
66 const styles = cell.getStyles();
67 const modalForm = context.manager.createModal('cell_properties');
69 width: $getTableCellColumnWidth(context.editor, cell),
70 height: styles.get('height') || '',
72 h_align: cell.getAlignment(),
73 v_align: styles.get('vertical-align') || '',
74 border_width: styles.get('border-width') || '',
75 border_style: styles.get('border-style') || '',
76 border_color: styles.get('border-color') || '',
77 background_color: styles.get('background-color') || '',
82 export const cellProperties: EditorFormDefinition = {
84 async action(formData, context: EditorUiContext) {
85 context.editor.update(() => {
86 const cells = $getTableCellsFromSelection($getSelection());
87 for (const cell of cells) {
88 const width = formData.get('width')?.toString() || '';
90 $setTableCellColumnWidth(cell, width);
91 cell.updateTag(formData.get('type')?.toString() || '');
92 cell.setAlignment((formData.get('h_align')?.toString() || '') as CommonBlockAlignment);
94 const styles = cell.getStyles();
95 styles.set('height', formatSizeValue(formData.get('height')?.toString() || ''));
96 styles.set('vertical-align', formData.get('v_align')?.toString() || '');
97 styles.set('border-width', formatSizeValue(formData.get('border_width')?.toString() || ''));
98 styles.set('border-style', formData.get('border_style')?.toString() || '');
99 styles.set('border-color', formData.get('border_color')?.toString() || '');
100 styles.set('background-color', formData.get('background_color')?.toString() || '');
102 cell.setStyles(styles);
111 const generalFields: EditorFormFieldDefinition[] = [
113 label: 'Width', // Colgroup width
118 label: 'Height', // inline-style: height
123 label: 'Cell type', // element
130 } as EditorSelectFormFieldDefinition,
132 ...alignmentInput, // class: 'align-right/left/center'
133 label: 'Horizontal align',
137 label: 'Vertical align', // inline-style: vertical-align
146 } as EditorSelectFormFieldDefinition,
149 const advancedFields: EditorFormFields = [
151 label: 'Border width', // inline-style: border-width
152 name: 'border_width',
155 borderStyleInput, // inline-style: border-style
156 colorFieldBuilder(borderColorInput),
157 colorFieldBuilder(backgroundColorInput),
160 return new EditorFormTabs([
163 contents: generalFields,
167 contents: advancedFields,
175 export function $showRowPropertiesForm(row: TableRowNode, context: EditorUiContext): EditorFormModal {
176 const styles = row.getStyles();
177 const modalForm = context.manager.createModal('row_properties');
179 height: styles.get('height') || '',
180 border_style: styles.get('border-style') || '',
181 border_color: styles.get('border-color') || '',
182 background_color: styles.get('background-color') || '',
187 export const rowProperties: EditorFormDefinition = {
189 async action(formData, context: EditorUiContext) {
190 context.editor.update(() => {
191 const rows = $getTableRowsFromSelection($getSelection());
192 for (const row of rows) {
193 const styles = row.getStyles();
194 styles.set('height', formatSizeValue(formData.get('height')?.toString() || ''));
195 styles.set('border-style', formData.get('border_style')?.toString() || '');
196 styles.set('border-color', formData.get('border_color')?.toString() || '');
197 styles.set('background-color', formData.get('background_color')?.toString() || '');
198 row.setStyles(styles);
205 // Removed 'Row Type' as we don't currently support thead/tfoot elements
206 // TinyMCE would move rows up/down into these parents when set
207 // Removed 'Alignment' since this was broken in our editor (applied alignment class to whole parent table)
209 label: 'Height', // style on tr: height
213 borderStyleInput, // style on tr: height
214 colorFieldBuilder(borderColorInput),
215 colorFieldBuilder(backgroundColorInput),
219 export function $showTablePropertiesForm(table: TableNode, context: EditorUiContext): EditorFormModal {
220 const styles = table.getStyles();
221 const modalForm = context.manager.createModal('table_properties');
223 width: styles.get('width') || '',
224 height: styles.get('height') || '',
225 cell_spacing: styles.get('cell-spacing') || '',
226 cell_padding: $getCellPaddingForTable(table),
227 border_width: styles.get('border-width') || '',
228 border_style: styles.get('border-style') || '',
229 border_color: styles.get('border-color') || '',
230 background_color: styles.get('background-color') || '',
232 align: table.getAlignment(),
237 export const tableProperties: EditorFormDefinition = {
239 async action(formData, context: EditorUiContext) {
240 context.editor.update(() => {
241 const table = $getTableFromSelection($getSelection());
246 const styles = table.getStyles();
247 styles.set('width', formatSizeValue(formData.get('width')?.toString() || ''));
248 styles.set('height', formatSizeValue(formData.get('height')?.toString() || ''));
249 styles.set('cell-spacing', formatSizeValue(formData.get('cell_spacing')?.toString() || ''));
250 styles.set('border-width', formatSizeValue(formData.get('border_width')?.toString() || ''));
251 styles.set('border-style', formData.get('border_style')?.toString() || '');
252 styles.set('border-color', formData.get('border_color')?.toString() || '');
253 styles.set('background-color', formData.get('background_color')?.toString() || '');
254 table.setStyles(styles);
256 table.setAlignment(formData.get('align') as CommonBlockAlignment);
258 const cellPadding = (formData.get('cell_padding')?.toString() || '');
260 const cellPaddingFormatted = formatSizeValue(cellPadding);
261 $forEachTableCell(table, (cell: TableCellNode) => {
262 const styles = cell.getStyles();
263 styles.set('padding', cellPaddingFormatted);
264 cell.setStyles(styles);
268 // TODO - cell caption
275 const generalFields: EditorFormFieldDefinition[] = [
277 label: 'Width', // Style - width
282 label: 'Height', // Style - height
287 label: 'Cell spacing', // Style - border-spacing
288 name: 'cell_spacing',
292 label: 'Cell padding', // Style - padding on child cells?
293 name: 'cell_padding',
297 label: 'Border width', // Style - border-width
298 name: 'border_width',
302 label: 'caption', // Caption element
304 type: 'text', // TODO -
306 alignmentInput, // alignment class
309 const advancedFields: EditorFormFields = [
311 colorFieldBuilder(borderColorInput),
312 colorFieldBuilder(backgroundColorInput),
315 return new EditorFormTabs([
318 contents: generalFields,
322 contents: advancedFields,