1- import { findChildTableCells , findChildTableRows } from '../utils' ;
1+ import { findChildTableCells , isTableBodyNode , isTableCellNode } from '../utils' ;
22import { findChildIndex } from '../helpers' ;
33import type { CommandWithAttrs } from '../../core' ;
4- import { findParentNodeClosestToPos } from 'prosemirror-utils' ;
4+ import { findChildren , findParentNodeClosestToPos } from 'prosemirror-utils' ;
55import { isTableNode , isTableRowNode } from '..' ;
66
77export const appendColumn : CommandWithAttrs < {
@@ -20,15 +20,20 @@ export const appendColumn: CommandWithAttrs<{
2020 let parentCell ;
2121 let parentRow ;
2222
23+ const tableBody = findChildren ( parentTable , isTableBodyNode , false ) . pop ( ) ;
24+ if ( ! tableBody ) return false ;
25+
2326 if ( columnNumber !== undefined ) {
2427 parentCell = findChildTableCells ( parentTable ) [ columnNumber ] ;
2528 parentRow = findParentNodeClosestToPos (
2629 state . doc . resolve ( tablePos + parentCell . pos + 1 ) ,
2730 isTableRowNode ,
2831 ) ;
2932 } else {
30- parentCell = findChildTableCells ( parentTable ) . pop ( ) ;
31- parentRow = findChildTableRows ( parentTable ) . pop ( ) ;
33+ parentRow = findChildren ( tableBody . node , isTableRowNode , false ) . pop ( ) ;
34+ if ( ! parentRow ) return false ;
35+
36+ parentCell = findChildren ( parentRow . node , isTableCellNode , false ) . pop ( ) ;
3237 }
3338
3439 if ( ! parentCell || ! parentRow || ! parentTable ) {
@@ -42,14 +47,14 @@ export const appendColumn: CommandWithAttrs<{
4247 }
4348
4449 if ( dispatch ) {
45- const allRows = findChildTableRows ( parentTable ) ;
50+ const allRows = findChildren ( tableBody . node , isTableRowNode , false ) ;
4651
4752 let tr = state . tr ;
4853 for ( const row of allRows ) {
49- const rowCells = findChildTableCells ( row . node ) ;
54+ const rowCells = findChildren ( row . node , isTableCellNode , false ) ;
5055 const cell = rowCells [ parentCellIndex ] ;
5156
52- let position = tablePos + row . pos + cell . pos + 2 ;
57+ let position = tablePos + row . pos + cell . pos + 3 ;
5358 position += direction === 'before' ? 0 : cell . node . nodeSize ;
5459
5560 tr = tr . insert (
0 commit comments