@pairdraw/drawio 0.0.0-stage → 0.16.0
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 +30 -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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
//#region src/model.d.ts
|
|
2
|
+
/** A point or offset, in the coordinates of the cell's parent. */
|
|
3
|
+
interface DrawIOPoint {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A cell's `mxGeometry`. A vertex has a box in its parent's coordinates. An edge has its bend
|
|
9
|
+
* `points` and, when an end is not attached, `sourcePoint` and `targetPoint`. A `relative` vertex
|
|
10
|
+
* whose parent is an edge, as draw.io stores an edge label it moved, sits `x` of the way along the
|
|
11
|
+
* edge, from -1 at its source to 1 at its target, `y` across it, and `offset` from there.
|
|
12
|
+
*/
|
|
13
|
+
interface DrawIOGeometry {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
relative: boolean;
|
|
19
|
+
points: DrawIOPoint[];
|
|
20
|
+
sourcePoint: DrawIOPoint | null;
|
|
21
|
+
targetPoint: DrawIOPoint | null;
|
|
22
|
+
offset: DrawIOPoint | null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A `style` string: the names of stylesheet styles it starts from, such as `ellipse` or `text`,
|
|
26
|
+
* and its `key=value` pairs, both in the order written.
|
|
27
|
+
*/
|
|
28
|
+
interface DrawIOStyle {
|
|
29
|
+
names: string[];
|
|
30
|
+
values: Partial<Record<string, string>>;
|
|
31
|
+
}
|
|
32
|
+
/** A shape or a connecting edge, in the order draw.io draws them. */
|
|
33
|
+
interface DrawIOCell {
|
|
34
|
+
id: string;
|
|
35
|
+
/** The cell or layer it belongs to. */
|
|
36
|
+
parent: string;
|
|
37
|
+
kind: 'vertex' | 'edge';
|
|
38
|
+
/** The label: HTML when the style says `html=1`, plain text otherwise. */
|
|
39
|
+
value: string;
|
|
40
|
+
style: DrawIOStyle;
|
|
41
|
+
geometry: DrawIOGeometry;
|
|
42
|
+
visible: boolean;
|
|
43
|
+
source: string | null;
|
|
44
|
+
target: string | null;
|
|
45
|
+
/**
|
|
46
|
+
* Attributes of the `UserObject` or `object` that wraps the cell, such as `tooltip` and `link`,
|
|
47
|
+
* and placeholders it fills in.
|
|
48
|
+
*/
|
|
49
|
+
attributes: Record<string, string>;
|
|
50
|
+
}
|
|
51
|
+
/** One of a page's layers, bottom first: the cells whose parent it is draw on it. */
|
|
52
|
+
interface DrawIOLayer {
|
|
53
|
+
id: string;
|
|
54
|
+
name: string;
|
|
55
|
+
visible: boolean;
|
|
56
|
+
locked: boolean;
|
|
57
|
+
}
|
|
58
|
+
interface DrawIOPage {
|
|
59
|
+
id: string;
|
|
60
|
+
name: string;
|
|
61
|
+
/** The model's own attributes, such as `grid` and `background`. */
|
|
62
|
+
attributes: Record<string, string>;
|
|
63
|
+
/** The id of the cell at the root of the model, which the layers belong to. */
|
|
64
|
+
rootId: string;
|
|
65
|
+
layers: DrawIOLayer[];
|
|
66
|
+
cells: DrawIOCell[];
|
|
67
|
+
}
|
|
68
|
+
/** A draw.io file: its pages, and the names of those that could not be read. */
|
|
69
|
+
interface DrawIOFile {
|
|
70
|
+
pages: DrawIOPage[];
|
|
71
|
+
unreadable: string[];
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/inflate.d.ts
|
|
75
|
+
/**
|
|
76
|
+
* The most a file's compressed parts may unpack to, all together, so a small crafted file with
|
|
77
|
+
* many diagrams or image chunks cannot exhaust memory.
|
|
78
|
+
*/
|
|
79
|
+
declare const MAX_INFLATED_BYTES: number;
|
|
80
|
+
/** What is left of a file's unpacking allowance, shared by every part it unpacks. */
|
|
81
|
+
interface InflateBudget {
|
|
82
|
+
remaining: number;
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/embedded.d.ts
|
|
86
|
+
/** Whether a file name is draw.io's for an editable export, such as `flow.drawio.png`. */
|
|
87
|
+
declare function isDrawIOExportFileName(fileName: string): boolean;
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/read.d.ts
|
|
90
|
+
/**
|
|
91
|
+
* The pages of a draw.io file, whether its diagrams are plain or compressed, with the names of
|
|
92
|
+
* pages it could not read: damaged, or past what the file may unpack to. Null when the source
|
|
93
|
+
* is not a draw.io file.
|
|
94
|
+
*/
|
|
95
|
+
declare function parseDrawIOFile(source: string, budget?: InflateBudget): DrawIOFile | null;
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/style.d.ts
|
|
98
|
+
/** A `style` string as its style names and its pairs. */
|
|
99
|
+
declare function parseStyle(style: string): DrawIOStyle;
|
|
100
|
+
/** A style as draw.io writes it: its names, then `key=value;` pairs, in order. */
|
|
101
|
+
declare function formatStyle({ names, values }: DrawIOStyle): string;
|
|
102
|
+
/** A number the style sets for `key`, or `fallback`. */
|
|
103
|
+
declare function styleNumber(style: DrawIOStyle, key: string, fallback: number): number;
|
|
104
|
+
/** Whether the style turns `key` on, as `key=1` does. */
|
|
105
|
+
declare function styleFlag(style: DrawIOStyle, key: string): boolean;
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/write.d.ts
|
|
108
|
+
/**
|
|
109
|
+
* A draw.io file as XML draw.io opens: each page a `<diagram>` holding its model uncompressed,
|
|
110
|
+
* as draw.io itself saves by default, so the file diffs and reads as text. A file with pages that
|
|
111
|
+
* could not be read is refused, since writing it would drop them.
|
|
112
|
+
*/
|
|
113
|
+
declare function writeDrawIOFile(file: DrawIOFile): string;
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/index.d.ts
|
|
116
|
+
/**
|
|
117
|
+
* The draw.io file in `bytes`: a `.drawio` or `.dio` file, or the diagram a draw.io PNG or SVG
|
|
118
|
+
* export embeds. Null when the bytes hold no draw.io diagram. Everything the file unpacks, its
|
|
119
|
+
* compressed pages and image chunks together, shares one size limit.
|
|
120
|
+
*/
|
|
121
|
+
declare function readDrawIOBytes(bytes: Uint8Array): DrawIOFile | null;
|
|
122
|
+
//#endregion
|
|
123
|
+
export { type DrawIOCell, type DrawIOFile, type DrawIOGeometry, type DrawIOLayer, type DrawIOPage, type DrawIOPoint, type DrawIOStyle, MAX_INFLATED_BYTES, formatStyle, isDrawIOExportFileName, parseDrawIOFile, parseStyle, readDrawIOBytes, styleFlag, styleNumber, writeDrawIOFile };
|
|
124
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/model.ts","../src/inflate.ts","../src/embedded.ts","../src/read.ts","../src/style.ts","../src/write.ts","../src/index.ts"],"mappings":";;UACiB;EACf;EACA;;;;;;;;UASe;EACf;EACA;EACA;EACA;EACA;EACA,QAAQ;EACR,aAAa;EACb,aAAa;EACb,QAAQ;;;;;;UAOO;EACf;EACA,QAAQ,QAAQ;;;UAID;EACf;;EAEA;EACA;;EAEA;EACA,OAAO;EACP,UAAU;EACV;EACA;EACA;;;;;EAKA,YAAY;;;UAIG;EACf;EACA;EACA;EACA;;UAGe;EACf;EACA;;EAEA,YAAY;;EAEZ;EACA,QAAQ;EACR,OAAO;;;UAIQ;EACf,OAAO;EACP;;;;;;;;cCrEW;;UAKI;EACf;;;;;iBCkFc,uBAAuB;;;;;;;;iBCoEvB,gBAAgB,gBAAgB,SAAM,gBAAqB;;;;iBC/J3D,WAAW,gBAAgB;;iBAa3B,cAAc,OAAO,UAAU;;iBAQ/B,YAAY,OAAO,aAAa,aAAa;;iBAM7C,UAAU,OAAO,aAAa;;;;;;;;iBCiE9B,gBAAgB,MAAM;;;;;;;;iBCtEtB,gBAAgB,OAAO,aAAa"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { MAX_INFLATED_BYTES, isDrawIOExportFileName } from "./chunks/embedded.js";
|
|
2
|
+
import { formatStyle, parseDrawIOFile, parseStyle, readDrawIOBytes, styleFlag, styleNumber, writeDrawIOFile } from "./chunks/index.js";
|
|
3
|
+
export { MAX_INFLATED_BYTES, formatStyle, isDrawIOExportFileName, parseDrawIOFile, parseStyle, readDrawIOBytes, styleFlag, styleNumber, writeDrawIOFile };
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pairdraw/drawio",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.16.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"src",
|
|
8
|
+
"README.md"
|
|
9
|
+
],
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@xmldom/xmldom": "^0.9.12",
|
|
14
|
+
"fflate": "^0.8.3",
|
|
15
|
+
"image-size": "^2.0.4",
|
|
16
|
+
"js-base64": "^3.9.3"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/BadryansahBangsawan/pairdraw.git",
|
|
30
|
+
"directory": "packages/drawio"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/embedded.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { Element } from '@xmldom/xmldom'
|
|
2
|
+
import { imageSize } from 'image-size'
|
|
3
|
+
|
|
4
|
+
import { boundedInflate, percentDecoded, type InflateBudget } from './inflate'
|
|
5
|
+
import { parseXML } from './xml'
|
|
6
|
+
|
|
7
|
+
/** A PNG's chunks start after its eight-byte signature. */
|
|
8
|
+
const PNG_SIGNATURE_LENGTH = 8
|
|
9
|
+
/** The keyword draw.io stores its diagram under in a PNG's text chunks. */
|
|
10
|
+
const DRAWIO_KEYWORD = 'mxfile'
|
|
11
|
+
|
|
12
|
+
const latin1 = new TextDecoder('latin1')
|
|
13
|
+
const utf8 = new TextDecoder()
|
|
14
|
+
|
|
15
|
+
function isPNG(bytes: Uint8Array): boolean {
|
|
16
|
+
try {
|
|
17
|
+
return imageSize(bytes).type === 'png'
|
|
18
|
+
} catch {
|
|
19
|
+
return false
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Where a NUL byte ends the field that starts at `from`, or -1. */
|
|
24
|
+
function fieldEnd(data: Uint8Array, from: number): number {
|
|
25
|
+
return data.indexOf(0, from)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The diagram a text chunk holds when its keyword is draw.io's: `tEXt` as Latin-1, `zTXt`
|
|
30
|
+
* zlib-compressed, `iTXt` as UTF-8, compressed or not. draw.io percent-encodes the XML in all three.
|
|
31
|
+
*/
|
|
32
|
+
function chunkDiagram(type: string, data: Uint8Array, budget: InflateBudget): string | null {
|
|
33
|
+
const keywordEnd = fieldEnd(data, 0)
|
|
34
|
+
if (keywordEnd === -1 || latin1.decode(data.subarray(0, keywordEnd)) !== DRAWIO_KEYWORD)
|
|
35
|
+
return null
|
|
36
|
+
if (type === 'tEXt') return percentDecoded(latin1.decode(data.subarray(keywordEnd + 1)))
|
|
37
|
+
if (type === 'zTXt') {
|
|
38
|
+
// The compression method byte, then a zlib stream: a 2-byte header before raw deflate.
|
|
39
|
+
const inflated = boundedInflate(data.subarray(keywordEnd + 4), budget)
|
|
40
|
+
return percentDecoded(latin1.decode(inflated))
|
|
41
|
+
}
|
|
42
|
+
// iTXt: compressed flag and method, then language and translated keyword, each NUL-ended.
|
|
43
|
+
const compressed = data[keywordEnd + 1] === 1
|
|
44
|
+
const languageEnd = fieldEnd(data, keywordEnd + 3)
|
|
45
|
+
const translatedEnd = languageEnd === -1 ? -1 : fieldEnd(data, languageEnd + 1)
|
|
46
|
+
if (translatedEnd === -1) return null
|
|
47
|
+
const text = data.subarray(translatedEnd + 1)
|
|
48
|
+
return percentDecoded(utf8.decode(compressed ? boundedInflate(text.subarray(2), budget) : text))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The draw.io diagram a PNG export carries, or null when it carries none. A compressed chunk that
|
|
53
|
+
* is damaged or too large to unpack is an error that says so.
|
|
54
|
+
*/
|
|
55
|
+
function diagramInPNG(bytes: Uint8Array, budget: InflateBudget): string | null {
|
|
56
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
57
|
+
let offset = PNG_SIGNATURE_LENGTH
|
|
58
|
+
while (offset + 8 <= bytes.length) {
|
|
59
|
+
const length = view.getUint32(offset)
|
|
60
|
+
const type = latin1.decode(bytes.subarray(offset + 4, offset + 8))
|
|
61
|
+
const start = offset + 8
|
|
62
|
+
if (type === 'IEND') return null
|
|
63
|
+
if (start + length > bytes.length) {
|
|
64
|
+
throw new Error('Could not read the draw.io diagram in this image: it is cut short')
|
|
65
|
+
}
|
|
66
|
+
if (type === 'tEXt' || type === 'zTXt' || type === 'iTXt') {
|
|
67
|
+
let diagram: string | null
|
|
68
|
+
try {
|
|
69
|
+
diagram = chunkDiagram(type, bytes.subarray(start, start + length), budget)
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new Error('Could not read the draw.io diagram in this image', { cause: error })
|
|
72
|
+
}
|
|
73
|
+
if (diagram) return diagram
|
|
74
|
+
}
|
|
75
|
+
// The chunk's data, then its 4-byte CRC.
|
|
76
|
+
offset = start + length + 4
|
|
77
|
+
}
|
|
78
|
+
return null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The root element of the draw.io XML a file holds: the file itself for `.drawio` and `.dio`, or
|
|
83
|
+
* the diagram a PNG export embeds in a text chunk or an SVG export in its root's `content`. Null
|
|
84
|
+
* when the file is not XML or an export embeds no diagram.
|
|
85
|
+
*/
|
|
86
|
+
export function drawioRoot(bytes: Uint8Array, budget: InflateBudget): Element | null {
|
|
87
|
+
const source = isPNG(bytes) ? diagramInPNG(bytes, budget) : utf8.decode(bytes)
|
|
88
|
+
const root = source === null ? null : parseXML(source)
|
|
89
|
+
if (root?.localName !== 'svg') return root
|
|
90
|
+
const content = root.getAttribute('content')
|
|
91
|
+
return content ? parseXML(content) : null
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Whether a file name is draw.io's for an editable export, such as `flow.drawio.png`. */
|
|
95
|
+
export function isDrawIOExportFileName(fileName: string): boolean {
|
|
96
|
+
const name = fileName.toLowerCase()
|
|
97
|
+
return name.endsWith('.drawio.png') || name.endsWith('.drawio.svg')
|
|
98
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { drawioRoot } from './embedded'
|
|
2
|
+
import { inflateBudget } from './inflate'
|
|
3
|
+
import type { DrawIOFile } from './model'
|
|
4
|
+
import { readDrawIORoot } from './read'
|
|
5
|
+
|
|
6
|
+
export { isDrawIOExportFileName } from './embedded'
|
|
7
|
+
export { MAX_INFLATED_BYTES } from './inflate'
|
|
8
|
+
export type {
|
|
9
|
+
DrawIOCell,
|
|
10
|
+
DrawIOFile,
|
|
11
|
+
DrawIOGeometry,
|
|
12
|
+
DrawIOLayer,
|
|
13
|
+
DrawIOPage,
|
|
14
|
+
DrawIOPoint,
|
|
15
|
+
DrawIOStyle
|
|
16
|
+
} from './model'
|
|
17
|
+
export { parseDrawIOFile } from './read'
|
|
18
|
+
export { formatStyle, parseStyle, styleFlag, styleNumber } from './style'
|
|
19
|
+
export { writeDrawIOFile } from './write'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The draw.io file in `bytes`: a `.drawio` or `.dio` file, or the diagram a draw.io PNG or SVG
|
|
23
|
+
* export embeds. Null when the bytes hold no draw.io diagram. Everything the file unpacks, its
|
|
24
|
+
* compressed pages and image chunks together, shares one size limit.
|
|
25
|
+
*/
|
|
26
|
+
export function readDrawIOBytes(bytes: Uint8Array): DrawIOFile | null {
|
|
27
|
+
const budget = inflateBudget()
|
|
28
|
+
const root = drawioRoot(bytes, budget)
|
|
29
|
+
return root ? readDrawIORoot(root, budget) : null
|
|
30
|
+
}
|
package/src/inflate.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Inflate } from 'fflate'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The most a file's compressed parts may unpack to, all together, so a small crafted file with
|
|
5
|
+
* many diagrams or image chunks cannot exhaust memory.
|
|
6
|
+
*/
|
|
7
|
+
export const MAX_INFLATED_BYTES = 32 * 1024 * 1024
|
|
8
|
+
/** Input fed per step; deflate expands at most about 1032 times, so each step stays small. */
|
|
9
|
+
const STEP_BYTES = 4096
|
|
10
|
+
|
|
11
|
+
/** What is left of a file's unpacking allowance, shared by every part it unpacks. */
|
|
12
|
+
export interface InflateBudget {
|
|
13
|
+
remaining: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function inflateBudget(bytes = MAX_INFLATED_BYTES): InflateBudget {
|
|
17
|
+
return { remaining: bytes }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Raw deflate, charged to `budget`; throws once the file would unpack to more than it allows. */
|
|
21
|
+
export function boundedInflate(data: Uint8Array, budget: InflateBudget): Uint8Array {
|
|
22
|
+
const chunks: Uint8Array[] = []
|
|
23
|
+
let size = 0
|
|
24
|
+
const inflater = new Inflate((chunk) => {
|
|
25
|
+
size += chunk.length
|
|
26
|
+
budget.remaining -= chunk.length
|
|
27
|
+
if (budget.remaining < 0) throw new RangeError('The draw.io file unpacks to too much data')
|
|
28
|
+
chunks.push(chunk)
|
|
29
|
+
})
|
|
30
|
+
for (let start = 0; start < data.length; start += STEP_BYTES) {
|
|
31
|
+
const end = start + STEP_BYTES
|
|
32
|
+
inflater.push(data.subarray(start, end), end >= data.length)
|
|
33
|
+
}
|
|
34
|
+
const output = new Uint8Array(size)
|
|
35
|
+
let offset = 0
|
|
36
|
+
for (const chunk of chunks) {
|
|
37
|
+
output.set(chunk, offset)
|
|
38
|
+
offset += chunk.length
|
|
39
|
+
}
|
|
40
|
+
return output
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A `%` that does not start an escape, as in "50% done". */
|
|
44
|
+
const STRAY_PERCENT = /%(?![0-9A-Fa-f]{2})/g
|
|
45
|
+
/** A run of escapes, decoded together so a character spread over several bytes stays whole. */
|
|
46
|
+
const ESCAPE_RUN = /(?:%[0-9A-Fa-f]{2})+/g
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The text with its percent escapes decoded. A stray `%` is kept as it is, and an escape run that
|
|
50
|
+
* is not valid UTF-8 stays encoded, so neither stops the rest of the text from decoding.
|
|
51
|
+
*/
|
|
52
|
+
export function percentDecoded(text: string): string {
|
|
53
|
+
const escaped = text.replace(STRAY_PERCENT, '%25')
|
|
54
|
+
try {
|
|
55
|
+
return decodeURIComponent(escaped)
|
|
56
|
+
} catch {
|
|
57
|
+
return escaped.replace(ESCAPE_RUN, (run) => {
|
|
58
|
+
try {
|
|
59
|
+
return decodeURIComponent(run)
|
|
60
|
+
} catch {
|
|
61
|
+
return run
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/model.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** A point or offset, in the coordinates of the cell's parent. */
|
|
2
|
+
export interface DrawIOPoint {
|
|
3
|
+
x: number
|
|
4
|
+
y: number
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A cell's `mxGeometry`. A vertex has a box in its parent's coordinates. An edge has its bend
|
|
9
|
+
* `points` and, when an end is not attached, `sourcePoint` and `targetPoint`. A `relative` vertex
|
|
10
|
+
* whose parent is an edge, as draw.io stores an edge label it moved, sits `x` of the way along the
|
|
11
|
+
* edge, from -1 at its source to 1 at its target, `y` across it, and `offset` from there.
|
|
12
|
+
*/
|
|
13
|
+
export interface DrawIOGeometry {
|
|
14
|
+
x: number
|
|
15
|
+
y: number
|
|
16
|
+
width: number
|
|
17
|
+
height: number
|
|
18
|
+
relative: boolean
|
|
19
|
+
points: DrawIOPoint[]
|
|
20
|
+
sourcePoint: DrawIOPoint | null
|
|
21
|
+
targetPoint: DrawIOPoint | null
|
|
22
|
+
offset: DrawIOPoint | null
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A `style` string: the names of stylesheet styles it starts from, such as `ellipse` or `text`,
|
|
27
|
+
* and its `key=value` pairs, both in the order written.
|
|
28
|
+
*/
|
|
29
|
+
export interface DrawIOStyle {
|
|
30
|
+
names: string[]
|
|
31
|
+
values: Partial<Record<string, string>>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A shape or a connecting edge, in the order draw.io draws them. */
|
|
35
|
+
export interface DrawIOCell {
|
|
36
|
+
id: string
|
|
37
|
+
/** The cell or layer it belongs to. */
|
|
38
|
+
parent: string
|
|
39
|
+
kind: 'vertex' | 'edge'
|
|
40
|
+
/** The label: HTML when the style says `html=1`, plain text otherwise. */
|
|
41
|
+
value: string
|
|
42
|
+
style: DrawIOStyle
|
|
43
|
+
geometry: DrawIOGeometry
|
|
44
|
+
visible: boolean
|
|
45
|
+
source: string | null
|
|
46
|
+
target: string | null
|
|
47
|
+
/**
|
|
48
|
+
* Attributes of the `UserObject` or `object` that wraps the cell, such as `tooltip` and `link`,
|
|
49
|
+
* and placeholders it fills in.
|
|
50
|
+
*/
|
|
51
|
+
attributes: Record<string, string>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** One of a page's layers, bottom first: the cells whose parent it is draw on it. */
|
|
55
|
+
export interface DrawIOLayer {
|
|
56
|
+
id: string
|
|
57
|
+
name: string
|
|
58
|
+
visible: boolean
|
|
59
|
+
locked: boolean
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface DrawIOPage {
|
|
63
|
+
id: string
|
|
64
|
+
name: string
|
|
65
|
+
/** The model's own attributes, such as `grid` and `background`. */
|
|
66
|
+
attributes: Record<string, string>
|
|
67
|
+
/** The id of the cell at the root of the model, which the layers belong to. */
|
|
68
|
+
rootId: string
|
|
69
|
+
layers: DrawIOLayer[]
|
|
70
|
+
cells: DrawIOCell[]
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A draw.io file: its pages, and the names of those that could not be read. */
|
|
74
|
+
export interface DrawIOFile {
|
|
75
|
+
pages: DrawIOPage[]
|
|
76
|
+
unreadable: string[]
|
|
77
|
+
}
|
package/src/read.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import type { Element } from '@xmldom/xmldom'
|
|
2
|
+
import { isValid, toUint8Array } from 'js-base64'
|
|
3
|
+
|
|
4
|
+
import { boundedInflate, inflateBudget, percentDecoded, type InflateBudget } from './inflate'
|
|
5
|
+
import type {
|
|
6
|
+
DrawIOCell,
|
|
7
|
+
DrawIOFile,
|
|
8
|
+
DrawIOGeometry,
|
|
9
|
+
DrawIOLayer,
|
|
10
|
+
DrawIOPage,
|
|
11
|
+
DrawIOPoint
|
|
12
|
+
} from './model'
|
|
13
|
+
import { parseStyle, styleFlag } from './style'
|
|
14
|
+
import { attributesOf, childElements, parseXML } from './xml'
|
|
15
|
+
|
|
16
|
+
/** What a cell wrapper keeps for itself rather than as the cell's attributes. */
|
|
17
|
+
const WRAPPER_OWN = new Set(['id', 'label'])
|
|
18
|
+
|
|
19
|
+
function number(element: Element, name: string, fallback = 0): number {
|
|
20
|
+
const value = Number.parseFloat(element.getAttribute(name) ?? '')
|
|
21
|
+
return Number.isFinite(value) ? value : fallback
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function pointOf(element: Element): DrawIOPoint {
|
|
25
|
+
return { x: number(element, 'x'), y: number(element, 'y') }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const NO_GEOMETRY: DrawIOGeometry = {
|
|
29
|
+
x: 0,
|
|
30
|
+
y: 0,
|
|
31
|
+
width: 0,
|
|
32
|
+
height: 0,
|
|
33
|
+
relative: false,
|
|
34
|
+
points: [],
|
|
35
|
+
sourcePoint: null,
|
|
36
|
+
targetPoint: null,
|
|
37
|
+
offset: null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function readGeometry(cell: Element): DrawIOGeometry {
|
|
41
|
+
const geometry = childElements(cell, 'mxGeometry').at(0)
|
|
42
|
+
if (!geometry) return structuredClone(NO_GEOMETRY)
|
|
43
|
+
const points = childElements(geometry, 'Array').find((e) => e.getAttribute('as') === 'points')
|
|
44
|
+
const point = (as: string) => {
|
|
45
|
+
const found = childElements(geometry, 'mxPoint').find((p) => p.getAttribute('as') === as)
|
|
46
|
+
return found ? pointOf(found) : null
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
x: number(geometry, 'x'),
|
|
50
|
+
y: number(geometry, 'y'),
|
|
51
|
+
width: number(geometry, 'width'),
|
|
52
|
+
height: number(geometry, 'height'),
|
|
53
|
+
relative: geometry.getAttribute('relative') === '1',
|
|
54
|
+
points: points ? childElements(points, 'mxPoint').map(pointOf) : [],
|
|
55
|
+
sourcePoint: point('sourcePoint'),
|
|
56
|
+
targetPoint: point('targetPoint'),
|
|
57
|
+
offset: point('offset')
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A cell element, and the `UserObject` or `object` around it when there is one. */
|
|
62
|
+
interface CellElements {
|
|
63
|
+
cell: Element
|
|
64
|
+
wrapper: Element | null
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function cellElements(element: Element): CellElements | null {
|
|
68
|
+
if (element.localName === 'mxCell') return { cell: element, wrapper: null }
|
|
69
|
+
if (element.localName !== 'UserObject' && element.localName !== 'object') return null
|
|
70
|
+
const cell = childElements(element, 'mxCell').at(0)
|
|
71
|
+
return cell ? { cell, wrapper: element } : null
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function readCell({ cell, wrapper }: CellElements, id: string, parent: string): DrawIOCell {
|
|
75
|
+
const isEdge = cell.getAttribute('edge') === '1'
|
|
76
|
+
return {
|
|
77
|
+
id,
|
|
78
|
+
parent,
|
|
79
|
+
kind: isEdge ? 'edge' : 'vertex',
|
|
80
|
+
value: (wrapper ? wrapper.getAttribute('label') : cell.getAttribute('value')) ?? '',
|
|
81
|
+
style: parseStyle(cell.getAttribute('style') ?? ''),
|
|
82
|
+
geometry: readGeometry(cell),
|
|
83
|
+
visible: cell.getAttribute('visible') !== '0',
|
|
84
|
+
source: cell.getAttribute('source'),
|
|
85
|
+
target: cell.getAttribute('target'),
|
|
86
|
+
attributes: wrapper ? attributesOf(wrapper, WRAPPER_OWN) : {}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A model's cells. Its root is the one cell without a parent, and its layers are the cells under
|
|
92
|
+
* the root that are neither shapes nor edges; draw.io names them `0` and `1` by default.
|
|
93
|
+
*/
|
|
94
|
+
function readModel(model: Element, page: Pick<DrawIOPage, 'id' | 'name'>): DrawIOPage | null {
|
|
95
|
+
const root = childElements(model, 'root').at(0)
|
|
96
|
+
if (!root) return null
|
|
97
|
+
const read: { elements: CellElements; id: string; parent: string | null }[] = []
|
|
98
|
+
for (const element of childElements(root)) {
|
|
99
|
+
const elements = cellElements(element)
|
|
100
|
+
const id = (elements?.wrapper ?? elements?.cell)?.getAttribute('id')
|
|
101
|
+
if (elements && id) read.push({ elements, id, parent: elements.cell.getAttribute('parent') })
|
|
102
|
+
}
|
|
103
|
+
const rootId = read.find((entry) => !entry.parent)?.id
|
|
104
|
+
if (rootId === undefined) return null
|
|
105
|
+
const layers: DrawIOLayer[] = []
|
|
106
|
+
const cells: DrawIOCell[] = []
|
|
107
|
+
for (const { elements, id, parent } of read) {
|
|
108
|
+
if (!parent) continue
|
|
109
|
+
const { cell } = elements
|
|
110
|
+
const shown = cell.getAttribute('vertex') === '1' || cell.getAttribute('edge') === '1'
|
|
111
|
+
if (!shown && parent === rootId) {
|
|
112
|
+
layers.push({
|
|
113
|
+
id,
|
|
114
|
+
name: cell.getAttribute('value') ?? '',
|
|
115
|
+
visible: cell.getAttribute('visible') !== '0',
|
|
116
|
+
locked: styleFlag(parseStyle(cell.getAttribute('style') ?? ''), 'locked')
|
|
117
|
+
})
|
|
118
|
+
} else if (shown) {
|
|
119
|
+
cells.push(readCell(elements, id, parent))
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
...page,
|
|
124
|
+
attributes: attributesOf(model, new Set()),
|
|
125
|
+
rootId,
|
|
126
|
+
layers,
|
|
127
|
+
cells
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The XML inside a compressed `<diagram>`: Base64, raw deflate, then percent-encoded text. */
|
|
132
|
+
function inflateDiagram(text: string, budget: InflateBudget): string | null {
|
|
133
|
+
const encoded = text.trim()
|
|
134
|
+
if (!encoded || !isValid(encoded)) return null
|
|
135
|
+
try {
|
|
136
|
+
return percentDecoded(new TextDecoder().decode(boundedInflate(toUint8Array(encoded), budget)))
|
|
137
|
+
} catch {
|
|
138
|
+
return null
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function readDiagram(
|
|
143
|
+
diagram: Element,
|
|
144
|
+
index: number,
|
|
145
|
+
budget: InflateBudget
|
|
146
|
+
): DrawIOPage | { unreadable: string } {
|
|
147
|
+
const name = diagram.getAttribute('name') || `Page ${index + 1}`
|
|
148
|
+
const page = { id: diagram.getAttribute('id') ?? `page-${index + 1}`, name }
|
|
149
|
+
let model = childElements(diagram, 'mxGraphModel').at(0) ?? null
|
|
150
|
+
if (!model) {
|
|
151
|
+
const xml = inflateDiagram(diagram.textContent ?? '', budget)
|
|
152
|
+
const root = xml ? parseXML(xml) : null
|
|
153
|
+
model = root?.localName === 'mxGraphModel' ? root : null
|
|
154
|
+
}
|
|
155
|
+
return (model ? readModel(model, page) : null) ?? { unreadable: name }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The pages of a draw.io file, whether its diagrams are plain or compressed, with the names of
|
|
160
|
+
* pages it could not read: damaged, or past what the file may unpack to. Null when the source
|
|
161
|
+
* is not a draw.io file.
|
|
162
|
+
*/
|
|
163
|
+
export function parseDrawIOFile(source: string, budget = inflateBudget()): DrawIOFile | null {
|
|
164
|
+
const root = parseXML(source)
|
|
165
|
+
return root ? readDrawIORoot(root, budget) : null
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The pages of a draw.io document from its parsed root, as `parseDrawIOFile` reads them. */
|
|
169
|
+
export function readDrawIORoot(root: Element, budget: InflateBudget): DrawIOFile | null {
|
|
170
|
+
if (root.localName === 'mxGraphModel') {
|
|
171
|
+
const page = readModel(root, { id: 'page-1', name: 'Page 1' })
|
|
172
|
+
return page ? { pages: [page], unreadable: [] } : null
|
|
173
|
+
}
|
|
174
|
+
if (root.localName !== 'mxfile') return null
|
|
175
|
+
const file: DrawIOFile = { pages: [], unreadable: [] }
|
|
176
|
+
for (const [index, diagram] of childElements(root, 'diagram').entries()) {
|
|
177
|
+
const read = readDiagram(diagram, index, budget)
|
|
178
|
+
if ('unreadable' in read) file.unreadable.push(read.unreadable)
|
|
179
|
+
else file.pages.push(read)
|
|
180
|
+
}
|
|
181
|
+
return file
|
|
182
|
+
}
|
package/src/style.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { DrawIOStyle } from './model'
|
|
2
|
+
|
|
3
|
+
/** A `style` string as its style names and its pairs. */
|
|
4
|
+
export function parseStyle(style: string): DrawIOStyle {
|
|
5
|
+
const result: DrawIOStyle = { names: [], values: {} }
|
|
6
|
+
for (const part of style.split(';')) {
|
|
7
|
+
const text = part.trim()
|
|
8
|
+
if (!text) continue
|
|
9
|
+
const eq = text.indexOf('=')
|
|
10
|
+
if (eq === -1) result.names.push(text)
|
|
11
|
+
else result.values[text.slice(0, eq)] = text.slice(eq + 1)
|
|
12
|
+
}
|
|
13
|
+
return result
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A style as draw.io writes it: its names, then `key=value;` pairs, in order. */
|
|
17
|
+
export function formatStyle({ names, values }: DrawIOStyle): string {
|
|
18
|
+
const pairs = Object.entries(values).flatMap(([key, value]) =>
|
|
19
|
+
value === undefined ? [] : [`${key}=${value}`]
|
|
20
|
+
)
|
|
21
|
+
return [...names, ...pairs].map((part) => `${part};`).join('')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A number the style sets for `key`, or `fallback`. */
|
|
25
|
+
export function styleNumber(style: DrawIOStyle, key: string, fallback: number): number {
|
|
26
|
+
const value = Number.parseFloat(style.values[key] ?? '')
|
|
27
|
+
return Number.isFinite(value) ? value : fallback
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Whether the style turns `key` on, as `key=1` does. */
|
|
31
|
+
export function styleFlag(style: DrawIOStyle, key: string): boolean {
|
|
32
|
+
return style.values[key] === '1'
|
|
33
|
+
}
|