@@ -81,11 +81,8 @@ export class DrawIODocumentModel implements DocumentRegistry.IModel {
8181
8282 toString ( ) : string {
8383 // 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');
8986 return this . sharedModel . getSource ( ) ;
9087 }
9188
@@ -97,17 +94,23 @@ export class DrawIODocumentModel implements DocumentRegistry.IModel {
9794
9895 toJSON ( ) : PartialJSONValue {
9996 // 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 ( ) ) ;
102103 }
103104
104105 fromJSON ( value : ReadonlyPartialJSONValue ) : void {
105106 // 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 ( ) ) ;
107110 }
108111
109112 initialize ( ) : void {
110- console . warn ( 'fromJSON (): Not implemented' ) ;
113+ // console.warn('initialize (): Not implemented');
111114 }
112115
113116 private _dirty = false ;
@@ -160,11 +163,8 @@ export interface ISharedXMLFile extends ISharedDocument {
160163export class XMLFile extends YDocument < XMLChange > implements ISharedDocument {
161164 constructor ( ) {
162165 super ( ) ;
163- console . debug ( 'XMLFile:' , this . ydoc ) ;
164166 this . _mxGraphAttributes = this . ydoc . getMap ( 'attributes' ) ;
165167 this . _root = this . ydoc . getXmlFragment ( 'root' ) ;
166- console . debug ( this . _root instanceof Y . XmlFragment ) ;
167- this . _root . insert ( 0 , [ new Y . XmlElement ( 'cell' ) ] ) ;
168168
169169 this . _mxGraphAttributes . observeDeep ( this . _modelObserver ) ;
170170 this . _root . observeDeep ( this . _cellsObserver ) ;
@@ -198,20 +198,39 @@ export class XMLFile extends YDocument<XMLChange> implements ISharedDocument {
198198 * @returns Cell's source.
199199 */
200200 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 } "` ;
208204 } ) ;
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 ;
215234 }
216235
217236 /**
@@ -220,15 +239,54 @@ export class XMLFile extends YDocument<XMLChange> implements ISharedDocument {
220239 * @param value: New source.
221240 */
222241 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+
225258 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 ] ) ;
229289 } ) ;
230- //this._source.delete(0, this._source.length);
231- //this._source.insert(0, [new XmlElement(value)]);
232290 } ) ;
233291 }
234292
0 commit comments