Skip to content

Commit 36e33ce

Browse files
committed
add layout restorer to keep widget open after refresh
1 parent 38763ac commit 36e33ce

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

src/components/BrowserCookie.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
3+
const BrowserCookie = () => {
4+
const loadCookies = () => {
5+
console.log('Loading cookies from leetcode.com');
6+
};
7+
8+
return (
9+
<div>
10+
<button onClick={loadCookies}>Load</button>
11+
</div>
12+
);
13+
};
14+
15+
export default BrowserCookie;

src/index.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
JupyterFrontEnd,
3-
JupyterFrontEndPlugin
3+
JupyterFrontEndPlugin,
4+
ILayoutRestorer
45
} from '@jupyterlab/application';
6+
import { ICommandPalette, WidgetTracker } from '@jupyterlab/apputils';
57

68
import LeetCodeWidget from './widget';
79

@@ -12,13 +14,42 @@ const plugin: JupyterFrontEndPlugin<void> = {
1214
id: 'jupyterlab-leetcode:plugin',
1315
description: 'Integrate LeetCode into beloved Jupyter.',
1416
autoStart: true,
15-
requires: [],
16-
optional: [],
17-
activate: (app: JupyterFrontEnd) => {
17+
requires: [ICommandPalette],
18+
optional: [ILayoutRestorer],
19+
activate: (
20+
app: JupyterFrontEnd,
21+
palette: ICommandPalette,
22+
restorer: ILayoutRestorer | null
23+
) => {
1824
console.log('JupyterLab extension jupyterlab-leetcode is activated!');
1925

20-
const leetcodeWidget: LeetCodeWidget = new LeetCodeWidget();
21-
app.shell.add(leetcodeWidget, 'right', { rank: 599 });
26+
let leetcodeWidget: LeetCodeWidget;
27+
28+
const command = 'leetcode-widget:open';
29+
app.commands.addCommand(command, {
30+
label: 'Open LeetCode Widget',
31+
execute: () => {
32+
if (!leetcodeWidget || leetcodeWidget.isDisposed) {
33+
leetcodeWidget = new LeetCodeWidget();
34+
}
35+
if (!tracker.has(leetcodeWidget)) {
36+
tracker.add(leetcodeWidget);
37+
}
38+
if (!leetcodeWidget.isAttached) {
39+
app.shell.add(leetcodeWidget, 'right', { rank: 599 });
40+
}
41+
app.shell.activateById(leetcodeWidget.id);
42+
}
43+
});
44+
45+
palette.addItem({ command, category: 'LeetCode' });
46+
const tracker = new WidgetTracker<LeetCodeWidget>({
47+
namespace: 'leetcode-widget'
48+
});
49+
if (restorer) {
50+
console.log('Restoring layout for jupyterlab-leetcode plugin');
51+
restorer.restore(tracker, { command, name: () => 'leetcode' });
52+
}
2253
}
2354
};
2455

src/widget.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { ReactWidget } from '@jupyterlab/ui-components';
22
import React from 'react';
3+
import BrowserCookie from './components/BrowserCookie';
34

45
const LeetCodeComponent = (): JSX.Element => {
56
return (
67
<div>
78
<p>Welcome to JupyterLab LeetCode Widget.</p>
9+
<BrowserCookie />
810
</div>
911
);
1012
};

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
"target": "ES2018",
2222
"types": ["jest"]
2323
},
24-
"include": ["src/*"]
24+
"include": ["src/**/*"]
2525
}

0 commit comments

Comments
 (0)