@senad-d/drawme 0.1.11 → 0.3.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/CHANGELOG.md +20 -1
- package/README.md +54 -57
- package/SECURITY.md +1 -1
- package/assets/references/diagram-types.md +52 -26
- package/assets/references/mermaid-authoring.md +18 -9
- package/assets/references/style-presets.md +12 -13
- package/assets/references/troubleshooting.md +5 -27
- package/assets/references/xml-authoring.md +25 -25
- package/docs/diagrams/drawme-architecture-overview.drawio +52 -0
- package/docs/diagrams/drawme-architecture-overview.png +0 -0
- package/docs/reference.md +83 -77
- package/docs/testing.md +18 -0
- package/example/README.md +9 -9
- package/example/drawme-architecture.drawio +5 -5
- package/example/drawme-how-it-works.drawio +7 -7
- package/example/drawme-installation-guide.drawio +6 -6
- package/package.json +7 -3
- package/src/animate.ts +124 -88
- package/src/animation.ts +105 -109
- package/src/diagram-files.ts +159 -0
- package/src/dom.ts +5 -0
- package/src/drawio.ts +324 -566
- package/src/editable-import.ts +228 -0
- package/src/explain.ts +81 -88
- package/src/export-plan.ts +175 -0
- package/src/fitcanvas.ts +29 -25
- package/src/geometry.ts +68 -29
- package/src/index.ts +201 -142
- package/src/motion.ts +251 -163
- package/src/png.ts +233 -34
- package/src/renderer.ts +361 -0
- package/src/shapesearch.ts +33 -32
- package/src/storyboard.ts +41 -26
- package/src/svg-helpers.ts +129 -0
- package/src/svgexport.ts +6 -9
- package/src/validate.ts +686 -292
- package/src/workflow.ts +63 -48
- package/docs/visual-review-implementation-tasks.md +0 -271
package/src/fitcanvas.ts
CHANGED
|
@@ -5,8 +5,9 @@
|
|
|
5
5
|
* oversized ones without touching element sizes or relative positions. No
|
|
6
6
|
* draw.io CLI involved.
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { writeFile } from "node:fs/promises";
|
|
9
9
|
import { DOMParser, XMLSerializer } from "@xmldom/xmldom";
|
|
10
|
+
import { readDiagramXml, withDiagramFiles } from "./diagram-files";
|
|
10
11
|
import { type DomEl, attr, childrenByTag, directText, firstByTag } from "./dom";
|
|
11
12
|
import { type ById, collectCellsWithIds, contentBounds, geometryIsRelative, pyFloat } from "./geometry";
|
|
12
13
|
|
|
@@ -144,29 +145,32 @@ function fitPage(diagram: DomEl, margin: number): FitPageResult {
|
|
|
144
145
|
export async function fitCanvasFile(options: FitCanvasOptions): Promise<FitCanvasResult> {
|
|
145
146
|
const margin = options.margin ?? DEFAULT_FIT_MARGIN;
|
|
146
147
|
if (!Number.isFinite(margin) || margin < 0) throw new Error(`invalid margin ${String(options.margin)}`);
|
|
147
|
-
const
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
pages
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
148
|
+
const { input } = options;
|
|
149
|
+
const output = options.output ?? input;
|
|
150
|
+
return withDiagramFiles([input, output], undefined, async () => {
|
|
151
|
+
const xml = await readDiagramXml(input);
|
|
152
|
+
const doc = new DOMParser().parseFromString(xml, "text/xml");
|
|
153
|
+
const root = doc.documentElement as unknown as DomEl | null;
|
|
154
|
+
if (!root) throw new Error("no root element (malformed XML)");
|
|
155
|
+
|
|
156
|
+
const diagrams = childrenByTag(root, "diagram");
|
|
157
|
+
let pages: FitPageResult[];
|
|
158
|
+
if (diagrams.length > 0) {
|
|
159
|
+
pages = diagrams.map((diagram) => fitPage(diagram, margin));
|
|
160
|
+
} else if (root.tagName === "mxGraphModel") {
|
|
161
|
+
pages = [fitModel("?", root, margin)];
|
|
162
|
+
} else {
|
|
163
|
+
pages = [{ page: "?", status: "skipped", reason: "no <diagram> pages and root is not <mxGraphModel>" }];
|
|
164
|
+
}
|
|
162
165
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
166
|
+
const changed = pages.some((page) => page.status === "fitted");
|
|
167
|
+
if (changed || output !== input) {
|
|
168
|
+
let serialized = new XMLSerializer().serializeToString(doc as never);
|
|
169
|
+
const declaration = /^<\?xml[^>]*\?>/.exec(xml)?.[0];
|
|
170
|
+
if (declaration && !serialized.startsWith("<?xml")) serialized = `${declaration}\n${serialized}`;
|
|
171
|
+
if (xml.endsWith("\n") && !serialized.endsWith("\n")) serialized += "\n";
|
|
172
|
+
await writeFile(output, serialized, "utf8");
|
|
173
|
+
}
|
|
174
|
+
return { input, output, margin, changed, pages };
|
|
175
|
+
});
|
|
172
176
|
}
|
package/src/geometry.ts
CHANGED
|
@@ -139,33 +139,67 @@ export function edgeRoute(edge: DomEl, byId: ById): Point[] | null {
|
|
|
139
139
|
return [source, ...waypoints.map(([x, y]): Point => [x + ox, y + oy]), target];
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
|
|
143
|
-
export function routePointAt(points: Point[], fraction: number): Point {
|
|
142
|
+
function pointAlongRoute(points: Point[], fraction: number, normal: number): Point | null {
|
|
144
143
|
const lengths: number[] = [];
|
|
145
144
|
let total = 0;
|
|
145
|
+
let lastSegment = -1;
|
|
146
146
|
for (let i = 0; i < points.length - 1; i++) {
|
|
147
147
|
const length = Math.hypot(points[i + 1][0] - points[i][0], points[i + 1][1] - points[i][1]);
|
|
148
148
|
lengths.push(length);
|
|
149
149
|
total += length;
|
|
150
|
+
if (length > 0) lastSegment = i;
|
|
150
151
|
}
|
|
152
|
+
if (!Number.isFinite(total)) return null;
|
|
153
|
+
|
|
151
154
|
let remaining = total * fraction;
|
|
152
|
-
for (
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
];
|
|
155
|
+
for (const [i, length] of lengths.entries()) {
|
|
156
|
+
if (length === 0) continue;
|
|
157
|
+
// At a bend use the outgoing direction; at the end use the last nonzero segment.
|
|
158
|
+
if (remaining < length || i === lastSegment) {
|
|
159
|
+
const [x, y] = points[i];
|
|
160
|
+
const dx = points[i + 1][0] - x;
|
|
161
|
+
const dy = points[i + 1][1] - y;
|
|
162
|
+
const progress = Math.min(1, remaining / length);
|
|
163
|
+
return [x + progress * dx + normal * (dy / length), y + progress * dy - normal * (dx / length)];
|
|
159
164
|
}
|
|
160
|
-
remaining -=
|
|
165
|
+
remaining -= length;
|
|
161
166
|
}
|
|
162
|
-
return points.at(-1) ??
|
|
167
|
+
return points.at(-1) ?? null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Point at `fraction` (0..1) of the route's arc length. */
|
|
171
|
+
export function routePointAt(points: Point[], fraction: number): Point {
|
|
172
|
+
return pointAlongRoute(points, fraction, 0) ?? [0, 0];
|
|
163
173
|
}
|
|
164
174
|
|
|
165
175
|
export function routeMidpoint(points: Point[]): Point {
|
|
166
176
|
return routePointAt(points, 0.5);
|
|
167
177
|
}
|
|
168
178
|
|
|
179
|
+
function geometryPoint(geometry: DomEl | null, kind: string): DomEl | undefined {
|
|
180
|
+
return geometry ? childrenByTag(geometry, "mxPoint").find((point) => attr(point, "as") === kind) : undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function pointCoordinates(point: DomEl): Point | null {
|
|
184
|
+
const x = pyFloat(attr(point, "x") ?? "0");
|
|
185
|
+
const y = pyFloat(attr(point, "y") ?? "0");
|
|
186
|
+
return x === undefined || y === undefined || hasInvalidNumber([x, y]) ? null : [x, y];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Label anchor: x runs from -1 to 1 along the route, y follows its signed normal, then offset is absolute. */
|
|
190
|
+
export function routeLabelPoint(cell: DomEl, route: Point[]): Point | null {
|
|
191
|
+
const geometry = firstByTag(cell, "mxGeometry");
|
|
192
|
+
const x = geometry ? pyFloat(attr(geometry, "x") ?? "0") : 0;
|
|
193
|
+
const y = geometry ? pyFloat(attr(geometry, "y") ?? "0") : 0;
|
|
194
|
+
if (x === undefined || y === undefined || hasInvalidNumber([x, y])) return null;
|
|
195
|
+
const offsetNode = geometryPoint(geometry, "offset");
|
|
196
|
+
const offset: Point | null = offsetNode ? pointCoordinates(offsetNode) : [0, 0];
|
|
197
|
+
const anchor = pointAlongRoute(route, (Math.min(1, Math.max(-1, x)) + 1) / 2, y);
|
|
198
|
+
if (!offset || !anchor) return null;
|
|
199
|
+
const point: Point = [anchor[0] + offset[0], anchor[1] + offset[1]];
|
|
200
|
+
return hasInvalidNumber(point) ? null : point;
|
|
201
|
+
}
|
|
202
|
+
|
|
169
203
|
/** Conservative box for a separately sized label on an explicitly routed edge. */
|
|
170
204
|
export function explicitEdgeLabelRect(cell: DomEl, byId: ById): Rect | null {
|
|
171
205
|
if (!isEdgeLabel(cell)) return null;
|
|
@@ -174,21 +208,13 @@ export function explicitEdgeLabelRect(cell: DomEl, byId: ById): Rect | null {
|
|
|
174
208
|
const edge = parent ? byId.get(parent) : undefined;
|
|
175
209
|
if (!local || hasInvalidNumber(local) || edge?.getAttribute("edge") !== "1") return null;
|
|
176
210
|
const route = edgeRoute(edge, byId);
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
? childrenByTag(geometry, "mxPoint").find((point) => attr(point, "as") === "offset")
|
|
182
|
-
: undefined;
|
|
183
|
-
const offsetX = offset ? pyFloat(attr(offset, "x") ?? "0") : 0;
|
|
184
|
-
const offsetY = offset ? pyFloat(attr(offset, "y") ?? "0") : 0;
|
|
185
|
-
if (offsetX === undefined || offsetY === undefined || !Number.isFinite(offsetX) || !Number.isFinite(offsetY)) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
return [midX + offsetX - local[2] / 2, midY + offsetY - local[3] / 2, local[2], local[3]];
|
|
211
|
+
const center = route ? routeLabelPoint(cell, route) : null;
|
|
212
|
+
if (!center) return null;
|
|
213
|
+
const [, , width, height] = local;
|
|
214
|
+
return [center[0] - width / 2, center[1] - height / 2, width, height];
|
|
189
215
|
}
|
|
190
216
|
|
|
191
|
-
/**
|
|
217
|
+
/** Analysis only: materialize effective wrapper ids and labels on their inner mxCells. */
|
|
192
218
|
export function collectCells(root: DomEl | null): DomEl[] {
|
|
193
219
|
const cells: DomEl[] = [];
|
|
194
220
|
for (const child of root ? elemChildren(root) : []) {
|
|
@@ -197,6 +223,8 @@ export function collectCells(root: DomEl | null): DomEl[] {
|
|
|
197
223
|
const inner = firstByTag(child, "mxCell");
|
|
198
224
|
if (inner) {
|
|
199
225
|
inner.setAttribute("id", attr(child, "id") ?? "");
|
|
226
|
+
const value = attr(child, "label") ?? attr(child, "value");
|
|
227
|
+
if (value !== null) inner.setAttribute("value", value);
|
|
200
228
|
cells.push(inner);
|
|
201
229
|
}
|
|
202
230
|
}
|
|
@@ -258,17 +286,28 @@ export function contentWaypoints(cell: DomEl): Point[] {
|
|
|
258
286
|
return cell.getAttribute("edge") === "1" ? edgeWaypoints(cell) : [];
|
|
259
287
|
}
|
|
260
288
|
|
|
261
|
-
/** Visible vertices/containers, practical edge-label boxes, and
|
|
289
|
+
/** Visible vertices/containers, practical edge-label boxes, waypoints, and floating terminals. */
|
|
262
290
|
export function contentBounds(cells: DomEl[], ids: ById): ContentBounds | null {
|
|
263
291
|
let bounds: ContentBounds | null = null;
|
|
264
292
|
for (const cell of cells) {
|
|
265
293
|
if (!isVisible(cell, ids)) continue;
|
|
266
294
|
const box = contentRect(cell, ids);
|
|
267
295
|
if (box && !hasInvalidNumber(box)) bounds = addRectToBounds(bounds, box);
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
296
|
+
if (attr(cell, "edge") !== "1") continue;
|
|
297
|
+
|
|
298
|
+
const points = contentWaypoints(cell);
|
|
299
|
+
const geometry = firstByTag(cell, "mxGeometry");
|
|
300
|
+
for (const end of ["source", "target"]) {
|
|
301
|
+
if (attr(cell, end) !== null) continue; // An attached end's stored point is not rendered.
|
|
302
|
+
const terminal = geometryPoint(geometry, `${end}Point`);
|
|
303
|
+
const point = terminal ? pointCoordinates(terminal) : null;
|
|
304
|
+
if (point) points.push(point);
|
|
305
|
+
}
|
|
306
|
+
if (points.length === 0) continue;
|
|
307
|
+
const [ox, oy] = parentOrigin(cell, ids);
|
|
308
|
+
for (const [x, y] of points) {
|
|
309
|
+
const point: Point = [x + ox, y + oy];
|
|
310
|
+
if (!hasInvalidNumber(point)) bounds = addPointToBounds(bounds, point);
|
|
272
311
|
}
|
|
273
312
|
}
|
|
274
313
|
return bounds;
|