Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lexical-table] Support table alignment #7044

Merged
merged 9 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,16 @@
margin: 0px 25px 30px 0px;
}
.PlaygroundEditorTheme__tableScrollableWrapper > .PlaygroundEditorTheme__table {
/* Remove the table's margin and put it on the wrapper */
margin: 0;
/* Remove the table's vertical margin and put it on the wrapper */
margin-top: 0;
margin-bottom: 0;
}
.PlaygroundEditorTheme__tableAlignmentCenter {
margin-left: auto;
margin-right: auto;
}
.PlaygroundEditorTheme__tableAlignmentRight {
margin-left: auto;
}
.PlaygroundEditorTheme__table {
border-collapse: collapse;
Expand All @@ -203,7 +211,8 @@
overflow-x: scroll;
table-layout: fixed;
width: fit-content;
margin: 0px 25px 30px 0px;
margin-top: 25px;
margin-bottom: 30px;
}
.PlaygroundEditorTheme__tableRowStriping tr:nth-child(even) {
background-color: #f2f5fb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ const theme: EditorThemeClasses = {
specialText: 'PlaygroundEditorTheme__specialText',
tab: 'PlaygroundEditorTheme__tabNode',
table: 'PlaygroundEditorTheme__table',
tableAlignment: {
center: 'PlaygroundEditorTheme__tableAlignmentCenter',
right: 'PlaygroundEditorTheme__tableAlignmentRight',
},
tableCell: 'PlaygroundEditorTheme__tableCell',
tableCellActionButton: 'PlaygroundEditorTheme__tableCellActionButton',
tableCellActionButtonContainer:
Expand Down
37 changes: 36 additions & 1 deletion packages/lexical-table/src/LexicalTableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
DOMExportOutput,
EditorConfig,
ElementDOMSlot,
type ElementFormatType,
ElementNode,
LexicalEditor,
LexicalNode,
Expand Down Expand Up @@ -77,7 +78,7 @@ function setRowStriping(
dom: HTMLElement,
config: EditorConfig,
rowStriping: boolean,
) {
): void {
if (rowStriping) {
addClassNamesToElement(dom, config.theme.tableRowStriping);
dom.setAttribute('data-lexical-row-striping', 'true');
Expand All @@ -87,6 +88,27 @@ function setRowStriping(
}
}

function alignTableElement(
dom: HTMLElement,
config: EditorConfig,
formatType: ElementFormatType,
): void {
if (!config.theme.tableAlignment) {
return;
}
const removeClasses: string[] = [];
const addClasses: string[] = [];
for (const format of ['center', 'right'] as const) {
const classes = config.theme.tableAlignment[format];
if (!classes) {
continue;
}
(format === formatType ? addClasses : removeClasses).push(classes);
}
removeClassNamesFromElement(dom, ...removeClasses);
addClassNamesToElement(dom, ...addClasses);
}

const scrollableEditors = new WeakSet<LexicalEditor>();

export function $isScrollableTablesActive(
Expand Down Expand Up @@ -211,6 +233,7 @@ export class TableNode extends ElementNode {
setDOMUnmanaged(colGroup);

addClassNamesToElement(tableElement, config.theme.table);
alignTableElement(tableElement, config, this.getFormatType());
if (this.__rowStriping) {
setRowStriping(tableElement, config, true);
}
Expand All @@ -234,6 +257,11 @@ export class TableNode extends ElementNode {
setRowStriping(dom, config, this.__rowStriping);
}
updateColgroup(dom, config, this.getColumnCount(), this.getColWidths());
alignTableElement(
this.getDOMSlot(dom).element,
config,
this.getFormatType(),
);
return false;
}

Expand All @@ -244,6 +272,13 @@ export class TableNode extends ElementNode {
after: (tableElement) => {
if (superExport.after) {
tableElement = superExport.after(tableElement);
if (this.__format) {
alignTableElement(
tableElement as HTMLElement,
editor._config,
this.getFormatType(),
);
}
}
if (isHTMLElement(tableElement) && tableElement.nodeName !== 'TABLE') {
tableElement = tableElement.querySelector('table');
Expand Down
24 changes: 24 additions & 0 deletions packages/lexical-table/src/LexicalTableSelectionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,12 @@ export function applyTableHandlers(
return false;
}

// Align the table if the entire table is selected
if ($isFullTableSelection(selection, tableNode)) {
tableNode.setFormat(formatType);
return true;
}

const [tableMap, anchorCell, focusCell] = $computeTableMap(
tableNode,
anchorNode,
Expand Down Expand Up @@ -1580,6 +1586,24 @@ function $isSelectionInTable(
return false;
}

function $isFullTableSelection(
ivailop7 marked this conversation as resolved.
Show resolved Hide resolved
selection: null | BaseSelection,
tableNode: TableNode,
): boolean {
if ($isTableSelection(selection)) {
const anchorNode = selection.anchor.getNode() as TableCellNode;
const focusNode = selection.focus.getNode() as TableCellNode;
if (tableNode && anchorNode && focusNode) {
const [map] = $computeTableMap(tableNode, anchorNode, focusNode);
return (
anchorNode.getKey() === map[0][0].cell.getKey() &&
focusNode.getKey() === map[map.length - 1].at(-1)!.cell.getKey()
);
}
}
return false;
}

function selectTableCellNode(tableCell: TableCellNode, fromStart: boolean) {
if (fromStart) {
tableCell.selectStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ const editorConfig = Object.freeze({
namespace: '',
theme: {
table: 'test-table-class',
tableAlignment: {
center: 'test-table-alignment-center',
right: 'test-table-alignment-right',
},
tableRowStriping: 'test-table-row-striping-class',
tableScrollableWrapper: 'table-scrollable-wrapper',
},
Expand Down Expand Up @@ -1040,6 +1044,69 @@ describe('LexicalTableNode tests', () => {
});
});

test('Change Table-level alignment', async () => {
const {editor} = testEnv;

await editor.update(() => {
const root = $getRoot();
const table = $createTableNodeWithDimensions(4, 4, true);
root.append(table);
});
await editor.update(() => {
const root = $getRoot();
const table = root.getLastChild<TableNode>();
if (table) {
table.setFormat('center');
}
});

await editor.update(() => {
const root = $getRoot();
const table = root.getLastChild<TableNode>();
expectTableHtmlToBeEqual(
table!.createDOM(editorConfig).outerHTML,
html`
<table
class="${editorConfig.theme.table} ${editorConfig.theme
.tableAlignment.center}">
<colgroup>
<col />
<col />
<col />
<col />
</colgroup>
</table>
`,
);
});

await editor.update(() => {
const root = $getRoot();
const table = root.getLastChild<TableNode>();
if (table) {
table.setFormat('left');
}
});

await editor.update(() => {
const root = $getRoot();
const table = root.getLastChild<TableNode>();
expectTableHtmlToBeEqual(
table!.createDOM(editorConfig).outerHTML,
html`
<table class="${editorConfig.theme.table}">
<colgroup>
<col />
<col />
<col />
<col />
</colgroup>
</table>
`,
);
});
});

test('Update column widths', async () => {
const {editor} = testEnv;

Expand Down
4 changes: 4 additions & 0 deletions packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ describe('LexicalEditor tests', () => {
nodes: nodes ?? [],
onError: onError || jest.fn(),
theme: {
tableAlignment: {
center: 'editor-table-alignment-center',
right: 'editor-table-alignment-right',
},
text: {
bold: 'editor-text-bold',
italic: 'editor-text-italic',
Expand Down
Loading