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

[Table] Fix merge headers #2832

Merged
merged 3 commits into from
Dec 26, 2023
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
6 changes: 6 additions & 0 deletions .changeset/serious-masks-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@udecode/plate-table": patch
---

- Fix: merge of header cells in table
- Fix: #2831
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const useTableCellElementState = ({
const rowIndex = getTableRowIndex(editor, cellElement);

const rowSize =
rowSizeOverrides.get(rowIndex) ?? rowElement?.size ?? undefined;
rowSizeOverrides.get?.(rowIndex) ?? rowElement?.size ?? undefined;

const isFirstCell = colIndex === 0;
const isFirstRow = tableElement.children?.[0] === rowElement;
Expand Down Expand Up @@ -115,7 +115,7 @@ export const useTableCellElementState = ({
const endingColIndex = colIndex + colSpan - 1;

const rowSize =
rowSizeOverrides.get(endingRowIndex) ?? rowElement?.size ?? undefined;
rowSizeOverrides.get?.(endingRowIndex) ?? rowElement?.size ?? undefined;

const isFirstCell = colIndex === 0;
const isFirstRow = tableElement.children?.[0] === rowElement;
Expand Down
13 changes: 4 additions & 9 deletions packages/table/src/merge/mergeTableCells.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
collapseSelection,
getBlockAbove,
getPluginOptions,
getPluginType,
Expand All @@ -10,7 +11,7 @@ import {
} from '@udecode/plate-common';
import { cloneDeep } from 'lodash';

import { ELEMENT_TABLE } from '../createTablePlugin';
import { ELEMENT_TABLE, ELEMENT_TH } from '../createTablePlugin';
import { getTableGridAbove } from '../queries';
import { getColSpan } from '../queries/getColSpan';
import { getRowSpan } from '../queries/getRowSpan';
Expand Down Expand Up @@ -78,14 +79,7 @@ export const mergeTableCells = <V extends Value = Value>(
// and values are an array of all paths with that column
const cols: { [key: string]: number[][] } = {};

// A boolean to keep track if we have a header cell among the cells we are merging
let hasHeaderCell = false;

cellEntries.forEach(([entry, path]) => {
if (!hasHeaderCell && entry.type === 'table_header_cell') {
hasHeaderCell = true;
}

const rowIndex = path.at(-2)!;

if (cols[rowIndex]) {
Expand All @@ -107,7 +101,7 @@ export const mergeTableCells = <V extends Value = Value>(
// calculated colSpan and rowSpan attributes and combined content
const mergedCell = {
...getEmptyCellNode(editor, {
header: cellEntries[0][0].type === 'th',
header: cellEntries[0][0].type === getPluginType(editor, ELEMENT_TH),
newCellChildren: contents,
}),
colSpan,
Expand All @@ -116,5 +110,6 @@ export const mergeTableCells = <V extends Value = Value>(

// insert the new merged cell in place of the first cell in the selection
insertElements(editor, mergedCell, { at: cellEntries[0][1] });
collapseSelection(editor);
});
};
4 changes: 2 additions & 2 deletions packages/table/src/merge/unmergeTableCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
withoutNormalizing,
} from '@udecode/plate-common';

import { ELEMENT_TABLE, ELEMENT_TR } from '../createTablePlugin';
import { ELEMENT_TABLE, ELEMENT_TH, ELEMENT_TR } from '../createTablePlugin';
import { getTableGridAbove } from '../queries';
import { getColSpan } from '../queries/getColSpan';
import { getRowSpan } from '../queries/getRowSpan';
Expand All @@ -34,7 +34,7 @@ export const unmergeTableCells = <V extends Value = Value>(
const createEmptyCell = (children?: TDescendant[]) => {
return {
...getEmptyCellNode(editor, {
header: cellElem.type === 'th',
header: cellElem.type === getPluginType(editor, ELEMENT_TH),
newCellChildren: children,
}),
colSpan: 1,
Expand Down
2 changes: 1 addition & 1 deletion packages/table/src/queries/getTableOverriddenColSizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getTableOverriddenColSizes = (
tableNode.colSizes
? [...tableNode.colSizes]
: (Array.from({ length: colCount }).fill(0) as number[])
).map((size, index) => colSizeOverrides?.get(index) ?? size);
).map((size, index) => colSizeOverrides?.get?.(index) ?? size);

return colSizes;
};
2 changes: 1 addition & 1 deletion packages/table/src/transforms/insertTableRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const insertTableRow = <V extends Value>(
const isHeaderColumn =
!hasSingleRow &&
(tableEntry[0].children as TElement[]).every(
(n) => n.children[i].type === ELEMENT_TH
(n) => n.children[i].type === getPluginType(editor, ELEMENT_TH)
);
return getEmptyCellNode(editor, {
header: header ?? isHeaderColumn,
Expand Down