@@ -78,6 +78,16 @@ const extension: JupyterFrontEndPlugin<void> = {
7878 let rightViewerRefId : string | null =
7979 localStorage . getItem ( rightViewerRefKey ) ;
8080
81+ // Reset our “splitDone” flags & remove saved refs
82+ function resetSplitState ( ) {
83+ splitDone = false ;
84+ localStorage . setItem ( splitDoneKey , 'false' ) ;
85+ leftEditorRefId = null ;
86+ rightViewerRefId = null ;
87+ localStorage . removeItem ( leftEditorRefKey ) ;
88+ localStorage . removeItem ( rightViewerRefKey ) ;
89+ }
90+
8191 // State restoration: reopen document if it was open previously
8292 if ( restorer ) {
8393 restorer . restore ( tracker , {
@@ -111,15 +121,21 @@ const extension: JupyterFrontEndPlugin<void> = {
111121 // Reset split state when all widgets are closed
112122 widget . disposed . connect ( ( ) => {
113123 if ( tracker . size === 0 ) {
114- splitDone = false ;
115- localStorage . setItem ( splitDoneKey , 'false' ) ;
116- leftEditorRefId = null ;
117- rightViewerRefId = null ;
118- localStorage . removeItem ( leftEditorRefKey ) ;
119- localStorage . removeItem ( rightViewerRefKey ) ;
124+ resetSplitState ( ) ;
120125 }
121126 } ) ;
122127
128+ // Detect stale editor-ref
129+ if ( splitDone && leftEditorRefId ) {
130+ // look through all widgets in the main area
131+ const allMain = [ ...shell . widgets ( 'main' ) ] ;
132+ const stillHasEditor = allMain . some ( w => w . id === leftEditorRefId ) ;
133+ if ( ! stillHasEditor ) {
134+ // the editor tab was closed ⇒ reset split state
135+ resetSplitState ( ) ;
136+ }
137+ }
138+
123139 // Split layout on first open, then tab into panels
124140 if ( ! splitDone ) {
125141 const editor = await commands . execute ( 'docmanager:open' , {
0 commit comments