@md2nativedocx/core 0.6.0 → 0.8.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/dist/diagrams/agentflow/parser.d.ts +20 -0
- package/dist/diagrams/agentflow/parser.js +155 -0
- package/dist/diagrams/agentflow/parser.js.map +1 -0
- package/dist/diagrams/architecture-diagram/icons.d.ts +24 -0
- package/dist/diagrams/architecture-diagram/icons.js +148 -0
- package/dist/diagrams/architecture-diagram/icons.js.map +1 -0
- package/dist/diagrams/architecture-diagram/layout.d.ts +71 -0
- package/dist/diagrams/architecture-diagram/layout.js +388 -0
- package/dist/diagrams/architecture-diagram/layout.js.map +1 -0
- package/dist/diagrams/architecture-diagram/parser.d.ts +14 -19
- package/dist/diagrams/architecture-diagram/parser.js +316 -113
- package/dist/diagrams/architecture-diagram/parser.js.map +1 -1
- package/dist/diagrams/architecture-diagram/translator.d.ts +26 -36
- package/dist/diagrams/architecture-diagram/translator.js +234 -147
- package/dist/diagrams/architecture-diagram/translator.js.map +1 -1
- package/dist/diagrams/architecture-diagram/types.d.ts +26 -16
- package/dist/diagrams/architecture-diagram/types.js +4 -14
- package/dist/diagrams/architecture-diagram/types.js.map +1 -1
- package/dist/diagrams/c4/parser.d.ts +11 -19
- package/dist/diagrams/c4/parser.js +242 -90
- package/dist/diagrams/c4/parser.js.map +1 -1
- package/dist/diagrams/c4/translator.d.ts +23 -43
- package/dist/diagrams/c4/translator.js +333 -204
- package/dist/diagrams/c4/translator.js.map +1 -1
- package/dist/diagrams/c4/types.d.ts +78 -45
- package/dist/diagrams/c4/types.js +17 -38
- package/dist/diagrams/c4/types.js.map +1 -1
- package/dist/diagrams/class-diagram/parser.d.ts +10 -11
- package/dist/diagrams/class-diagram/parser.js +322 -74
- package/dist/diagrams/class-diagram/parser.js.map +1 -1
- package/dist/diagrams/class-diagram/translator.d.ts +10 -10
- package/dist/diagrams/class-diagram/translator.js +305 -33
- package/dist/diagrams/class-diagram/translator.js.map +1 -1
- package/dist/diagrams/class-diagram/types.d.ts +61 -4
- package/dist/diagrams/er-diagram/parser.d.ts +22 -19
- package/dist/diagrams/er-diagram/parser.js +412 -89
- package/dist/diagrams/er-diagram/parser.js.map +1 -1
- package/dist/diagrams/er-diagram/translator.d.ts +8 -0
- package/dist/diagrams/er-diagram/translator.js +170 -38
- package/dist/diagrams/er-diagram/translator.js.map +1 -1
- package/dist/diagrams/er-diagram/types.d.ts +32 -5
- package/dist/diagrams/er-diagram/types.js +2 -5
- package/dist/diagrams/er-diagram/types.js.map +1 -1
- package/dist/diagrams/flowchart-source.d.ts +106 -0
- package/dist/diagrams/flowchart-source.js +272 -0
- package/dist/diagrams/flowchart-source.js.map +1 -0
- package/dist/diagrams/gantt/date-utils.d.ts +55 -33
- package/dist/diagrams/gantt/date-utils.js +147 -58
- package/dist/diagrams/gantt/date-utils.js.map +1 -1
- package/dist/diagrams/gantt/parser.d.ts +31 -22
- package/dist/diagrams/gantt/parser.js +259 -141
- package/dist/diagrams/gantt/parser.js.map +1 -1
- package/dist/diagrams/gantt/time-axis.d.ts +44 -0
- package/dist/diagrams/gantt/time-axis.js +158 -0
- package/dist/diagrams/gantt/time-axis.js.map +1 -0
- package/dist/diagrams/gantt/translator.d.ts +8 -5
- package/dist/diagrams/gantt/translator.js +72 -60
- package/dist/diagrams/gantt/translator.js.map +1 -1
- package/dist/diagrams/gantt/types.d.ts +25 -4
- package/dist/diagrams/gantt/types.js +4 -3
- package/dist/diagrams/gantt/types.js.map +1 -1
- package/dist/diagrams/mindmap/parser.d.ts +18 -14
- package/dist/diagrams/mindmap/parser.js +169 -69
- package/dist/diagrams/mindmap/parser.js.map +1 -1
- package/dist/diagrams/mindmap/translator.js +90 -35
- package/dist/diagrams/mindmap/translator.js.map +1 -1
- package/dist/diagrams/mindmap/types.d.ts +18 -0
- package/dist/diagrams/packet/parser.js +1 -1
- package/dist/diagrams/packet/parser.js.map +1 -1
- package/dist/diagrams/pie/translator.js +5 -4
- package/dist/diagrams/pie/translator.js.map +1 -1
- package/dist/diagrams/plantuml/class-parser.d.ts +2 -1
- package/dist/diagrams/plantuml/class-parser.js +19 -11
- package/dist/diagrams/plantuml/class-parser.js.map +1 -1
- package/dist/diagrams/plantuml/sequence-parser.d.ts +3 -2
- package/dist/diagrams/plantuml/sequence-parser.js +28 -12
- package/dist/diagrams/plantuml/sequence-parser.js.map +1 -1
- package/dist/diagrams/plantuml/state-parser.d.ts +5 -3
- package/dist/diagrams/plantuml/state-parser.js +141 -19
- package/dist/diagrams/plantuml/state-parser.js.map +1 -1
- package/dist/diagrams/quadrant/translator.js +5 -4
- package/dist/diagrams/quadrant/translator.js.map +1 -1
- package/dist/diagrams/railroad/layout.d.ts +89 -0
- package/dist/diagrams/railroad/layout.js +235 -0
- package/dist/diagrams/railroad/layout.js.map +1 -0
- package/dist/diagrams/railroad/parser.d.ts +37 -0
- package/dist/diagrams/railroad/parser.js +605 -0
- package/dist/diagrams/railroad/parser.js.map +1 -0
- package/dist/diagrams/railroad/translator.d.ts +11 -0
- package/dist/diagrams/railroad/translator.js +101 -0
- package/dist/diagrams/railroad/translator.js.map +1 -0
- package/dist/diagrams/railroad/types.d.ts +61 -0
- package/dist/diagrams/railroad/types.js +7 -0
- package/dist/diagrams/railroad/types.js.map +1 -0
- package/dist/diagrams/requirement-diagram/parser.d.ts +26 -19
- package/dist/diagrams/requirement-diagram/parser.js +249 -70
- package/dist/diagrams/requirement-diagram/parser.js.map +1 -1
- package/dist/diagrams/requirement-diagram/translator.d.ts +16 -11
- package/dist/diagrams/requirement-diagram/translator.js +170 -50
- package/dist/diagrams/requirement-diagram/translator.js.map +1 -1
- package/dist/diagrams/requirement-diagram/types.d.ts +30 -9
- package/dist/diagrams/requirement-diagram/types.js +7 -5
- package/dist/diagrams/requirement-diagram/types.js.map +1 -1
- package/dist/diagrams/sequence/parser.d.ts +8 -7
- package/dist/diagrams/sequence/parser.js +129 -32
- package/dist/diagrams/sequence/parser.js.map +1 -1
- package/dist/diagrams/sequence/participant-glyph.d.ts +38 -0
- package/dist/diagrams/sequence/participant-glyph.js +97 -0
- package/dist/diagrams/sequence/participant-glyph.js.map +1 -0
- package/dist/diagrams/sequence/participant-kind.d.ts +7 -0
- package/dist/diagrams/sequence/participant-kind.js +19 -0
- package/dist/diagrams/sequence/participant-kind.js.map +1 -0
- package/dist/diagrams/sequence/translator.d.ts +7 -3
- package/dist/diagrams/sequence/translator.js +140 -28
- package/dist/diagrams/sequence/translator.js.map +1 -1
- package/dist/diagrams/sequence/types.d.ts +24 -1
- package/dist/diagrams/state-diagram/parser.d.ts +24 -24
- package/dist/diagrams/state-diagram/parser.js +259 -73
- package/dist/diagrams/state-diagram/parser.js.map +1 -1
- package/dist/diagrams/state-diagram/translator.d.ts +13 -1
- package/dist/diagrams/state-diagram/translator.js +554 -72
- package/dist/diagrams/state-diagram/translator.js.map +1 -1
- package/dist/diagrams/state-diagram/types.d.ts +57 -11
- package/dist/diagrams/state-diagram/types.js +5 -7
- package/dist/diagrams/state-diagram/types.js.map +1 -1
- package/dist/diagrams/tree-view/implicit-root.d.ts +11 -0
- package/dist/diagrams/tree-view/implicit-root.js +14 -0
- package/dist/diagrams/tree-view/implicit-root.js.map +1 -0
- package/dist/diagrams/tree-view/parser.d.ts +23 -8
- package/dist/diagrams/tree-view/parser.js +242 -98
- package/dist/diagrams/tree-view/parser.js.map +1 -1
- package/dist/diagrams/tree-view/translator.d.ts +6 -5
- package/dist/diagrams/tree-view/translator.js +40 -23
- package/dist/diagrams/tree-view/translator.js.map +1 -1
- package/dist/diagrams/tree-view/types.d.ts +22 -6
- package/dist/diagrams/tree-view/types.js +2 -2
- package/dist/diagrams/treemap/parser.d.ts +2 -2
- package/dist/diagrams/treemap/parser.js +5 -12
- package/dist/diagrams/treemap/parser.js.map +1 -1
- package/dist/diagrams/usecase/parser.d.ts +24 -0
- package/dist/diagrams/usecase/parser.js +232 -0
- package/dist/diagrams/usecase/parser.js.map +1 -0
- package/dist/diagrams/venn/translator.js +5 -4
- package/dist/diagrams/venn/translator.js.map +1 -1
- package/dist/diagrams/zenuml/parser.d.ts +2 -2
- package/dist/diagrams/zenuml/parser.js +14 -7
- package/dist/diagrams/zenuml/parser.js.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/layout/collapse-subgraphs.d.ts +18 -0
- package/dist/layout/collapse-subgraphs.js +80 -0
- package/dist/layout/collapse-subgraphs.js.map +1 -0
- package/dist/layout/layout.d.ts +11 -0
- package/dist/layout/layout.js +89 -17
- package/dist/layout/layout.js.map +1 -1
- package/dist/parser/css-named-colors.d.ts +7 -0
- package/dist/parser/css-named-colors.js +157 -0
- package/dist/parser/css-named-colors.js.map +1 -0
- package/dist/parser/css-style.d.ts +39 -0
- package/dist/parser/css-style.js +88 -0
- package/dist/parser/css-style.js.map +1 -0
- package/dist/parser/diagram-type.d.ts +12 -1
- package/dist/parser/diagram-type.js +4 -0
- package/dist/parser/diagram-type.js.map +1 -1
- package/dist/parser/entities.d.ts +5 -0
- package/dist/parser/entities.js +52 -0
- package/dist/parser/entities.js.map +1 -0
- package/dist/parser/frontmatter.d.ts +36 -0
- package/dist/parser/frontmatter.js +66 -0
- package/dist/parser/frontmatter.js.map +1 -0
- package/dist/parser/interactive.d.ts +13 -0
- package/dist/parser/interactive.js +17 -0
- package/dist/parser/interactive.js.map +1 -0
- package/dist/parser/label.d.ts +37 -0
- package/dist/parser/label.js +73 -0
- package/dist/parser/label.js.map +1 -0
- package/dist/parser/normalize-statements.d.ts +8 -0
- package/dist/parser/normalize-statements.js +17 -2
- package/dist/parser/normalize-statements.js.map +1 -1
- package/dist/parser/parser.d.ts +2 -1
- package/dist/parser/parser.js +345 -324
- package/dist/parser/parser.js.map +1 -1
- package/dist/render-diagram-odf.d.ts +2 -1
- package/dist/render-diagram-odf.js +39 -8
- package/dist/render-diagram-odf.js.map +1 -1
- package/dist/render-diagram.d.ts +11 -3
- package/dist/render-diagram.js +66 -23
- package/dist/render-diagram.js.map +1 -1
- package/dist/smartart/chain.js +1 -1
- package/dist/smartart/chain.js.map +1 -1
- package/dist/smartart/cycle.js +1 -1
- package/dist/smartart/cycle.js.map +1 -1
- package/dist/smartart/drawing.js +2 -10
- package/dist/smartart/drawing.js.map +1 -1
- package/dist/smartart/embed.d.ts +4 -3
- package/dist/smartart/embed.js +5 -4
- package/dist/smartart/embed.js.map +1 -1
- package/dist/smartart/from-graph.d.ts +13 -3
- package/dist/smartart/from-graph.js +39 -7
- package/dist/smartart/from-graph.js.map +1 -1
- package/dist/smartart/from-tree.d.ts +9 -5
- package/dist/smartart/from-tree.js +23 -11
- package/dist/smartart/from-tree.js.map +1 -1
- package/dist/smartart/text.d.ts +18 -4
- package/dist/smartart/text.js +32 -8
- package/dist/smartart/text.js.map +1 -1
- package/dist/smartart/tree-deep.js +1 -1
- package/dist/smartart/tree-deep.js.map +1 -1
- package/dist/smartart/tree.js +2 -2
- package/dist/smartart/tree.js.map +1 -1
- package/dist/translator/actor-figure.d.ts +45 -0
- package/dist/translator/actor-figure.js +46 -0
- package/dist/translator/actor-figure.js.map +1 -0
- package/dist/translator/boxes.d.ts +12 -1
- package/dist/translator/boxes.js +11 -6
- package/dist/translator/boxes.js.map +1 -1
- package/dist/translator/canvas.d.ts +28 -1
- package/dist/translator/canvas.js +83 -16
- package/dist/translator/canvas.js.map +1 -1
- package/dist/translator/custom-geometry.d.ts +28 -0
- package/dist/translator/custom-geometry.js +59 -0
- package/dist/translator/custom-geometry.js.map +1 -0
- package/dist/translator/elbow-connector.d.ts +52 -0
- package/dist/translator/elbow-connector.js +110 -0
- package/dist/translator/elbow-connector.js.map +1 -0
- package/dist/translator/graph-shapes.d.ts +40 -4
- package/dist/translator/graph-shapes.js +62 -10
- package/dist/translator/graph-shapes.js.map +1 -1
- package/dist/translator/label-runs.d.ts +14 -0
- package/dist/translator/label-runs.js +36 -0
- package/dist/translator/label-runs.js.map +1 -0
- package/dist/translator/odf-translator.js +56 -24
- package/dist/translator/odf-translator.js.map +1 -1
- package/dist/translator/ooxml-translator.d.ts +17 -11
- package/dist/translator/ooxml-translator.js +159 -146
- package/dist/translator/ooxml-translator.js.map +1 -1
- package/dist/translator/text-color.d.ts +9 -0
- package/dist/translator/text-color.js +17 -0
- package/dist/translator/text-color.js.map +1 -0
- package/dist/types.d.ts +27 -4
- package/package.json +1 -1
|
@@ -1,241 +1,370 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* OOXML translator for a Mermaid C4 diagram
|
|
3
|
-
*
|
|
4
|
-
* rationale as `../requirement-diagram/translator.ts`'s module doc
|
|
5
|
-
* comment, which this module's 2-compartment box/relationship-label code
|
|
6
|
-
* mirrors closely).
|
|
2
|
+
* OOXML translator for a Mermaid C4 diagram, drawn the way Mermaid 12.3 draws it (its C4 renderer, read
|
|
3
|
+
* against the official examples; the code here is this project's own).
|
|
7
4
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* rather than a distinct shape:
|
|
14
|
-
* - **External elements aren't drawn differently** (no dashed border, no
|
|
15
|
-
* gray recolor) — `C4Element.external` only changes the stereotype text
|
|
16
|
-
* (e.g. `[System, External]`). Real C4 tooling (and Mermaid's own
|
|
17
|
-
* renderer) typically grays out external elements; reproducing that
|
|
18
|
-
* would mean threading a second border/fill style through `rect()`
|
|
19
|
-
* (`../../translator/graph-shapes.ts`) for a purely cosmetic distinction
|
|
20
|
-
* this project's other 2-compartment-box modules don't need either.
|
|
21
|
-
* - **`Db`/`Queue` variants don't get a cylinder/queue shape** — same
|
|
22
|
-
* "text conveys the semantics, box geometry stays uniform" choice
|
|
23
|
-
* `requirementDiagram`'s translator already makes for its own stereotype
|
|
24
|
-
* line, rather than inventing shape-per-field handling this diagram
|
|
25
|
-
* family has never needed before.
|
|
5
|
+
* **Layout.** Mermaid does not run a graph layout for C4: it places things in declaration order on a
|
|
6
|
+
* grid. Inside the diagram and inside each frame, the elements come first, `shapesPerRow` to a row
|
|
7
|
+
* (default 4), then the nested frames below them, `boundariesPerRow` to a row (default 2); a frame is as
|
|
8
|
+
* large as what it holds. `UpdateLayoutConfig` changes both numbers. The spacing is tighter than Mermaid's
|
|
9
|
+
* (50 px margins on every side of a shape) so a diagram keeps a readable size once scaled to a page.
|
|
26
10
|
*
|
|
27
|
-
* **
|
|
28
|
-
* `
|
|
29
|
-
*
|
|
11
|
+
* **Shapes and colours.** One shape per element, its text inside it (so it moves with the shape in Word):
|
|
12
|
+
* a person is a rounded body under a round head, a `Db` a cylinder, a `Queue` a cylinder lying on its
|
|
13
|
+
* side, anything else a rounded rectangle. Fill and outline per category and external flag are Mermaid's
|
|
14
|
+
* (`person_bg_color`...), white text, bold name, then `[Software System]`/`[Container: techn]` and the
|
|
15
|
+
* description in smaller type. A boundary is a dashed frame, a deployment node a solid one, label bold and
|
|
16
|
+
* centred at the top with its `[type]` under it. `UpdateElementStyle` replaces fill, outline and text
|
|
17
|
+
* colour, on an element or a frame.
|
|
30
18
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* (`Rel(customer, spa, ...)`, where `Rel(web_app, spa, "Delivers")`
|
|
38
|
-
* separately forces `spa` a rank below `web_app`) draws straight through
|
|
39
|
-
* `web_app`'s box rather than routing around it, same as a flowchart
|
|
40
|
-
* skip-rank edge would without `ooxml-translator.ts`'s dedicated routing —
|
|
41
|
-
* which this simpler family of translators doesn't have. Not fixed here,
|
|
42
|
-
* consistent with the rest of the family.
|
|
19
|
+
* **Relationships** are straight lines between the two shapes' borders, `444444` unless `UpdateRelStyle`
|
|
20
|
+
* says otherwise, arrow at the target (both ends for `BiRel`), label at the middle of the line shifted by
|
|
21
|
+
* `$offsetX`/`$offsetY`, technology in italics under it, numbered `1:`, `2:`... in a `C4Dynamic`. Mermaid
|
|
22
|
+
* curves every line after the first one; here they all stay straight, and two relationships between the
|
|
23
|
+
* same pair are drawn side by side rather than on top of each other. A line can cross a shape it passes:
|
|
24
|
+
* Mermaid's grid does not route around shapes either.
|
|
43
25
|
*/
|
|
44
|
-
import dagre from 'dagre';
|
|
45
|
-
import { layoutWithinLimits, nodeKey } from '../../layout/graph-limits.js';
|
|
46
26
|
import { estimateTextWidth } from '../../layout/layout.js';
|
|
47
|
-
import { createIdAllocator, scaledExtent, scaledFontSizeHalfPt, scaledLineWidthEmu, wrapDrawingCanvas
|
|
48
|
-
import {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
27
|
+
import { createIdAllocator, scaledExtent, scaledFontSizeHalfPt, scaledLineWidthEmu, wrapDrawingCanvas } from '../../translator/canvas.js';
|
|
28
|
+
import { customGeometry, circlePath, horizontalCylinderGeometry, roundedRectPath } from '../../translator/custom-geometry.js';
|
|
29
|
+
import { connector, edgePoint, parallelEdgeOffset, perpendicularUnit, scalePt, shiftPoint, textBoxLines } from '../../translator/graph-shapes.js';
|
|
30
|
+
import { noteParagraph, wrapText } from '../../translator/boxes.js';
|
|
31
|
+
import { escapeXml } from '../../translator/xml-escape.js';
|
|
32
|
+
/** Mermaid's `<category>_bg_color` / `_border_color` defaults, internal then external. */
|
|
33
|
+
const PALETTE = {
|
|
34
|
+
person: { internal: ['08427B', '073B6F'], external: ['686868', '8A8A8A'] },
|
|
35
|
+
system: { internal: ['1168BD', '3C7FC0'], external: ['999999', '8A8A8A'] },
|
|
36
|
+
container: { internal: ['438DD5', '3C7FC0'], external: ['B3B3B3', 'A6A6A6'] },
|
|
37
|
+
component: { internal: ['85BBF0', '78A8D8'], external: ['CCCCCC', 'BFBFBF'] },
|
|
56
38
|
};
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
const TITLE_SIZE_HALFPT = 22;
|
|
60
|
-
const BODY_SIZE_HALFPT = 18;
|
|
61
|
-
const LABEL_SIZE_HALFPT = 16;
|
|
62
|
-
const TITLE_H = 32;
|
|
63
|
-
const ROW_H = 22;
|
|
64
|
-
const COMPARTMENT_PAD_Y = 6;
|
|
65
|
-
const PAD_X = 10;
|
|
66
|
-
const DIAGRAM_TITLE_H = 34;
|
|
67
|
-
const DIAGRAM_TITLE_SIZE_HALFPT = 28;
|
|
68
|
-
const CATEGORY_LABEL = {
|
|
39
|
+
/** Mermaid's stereotype names (`[Software System]`, not `[System]`). */
|
|
40
|
+
const STEREOTYPE = {
|
|
69
41
|
person: 'Person',
|
|
70
|
-
system: 'System',
|
|
42
|
+
system: 'Software System',
|
|
71
43
|
container: 'Container',
|
|
72
44
|
component: 'Component',
|
|
73
|
-
node: 'Node',
|
|
74
45
|
};
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
46
|
+
const ELEMENT_TEXT = 'FFFFFF';
|
|
47
|
+
const FRAME_COLOR = '444444';
|
|
48
|
+
const REL_COLOR = '444444';
|
|
49
|
+
/** Type sizes in pt: Mermaid's 14 px name, its stereotype at 0.75 em and description at 0.82 em. */
|
|
50
|
+
const NAME_PT = 10.5;
|
|
51
|
+
const STEREO_PT = 8;
|
|
52
|
+
const DESCR_PT = 8.5;
|
|
53
|
+
const FRAME_LABEL_PT = 12;
|
|
54
|
+
const FRAME_TYPE_PT = 10.5;
|
|
55
|
+
const FRAME_DESCR_PT = 9;
|
|
56
|
+
const REL_PT = 9;
|
|
57
|
+
const TITLE_PT = 14;
|
|
58
|
+
const pxOf = (pt) => (pt * 4) / 3;
|
|
59
|
+
/** Height of one line of text at `pt`, in px (Word's single spacing is about 1.2 em; a little room on top). */
|
|
60
|
+
const lineH = (pt) => Math.ceil(pxOf(pt) * 1.3);
|
|
61
|
+
const halfPt = (pt) => Math.round(pt * 2);
|
|
62
|
+
/** Mermaid's element width; text wraps inside it, less the padding. */
|
|
63
|
+
const ELEMENT_W = 216;
|
|
64
|
+
const ELEMENT_PAD_X = 16;
|
|
65
|
+
const ELEMENT_PAD_Y = 10;
|
|
66
|
+
const ELEMENT_MIN_H = 60;
|
|
67
|
+
const SECTION_GAP = 3;
|
|
68
|
+
/** Rounded-rectangle corner radius (Mermaid's `rx: 12px`). */
|
|
69
|
+
const CORNER = 12;
|
|
70
|
+
/** Half the height of a `Db` cylinder's top ellipse, and the width of a `Queue` cylinder's end cap. */
|
|
71
|
+
const DB_CAP = 10;
|
|
72
|
+
const QUEUE_CAP = 8;
|
|
73
|
+
const GAP_X = 40;
|
|
74
|
+
const GAP_Y = 56;
|
|
75
|
+
const FRAME_PAD = 20;
|
|
76
|
+
const FRAME_HEADER_GAP = 12;
|
|
77
|
+
const CANVAS_PAD = 24;
|
|
78
|
+
const DEFAULT_SHAPES_PER_ROW = 4;
|
|
79
|
+
const DEFAULT_BOUNDARIES_PER_ROW = 2;
|
|
80
|
+
function stereotype(el) {
|
|
81
|
+
return el.techn ? `[${STEREOTYPE[el.category]}: ${el.techn}]` : `[${STEREOTYPE[el.category]}]`;
|
|
85
82
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
83
|
+
const asWritten = (pt) => pt;
|
|
84
|
+
function wrapped(text, pt, widthPx, fit, style = {}) {
|
|
85
|
+
return wrapText(text, pxOf(fit(pt)), widthPx).map((line) => ({ text: line, pt, ...style }));
|
|
86
|
+
}
|
|
87
|
+
const textHeight = (lines, fit) => lines.reduce((sum, l) => sum + lineH(fit(l.pt)), 0);
|
|
88
|
+
const textWidth = (lines, fit) => Math.max(0, ...lines.map((l) => estimateTextWidth(l.text, pxOf(fit(l.pt))) * (l.bold ? 1.08 : 1)));
|
|
89
|
+
/** Person geometry for a body `w` wide, in Mermaid's proportions: head radius about a quarter of the
|
|
90
|
+
* width (16 to 56 px), sinking into the body by a quarter of that radius. */
|
|
91
|
+
function personHead(w) {
|
|
92
|
+
const r = Math.min(Math.max(w * 0.23, 16), 56);
|
|
93
|
+
return { r, overlap: r * 0.27 };
|
|
91
94
|
}
|
|
92
|
-
function
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
95
|
+
function measureElement(el, fit) {
|
|
96
|
+
// A word longer than the wrap width widens the shape rather than being cut.
|
|
97
|
+
const sections = [el.label, stereotype(el), el.description ?? ''];
|
|
98
|
+
const longestWord = Math.max(0, ...sections.flatMap((s, i) => s.split(/\s+/).map((word) => estimateTextWidth(word, pxOf(fit([NAME_PT, STEREO_PT, DESCR_PT][i]))) * (i === 0 ? 1.08 : 1))));
|
|
99
|
+
const w = Math.max(ELEMENT_W, Math.ceil(longestWord) + 2 * ELEMENT_PAD_X);
|
|
100
|
+
const wrapW = w - 2 * ELEMENT_PAD_X;
|
|
101
|
+
const lines = [
|
|
102
|
+
...wrapped(el.label, NAME_PT, wrapW, fit, { bold: true }),
|
|
103
|
+
...wrapped(stereotype(el), STEREO_PT, wrapW, fit),
|
|
104
|
+
...(el.description ? wrapped(el.description, DESCR_PT, wrapW, fit) : []),
|
|
105
|
+
];
|
|
106
|
+
const sectionsShown = el.description ? 3 : 2;
|
|
107
|
+
const textH = textHeight(lines, fit) + (sectionsShown - 1) * SECTION_GAP;
|
|
108
|
+
const bodyH = Math.max(ELEMENT_MIN_H, textH + 2 * ELEMENT_PAD_Y);
|
|
109
|
+
let bodyTop = 0;
|
|
110
|
+
let bodyBottom = 0;
|
|
111
|
+
if (el.category === 'person') {
|
|
112
|
+
const { r, overlap } = personHead(w);
|
|
113
|
+
bodyTop = 2 * r - overlap;
|
|
114
|
+
}
|
|
115
|
+
else if (el.variant === 'db') {
|
|
116
|
+
// The top ellipse is 2 x DB_CAP high; the bottom curve dips DB_CAP under the side lines.
|
|
117
|
+
bodyTop = 2 * DB_CAP;
|
|
118
|
+
bodyBottom = DB_CAP;
|
|
96
119
|
}
|
|
97
|
-
|
|
98
|
-
const bodyH = bodyLines.length * ROW_H + COMPARTMENT_PAD_Y;
|
|
99
|
-
return { width, height: TITLE_H + bodyH, titleH: TITLE_H, bodyH };
|
|
120
|
+
return { element: el, lines, bodyTop, bodyBottom, x: 0, y: 0, w, h: bodyTop + bodyH + bodyBottom };
|
|
100
121
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
122
|
+
/** Place `items` left to right, `perRow` to a row, from (`x0`, `y0`); returns the extent used. */
|
|
123
|
+
function placeRows(items, perRow, x0, y0) {
|
|
124
|
+
let y = y0;
|
|
125
|
+
let maxW = 0;
|
|
126
|
+
for (let start = 0; start < items.length; start += perRow) {
|
|
127
|
+
const row = items.slice(start, start + perRow);
|
|
128
|
+
let x = x0;
|
|
129
|
+
let rowH = 0;
|
|
130
|
+
for (const item of row) {
|
|
131
|
+
item.x = x;
|
|
132
|
+
item.y = y;
|
|
133
|
+
x += item.w + GAP_X;
|
|
134
|
+
rowH = Math.max(rowH, item.h);
|
|
135
|
+
}
|
|
136
|
+
maxW = Math.max(maxW, x - GAP_X - x0);
|
|
137
|
+
y += rowH + GAP_Y;
|
|
138
|
+
}
|
|
139
|
+
return { w: maxW, h: items.length > 0 ? y - GAP_Y - y0 : 0 };
|
|
118
140
|
}
|
|
119
|
-
/**
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
'<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">',
|
|
127
|
-
' <w:pPr><w:spacing w:before="0" w:after="120"/></w:pPr>',
|
|
128
|
-
' <w:r>',
|
|
129
|
-
' <w:rPr><w:i/><w:color w:val="808080"/><w:sz w:val="18"/></w:rPr>',
|
|
130
|
-
' <w:t xml:space="preserve">This C4 diagram has no content to render.</w:t>',
|
|
131
|
-
' </w:r>',
|
|
132
|
-
'</w:p>',
|
|
133
|
-
].join('\n');
|
|
141
|
+
/** Move a block and everything it holds by (`dx`, `dy`). */
|
|
142
|
+
function shiftBlock(block, dx, dy) {
|
|
143
|
+
block.x += dx;
|
|
144
|
+
block.y += dy;
|
|
145
|
+
for (const el of block.elements) {
|
|
146
|
+
el.x += dx;
|
|
147
|
+
el.y += dy;
|
|
134
148
|
}
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
149
|
+
for (const child of block.children)
|
|
150
|
+
shiftBlock(child, dx, dy);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Size `block` and place its content relative to its own top-left corner (children are laid out first,
|
|
154
|
+
* then moved into place, so every coordinate ends up relative to this block).
|
|
155
|
+
*/
|
|
156
|
+
function layoutBlock(block, shapesPerRow, boundariesPerRow, fit) {
|
|
157
|
+
for (const child of block.children)
|
|
158
|
+
layoutBlock(child, shapesPerRow, boundariesPerRow, fit);
|
|
159
|
+
const framed = block.boundary !== undefined;
|
|
160
|
+
const pad = framed ? FRAME_PAD : 0;
|
|
161
|
+
const headerH = framed ? textHeight(block.header, fit) + FRAME_HEADER_GAP : 0;
|
|
162
|
+
const top = pad + headerH;
|
|
163
|
+
const elementsExtent = placeRows(block.elements, shapesPerRow, pad, top);
|
|
164
|
+
const childTop = top + elementsExtent.h + (elementsExtent.h > 0 && block.children.length > 0 ? GAP_Y : 0);
|
|
165
|
+
// Children were laid out at their own origin: record it, place their boxes, then move their content.
|
|
166
|
+
const origins = block.children.map((c) => ({ x: c.x, y: c.y }));
|
|
167
|
+
const childExtent = placeRows(block.children, boundariesPerRow, pad, childTop);
|
|
168
|
+
block.children.forEach((child, i) => {
|
|
169
|
+
const target = { x: child.x, y: child.y };
|
|
170
|
+
child.x = origins[i].x;
|
|
171
|
+
child.y = origins[i].y;
|
|
172
|
+
shiftBlock(child, target.x - child.x, target.y - child.y);
|
|
173
|
+
});
|
|
174
|
+
const contentW = Math.max(elementsExtent.w, childExtent.w);
|
|
175
|
+
const contentBottom = childExtent.h > 0 ? childTop + childExtent.h : top + elementsExtent.h;
|
|
176
|
+
block.w = Math.max(contentW, framed ? textWidth(block.header, fit) + 16 : 0, framed ? 120 : 0) + 2 * pad;
|
|
177
|
+
block.h = Math.max(contentBottom + pad, framed ? top + 24 : 0);
|
|
178
|
+
// Centre the content of a frame under its (wider) header, as Mermaid centres the label over it.
|
|
179
|
+
const slack = block.w - 2 * pad - contentW;
|
|
180
|
+
if (slack > 0 && framed) {
|
|
181
|
+
for (const el of block.elements)
|
|
182
|
+
el.x += slack / 2;
|
|
183
|
+
for (const child of block.children)
|
|
184
|
+
shiftBlock(child, slack / 2, 0);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function frameHeader(b) {
|
|
188
|
+
const lines = [];
|
|
189
|
+
if (b.label)
|
|
190
|
+
lines.push(...b.label.split('\n').map((text) => ({ text, pt: FRAME_LABEL_PT, bold: true })));
|
|
191
|
+
if (b.type)
|
|
192
|
+
lines.push(...`[${b.type}]`.split('\n').map((text) => ({ text, pt: FRAME_TYPE_PT })));
|
|
193
|
+
if (b.description)
|
|
194
|
+
lines.push(...b.description.split('\n').map((text) => ({ text, pt: FRAME_DESCR_PT })));
|
|
195
|
+
return lines;
|
|
196
|
+
}
|
|
197
|
+
/** Build the block tree (the diagram, its frames by `parent`, each holding its elements) and lay it out
|
|
198
|
+
* on the canvas, under the title. */
|
|
199
|
+
function layoutDiagram(chart, fit) {
|
|
200
|
+
const root = { header: [], elements: [], children: [], x: 0, y: 0, w: 0, h: 0 };
|
|
201
|
+
const byId = new Map();
|
|
202
|
+
for (const b of chart.boundaries ?? [])
|
|
203
|
+
byId.set(b.id, { boundary: b, header: frameHeader(b), elements: [], children: [], x: 0, y: 0, w: 0, h: 0 });
|
|
204
|
+
// A parent that names nothing known (or itself, or a cycle built by hand) falls back to the diagram.
|
|
205
|
+
const parentOf = (parent, self) => {
|
|
206
|
+
if (parent === undefined || parent === self)
|
|
207
|
+
return root;
|
|
208
|
+
const seen = new Set(self ? [self] : []);
|
|
209
|
+
let cursor = parent;
|
|
210
|
+
while (cursor !== undefined) {
|
|
211
|
+
if (seen.has(cursor))
|
|
212
|
+
return root;
|
|
213
|
+
seen.add(cursor);
|
|
214
|
+
cursor = byId.get(cursor)?.boundary?.parent;
|
|
215
|
+
}
|
|
216
|
+
return byId.get(parent) ?? root;
|
|
217
|
+
};
|
|
218
|
+
for (const block of byId.values())
|
|
219
|
+
parentOf(block.boundary.parent, block.boundary.id).children.push(block);
|
|
220
|
+
const elementsById = new Map();
|
|
142
221
|
for (const el of chart.elements) {
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
222
|
+
const layout = measureElement(el, fit);
|
|
223
|
+
elementsById.set(el.id, layout);
|
|
224
|
+
parentOf(el.parent).elements.push(layout);
|
|
225
|
+
}
|
|
226
|
+
layoutBlock(root, chart.layout?.shapesPerRow ?? DEFAULT_SHAPES_PER_ROW, chart.layout?.boundariesPerRow ?? DEFAULT_BOUNDARIES_PER_ROW, fit);
|
|
227
|
+
const titleH = chart.title ? lineH(fit(TITLE_PT)) + 12 : 0;
|
|
228
|
+
const canvasW = Math.max(root.w, chart.title ? estimateTextWidth(chart.title, pxOf(fit(TITLE_PT))) * 1.08 : 0) + 2 * CANVAS_PAD;
|
|
229
|
+
const canvasH = root.h + 2 * CANVAS_PAD + titleH;
|
|
230
|
+
shiftBlock(root, CANVAS_PAD + (canvasW - 2 * CANVAS_PAD - root.w) / 2, CANVAS_PAD + titleH);
|
|
231
|
+
return { root, byId, elementsById, canvasW, canvasH };
|
|
232
|
+
}
|
|
233
|
+
/** One `wps:wsp`: a geometry, its fill and outline, and its text inside it (insets in EMU). */
|
|
234
|
+
function shapeWithText(id, name, box, geometry, fill, line, paragraphs, inset, anchor) {
|
|
235
|
+
const fillXml = fill ? `<a:solidFill><a:srgbClr val="${fill}"/></a:solidFill>` : '<a:noFill/>';
|
|
236
|
+
const lnXml = `<a:ln w="${line.emu}"><a:solidFill><a:srgbClr val="${line.color}"/></a:solidFill>${line.dashed ? '<a:prstDash val="dash"/>' : ''}</a:ln>`;
|
|
237
|
+
const body = paragraphs
|
|
238
|
+
.map((p) => '<w:p><w:pPr><w:spacing w:before="0" w:after="0"/><w:jc w:val="center"/></w:pPr>' +
|
|
239
|
+
`<w:r><w:rPr>${p.bold ? '<w:b/>' : ''}${p.italic ? '<w:i/>' : ''}<w:color w:val="${p.color}"/><w:sz w:val="${p.sizeHalfPt}"/></w:rPr>` +
|
|
240
|
+
`<w:t xml:space="preserve">${escapeXml(p.text)}</w:t></w:r></w:p>`)
|
|
241
|
+
.join('');
|
|
242
|
+
return [
|
|
243
|
+
'<wps:wsp>',
|
|
244
|
+
` <wps:cNvPr id="${id}" name="${escapeXml(name)}"/>`,
|
|
245
|
+
' <wps:cNvSpPr/>',
|
|
246
|
+
' <wps:spPr>',
|
|
247
|
+
` <a:xfrm><a:off x="${box.x}" y="${box.y}"/><a:ext cx="${Math.max(1, box.w)}" cy="${Math.max(1, box.h)}"/></a:xfrm>`,
|
|
248
|
+
` ${geometry}`,
|
|
249
|
+
` ${fillXml}`,
|
|
250
|
+
` ${lnXml}`,
|
|
251
|
+
' </wps:spPr>',
|
|
252
|
+
...(paragraphs.length > 0 ? [' <wps:txbx>', ` <w:txbxContent>${body}</w:txbxContent>`, ' </wps:txbx>'] : []),
|
|
253
|
+
` <wps:bodyPr wrap="square" lIns="${inset.lr}" tIns="${inset.t}" rIns="${inset.lr}" bIns="${inset.b}" anchor="${anchor}"/>`,
|
|
254
|
+
'</wps:wsp>',
|
|
255
|
+
].join('\n');
|
|
256
|
+
}
|
|
257
|
+
const roundRectPreset = (w, h, radius) => `<a:prstGeom prst="roundRect"><a:avLst><a:gd name="adj" fmla="val ${Math.round((Math.min(radius, Math.min(w, h) / 2) / Math.min(w, h)) * 100000)}"/></a:avLst></a:prstGeom>`;
|
|
258
|
+
/** The geometry of an element `w` x `h` px (path units are px, scaled by DrawingML to the shape). */
|
|
259
|
+
function elementGeometry(el) {
|
|
260
|
+
const { w, h } = el;
|
|
261
|
+
if (el.element.category === 'person') {
|
|
262
|
+
const { r, overlap } = personHead(w);
|
|
263
|
+
const bodyTop = 2 * r - overlap;
|
|
264
|
+
const bodyH = h - bodyTop;
|
|
265
|
+
const bodyR = Math.min(w * 0.177, bodyH * 0.45);
|
|
266
|
+
return customGeometry(w, h, [
|
|
267
|
+
['norm', roundedRectPath(0, bodyTop, w, bodyH, bodyR)],
|
|
268
|
+
['norm', circlePath(w / 2, r, r)],
|
|
269
|
+
]);
|
|
146
270
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
g.setEdge(nodeKey(rel.from), nodeKey(rel.to));
|
|
271
|
+
if (el.element.variant === 'db') {
|
|
272
|
+
return `<a:prstGeom prst="can"><a:avLst><a:gd name="adj" fmla="val ${Math.round(((2 * DB_CAP) / Math.min(w, h)) * 100000)}"/></a:avLst></a:prstGeom>`;
|
|
150
273
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
274
|
+
if (el.element.variant === 'queue')
|
|
275
|
+
return horizontalCylinderGeometry(w, h, QUEUE_CAP);
|
|
276
|
+
return roundRectPreset(w, h, CORNER);
|
|
277
|
+
}
|
|
278
|
+
/** Translate a parsed C4 diagram into a self-contained WordprocessingML paragraph. An empty diagram
|
|
279
|
+
* renders a visible note, never a silent blank canvas. */
|
|
280
|
+
export function translateC4DiagramToOoxml(chart, options = {}) {
|
|
281
|
+
if (chart.elements.length === 0 && (chart.boundaries ?? []).length === 0)
|
|
282
|
+
return noteParagraph('This C4 diagram has no content to render.');
|
|
283
|
+
// Lay out, find the scale the page imposes, then lay out again measuring text at the half-point sizes
|
|
284
|
+
// Word will get at that scale. The scale barely moves between passes; three is more than enough.
|
|
285
|
+
let laid = layoutDiagram(chart, asWritten);
|
|
286
|
+
let s = scaledExtent(laid.canvasW, laid.canvasH, options).scale;
|
|
287
|
+
for (let pass = 0; pass < 3 && s < 1; pass++) {
|
|
288
|
+
const scale = s;
|
|
289
|
+
// Only a size rounded up is measured larger; one rounded down keeps its proportional size, whose
|
|
290
|
+
// slack absorbs the gap between this width estimate and Word's own line breaking.
|
|
291
|
+
laid = layoutDiagram(chart, (pt) => Math.max(pt, scaledFontSizeHalfPt(halfPt(pt), scale) / 2 / scale));
|
|
292
|
+
const next = scaledExtent(laid.canvasW, laid.canvasH, options).scale;
|
|
293
|
+
if (next === s)
|
|
294
|
+
break;
|
|
295
|
+
s = next;
|
|
163
296
|
}
|
|
164
|
-
const
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
const canvasW = maxX - minX + 2 * PAD;
|
|
168
|
-
const canvasH = maxY - minY + 2 * PAD + topMargin;
|
|
169
|
-
const dx = -minX + PAD;
|
|
170
|
-
const dy = -minY + PAD + topMargin;
|
|
171
|
-
const { scale: s } = scaledExtent(canvasW, canvasH, options);
|
|
297
|
+
const { root, byId, elementsById, canvasW, canvasH } = laid;
|
|
298
|
+
const S = (px) => scalePt(px, s);
|
|
299
|
+
const sz = (pt) => scaledFontSizeHalfPt(halfPt(pt), s);
|
|
172
300
|
const nextId = createIdAllocator();
|
|
173
301
|
const parts = [];
|
|
174
302
|
if (chart.title) {
|
|
175
|
-
parts.push(textBoxLines(nextId(), 0,
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
303
|
+
parts.push(textBoxLines(nextId(), 0, S(CANVAS_PAD / 2), S(canvasW), S(lineH(TITLE_PT)), [chart.title], sz(TITLE_PT), { bold: true, align: 'ctr' }));
|
|
304
|
+
}
|
|
305
|
+
// Frames, outermost first so an inner frame is drawn over its parent.
|
|
306
|
+
const drawFrame = (block) => {
|
|
307
|
+
const b = block.boundary;
|
|
308
|
+
const color = b.style?.text ?? FRAME_COLOR;
|
|
309
|
+
parts.push(shapeWithText(nextId(), b.label || b.id, { x: S(block.x), y: S(block.y), w: S(block.w), h: S(block.h) }, roundRectPreset(block.w, block.h, 3), b.style?.fill, { color: b.style?.line ?? FRAME_COLOR, emu: scaledLineWidthEmu(9525, s), dashed: b.kind === 'boundary' }, block.header.map((l) => ({ text: l.text, sizeHalfPt: sz(l.pt), bold: l.bold, color })), { t: S(FRAME_PAD / 2), b: 0, lr: S(8) }, 't'));
|
|
310
|
+
for (const child of block.children)
|
|
311
|
+
drawFrame(child);
|
|
312
|
+
};
|
|
313
|
+
for (const child of root.children)
|
|
314
|
+
drawFrame(child);
|
|
315
|
+
for (const el of elementsById.values()) {
|
|
316
|
+
const e = el.element;
|
|
317
|
+
const [defFill, defLine] = PALETTE[e.category][e.external ? 'external' : 'internal'];
|
|
318
|
+
const color = e.style?.text ?? ELEMENT_TEXT;
|
|
319
|
+
const paragraphs = el.lines.map((l) => ({ text: l.text, sizeHalfPt: sz(l.pt), bold: l.bold, color }));
|
|
320
|
+
parts.push(shapeWithText(nextId(), e.label.split('\n')[0] || e.id, { x: S(el.x), y: S(el.y), w: S(el.w), h: S(el.h) }, elementGeometry(el), e.style?.fill ?? defFill, { color: e.style?.line ?? defLine, emu: scaledLineWidthEmu(19050, s) }, paragraphs,
|
|
321
|
+
// Word centres the text in the body: the insets keep it off a head or a cylinder's ends.
|
|
322
|
+
{ t: S(el.bodyTop), b: S(el.bodyBottom), lr: S(ELEMENT_PAD_X / 2) }, 'ctr'));
|
|
179
323
|
}
|
|
180
|
-
//
|
|
181
|
-
|
|
182
|
-
// other — see parallelEdgeOffset()'s doc comment for the real-render bug
|
|
183
|
-
// this fixes (first found on requirementDiagram; the same geometry risk
|
|
184
|
-
// applies here, e.g. a request/response pair of Rels between the same two
|
|
185
|
-
// containers).
|
|
324
|
+
// Each relationship's endpoints: an element's box, or a frame's.
|
|
325
|
+
const boxOf = (id) => elementsById.get(id) ?? byId.get(id);
|
|
186
326
|
const pairGroups = new Map();
|
|
187
327
|
chart.relationships.forEach((rel, i) => {
|
|
188
328
|
const key = [rel.from, rel.to].sort().join('|');
|
|
189
|
-
|
|
190
|
-
group.push(i);
|
|
191
|
-
pairGroups.set(key, group);
|
|
329
|
+
pairGroups.set(key, [...(pairGroups.get(key) ?? []), i]);
|
|
192
330
|
});
|
|
193
|
-
const
|
|
194
|
-
for (const group of pairGroups.values()) {
|
|
195
|
-
group.forEach((relIndex, i) => offsetIndexByRel.set(relIndex, { index: i, count: group.length }));
|
|
196
|
-
}
|
|
331
|
+
const labels = [];
|
|
197
332
|
chart.relationships.forEach((rel, relIndex) => {
|
|
198
|
-
const
|
|
199
|
-
const
|
|
200
|
-
if (!
|
|
333
|
+
const a = boxOf(rel.from);
|
|
334
|
+
const b = boxOf(rel.to);
|
|
335
|
+
if (!a || !b || a === b)
|
|
201
336
|
return;
|
|
202
|
-
const
|
|
203
|
-
const
|
|
204
|
-
const
|
|
205
|
-
const
|
|
206
|
-
const
|
|
207
|
-
const
|
|
208
|
-
const posA = g.node(nodeKey(sortedPair[0]));
|
|
209
|
-
const posB = g.node(nodeKey(sortedPair[1]));
|
|
337
|
+
const ca = { x: a.x + a.w / 2, y: a.y + a.h / 2 };
|
|
338
|
+
const cb = { x: b.x + b.w / 2, y: b.y + b.h / 2 };
|
|
339
|
+
const group = pairGroups.get([rel.from, rel.to].sort().join('|'));
|
|
340
|
+
const [first, second] = [rel.from, rel.to].sort();
|
|
341
|
+
const posA = first === rel.from ? ca : cb;
|
|
342
|
+
const posB = second === rel.to ? cb : ca;
|
|
210
343
|
const { ux, uy } = perpendicularUnit(posA, posB);
|
|
211
|
-
const amount = parallelEdgeOffset(
|
|
212
|
-
const p1 = shiftPoint(
|
|
213
|
-
const p2 = shiftPoint(
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
344
|
+
const amount = parallelEdgeOffset(group.indexOf(relIndex), group.length);
|
|
345
|
+
const p1 = shiftPoint(edgePoint(ca.x, ca.y, a.w / 2, a.h / 2, cb.x, cb.y), ux, uy, amount);
|
|
346
|
+
const p2 = shiftPoint(edgePoint(cb.x, cb.y, b.w / 2, b.h / 2, ca.x, ca.y), ux, uy, amount);
|
|
347
|
+
const lineColor = rel.style?.line ?? REL_COLOR;
|
|
348
|
+
parts.push(connector(nextId(), S(p1.x), S(p1.y), S(p2.x), S(p2.y), lineColor, scaledLineWidthEmu(9525, s), 'solid', rel.bidirectional ? 'triangle' : 'none', 'sm', 'triangle', 'sm'));
|
|
349
|
+
const label = chart.level === 'dynamic' ? `${relIndex + 1}: ${rel.label}` : rel.label;
|
|
350
|
+
const textColor = rel.style?.text ?? REL_COLOR;
|
|
351
|
+
const midX = (p1.x + p2.x) / 2 + (rel.style?.offsetX ?? 0);
|
|
352
|
+
const midY = (p1.y + p2.y) / 2 + (rel.style?.offsetY ?? 0);
|
|
353
|
+
const rows = [
|
|
354
|
+
...(label ? label.split('\n').map((text) => ({ text, italic: false })) : []),
|
|
355
|
+
...(rel.techn ? `[${rel.techn}]`.split('\n').map((text) => ({ text, italic: true })) : []),
|
|
356
|
+
];
|
|
357
|
+
if (rows.length === 0)
|
|
217
358
|
return;
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
const canonicalDy = posB.y - posA.y;
|
|
225
|
-
const canonicalLen = Math.hypot(canonicalDx, canonicalDy) || 1;
|
|
226
|
-
const alongShift = count > 1 ? (index - (count - 1) / 2) * alongStepPx : 0;
|
|
227
|
-
const midX = (p1.x + p2.x) / 2 + (canonicalDx / canonicalLen) * alongShift;
|
|
228
|
-
const midY = (p1.y + p2.y) / 2 + (canonicalDy / canonicalLen) * alongShift;
|
|
229
|
-
const labelWidth = estimateTextWidth(label, 8) + 12;
|
|
230
|
-
parts.push(textBoxLines(nextId(), scalePt(midX + dx - labelWidth / 2, s), scalePt(midY + dy - 10, s), scalePt(labelWidth, s), scalePt(20, s), [label], scaledFontSizeHalfPt(LABEL_SIZE_HALFPT, s), { align: 'ctr' }));
|
|
359
|
+
const rowH = lineH(REL_PT);
|
|
360
|
+
const top = midY - rowH / 2;
|
|
361
|
+
rows.forEach((row, i) => {
|
|
362
|
+
const w = estimateTextWidth(row.text, pxOf(REL_PT)) + 12;
|
|
363
|
+
labels.push(textBoxLines(nextId(), S(midX - w / 2), S(top + i * rowH), S(w), S(rowH), [row.text], sz(REL_PT), { align: 'ctr', italic: row.italic, color: textColor }));
|
|
364
|
+
});
|
|
231
365
|
});
|
|
232
|
-
|
|
233
|
-
const n = g.node(nodeKey(el.id));
|
|
234
|
-
const size = sizes.get(el.id);
|
|
235
|
-
parts.push(...elementShapes(nextId, el, bodyLinesById.get(el.id), size, n.x - size.width / 2 + dx, n.y - size.height / 2 + dy, s));
|
|
236
|
-
}
|
|
237
|
-
const content = parts.join('\n');
|
|
366
|
+
parts.push(...labels);
|
|
238
367
|
const docPrId = nextId();
|
|
239
|
-
return wrapDrawingCanvas(
|
|
368
|
+
return wrapDrawingCanvas(parts.join('\n'), canvasW, canvasH, docPrId, chart.title ?? 'C4 diagram', options);
|
|
240
369
|
}
|
|
241
370
|
//# sourceMappingURL=translator.js.map
|