@pairdraw/drawio 0.0.0-stage → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/chunks/embedded.js +167 -0
- package/dist/chunks/embedded.js.map +1 -0
- package/dist/chunks/index.js +313 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/index.d.ts +124 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/package.json +42 -4
- package/src/embedded.ts +98 -0
- package/src/index.ts +30 -0
- package/src/inflate.ts +65 -0
- package/src/model.ts +77 -0
- package/src/read.ts +182 -0
- package/src/style.ts +33 -0
- package/src/write.ts +106 -0
- package/src/xml.ts +38 -0
package/src/write.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { DOMImplementation, XMLSerializer, type Document, type Element } from '@xmldom/xmldom'
|
|
2
|
+
|
|
3
|
+
import type { DrawIOCell, DrawIOFile, DrawIOGeometry, DrawIOPage, DrawIOPoint } from './model'
|
|
4
|
+
import { formatStyle } from './style'
|
|
5
|
+
|
|
6
|
+
/** What the writer puts on `<mxfile>`, so draw.io knows who wrote it. */
|
|
7
|
+
const HOST = 'Pairdraw'
|
|
8
|
+
|
|
9
|
+
function setNumber(element: Element, name: string, value: number) {
|
|
10
|
+
if (value !== 0) element.setAttribute(name, String(value))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function point(document: Document, at: DrawIOPoint, as?: string): Element {
|
|
14
|
+
const element = document.createElement('mxPoint')
|
|
15
|
+
element.setAttribute('x', String(at.x))
|
|
16
|
+
element.setAttribute('y', String(at.y))
|
|
17
|
+
if (as) element.setAttribute('as', as)
|
|
18
|
+
return element
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function geometry(document: Document, value: DrawIOGeometry): Element {
|
|
22
|
+
const element = document.createElement('mxGeometry')
|
|
23
|
+
setNumber(element, 'x', value.x)
|
|
24
|
+
setNumber(element, 'y', value.y)
|
|
25
|
+
setNumber(element, 'width', value.width)
|
|
26
|
+
setNumber(element, 'height', value.height)
|
|
27
|
+
if (value.relative) element.setAttribute('relative', '1')
|
|
28
|
+
element.setAttribute('as', 'geometry')
|
|
29
|
+
if (value.sourcePoint) element.appendChild(point(document, value.sourcePoint, 'sourcePoint'))
|
|
30
|
+
if (value.targetPoint) element.appendChild(point(document, value.targetPoint, 'targetPoint'))
|
|
31
|
+
if (value.points.length > 0) {
|
|
32
|
+
const points = document.createElement('Array')
|
|
33
|
+
points.setAttribute('as', 'points')
|
|
34
|
+
for (const at of value.points) points.appendChild(point(document, at))
|
|
35
|
+
element.appendChild(points)
|
|
36
|
+
}
|
|
37
|
+
if (value.offset) element.appendChild(point(document, value.offset, 'offset'))
|
|
38
|
+
return element
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A cell as draw.io writes it, wrapped in a `UserObject` when it carries attributes. */
|
|
42
|
+
function cellElement(document: Document, cell: DrawIOCell): Element {
|
|
43
|
+
const element = document.createElement('mxCell')
|
|
44
|
+
const wrapped = Object.keys(cell.attributes).length > 0
|
|
45
|
+
if (!wrapped) {
|
|
46
|
+
element.setAttribute('id', cell.id)
|
|
47
|
+
element.setAttribute('value', cell.value)
|
|
48
|
+
}
|
|
49
|
+
const style = formatStyle(cell.style)
|
|
50
|
+
if (style) element.setAttribute('style', style)
|
|
51
|
+
element.setAttribute(cell.kind, '1')
|
|
52
|
+
if (!cell.visible) element.setAttribute('visible', '0')
|
|
53
|
+
element.setAttribute('parent', cell.parent)
|
|
54
|
+
if (cell.source) element.setAttribute('source', cell.source)
|
|
55
|
+
if (cell.target) element.setAttribute('target', cell.target)
|
|
56
|
+
element.appendChild(geometry(document, cell.geometry))
|
|
57
|
+
if (!wrapped) return element
|
|
58
|
+
const wrapper = document.createElement('UserObject')
|
|
59
|
+
wrapper.setAttribute('label', cell.value)
|
|
60
|
+
for (const [name, value] of Object.entries(cell.attributes)) wrapper.setAttribute(name, value)
|
|
61
|
+
wrapper.setAttribute('id', cell.id)
|
|
62
|
+
wrapper.appendChild(element)
|
|
63
|
+
return wrapper
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function pageElement(document: Document, page: DrawIOPage): Element {
|
|
67
|
+
const diagram = document.createElement('diagram')
|
|
68
|
+
diagram.setAttribute('id', page.id)
|
|
69
|
+
diagram.setAttribute('name', page.name)
|
|
70
|
+
const model = document.createElement('mxGraphModel')
|
|
71
|
+
for (const [name, value] of Object.entries(page.attributes)) model.setAttribute(name, value)
|
|
72
|
+
const root = document.createElement('root')
|
|
73
|
+
const rootCell = document.createElement('mxCell')
|
|
74
|
+
rootCell.setAttribute('id', page.rootId)
|
|
75
|
+
root.appendChild(rootCell)
|
|
76
|
+
for (const layer of page.layers) {
|
|
77
|
+
const element = document.createElement('mxCell')
|
|
78
|
+
element.setAttribute('id', layer.id)
|
|
79
|
+
if (layer.name) element.setAttribute('value', layer.name)
|
|
80
|
+
if (layer.locked) element.setAttribute('style', 'locked=1;')
|
|
81
|
+
if (!layer.visible) element.setAttribute('visible', '0')
|
|
82
|
+
element.setAttribute('parent', page.rootId)
|
|
83
|
+
root.appendChild(element)
|
|
84
|
+
}
|
|
85
|
+
for (const cell of page.cells) root.appendChild(cellElement(document, cell))
|
|
86
|
+
model.appendChild(root)
|
|
87
|
+
diagram.appendChild(model)
|
|
88
|
+
return diagram
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A draw.io file as XML draw.io opens: each page a `<diagram>` holding its model uncompressed,
|
|
93
|
+
* as draw.io itself saves by default, so the file diffs and reads as text. A file with pages that
|
|
94
|
+
* could not be read is refused, since writing it would drop them.
|
|
95
|
+
*/
|
|
96
|
+
export function writeDrawIOFile(file: DrawIOFile): string {
|
|
97
|
+
if (file.unreadable.length > 0) {
|
|
98
|
+
throw new Error(`Could not write the draw.io pages ${file.unreadable.join(', ')}`)
|
|
99
|
+
}
|
|
100
|
+
const document = new DOMImplementation().createDocument(null, 'mxfile', null)
|
|
101
|
+
const root = document.documentElement
|
|
102
|
+
if (!root) throw new Error('Could not create a draw.io document')
|
|
103
|
+
root.setAttribute('host', HOST)
|
|
104
|
+
for (const page of file.pages) root.appendChild(pageElement(document, page))
|
|
105
|
+
return new XMLSerializer().serializeToString(document)
|
|
106
|
+
}
|
package/src/xml.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { DOMParser, type Element } from '@xmldom/xmldom'
|
|
2
|
+
|
|
3
|
+
/** The root element of `source`, or null when it is not well-formed XML. */
|
|
4
|
+
export function parseXML(source: string): Element | null {
|
|
5
|
+
try {
|
|
6
|
+
const parsed = new DOMParser({
|
|
7
|
+
onError: (level, message) => {
|
|
8
|
+
if (level !== 'warning') throw new Error(message)
|
|
9
|
+
}
|
|
10
|
+
}).parseFromString(source, 'text/xml')
|
|
11
|
+
return parsed.documentElement
|
|
12
|
+
} catch {
|
|
13
|
+
return null
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The element children of `element`, named `name` when given. */
|
|
18
|
+
export function childElements(element: Element, name?: string): Element[] {
|
|
19
|
+
const found: Element[] = []
|
|
20
|
+
for (let node = element.firstChild; node; node = node.nextSibling) {
|
|
21
|
+
if (node.nodeType === node.ELEMENT_NODE && (!name || (node as Element).localName === name)) {
|
|
22
|
+
found.push(node as Element)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return found
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** All of an element's attributes but those in `except`. */
|
|
29
|
+
export function attributesOf(
|
|
30
|
+
element: Element,
|
|
31
|
+
except: ReadonlySet<string>
|
|
32
|
+
): Record<string, string> {
|
|
33
|
+
const attributes: Record<string, string> = {}
|
|
34
|
+
for (const attribute of Array.from(element.attributes)) {
|
|
35
|
+
if (!except.has(attribute.name)) attributes[attribute.name] = attribute.value
|
|
36
|
+
}
|
|
37
|
+
return attributes
|
|
38
|
+
}
|