5
5
} from '@jupyterlab/docregistry' ;
6
6
import { IRenderMimeRegistry } from '@jupyterlab/rendermime' ;
7
7
import { runIcon } from '@jupyterlab/ui-components' ;
8
+ import { showErrorMessage } from "@jupyterlab/apputils" ;
8
9
10
+ import { PartialJSONObject } from "@lumino/coreutils" ;
9
11
import { SplitPanel } from '@lumino/widgets' ;
10
12
import { Signal } from '@lumino/signaling' ;
11
13
@@ -82,6 +84,7 @@ export namespace BlocklyEditor {
82
84
*/
83
85
export class BlocklyPanel extends SplitPanel {
84
86
private _context : DocumentRegistry . IContext < DocumentModel > ;
87
+ private _manager : BlocklyManager ;
85
88
private _rendermime : IRenderMimeRegistry ;
86
89
87
90
/**
@@ -105,6 +108,7 @@ export class BlocklyPanel extends SplitPanel {
105
108
} ) ;
106
109
this . addClass ( 'jp-BlocklyPanel' ) ;
107
110
this . _context = context ;
111
+ this . _manager = manager ;
108
112
this . _rendermime = rendermime ;
109
113
110
114
// Load the content of the file when the context is ready
@@ -139,9 +143,44 @@ export class BlocklyPanel extends SplitPanel {
139
143
}
140
144
141
145
private _load ( ) : void {
142
- // Loading the content of the document into the workspace
143
- const content = this . _context . model . toJSON ( ) as any as Blockly . Workspace ;
144
- ( this . layout as BlocklyLayout ) . workspace = content ;
146
+ const fileContent = this . _context . model . toJSON ( ) ;
147
+ const fileFormat = fileContent [ 'format' ] ;
148
+ // Check if format is set or if we have legacy content
149
+ if ( fileFormat === undefined && fileContent [ 'blocks' ] ) {
150
+ // Load legacy content
151
+ ( this . layout as BlocklyLayout ) . workspace = fileContent as any as Blockly . Workspace ;
152
+ } else if ( fileFormat === 2 ) {
153
+ // Load the content from the "workspace" key
154
+ ( this . layout as BlocklyLayout ) . workspace = fileContent [ 'workspace' ] as any as Blockly . Workspace ;
155
+ const metadata = fileContent [ 'metadata' ] ;
156
+ if ( metadata ) {
157
+ if ( metadata [ 'toolbox' ] ) {
158
+ const toolbox = metadata [ 'toolbox' ] ;
159
+ if ( this . _manager . listToolboxes ( ) . find ( value => value . value === toolbox ) ) {
160
+ this . _manager . setToolbox ( metadata [ 'toolbox' ] ) ;
161
+ } else {
162
+ // Unknown toolbox
163
+ showErrorMessage ( `Unknown toolbox` ,
164
+ `The toolbox '` + toolbox + `' is not available. Using default toolbox.`
165
+ ) ;
166
+ }
167
+ }
168
+ if ( metadata [ 'kernel' ] && metadata [ 'kernel' ] !== 'No kernel' ) {
169
+ const kernel = metadata [ 'kernel' ] ;
170
+ if ( this . _manager . listKernels ( ) . find ( value => value . value === kernel ) ) {
171
+ this . _manager . selectKernel ( metadata [ 'kernel' ] ) ;
172
+ } else {
173
+ // Unknown kernel
174
+ console . warn ( `Unknown kernel in blockly file: ` + kernel ) ;
175
+ }
176
+ }
177
+ }
178
+ } else {
179
+ // Unsupported format
180
+ showErrorMessage ( `Unsupported file format` ,
181
+ `The file format '` + fileFormat + `' is not supported by the Blockly editor.`
182
+ ) ;
183
+ }
145
184
}
146
185
147
186
private _onSave (
@@ -150,7 +189,15 @@ export class BlocklyPanel extends SplitPanel {
150
189
) : void {
151
190
if ( state === 'started' ) {
152
191
const workspace = ( this . layout as BlocklyLayout ) . workspace ;
153
- this . _context . model . fromJSON ( workspace as any ) ;
192
+ const fileContent : PartialJSONObject = {
193
+ format : 2 ,
194
+ workspace : workspace as any ,
195
+ metadata : {
196
+ toolbox : this . _manager . getToolbox ( ) ,
197
+ kernel : this . _manager . kernel
198
+ }
199
+ } ;
200
+ this . _context . model . fromJSON ( fileContent ) ;
154
201
}
155
202
}
156
203
}
0 commit comments