@@ -17,6 +17,8 @@ import { Cell, CodeCell } from '@jupyterlab/cells';
1717
1818import { PageConfig , Text , Time , URLExt } from '@jupyterlab/coreutils' ;
1919
20+ import { IDebugger , IDebuggerSidebar } from '@jupyterlab/debugger' ;
21+
2022import { IDocumentManager } from '@jupyterlab/docmanager' ;
2123
2224import { DocumentRegistry } from '@jupyterlab/docregistry' ;
@@ -31,10 +33,14 @@ import {
3133
3234import { ISettingRegistry } from '@jupyterlab/settingregistry' ;
3335
36+ import { ITableOfContentsTracker } from '@jupyterlab/toc' ;
37+
3438import { ITranslator , nullTranslator } from '@jupyterlab/translation' ;
3539
3640import { INotebookShell } from '@jupyter-notebook/application' ;
3741
42+ import { find } from '@lumino/algorithm' ;
43+
3844import { Poll } from '@lumino/polling' ;
3945
4046import { Widget } from '@lumino/widgets' ;
@@ -749,6 +755,122 @@ const editNotebookMetadata: JupyterFrontEndPlugin<void> = {
749755 } ,
750756} ;
751757
758+ /**
759+ * A plugin to replace the menu item activating the TOC panel, to allow toggling it.
760+ */
761+ const overrideMenuItems : JupyterFrontEndPlugin < void > = {
762+ id : '@jupyter-notebook/notebook-extension:menu-override' ,
763+ description : 'A plugin to override some menu items' ,
764+ autoStart : true ,
765+ optional : [
766+ IDebuggerSidebar ,
767+ IMainMenu ,
768+ INotebookShell ,
769+ ITableOfContentsTracker ,
770+ ITranslator ,
771+ ] ,
772+ activate : (
773+ app : JupyterFrontEnd ,
774+ debuggerSidebar : IDebugger . ISidebar | null ,
775+ mainMenu : IMainMenu | null ,
776+ shell : INotebookShell | null ,
777+ tocTracker : ITableOfContentsTracker | null ,
778+ translator : ITranslator | null
779+ ) => {
780+ if ( ! mainMenu || ! shell ) {
781+ return ;
782+ }
783+ const trans = ( translator ?? nullTranslator ) . load ( 'notebook' ) ;
784+ const { commands } = app ;
785+
786+ if ( tocTracker ) {
787+ const TOC_PANEL_ID = 'table-of-contents' ;
788+ commands . addCommand ( 'toc:toggle-panel' , {
789+ label : trans . __ ( 'Table of Contents' ) ,
790+ isToggleable : true ,
791+ isToggled : ( ) => {
792+ const area = shell . getWidgetArea ( TOC_PANEL_ID ) ;
793+ if ( ! area ) {
794+ return false ;
795+ }
796+ const widget = find (
797+ shell . widgets ( area as INotebookShell . Area ) ,
798+ ( w ) => w . id === TOC_PANEL_ID
799+ ) ;
800+ if ( ! widget ) {
801+ return false ;
802+ }
803+ return shell . isSidePanelVisible ( area ) && widget . isVisible ;
804+ } ,
805+ execute : ( ) => {
806+ const area = shell . getWidgetArea ( TOC_PANEL_ID ) ;
807+ if ( ! area ) {
808+ return ;
809+ }
810+ const widget = find (
811+ shell . widgets ( area as INotebookShell . Area ) ,
812+ ( w ) => w . id === TOC_PANEL_ID
813+ ) ;
814+ if ( shell . isSidePanelVisible ( area ) && widget ?. isVisible ) {
815+ shell . collapse ( area ) ;
816+ } else {
817+ shell . activateById ( TOC_PANEL_ID ) ;
818+ }
819+ } ,
820+ describedBy : {
821+ args : {
822+ type : 'object' ,
823+ properties : { } ,
824+ } ,
825+ } ,
826+ } ) ;
827+ }
828+
829+ if ( debuggerSidebar ) {
830+ const DEBUGGER_PANEL_ID = 'jp-debugger-sidebar' ;
831+ commands . addCommand ( 'debugger:toggle-panel' , {
832+ label : trans . __ ( 'Debugger Panel' ) ,
833+ isToggleable : true ,
834+ isToggled : ( ) => {
835+ const area = shell . getWidgetArea ( DEBUGGER_PANEL_ID ) ;
836+ if ( ! area ) {
837+ return false ;
838+ }
839+ const widget = find (
840+ shell . widgets ( area as INotebookShell . Area ) ,
841+ ( w ) => w . id === DEBUGGER_PANEL_ID
842+ ) ;
843+ if ( ! widget ) {
844+ return false ;
845+ }
846+ return shell . isSidePanelVisible ( area ) && widget . isVisible ;
847+ } ,
848+ execute : ( ) => {
849+ const area = shell . getWidgetArea ( DEBUGGER_PANEL_ID ) ;
850+ if ( ! area ) {
851+ return ;
852+ }
853+ const widget = find (
854+ shell . widgets ( area as INotebookShell . Area ) ,
855+ ( w ) => w . id === DEBUGGER_PANEL_ID
856+ ) ;
857+ if ( shell . isSidePanelVisible ( area ) && widget ?. isVisible ) {
858+ shell . collapse ( area ) ;
859+ } else {
860+ shell . activateById ( DEBUGGER_PANEL_ID ) ;
861+ }
862+ } ,
863+ describedBy : {
864+ args : {
865+ type : 'object' ,
866+ properties : { } ,
867+ } ,
868+ } ,
869+ } ) ;
870+ }
871+ } ,
872+ } ;
873+
752874/**
753875 * Export the plugins as default.
754876 */
@@ -761,6 +883,7 @@ const plugins: JupyterFrontEndPlugin<any>[] = [
761883 kernelLogo ,
762884 kernelStatus ,
763885 notebookToolsWidget ,
886+ overrideMenuItems ,
764887 scrollOutput ,
765888 tabIcon ,
766889 trusted ,
0 commit comments