Skip to content

Commit

Permalink
Merge pull request #3172 from KorovinQuantori/main
Browse files Browse the repository at this point in the history
Add computeCellIndices fallback for table insert functions
  • Loading branch information
zbeyens committed May 6, 2024
2 parents 5ff6a02 + 93c3c0d commit c2c276a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-balloons-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-table": patch
---

Add computeCellIndices fallback for table insert functions
2 changes: 1 addition & 1 deletion packages/table/src/merge/computeCellIndices.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getPluginOptions, PlateEditor, Value } from '@udecode/plate-common';

import { ELEMENT_TABLE } from '../createTablePlugin';
import { getColSpan } from '../queries';
import { getRowSpan } from '../queries/getRowSpan';
import {
TablePlugin,
TTableCellElement,
TTableElement,
TTableRowElement,
} from '../types';
import { getColSpan } from '../queries';

export function computeCellIndices<V extends Value>(
editor: PlateEditor<V>,
Expand Down
12 changes: 7 additions & 5 deletions packages/table/src/merge/insertTableColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
TTableRowElement,
} from '../types';
import { getCellTypes } from '../utils';
import { computeCellIndices } from './computeCellIndices';
import { createEmptyCell } from './createEmptyCell';
import { findCellByIndexes } from './findCellByIndexes';
import { getCellIndices } from './getCellIndices';
Expand Down Expand Up @@ -82,7 +83,9 @@ export const insertTableMergeColumn = <V extends Value>(
getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const [tableNode, tablePath] = tableEntry;

const { col: cellColIndex } = getCellIndices(cellIndices!, cell)!;
const { col: cellColIndex } =
getCellIndices(cellIndices!, cell) ||
computeCellIndices(editor, tableNode, cell)!;
const cellColSpan = getColSpan(cell);

let nextColIndex: number;
Expand Down Expand Up @@ -118,10 +121,9 @@ export const insertTableMergeColumn = <V extends Value>(

affectedCells.forEach((cur) => {
const curCell = cur as TTableCellElement;
const { row: curRowIndex, col: curColIndex } = getCellIndices(
cellIndices!,
curCell
)!;
const { row: curRowIndex, col: curColIndex } =
getCellIndices(cellIndices!, curCell) ||
computeCellIndices(editor, tableNode, curCell)!;
const curRowSpan = getRowSpan(curCell);
const curColSpan = getColSpan(curCell);

Expand Down
12 changes: 7 additions & 5 deletions packages/table/src/merge/insertTableRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
TTableRowElement,
} from '../types';
import { getCellTypes } from '../utils';
import { computeCellIndices } from './computeCellIndices';
import { createEmptyCell } from './createEmptyCell';
import { findCellByIndexes } from './findCellByIndexes';
import { getCellIndices } from './getCellIndices';
Expand Down Expand Up @@ -82,7 +83,9 @@ export const insertTableMergeRow = <V extends Value>(
const [cellNode, cellPath] = cellEntry;
const cellElement = cellNode as TTableCellElement;
const cellRowSpan = getRowSpan(cellElement);
const { row: cellRowIndex } = getCellIndices(cellIndices!, cellElement)!;
const { row: cellRowIndex } =
getCellIndices(cellIndices!, cellElement) ||
computeCellIndices(editor, tableNode, cellElement)!;

const rowPath = cellPath.at(-2)!;
const tablePath = cellPath.slice(0, -2)!;
Expand Down Expand Up @@ -120,10 +123,9 @@ export const insertTableMergeRow = <V extends Value>(
if (!cur) return;

const curCell = cur as TTableCellElement;
const { row: curRowIndex, col: curColIndex } = getCellIndices(
cellIndices!,
curCell
)!;
const { row: curRowIndex, col: curColIndex } =
getCellIndices(cellIndices!, curCell) ||
computeCellIndices(editor, tableNode, curCell)!;

const curRowSpan = getRowSpan(curCell);
const curColSpan = getColSpan(curCell);
Expand Down
2 changes: 1 addition & 1 deletion packages/table/src/merge/useTableMergeState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getTableGridAbove } from '../queries';
import { getColSpan } from '../queries/getColSpan';
import { getRowSpan } from '../queries/getRowSpan';
import { useTableStore } from '../stores';
import { TTableCellElement, TablePlugin } from '../types';
import { TablePlugin, TTableCellElement } from '../types';
import { isTableRectangular } from './isTableRectangular';

export const useTableMergeState = () => {
Expand Down

0 comments on commit c2c276a

Please sign in to comment.