Skip to content

Commit b851f30

Browse files
committed
Shared model
1 parent ce12e3b commit b851f30

File tree

5 files changed

+110
-35
lines changed

5 files changed

+110
-35
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"@jupyterlab/launcher": "^3.1.0-alpha.8",
5555
"@jupyterlab/mainmenu": "^3.1.0-alpha.8",
5656
"@jupyterlab/observables": "^4.1.0-alpha.8",
57-
"@jupyterlab/services": "~6.1.0-alpha.8",
57+
"@jupyterlab/services": "^6.1.0-alpha.8",
5858
"@jupyterlab/shared-models": "^3.1.0-alpha.8",
5959
"@jupyterlab/ui-components": "^3.1.0-alpha.8",
6060
"@lumino/commands": "^1.12.0",

src/model.ts

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
160163
export 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

src/panel.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ export class DrawIOWidget extends Widget {
416416

417417
const graph = this._editor.editor.getGraphXml();
418418
const xml = this._mx.mxUtils.getXml(graph);
419-
this._context.model.sharedModel.setSource(xml);
419+
console.debug('Save model: ', xml);
420+
//this._context.model.sharedModel.setSource(xml);
420421
}
421422
);
422423

@@ -458,6 +459,7 @@ export class DrawIOWidget extends Widget {
458459
);
459460

460461
const data = this._context.model.sharedModel.getSource();
462+
console.debug('Get Model:', data);
461463
const xml = this._mx.mxUtils.parseXml(data);
462464
this._editor.editor.setGraphXml(xml.documentElement);
463465
this._ready.resolve(void 0);

testfiles/test.dio

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<mxGraphModel dx="1297" dy="776" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" background="#ffffff">
2+
<root>
3+
<mxCell id="0"/>
4+
<mxCell id="1" parent="0"/>
5+
<mxCell id="2" value="" style="ellipse;whiteSpace=wrap;html=1;" vertex="1" parent="1">
6+
<mxGeometry x="20" y="210" width="120" height="80" as="geometry"/>
7+
</mxCell>
8+
<mxCell id="3" value="" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;" vertex="1" parent="1">
9+
<mxGeometry x="340" y="310" width="120" height="60" as="geometry"/>
10+
</mxCell>
11+
<mxCell id="4" value="" style="whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
12+
<mxGeometry x="250" y="100" width="80" height="80" as="geometry"/>
13+
</mxCell>
14+
</root>
15+
</mxGraphModel>

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@
427427
lodash.escape "^4.0.1"
428428
marked "^2.0.0"
429429

430-
"@jupyterlab/services@^6.1.0-alpha.8", "@jupyterlab/services@~6.1.0-alpha.8":
430+
"@jupyterlab/services@^6.1.0-alpha.8":
431431
version "6.1.0-alpha.8"
432432
resolved "https://registry.yarnpkg.com/@jupyterlab/services/-/services-6.1.0-alpha.8.tgz#7d70cb26474aaba9979e9dc632539b21371266eb"
433433
integrity sha512-1OPIkdF7zC5rUklD0zfR7Ozyg1RHW5IyY1CKE2ksbxQsBPAB1aPDj9V4CLOyla3l6QBLkPTpm6MTlXgQh/MD8Q==

0 commit comments

Comments
 (0)