Skip to content

Commit

Permalink
feat: rows and columns for insertTable (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
forhappy authored Nov 5, 2023
1 parent c71cee4 commit 1c99eb9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
47 changes: 36 additions & 11 deletions src/plugins/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,53 @@ import { LexicalTableVisitor } from './LexicalTableVisitor'
import { MdastTableVisitor } from './MdastTableVisitor'
import { $createTableNode, TableNode } from './TableNode'

function seedTable(): Mdast.Table {
return {
type InsertTablePayload = {
/**
* The nunber of rows of the table.
*/
rows?: number
/**
* The nunber of columns of the table.
*/
columns?: number
}

function seedTable(rows: number = 1, columns : number = 1): Mdast.Table {
const table: Mdast.Table = {
type: 'table',
children: [
{
type: 'tableRow',
children: [{ type: 'tableCell', children: [] }]
}
]
children: []
};

for (let i = 0; i < rows; i++) {
const tableRow: Mdast.TableRow = {
type: 'tableRow',
children: []
};

for (let j = 0; j < columns; j++) {
const cell: Mdast.TableCell = {
type: 'tableCell',
children: []
};
tableRow.children.push(cell);
}

table.children.push(tableRow);
}

return table;
}

/** @internal */
export const tableSystem = system(
(r, [{ insertDecoratorNode }]) => {
const insertTable = r.node<true>()
const insertTable = r.node<InsertTablePayload>()

r.link(
r.pipe(
insertTable,
r.o.mapTo(() => {
return $createTableNode(seedTable())
r.o.map(({rows, columns}) => {
return () => $createTableNode(seedTable(rows, columns))
})
),
insertDecoratorNode
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/toolbar/components/InsertTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const InsertTable: React.FC = () => {
<ButtonWithTooltip
title="Insert table"
onClick={() => {
insertTable(true)
insertTable({rows: 3, columns: 3})
}}
>
<TableIcon />
Expand Down

0 comments on commit 1c99eb9

Please sign in to comment.