Skip to content

Commit 958b537

Browse files
committed
Lexical: Linked table form to have caption toggle option
1 parent 8a66365 commit 958b537

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

resources/js/wysiwyg/lexical/table/LexicalCaptionNode.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
$createTextNode,
23
DOMConversionMap,
34
DOMExportOutput,
45
EditorConfig,
@@ -7,6 +8,7 @@ import {
78
LexicalNode,
89
SerializedElementNode
910
} from "lexical";
11+
import {TableNode} from "@lexical/table/LexicalTableNode";
1012

1113

1214
export class CaptionNode extends ElementNode {
@@ -71,4 +73,20 @@ export function $createCaptionNode(): CaptionNode {
7173

7274
export function $isCaptionNode(node: LexicalNode | null | undefined): node is CaptionNode {
7375
return node instanceof CaptionNode;
76+
}
77+
78+
export function $tableHasCaption(table: TableNode): boolean {
79+
for (const child of table.getChildren()) {
80+
if ($isCaptionNode(child)) {
81+
return true;
82+
}
83+
}
84+
return false;
85+
}
86+
87+
export function $addCaptionToTable(table: TableNode, text: string = ''): void {
88+
const caption = $createCaptionNode();
89+
const textNode = $createTextNode(text || ' ');
90+
caption.append(textNode);
91+
table.append(caption);
7492
}

resources/js/wysiwyg/todo.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
## Secondary Todo
1212

13-
- Table caption text support
1413
- Support media src conversions (https://github.com/tinymce/tinymce/blob/release/6.6/modules/tinymce/src/plugins/media/main/ts/core/UrlPatterns.ts)
1514
- Deep check of translation coverage
1615

resources/js/wysiwyg/ui/defaults/forms/tables.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {formatSizeValue} from "../../../utils/dom";
1818
import {TableCellNode, TableNode, TableRowNode} from "@lexical/table";
1919
import {CommonBlockAlignment} from "lexical/nodes/common";
2020
import {colorFieldBuilder} from "../../framework/blocks/color-field";
21+
import {$addCaptionToTable, $isCaptionNode, $tableHasCaption} from "@lexical/table/LexicalCaptionNode";
2122

2223
const borderStyleInput: EditorSelectFormFieldDefinition = {
2324
label: 'Border style',
@@ -219,6 +220,7 @@ export const rowProperties: EditorFormDefinition = {
219220
export function $showTablePropertiesForm(table: TableNode, context: EditorUiContext): EditorFormModal {
220221
const styles = table.getStyles();
221222
const modalForm = context.manager.createModal('table_properties');
223+
222224
modalForm.show({
223225
width: styles.get('width') || '',
224226
height: styles.get('height') || '',
@@ -228,7 +230,7 @@ export function $showTablePropertiesForm(table: TableNode, context: EditorUiCont
228230
border_style: styles.get('border-style') || '',
229231
border_color: styles.get('border-color') || '',
230232
background_color: styles.get('background-color') || '',
231-
// caption: '', TODO
233+
caption: $tableHasCaption(table) ? 'true' : '',
232234
align: table.getAlignment(),
233235
});
234236
return modalForm;
@@ -265,7 +267,17 @@ export const tableProperties: EditorFormDefinition = {
265267
});
266268
}
267269

268-
// TODO - cell caption
270+
const showCaption = Boolean(formData.get('caption')?.toString() || '');
271+
const hasCaption = $tableHasCaption(table);
272+
if (showCaption && !hasCaption) {
273+
$addCaptionToTable(table, context.translate('Caption'));
274+
} else if (!showCaption && hasCaption) {
275+
for (const child of table.getChildren()) {
276+
if ($isCaptionNode(child)) {
277+
child.remove();
278+
}
279+
}
280+
}
269281
});
270282
return true;
271283
},
@@ -299,9 +311,9 @@ export const tableProperties: EditorFormDefinition = {
299311
type: 'text',
300312
},
301313
{
302-
label: 'caption', // Caption element
314+
label: 'Show caption', // Caption element
303315
name: 'caption',
304-
type: 'text', // TODO -
316+
type: 'checkbox',
305317
},
306318
alignmentInput, // alignment class
307319
];

resources/js/wysiwyg/ui/framework/forms.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {el} from "../../utils/dom";
1111
export interface EditorFormFieldDefinition {
1212
label: string;
1313
name: string;
14-
type: 'text' | 'select' | 'textarea';
14+
type: 'text' | 'select' | 'textarea' | 'checkbox';
1515
}
1616

1717
export interface EditorSelectFormFieldDefinition extends EditorFormFieldDefinition {
@@ -42,7 +42,11 @@ export class EditorFormField extends EditorUiElement {
4242

4343
setValue(value: string) {
4444
const input = this.getDOMElement().querySelector('input,select,textarea') as HTMLInputElement;
45-
input.value = value;
45+
if (this.definition.type === 'checkbox') {
46+
input.checked = Boolean(value);
47+
} else {
48+
input.value = value;
49+
}
4650
input.dispatchEvent(new Event('change'));
4751
}
4852

@@ -61,6 +65,8 @@ export class EditorFormField extends EditorUiElement {
6165
input = el('select', {id, name: this.definition.name, class: 'editor-form-field-input'}, optionElems);
6266
} else if (this.definition.type === 'textarea') {
6367
input = el('textarea', {id, name: this.definition.name, class: 'editor-form-field-input'});
68+
} else if (this.definition.type === 'checkbox') {
69+
input = el('input', {id, name: this.definition.name, type: 'checkbox', class: 'editor-form-field-input-checkbox', value: 'true'});
6470
} else {
6571
input = el('input', {id, name: this.definition.name, class: 'editor-form-field-input'});
6672
}

0 commit comments

Comments
 (0)