Skip to content

Commit 7137789

Browse files
committed
Avoid saving/restoring document widget
1 parent 1699d62 commit 7137789

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed

packages/application-extension/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
// Distributed under the terms of the Modified BSD License.
33

44
import {
5+
ILabShell,
56
ILabStatus,
67
ILayoutRestorer,
78
IRouter,
89
ITreePathUpdater,
910
JupyterFrontEnd,
1011
JupyterFrontEndPlugin,
1112
JupyterLab,
13+
LayoutRestorer,
1214
} from '@jupyterlab/application';
1315

1416
import {
@@ -54,7 +56,7 @@ import {
5456
SidePanelPalette,
5557
INotebookPathOpener,
5658
defaultNotebookPathOpener,
57-
NotebookLayoutRestorer,
59+
NotebookStateDB,
5860
} from '@jupyter-notebook/application';
5961

6062
import { jupyterIcon } from '@jupyter-notebook/ui-components';
@@ -216,7 +218,7 @@ const layoutRestorer: JupyterFrontEndPlugin<ILayoutRestorer | null> = {
216218
const first = app.started;
217219
const registry = app.commands;
218220

219-
const restorer = new NotebookLayoutRestorer({
221+
const restorer = new LayoutRestorer({
220222
connector: state,
221223
first,
222224
registry,
@@ -228,7 +230,7 @@ const layoutRestorer: JupyterFrontEndPlugin<ILayoutRestorer | null> = {
228230
// promise.
229231
void notebookShell.restoreLayout(restorer).then(() => {
230232
notebookShell.layoutModified.connect(() => {
231-
void restorer.save(notebookShell.saveLayout());
233+
void restorer.save(notebookShell.saveLayout() as ILabShell.ILayout);
232234
});
233235
});
234236
});
@@ -623,7 +625,7 @@ const state: JupyterFrontEndPlugin<IStateDB> = {
623625
const { workspaces } = serviceManager;
624626
const workspace = PageConfig.getOption('notebookPage');
625627
const transform = new PromiseDelegate<StateDB.DataTransform>();
626-
const db = new StateDB({ transform: transform.promise });
628+
const db = new NotebookStateDB({ transform: transform.promise });
627629
const save = new Debouncer(async () => {
628630
const id = workspace;
629631
const metadata = { id };

packages/application/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
export * from './app';
55
export * from './shell';
6-
export * from './layoutrestorer';
76
export * from './panelhandler';
87
export * from './pathopener';
8+
export * from './statedb';
99
export * from './tokens';

packages/application/src/layoutrestorer.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { StateDB } from '@jupyterlab/statedb';
2+
import {
3+
ReadonlyPartialJSONObject,
4+
ReadonlyPartialJSONValue,
5+
} from '@lumino/coreutils';
6+
7+
/**
8+
* The default concrete implementation of a state database.
9+
*/
10+
export class NotebookStateDB extends StateDB<ReadonlyPartialJSONValue> {
11+
constructor(options: StateDB.IOptions<ReadonlyPartialJSONValue> = {}) {
12+
super(options);
13+
this._originalSave = super.save.bind(this);
14+
}
15+
16+
// Override the save method to avoid saving the document widget (in main area).
17+
// NOTE: restoring a document widget open a new tab.
18+
async save(id: string, value: ReadonlyPartialJSONValue): Promise<void> {
19+
const data = (value as ReadonlyPartialJSONObject)[
20+
'data'
21+
] as ReadonlyPartialJSONObject;
22+
23+
// If data.path and data.factory are defined, the widget is a document widget, that
24+
// we don't want to save in the layout restoration.
25+
if (data?.['path'] && data?.['factory']) {
26+
return;
27+
} else {
28+
this._originalSave(id, value);
29+
}
30+
}
31+
32+
private _originalSave: typeof StateDB.prototype.save;
33+
}

0 commit comments

Comments
 (0)