From d2da60dde53df6bf47e802e5e6edc1afa7aedf1e Mon Sep 17 00:00:00 2001 From: Ruslan Palkin Date: Tue, 10 Oct 2023 00:33:17 +0300 Subject: [PATCH] fix: trying to add a row or column to an outer table caused it to be added into inner one --- src/table-utils/commands/appendColumn.ts | 19 ++++++++++++------- src/table-utils/commands/appendRow.ts | 16 ++++++++++------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/table-utils/commands/appendColumn.ts b/src/table-utils/commands/appendColumn.ts index d3846165..5e4cbde0 100644 --- a/src/table-utils/commands/appendColumn.ts +++ b/src/table-utils/commands/appendColumn.ts @@ -1,7 +1,7 @@ -import {findChildTableCells, findChildTableRows} from '../utils'; +import {findChildTableCells, isTableBodyNode, isTableCellNode} from '../utils'; import {findChildIndex} from '../helpers'; import type {CommandWithAttrs} from '../../core'; -import {findParentNodeClosestToPos} from 'prosemirror-utils'; +import {findChildren, findParentNodeClosestToPos} from 'prosemirror-utils'; import {isTableNode, isTableRowNode} from '..'; export const appendColumn: CommandWithAttrs<{ @@ -20,6 +20,9 @@ export const appendColumn: CommandWithAttrs<{ let parentCell; let parentRow; + const tableBody = findChildren(parentTable, isTableBodyNode, false).pop(); + if (!tableBody) return false; + if (columnNumber !== undefined) { parentCell = findChildTableCells(parentTable)[columnNumber]; parentRow = findParentNodeClosestToPos( @@ -27,8 +30,10 @@ export const appendColumn: CommandWithAttrs<{ isTableRowNode, ); } else { - parentCell = findChildTableCells(parentTable).pop(); - parentRow = findChildTableRows(parentTable).pop(); + parentRow = findChildren(tableBody.node, isTableRowNode, false).pop(); + if (!parentRow) return false; + + parentCell = findChildren(parentRow.node, isTableCellNode, false).pop(); } if (!parentCell || !parentRow || !parentTable) { @@ -42,14 +47,14 @@ export const appendColumn: CommandWithAttrs<{ } if (dispatch) { - const allRows = findChildTableRows(parentTable); + const allRows = findChildren(tableBody.node, isTableRowNode, false); let tr = state.tr; for (const row of allRows) { - const rowCells = findChildTableCells(row.node); + const rowCells = findChildren(row.node, isTableCellNode, false); const cell = rowCells[parentCellIndex]; - let position = tablePos + row.pos + cell.pos + 2; + let position = tablePos + row.pos + cell.pos + 3; position += direction === 'before' ? 0 : cell.node.nodeSize; tr = tr.insert( diff --git a/src/table-utils/commands/appendRow.ts b/src/table-utils/commands/appendRow.ts index 906413d5..3bf84b3f 100644 --- a/src/table-utils/commands/appendRow.ts +++ b/src/table-utils/commands/appendRow.ts @@ -1,6 +1,6 @@ import {Fragment, Node} from 'prosemirror-model'; -import {findParentNodeClosestToPos} from 'prosemirror-utils'; -import {findChildTableBody, findChildTableRows, isTableNode} from '..'; +import {findChildren, findParentNodeClosestToPos} from 'prosemirror-utils'; +import {findChildTableRows, isTableBodyNode, isTableNode, isTableRowNode} from '..'; import type {CommandWithAttrs} from '../../core'; export const appendRow: CommandWithAttrs<{ @@ -15,16 +15,20 @@ export const appendRow: CommandWithAttrs<{ state.doc.resolve(tablePos + 1), isTableNode, )?.node; + if (!tableNode) return false; + + const parentBody = findChildren(tableNode, isTableBodyNode, false).pop(); + if (!parentBody) return false; + let parentRow; if (rowNumber !== undefined) { parentRow = findChildTableRows(tableNode)[rowNumber]; } else { - parentRow = findChildTableRows(tableNode).pop(); + parentRow = findChildren(parentBody.node, isTableRowNode, false).pop(); } - const parentBody = findChildTableBody(tableNode).pop(); - if (!parentRow || !parentBody) { + if (!parentRow) { return false; } @@ -35,7 +39,7 @@ export const appendRow: CommandWithAttrs<{ }); let position = tablePos + parentRow.pos; - position += direction === 'before' ? 1 : parentRow.node.nodeSize; + position += direction === 'before' ? 1 : parentRow.node.nodeSize + 2; dispatch(state.tr.insert(position, parentRow.node.copy(Fragment.from(newCellNodes)))); }