3 EditorFormFieldDefinition,
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";
21 const borderStyleInput: EditorSelectFormFieldDefinition = {
22 label: 'Border style',
40 const borderColorInput: EditorFormFieldDefinition = {
41 label: 'Border color',
46 const backgroundColorInput: EditorFormFieldDefinition = {
47 label: 'Background color',
48 name: 'background_color',
52 const alignmentInput: EditorSelectFormFieldDefinition = {
64 export function $showCellPropertiesForm(cell: TableCellNode, context: EditorUiContext): EditorFormModal {
65 const styles = cell.getStyles();
66 const modalForm = context.manager.createModal('cell_properties');
68 width: $getTableCellColumnWidth(context.editor, cell),
69 height: styles.get('height') || '',
71 h_align: cell.getAlignment(),
72 v_align: styles.get('vertical-align') || '',
73 border_width: styles.get('border-width') || '',
74 border_style: styles.get('border-style') || '',
75 border_color: styles.get('border-color') || '',
76 background_color: styles.get('background-color') || '',
81 export const cellProperties: EditorFormDefinition = {
83 async action(formData, context: EditorUiContext) {
84 context.editor.update(() => {
85 const cells = $getTableCellsFromSelection($getSelection());
86 for (const cell of cells) {
87 const width = formData.get('width')?.toString() || '';
89 $setTableCellColumnWidth(cell, width);
90 cell.updateTag(formData.get('type')?.toString() || '');
91 cell.setAlignment((formData.get('h_align')?.toString() || '') as CommonBlockAlignment);
93 const styles = cell.getStyles();
94 styles.set('height', formatSizeValue(formData.get('height')?.toString() || ''));
95 styles.set('vertical-align', formData.get('v_align')?.toString() || '');
96 styles.set('border-width', formatSizeValue(formData.get('border_width')?.toString() || ''));
97 styles.set('border-style', formData.get('border_style')?.toString() || '');
98 styles.set('border-color', formData.get('border_color')?.toString() || '');
99 styles.set('background-color', formData.get('background_color')?.toString() || '');
101 cell.setStyles(styles);
110 const generalFields: EditorFormFieldDefinition[] = [
112 label: 'Width', // Colgroup width
117 label: 'Height', // inline-style: height
122 label: 'Cell type', // element
129 } as EditorSelectFormFieldDefinition,
131 ...alignmentInput, // class: 'align-right/left/center'
132 label: 'Horizontal align',
136 label: 'Vertical align', // inline-style: vertical-align
145 } as EditorSelectFormFieldDefinition,
148 const advancedFields: EditorFormFieldDefinition[] = [
150 label: 'Border width', // inline-style: border-width
151 name: 'border_width',
154 borderStyleInput, // inline-style: border-style
155 borderColorInput, // inline-style: border-color
156 backgroundColorInput, // inline-style: background-color
159 return new EditorFormTabs([
162 contents: generalFields,
166 contents: advancedFields,
174 export function $showRowPropertiesForm(row: TableRowNode, context: EditorUiContext): EditorFormModal {
175 const styles = row.getStyles();
176 const modalForm = context.manager.createModal('row_properties');
178 height: styles.get('height') || '',
179 border_style: styles.get('border-style') || '',
180 border_color: styles.get('border-color') || '',
181 background_color: styles.get('background-color') || '',
186 export const rowProperties: EditorFormDefinition = {
188 async action(formData, context: EditorUiContext) {
189 context.editor.update(() => {
190 const rows = $getTableRowsFromSelection($getSelection());
191 for (const row of rows) {
192 const styles = row.getStyles();
193 styles.set('height', formatSizeValue(formData.get('height')?.toString() || ''));
194 styles.set('border-style', formData.get('border_style')?.toString() || '');
195 styles.set('border-color', formData.get('border_color')?.toString() || '');
196 styles.set('background-color', formData.get('background_color')?.toString() || '');
197 row.setStyles(styles);
204 // Removed 'Row Type' as we don't currently support thead/tfoot elements
205 // TinyMCE would move rows up/down into these parents when set
206 // Removed 'Alignment' since this was broken in our editor (applied alignment class to whole parent table)
208 label: 'Height', // style on tr: height
212 borderStyleInput, // style on tr: height
213 borderColorInput, // style on tr: height
214 backgroundColorInput, // style on tr: height
218 export function $showTablePropertiesForm(table: TableNode, context: EditorUiContext): EditorFormModal {
219 const styles = table.getStyles();
220 const modalForm = context.manager.createModal('table_properties');
222 width: styles.get('width') || '',
223 height: styles.get('height') || '',
224 cell_spacing: styles.get('cell-spacing') || '',
225 cell_padding: $getCellPaddingForTable(table),
226 border_width: styles.get('border-width') || '',
227 border_style: styles.get('border-style') || '',
228 border_color: styles.get('border-color') || '',
229 background_color: styles.get('background-color') || '',
231 align: table.getAlignment(),
236 export const tableProperties: EditorFormDefinition = {
238 async action(formData, context: EditorUiContext) {
239 context.editor.update(() => {
240 const table = $getTableFromSelection($getSelection());
245 const styles = table.getStyles();
246 styles.set('width', formatSizeValue(formData.get('width')?.toString() || ''));
247 styles.set('height', formatSizeValue(formData.get('height')?.toString() || ''));
248 styles.set('cell-spacing', formatSizeValue(formData.get('cell_spacing')?.toString() || ''));
249 styles.set('border-width', formatSizeValue(formData.get('border_width')?.toString() || ''));
250 styles.set('border-style', formData.get('border_style')?.toString() || '');
251 styles.set('border-color', formData.get('border_color')?.toString() || '');
252 styles.set('background-color', formData.get('background_color')?.toString() || '');
253 table.setStyles(styles);
255 table.setAlignment(formData.get('align') as CommonBlockAlignment);
257 const cellPadding = (formData.get('cell_padding')?.toString() || '');
259 const cellPaddingFormatted = formatSizeValue(cellPadding);
260 $forEachTableCell(table, (cell: TableCellNode) => {
261 const styles = cell.getStyles();
262 styles.set('padding', cellPaddingFormatted);
263 cell.setStyles(styles);
267 // TODO - cell caption
274 const generalFields: EditorFormFieldDefinition[] = [
276 label: 'Width', // Style - width
281 label: 'Height', // Style - height
286 label: 'Cell spacing', // Style - border-spacing
287 name: 'cell_spacing',
291 label: 'Cell padding', // Style - padding on child cells?
292 name: 'cell_padding',
296 label: 'Border width', // Style - border-width
297 name: 'border_width',
301 label: 'caption', // Caption element
303 type: 'text', // TODO -
305 alignmentInput, // alignment class
308 const advancedFields: EditorFormFieldDefinition[] = [
309 borderStyleInput, // Style - border-style
310 borderColorInput, // Style - border-color
311 backgroundColorInput, // Style - background-color
314 return new EditorFormTabs([
317 contents: generalFields,
321 contents: advancedFields,