-
Notifications
You must be signed in to change notification settings - Fork 10
/
modeligado.js
96 lines (84 loc) · 2.52 KB
/
modeligado.js
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
/* global go,JSZip */
import { Parser } from './parser.js'
import { process } from './convert.js'
function difference (setA, setB) {
const _difference = new Set(setA)
for (const elem of setB) {
_difference.delete(elem)
}
return _difference
}
function parse (text, nodeName, linkData) {
const result = new Parser().parse(text)
const relates = new Set()
const classes = new Set()
for (const node of result) {
for (const relatedClass of node.relates) {
relates.add(relatedClass)
}
classes.add(node.context.name)
nodeName.push(node.context)
linkData.push(...node.relations)
}
const missingClasses = []
for (const missingClass of difference(relates, classes)) {
missingClasses.push({ name: missingClass, key: missingClass })
}
return missingClasses
}
function _saveAs (blob, filename) {
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.style = 'display: none'
a.href = url
a.download = filename
// IE 11
if (window.navigator.msSaveBlob !== undefined) {
window.navigator.msSaveBlob(blob, filename)
return
}
document.body.appendChild(a)
requestAnimationFrame(function () {
a.click()
window.URL.revokeObjectURL(url)
document.body.removeChild(a)
})
}
// https://gojs.net/latest/samples/minimalBlob.html
function exportPng (myDiagram, filename) {
myDiagram.makeImageData({
type: 'image/png',
size: new go.Size(Math.min(2000, myDiagram.documentBounds.width), Math.min(2000, myDiagram.documentBounds.height)),
background: 'white',
returnType: 'blob',
callback: (blob) => {
_saveAs(blob, filename)
}
})
}
// https://github.com/NorthwoodsSoftware/GoJS/blob/master/samples/minimalSvg.html
function exportSvg (myDiagram, filename) {
const svg = myDiagram.makeSvg({ scale: 1, background: 'white' })
const svgstr = new XMLSerializer().serializeToString(svg)
const blob = new Blob([svgstr], { type: 'image/svg+xml' })
_saveAs(blob, filename)
}
function exportTxt (text, filename) {
_saveAs(new Blob([text], {
type: 'text/plain'
}), filename)
}
function exportJava (umlText, filename) {
const zip = new JSZip()
const folder = zip.folder('app')
const result = process(new Parser().parse(umlText), 'app')
Object.keys(result).forEach(function (key) {
const code = result[key]
folder.file(key + '.java', code)
})
zip.generateAsync({ type: 'blob' })
.then(function (content) {
_saveAs(content, filename)
})
}
export { parse, exportPng, exportSvg, exportTxt, exportJava }