From c55ccf39e73acf516f6a4124cc0fede5fffa2b18 Mon Sep 17 00:00:00 2001 From: Dzmitry Tamashevich Date: Fri, 10 Nov 2023 13:22:17 +0300 Subject: [PATCH] improve selection, indices computation --- .../src/lib/plate/demo/values/tableValue.tsx | 1 + .../useTableCellElementState.ts | 61 ++++---- .../TableElement/useTableElement.ts | 139 +----------------- packages/table/src/createTablePlugin.ts | 3 +- .../table/src/queries/findCellByIndexes.ts | 41 ++++++ packages/table/src/queries/getCellIdices.ts | 105 +++++++++++++ packages/table/src/queries/getColSpan.ts | 10 ++ packages/table/src/queries/getIndices.ts | 13 ++ .../table/src/queries/getIndicesWithSpans.ts | 19 +++ packages/table/src/queries/getRowSpan.ts | 10 ++ .../table/src/queries/getTableGridByRange.ts | 109 +++++++++++--- packages/table/src/stores/tableStore.ts | 7 - packages/table/src/types.ts | 10 ++ packages/table/src/utils/getEmptyCellNode.ts | 3 +- packages/table/src/utils/getEmptyRowNode.ts | 3 +- 15 files changed, 336 insertions(+), 198 deletions(-) create mode 100644 packages/table/src/queries/findCellByIndexes.ts create mode 100644 packages/table/src/queries/getCellIdices.ts create mode 100644 packages/table/src/queries/getColSpan.ts create mode 100644 packages/table/src/queries/getIndices.ts create mode 100644 packages/table/src/queries/getIndicesWithSpans.ts create mode 100644 packages/table/src/queries/getRowSpan.ts diff --git a/apps/www/src/lib/plate/demo/values/tableValue.tsx b/apps/www/src/lib/plate/demo/values/tableValue.tsx index 30417572f0..0e79843495 100644 --- a/apps/www/src/lib/plate/demo/values/tableValue.tsx +++ b/apps/www/src/lib/plate/demo/values/tableValue.tsx @@ -167,6 +167,7 @@ export const tableValue: any = ( to design structured layouts. {createMergedCellsTable()} + {createSpanningTable()} {/* {createTable()} */} {/* This table is an example of rendering a table spanning multiple columns: diff --git a/packages/table/src/components/TableCellElement/useTableCellElementState.ts b/packages/table/src/components/TableCellElement/useTableCellElementState.ts index ff45a0de15..875503e60a 100644 --- a/packages/table/src/components/TableCellElement/useTableCellElementState.ts +++ b/packages/table/src/components/TableCellElement/useTableCellElementState.ts @@ -1,11 +1,19 @@ -import { useEffect } from 'react'; -import { useEditorRef, useElement } from '@udecode/plate-common'; +import { useEffect, useMemo } from 'react'; +import { + getPluginOptions, + useEditorRef, + useElement, +} from '@udecode/plate-common'; import { useReadOnly } from 'slate-react'; import { ELEMENT_TABLE, ELEMENT_TR } from '../../createTablePlugin'; +import { getCellIndices } from '../../queries/getCellIdices'; +import { getColSpan } from '../../queries/getColSpan'; +import { getRowSpan } from '../../queries/getRowSpan'; import { getTableColumnIndex, getTableRowIndex } from '../../queries/index'; import { useTableStore } from '../../stores/tableStore'; import { + TablePlugin, TTableCellElement, TTableElement, TTableRowElement, @@ -29,24 +37,6 @@ export type TableCellElementState = { colSpan: number; }; -/** - * Returns the colspan attribute of the table cell element. - * @default 1 if undefined. - */ -export const getColSpan = (cellElem: TTableCellElement) => { - const attrColSpan = Number(cellElem.attributes?.colspan); - return cellElem.colSpan || attrColSpan || 1; -}; - -/** - * Returns the rowspan attribute of the table cell element. - * @default 1 if undefined - */ -export const getRowSpan = (cellElem: TTableCellElement) => { - const attrRowSpan = Number(cellElem.attributes?.rowspan); - return cellElem.rowSpan || attrRowSpan || 1; -}; - export const useTableCellElementState = ({ ignoreReadOnly, }: { @@ -61,22 +51,39 @@ export const useTableCellElementState = ({ const colSpan = getColSpan(cellElement); const rowSpan = getRowSpan(cellElement); - const defaultColIndex = getTableColumnIndex(editor, cellElement); - const defaultRowIndex = getTableRowIndex(editor, cellElement); - const readOnly = useReadOnly(); const isCellSelected = useIsCellSelected(cellElement); const hoveredColIndex = useTableStore().get.hoveredColIndex(); const selectedCells = useTableStore().get.selectedCells(); - const cellAttributes = useTableStore().get.cellAttributes(); const tableElement = useElement(ELEMENT_TABLE); const rowElement = useElement(ELEMENT_TR); - const x = cellAttributes.get(cellElement); - const colIndex = x?.col ?? defaultColIndex; - const rowIndex = x?.row ?? defaultRowIndex; + const { _cellIndices } = useMemo( + () => getPluginOptions(editor as any, ELEMENT_TABLE), + [editor] + ); + + let x: { col: number; row: number }; + const fromWeakMap = _cellIndices.get(cellElement); + if (fromWeakMap) { + x = fromWeakMap; + console.log('from weak map', x, 'cellElement', cellElement); + } else { + const x1 = getCellIndices(editor, tableElement, cellElement); + if (x1) { + x = x1; + console.log('computed', x, 'cellElement', cellElement); + } else { + const defaultColIndex = getTableColumnIndex(editor, cellElement); + const defaultRowIndex = getTableRowIndex(editor, cellElement); + x = { col: defaultColIndex, row: defaultRowIndex }; + console.log('get default', x, 'cellElement', cellElement); + } + } + const colIndex = x.col; + const rowIndex = x.row; const endingRowIndex = rowIndex + rowSpan - 1; const endingColIndex = colIndex + colSpan - 1; diff --git a/packages/table/src/components/TableElement/useTableElement.ts b/packages/table/src/components/TableElement/useTableElement.ts index 9cea6ba0f4..e0d5b76765 100644 --- a/packages/table/src/components/TableElement/useTableElement.ts +++ b/packages/table/src/components/TableElement/useTableElement.ts @@ -1,27 +1,13 @@ -import { useEffect } from 'react'; import { collapseSelection, - findNodePath, - getNode, - getParentNode, getPluginOptions, - PlateEditor, useEditorRef, useElement, } from '@udecode/plate-common'; -import { Path } from 'slate'; import { ELEMENT_TABLE } from '../../createTablePlugin'; -import { - TableStoreCellAttributes, - useTableStore, -} from '../../stores/tableStore'; -import { - TablePlugin, - TTableCellElement, - TTableElement, - TTableRowElement, -} from '../../types'; +import { useTableStore } from '../../stores/tableStore'; +import { TablePlugin, TTableElement } from '../../types'; import { useSelectedCells } from './useSelectedCells'; import { useTableColSizes } from './useTableColSizes'; @@ -50,13 +36,6 @@ export const useTableElementState = ({ const element = useElement(); const selectedCells = useTableStore().get.selectedCells(); const marginLeftOverride = useTableStore().get.marginLeftOverride(); - const setCellAttributes = useTableStore().set.cellAttributes(); - - useEffect(() => { - const newAttributes = new WeakMap() as TableStoreCellAttributes; - calculateCellIndexes(editor, element, newAttributes); - setCellAttributes(newAttributes); - }, [editor, element, setCellAttributes]); const marginLeft = disableMarginLeft ? 0 @@ -102,117 +81,3 @@ export const useTableElement = () => { }, }; }; - -// const cellAttributes = new WeakMap< -// TTableCellElement, -// { row: number; col: number } -// >(); - -function getCellIndices( - cellAttributes: TableStoreCellAttributes, - tableEl: TTableElement, - tablePath: Path, - cellPath: Path -) { - const tableNodes = tableEl.children; - - let rowIndex = -1; - let colIndex = -1; - - for (let r = 0; r < tableNodes.length; r++) { - const row = tableNodes[r] as TTableRowElement; - - let cIndex = 0; - for (let c = 0; c < row.children.length; c++) { - const cell = row.children[c] as TTableCellElement; - const curCellPath = [r, c]; - if (Path.equals(curCellPath, cellPath)) { - colIndex = cIndex; - rowIndex = r; - break; - } - cIndex += cell.colSpan || 1; // consider 0 and undefined as 1 - } - } - - tableNodes.slice(0, rowIndex).forEach((pR, _rowIndex) => { - const prevRow = pR as TTableRowElement; - prevRow.children.forEach((pC) => { - const prevCell = pC as TTableCellElement; - const prevIndices = cellAttributes.get(prevCell); - if (prevIndices) { - const { col: prevColIndex } = prevIndices; - if ( - // colIndex affects - prevColIndex <= colIndex && - // rowSpan affects - prevCell.rowSpan && - prevCell.rowSpan > 1 && - rowIndex - _rowIndex < prevCell.rowSpan - ) { - colIndex += prevCell.colSpan || 1; - } - } - }); - }); - - if (rowIndex === -1 || colIndex === -1) { - console.log('Invalid cell location.'); - return null; - } - - return { row: rowIndex, col: colIndex }; -} - -const calculateCellIndexes = ( - editor: PlateEditor, - tableNode: TTableElement, - cellAttributes: TableStoreCellAttributes -) => { - // (Place the `getCellIndices()` function from the previous response here) - - // Initialize an array to store the indices of each cell - const cellIndicesArray = []; - - const tablePath = findNodePath(editor, tableNode)!; - - // Iterate through the table rows - for (let r = 0; r < tableNode.children.length; r++) { - const row = tableNode.children[r] as TTableRowElement; - const rowIndicesArray = []; - - // Iterate through the row cells - for (let c = 0; c < row.children.length; c++) { - const cell = row.children[c] as TTableCellElement; - - // Get cell indices and store them in the row's array - const cellPath = [r, c]; - // console.log( - // 'searching for', - // cell.children.map((m) => { - // return (m as any).children[0].text; - // }), - // tableNode, - // tablePath, - // cellPath - // ); - - const indices = getCellIndices( - cellAttributes, - tableNode, - tablePath, - cellPath - ); - if (indices) { - cellAttributes.set(cell, indices); - } - rowIndicesArray.push(indices); - } - - // Push the rowIndicesArray to the cellIndicesArray - cellIndicesArray.push(rowIndicesArray); - } - - console.log('cellIndicesArray', cellIndicesArray); - return cellIndicesArray; -}; diff --git a/packages/table/src/createTablePlugin.ts b/packages/table/src/createTablePlugin.ts index 533f30ef13..7ecb072bb6 100644 --- a/packages/table/src/createTablePlugin.ts +++ b/packages/table/src/createTablePlugin.ts @@ -2,7 +2,7 @@ import { createPluginFactory } from '@udecode/plate-common'; import { onKeyDownTable } from './onKeyDownTable'; import { insertTableColumn, insertTableRow } from './transforms/index'; -import { TablePlugin } from './types'; +import { TablePlugin, TableStoreCellAttributes } from './types'; import { withTable } from './withTable'; export const ELEMENT_TABLE = 'table'; @@ -36,6 +36,7 @@ export const createTablePlugin = createPluginFactory({ }); }, minColumnWidth: 48, + _cellIndices: new WeakMap() as TableStoreCellAttributes, }, withOverrides: withTable, plugins: [ diff --git a/packages/table/src/queries/findCellByIndexes.ts b/packages/table/src/queries/findCellByIndexes.ts new file mode 100644 index 0000000000..150c724af0 --- /dev/null +++ b/packages/table/src/queries/findCellByIndexes.ts @@ -0,0 +1,41 @@ +import { Value } from '@udecode/plate-common'; + +import { TablePlugin, TTableCellElement, TTableElement } from '../types'; +import { getIndices } from './getIndices'; +import { getIndicesWithSpans } from './getIndicesWithSpans'; + +export const findCellByIndexes1 = ( + options: TablePlugin, + table: TTableElement, + searchRowIndex: number, + searchColIndex: number +) => { + const allCells = table.children.flatMap( + (current) => current.children + ) as TTableCellElement[]; + + // console.log('searching for', searchRowIndex, searchColIndex); + const foundCell = allCells.find((cell) => { + const cellElement = cell as TTableCellElement; + + const { _startColIndex, _startRowIndex } = getIndices(options, cellElement); + const { _endRowIndex, _endColIndex } = getIndicesWithSpans( + options, + cellElement + ); + + // console.log('current', colIndex, endColIndex, rowIndex, endRowIndex); + if ( + searchColIndex >= _startColIndex && + searchColIndex <= _endColIndex && + searchRowIndex >= _startRowIndex && + searchRowIndex <= _endRowIndex + ) { + return true; + } + + return false; + }); + + return foundCell; +}; diff --git a/packages/table/src/queries/getCellIdices.ts b/packages/table/src/queries/getCellIdices.ts new file mode 100644 index 0000000000..e51356274b --- /dev/null +++ b/packages/table/src/queries/getCellIdices.ts @@ -0,0 +1,105 @@ +import { getPluginOptions, PlateEditor } from '@udecode/plate-common'; + +import { ELEMENT_TABLE } from '../createTablePlugin'; +import { + TablePlugin, + TTableCellElement, + TTableElement, + TTableRowElement, +} from '../types'; + +export function getCellIndices( + editor: PlateEditor, + tableEl: TTableElement, + cellEl: TTableCellElement +) { + const options = getPluginOptions(editor, ELEMENT_TABLE); + + const tableNodes = tableEl.children; + + let rowIndex = -1; + let colIndex = -1; + + for (let r = 0; r < tableNodes.length; r++) { + const row = tableNodes[r] as TTableRowElement; + + let cIndex = 0; + for (let c = 0; c < row.children.length; c++) { + const cell = row.children[c] as TTableCellElement; + if (cellEl === cell) { + colIndex = cIndex; + rowIndex = r; + break; + } + cIndex += cell.colSpan || 1; // consider 0 and undefined as 1 + } + } + + tableNodes.slice(0, rowIndex).forEach((pR, _rowIndex) => { + const prevRow = pR as TTableRowElement; + prevRow.children.forEach((pC) => { + const prevCell = pC as TTableCellElement; + const prevIndices = options?._cellIndices?.get(prevCell); + if (prevIndices) { + const { col: prevColIndex } = prevIndices; + if ( + // colIndex affects + prevColIndex <= colIndex && + // rowSpan affects + prevCell.rowSpan && + prevCell.rowSpan > 1 && + rowIndex - _rowIndex < prevCell.rowSpan + ) { + colIndex += prevCell.colSpan || 1; + } + } + }); + }); + + if (rowIndex === -1 || colIndex === -1) { + console.log('Invalid cell location.'); + return null; + } + + const indices = { row: rowIndex, col: colIndex }; + + options?._cellIndices?.set(cellEl, indices); + + return indices; +} + +// const calculateCellIndexes = ( +// editor: PlateEditor, +// tableNode: TTableElement +// ) => { +// // Initialize an array to store the indices of each cell +// const cellIndicesArray = []; + +// const tablePath = findNodePath(editor, tableNode)!; + +// // Iterate through the table rows +// for (let r = 0; r < tableNode.children.length; r++) { +// const row = tableNode.children[r] as TTableRowElement; +// const rowIndicesArray = []; + +// // Iterate through the row cells +// for (let c = 0; c < row.children.length; c++) { +// const cell = row.children[c] as TTableCellElement; + +// // Get cell indices and store them in the row's array +// const cellPath = [r, c]; + +// const indices = getCellIndices(editor, tableNode, cell); +// if (indices) { +// cellIndices.set(cell, indices); +// } +// rowIndicesArray.push(indices); +// } + +// // Push the rowIndicesArray to the cellIndicesArray +// cellIndicesArray.push(rowIndicesArray); +// console.log('calculated array', cellIndicesArray); +// } + +// return cellIndicesArray; +// }; diff --git a/packages/table/src/queries/getColSpan.ts b/packages/table/src/queries/getColSpan.ts new file mode 100644 index 0000000000..2df1070a04 --- /dev/null +++ b/packages/table/src/queries/getColSpan.ts @@ -0,0 +1,10 @@ +import { TTableCellElement } from '../types'; + +/** + * Returns the colspan attribute of the table cell element. + * @default 1 if undefined. + */ +export const getColSpan = (cellElem: TTableCellElement) => { + const attrColSpan = Number(cellElem.attributes?.colspan); + return cellElem.colSpan || attrColSpan || 1; +}; diff --git a/packages/table/src/queries/getIndices.ts b/packages/table/src/queries/getIndices.ts new file mode 100644 index 0000000000..c5b6a60bbb --- /dev/null +++ b/packages/table/src/queries/getIndices.ts @@ -0,0 +1,13 @@ +import { Value } from '@udecode/plate-common'; + +import { TablePlugin, TTableCellElement } from '../types'; + +export const getIndices = ( + options: TablePlugin, + startCell: TTableCellElement +) => { + const { col: _startColIndex, row: _startRowIndex } = + options._cellIndices.get(startCell)!; + + return { _startColIndex, _startRowIndex }; +}; diff --git a/packages/table/src/queries/getIndicesWithSpans.ts b/packages/table/src/queries/getIndicesWithSpans.ts new file mode 100644 index 0000000000..c07cf73b62 --- /dev/null +++ b/packages/table/src/queries/getIndicesWithSpans.ts @@ -0,0 +1,19 @@ +import { Value } from '@udecode/plate-common'; + +import { TablePlugin, TTableCellElement } from '../types'; +import { getColSpan } from './getColSpan'; +import { getRowSpan } from './getRowSpan'; + +export const getIndicesWithSpans = ( + options: TablePlugin, + endCell: TTableCellElement +) => { + const { col: __endColIndex, row: __endRowIndex } = + options._cellIndices.get(endCell)!; + + // TODO: improve typing + const _endRowIndex = __endRowIndex + getRowSpan(endCell) - 1; + const _endColIndex = __endColIndex + getColSpan(endCell) - 1; + + return { _endRowIndex, _endColIndex }; +}; diff --git a/packages/table/src/queries/getRowSpan.ts b/packages/table/src/queries/getRowSpan.ts new file mode 100644 index 0000000000..6e83104836 --- /dev/null +++ b/packages/table/src/queries/getRowSpan.ts @@ -0,0 +1,10 @@ +import { TTableCellElement } from '../types'; + +/** + * Returns the rowspan attribute of the table cell element. + * @default 1 if undefined + */ +export const getRowSpan = (cellElem: TTableCellElement) => { + const attrRowSpan = Number(cellElem.attributes?.rowspan); + return cellElem.rowSpan || attrRowSpan || 1; +}; diff --git a/packages/table/src/queries/getTableGridByRange.ts b/packages/table/src/queries/getTableGridByRange.ts index 84caada168..a0f9fceb9a 100644 --- a/packages/table/src/queries/getTableGridByRange.ts +++ b/packages/table/src/queries/getTableGridByRange.ts @@ -1,5 +1,8 @@ import { - getNode, + findNode, + findNodePath, + getPluginOptions, + getPluginType, PlateEditor, TElement, TElementEntry, @@ -7,8 +10,29 @@ import { } from '@udecode/plate-common'; import { Range } from 'slate'; -import { TTableElement } from '../types'; +import { ELEMENT_TABLE } from '../createTablePlugin'; +import { + TablePlugin, + TTableCellElement, + TTableElement, + TTableRowElement, +} from '../types'; +import { getCellTypes } from '../utils'; import { getEmptyTableNode } from '../utils/getEmptyTableNode'; +import { findCellByIndexes1 } from './findCellByIndexes'; +import { getIndices } from './getIndices'; +import { getIndicesWithSpans } from './getIndicesWithSpans'; + +export type FormatType = 'table' | 'cell' | 'all'; + +export interface TableGridEntries { + tableEntries: TElementEntry[]; + cellEntries: TElementEntry[]; +} + +export type GetTableGridReturnType = T extends 'all' + ? TableGridEntries + : TElementEntry[]; export interface GetTableGridByRangeOptions { at: Range; @@ -28,21 +52,37 @@ export const getTableGridByRange = ( editor: PlateEditor, { at, format = 'table' }: GetTableGridByRangeOptions ): TElementEntry[] => { - const startCellPath = at.anchor.path; - const endCellPath = at.focus.path; + const options = getPluginOptions(editor, ELEMENT_TABLE); + + const startCellEntry = findNode(editor, { + at: (at as any).anchor.path, + match: { type: getCellTypes(editor) }, + })!; // TODO: improve typing + const endCellEntry = findNode(editor, { + at: (at as any).focus.path, + match: { type: getCellTypes(editor) }, + })!; + + const startCell = startCellEntry[0] as TTableCellElement; + const endCell = endCellEntry[0] as TTableCellElement; - const _startRowIndex = startCellPath.at(-2)!; - const _endRowIndex = endCellPath.at(-2)!; - const _startColIndex = startCellPath.at(-1)!; - const _endColIndex = endCellPath.at(-1)!; + const startCellPath = (at as any).anchor.path; + const tablePath = startCellPath.slice(0, -2); + + const tableEntry = findNode(editor, { + at: tablePath, + match: { type: getPluginType(editor, ELEMENT_TABLE) }, + })!; // TODO: improve typing + const realTable = tableEntry[0] as TTableElement; + + const { _startColIndex, _startRowIndex } = getIndices(options, startCell); + const { _endRowIndex, _endColIndex } = getIndicesWithSpans(options, endCell); const startRowIndex = Math.min(_startRowIndex, _endRowIndex); const endRowIndex = Math.max(_startRowIndex, _endRowIndex); const startColIndex = Math.min(_startColIndex, _endColIndex); const endColIndex = Math.max(_startColIndex, _endColIndex); - const tablePath = startCellPath.slice(0, -2); - const relativeRowIndex = endRowIndex - startRowIndex; const relativeColIndex = endColIndex - startColIndex; @@ -52,37 +92,58 @@ export const getTableGridByRange = ( newCellChildren: [], }); - let rowIndex = startRowIndex; - let colIndex = startColIndex; - const cellEntries: TElementEntry[] = []; + const cellsSet = new Set(); + let rowIndex = startRowIndex; + let colIndex = startColIndex; while (true) { - const cellPath = tablePath.concat([rowIndex, colIndex]); - - const cell = getNode(editor, cellPath); - if (!cell) break; + const cell = findCellByIndexes1(options, realTable, rowIndex, colIndex); + if (!cell) { + break; + } - const rows = table.children[rowIndex - startRowIndex] - .children as TElement[]; + const cellPath = findNodePath(editor, cell)!; + const path = cellPath.join(''); + if (!cellsSet.has(path)) { + cellsSet.add(path); - rows[colIndex - startColIndex] = cell; + const rows = table.children[rowIndex - startRowIndex] + .children as TElement[]; + rows[colIndex - startColIndex] = cell; - cellEntries.push([cell, cellPath]); + cellEntries.push([cell, cellPath]); + } if (colIndex + 1 <= endColIndex) { - colIndex += 1; + colIndex = colIndex + 1; } else if (rowIndex + 1 <= endRowIndex) { colIndex = startColIndex; - rowIndex += 1; + rowIndex = rowIndex + 1; } else { break; } } - if (format === 'cell') { + const formatType = (format as string) || 'table'; + + if (formatType === 'cell') { return cellEntries; } + // 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; + }); + + console.log('return entries', [[table, tablePath]], cellEntries); + return [[table, tablePath]]; }; diff --git a/packages/table/src/stores/tableStore.ts b/packages/table/src/stores/tableStore.ts index 676bf5fcee..91842d9a93 100644 --- a/packages/table/src/stores/tableStore.ts +++ b/packages/table/src/stores/tableStore.ts @@ -2,20 +2,13 @@ import { useCallback } from 'react'; import { createAtomStore, TElement } from '@udecode/plate-common'; import { ELEMENT_TABLE } from '../createTablePlugin'; -import { TTableCellElement } from '../types'; export type TableStoreSizeOverrides = Map; -export type TableStoreCellAttributes = Map< - TTableCellElement, - { row: number; col: number } ->; - export const { tableStore, useTableStore } = createAtomStore( { colSizeOverrides: new Map() as TableStoreSizeOverrides, rowSizeOverrides: new Map() as TableStoreSizeOverrides, - cellAttributes: new WeakMap() as TableStoreCellAttributes, marginLeftOverride: null as number | null, hoveredColIndex: null as number | null, selectedCells: null as TElement[] | null, diff --git a/packages/table/src/types.ts b/packages/table/src/types.ts index 1f21d5b7c8..173462c7e5 100644 --- a/packages/table/src/types.ts +++ b/packages/table/src/types.ts @@ -58,8 +58,18 @@ export interface TablePlugin { * @default 48 */ minColumnWidth?: number; + + /** + * For internal use. Keeps track of cell indices + */ + _cellIndices: TableStoreCellAttributes; } +export type TableStoreCellAttributes = Map< + TTableCellElement, + { row: number; col: number } +>; + export interface BorderStyle { // https://docx.js.org/api/enums/BorderStyle.html style?: string; diff --git a/packages/table/src/utils/getEmptyCellNode.ts b/packages/table/src/utils/getEmptyCellNode.ts index d9b87b5b5f..98c84984bb 100644 --- a/packages/table/src/utils/getEmptyCellNode.ts +++ b/packages/table/src/utils/getEmptyCellNode.ts @@ -3,7 +3,8 @@ import { getPluginType, PlateEditor, Value } from '@udecode/plate-common'; import { ELEMENT_TD, ELEMENT_TH } from '../createTablePlugin'; import { TablePlugin } from '../types'; -export interface GetEmptyCellNodeOptions extends TablePlugin { +export interface GetEmptyCellNodeOptions + extends Omit { /** * Header cell */ diff --git a/packages/table/src/utils/getEmptyRowNode.ts b/packages/table/src/utils/getEmptyRowNode.ts index 38682507a0..063434e34e 100644 --- a/packages/table/src/utils/getEmptyRowNode.ts +++ b/packages/table/src/utils/getEmptyRowNode.ts @@ -3,7 +3,8 @@ import { getPluginType, PlateEditor, Value } from '@udecode/plate-common'; import { ELEMENT_TR } from '../createTablePlugin'; import { getEmptyCellNode, GetEmptyCellNodeOptions } from './getEmptyCellNode'; -export interface GetEmptyRowNodeOptions extends GetEmptyCellNodeOptions { +export interface GetEmptyRowNodeOptions + extends Omit { colCount?: number; }