@@ -181,21 +181,29 @@ const plugin: JupyterFrontEndPlugin<void> = {
181181 }
182182 } ;
183183
184+ // Add cell numbering to a notebook panel (guard against duplicates)
185+ const addCellNumbering = ( notebookPanel : NotebookPanel ) : void => {
186+ if ( cellNumberingCleanups . has ( notebookPanel ) ) {
187+ return ;
188+ }
189+ const cleanup = setupCellNumbering ( notebookPanel ) ;
190+ if ( cleanup ) {
191+ cellNumberingCleanups . set ( notebookPanel , cleanup ) ;
192+ // Also cleanup when notebook is disposed
193+ notebookPanel . disposed . connect ( ( ) => {
194+ cellNumberingCleanups . delete ( notebookPanel ) ;
195+ } ) ;
196+ }
197+ } ;
198+
184199 // Add buttons and cell numbering to all notebooks (for a specific theme)
185200 const addButtonsToAllNotebooks = ( themeName : string ) : void => {
186201 notebookTracker . forEach ( widget => {
187202 addRunCellButton ( widget ) ;
188203 // Enable line numbers if needed
189204 void enableLineNumbersIfNeeded ( app , widget ) ;
190205 // Setup cell numbering
191- const cleanup = setupCellNumbering ( widget ) ;
192- if ( cleanup ) {
193- cellNumberingCleanups . set ( widget , cleanup ) ;
194- // Also cleanup when notebook is disposed
195- widget . disposed . connect ( ( ) => {
196- cellNumberingCleanups . delete ( widget ) ;
197- } ) ;
198- }
206+ addCellNumbering ( widget ) ;
199207 } ) ;
200208
201209 // Connect to new notebooks
@@ -211,14 +219,7 @@ const plugin: JupyterFrontEndPlugin<void> = {
211219 // Enable line numbers if needed
212220 void enableLineNumbersIfNeeded ( app , widget ) ;
213221 // Setup cell numbering
214- const cleanup = setupCellNumbering ( widget ) ;
215- if ( cleanup ) {
216- cellNumberingCleanups . set ( widget , cleanup ) ;
217- // Also cleanup when notebook is disposed
218- widget . disposed . connect ( ( ) => {
219- cellNumberingCleanups . delete ( widget ) ;
220- } ) ;
221- }
222+ addCellNumbering ( widget ) ;
222223 }
223224 } , 100 ) ;
224225 } ;
0 commit comments