@@ -81,11 +81,8 @@ export class DrawIODocumentModel implements DocumentRegistry.IModel {
81
81
82
82
toString ( ) : string {
83
83
// TODO: Return content from shared model
84
- console . info (
85
- 'DrawIODocumentModel.toString():' ,
86
- this . sharedModel . getSource ( )
87
- ) ;
88
- throw new Error ( 'not implemented' ) ;
84
+ //console.info('DrawIODocumentModel.toString():', this.sharedModel.getSource());
85
+ //throw new Error('not implemented');
89
86
return this . sharedModel . getSource ( ) ;
90
87
}
91
88
@@ -97,17 +94,23 @@ export class DrawIODocumentModel implements DocumentRegistry.IModel {
97
94
98
95
toJSON ( ) : PartialJSONValue {
99
96
// TODO: Return content from shared model
100
- console . warn ( 'toJSON(): Not implemented' ) ;
101
- return { } ;
97
+ console . info (
98
+ 'DrawIODocumentModel.toJSON():' ,
99
+ JSON . parse ( this . sharedModel . getSource ( ) )
100
+ ) ;
101
+ throw new Error ( 'not implemented' ) ;
102
+ return JSON . parse ( this . sharedModel . getSource ( ) ) ;
102
103
}
103
104
104
105
fromJSON ( value : ReadonlyPartialJSONValue ) : void {
105
106
// TODO: Add content to shared model
106
- console . warn ( 'fromJSON(): Not implemented' ) ;
107
+ console . info ( 'DrawIODocumentModel.fromJSON():' , value ) ;
108
+ throw new Error ( 'not implemented' ) ;
109
+ this . sharedModel . setSource ( value . toString ( ) ) ;
107
110
}
108
111
109
112
initialize ( ) : void {
110
- console . warn ( 'fromJSON (): Not implemented' ) ;
113
+ // console.warn('initialize (): Not implemented');
111
114
}
112
115
113
116
private _dirty = false ;
@@ -160,11 +163,8 @@ export interface ISharedXMLFile extends ISharedDocument {
160
163
export class XMLFile extends YDocument < XMLChange > implements ISharedDocument {
161
164
constructor ( ) {
162
165
super ( ) ;
163
- console . debug ( 'XMLFile:' , this . ydoc ) ;
164
166
this . _mxGraphAttributes = this . ydoc . getMap ( 'attributes' ) ;
165
167
this . _root = this . ydoc . getXmlFragment ( 'root' ) ;
166
- console . debug ( this . _root instanceof Y . XmlFragment ) ;
167
- this . _root . insert ( 0 , [ new Y . XmlElement ( 'cell' ) ] ) ;
168
168
169
169
this . _mxGraphAttributes . observeDeep ( this . _modelObserver ) ;
170
170
this . _root . observeDeep ( this . _cellsObserver ) ;
@@ -198,20 +198,39 @@ export class XMLFile extends YDocument<XMLChange> implements ISharedDocument {
198
198
* @returns Cell's source.
199
199
*/
200
200
public getSource ( ) : string {
201
- const serializer = new XMLSerializer ( ) ;
202
-
203
- const doc = new Y . XmlElement ( 'mxGraphModel' ) ;
204
-
205
- this . _mxGraphAttributes . forEach ( ( key , value ) => {
206
- console . debug ( 'key:' , key , 'value:' , value ) ;
207
- doc . setAttribute ( key , value ) ;
201
+ let source = '<mxGraphModel' ;
202
+ this . _mxGraphAttributes . forEach ( ( value , key ) => {
203
+ source += ` ${ key } ="${ value } "` ;
208
204
} ) ;
209
-
210
- const root = this . _root . get ( 0 ) . clone ( ) as Y . XmlElement ;
211
- root . nodeName = 'root' ;
212
- doc . insert ( 0 , [ root ] ) ;
213
-
214
- return serializer . serializeToString ( doc . toDOM ( ) ) ;
205
+ source += '><root>' ;
206
+
207
+ for ( let i = this . _root . length - 1 ; i >= 0 ; i -- ) {
208
+ let mxCell = '<mxCell' ;
209
+ const cell = this . _root . get ( i ) as Y . XmlElement ;
210
+ const cellAttrs = cell . getAttributes ( ) ;
211
+ const cellGeometry = cell . firstChild ;
212
+
213
+ for ( const [ key , value ] of Object . entries ( cellAttrs ) ) {
214
+ mxCell += ` ${ key } ="${ value } "` ;
215
+ }
216
+
217
+ if ( cellGeometry ) {
218
+ let mxGeometry = '<mxGeometry' ;
219
+ for ( const [ key , value ] of Object . entries (
220
+ cellGeometry . getAttributes ( )
221
+ ) ) {
222
+ mxGeometry += ` ${ key } ="${ value } "` ;
223
+ }
224
+ mxCell += `>${ mxGeometry } /></mxCell>` ;
225
+ } else {
226
+ mxCell += ' />' ;
227
+ }
228
+
229
+ source += mxCell ;
230
+ }
231
+
232
+ source += '</root></mxGraphModel>' ;
233
+ return source ;
215
234
}
216
235
217
236
/**
@@ -220,15 +239,54 @@ export class XMLFile extends YDocument<XMLChange> implements ISharedDocument {
220
239
* @param value: New source.
221
240
*/
222
241
public setSource ( value : string ) : void {
223
- const doc = parse ( value ) ;
224
- console . debug ( 'setSource:' , doc ) ;
242
+ const doc = parse (
243
+ value ,
244
+ {
245
+ attrNodeName : 'attr' ,
246
+ textNodeName : 'text' ,
247
+ attributeNamePrefix : '' ,
248
+ arrayMode : false ,
249
+ ignoreAttributes : false ,
250
+ parseAttributeValue : false
251
+ } ,
252
+ true
253
+ ) ;
254
+
255
+ const attrs = doc [ 'mxGraphModel' ] [ 'attr' ] ;
256
+ const cells = doc [ 'mxGraphModel' ] [ 'root' ] [ 'mxCell' ] ;
257
+
225
258
this . transact ( ( ) => {
226
- console . debug ( doc [ 'mxGraphModel' ] ) ;
227
- doc [ 'mxGraphModel' ] . attributes . map ( ( key : string , value : any ) => {
228
- console . debug ( key , value ) ;
259
+ // Delete previus attribute entries
260
+ // this._mxGraphAttributes.entries.forEach( key => this._mxGraphAttributes.delete(key) );
261
+
262
+ // Inserting attributes
263
+ for ( const [ key , value ] of Object . entries ( attrs ) ) {
264
+ this . _mxGraphAttributes . set ( key , value ) ;
265
+ }
266
+
267
+ // Inserting mxCells
268
+ cells . forEach ( ( value : any ) => {
269
+ const cellAttrs = value [ 'attr' ] ;
270
+ const cellGeometry = value [ 'mxGeometry' ] ;
271
+
272
+ const mxCell = new Y . XmlElement ( 'mxCell' ) ;
273
+ // Inserting attributes
274
+ for ( const [ key , value ] of Object . entries ( cellAttrs ) ) {
275
+ //console.debug(key, value);
276
+ mxCell . setAttribute ( key , value as string ) ;
277
+ }
278
+
279
+ if ( cellGeometry ) {
280
+ const geometryAttrs = cellGeometry [ 'attr' ] ;
281
+ const mxGeometry = new Y . XmlElement ( 'mxGeometry' ) ;
282
+ for ( const [ key , value ] of Object . entries ( geometryAttrs ) ) {
283
+ mxGeometry . setAttribute ( key , value as string ) ;
284
+ }
285
+ mxCell . push ( [ mxGeometry ] ) ;
286
+ }
287
+
288
+ this . _root . insert ( 0 , [ mxCell ] ) ;
229
289
} ) ;
230
- //this._source.delete(0, this._source.length);
231
- //this._source.insert(0, [new XmlElement(value)]);
232
290
} ) ;
233
291
}
234
292
0 commit comments