Skip to content

Commit

Permalink
Store selected kernel and toolbox in .jpblockly file
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix616 committed Oct 1, 2024
1 parent a821459 commit 10aa25e
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions packages/blockly/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
} from '@jupyterlab/docregistry';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { runIcon } from '@jupyterlab/ui-components';
import { showErrorMessage } from "@jupyterlab/apputils";

import { PartialJSONObject } from "@lumino/coreutils";
import { SplitPanel } from '@lumino/widgets';
import { Signal } from '@lumino/signaling';

Expand Down Expand Up @@ -82,6 +84,7 @@ export namespace BlocklyEditor {
*/
export class BlocklyPanel extends SplitPanel {
private _context: DocumentRegistry.IContext<DocumentModel>;
private _manager: BlocklyManager;
private _rendermime: IRenderMimeRegistry;

/**
Expand All @@ -105,6 +108,7 @@ export class BlocklyPanel extends SplitPanel {
});
this.addClass('jp-BlocklyPanel');
this._context = context;
this._manager = manager;
this._rendermime = rendermime;

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

private _load(): void {
// Loading the content of the document into the workspace
const content = this._context.model.toJSON() as any as Blockly.Workspace;
(this.layout as BlocklyLayout).workspace = content;
const fileContent = this._context.model.toJSON();
const fileFormat = fileContent['format'];
// Check if format is set or if we have legacy content
if (fileFormat === undefined && fileContent['blocks']) {
// Load legacy content
(this.layout as BlocklyLayout).workspace = fileContent as any as Blockly.Workspace;
} else if (fileFormat === 2) {
// Load the content from the "workspace" key
(this.layout as BlocklyLayout).workspace = fileContent['workspace'] as any as Blockly.Workspace;
const metadata = fileContent['metadata'];
if (metadata) {
if (metadata['toolbox']) {
const toolbox = metadata['toolbox'];
if (this._manager.listToolboxes().find(value => value.value === toolbox)) {
this._manager.setToolbox(metadata['toolbox']);
} else {
// Unknown toolbox
showErrorMessage(`Unknown toolbox`,
`The toolbox '` + toolbox + `' is not available. Using default toolbox.`
);
}
}
if (metadata['kernel'] && metadata['kernel'] !== 'No kernel') {
const kernel = metadata['kernel'];
if (this._manager.listKernels().find(value => value.value === kernel)) {
this._manager.selectKernel(metadata['kernel']);
} else {
// Unknown kernel
console.warn(`Unknown kernel in blockly file: ` + kernel);
}
}
}
} else {
// Unsupported format
showErrorMessage(`Unsupported file format`,
`The file format '` + fileFormat + `' is not supported by the Blockly editor.`
);
}
}

private _onSave(
Expand All @@ -150,7 +189,15 @@ export class BlocklyPanel extends SplitPanel {
): void {
if (state === 'started') {
const workspace = (this.layout as BlocklyLayout).workspace;
this._context.model.fromJSON(workspace as any);
const fileContent: PartialJSONObject = {
format: 2,
workspace: workspace as any,
metadata: {
toolbox: this._manager.getToolbox(),
kernel: this._manager.kernel
}
};
this._context.model.fromJSON(fileContent);
}
}
}

0 comments on commit 10aa25e

Please sign in to comment.