Skip to content

Commit 85bf28b

Browse files
committed
Refactor code
1 parent bd78cb9 commit 85bf28b

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

src/index.ts

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -62,53 +62,59 @@ const extension: JupyterFrontEndPlugin<void> = {
6262
launcher: ILauncher,
6363
languageRegistry: IEditorLanguageRegistry
6464
) => {
65+
console.log('JupyterLab extension URDF is activated!');
6566
const { commands, shell } = app;
6667

67-
// --- track whether we've already done the split, and the two anchor IDs ---
68+
// Tracker
69+
const namespace = 'jupyterlab-urdf';
70+
const tracker = new WidgetTracker<URDFWidget>({ namespace });
71+
72+
// Track split state
6873
let splitDone = false;
6974
let leftEditorRefId: string | null = null;
7075
let rightViewerRefId: string | null = null;
7176

72-
const tracker = new WidgetTracker<URDFWidget>({
73-
namespace: 'jupyterlab-urdf'
74-
});
77+
// State restoration: reopen document if it was open previously
7578
if (restorer) {
7679
restorer.restore(tracker, {
7780
command: 'docmanager:open',
78-
args: w => ({ path: w.context.path, factory: FACTORY }),
79-
name: w => w.context.path
81+
args: widget => ({ path: widget.context.path, factory: FACTORY }),
82+
name: widget => {
83+
console.debug('[Restorer]: Re-opening', widget.context.path);
84+
return widget.context.path;
85+
}
8086
});
8187
}
8288

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-
89+
// Create widget factory so that manager knows about widget
9390
const widgetFactory = new URDFWidgetFactory({
9491
name: FACTORY,
9592
fileTypes: ['urdf'],
9693
defaultFor: ['urdf']
9794
});
9895

96+
// Add widget to tracker when created
9997
widgetFactory.widgetCreated.connect(async (sender, widget) => {
10098
widget.title.icon = urdf_icon;
10199
widget.title.iconClass = 'jp-URDFIcon';
102-
widget.context.pathChanged.connect(() => tracker.save(widget));
100+
101+
// Notify instance tracker if restore data needs to be updated
102+
widget.context.pathChanged.connect(() => {
103+
tracker.save(widget);
104+
});
103105
tracker.add(widget);
104106

105-
// Add dispose listener to reset split state when all widgets are closed
107+
// Reset split state when all widgets are closed
106108
widget.disposed.connect(() => {
107-
checkAndResetSplitState();
109+
if (tracker.size === 0) {
110+
splitDone = false;
111+
leftEditorRefId = null;
112+
rightViewerRefId = null;
113+
}
108114
});
109115

116+
// Split layout on first open, then tab into panels
110117
if (!splitDone) {
111-
// First file: split out the editor to the left of this viewer
112118
const editor = await commands.execute('docmanager:open', {
113119
path: widget.context.path,
114120
factory: 'Editor',
@@ -118,14 +124,12 @@ const extension: JupyterFrontEndPlugin<void> = {
118124
leftEditorRefId = editor.id;
119125
rightViewerRefId = widget.id;
120126
} else {
121-
// Subsequent viewers → tab them into the _right_ panel
122127
if (rightViewerRefId) {
123128
shell.add(widget, 'main', {
124129
mode: 'tab-after',
125130
ref: rightViewerRefId
126131
});
127132
}
128-
// And open each new editor as a tab in the _left_ panel
129133
if (leftEditorRefId) {
130134
await commands.execute('docmanager:open', {
131135
path: widget.context.path,
@@ -136,7 +140,10 @@ const extension: JupyterFrontEndPlugin<void> = {
136140
}
137141
});
138142

143+
// Register widget and model factories
139144
app.docRegistry.addWidgetFactory(widgetFactory);
145+
146+
// Register file type
140147
app.docRegistry.addFileType({
141148
name: 'urdf',
142149
displayName: 'URDF',
@@ -148,22 +155,26 @@ const extension: JupyterFrontEndPlugin<void> = {
148155
icon: urdf_icon
149156
});
150157

151-
// new‐file command now just fires the viewer; widgetCreated handles the rest
158+
// Add command for creating new urdf (file)
152159
commands.addCommand('urdf:create-new', {
153160
label: 'Create new URDF',
154161
icon: urdf_icon,
162+
iconClass: 'jp-URDFIcon',
155163
caption: 'Create a new URDF',
156-
execute: async () => {
164+
execute: () => {
157165
const cwd = browserFactory.model.path;
158-
const { path } = await commands.execute('docmanager:new-untitled', {
159-
path: cwd,
160-
type: 'file',
161-
ext: '.urdf'
162-
});
163-
await commands.execute('docmanager:open', {
164-
path,
165-
factory: FACTORY
166-
});
166+
commands
167+
.execute('docmanager:new-untitled', {
168+
path: cwd,
169+
type: 'file',
170+
ext: '.urdf'
171+
})
172+
.then(model =>
173+
commands.execute('docmanager:open', {
174+
path: model.path,
175+
factory: FACTORY
176+
})
177+
);
167178
}
168179
});
169180

0 commit comments

Comments
 (0)