@@ -378,6 +378,35 @@ function compactLineBreak(state, dispatch) {
378378 return true ;
379379}
380380
381+ function getActiveTableContext ( state ) {
382+ const { $head } = state . selection ;
383+ let tableDepth = - 1 ;
384+ let rowDepth = - 1 ;
385+
386+ for ( let d = $head . depth ; d > 0 ; d -- ) {
387+ const name = $head . node ( d ) . type . name ;
388+ if ( rowDepth < 0 && name === "table_row" ) rowDepth = d ;
389+ if ( name === "table" ) {
390+ tableDepth = d ;
391+ break ;
392+ }
393+ }
394+
395+ if ( tableDepth < 0 || rowDepth < 0 ) return null ;
396+
397+ return {
398+ tableDepth,
399+ rowDepth,
400+ rowIndex : $head . index ( tableDepth ) ,
401+ } ;
402+ }
403+
404+ function addRowBeforeIfAllowed ( state , dispatch ) {
405+ const tableContext = getActiveTableContext ( state ) ;
406+ if ( tableContext ?. rowIndex === 0 ) return false ;
407+ return addRowBefore ( state , dispatch ) ;
408+ }
409+
381410// ── Inline Markdown Input Rules ──
382411const inlineMarkdownRules = inputRules ( { rules : [
383412 // **bold**
@@ -464,7 +493,16 @@ function tableToolbarPlugin() {
464493 return el ;
465494 }
466495 const cmds = { addColumnBefore, addColumnAfter, deleteColumn,
467- addRowBefore, addRowAfter, deleteRow, deleteTable } ;
496+ addRowBefore : addRowBeforeIfAllowed , addRowAfter, deleteRow, deleteTable } ;
497+
498+ function updateButtonStates ( state ) {
499+ if ( ! toolbarEl ) return ;
500+
501+ toolbarEl . querySelectorAll ( "button[data-cmd]" ) . forEach ( ( btn ) => {
502+ const cmd = cmds [ btn . dataset . cmd ] ;
503+ btn . disabled = cmd ? ! cmd ( state ) : true ;
504+ } ) ;
505+ }
468506
469507 function positionForSelection ( view ) {
470508 if ( ! view . hasFocus ( ) ) { hideToolbar ( ) ; return ; }
@@ -482,6 +520,7 @@ function tableToolbarPlugin() {
482520
483521 if ( ! inTbl || ! tblDom ) { hideToolbar ( ) ; return ; }
484522
523+ updateButtonStates ( view . state ) ;
485524 const r = tblDom . getBoundingClientRect ( ) ;
486525 toolbarEl . style . display = "flex" ;
487526 toolbarEl . style . left = r . left + "px" ;
@@ -494,6 +533,7 @@ function tableToolbarPlugin() {
494533 toolbarEl . addEventListener ( "mousedown" , e => {
495534 e . preventDefault ( ) ;
496535 const btn = e . target . closest ( "button[data-cmd]" ) ;
536+ if ( btn ?. disabled ) return ;
497537 if ( btn ) { const c = cmds [ btn . dataset . cmd ] ; if ( c ) c ( editorView . state , editorView . dispatch ) ; }
498538 } ) ;
499539
0 commit comments