-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy patheditor.ts
131 lines (114 loc) · 4.53 KB
/
editor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*******************************************************************************
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
import 'reflect-metadata';
import { LocalModelSource, TYPES } from 'sprotty';
import { createMonacoEditor, openWebSocketElkGraph } from '../common/creators';
import createContainer from '../sprotty-config';
import { getParameters, setupModelLink } from "../url-parameters";
import { ElkGraphJsonToSprotty } from './elkgraph-to-sprotty';
import JSON5 = require('json5');
import LZString = require('lz-string');
require('./elk-json-language');
const loading = document.getElementById('loading-sprotty')!;
const urlParameters = getParameters();
let initialContent: string;
if (urlParameters.compressedContent !== undefined) {
initialContent = LZString.decompressFromEncodedURIComponent(urlParameters.compressedContent);
} else if (urlParameters.initialContent !== undefined) {
initialContent = decodeURIComponent(urlParameters.initialContent);
} else {
initialContent = `{
id: "root",
layoutOptions: { 'algorithm': 'layered' },
children: [
{ id: "n1", width: 30, height: 30 },
{ id: "n2", width: 30, height: 30 },
{ id: "n3", width: 30, height: 30 }
],
edges: [
{ id: "e1", sources: [ "n1" ], targets: [ "n2" ] },
{ id: "e2", sources: [ "n1" ], targets: [ "n3" ] }
]
}`;
}
// Create Monaco editor and connect to language server (mainly for content assist)
const editor = createMonacoEditor('monaco-editor', 'elkj', initialContent);
openWebSocketElkGraph({
endpoint: 'elkgraphjson',
name: 'ELK JSON Graph Language Client',
documentSelector: ['elkj'],
});
// Create Sprotty viewer
const sprottyContainer = createContainer();
sprottyContainer.bind(TYPES.ModelSource).to(LocalModelSource).inSingletonScope();
const modelSource = sprottyContainer.get<LocalModelSource>(TYPES.ModelSource);
const versionSelect = <HTMLSelectElement>document.getElementById('elk-version');
// Prepare multiple elks to be loaded lazily
let elks = {}
// Set up ELK
function retrieveElk(version) {
if (elks[version] !== undefined) {
return Promise.resolve(elks[version]);
}
return import(/* webpackChunkName: "[request]" */ '../../node_modules/elkjs-' + version + '/lib/elk-api')
.then(ELK => {
elks[version] = new ELK.default({
workerUrl: './elk-' + version + '/elk-worker.min.js'
});
return elks[version];
});
}
// Register listener
editor.getModel().onDidChangeContent(e => updateModel());
versionSelect.onchange = e => updateModel();
// Initial layout
updateModel();
setupModelLink(editor, (event) => {
return {
compressedContent: LZString.compressToEncodedURIComponent(editor.getValue())
}
});
function updateModel() {
try {
let json = JSON5.parse(editor.getValue());
// Force PARENT edge coordinates, so that Sprotty can draw the graph correctly.
let props = json.properties || {};
props["org.eclipse.elk.json.edgeCoords"] = "PARENT";
json.properties = props;
monaco.editor.setModelMarkers(editor.getModel(), "", []);
// Prepare the elk version selected by the user
let selectedVersion = versionSelect.options[versionSelect.selectedIndex].value;
loading.style.display = 'block';
retrieveElk(selectedVersion).then(elk => {
elk.layout(json)
.then(g => {
let sGraph = new ElkGraphJsonToSprotty().transform(g);
modelSource.updateModel(sGraph)
})
.catch(e => {
let markers = [ errorToMarker(e) ]
monaco.editor.setModelMarkers(editor.getModel(), "", markers)
})
.finally(() => loading.style.display = 'none');
});
} catch (e) {
let markers = [ errorToMarker(e) ];
monaco.editor.setModelMarkers(editor.getModel(), "", markers);
loading.style.display = 'none';
}
}
function errorToMarker(e: any): monaco.editor.IMarkerData {
return <monaco.editor.IMarkerData> {
severity: monaco.MarkerSeverity.Error,
startLineNumber: e.lineNumber || 0,
startColumn: e.columnNumber || 0,
message: e.message
};
}