Skip to content

Commit 1927c09

Browse files
committed
Address #13
Update stickyland map when a user closes notebook tabs Signed-off-by: Jay Wang <[email protected]>
1 parent a953da4 commit 1927c09

File tree

6 files changed

+63
-9
lines changed

6 files changed

+63
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,4 @@ dmypy.json
115115
# OSX files
116116
.DS_Store
117117
lite/
118+
.yarn/

.yarnrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodeLinker: node-modules

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
"eslint-config-prettier": "^6.15.0",
6161
"eslint-plugin-prettier": "^3.1.4",
6262
"npm-run-all": "^4.1.5",
63-
"prettier": "^2.1.1"
63+
"prettier": "^2.1.1",
64+
"typescript": "4.3.5"
6465
},
6566
"sideEffects": [
6667
"style/*.css",

src/button.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,58 @@
1+
import { JupyterFrontEnd } from '@jupyterlab/application';
12
import { ToolbarButton } from '@jupyterlab/apputils';
3+
import { IDocumentManager } from '@jupyterlab/docmanager';
24
import { IDisposable, DisposableDelegate } from '@lumino/disposable';
35
import { DocumentRegistry } from '@jupyterlab/docregistry';
4-
import { NotebookPanel, INotebookModel } from '@jupyterlab/notebook';
6+
import { Widget } from '@lumino/widgets';
7+
import {
8+
NotebookPanel,
9+
INotebookModel,
10+
INotebookTracker
11+
} from '@jupyterlab/notebook';
512
import { StickyLand } from './stickyland';
613

714
export class ButtonExtension
815
implements DocumentRegistry.IWidgetExtension<NotebookPanel, INotebookModel>
916
{
1017
// This maps each stickyLand object to a notebook title
11-
stickyLandMap: Map<string, StickyLand> | null;
18+
stickyLandMap: Map<string, StickyLand>;
19+
shell: JupyterFrontEnd.IShell;
20+
notebookTracker: INotebookTracker;
21+
documentManager: IDocumentManager;
1222

13-
constructor() {
14-
this.stickyLandMap = new Map();
23+
constructor(
24+
shell: JupyterFrontEnd.IShell,
25+
notebookTracker: INotebookTracker,
26+
documentManager: IDocumentManager
27+
) {
28+
this.shell = shell;
29+
this.stickyLandMap = new Map<string, StickyLand>();
30+
this.notebookTracker = notebookTracker;
31+
this.documentManager = documentManager;
32+
33+
// Listen to the notebook tracker to update stickyLandMap when a user closes
34+
// a notebook
35+
this.notebookTracker.currentChanged.connect((sender, panel) => {
36+
setTimeout(() => {
37+
const widgetIter = this.shell.widgets();
38+
const openPaths: Set<string> = new Set();
39+
let nextWidget = widgetIter.next();
40+
while (nextWidget !== undefined) {
41+
const context = this.documentManager.contextForWidget(nextWidget);
42+
if (context !== undefined) {
43+
openPaths.add(context.path);
44+
}
45+
nextWidget = widgetIter.next();
46+
}
47+
48+
// Remove stickylands where their associated notebooks are closed
49+
for (const path of this.stickyLandMap.keys()) {
50+
if (!openPaths.has(path)) {
51+
this.stickyLandMap.delete(path);
52+
}
53+
}
54+
}, 500);
55+
});
1556
}
1657

1758
createNew(

src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import { JupyterFrontEnd } from '@jupyterlab/application';
22
import { ICommandPalette } from '@jupyterlab/apputils';
3+
import { IDocumentManager } from '@jupyterlab/docmanager';
34
import { ButtonExtension } from './button';
5+
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
46

57
const plugin = {
68
id: 'jupyterlab_stickyland',
79
autoStart: true,
8-
requires: [ICommandPalette],
9-
activate: function (app: JupyterFrontEnd) {
10+
requires: [INotebookTracker, IDocumentManager],
11+
activate: function (
12+
app: JupyterFrontEnd,
13+
notebookTracker: INotebookTracker,
14+
documentManager: IDocumentManager
15+
) {
1016
console.log('Activating StickyLand.');
11-
app.docRegistry.addWidgetExtension('Notebook', new ButtonExtension());
17+
18+
app.docRegistry.addWidgetExtension(
19+
'Notebook',
20+
new ButtonExtension(app.shell, notebookTracker, documentManager)
21+
);
1222
}
1323
};
1424

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"rootDir": "src",
1818
"strict": true,
1919
"strictNullChecks": true,
20-
"target": "es2017",
20+
"target": "ESNext",
2121
"types": []
2222
},
2323
"include": ["src/*"]

0 commit comments

Comments
 (0)