Commit 68e13496 authored by Mark Ghiorso's avatar Mark Ghiorso

Restore panel state

parent db689741
import {
JupyterLab, JupyterLabPlugin
JupyterLab, JupyterLabPlugin, ILayoutRestorer
} from '@jupyterlab/application';
import {
ICommandPalette
ICommandPalette, InstanceTracker
} from '@jupyterlab/apputils';
import {
......@@ -14,6 +14,10 @@ import {
Message
} from '@phosphor/messaging';
import {
JSONExt
} from '@phosphor/coreutils';
import {
ServerConnection
} from '@jupyterlab/services';
......@@ -74,23 +78,33 @@ class XkcdWidget extends Widget {
/**
* Activate the xckd widget extension.
*/
function activate(app: JupyterLab, palette: ICommandPalette) {
function activate(app: JupyterLab, palette: ICommandPalette, restorer: ILayoutRestorer) {
console.log('JupyterLab extension jupyterlab_xkcd is activated!');
// Create a single widget
let widget: XkcdWidget = new XkcdWidget();
// Declare a widget variable
let widget: XkcdWidget;
// Add an application command
const command: string = 'xkcd:open';
app.commands.addCommand(command, {
label: 'Random xkcd comic',
execute: () => {
if (!widget) {
// Create a new widget if one does not exist
widget = new XkcdWidget();
widget.update();
}
if (!tracker.has(widget)) {
// Track the state of the widget for later restoration
tracker.add(widget);
}
if (!widget.isAttached) {
// Attach the widget to the main area if it's not there
app.shell.addToMainArea(widget);
} else {
// Refresh the comic in the widget
widget.update();
}
// Refresh the comic in the widget
widget.update();
// Activate the widget
app.shell.activateById(widget.id);
}
......@@ -98,6 +112,14 @@ function activate(app: JupyterLab, palette: ICommandPalette) {
// Add the command to the palette.
palette.addItem({ command, category: 'Tutorial' });
// Track and restore the widget state
let tracker = new InstanceTracker<Widget>({ namespace: 'xkcd' });
restorer.restore(tracker, {
command,
args: () => JSONExt.emptyObject,
name: () => 'xkcd'
});
};
/**
......@@ -106,7 +128,7 @@ function activate(app: JupyterLab, palette: ICommandPalette) {
const extension: JupyterLabPlugin<void> = {
id: 'jupyterlab_gitlab',
autoStart: true,
requires: [ICommandPalette],
requires: [ICommandPalette, ILayoutRestorer],
activate: activate
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment