@node-projects/web-component-designer-mermaid 0.1.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/README.md +9 -0
- package/package.json +24 -0
- package/src/index.ts +20 -0
- package/src/monaco/MermaidLanguage.ts +34 -0
- package/src/services/MermaidConnectionRouting.ts +92 -0
- package/src/services/MermaidDocumentPropertiesService.ts +179 -0
- package/src/services/MermaidElementsService.ts +81 -0
- package/src/services/MermaidLayoutCopyPasteService.ts +18 -0
- package/src/services/MermaidLayoutPlacementService.ts +30 -0
- package/src/services/MermaidParserService.ts +2473 -0
- package/src/services/MermaidPropertyGroupsService.ts +18 -0
- package/src/services/mermaidGeometry.ts +131 -0
- package/src/setupMermaidServiceContainer.ts +123 -0
- package/src/toolbar/ConnectMermaidNodesTool.ts +197 -0
- package/src/widgets/elements.json +198 -0
- package/src/widgets/mermaid-edge.ts +215 -0
- package/src/widgets/mermaid-flowchart-directive.ts +58 -0
- package/src/widgets/mermaid-mindmap-node.ts +218 -0
- package/src/widgets/mermaid-node.ts +456 -0
- package/src/widgets/mermaid-requirement-node.ts +202 -0
- package/src/widgets/mermaid-requirement-relationship.ts +139 -0
- package/src/widgets/mermaid-sequence-message.ts +145 -0
- package/src/widgets/mermaid-sequence-participant.ts +80 -0
- package/src/widgets/mermaid-subgraph.ts +80 -0
- package/src/widgets/views/mermaid-demo-view.ts +47 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
|
|
2
|
+
import { boundsFromWaypoints, decodeWaypoints, midpointFromWaypoints, pathDataFromWaypoints } from "../services/mermaidGeometry.js";
|
|
3
|
+
import { escapeLabel, sanitizeId } from "./mermaid-node.js";
|
|
4
|
+
|
|
5
|
+
enum MermaidEdgeType {
|
|
6
|
+
arrow = "arrow",
|
|
7
|
+
open = "open",
|
|
8
|
+
dotted = "dotted",
|
|
9
|
+
thick = "thick",
|
|
10
|
+
invisible = "invisible",
|
|
11
|
+
circle = "circle",
|
|
12
|
+
cross = "cross",
|
|
13
|
+
multi = "multi",
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class MermaidEdge extends BaseCustomWebComponentConstructorAppend {
|
|
17
|
+
static override readonly style = css`
|
|
18
|
+
* {
|
|
19
|
+
box-sizing: border-box;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
:host {
|
|
23
|
+
display: block;
|
|
24
|
+
overflow: visible;
|
|
25
|
+
pointer-events: auto;
|
|
26
|
+
position: absolute;
|
|
27
|
+
z-index: 4;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
svg {
|
|
31
|
+
height: 100%;
|
|
32
|
+
overflow: visible;
|
|
33
|
+
width: 100%;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
path {
|
|
37
|
+
fill: none;
|
|
38
|
+
stroke: #333;
|
|
39
|
+
stroke-linecap: round;
|
|
40
|
+
stroke-linejoin: round;
|
|
41
|
+
stroke-width: 2.5;
|
|
42
|
+
vector-effect: non-scaling-stroke;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#marker-shape {
|
|
46
|
+
fill: #333;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
text {
|
|
50
|
+
fill: #333;
|
|
51
|
+
font-family: Arial, sans-serif;
|
|
52
|
+
font-size: 12px;
|
|
53
|
+
pointer-events: none;
|
|
54
|
+
}
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
static override readonly template = html`
|
|
58
|
+
<svg id="svg" viewBox="0 0 10 10" preserveAspectRatio="none">
|
|
59
|
+
<defs>
|
|
60
|
+
<marker id="marker" viewBox="0 0 12 12" markerWidth="10" markerHeight="10" refX="10.5" refY="6" orient="auto" markerUnits="userSpaceOnUse">
|
|
61
|
+
<path id="marker-shape" d="M 1 1 L 10.5 6 L 1 11 z"></path>
|
|
62
|
+
</marker>
|
|
63
|
+
<marker id="marker-start" viewBox="0 0 12 12" markerWidth="10" markerHeight="10" refX="1.5" refY="6" orient="auto" markerUnits="userSpaceOnUse">
|
|
64
|
+
<path id="marker-start-shape" d="M 11 1 L 1.5 6 L 11 11 z"></path>
|
|
65
|
+
</marker>
|
|
66
|
+
<marker id="circle-marker" viewBox="0 0 12 12" markerWidth="10" markerHeight="10" refX="10" refY="6" orient="auto" markerUnits="userSpaceOnUse">
|
|
67
|
+
<circle cx="6" cy="6" r="4" fill="white" stroke="#333" stroke-width="2"></circle>
|
|
68
|
+
</marker>
|
|
69
|
+
<marker id="cross-marker" viewBox="0 0 12 12" markerWidth="10" markerHeight="10" refX="10" refY="6" orient="auto" markerUnits="userSpaceOnUse">
|
|
70
|
+
<path d="M 3 3 L 9 9 M 9 3 L 3 9" fill="none" stroke="#333" stroke-width="2"></path>
|
|
71
|
+
</marker>
|
|
72
|
+
</defs>
|
|
73
|
+
<path id="path"></path>
|
|
74
|
+
<text id="label"></text>
|
|
75
|
+
</svg>`;
|
|
76
|
+
|
|
77
|
+
static readonly is = "mermaid-edge";
|
|
78
|
+
|
|
79
|
+
public from: string = "A";
|
|
80
|
+
public to: string = "B";
|
|
81
|
+
public label: string = "";
|
|
82
|
+
public edgeType: MermaidEdgeType = MermaidEdgeType.arrow;
|
|
83
|
+
public connector: string = "-->";
|
|
84
|
+
public edgeId: string = "";
|
|
85
|
+
public animation: string = "";
|
|
86
|
+
public waypoints: string = "";
|
|
87
|
+
private _previewWaypoints: string;
|
|
88
|
+
|
|
89
|
+
private _label: SVGTextElement;
|
|
90
|
+
private _path: SVGPathElement;
|
|
91
|
+
private _svg: SVGSVGElement;
|
|
92
|
+
|
|
93
|
+
static readonly properties = {
|
|
94
|
+
from: String,
|
|
95
|
+
to: String,
|
|
96
|
+
label: String,
|
|
97
|
+
edgeType: MermaidEdgeType,
|
|
98
|
+
connector: String,
|
|
99
|
+
edgeId: String,
|
|
100
|
+
animation: String,
|
|
101
|
+
waypoints: String,
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
constructor() {
|
|
105
|
+
super();
|
|
106
|
+
this._restoreCachedInititalValues();
|
|
107
|
+
this._label = this._getDomElement<SVGTextElement>("label");
|
|
108
|
+
this._path = this._getDomElement<SVGPathElement>("path");
|
|
109
|
+
this._svg = this._getDomElement<SVGSVGElement>("svg");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async ready() {
|
|
113
|
+
this._parseAttributesToProperties();
|
|
114
|
+
this._render();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
|
|
118
|
+
if (oldValue !== newValue) {
|
|
119
|
+
this._parseAttributesToProperties();
|
|
120
|
+
this._render();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public setPreviewWaypoints(waypoints: string | null) {
|
|
125
|
+
this._previewWaypoints = waypoints;
|
|
126
|
+
this._render();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private _render() {
|
|
130
|
+
let waypoints = decodeWaypoints(this._previewWaypoints ?? this.getAttribute("waypoints") ?? this.waypoints);
|
|
131
|
+
if (waypoints.length < 2)
|
|
132
|
+
waypoints = [{ x: 0, y: 12 }, { x: 180, y: 12 }];
|
|
133
|
+
|
|
134
|
+
const padding = 16;
|
|
135
|
+
const minCrossAxisSize = 24;
|
|
136
|
+
const bounds = boundsFromWaypoints(waypoints);
|
|
137
|
+
const width = Math.max(minCrossAxisSize, Math.round(bounds.width + padding * 2));
|
|
138
|
+
const height = Math.max(minCrossAxisSize, Math.round(bounds.height + padding * 2));
|
|
139
|
+
const offsetX = (width - bounds.width) / 2;
|
|
140
|
+
const offsetY = (height - bounds.height) / 2;
|
|
141
|
+
|
|
142
|
+
this.style.left = `${Math.round(bounds.x - offsetX)}px`;
|
|
143
|
+
this.style.top = `${Math.round(bounds.y - offsetY)}px`;
|
|
144
|
+
this.style.width = `${width}px`;
|
|
145
|
+
this.style.height = `${height}px`;
|
|
146
|
+
|
|
147
|
+
const shifted = waypoints.map(point => ({ x: point.x - bounds.x + offsetX, y: point.y - bounds.y + offsetY }));
|
|
148
|
+
const middle = midpointFromWaypoints(shifted);
|
|
149
|
+
|
|
150
|
+
this._svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
|
|
151
|
+
this._path.setAttribute("d", pathDataFromWaypoints(shifted));
|
|
152
|
+
const edgeType = this.getAttribute("edge-type") as MermaidEdgeType ?? this.edgeType;
|
|
153
|
+
const connector = this.getAttribute("connector") ?? this.connector;
|
|
154
|
+
const label = this.getAttribute("label") ?? this.label ?? "";
|
|
155
|
+
this._path.setAttribute("marker-start", connector.startsWith("<") || edgeType === MermaidEdgeType.multi ? "url(#marker-start)" : "");
|
|
156
|
+
this._path.setAttribute("marker-end", getMarkerEnd(edgeType, connector));
|
|
157
|
+
this._path.style.strokeDasharray = edgeType === MermaidEdgeType.dotted ? "4 4" : "";
|
|
158
|
+
this._path.style.strokeWidth = edgeType === MermaidEdgeType.thick ? "4" : "2.5";
|
|
159
|
+
this._path.style.opacity = edgeType === MermaidEdgeType.invisible ? "0" : "";
|
|
160
|
+
this._label.textContent = label;
|
|
161
|
+
this._label.style.display = label ? "block" : "none";
|
|
162
|
+
this._label.setAttribute("x", middle.x.toString());
|
|
163
|
+
this._label.setAttribute("y", (middle.y - 8).toString());
|
|
164
|
+
this._label.setAttribute("text-anchor", "middle");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public createMermaid() {
|
|
168
|
+
const from = sanitizeId(this.getAttribute("from") ?? this.from);
|
|
169
|
+
const to = sanitizeId(this.getAttribute("to") ?? this.to);
|
|
170
|
+
const label = this.getAttribute("label") ?? this.label;
|
|
171
|
+
const explicitConnector = this.getAttribute("connector") ?? this.connector;
|
|
172
|
+
const connector = explicitConnector ? { plain: explicitConnector, label: explicitConnector } : getConnector(this.getAttribute("edge-type") as MermaidEdgeType ?? this.edgeType);
|
|
173
|
+
const rawEdgeId = this.getAttribute("edge-id") ?? this.edgeId;
|
|
174
|
+
const edgeId = rawEdgeId ? sanitizeId(rawEdgeId) : "";
|
|
175
|
+
const prefix = edgeId ? edgeId + "@" : "";
|
|
176
|
+
|
|
177
|
+
if (label)
|
|
178
|
+
return `${from} ${prefix}${connector.label}|${escapeLabel(label)}| ${to}`;
|
|
179
|
+
|
|
180
|
+
return `${from} ${prefix}${connector.plain} ${to}`;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function getMarkerEnd(edgeType: MermaidEdgeType, connector: string) {
|
|
185
|
+
if (edgeType === MermaidEdgeType.open || edgeType === MermaidEdgeType.invisible || connector.endsWith("---") || connector.endsWith("===") || connector.endsWith("-.-"))
|
|
186
|
+
return "";
|
|
187
|
+
if (edgeType === MermaidEdgeType.circle || connector.includes("o"))
|
|
188
|
+
return "url(#circle-marker)";
|
|
189
|
+
if (edgeType === MermaidEdgeType.cross || connector.includes("x"))
|
|
190
|
+
return "url(#cross-marker)";
|
|
191
|
+
return "url(#marker)";
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function getConnector(edgeType: MermaidEdgeType) {
|
|
195
|
+
switch (edgeType) {
|
|
196
|
+
case MermaidEdgeType.open:
|
|
197
|
+
return { plain: "---", label: "---" };
|
|
198
|
+
case MermaidEdgeType.dotted:
|
|
199
|
+
return { plain: "-.->", label: "-.->" };
|
|
200
|
+
case MermaidEdgeType.thick:
|
|
201
|
+
return { plain: "==>", label: "==>" };
|
|
202
|
+
case MermaidEdgeType.invisible:
|
|
203
|
+
return { plain: "~~~", label: "~~~" };
|
|
204
|
+
case MermaidEdgeType.circle:
|
|
205
|
+
return { plain: "--o", label: "--o" };
|
|
206
|
+
case MermaidEdgeType.cross:
|
|
207
|
+
return { plain: "--x", label: "--x" };
|
|
208
|
+
case MermaidEdgeType.multi:
|
|
209
|
+
return { plain: "<-->", label: "<-->" };
|
|
210
|
+
default:
|
|
211
|
+
return { plain: "-->", label: "-->" };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
customElements.define(MermaidEdge.is, MermaidEdge);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
|
|
2
|
+
|
|
3
|
+
export class MermaidFlowchartDirective extends BaseCustomWebComponentConstructorAppend {
|
|
4
|
+
static override readonly style = css`
|
|
5
|
+
:host {
|
|
6
|
+
background: #fffbe6;
|
|
7
|
+
border: 1px solid #d6b656;
|
|
8
|
+
color: #6b5600;
|
|
9
|
+
display: block;
|
|
10
|
+
font-family: Arial, sans-serif;
|
|
11
|
+
font-size: 11px;
|
|
12
|
+
overflow: hidden;
|
|
13
|
+
padding: 4px 6px;
|
|
14
|
+
text-overflow: ellipsis;
|
|
15
|
+
white-space: nowrap;
|
|
16
|
+
}
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
static override readonly template = html`<span id="text"></span>`;
|
|
20
|
+
|
|
21
|
+
static readonly is = "mermaid-flowchart-directive";
|
|
22
|
+
|
|
23
|
+
public line: string = "";
|
|
24
|
+
|
|
25
|
+
private _text: HTMLSpanElement;
|
|
26
|
+
|
|
27
|
+
static readonly properties = {
|
|
28
|
+
line: String,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
constructor() {
|
|
32
|
+
super();
|
|
33
|
+
this._restoreCachedInititalValues();
|
|
34
|
+
this._text = this._getDomElement<HTMLSpanElement>("text");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async ready() {
|
|
38
|
+
this._parseAttributesToProperties();
|
|
39
|
+
this._render();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
|
|
43
|
+
if (oldValue !== newValue) {
|
|
44
|
+
this._parseAttributesToProperties();
|
|
45
|
+
this._render();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private _render() {
|
|
50
|
+
this._text.textContent = this.getAttribute("line") ?? this.line;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public createMermaid() {
|
|
54
|
+
return this.getAttribute("line") ?? this.line;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
customElements.define(MermaidFlowchartDirective.is, MermaidFlowchartDirective);
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
|
|
2
|
+
import { formatMermaidLabel, renderMarkdownLabel, sanitizeId } from "./mermaid-node.js";
|
|
3
|
+
|
|
4
|
+
enum MermaidMindmapNodeShape {
|
|
5
|
+
default = "default",
|
|
6
|
+
square = "square",
|
|
7
|
+
round = "round",
|
|
8
|
+
circle = "circle",
|
|
9
|
+
bang = "bang",
|
|
10
|
+
cloud = "cloud",
|
|
11
|
+
hexagon = "hexagon",
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class MermaidMindmapNode extends BaseCustomWebComponentConstructorAppend {
|
|
15
|
+
static override readonly style = css`
|
|
16
|
+
* {
|
|
17
|
+
box-sizing: border-box;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
:host {
|
|
21
|
+
display: block;
|
|
22
|
+
overflow: visible;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#connections {
|
|
26
|
+
height: 100%;
|
|
27
|
+
left: 0;
|
|
28
|
+
overflow: visible;
|
|
29
|
+
pointer-events: none;
|
|
30
|
+
position: absolute;
|
|
31
|
+
top: 0;
|
|
32
|
+
width: 100%;
|
|
33
|
+
z-index: 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
#node {
|
|
37
|
+
align-items: center;
|
|
38
|
+
background: white;
|
|
39
|
+
border: 2px solid #333;
|
|
40
|
+
color: #111;
|
|
41
|
+
display: flex;
|
|
42
|
+
font-family: Arial, sans-serif;
|
|
43
|
+
font-size: 14px;
|
|
44
|
+
height: 100%;
|
|
45
|
+
justify-content: center;
|
|
46
|
+
min-height: 34px;
|
|
47
|
+
min-width: 84px;
|
|
48
|
+
overflow: hidden;
|
|
49
|
+
padding: 6px 12px;
|
|
50
|
+
pointer-events: none;
|
|
51
|
+
position: relative;
|
|
52
|
+
text-align: center;
|
|
53
|
+
width: 100%;
|
|
54
|
+
z-index: 1;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
::slotted(mermaid-mindmap-node) {
|
|
58
|
+
z-index: 1;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#node[data-shape="circle"] {
|
|
62
|
+
border-radius: 50%;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#node[data-shape="round"] {
|
|
66
|
+
border-radius: 12px;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#node[data-shape="bang"] {
|
|
70
|
+
clip-path: polygon(50% 0, 61% 34%, 98% 35%, 68% 56%, 79% 91%, 50% 70%, 21% 91%, 32% 56%, 2% 35%, 39% 34%);
|
|
71
|
+
padding: 16px;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#node[data-shape="cloud"] {
|
|
75
|
+
border-radius: 999px;
|
|
76
|
+
box-shadow: 12px 0 0 -2px white, 13px 0 0 0 #333, -12px 0 0 -2px white, -13px 0 0 0 #333;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
#node[data-shape="hexagon"] {
|
|
80
|
+
clip-path: polygon(20% 0, 80% 0, 100% 50%, 80% 100%, 20% 100%, 0 50%);
|
|
81
|
+
padding: 8px 22px;
|
|
82
|
+
}
|
|
83
|
+
`;
|
|
84
|
+
|
|
85
|
+
static override readonly template = html`<svg id="connections"></svg><div id="node"></div><slot id="children"></slot>`;
|
|
86
|
+
|
|
87
|
+
static readonly is = "mermaid-mindmap-node";
|
|
88
|
+
|
|
89
|
+
public mindmapId: string = "Root";
|
|
90
|
+
public label: string = "Node";
|
|
91
|
+
public shape: MermaidMindmapNodeShape = MermaidMindmapNodeShape.default;
|
|
92
|
+
|
|
93
|
+
private _node: HTMLDivElement;
|
|
94
|
+
private _connections: SVGSVGElement;
|
|
95
|
+
private _slot: HTMLSlotElement;
|
|
96
|
+
private _childObserver = new MutationObserver(() => this._renderConnections());
|
|
97
|
+
private _resizeObserver = new ResizeObserver(() => this._renderConnections());
|
|
98
|
+
|
|
99
|
+
static readonly properties = {
|
|
100
|
+
mindmapId: String,
|
|
101
|
+
label: String,
|
|
102
|
+
shape: MermaidMindmapNodeShape,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
constructor() {
|
|
106
|
+
super();
|
|
107
|
+
this._restoreCachedInititalValues();
|
|
108
|
+
this._node = this._getDomElement<HTMLDivElement>("node");
|
|
109
|
+
this._connections = this._getDomElement<SVGSVGElement>("connections");
|
|
110
|
+
this._slot = this._getDomElement<HTMLSlotElement>("children");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async ready() {
|
|
114
|
+
this._parseAttributesToProperties();
|
|
115
|
+
this._render();
|
|
116
|
+
this._slot.addEventListener("slotchange", () => this._observeChildren());
|
|
117
|
+
this._resizeObserver.observe(this);
|
|
118
|
+
this._observeChildren();
|
|
119
|
+
requestAnimationFrame(() => this._renderConnections());
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
|
|
123
|
+
if (oldValue !== newValue) {
|
|
124
|
+
this._parseAttributesToProperties();
|
|
125
|
+
this._render();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private _render() {
|
|
130
|
+
const label = this.getAttribute("label") ?? this.label ?? this.getAttribute("mindmap-id") ?? this.mindmapId;
|
|
131
|
+
const shape = this.getAttribute("shape") as MermaidMindmapNodeShape ?? this.shape;
|
|
132
|
+
renderMarkdownLabel(this._node, label);
|
|
133
|
+
this._node.dataset.shape = shape;
|
|
134
|
+
this._renderConnections();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private _observeChildren() {
|
|
138
|
+
this._childObserver.disconnect();
|
|
139
|
+
this._resizeObserver.disconnect();
|
|
140
|
+
this._resizeObserver.observe(this);
|
|
141
|
+
for (const child of this._getMindmapChildren()) {
|
|
142
|
+
this._childObserver.observe(child, { attributeFilter: ["style"], attributes: true });
|
|
143
|
+
this._resizeObserver.observe(child);
|
|
144
|
+
}
|
|
145
|
+
this._renderConnections();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private _renderConnections() {
|
|
149
|
+
this._connections.replaceChildren();
|
|
150
|
+
const children = this._getMindmapChildren();
|
|
151
|
+
if (!children.length)
|
|
152
|
+
return;
|
|
153
|
+
|
|
154
|
+
const hostWidth = this.offsetWidth;
|
|
155
|
+
const hostHeight = this.offsetHeight;
|
|
156
|
+
const source = { x: hostWidth / 2, y: hostHeight / 2 };
|
|
157
|
+
const strokeWidth = getConnectionStrokeWidth(this);
|
|
158
|
+
|
|
159
|
+
for (const child of children) {
|
|
160
|
+
const childLeft = Number.parseFloat(child.style.left || "0");
|
|
161
|
+
const childTop = Number.parseFloat(child.style.top || "0");
|
|
162
|
+
const childWidth = child.offsetWidth || Number.parseFloat(child.style.width || "0");
|
|
163
|
+
const childHeight = child.offsetHeight || Number.parseFloat(child.style.height || "0");
|
|
164
|
+
const target = { x: childLeft + childWidth / 2, y: childTop + childHeight / 2 };
|
|
165
|
+
const direction = target.x >= source.x ? 1 : -1;
|
|
166
|
+
const start = { x: source.x + direction * hostWidth / 2, y: source.y };
|
|
167
|
+
const end = { x: target.x - direction * childWidth / 2, y: target.y };
|
|
168
|
+
const controlDistance = Math.max(24, Math.abs(end.x - start.x) * 0.45);
|
|
169
|
+
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
170
|
+
path.setAttribute("d", `M ${start.x} ${start.y} C ${start.x + direction * controlDistance} ${start.y}, ${end.x - direction * controlDistance} ${end.y}, ${end.x} ${end.y}`);
|
|
171
|
+
path.setAttribute("fill", "none");
|
|
172
|
+
path.setAttribute("stroke", "#666");
|
|
173
|
+
path.setAttribute("stroke-linecap", "round");
|
|
174
|
+
path.setAttribute("stroke-width", strokeWidth.toString());
|
|
175
|
+
this._connections.appendChild(path);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private _getMindmapChildren() {
|
|
180
|
+
return this._slot.assignedElements().filter((element): element is MermaidMindmapNode => element.localName === MermaidMindmapNode.is);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
public createMermaid() {
|
|
184
|
+
const id = sanitizeId(this.getAttribute("mindmap-id") ?? this.mindmapId);
|
|
185
|
+
const label = formatMermaidLabel((this.getAttribute("label") ?? this.label) || id);
|
|
186
|
+
const shape = this.getAttribute("shape") as MermaidMindmapNodeShape ?? this.shape;
|
|
187
|
+
|
|
188
|
+
switch (shape) {
|
|
189
|
+
case MermaidMindmapNodeShape.square:
|
|
190
|
+
return `${id}[${label}]`;
|
|
191
|
+
case MermaidMindmapNodeShape.round:
|
|
192
|
+
return `${id}(${label})`;
|
|
193
|
+
case MermaidMindmapNodeShape.circle:
|
|
194
|
+
return `${id}((${label}))`;
|
|
195
|
+
case MermaidMindmapNodeShape.bang:
|
|
196
|
+
return `${id}))${label}((`;
|
|
197
|
+
case MermaidMindmapNodeShape.cloud:
|
|
198
|
+
return `${id})${label}(`;
|
|
199
|
+
case MermaidMindmapNodeShape.hexagon:
|
|
200
|
+
return `${id}{{${label}}}`;
|
|
201
|
+
default:
|
|
202
|
+
return label;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
customElements.define(MermaidMindmapNode.is, MermaidMindmapNode);
|
|
208
|
+
|
|
209
|
+
function getConnectionStrokeWidth(element: Element) {
|
|
210
|
+
let depth = 0;
|
|
211
|
+
let current = element.parentElement;
|
|
212
|
+
while (current) {
|
|
213
|
+
if (current.localName === MermaidMindmapNode.is)
|
|
214
|
+
depth++;
|
|
215
|
+
current = current.parentElement;
|
|
216
|
+
}
|
|
217
|
+
return Math.max(1.5, 5 - depth * 1.2);
|
|
218
|
+
}
|