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

plugin-table: Add at option for inserts #2006

Merged
merged 3 commits into from
Nov 21, 2022
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
5 changes: 5 additions & 0 deletions .changeset/cuddly-cups-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-table": patch
---

`insertTableColumn`, `insertTableRow`: new option `at`
77 changes: 77 additions & 0 deletions packages/nodes/table/src/transforms/insertTableColumn.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,81 @@ describe('insertTableColumn', () => {
expect(editor.selection).toEqual(output.selection);
});
});

describe('when using at', () => {
it('should insert', () => {
const input = ((
<editor>
<htable>
<htr>
<htd>
<hp>11</hp>
</htd>
<htd>
<hp>12</hp>
</htd>
</htr>
<htr>
<htd>
<hp>
21
<cursor />
</hp>
</htd>
<htd>
<hp>22</hp>
</htd>
</htr>
</htable>
</editor>
) as any) as PlateEditor;

const output = ((
<editor>
<htable>
<htr>
<htd>
<hp>
<htext />
</hp>
</htd>
<htd>
<hp>11</hp>
</htd>
<htd>
<hp>12</hp>
</htd>
</htr>
<htr>
<htd>
<hp>
<cursor />
</hp>
</htd>
<htd>
<hp>21</hp>
</htd>
<htd>
<hp>22</hp>
</htd>
</htr>
</htable>
</editor>
) as any) as PlateEditor;

const editor = createPlateEditor({
editor: input,
plugins: [
createTablePlugin({
options: { newCellChildren: [{ text: '' }] },
}),
],
});

insertTableColumn(editor, { at: [0, 0, 0] });

expect(editor.children).toEqual(output.children);
expect(editor.selection).toEqual(output.selection);
});
});
});
25 changes: 22 additions & 3 deletions packages/nodes/table/src/transforms/insertTableColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const insertTableColumn = <V extends Value>(
{
disableSelect,
fromCell,
at,
header,
}: {
header?: boolean;
Expand All @@ -30,6 +31,12 @@ export const insertTableColumn = <V extends Value>(
*/
fromCell?: Path;

/**
* Exact path of the cell to insert the column at.
* Will overrule `fromCell`.
*/
at?: Path;

/**
* Disable selection after insertion.
*/
Expand All @@ -56,8 +63,16 @@ export const insertTableColumn = <V extends Value>(

const [tableNode, tablePath] = tableEntry;

const nextCellPath = Path.next(cellPath);
const nextColIndex = cellPath[cellPath.length - 1] + 1;
let nextCellPath: Path;
let nextColIndex: number;

if (Path.isPath(at)) {
nextCellPath = at;
nextColIndex = at[at.length - 1];
} else {
nextCellPath = Path.next(cellPath);
nextColIndex = cellPath[cellPath.length - 1] + 1;
}
const currentRowIndex = cellPath[cellPath.length - 2];

const { newCellChildren } = getPluginOptions<TablePlugin, V>(
Expand All @@ -69,7 +84,11 @@ export const insertTableColumn = <V extends Value>(
// for each row, insert a new cell
tableNode.children.forEach((row, rowIndex) => {
const insertCellPath = [...nextCellPath];
insertCellPath[cellPath.length - 2] = rowIndex;
if (Path.isPath(at)) {
insertCellPath[at.length - 2] = rowIndex;
} else {
insertCellPath[cellPath.length - 2] = rowIndex;
}

const isHeaderRow =
header === undefined
Expand Down
79 changes: 79 additions & 0 deletions packages/nodes/table/src/transforms/insertTableRow.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,83 @@ describe('insertTableRow', () => {
expect(editor.selection).toEqual(output.selection);
});
});

describe('when inserting a table row at specific path', () => {
it('should insert a tr with empty cells', () => {
const input = ((
<editor>
<htable>
<htr>
<htd>
<hp>11</hp>
</htd>
<htd>
<hp>12</hp>
</htd>
</htr>
<htr>
<htd>
<hp>
21
<cursor />
</hp>
</htd>
<htd>
<hp>22</hp>
</htd>
</htr>
</htable>
</editor>
) as any) as PlateEditor;

const output = ((
<editor>
<htable>
<htr>
<htd>
<hp>
<cursor />
</hp>
</htd>
<htd>
<hp>
<htext />
</hp>
</htd>
</htr>
<htr>
<htd>
<hp>11</hp>
</htd>
<htd>
<hp>12</hp>
</htd>
</htr>
<htr>
<htd>
<hp>21</hp>
</htd>
<htd>
<hp>22</hp>
</htd>
</htr>
</htable>
</editor>
) as any) as PlateEditor;

const editor = createPlateEditor({
editor: input,
plugins: [
createTablePlugin({
options: { newCellChildren: [{ text: '' }] },
}),
],
});

insertTableRow(editor, { at: [0, 0] });

expect(editor.children).toEqual(output.children);
expect(editor.selection).toEqual(output.selection);
});
});
});
21 changes: 17 additions & 4 deletions packages/nodes/table/src/transforms/insertTableRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@ export const insertTableRow = <V extends Value>(
{
header,
fromRow,
at,
disableSelect,
}: { header?: boolean; fromRow?: Path; disableSelect?: boolean } = {}
}: {
header?: boolean;
fromRow?: Path;
/**
* Exact path of the row to insert the column at.
* Will overrule `fromRow`.
*/
at?: Path;
disableSelect?: boolean;
} = {}
) => {
const trEntry = fromRow
? findNode(editor, {
Expand Down Expand Up @@ -55,7 +65,7 @@ export const insertTableRow = <V extends Value>(
newCellChildren,
}),
{
at: Path.next(trPath),
at: Path.isPath(at) ? at : Path.next(trPath),
}
);
});
Expand All @@ -67,8 +77,11 @@ export const insertTableRow = <V extends Value>(
if (!cellEntry) return;

const [, nextCellPath] = cellEntry;

nextCellPath[nextCellPath.length - 2] += 1;
if (Path.isPath(at)) {
nextCellPath[nextCellPath.length - 2] = at[at.length - 2];
} else {
nextCellPath[nextCellPath.length - 2] += 1;
}

select(editor, nextCellPath);
}
Expand Down