@@ -79,7 +79,9 @@ export class DrawioWidget extends DocumentWidget<Widget> {
7979 onAfterShow ( msg : Message ) : void {
8080 Private . ensureMx ( ) . then ( ( ) => {
8181 this . _loadEditor ( this . node ) ;
82- this . _onContentChanged ( ) ;
82+ const contextModel = this . context . model ;
83+ const xml = this . mx . mxUtils . parseXml ( contextModel . toString ( ) ) ;
84+ this . _editor . editor . setGraphXml ( xml . documentElement ) ;
8385 } ) ;
8486 }
8587
@@ -90,8 +92,8 @@ export class DrawioWidget extends DocumentWidget<Widget> {
9092 private _onContextReady ( ) : void {
9193 const contextModel = this . context . model ;
9294
93- // Set the editor model value.
94- this . _onContentChanged ( ) ;
95+ const xml = this . mx . mxUtils . parseXml ( contextModel . toString ( ) ) ;
96+ this . _editor . editor . setGraphXml ( xml . documentElement ) ;
9597
9698 contextModel . contentChanged . connect ( this . _onContentChanged , this ) ;
9799 contextModel . stateChanged . connect ( this . _onModelStateChangedNew , this ) ;
@@ -129,13 +131,17 @@ export class DrawioWidget extends DocumentWidget<Widget> {
129131 // Workaround for TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature
130132 const _Editor : any = mx . Editor ;
131133 this . _editor = new mx . EditorUi ( new _Editor ( false , themes ) , node ) ;
132-
134+
133135 this . _editor . editor . graph . model . addListener (
134- mx . mxEvent . NOTIFY ,
136+ mx . mxEvent . CHANGE ,
135137 ( sender : any , evt : any ) => {
138+ //console.log(mx.mxEvent.CHANGE, sender, evt);
139+ const changes : any [ ] = evt . properties . changes ;
140+ for ( let i = 0 ; i < changes . length ; i ++ ) {
141+ if ( changes [ i ] . root ) return ;
142+ }
136143 this . _saveToContext ( ) ;
137- }
138- ) ;
144+ } ) ;
139145
140146 return this . _editor ;
141147 }
@@ -157,17 +163,16 @@ export class DrawioWidget extends DocumentWidget<Widget> {
157163 const newValue = this . context . model . toString ( ) ;
158164
159165 if ( oldValue !== newValue && ! this . _editor . editor . graph . isEditing ( ) ) {
160- if ( newValue . length ) {
161- const xml = mx . mxUtils . parseXml ( newValue ) ;
162- this . _editor . editor . setGraphXml ( xml . documentElement ) ;
163- }
166+ const xml = mx . mxUtils . parseXml ( newValue ) ;
167+ this . _editor . editor . setGraphXml ( xml . documentElement ) ;
164168 }
165169 }
166170
167171 private _saveToContext ( ) : void {
168172 if ( this . _editor . editor . graph . isEditing ( ) ) {
169173 this . _editor . editor . graph . stopEditing ( ) ;
170174 }
175+
171176 const xml = this . mx . mxUtils . getXml ( this . _editor . editor . getGraphXml ( ) ) ;
172177 this . context . model . fromString ( xml ) ;
173178 }
0 commit comments