Skip to content

Commit 638aef0

Browse files
authored
Merge pull request #66 from hbcarlos/kernel
Kernel controls and split panel
2 parents 3573e98 + 5932009 commit 638aef0

File tree

5 files changed

+208
-31
lines changed

5 files changed

+208
-31
lines changed

packages/blockly-extension/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@jupyterlab/codeeditor": "^3.4",
4949
"@jupyterlab/filebrowser": "^3.4",
5050
"@jupyterlab/launcher": "^3.4",
51+
"@jupyterlab/mainmenu": "^3.4",
5152
"@jupyterlab/rendermime": "^3.4",
5253
"@jupyterlab/settingregistry": "^3.4",
5354
"@jupyterlab/translation": "^3.4",

packages/blockly-extension/src/index.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
1111
import { ILauncher } from '@jupyterlab/launcher';
1212
import { ITranslator } from '@jupyterlab/translation';
1313
import { ISettingRegistry } from '@jupyterlab/settingregistry';
14+
import { IKernelMenu, IMainMenu } from '@jupyterlab/mainmenu';
1415

1516
import { BlocklyEditorFactory } from 'jupyterlab-blockly';
1617
import { IBlocklyRegistry } from 'jupyterlab-blockly';
@@ -48,7 +49,7 @@ const plugin: JupyterFrontEndPlugin<IBlocklyRegistry> = {
4849
ISettingRegistry,
4950
ITranslator
5051
],
51-
optional: [ILauncher, ICommandPalette],
52+
optional: [ILauncher, ICommandPalette, IMainMenu],
5253
provides: IBlocklyRegistry,
5354
activate: (
5455
app: JupyterFrontEnd,
@@ -59,7 +60,8 @@ const plugin: JupyterFrontEndPlugin<IBlocklyRegistry> = {
5960
settings: ISettingRegistry,
6061
translator: ITranslator,
6162
launcher: ILauncher | null,
62-
palette: ICommandPalette | null
63+
palette: ICommandPalette | null,
64+
mainMenu: IMainMenu | null
6365
): IBlocklyRegistry => {
6466
console.log('JupyterLab extension jupyterlab-blocky is activated!');
6567

@@ -80,7 +82,6 @@ const plugin: JupyterFrontEndPlugin<IBlocklyRegistry> = {
8082
}
8183

8284
const { commands } = app;
83-
const command = CommandIDs.createNew;
8485

8586
// Creating the widget factory to register it so the document manager knows about
8687
// our new DocumentWidget
@@ -159,7 +160,7 @@ const plugin: JupyterFrontEndPlugin<IBlocklyRegistry> = {
159160
widgetFactory.registry.setlanguage(language);
160161
});
161162

162-
commands.addCommand(command, {
163+
commands.addCommand(CommandIDs.createNew, {
163164
label: args =>
164165
args['isPalette'] ? 'New Blockly Editor' : 'Blockly Editor',
165166
caption: 'Create a new Blockly Editor',
@@ -188,7 +189,7 @@ const plugin: JupyterFrontEndPlugin<IBlocklyRegistry> = {
188189
// Add the command to the launcher
189190
if (launcher) {
190191
launcher.add({
191-
command,
192+
command: CommandIDs.createNew,
192193
category: 'Other',
193194
rank: 1
194195
});
@@ -197,12 +198,41 @@ const plugin: JupyterFrontEndPlugin<IBlocklyRegistry> = {
197198
// Add the command to the palette
198199
if (palette) {
199200
palette.addItem({
200-
command,
201+
command: CommandIDs.createNew,
201202
args: { isPalette: true },
202203
category: PALETTE_CATEGORY
203204
});
204205
}
205206

207+
// Add the command to the main menu
208+
if (mainMenu) {
209+
mainMenu.kernelMenu.kernelUsers.add({
210+
tracker,
211+
interruptKernel: current => {
212+
const kernel = current.context.sessionContext.session?.kernel;
213+
if (kernel) {
214+
return kernel.interrupt();
215+
}
216+
return Promise.resolve(void 0);
217+
},
218+
reconnectToKernel: current => {
219+
const kernel = current.context.sessionContext.session?.kernel;
220+
if (kernel) {
221+
return kernel.reconnect();
222+
}
223+
return Promise.resolve(void 0);
224+
},
225+
restartKernel: current => {
226+
const kernel = current.context.sessionContext.session?.kernel;
227+
if (kernel) {
228+
return kernel.restart();
229+
}
230+
return Promise.resolve(void 0);
231+
},
232+
shutdownKernel: current => current.context.sessionContext.shutdown()
233+
} as IKernelMenu.IKernelUser<BlocklyEditor>);
234+
}
235+
206236
return widgetFactory.registry;
207237
}
208238
};

packages/blockly/src/layout.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CodeCell, CodeCellModel } from '@jupyterlab/cells';
44

55
import { Message } from '@lumino/messaging';
66
import { PartialJSONValue } from '@lumino/coreutils';
7-
import { PanelLayout, Widget } from '@lumino/widgets';
7+
import { SplitLayout, SplitPanel, Widget } from '@lumino/widgets';
88
import { IIterator, ArrayIterator } from '@lumino/algorithm';
99
import { Signal } from '@lumino/signaling';
1010

@@ -16,8 +16,8 @@ import { THEME } from './utils';
1616
/**
1717
* A blockly layout to host the Blockly editor.
1818
*/
19-
export class BlocklyLayout extends PanelLayout {
20-
private _host: HTMLElement;
19+
export class BlocklyLayout extends SplitLayout {
20+
private _host: Widget;
2121
private _manager: BlocklyManager;
2222
private _workspace: Blockly.WorkspaceSvg;
2323
private _sessionContext: ISessionContext;
@@ -32,13 +32,13 @@ export class BlocklyLayout extends PanelLayout {
3232
sessionContext: ISessionContext,
3333
rendermime: IRenderMimeRegistry
3434
) {
35-
super();
35+
super({ renderer: SplitPanel.defaultRenderer, orientation: 'vertical' });
3636
this._manager = manager;
3737
this._sessionContext = sessionContext;
3838

3939
// Creating the container for the Blockly editor
4040
// and the output area to render the execution replies.
41-
this._host = document.createElement('div');
41+
this._host = new Widget();
4242

4343
// Creating a CodeCell widget to render the code and
4444
// outputs from the execution reply.
@@ -83,7 +83,8 @@ export class BlocklyLayout extends PanelLayout {
8383
init(): void {
8484
super.init();
8585
// Add the blockly container into the DOM
86-
this.addWidget(new Widget({ node: this._host }));
86+
this.addWidget(this._host);
87+
this.addWidget(this._cell);
8788
}
8889

8990
/**
@@ -141,8 +142,6 @@ export class BlocklyLayout extends PanelLayout {
141142
const code =
142143
extra_init + this._manager.generator.workspaceToCode(this._workspace);
143144
this._cell.model.sharedModel.setSource(code);
144-
this.addWidget(this._cell);
145-
this._resizeWorkspace();
146145

147146
// Execute the code using the kernel, by using a static method from the
148147
// same class to make an execution request.
@@ -165,49 +164,49 @@ export class BlocklyLayout extends PanelLayout {
165164
* Handle `update-request` messages sent to the widget.
166165
*/
167166
protected onUpdateRequest(msg: Message): void {
167+
super.onUpdateRequest(msg);
168168
this._resizeWorkspace();
169169
}
170170

171171
/**
172172
* Handle `resize-request` messages sent to the widget.
173173
*/
174-
protected onResize(msg: Message): void {
174+
protected onResize(msg: Widget.ResizeMessage): void {
175+
super.onResize(msg);
175176
this._resizeWorkspace();
176177
}
177178

178179
/**
179180
* Handle `fit-request` messages sent to the widget.
180181
*/
181182
protected onFitRequest(msg: Message): void {
183+
super.onFitRequest(msg);
182184
this._resizeWorkspace();
183185
}
184186

185187
/**
186188
* Handle `after-attach` messages sent to the widget.
187189
*/
188190
protected onAfterAttach(msg: Message): void {
191+
super.onAfterAttach(msg);
189192
//inject Blockly with appropiate JupyterLab theme.
190-
this._workspace = Blockly.inject(this._host, {
193+
this._workspace = Blockly.inject(this._host.node, {
191194
toolbox: this._manager.toolbox,
192195
theme: THEME
193196
});
197+
198+
this._workspace.addChangeListener(() => {
199+
// Get extra code from the blocks in the workspace.
200+
const extra_init = this.getBlocksToplevelInit();
201+
// Serializing our workspace into the chosen language generator.
202+
const code =
203+
extra_init + this._manager.generator.workspaceToCode(this._workspace);
204+
this._cell.model.sharedModel.setSource(code);
205+
});
194206
}
195207

196208
private _resizeWorkspace(): void {
197209
//Resize logic.
198-
const rect = this.parent.node.getBoundingClientRect();
199-
const { height } = this._cell.node.getBoundingClientRect();
200-
const margin = rect.height / 3;
201-
202-
if (height > margin) {
203-
this._host.style.height = rect.height - margin + 'px';
204-
this._cell.node.style.height = margin + 'px';
205-
this._cell.node.style.overflowY = 'scroll';
206-
} else {
207-
this._host.style.height = rect.height - height + 'px';
208-
this._cell.node.style.overflowY = 'scroll';
209-
}
210-
211210
Blockly.svgResize(this._workspace);
212211
}
213212

packages/blockly/src/widget.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
77
import { runIcon } from '@jupyterlab/ui-components';
88

9-
import { Panel } from '@lumino/widgets';
9+
import { SplitPanel } from '@lumino/widgets';
1010
import { Signal } from '@lumino/signaling';
1111

1212
import { BlocklyLayout } from './layout';
@@ -76,7 +76,7 @@ export namespace BlocklyEditor {
7676
/**
7777
* Widget that contains the main view of the DocumentWidget.
7878
*/
79-
export class BlocklyPanel extends Panel {
79+
export class BlocklyPanel extends SplitPanel {
8080
private _context: DocumentRegistry.IContext<DocumentModel>;
8181

8282
/**

0 commit comments

Comments
 (0)