1- import { Layout , Widget } from '@lumino/widgets' ;
1+ import { IRenderMimeRegistry } from '@jupyterlab/rendermime' ;
2+ import { ISessionContext } from '@jupyterlab/apputils' ;
23
4+ import { Message } from '@lumino/messaging' ;
35import { PartialJSONValue } from '@lumino/coreutils' ;
4-
6+ import { PanelLayout , Widget } from '@lumino/widgets' ;
57import { IIterator , ArrayIterator } from '@lumino/algorithm' ;
68
7- import { Message } from '@lumino/messaging' ;
8-
99import * as Blockly from 'blockly' ;
1010
11- import { TOOLBOX } from './utils ' ;
11+ import { BlocklyManager } from './manager ' ;
1212
1313/**
1414 * A blockly layout to host the Blockly editor.
1515 */
16- export class BlocklyLayout extends Layout {
17- private _workspace : Blockly . WorkspaceSvg ;
16+ export class BlocklyLayout extends PanelLayout {
1817 private _host : HTMLElement ;
18+ private _manager : BlocklyManager ;
19+ private _workspace : Blockly . WorkspaceSvg ;
20+ private _sessionContext : ISessionContext ;
21+ private _outputArea : Widget ;
1922
2023 /**
2124 * Construct a `BlocklyLayout`.
2225 *
2326 */
24- constructor ( ) {
27+ constructor (
28+ manager : BlocklyManager ,
29+ sessionContext : ISessionContext ,
30+ rendermime : IRenderMimeRegistry
31+ ) {
2532 super ( ) ;
26- console . debug ( '[BlocklyLayout]' ) ;
33+ this . _manager = manager ;
34+ this . _sessionContext = sessionContext ;
2735
2836 // Creating the container for the Blockly editor
37+ // and the output area to render the execution replies.
2938 this . _host = document . createElement ( 'div' ) ;
30- this . _host . className = 'grid-stack' ;
39+ this . _outputArea = new Widget ( ) ;
3140 }
3241
3342 get workspace ( ) : PartialJSONValue {
@@ -55,9 +64,8 @@ export class BlocklyLayout extends Layout {
5564 */
5665 init ( ) : void {
5766 super . init ( ) ;
58- console . debug ( '[BlocklyLayout] init' ) ;
5967 // Add the blockly container into the DOM
60- this . parent ! . node . appendChild ( this . _host ) ;
68+ this . addWidget ( new Widget ( { node : this . _host } ) ) ;
6169 }
6270
6371 /**
@@ -76,49 +84,56 @@ export class BlocklyLayout extends Layout {
7684 return ;
7785 }
7886
87+ run ( ) : void {
88+ const code = this . _manager . generator . workspaceToCode ( this . _workspace ) ;
89+ // Execute the code using the kernel
90+ }
91+
7992 /**
8093 * Handle `update-request` messages sent to the widget.
8194 */
8295 protected onUpdateRequest ( msg : Message ) : void {
83- console . debug ( '[BlocklyLayout] onUpdateRequest' ) ;
84- // TODO: write the resize logic
96+ this . _resizeWorkspace ( ) ;
8597 }
8698
8799 /**
88100 * Handle `resize-request` messages sent to the widget.
89101 */
90102 protected onResize ( msg : Message ) : void {
91- console . debug ( '[BlocklyLayout] onResize' ) ;
92- // TODO: write the resize logic
93- const rect = this . parent . node . getBoundingClientRect ( ) ;
94- this . _host . style . width = rect . width + 'px' ;
95- this . _host . style . height = rect . height + 'px' ;
96- Blockly . svgResize ( this . _workspace ) ;
103+ this . _resizeWorkspace ( ) ;
97104 }
98105
99106 /**
100107 * Handle `fit-request` messages sent to the widget.
101108 */
102109 protected onFitRequest ( msg : Message ) : void {
103- console . debug ( '[BlocklyLayout] onFitRequest' ) ;
104- // TODO: write the resize logic
105- //
110+ this . _resizeWorkspace ( ) ;
106111 }
107112
108113 /**
109114 * Handle `after-attach` messages sent to the widget.
110115 */
111116 protected onAfterAttach ( msg : Message ) : void {
112- console . debug ( '[BlocklyLayout] onAfterAttach' ) ;
113117 this . _workspace = Blockly . inject ( this . _host , {
114- toolbox : TOOLBOX
118+ toolbox : this . _manager . toolbox
115119 } ) ;
116120 }
117121
118- /**
119- * Handle `after-show` messages sent to the widget.
120- */
121- protected onAfterShow ( msg : Message ) : void {
122- console . debug ( '[BlocklyLayout] onAfterShow' ) ;
122+ private _resizeWorkspace ( ) : void {
123+ const rect = this . parent . node . getBoundingClientRect ( ) ;
124+ const { height } = this . _outputArea . node . getBoundingClientRect ( ) ;
125+ this . _host . style . width = rect . width + 'px' ;
126+ const margin = rect . height / 3 ;
127+
128+ if ( height > margin ) {
129+ this . _host . style . height = rect . height - margin + 'px' ;
130+ this . _outputArea . node . style . height = margin + 'px' ;
131+ this . _outputArea . node . style . overflowY = 'scroll' ;
132+ } else {
133+ this . _host . style . height = rect . height - height + 'px' ;
134+ this . _outputArea . node . style . overflowY = 'hidden' ;
135+ }
136+
137+ Blockly . svgResize ( this . _workspace ) ;
123138 }
124139}
0 commit comments