Skip to content

Commit

Permalink
fix: trying to add a row or column to an outer table caused it to be …
Browse files Browse the repository at this point in the history
…added into inner one
  • Loading branch information
rusandorx committed Oct 9, 2023
1 parent 198f805 commit d2da60d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
19 changes: 12 additions & 7 deletions src/table-utils/commands/appendColumn.ts
Original file line number Diff line number Diff line change
@@ -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<{
Expand All @@ -20,15 +20,20 @@ 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(
state.doc.resolve(tablePos + parentCell.pos + 1),
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) {
Expand All @@ -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(
Expand Down
16 changes: 10 additions & 6 deletions src/table-utils/commands/appendRow.ts
Original file line number Diff line number Diff line change
@@ -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<{
Expand All @@ -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;
}

Expand All @@ -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))));
}
Expand Down

0 comments on commit d2da60d

Please sign in to comment.