Skip to content

Commit

Permalink
fix some other stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaanj committed Nov 29, 2023
1 parent 1e20072 commit fad078e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
37 changes: 32 additions & 5 deletions packages/table/src/merge/unmergeTableCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ export const unmergeTableCells = <V extends Value = Value>(
at: [...tablePath, row],
match: { type: getPluginType(editor, ELEMENT_TR) },
})!; // TODO: improve typing
const rowEl = rowEntry[0] as TTableRowElement;

if (!rowEntry) {
return newColPath;
}

const rowEl = rowEntry[0] as TTableRowElement;
for (const item of rowEl.children) {
const { col: c } = getCellIndices(
cellIndices!,
Expand All @@ -94,16 +98,39 @@ export const unmergeTableCells = <V extends Value = Value>(
for (let i = 0; i < rowSpan; i++) {
const currentRowPath = rowPath + i;
const pathForNextRows = getColPathForRow(currentRowPath);
for (let j = 0; j < colPaths.length; j++) {
const currentColPath = i === 0 ? colPaths[j] : pathForNextRows;
const newRowChildren = [];
const _rowPath = [...tablePath, currentRowPath];
const rowEntry = findNode(editor, {
at: _rowPath,
match: { type: getPluginType(editor, ELEMENT_TABLE) },
});

const pathForNewCell = [...tablePath, currentRowPath, currentColPath];
for (let j = 0; j < colPaths.length; j++) {
const cellToInsert =
i === 0 && j === 0
? createEmptyCell(cellElem.children)
: createEmptyCell();

insertElements(editor, cellToInsert, { at: pathForNewCell });
// if row exists, insert into it, otherwise insert row
if (rowEntry) {
const currentColPath = i === 0 ? colPaths[j] : pathForNextRows;
const pathForNewCell = [...tablePath, currentRowPath, currentColPath];

insertElements(editor, cellToInsert, { at: pathForNewCell });
} else {
newRowChildren.push(cellToInsert);
}
}

if (!rowEntry) {
insertElements(
editor,
{
type: getPluginType(editor, ELEMENT_TR),
children: newRowChildren,
},
{ at: _rowPath }
);
}
}
});
Expand Down
10 changes: 5 additions & 5 deletions packages/table/src/merge/useTableMergeState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export const useTableMergeState = () => {
}, [readOnly, selected, editor.selection, selectedTable]);

const canUnmerge =
(collapsed &&
selectedCellEntries &&
selectedCellEntries.length === 1 &&
getColSpan(selectedCellEntries[0][0] as any) > 1) ||
getRowSpan(selectedCellEntries[0][0] as any) > 1;
collapsed &&
selectedCellEntries &&
selectedCellEntries.length === 1 &&
(getColSpan(selectedCellEntries[0][0] as any) > 1 ||
getRowSpan(selectedCellEntries[0][0] as any) > 1);

return { canMerge, canUnmerge };
};

0 comments on commit fad078e

Please sign in to comment.