1- import { Layout , Widget } from '@lumino/widgets' ;
1+ import { SimplifiedOutputArea , OutputAreaModel } from '@jupyterlab/outputarea' ;
2+ import { IRenderMimeRegistry } from '@jupyterlab/rendermime' ;
3+ import { ISessionContext } from '@jupyterlab/apputils' ;
24
5+ import { Message } from '@lumino/messaging' ;
36import { PartialJSONValue } from '@lumino/coreutils' ;
4-
7+ import { PanelLayout , Widget } from '@lumino/widgets' ;
58import { IIterator , ArrayIterator } from '@lumino/algorithm' ;
69
7- import { Message } from '@lumino/messaging' ;
8-
910import * as Blockly from 'blockly' ;
1011
11- import { TOOLBOX } from './utils ' ;
12+ import { BlocklyManager } from './manager ' ;
1213
1314/**
1415 * A blockly layout to host the Blockly editor.
1516 */
16- export class BlocklyLayout extends Layout {
17- private _workspace : Blockly . WorkspaceSvg ;
17+ export class BlocklyLayout extends PanelLayout {
1818 private _host : HTMLElement ;
19+ private _manager : BlocklyManager ;
20+ private _workspace : Blockly . WorkspaceSvg ;
21+ private _sessionContext : ISessionContext ;
22+ private _outputArea : SimplifiedOutputArea ;
1923
2024 /**
2125 * Construct a `BlocklyLayout`.
2226 *
2327 */
24- constructor ( ) {
28+ constructor (
29+ manager : BlocklyManager ,
30+ sessionContext : ISessionContext ,
31+ rendermime : IRenderMimeRegistry
32+ ) {
2533 super ( ) ;
26- console . debug ( '[BlocklyLayout]' ) ;
34+ this . _manager = manager ;
35+ this . _sessionContext = sessionContext ;
2736
2837 // Creating the container for the Blockly editor
2938 this . _host = document . createElement ( 'div' ) ;
30- this . _host . className = 'grid-stack' ;
39+ this . _outputArea = new SimplifiedOutputArea ( {
40+ model : new OutputAreaModel ( { trusted : true } ) ,
41+ rendermime
42+ } ) ;
3143 }
3244
3345 get workspace ( ) : PartialJSONValue {
@@ -55,9 +67,8 @@ export class BlocklyLayout extends Layout {
5567 */
5668 init ( ) : void {
5769 super . init ( ) ;
58- console . debug ( '[BlocklyLayout] init' ) ;
5970 // Add the blockly container into the DOM
60- this . parent ! . node . appendChild ( this . _host ) ;
71+ this . addWidget ( new Widget ( { node : this . _host } ) ) ;
6172 }
6273
6374 /**
@@ -76,49 +87,61 @@ export class BlocklyLayout extends Layout {
7687 return ;
7788 }
7889
90+ run ( ) : void {
91+ const code = this . _manager . generator . workspaceToCode ( this . _workspace ) ;
92+ SimplifiedOutputArea . execute ( code , this . _outputArea , this . _sessionContext )
93+ . then ( resp => {
94+ this . addWidget ( this . _outputArea ) ;
95+ this . _resizeWorkspace ( ) ;
96+ } )
97+ . catch ( e => console . error ( e ) ) ;
98+ }
99+
79100 /**
80101 * Handle `update-request` messages sent to the widget.
81102 */
82103 protected onUpdateRequest ( msg : Message ) : void {
83- console . debug ( '[BlocklyLayout] onUpdateRequest' ) ;
84- // TODO: write the resize logic
104+ this . _resizeWorkspace ( ) ;
85105 }
86106
87107 /**
88108 * Handle `resize-request` messages sent to the widget.
89109 */
90110 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 ) ;
111+ this . _resizeWorkspace ( ) ;
97112 }
98113
99114 /**
100115 * Handle `fit-request` messages sent to the widget.
101116 */
102117 protected onFitRequest ( msg : Message ) : void {
103- console . debug ( '[BlocklyLayout] onFitRequest' ) ;
104- // TODO: write the resize logic
105- //
118+ this . _resizeWorkspace ( ) ;
106119 }
107120
108121 /**
109122 * Handle `after-attach` messages sent to the widget.
110123 */
111124 protected onAfterAttach ( msg : Message ) : void {
112- console . debug ( '[BlocklyLayout] onAfterAttach' ) ;
113125 this . _workspace = Blockly . inject ( this . _host , {
114- toolbox : TOOLBOX
126+ toolbox : this . _manager . toolbox
115127 } ) ;
116128 }
117129
118- /**
119- * Handle `after-show` messages sent to the widget.
120- */
121- protected onAfterShow ( msg : Message ) : void {
122- console . debug ( '[BlocklyLayout] onAfterShow' ) ;
130+ private _resizeWorkspace ( ) : void {
131+ const rect = this . parent . node . getBoundingClientRect ( ) ;
132+ const { height } = this . _outputArea . node . getBoundingClientRect ( ) ;
133+ this . _host . style . width = rect . width + 'px' ;
134+ const margin = rect . height / 3 ;
135+
136+ if ( height > margin ) {
137+ this . _host . style . height = rect . height - margin + 'px' ;
138+ this . _outputArea . node . style . height = margin + 'px' ;
139+ this . _outputArea . node . style . overflowY = 'scroll' ;
140+ } else {
141+ this . _host . style . height = rect . height - height + 'px' ;
142+ this . _outputArea . node . style . overflowY = 'hidden' ;
143+ }
144+
145+ Blockly . svgResize ( this . _workspace ) ;
123146 }
124147}
0 commit comments