@@ -28,7 +28,10 @@ w.OPEN_FORM = web_path + 'open.html';
28
28
w . mxLoadStylesheets = false ; // disable loading stylesheets
29
29
w . mxLoadResources = false ;
30
30
31
- import * as mx from './mxgraph/javascript/examples/grapheditor/www/modulated.js' ;
31
+ /* This is a typing-only import. If you use it directly, the mxgraph content
32
+ will be included in the main JupyterLab js bundle.
33
+ */
34
+ import * as MXModuleType from './mxgraph/javascript/examples/grapheditor/www/modulated.js' ;
32
35
33
36
import {
34
37
ABCWidgetFactory , DocumentRegistry , DocumentWidget ,
@@ -69,21 +72,29 @@ class DrawioWidget extends DocumentWidget<Widget> {
69
72
constructor ( options : DocumentWidget . IOptions < Widget > ) {
70
73
super ( { ...options } ) ;
71
74
this . context = options [ 'context' ] ;
75
+ void Private . ensureMx ( ) . then ( ( mx ) => this . onMxLoaded ( mx ) ) ;
76
+ }
72
77
78
+ protected async onMxLoaded ( mx : Private . MX ) {
79
+ this . mx = mx ;
73
80
this . _onTitleChanged ( ) ;
74
81
this . context . pathChanged . connect ( this . _onTitleChanged , this ) ;
75
82
76
- this . context . ready . then ( ( ) => { this . _onContextReady ( ) ; } ) ;
77
- this . context . ready . then ( ( ) => { this . _handleDirtyStateNew ( ) ; } ) ;
83
+ await this . context . ready ;
84
+
85
+ this . _onContextReady ( ) ;
86
+ this . _handleDirtyStateNew ( ) ;
78
87
}
79
88
80
- protected onAfterShow ( msg : Message ) : void {
81
- this . _loadEditor ( this . node ) ;
82
- this . _onContentChanged ( ) ;
89
+ onAfterShow ( msg : Message ) : void {
90
+ Private . ensureMx ( ) . then ( ( ) => {
91
+ this . _loadEditor ( this . node ) ;
92
+ this . _onContentChanged ( ) ;
93
+ } ) ;
83
94
}
84
95
85
96
public getSVG ( ) : string {
86
- return mx . mxUtils . getXml ( this . _editor . editor . graph . getSvg ( ) ) ;
97
+ return this . mx . mxUtils . getXml ( this . _editor . editor . graph . getSvg ( ) ) ;
87
98
}
88
99
89
100
private _onContextReady ( ) : void {
@@ -116,6 +127,7 @@ class DrawioWidget extends DocumentWidget<Widget> {
116
127
}
117
128
118
129
private _loadEditor ( node : HTMLElement , contents ?: string ) : void {
130
+ const { mx} = this ;
119
131
// Adds required resources (disables loading of fallback properties, this can only
120
132
// be used if we know that all keys are defined in the language specific file)
121
133
mx . mxResources . loadDefaultBundle = false ;
@@ -145,6 +157,7 @@ class DrawioWidget extends DocumentWidget<Widget> {
145
157
}
146
158
147
159
private _onContentChanged ( ) : void {
160
+ const { mx} = this ;
148
161
if ( this . _editor === undefined )
149
162
{
150
163
return ;
@@ -167,7 +180,7 @@ class DrawioWidget extends DocumentWidget<Widget> {
167
180
{
168
181
this . _editor . editor . graph . stopEditing ( ) ;
169
182
}
170
- let xml = mx . mxUtils . getXml ( this . _editor . editor . getGraphXml ( ) ) ;
183
+ let xml = this . mx . mxUtils . getXml ( this . _editor . editor . getGraphXml ( ) ) ;
171
184
this . context . model . fromString ( xml ) ;
172
185
}
173
186
@@ -198,6 +211,7 @@ class DrawioWidget extends DocumentWidget<Widget> {
198
211
readonly context : DocumentRegistry . Context ;
199
212
private _editor : any ;
200
213
private _ready = new PromiseDelegate < void > ( ) ;
214
+ protected mx : Private . MX ;
201
215
}
202
216
203
217
/**
@@ -216,3 +230,38 @@ class DrawioFactory extends ABCWidgetFactory<DrawioWidget, DocumentRegistry.IMod
216
230
return new DrawioWidget ( { context, content : new Widget ( ) } ) ;
217
231
}
218
232
}
233
+
234
+
235
+ /**
236
+ * A namespace for module-level concerns like loading mxgraph
237
+ */
238
+
239
+ namespace Private {
240
+ export type MX = typeof MXModuleType ;
241
+
242
+ let _mx : typeof MXModuleType ;
243
+ let _mxLoading : PromiseDelegate < MX > ;
244
+
245
+ /**
246
+ * Asynchronously load the mx bundle, or return it if already available
247
+ */
248
+ export async function ensureMx ( ) : Promise < MX > {
249
+ if ( _mx )
250
+ {
251
+ return _mx ;
252
+ }
253
+
254
+ if ( _mxLoading )
255
+ {
256
+ return await _mxLoading . promise ;
257
+ }
258
+
259
+ _mxLoading = new PromiseDelegate ( ) ;
260
+ _mx = await import (
261
+ /* webpackChunkName: "mxgraph" */
262
+ './mxgraph/javascript/examples/grapheditor/www/modulated.js'
263
+ ) ;
264
+ _mxLoading . resolve ( _mx ) ;
265
+ return _mx ;
266
+ }
267
+ }
0 commit comments