@@ -4,7 +4,7 @@ import { CodeCell, CodeCellModel } from '@jupyterlab/cells';
4
4
5
5
import { Message } from '@lumino/messaging' ;
6
6
import { PartialJSONValue } from '@lumino/coreutils' ;
7
- import { PanelLayout , Widget } from '@lumino/widgets' ;
7
+ import { SplitLayout , SplitPanel , Widget } from '@lumino/widgets' ;
8
8
import { IIterator , ArrayIterator } from '@lumino/algorithm' ;
9
9
import { Signal } from '@lumino/signaling' ;
10
10
@@ -16,8 +16,8 @@ import { THEME } from './utils';
16
16
/**
17
17
* A blockly layout to host the Blockly editor.
18
18
*/
19
- export class BlocklyLayout extends PanelLayout {
20
- private _host : HTMLElement ;
19
+ export class BlocklyLayout extends SplitLayout {
20
+ private _host : Widget ;
21
21
private _manager : BlocklyManager ;
22
22
private _workspace : Blockly . WorkspaceSvg ;
23
23
private _sessionContext : ISessionContext ;
@@ -32,13 +32,13 @@ export class BlocklyLayout extends PanelLayout {
32
32
sessionContext : ISessionContext ,
33
33
rendermime : IRenderMimeRegistry
34
34
) {
35
- super ( ) ;
35
+ super ( { renderer : SplitPanel . defaultRenderer , orientation : 'vertical' } ) ;
36
36
this . _manager = manager ;
37
37
this . _sessionContext = sessionContext ;
38
38
39
39
// Creating the container for the Blockly editor
40
40
// and the output area to render the execution replies.
41
- this . _host = document . createElement ( 'div' ) ;
41
+ this . _host = new Widget ( ) ;
42
42
43
43
// Creating a CodeCell widget to render the code and
44
44
// outputs from the execution reply.
@@ -83,7 +83,8 @@ export class BlocklyLayout extends PanelLayout {
83
83
init ( ) : void {
84
84
super . init ( ) ;
85
85
// 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 ) ;
87
88
}
88
89
89
90
/**
@@ -141,8 +142,6 @@ export class BlocklyLayout extends PanelLayout {
141
142
const code =
142
143
extra_init + this . _manager . generator . workspaceToCode ( this . _workspace ) ;
143
144
this . _cell . model . sharedModel . setSource ( code ) ;
144
- this . addWidget ( this . _cell ) ;
145
- this . _resizeWorkspace ( ) ;
146
145
147
146
// Execute the code using the kernel, by using a static method from the
148
147
// same class to make an execution request.
@@ -165,49 +164,49 @@ export class BlocklyLayout extends PanelLayout {
165
164
* Handle `update-request` messages sent to the widget.
166
165
*/
167
166
protected onUpdateRequest ( msg : Message ) : void {
167
+ super . onUpdateRequest ( msg ) ;
168
168
this . _resizeWorkspace ( ) ;
169
169
}
170
170
171
171
/**
172
172
* Handle `resize-request` messages sent to the widget.
173
173
*/
174
- protected onResize ( msg : Message ) : void {
174
+ protected onResize ( msg : Widget . ResizeMessage ) : void {
175
+ super . onResize ( msg ) ;
175
176
this . _resizeWorkspace ( ) ;
176
177
}
177
178
178
179
/**
179
180
* Handle `fit-request` messages sent to the widget.
180
181
*/
181
182
protected onFitRequest ( msg : Message ) : void {
183
+ super . onFitRequest ( msg ) ;
182
184
this . _resizeWorkspace ( ) ;
183
185
}
184
186
185
187
/**
186
188
* Handle `after-attach` messages sent to the widget.
187
189
*/
188
190
protected onAfterAttach ( msg : Message ) : void {
191
+ super . onAfterAttach ( msg ) ;
189
192
//inject Blockly with appropiate JupyterLab theme.
190
- this . _workspace = Blockly . inject ( this . _host , {
193
+ this . _workspace = Blockly . inject ( this . _host . node , {
191
194
toolbox : this . _manager . toolbox ,
192
195
theme : THEME
193
196
} ) ;
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
+ } ) ;
194
206
}
195
207
196
208
private _resizeWorkspace ( ) : void {
197
209
//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
-
211
210
Blockly . svgResize ( this . _workspace ) ;
212
211
}
213
212
0 commit comments