Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trying to add a row or column to an outer table caused it to be added into inner one #139

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading