@@ -4,7 +4,7 @@ import { CodeCell, CodeCellModel } from '@jupyterlab/cells';
44
55import { Message } from '@lumino/messaging' ;
66import { PartialJSONValue } from '@lumino/coreutils' ;
7- import { PanelLayout , Widget } from '@lumino/widgets' ;
7+ import { SplitLayout , SplitPanel , Widget } from '@lumino/widgets' ;
88import { IIterator , ArrayIterator } from '@lumino/algorithm' ;
99import { 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
0 commit comments