Skip to content

Commit

Permalink
Add more configuration for heading in INSERT_TABLE_COMMAND (facebook#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien-Ahkrin authored Feb 8, 2023
1 parent fcf2330 commit 75844c3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/lexical-playground/src/plugins/TablePlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ export function InsertTableDialog({
const [columns, setColumns] = useState('5');

const onClick = () => {
activeEditor.dispatchCommand(INSERT_TABLE_COMMAND, {columns, rows});
activeEditor.dispatchCommand(INSERT_TABLE_COMMAND, {
columns,
rows,
});

onClose();
};

Expand Down
10 changes: 8 additions & 2 deletions packages/lexical-table/src/LexicalTableUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {$findMatchingParent} from '@lexical/utils';
import {$createParagraphNode, $createTextNode} from 'lexical';
import invariant from 'shared/invariant';

import {InsertTableCommandPayloadHeaders} from '.';
import {
$createTableCellNode,
$isTableCellNode,
Expand All @@ -29,7 +30,7 @@ import {
export function $createTableNodeWithDimensions(
rowCount: number,
columnCount: number,
includeHeaders = true,
includeHeaders: InsertTableCommandPayloadHeaders = true,
): TableNode {
const tableNode = $createTableNode();

Expand All @@ -39,7 +40,12 @@ export function $createTableNodeWithDimensions(
for (let iColumn = 0; iColumn < columnCount; iColumn++) {
let headerState = TableCellHeaderStates.NO_STATUS;

if (includeHeaders) {
if (typeof includeHeaders === 'object') {
if (iRow === 0 && includeHeaders.rows)
headerState |= TableCellHeaderStates.ROW;
if (iColumn === 0 && includeHeaders.columns)
headerState |= TableCellHeaderStates.COLUMN;
} else if (includeHeaders) {
if (iRow === 0) headerState |= TableCellHeaderStates.ROW;
if (iColumn === 0) headerState |= TableCellHeaderStates.COLUMN;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/lexical-table/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ export type {
SerializedTableRowNode,
};

export type InsertTableCommandPayloadHeaders =
| Readonly<{
rows: boolean;
columns: boolean;
}>
| boolean;

export type InsertTableCommandPayload = Readonly<{
columns: string;
rows: string;
includeHeaders?: boolean;
includeHeaders?: InsertTableCommandPayloadHeaders;
}>;

export const INSERT_TABLE_COMMAND: LexicalCommand<InsertTableCommandPayload> =
Expand Down

0 comments on commit 75844c3

Please sign in to comment.