Skip to content

Commit

Permalink
fix column remove
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaanj committed Nov 19, 2023
1 parent c892c56 commit 7dd65bd
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export const useTableCellElementResizable = ({
ELEMENT_TABLE
);

// MERGE: override width for horizontally merged cell
let initialWidth: number | undefined;
if (colSpan > 1) {
initialWidth = tableElement.colSizes?.[colIndex];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const useSelectedCells = () => {
const { tableEntries, cellEntries } = getTableGridAbove(editor, {
format: 'all',
});
console.log('tableEntries', tableEntries, 'cellEntries', cellEntries);
if (cellEntries?.length > 1) {
const cells = cellEntries.map((entry) => entry[0]);
const tables = tableEntries.map((entry) => entry[0]);
Expand Down
6 changes: 6 additions & 0 deletions packages/table/src/components/TableElement/useTableElement.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect } from 'react';
import {
collapseSelection,
getPluginOptions,
Expand All @@ -6,6 +7,7 @@ import {
} from '@udecode/plate-common';

import { ELEMENT_TABLE } from '../../createTablePlugin';
import { computeAllCellIndices } from '../../merge/computeCellIndices';
import { useTableStore } from '../../stores/tableStore';
import { TablePlugin, TTableElement } from '../../types';
import { useSelectedCells } from './useSelectedCells';
Expand Down Expand Up @@ -43,6 +45,10 @@ export const useTableElementState = ({

let colSizes = useTableColSizes(element);

useEffect(() => {
computeAllCellIndices(editor, element);
}, [editor, element]);

if (transformColSizes) {
colSizes = transformColSizes(colSizes);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/table/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export const keyShiftEdges = {
'shift+down': 'bottom',
'shift+left': 'left',
};

export const MIN_COLUMN_HEIGHT = 48;
1 change: 0 additions & 1 deletion packages/table/src/merge/computeCellIndices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export function computeCellIndices<V extends Value>(
});

if (rowIndex === -1 || colIndex === -1) {
console.log('Invalid cell location.', rowIndex, colIndex);
return null;
}

Expand Down
30 changes: 30 additions & 0 deletions packages/table/src/merge/createEmptyCell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
getPluginType,
PlateEditor,
TDescendant,
TElement,
Value,
} from '@udecode/plate-common';

import { ELEMENT_TH } from '../createTablePlugin';
import { TTableRowElement } from '../types';
import { getEmptyCellNode } from '../utils';

export const createEmptyCell = <V extends Value>(
editor: PlateEditor<V>,
row: TTableRowElement,
newCellChildren?: TDescendant[],
header?: boolean
) => {
const isHeaderRow =
header === undefined
? (row as TElement).children.every(
(c) => c.type === getPluginType(editor, ELEMENT_TH)
)
: header;

return getEmptyCellNode(editor, {
header: isHeaderRow,
newCellChildren,
});
};
74 changes: 16 additions & 58 deletions packages/table/src/merge/deleteColumn.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
focusEditor,
getAboveNode,
getPluginOptions,
getPluginType,
insertElements,
PlateEditor,
removeNodes,
setNodes,
Expand All @@ -13,14 +13,8 @@ import {
import { Path } from 'slate';

import { ELEMENT_TABLE, ELEMENT_TR } from '../createTablePlugin';
import { getTableColumnCount } from '../queries';
import { getColSpan } from '../queries/getColSpan';
import {
TablePlugin,
TTableCellElement,
TTableElement,
TTableRowElement,
} from '../types';
import { TablePlugin, TTableCellElement, TTableElement } from '../types';
import { getCellTypes } from '../utils';
import { findCellByIndexes } from './findCellByIndexes';
import { getCellIndices } from './getCellIndices';
Expand Down Expand Up @@ -71,9 +65,8 @@ export const deleteTableMergeColumn = <V extends Value>(
});
const affectedCells = Array.from(affectedCellsSet) as TTableCellElement[];

const { moveToNextColCells, squizeColSpanCells } = affectedCells.reduce<{
const { squizeColSpanCells } = affectedCells.reduce<{
squizeColSpanCells: TTableCellElement[];
moveToNextColCells: TTableCellElement[];
}>(
(acc, cur) => {
if (!cur) return acc;
Expand All @@ -88,57 +81,16 @@ export const deleteTableMergeColumn = <V extends Value>(
curColSpan > 1 &&
curColIndex + curColSpan - 1 > endingColIndex
) {
acc.moveToNextColCells.push(currentCell);
acc.squizeColSpanCells.push(currentCell);
}
return acc;
},
{ moveToNextColCells: [], squizeColSpanCells: [] }
{ squizeColSpanCells: [] }
);

const nextColIndex = deletingColIndex + colsDeleteNumber;
const colNumber = getTableColumnCount(table);
if (colNumber > nextColIndex) {
moveToNextColCells.forEach((cur) => {
const curCell = cur as TTableCellElement;
const { col: curColIndex, row: curRowIndex } = getCellIndices(
options,
curCell
)!;

const curColSpan = getColSpan(curCell);

// simplify logic here. use getParent
const curRow = table.children[curRowIndex] as TTableRowElement;
const startingCellIndex = curRow.children.findIndex((curC) => {
const cell = curC as TTableCellElement;
const { col: cellColIndex } = getCellIndices(options, cell)!;
return cellColIndex >= curColIndex + 1;
});

const startingCell = curRow.children.at(
startingCellIndex
) as TTableCellElement;
const { col: startingColIndex, row: startingRowIndex } = getCellIndices(
options,
startingCell
)!;

const startingCellPath = getCellPath(
editor,
tableEntry,
startingRowIndex,
startingColIndex
);
const colsNumberAffected = endingColIndex - curColIndex + 1;

const newCell = {
...curCell,
colSpan: curColSpan - colsNumberAffected,
};
insertElements(editor, newCell, { at: startingCellPath });
});
}

/**
* Change colSpans
*/
squizeColSpanCells.forEach((cur) => {
const curCell = cur as TTableCellElement;

Expand Down Expand Up @@ -172,6 +124,9 @@ export const deleteTableMergeColumn = <V extends Value>(
match: { type: getPluginType(editor, ELEMENT_TR) },
});

/**
* Remove cells
*/
if (
selectedCell &&
trEntry &&
Expand All @@ -189,8 +144,11 @@ export const deleteTableMergeColumn = <V extends Value>(
options,
curCell
)!;

if (curColIndex >= deletingColIndex && curColIndex <= endingColIndex) {
if (
!squizeColSpanCells.includes(curCell) &&
curColIndex >= deletingColIndex &&
curColIndex <= endingColIndex
) {
const cellPath = getCellPath(
editor,
tableEntry,
Expand Down
12 changes: 9 additions & 3 deletions packages/table/src/merge/deleteRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const deleteTableRowMerging = <V extends Value>(
const { row: curRowIndex } = getCellIndices(options, currentCell)!;
const curRowSpan = getRowSpan(currentCell);

if (!curRowIndex || !curRowSpan) return acc;
// if (!curRowIndex || !curRowSpan) return acc;

if (curRowIndex < deletingRowIndex && curRowSpan > 1) {
acc.squizeRowSpanCells.push(currentCell);
Expand All @@ -99,7 +99,10 @@ export const deleteTableRowMerging = <V extends Value>(
if (nextRow) {
moveToNextRowCells.forEach((cur, index) => {
const curRowCell = cur as TTableCellElement;
const { col: curRowCellColIndex } = getCellIndices(options, curRowCell)!;
const { col: curRowCellColIndex } = getCellIndices(
options,
curRowCell
)!;
const curRowCellRowSpan = getRowSpan(curRowCell);

// search for anchor cell where to place current cell
Expand All @@ -112,7 +115,10 @@ export const deleteTableRowMerging = <V extends Value>(
const startingCell = nextRow.children[
startingCellIndex
] as TTableCellElement;
const { col: startingColIndex } = getCellIndices(options, startingCell)!;
const { col: startingColIndex } = getCellIndices(
options,
startingCell
)!;

// consider already inserted cell by adding index each time to the col path
let incrementBy = index;
Expand Down
13 changes: 4 additions & 9 deletions packages/table/src/merge/getTableGridByRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,25 @@ export const getTableMergeGridByRange = <T extends FormatType, V extends Value>(
const formatType = (format as string) || 'table';

if (formatType === 'cell') {
console.log(0);

return cellEntries as GetTableGridReturnType<T>;
}

// clear redundant cells
table.children?.forEach((rowEl) => {
const rowElement = rowEl as TTableRowElement;

const filteredChildren = rowElement.children?.filter((cellEl) => {
const cellElement = cellEl as TTableCellElement;
return !!cellElement?.children.length;
});

rowElement.children = filteredChildren;
});

if (formatType === 'table') {
console.log(11)
return [[table, tablePath]] as GetTableGridReturnType<T>;
}

console.log(22)

return {
tableEntries: [[table, tablePath]],
cellEntries,
Expand Down
29 changes: 4 additions & 25 deletions packages/table/src/merge/insertTableColumn.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import {
findNode,
getBlockAbove,
getNodeEntry,
getParentNode,
getPluginOptions,
getPluginType,
insertElements,
PlateEditor,
setNodes,
TDescendant,
TElement,
Value,
withoutNormalizing,
} from '@udecode/plate-common';
import { Path } from 'slate';

import { ELEMENT_TABLE, ELEMENT_TH } from '../createTablePlugin';
import { ELEMENT_TABLE } from '../createTablePlugin';
import { getColSpan } from '../queries/getColSpan';
import { getRowSpan } from '../queries/getRowSpan';
import {
Expand All @@ -24,29 +21,11 @@ import {
TTableElement,
TTableRowElement,
} from '../types';
import { getCellTypes, getEmptyCellNode } from '../utils';
import { getCellTypes } from '../utils';
import { createEmptyCell } from './createEmptyCell';
import { findCellByIndexes } from './findCellByIndexes';
import { getCellPath } from './getCellPath';
import { getCellIndices } from './getCellIndices';

const createEmptyCell = <V extends Value>(
editor: PlateEditor<V>,
row: TTableRowElement,
newCellChildren?: TDescendant[],
header?: boolean
) => {
const isHeaderRow =
header === undefined
? (row as TElement).children.every(
(c) => c.type === getPluginType(editor, ELEMENT_TH)
)
: header;

return getEmptyCellNode(editor, {
header: isHeaderRow,
newCellChildren,
});
};
import { getCellPath } from './getCellPath';

export const insertTableColumnMerging = <V extends Value>(
editor: PlateEditor<V>,
Expand Down
28 changes: 4 additions & 24 deletions packages/table/src/merge/insertTableRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import {
PlateEditor,
select,
setNodes,
TDescendant,
TElement,
Value,
withoutNormalizing,
} from '@udecode/plate-common';
import { Path } from 'slate';

import { ELEMENT_TABLE, ELEMENT_TH, ELEMENT_TR } from '../createTablePlugin';
import { ELEMENT_TABLE, ELEMENT_TR } from '../createTablePlugin';
import { getTableColumnCount } from '../queries';
import { getColSpan } from '../queries/getColSpan';
import { getRowSpan } from '../queries/getRowSpan';
Expand All @@ -25,29 +23,11 @@ import {
TTableElement,
TTableRowElement,
} from '../types';
import { getCellTypes, getEmptyCellNode } from '../utils';
import { getCellTypes } from '../utils';
import { createEmptyCell } from './createEmptyCell';
import { findCellByIndexes } from './findCellByIndexes';
import { getCellPath } from './getCellPath';
import { getCellIndices } from './getCellIndices';

const createEmptyCell = <V extends Value>(
editor: PlateEditor<V>,
row: TTableRowElement,
newCellChildren?: TDescendant[],
header?: boolean
) => {
const isHeaderRow =
header === undefined
? (row as TElement).children.every(
(c) => c.type === getPluginType(editor, ELEMENT_TH)
)
: header;

return getEmptyCellNode(editor, {
header: isHeaderRow,
newCellChildren,
});
};
import { getCellPath } from './getCellPath';

export const insertTableRowMerging = <V extends Value>(
editor: PlateEditor<V>,
Expand Down
1 change: 0 additions & 1 deletion packages/table/src/merge/isTableRectangular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const isTableRectangular = (table?: TTableElement) => {
if (!arr[rI + i]) {
arr[rI + i] = 0;
}
console.log('current', cellElem?.colSpan || 1);
arr[rI + i] += cellElem?.colSpan || 1;
});
});
Expand Down
Loading

0 comments on commit 7dd65bd

Please sign in to comment.