@@ -466,6 +466,75 @@ export function deleteColumn(state: EditorState, dispatch?: (tr: Transaction) =>
466466 return true ;
467467}
468468
469+ /**
470+ * 获取当前行的 Markdown 文本
471+ */
472+ export function getCurrentRowMarkdown ( state : EditorState ) : string | null {
473+ const ctx = findTableContext ( state ) ;
474+ if ( ! ctx ) return null ;
475+
476+ const rowNode = ctx . tableNode . child ( ctx . rowIndex ) ;
477+ const cells : string [ ] = [ ] ;
478+
479+ rowNode . forEach ( ( cell ) => {
480+ cells . push ( cell . textContent || "" ) ;
481+ } ) ;
482+
483+ return `| ${ cells . join ( " | " ) } |` ;
484+ }
485+
486+ function parseMarkdownTableRow ( text : string ) : string [ ] | null {
487+ const trimmed = text . trim ( ) ;
488+ if ( ! / ^ \| .* \| $ / u. test ( trimmed ) ) return null ;
489+
490+ const inner = trimmed . slice ( 1 , - 1 ) ;
491+ const cells = inner . split ( "|" ) . map ( ( cell ) => cell . trim ( ) ) ;
492+ return cells ;
493+ }
494+
495+ /**
496+ * 在当前行后插入 Markdown 表格行
497+ */
498+ export function insertMarkdownTableRowAfterCurrent (
499+ state : EditorState ,
500+ rowMarkdown : string ,
501+ dispatch ?: ( tr : Transaction ) => void
502+ ) : boolean {
503+ const ctx = findTableContext ( state ) ;
504+ if ( ! ctx ) return false ;
505+
506+ const cells = parseMarkdownTableRow ( rowMarkdown ) ;
507+ if ( ! cells ) return false ;
508+
509+ const colCount = ctx . tableNode . child ( 0 ) ?. childCount ?? 0 ;
510+ if ( cells . length !== colCount ) return false ;
511+
512+ const { table_row, table_header, table_cell, paragraph } = state . schema . nodes ;
513+ if ( ! table_row || ! table_header || ! table_cell || ! paragraph ) return false ;
514+
515+ const cellType = table_cell || table_header ;
516+ const rowNode = table_row . create (
517+ null ,
518+ cells . map ( ( cellText ) =>
519+ cellType . create (
520+ null ,
521+ cellText ? paragraph . create ( null , state . schema . text ( cellText ) ) : paragraph . create ( )
522+ )
523+ )
524+ ) ;
525+
526+ if ( dispatch ) {
527+ const { $from } = state . selection ;
528+ const rowPos = $from . after ( ctx . rowDepth ) ;
529+ const tr = state . tr . insert ( rowPos , rowNode ) ;
530+ const selectionPos = Math . min ( rowPos + 2 , tr . doc . content . size ) ;
531+ tr . setSelection ( TextSelection . create ( tr . doc , selectionPos ) ) ;
532+ dispatch ( tr . scrollIntoView ( ) ) ;
533+ }
534+
535+ return true ;
536+ }
537+
469538/**
470539 * 插入数学块
471540 */
@@ -526,6 +595,8 @@ export const commands = {
526595 addColumnAtEnd,
527596 deleteRow,
528597 deleteColumn,
598+ getCurrentRowMarkdown,
599+ insertMarkdownTableRowAfterCurrent,
529600 insertMathBlock,
530601 insertContainer,
531602} ;
0 commit comments