From 61157c16f0bb29b7d2091506c762f44fcbcab93f Mon Sep 17 00:00:00 2001 From: zbeyens Date: Thu, 23 Nov 2023 14:11:23 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/table/src/merge/deleteColumn.ts | 17 +++++++++----- packages/table/src/merge/deleteRow.ts | 23 +++++++++++++------ packages/table/src/merge/findCellByIndexes.ts | 7 ++++-- packages/table/src/merge/getCellPath.ts | 7 ++++-- .../table/src/merge/getTableGridByRange.ts | 9 +++++--- packages/table/src/merge/insertTableColumn.ts | 10 ++++---- packages/table/src/merge/insertTableRow.ts | 10 ++++---- packages/table/src/merge/mergeTableCells.ts | 9 +++++--- packages/table/src/merge/unmergeTableCells.ts | 15 +++++++++--- 9 files changed, 73 insertions(+), 34 deletions(-) diff --git a/packages/table/src/merge/deleteColumn.ts b/packages/table/src/merge/deleteColumn.ts index 30445372b4..7b466576de 100644 --- a/packages/table/src/merge/deleteColumn.ts +++ b/packages/table/src/merge/deleteColumn.ts @@ -1,5 +1,4 @@ import { - focusEditor, getAboveNode, getPluginOptions, getPluginType, @@ -28,7 +27,10 @@ export const deleteTableMergeColumn = ( match: { type: getPluginType(editor, ELEMENT_TABLE) }, }) ) { - const options = getPluginOptions(editor, ELEMENT_TABLE); + const { _cellIndices: cellIndices } = getPluginOptions( + editor, + ELEMENT_TABLE + ); const tableEntry = getAboveNode(editor, { match: { type: getPluginType(editor, ELEMENT_TABLE) }, @@ -44,7 +46,10 @@ export const deleteTableMergeColumn = ( 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; @@ -72,7 +77,7 @@ export const deleteTableMergeColumn = ( 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) { @@ -95,7 +100,7 @@ export const deleteTableMergeColumn = ( const curCell = cur as TTableCellElement; const { col: curColIndex, row: curColRowIndex } = getCellIndices( - options, + cellIndices!, curCell )!; const curColSpan = getColSpan(curCell); @@ -141,7 +146,7 @@ export const deleteTableMergeColumn = ( affectedCells.forEach((cur) => { const curCell = cur as TTableCellElement; const { col: curColIndex, row: curRowIndex } = getCellIndices( - options, + cellIndices!, curCell )!; if ( diff --git a/packages/table/src/merge/deleteRow.ts b/packages/table/src/merge/deleteRow.ts index 24027f9d8b..33155bd639 100644 --- a/packages/table/src/merge/deleteRow.ts +++ b/packages/table/src/merge/deleteRow.ts @@ -32,7 +32,10 @@ export const deleteTableMergeRow = ( match: { type: getPluginType(editor, ELEMENT_TABLE) }, }) ) { - const options = getPluginOptions(editor, ELEMENT_TABLE); + const { _cellIndices: cellIndices } = getPluginOptions( + editor, + ELEMENT_TABLE + ); const currentTableItem = getAboveNode(editor, { match: { type: getPluginType(editor, ELEMENT_TABLE) }, @@ -46,7 +49,10 @@ export const deleteTableMergeRow = ( 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; @@ -72,7 +78,7 @@ export const deleteTableMergeRow = ( 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; @@ -100,7 +106,7 @@ export const deleteTableMergeRow = ( moveToNextRowCells.forEach((cur, index) => { const curRowCell = cur as TTableCellElement; const { col: curRowCellColIndex } = getCellIndices( - options, + cellIndices!, curRowCell )!; const curRowCellRowSpan = getRowSpan(curRowCell); @@ -108,7 +114,7 @@ export const deleteTableMergeRow = ( // 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; }); @@ -116,7 +122,7 @@ export const deleteTableMergeRow = ( startingCellIndex ] as TTableCellElement; const { col: startingColIndex } = getCellIndices( - options, + cellIndices!, startingCell )!; @@ -151,7 +157,10 @@ export const deleteTableMergeRow = ( 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)!; diff --git a/packages/table/src/merge/findCellByIndexes.ts b/packages/table/src/merge/findCellByIndexes.ts index ad1bdfa63f..aeb0f352e5 100644 --- a/packages/table/src/merge/findCellByIndexes.ts +++ b/packages/table/src/merge/findCellByIndexes.ts @@ -12,7 +12,10 @@ export const findCellByIndexes = ( searchRowIndex: number, searchColIndex: number ) => { - const options = getPluginOptions(editor, ELEMENT_TABLE); + const { _cellIndices: cellIndices } = getPluginOptions( + editor, + ELEMENT_TABLE + ); const allCells = table.children.flatMap( (current) => current.children @@ -22,7 +25,7 @@ export const findCellByIndexes = ( const cellElement = cell as TTableCellElement; const indices = - getCellIndices(options, cellElement) || + getCellIndices(cellIndices!, cellElement) || computeCellIndices(editor, table, cellElement)!; const { col: _startColIndex, row: _startRowIndex } = indices; diff --git a/packages/table/src/merge/getCellPath.ts b/packages/table/src/merge/getCellPath.ts index bcb29f9a45..63d07325b3 100644 --- a/packages/table/src/merge/getCellPath.ts +++ b/packages/table/src/merge/getCellPath.ts @@ -20,13 +20,16 @@ export const getCellPath = ( curRowIndex: number, curColIndex: number ) => { - const options = getPluginOptions(editor, ELEMENT_TABLE); + const { _cellIndices: cellIndices } = getPluginOptions( + 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]); diff --git a/packages/table/src/merge/getTableGridByRange.ts b/packages/table/src/merge/getTableGridByRange.ts index 7d953d3c66..f832fb170c 100644 --- a/packages/table/src/merge/getTableGridByRange.ts +++ b/packages/table/src/merge/getTableGridByRange.ts @@ -53,7 +53,10 @@ export const getTableMergeGridByRange = ( editor: PlateEditor, { at, format }: GetTableGridByRangeOptions ): GetTableGridReturnType => { - const options = getPluginOptions(editor, ELEMENT_TABLE); + const { _cellIndices: cellIndices } = getPluginOptions( + editor, + ELEMENT_TABLE + ); const startCellEntry = findNode(editor, { at: (at as any).anchor.path, @@ -77,11 +80,11 @@ export const getTableMergeGridByRange = ( 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 ); diff --git a/packages/table/src/merge/insertTableColumn.ts b/packages/table/src/merge/insertTableColumn.ts index edf99f08e8..aa4aad98bc 100644 --- a/packages/table/src/merge/insertTableColumn.ts +++ b/packages/table/src/merge/insertTableColumn.ts @@ -54,7 +54,10 @@ export const insertTableMergeColumn = ( disableSelect?: boolean; } = {} ) => { - const options = getPluginOptions(editor, ELEMENT_TABLE); + const { _cellIndices: cellIndices } = getPluginOptions( + editor, + ELEMENT_TABLE + ); const cellEntry = fromCell ? findNode(editor, { @@ -79,7 +82,7 @@ export const insertTableMergeColumn = ( getPluginOptions(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; @@ -92,7 +95,6 @@ export const insertTableMergeColumn = ( checkingColIndex = cellColIndex + cellColSpan - 1; } - const currentRowIndex = cellPath.at(-2); // recheck it const rowNumber = tableNode.children.length; const firstCol = nextColIndex <= 0; @@ -117,7 +119,7 @@ export const insertTableMergeColumn = ( affectedCells.forEach((cur) => { const curCell = cur as TTableCellElement; const { row: curRowIndex, col: curColIndex } = getCellIndices( - options, + cellIndices!, curCell )!; const curRowSpan = getRowSpan(curCell); diff --git a/packages/table/src/merge/insertTableRow.ts b/packages/table/src/merge/insertTableRow.ts index cf1d7a7d9f..6731e159f6 100644 --- a/packages/table/src/merge/insertTableRow.ts +++ b/packages/table/src/merge/insertTableRow.ts @@ -6,7 +6,6 @@ import { getPluginType, insertElements, PlateEditor, - select, setNodes, Value, withoutNormalizing, @@ -47,7 +46,10 @@ export const insertTableMergeRow = ( disableSelect?: boolean; } = {} ) => { - const options = getPluginOptions(editor, ELEMENT_TABLE); + const { _cellIndices: cellIndices } = getPluginOptions( + editor, + ELEMENT_TABLE + ); const trEntry = fromRow ? findNode(editor, { @@ -80,7 +82,7 @@ export const insertTableMergeRow = ( 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)!; @@ -119,7 +121,7 @@ export const insertTableMergeRow = ( const curCell = cur as TTableCellElement; const { row: curRowIndex, col: curColIndex } = getCellIndices( - options, + cellIndices!, curCell )!; diff --git a/packages/table/src/merge/mergeTableCells.ts b/packages/table/src/merge/mergeTableCells.ts index 2fdd1ff8e2..18b88d0ab5 100644 --- a/packages/table/src/merge/mergeTableCells.ts +++ b/packages/table/src/merge/mergeTableCells.ts @@ -26,7 +26,10 @@ export const mergeTableCells = ( editor: PlateEditor ) => { withoutNormalizing(editor, () => { - const options = getPluginOptions(editor, ELEMENT_TABLE); + const { _cellIndices } = getPluginOptions( + editor, + ELEMENT_TABLE + ); const tableEntry = getBlockAbove(editor, { at: editor.selection?.anchor.path, match: { type: getPluginType(editor, ELEMENT_TABLE) }, @@ -49,13 +52,13 @@ export const mergeTableCells = ( // 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); diff --git a/packages/table/src/merge/unmergeTableCells.ts b/packages/table/src/merge/unmergeTableCells.ts index dd76145071..400e34f21d 100644 --- a/packages/table/src/merge/unmergeTableCells.ts +++ b/packages/table/src/merge/unmergeTableCells.ts @@ -21,7 +21,10 @@ export const unmergeTableCells = ( editor: PlateEditor ) => { withoutNormalizing(editor, () => { - const options = getPluginOptions(editor, ELEMENT_TABLE); + const { _cellIndices: cellIndices } = getPluginOptions( + editor, + ELEMENT_TABLE + ); const cellEntries = getTableGridAbove(editor, { format: 'cell' }); const [[cellElem, path]] = cellEntries; @@ -54,7 +57,10 @@ export const unmergeTableCells = ( // 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; @@ -66,7 +72,10 @@ export const unmergeTableCells = ( 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;