Skip to content

Commit 10aa25e

Browse files
committed
Store selected kernel and toolbox in .jpblockly file
1 parent a821459 commit 10aa25e

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

packages/blockly/src/widget.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {
55
} from '@jupyterlab/docregistry';
66
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
77
import { runIcon } from '@jupyterlab/ui-components';
8+
import { showErrorMessage } from "@jupyterlab/apputils";
89

10+
import { PartialJSONObject } from "@lumino/coreutils";
911
import { SplitPanel } from '@lumino/widgets';
1012
import { Signal } from '@lumino/signaling';
1113

@@ -82,6 +84,7 @@ export namespace BlocklyEditor {
8284
*/
8385
export class BlocklyPanel extends SplitPanel {
8486
private _context: DocumentRegistry.IContext<DocumentModel>;
87+
private _manager: BlocklyManager;
8588
private _rendermime: IRenderMimeRegistry;
8689

8790
/**
@@ -105,6 +108,7 @@ export class BlocklyPanel extends SplitPanel {
105108
});
106109
this.addClass('jp-BlocklyPanel');
107110
this._context = context;
111+
this._manager = manager;
108112
this._rendermime = rendermime;
109113

110114
// Load the content of the file when the context is ready
@@ -139,9 +143,44 @@ export class BlocklyPanel extends SplitPanel {
139143
}
140144

141145
private _load(): void {
142-
// Loading the content of the document into the workspace
143-
const content = this._context.model.toJSON() as any as Blockly.Workspace;
144-
(this.layout as BlocklyLayout).workspace = content;
146+
const fileContent = this._context.model.toJSON();
147+
const fileFormat = fileContent['format'];
148+
// Check if format is set or if we have legacy content
149+
if (fileFormat === undefined && fileContent['blocks']) {
150+
// Load legacy content
151+
(this.layout as BlocklyLayout).workspace = fileContent as any as Blockly.Workspace;
152+
} else if (fileFormat === 2) {
153+
// Load the content from the "workspace" key
154+
(this.layout as BlocklyLayout).workspace = fileContent['workspace'] as any as Blockly.Workspace;
155+
const metadata = fileContent['metadata'];
156+
if (metadata) {
157+
if (metadata['toolbox']) {
158+
const toolbox = metadata['toolbox'];
159+
if (this._manager.listToolboxes().find(value => value.value === toolbox)) {
160+
this._manager.setToolbox(metadata['toolbox']);
161+
} else {
162+
// Unknown toolbox
163+
showErrorMessage(`Unknown toolbox`,
164+
`The toolbox '` + toolbox + `' is not available. Using default toolbox.`
165+
);
166+
}
167+
}
168+
if (metadata['kernel'] && metadata['kernel'] !== 'No kernel') {
169+
const kernel = metadata['kernel'];
170+
if (this._manager.listKernels().find(value => value.value === kernel)) {
171+
this._manager.selectKernel(metadata['kernel']);
172+
} else {
173+
// Unknown kernel
174+
console.warn(`Unknown kernel in blockly file: ` + kernel);
175+
}
176+
}
177+
}
178+
} else {
179+
// Unsupported format
180+
showErrorMessage(`Unsupported file format`,
181+
`The file format '` + fileFormat + `' is not supported by the Blockly editor.`
182+
);
183+
}
145184
}
146185

147186
private _onSave(
@@ -150,7 +189,15 @@ export class BlocklyPanel extends SplitPanel {
150189
): void {
151190
if (state === 'started') {
152191
const workspace = (this.layout as BlocklyLayout).workspace;
153-
this._context.model.fromJSON(workspace as any);
192+
const fileContent: PartialJSONObject = {
193+
format: 2,
194+
workspace: workspace as any,
195+
metadata: {
196+
toolbox: this._manager.getToolbox(),
197+
kernel: this._manager.kernel
198+
}
199+
};
200+
this._context.model.fromJSON(fileContent);
154201
}
155202
}
156203
}

0 commit comments

Comments
 (0)