@@ -90,6 +90,51 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
9090 return ! ! findEditorView ( ) ;
9191 } ;
9292
93+ const getActiveEditorInfo = ( ) => {
94+ const currentWidget = app . shell . currentWidget ;
95+
96+ // Check for active notebook
97+ const notebook = tracker . currentWidget ?. content ;
98+ if (
99+ tracker . currentWidget !== null &&
100+ tracker . currentWidget === currentWidget &&
101+ notebook &&
102+ notebook . activeCell
103+ ) {
104+ return {
105+ type : 'notebook' as const ,
106+ widget : currentWidget ,
107+ notebook,
108+ isEnabled : true
109+ } ;
110+ }
111+
112+ // Check for file editor
113+ const fileEditorWidget = currentWidget as any ;
114+ if (
115+ fileEditorWidget &&
116+ fileEditorWidget . content &&
117+ typeof fileEditorWidget . content . editor ?. focus === 'function'
118+ ) {
119+ return {
120+ type : 'fileEditor' as const ,
121+ widget : fileEditorWidget ,
122+ isEnabled : true
123+ } ;
124+ }
125+
126+ return {
127+ type : 'none' as const ,
128+ widget : null ,
129+ isEnabled : false
130+ } ;
131+ } ;
132+
133+ // Simplified isEnabled function using the helper
134+ const isCodeFoldingEnabled = ( ) => {
135+ return getActiveEditorInfo ( ) . isEnabled ;
136+ } ;
137+
93138 app . commands . addCommand ( CommandIDs . deleteLine , {
94139 label : trans . __ ( 'Delete the current line' ) ,
95140 describedBy : {
@@ -194,28 +239,17 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
194239 properties : { }
195240 }
196241 } ,
242+ // Updated execute function using the helper
197243 execute : ( ) => {
198- // Try notebook first
199- const notebook = tracker . currentWidget ?. content ;
200- const currentWidget = app . shell . currentWidget ;
201- if (
202- notebook &&
203- notebook . activeCell &&
204- currentWidget &&
205- currentWidget === tracker . currentWidget
206- ) {
207- notebook . mode = 'edit' ;
208- notebook . activeCell . editor ?. focus ( ) ;
209- } else {
210- // Try file editor
211- const fileEditorWidget = currentWidget as any ;
212- if (
213- fileEditorWidget &&
214- fileEditorWidget . content &&
215- typeof fileEditorWidget . content . editor ?. focus === 'function'
216- ) {
217- fileEditorWidget . content . editor . focus ( ) ;
244+ const editorInfo = getActiveEditorInfo ( ) ;
245+
246+ if ( editorInfo . type === 'notebook' ) {
247+ editorInfo . notebook . mode = 'edit' ;
248+ if ( editorInfo . notebook . activeCell ) {
249+ editorInfo . notebook . activeCell . editor ?. focus ( ) ;
218250 }
251+ } else if ( editorInfo . type === 'fileEditor' ) {
252+ editorInfo . widget . content . editor . focus ( ) ;
219253 }
220254 const view = findEditorView ( ) ;
221255 if ( ! view ) {
@@ -228,7 +262,8 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
228262 if ( range ) {
229263 foldCode ( view ) ;
230264 }
231- }
265+ } ,
266+ isEnabled : isCodeFoldingEnabled
232267 } ) ;
233268
234269 app . commands . addCommand ( CommandIDs . unfoldCurrent , {
@@ -240,26 +275,15 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
240275 }
241276 } ,
242277 execute : ( ) => {
243- const notebook = tracker . currentWidget ?. content ;
244- const currentWidget = app . shell . currentWidget ;
245- if (
246- notebook &&
247- notebook . activeCell &&
248- currentWidget &&
249- currentWidget === tracker . currentWidget
250- ) {
251- notebook . mode = 'edit' ;
252- notebook . activeCell . editor ?. focus ( ) ;
253- } else {
254- // Try file editor
255- const fileEditorWidget = currentWidget as any ;
256- if (
257- fileEditorWidget &&
258- fileEditorWidget . content &&
259- typeof fileEditorWidget . content . editor ?. focus === 'function'
260- ) {
261- fileEditorWidget . content . editor . focus ( ) ;
278+ const editorInfo = getActiveEditorInfo ( ) ;
279+
280+ if ( editorInfo . type === 'notebook' ) {
281+ editorInfo . notebook . mode = 'edit' ;
282+ if ( editorInfo . notebook . activeCell ) {
283+ editorInfo . notebook . activeCell . editor ?. focus ( ) ;
262284 }
285+ } else if ( editorInfo . type === 'fileEditor' ) {
286+ editorInfo . widget . content . editor . focus ( ) ;
263287 }
264288 const view = findEditorView ( ) ;
265289 if ( ! view ) {
@@ -272,7 +296,8 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
272296 if ( range ) {
273297 unfoldCode ( view ) ;
274298 }
275- }
299+ } ,
300+ isEnabled : isCodeFoldingEnabled
276301 } ) ;
277302
278303 app . commands . addCommand ( CommandIDs . foldSubregions , {
@@ -284,26 +309,15 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
284309 }
285310 } ,
286311 execute : ( ) => {
287- const notebook = tracker . currentWidget ?. content ;
288- const currentWidget = app . shell . currentWidget ;
289- if (
290- notebook &&
291- notebook . activeCell &&
292- currentWidget &&
293- currentWidget === tracker . currentWidget
294- ) {
295- notebook . mode = 'edit' ;
296- notebook . activeCell . editor ?. focus ( ) ;
297- } else {
298- // Try file editor
299- const fileEditorWidget = currentWidget as any ;
300- if (
301- fileEditorWidget &&
302- fileEditorWidget . content &&
303- typeof fileEditorWidget . content . editor ?. focus === 'function'
304- ) {
305- fileEditorWidget . content . editor . focus ( ) ;
312+ const editorInfo = getActiveEditorInfo ( ) ;
313+
314+ if ( editorInfo . type === 'notebook' ) {
315+ editorInfo . notebook . mode = 'edit' ;
316+ if ( editorInfo . notebook . activeCell ) {
317+ editorInfo . notebook . activeCell . editor ?. focus ( ) ;
306318 }
319+ } else if ( editorInfo . type === 'fileEditor' ) {
320+ editorInfo . widget . content . editor . focus ( ) ;
307321 }
308322 const view = findEditorView ( ) ;
309323 if ( ! view ) {
@@ -352,7 +366,8 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
352366 } catch ( e ) {
353367 // Silent fail
354368 }
355- }
369+ } ,
370+ isEnabled : isCodeFoldingEnabled
356371 } ) ;
357372
358373 app . commands . addCommand ( CommandIDs . unfoldSubregions , {
@@ -364,26 +379,15 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
364379 }
365380 } ,
366381 execute : ( ) => {
367- const notebook = tracker . currentWidget ?. content ;
368- const currentWidget = app . shell . currentWidget ;
369- if (
370- notebook &&
371- notebook . activeCell &&
372- currentWidget &&
373- currentWidget === tracker . currentWidget
374- ) {
375- notebook . mode = 'edit' ;
376- notebook . activeCell . editor ?. focus ( ) ;
377- } else {
378- // Try file editor
379- const fileEditorWidget = currentWidget as any ;
380- if (
381- fileEditorWidget &&
382- fileEditorWidget . content &&
383- typeof fileEditorWidget . content . editor ?. focus === 'function'
384- ) {
385- fileEditorWidget . content . editor . focus ( ) ;
382+ const editorInfo = getActiveEditorInfo ( ) ;
383+
384+ if ( editorInfo . type === 'notebook' ) {
385+ editorInfo . notebook . mode = 'edit' ;
386+ if ( editorInfo . notebook . activeCell ) {
387+ editorInfo . notebook . activeCell . editor ?. focus ( ) ;
386388 }
389+ } else if ( editorInfo . type === 'fileEditor' ) {
390+ editorInfo . widget . content . editor . focus ( ) ;
387391 }
388392 const view = findEditorView ( ) ;
389393 if ( ! view ) {
@@ -413,7 +417,8 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
413417 } catch ( e ) {
414418 // Silent fail
415419 }
416- }
420+ } ,
421+ isEnabled : isCodeFoldingEnabled
417422 } ) ;
418423
419424 app . commands . addCommand ( CommandIDs . foldAll , {
@@ -425,26 +430,15 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
425430 }
426431 } ,
427432 execute : ( ) => {
428- const notebook = tracker . currentWidget ?. content ;
429- const currentWidget = app . shell . currentWidget ;
430- if (
431- notebook &&
432- notebook . activeCell &&
433- currentWidget &&
434- currentWidget === tracker . currentWidget
435- ) {
436- notebook . mode = 'edit' ;
437- notebook . activeCell . editor ?. focus ( ) ;
438- } else {
439- // Try file editor
440- const fileEditorWidget = currentWidget as any ;
441- if (
442- fileEditorWidget &&
443- fileEditorWidget . content &&
444- typeof fileEditorWidget . content . editor ?. focus === 'function'
445- ) {
446- fileEditorWidget . content . editor . focus ( ) ;
433+ const editorInfo = getActiveEditorInfo ( ) ;
434+
435+ if ( editorInfo . type === 'notebook' ) {
436+ editorInfo . notebook . mode = 'edit' ;
437+ if ( editorInfo . notebook . activeCell ) {
438+ editorInfo . notebook . activeCell . editor ?. focus ( ) ;
447439 }
440+ } else if ( editorInfo . type === 'fileEditor' ) {
441+ editorInfo . widget . content . editor . focus ( ) ;
448442 }
449443 const view = findEditorView ( ) ;
450444 if ( ! view ) {
@@ -455,7 +449,8 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
455449 } catch ( e ) {
456450 // Silent fail
457451 }
458- }
452+ } ,
453+ isEnabled : isCodeFoldingEnabled
459454 } ) ;
460455
461456 app . commands . addCommand ( CommandIDs . unfoldAll , {
@@ -467,26 +462,15 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
467462 }
468463 } ,
469464 execute : ( ) => {
470- const notebook = tracker . currentWidget ?. content ;
471- const currentWidget = app . shell . currentWidget ;
472- if (
473- notebook &&
474- notebook . activeCell &&
475- currentWidget &&
476- currentWidget === tracker . currentWidget
477- ) {
478- notebook . mode = 'edit' ;
479- notebook . activeCell . editor ?. focus ( ) ;
480- } else {
481- // Try file editor
482- const fileEditorWidget = currentWidget as any ;
483- if (
484- fileEditorWidget &&
485- fileEditorWidget . content &&
486- typeof fileEditorWidget . content . editor ?. focus === 'function'
487- ) {
488- fileEditorWidget . content . editor . focus ( ) ;
465+ const editorInfo = getActiveEditorInfo ( ) ;
466+
467+ if ( editorInfo . type === 'notebook' ) {
468+ editorInfo . notebook . mode = 'edit' ;
469+ if ( editorInfo . notebook . activeCell ) {
470+ editorInfo . notebook . activeCell . editor ?. focus ( ) ;
489471 }
472+ } else if ( editorInfo . type === 'fileEditor' ) {
473+ editorInfo . widget . content . editor . focus ( ) ;
490474 }
491475 const view = findEditorView ( ) ;
492476 if ( ! view ) {
@@ -497,7 +481,8 @@ export const commandsPlugin: JupyterFrontEndPlugin<void> = {
497481 } catch ( e ) {
498482 // Silent fail
499483 }
500- }
484+ } ,
485+ isEnabled : isCodeFoldingEnabled
501486 } ) ;
502487
503488 if ( palette ) {
0 commit comments