@@ -62,54 +62,81 @@ const extension: JupyterFrontEndPlugin<void> = {
6262 launcher : ILauncher ,
6363 languageRegistry : IEditorLanguageRegistry
6464 ) => {
65- console . log ( 'JupyterLab extension URDF is activated!' ) ;
66- const { commands } = app ;
65+ const { commands, shell } = app ;
6766
68- // Tracker
69- const namespace = 'jupyterlab-urdf' ;
70- const tracker = new WidgetTracker < URDFWidget > ( { namespace } ) ;
67+ // --- track whether we've already done the split, and the two anchor IDs ---
68+ let splitDone = false ;
69+ let leftEditorRefId : string | null = null ;
70+ let rightViewerRefId : string | null = null ;
7171
72- // State restoration: reopen document if it was open previously
72+ const tracker = new WidgetTracker < URDFWidget > ( {
73+ namespace : 'jupyterlab-urdf'
74+ } ) ;
7375 if ( restorer ) {
7476 restorer . restore ( tracker , {
7577 command : 'docmanager:open' ,
76- args : widget => ( { path : widget . context . path , factory : FACTORY } ) ,
77- name : widget => {
78- console . debug ( '[Restorer]: Re-opening' , widget . context . path ) ;
79- return widget . context . path ;
80- }
78+ args : w => ( { path : w . context . path , factory : FACTORY } ) ,
79+ name : w => w . context . path
8180 } ) ;
8281 }
8382
84- // Create widget factory so that manager knows about widget
83+ // Function to check if any URDF widgets are currently open
84+ const checkAndResetSplitState = ( ) => {
85+ if ( tracker . size === 0 ) {
86+ // No URDF widgets left, reset split state
87+ splitDone = false ;
88+ leftEditorRefId = null ;
89+ rightViewerRefId = null ;
90+ }
91+ } ;
92+
8593 const widgetFactory = new URDFWidgetFactory ( {
8694 name : FACTORY ,
8795 fileTypes : [ 'urdf' ] ,
8896 defaultFor : [ 'urdf' ]
8997 } ) ;
9098
91- // Add widget to tracker when created
92- widgetFactory . widgetCreated . connect ( ( sender , widget ) => {
99+ widgetFactory . widgetCreated . connect ( async ( sender , widget ) => {
93100 widget . title . icon = urdf_icon ;
94101 widget . title . iconClass = 'jp-URDFIcon' ;
95-
96- // Notify instance tracker if restore data needs to be updated
97- widget . context . pathChanged . connect ( ( ) => {
98- tracker . save ( widget ) ;
99- } ) ;
102+ widget . context . pathChanged . connect ( ( ) => tracker . save ( widget ) ) ;
100103 tracker . add ( widget ) ;
101104
102- // Open editor alongside viewer
103- commands . execute ( 'docmanager:open' , {
104- path : widget . context . path ,
105- factory : 'Editor'
105+ // Add dispose listener to reset split state when all widgets are closed
106+ widget . disposed . connect ( ( ) => {
107+ checkAndResetSplitState ( ) ;
106108 } ) ;
109+
110+ if ( ! splitDone ) {
111+ // First file: split out the editor to the left of this viewer
112+ const editor = await commands . execute ( 'docmanager:open' , {
113+ path : widget . context . path ,
114+ factory : 'Editor' ,
115+ options : { mode : 'split-left' , ref : widget . id }
116+ } ) ;
117+ splitDone = true ;
118+ leftEditorRefId = editor . id ;
119+ rightViewerRefId = widget . id ;
120+ } else {
121+ // Subsequent viewers → tab them into the _right_ panel
122+ if ( rightViewerRefId ) {
123+ shell . add ( widget , 'main' , {
124+ mode : 'tab-after' ,
125+ ref : rightViewerRefId
126+ } ) ;
127+ }
128+ // And open each new editor as a tab in the _left_ panel
129+ if ( leftEditorRefId ) {
130+ await commands . execute ( 'docmanager:open' , {
131+ path : widget . context . path ,
132+ factory : 'Editor' ,
133+ options : { mode : 'tab-after' , ref : leftEditorRefId }
134+ } ) ;
135+ }
136+ }
107137 } ) ;
108138
109- // Register widget and model factories
110139 app . docRegistry . addWidgetFactory ( widgetFactory ) ;
111-
112- // Register file type
113140 app . docRegistry . addFileType ( {
114141 name : 'urdf' ,
115142 displayName : 'URDF' ,
@@ -121,29 +148,22 @@ const extension: JupyterFrontEndPlugin<void> = {
121148 icon : urdf_icon
122149 } ) ;
123150
124- // Add command for creating new urdf (file)
151+ // new‐file command now just fires the viewer; widgetCreated handles the rest
125152 commands . addCommand ( 'urdf:create-new' , {
126153 label : 'Create new URDF' ,
127154 icon : urdf_icon ,
128- iconClass : 'jp-URDFIcon' ,
129155 caption : 'Create a new URDF' ,
130156 execute : async ( ) => {
131157 const cwd = browserFactory . model . path ;
132- const model = await commands . execute ( 'docmanager:new-untitled' , {
158+ const { path } = await commands . execute ( 'docmanager:new-untitled' , {
133159 path : cwd ,
134160 type : 'file' ,
135161 ext : '.urdf'
136162 } ) ;
137-
138163 await commands . execute ( 'docmanager:open' , {
139- path : model . path ,
164+ path,
140165 factory : FACTORY
141166 } ) ;
142-
143- await commands . execute ( 'docmanager:open' , {
144- path : model . path ,
145- factory : 'Editor'
146- } ) ;
147167 }
148168 } ) ;
149169
0 commit comments