Skip to content

Commit

Permalink
🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
zbeyens committed Nov 23, 2023
1 parent 0b8b325 commit 61157c1
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 34 deletions.
17 changes: 11 additions & 6 deletions packages/table/src/merge/deleteColumn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
focusEditor,
getAboveNode,
getPluginOptions,
getPluginType,
Expand Down Expand Up @@ -28,7 +27,10 @@ export const deleteTableMergeColumn = <V extends Value>(
match: { type: getPluginType(editor, ELEMENT_TABLE) },
})
) {
const options = getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const { _cellIndices: cellIndices } = getPluginOptions<TablePlugin, V>(
editor,
ELEMENT_TABLE
);

const tableEntry = getAboveNode<TTableElement>(editor, {
match: { type: getPluginType(editor, ELEMENT_TABLE) },
Expand All @@ -44,7 +46,10 @@ export const deleteTableMergeColumn = <V extends Value>(
if (!selectedCellEntry) return;
const selectedCell = selectedCellEntry[0] as TTableCellElement;

const { col: deletingColIndex } = getCellIndices(options, selectedCell)!;
const { col: deletingColIndex } = getCellIndices(
cellIndices!,
selectedCell
)!;
const colsDeleteNumber = getColSpan(selectedCell);

const endingColIndex = deletingColIndex + colsDeleteNumber - 1;
Expand Down Expand Up @@ -72,7 +77,7 @@ export const deleteTableMergeColumn = <V extends Value>(
if (!cur) return acc;

const currentCell = cur as TTableCellElement;
const { col: curColIndex } = getCellIndices(options, currentCell)!;
const { col: curColIndex } = getCellIndices(cellIndices!, currentCell)!;
const curColSpan = getColSpan(currentCell);

if (curColIndex < deletingColIndex && curColSpan > 1) {
Expand All @@ -95,7 +100,7 @@ export const deleteTableMergeColumn = <V extends Value>(
const curCell = cur as TTableCellElement;

const { col: curColIndex, row: curColRowIndex } = getCellIndices(
options,
cellIndices!,
curCell
)!;
const curColSpan = getColSpan(curCell);
Expand Down Expand Up @@ -141,7 +146,7 @@ export const deleteTableMergeColumn = <V extends Value>(
affectedCells.forEach((cur) => {
const curCell = cur as TTableCellElement;
const { col: curColIndex, row: curRowIndex } = getCellIndices(
options,
cellIndices!,
curCell
)!;
if (
Expand Down
23 changes: 16 additions & 7 deletions packages/table/src/merge/deleteRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export const deleteTableMergeRow = <V extends Value>(
match: { type: getPluginType(editor, ELEMENT_TABLE) },
})
) {
const options = getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const { _cellIndices: cellIndices } = getPluginOptions<TablePlugin, V>(
editor,
ELEMENT_TABLE
);

const currentTableItem = getAboveNode<TTableElement>(editor, {
match: { type: getPluginType(editor, ELEMENT_TABLE) },
Expand All @@ -46,7 +49,10 @@ export const deleteTableMergeRow = <V extends Value>(
if (!selectedCellEntry) return;

const selectedCell = selectedCellEntry[0] as TTableCellElement;
const { row: deletingRowIndex } = getCellIndices(options, selectedCell)!;
const { row: deletingRowIndex } = getCellIndices(
cellIndices!,
selectedCell
)!;
const rowsDeleteNumber = getRowSpan(selectedCell);
const endingRowIndex = deletingRowIndex + rowsDeleteNumber - 1;

Expand All @@ -72,7 +78,7 @@ export const deleteTableMergeRow = <V extends Value>(
if (!cur) return acc;

const currentCell = cur as TTableCellElement;
const { row: curRowIndex } = getCellIndices(options, currentCell)!;
const { row: curRowIndex } = getCellIndices(cellIndices!, currentCell)!;
const curRowSpan = getRowSpan(currentCell);

// if (!curRowIndex || !curRowSpan) return acc;
Expand Down Expand Up @@ -100,23 +106,23 @@ export const deleteTableMergeRow = <V extends Value>(
moveToNextRowCells.forEach((cur, index) => {
const curRowCell = cur as TTableCellElement;
const { col: curRowCellColIndex } = getCellIndices(
options,
cellIndices!,
curRowCell
)!;
const curRowCellRowSpan = getRowSpan(curRowCell);

// search for anchor cell where to place current cell
const startingCellIndex = nextRow.children.findIndex((curC) => {
const cell = curC as TTableCellElement;
const { col: curColIndex } = getCellIndices(options, cell)!;
const { col: curColIndex } = getCellIndices(cellIndices!, cell)!;
return curColIndex >= curRowCellColIndex;
});

const startingCell = nextRow.children[
startingCellIndex
] as TTableCellElement;
const { col: startingColIndex } = getCellIndices(
options,
cellIndices!,
startingCell
)!;

Expand Down Expand Up @@ -151,7 +157,10 @@ export const deleteTableMergeRow = <V extends Value>(

squizeRowSpanCells.forEach((cur) => {
const curRowCell = cur as TTableCellElement;
const { row: curRowCellRowIndex } = getCellIndices(options, curRowCell)!;
const { row: curRowCellRowIndex } = getCellIndices(
cellIndices!,
curRowCell
)!;
const curRowCellRowSpan = getRowSpan(curRowCell);

const curCellPath = findNodePath(editor, curRowCell)!;
Expand Down
7 changes: 5 additions & 2 deletions packages/table/src/merge/findCellByIndexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export const findCellByIndexes = <V extends Value>(
searchRowIndex: number,
searchColIndex: number
) => {
const options = getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const { _cellIndices: cellIndices } = getPluginOptions<TablePlugin, V>(
editor,
ELEMENT_TABLE
);

const allCells = table.children.flatMap(
(current) => current.children
Expand All @@ -22,7 +25,7 @@ export const findCellByIndexes = <V extends Value>(
const cellElement = cell as TTableCellElement;

const indices =
getCellIndices(options, cellElement) ||
getCellIndices(cellIndices!, cellElement) ||
computeCellIndices(editor, table, cellElement)!;

const { col: _startColIndex, row: _startRowIndex } = indices;
Expand Down
7 changes: 5 additions & 2 deletions packages/table/src/merge/getCellPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ export const getCellPath = <V extends Value>(
curRowIndex: number,
curColIndex: number
) => {
const options = getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const { _cellIndices: cellIndices } = getPluginOptions<TablePlugin, V>(
editor,
ELEMENT_TABLE
);
const [tableNode, tablePath] = tableEntry;

const rowElem = tableNode.children[curRowIndex] as TTableRowElement;
const foundColIndex = rowElem.children.findIndex((c) => {
const cE = c as TTableCellElement;
const { col: colIndex } = getCellIndices(options, cE)!;
const { col: colIndex } = getCellIndices(cellIndices!, cE)!;
return colIndex === curColIndex;
});
return tablePath.concat([curRowIndex, foundColIndex]);
Expand Down
9 changes: 6 additions & 3 deletions packages/table/src/merge/getTableGridByRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export const getTableMergeGridByRange = <T extends FormatType, V extends Value>(
editor: PlateEditor<V>,
{ at, format }: GetTableGridByRangeOptions<T>
): GetTableGridReturnType<T> => {
const options = getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const { _cellIndices: cellIndices } = getPluginOptions<TablePlugin, V>(
editor,
ELEMENT_TABLE
);

const startCellEntry = findNode(editor, {
at: (at as any).anchor.path,
Expand All @@ -77,11 +80,11 @@ export const getTableMergeGridByRange = <T extends FormatType, V extends Value>(
const realTable = tableEntry[0] as TTableElement;

const { col: _startColIndex, row: _startRowIndex } =
getCellIndices(options, startCell) ||
getCellIndices(cellIndices!, startCell) ||
computeCellIndices(editor, realTable, startCell)!;

const { row: _endRowIndex, col: _endColIndex } = getCellIndicesWithSpans(
getCellIndices(options, endCell) ||
getCellIndices(cellIndices!, endCell) ||
computeCellIndices(editor, realTable, endCell)!,
endCell
);
Expand Down
10 changes: 6 additions & 4 deletions packages/table/src/merge/insertTableColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export const insertTableMergeColumn = <V extends Value>(
disableSelect?: boolean;
} = {}
) => {
const options = getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const { _cellIndices: cellIndices } = getPluginOptions<TablePlugin, V>(
editor,
ELEMENT_TABLE
);

const cellEntry = fromCell
? findNode(editor, {
Expand All @@ -79,7 +82,7 @@ export const insertTableMergeColumn = <V extends Value>(
getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const [tableNode, tablePath] = tableEntry;

const { col: cellColIndex } = getCellIndices(options, cell)!;
const { col: cellColIndex } = getCellIndices(cellIndices!, cell)!;
const cellColSpan = getColSpan(cell);

let nextColIndex: number;
Expand All @@ -92,7 +95,6 @@ export const insertTableMergeColumn = <V extends Value>(
checkingColIndex = cellColIndex + cellColSpan - 1;
}

const currentRowIndex = cellPath.at(-2); // recheck it
const rowNumber = tableNode.children.length;
const firstCol = nextColIndex <= 0;

Expand All @@ -117,7 +119,7 @@ export const insertTableMergeColumn = <V extends Value>(
affectedCells.forEach((cur) => {
const curCell = cur as TTableCellElement;
const { row: curRowIndex, col: curColIndex } = getCellIndices(
options,
cellIndices!,
curCell
)!;
const curRowSpan = getRowSpan(curCell);
Expand Down
10 changes: 6 additions & 4 deletions packages/table/src/merge/insertTableRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getPluginType,
insertElements,
PlateEditor,
select,
setNodes,
Value,
withoutNormalizing,
Expand Down Expand Up @@ -47,7 +46,10 @@ export const insertTableMergeRow = <V extends Value>(
disableSelect?: boolean;
} = {}
) => {
const options = getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const { _cellIndices: cellIndices } = getPluginOptions<TablePlugin, V>(
editor,
ELEMENT_TABLE
);

const trEntry = fromRow
? findNode(editor, {
Expand Down Expand Up @@ -80,7 +82,7 @@ export const insertTableMergeRow = <V extends Value>(
const [cellNode, cellPath] = cellEntry;
const cellElement = cellNode as TTableCellElement;
const cellRowSpan = getRowSpan(cellElement);
const { row: cellRowIndex } = getCellIndices(options, cellElement)!;
const { row: cellRowIndex } = getCellIndices(cellIndices!, cellElement)!;

const rowPath = cellPath.at(-2)!;
const tablePath = cellPath.slice(0, -2)!;
Expand Down Expand Up @@ -119,7 +121,7 @@ export const insertTableMergeRow = <V extends Value>(

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

Expand Down
9 changes: 6 additions & 3 deletions packages/table/src/merge/mergeTableCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const mergeTableCells = <V extends Value = Value>(
editor: PlateEditor<V>
) => {
withoutNormalizing(editor, () => {
const options = getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const { _cellIndices } = getPluginOptions<TablePlugin, V>(
editor,
ELEMENT_TABLE
);
const tableEntry = getBlockAbove(editor, {
at: editor.selection?.anchor.path,
match: { type: getPluginType(editor, ELEMENT_TABLE) },
Expand All @@ -49,13 +52,13 @@ export const mergeTableCells = <V extends Value = Value>(
// calculate the rowSpan which is the number of vertical cells that a cell should span.
let rowSpan = 0;
const { col } = getCellIndices(
options,
_cellIndices!,
cellEntries[0][0] as TTableCellElement
)!;
cellEntries.forEach((cE) => {
const cell = cE[0] as TTableCellElement;
const { col: curCol } =
options._cellIndices?.get(cell) ||
_cellIndices?.get(cell) ||
computeCellIndices(editor, tableEntry[0] as TTableElement, cell)!;
if (col === curCol) {
rowSpan += getRowSpan(cell);
Expand Down
15 changes: 12 additions & 3 deletions packages/table/src/merge/unmergeTableCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export const unmergeTableCells = <V extends Value = Value>(
editor: PlateEditor<V>
) => {
withoutNormalizing(editor, () => {
const options = getPluginOptions<TablePlugin, V>(editor, ELEMENT_TABLE);
const { _cellIndices: cellIndices } = getPluginOptions<TablePlugin, V>(
editor,
ELEMENT_TABLE
);

const cellEntries = getTableGridAbove(editor, { format: 'cell' });
const [[cellElem, path]] = cellEntries;
Expand Down Expand Up @@ -54,7 +57,10 @@ export const unmergeTableCells = <V extends Value = Value>(
// Remove the original merged cell from the editor
removeNodes(editor, { at: path });

const { col } = getCellIndices(options, cellElem as TTableCellElement)!;
const { col } = getCellIndices(
cellIndices!,
cellElem as TTableCellElement
)!;

const getColPathForRow = (row: number) => {
let newColPath = 0;
Expand All @@ -66,7 +72,10 @@ export const unmergeTableCells = <V extends Value = Value>(
const rowEl = rowEntry[0] as TTableRowElement;

for (const item of rowEl.children) {
const { col: c } = getCellIndices(options, item as TTableCellElement)!;
const { col: c } = getCellIndices(
cellIndices!,
item as TTableCellElement
)!;
if (c === col - 1) {
newColPath = rowEl.children.indexOf(item) + 1;
break;
Expand Down

0 comments on commit 61157c1

Please sign in to comment.