@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,456 @@
|
|
|
1
|
+
import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
|
|
2
|
+
|
|
3
|
+
enum MermaidNodeShape {
|
|
4
|
+
rectangle = "rectangle",
|
|
5
|
+
round = "round",
|
|
6
|
+
stadium = "stadium",
|
|
7
|
+
circle = "circle",
|
|
8
|
+
decision = "decision",
|
|
9
|
+
subroutine = "subroutine",
|
|
10
|
+
cylinder = "cylinder",
|
|
11
|
+
asymmetric = "asymmetric",
|
|
12
|
+
hexagon = "hexagon",
|
|
13
|
+
parallelogram = "parallelogram",
|
|
14
|
+
parallelogramAlt = "parallelogramAlt",
|
|
15
|
+
trapezoid = "trapezoid",
|
|
16
|
+
trapezoidAlt = "trapezoidAlt",
|
|
17
|
+
doubleCircle = "doubleCircle",
|
|
18
|
+
bang = "bang",
|
|
19
|
+
cloud = "cloud",
|
|
20
|
+
hourglass = "hourglass",
|
|
21
|
+
bolt = "bolt",
|
|
22
|
+
brace = "brace",
|
|
23
|
+
braceRight = "brace-r",
|
|
24
|
+
braces = "braces",
|
|
25
|
+
dataStore = "datastore",
|
|
26
|
+
delay = "delay",
|
|
27
|
+
horizontalCylinder = "h-cyl",
|
|
28
|
+
linedCylinder = "lin-cyl",
|
|
29
|
+
curvedTrapezoid = "curv-trap",
|
|
30
|
+
dividedRectangle = "div-rect",
|
|
31
|
+
document = "doc",
|
|
32
|
+
triangle = "tri",
|
|
33
|
+
fork = "fork",
|
|
34
|
+
windowPane = "win-pane",
|
|
35
|
+
filledCircle = "f-circ",
|
|
36
|
+
linedDocument = "lin-doc",
|
|
37
|
+
linedRectangle = "lin-rect",
|
|
38
|
+
notchedPentagon = "notch-pent",
|
|
39
|
+
flippedTriangle = "flip-tri",
|
|
40
|
+
slopedRectangle = "sl-rect",
|
|
41
|
+
stackedDocument = "docs",
|
|
42
|
+
stackedRectangle = "st-rect",
|
|
43
|
+
flag = "flag",
|
|
44
|
+
bowTieRectangle = "bow-rect",
|
|
45
|
+
framedCircle = "fr-circ",
|
|
46
|
+
crossedCircle = "cross-circ",
|
|
47
|
+
taggedDocument = "tag-doc",
|
|
48
|
+
taggedRectangle = "tag-rect",
|
|
49
|
+
text = "text",
|
|
50
|
+
card = "notch-rect",
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class MermaidNode extends BaseCustomWebComponentConstructorAppend {
|
|
54
|
+
static override readonly style = css`
|
|
55
|
+
* {
|
|
56
|
+
box-sizing: border-box;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#node {
|
|
60
|
+
align-items: center;
|
|
61
|
+
background: white;
|
|
62
|
+
border: 2px solid #333;
|
|
63
|
+
color: #111;
|
|
64
|
+
display: flex;
|
|
65
|
+
font-family: Arial, sans-serif;
|
|
66
|
+
font-size: 14px;
|
|
67
|
+
height: 100%;
|
|
68
|
+
justify-content: center;
|
|
69
|
+
min-height: 32px;
|
|
70
|
+
min-width: 64px;
|
|
71
|
+
overflow: hidden;
|
|
72
|
+
white-space: pre-line;
|
|
73
|
+
padding: 6px 10px;
|
|
74
|
+
pointer-events: none;
|
|
75
|
+
text-align: center;
|
|
76
|
+
width: 100%;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
#node[data-shape="decision"] {
|
|
80
|
+
clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
|
|
81
|
+
padding: 10px 22px;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#node[data-shape="subroutine"] {
|
|
85
|
+
box-shadow: inset 8px 0 0 white, inset 10px 0 0 #333, inset -8px 0 0 white, inset -10px 0 0 #333;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#node[data-shape="cylinder"] {
|
|
89
|
+
border-radius: 50% / 14%;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
#node[data-shape="asymmetric"] {
|
|
93
|
+
clip-path: polygon(0 0, 86% 0, 100% 50%, 86% 100%, 0 100%);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#node[data-shape="hexagon"] {
|
|
97
|
+
clip-path: polygon(20% 0, 80% 0, 100% 50%, 80% 100%, 20% 100%, 0 50%);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#node[data-shape="parallelogram"] {
|
|
101
|
+
clip-path: polygon(14% 0, 100% 0, 86% 100%, 0 100%);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
#node[data-shape="parallelogramAlt"] {
|
|
105
|
+
clip-path: polygon(0 0, 86% 0, 100% 100%, 14% 100%);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
#node[data-shape="trapezoid"] {
|
|
109
|
+
clip-path: polygon(14% 0, 86% 0, 100% 100%, 0 100%);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
#node[data-shape="trapezoidAlt"] {
|
|
113
|
+
clip-path: polygon(0 0, 100% 0, 86% 100%, 14% 100%);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
#node[data-shape="doubleCircle"] {
|
|
117
|
+
border-radius: 50%;
|
|
118
|
+
box-shadow: inset 0 0 0 4px white, inset 0 0 0 6px #333;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#node[data-shape="bang"] {
|
|
122
|
+
clip-path: polygon(50% 0, 61% 34%, 98% 35%, 68% 56%, 79% 91%, 50% 70%, 21% 91%, 32% 56%, 2% 35%, 39% 34%);
|
|
123
|
+
padding: 14px;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#node[data-shape="cloud"] {
|
|
127
|
+
border-radius: 999px;
|
|
128
|
+
box-shadow: 12px 0 0 -2px white, 13px 0 0 0 #333, -12px 0 0 -2px white, -13px 0 0 0 #333;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
#node[data-shape="hourglass"] {
|
|
132
|
+
clip-path: polygon(0 0, 100% 0, 62% 50%, 100% 100%, 0 100%, 38% 50%);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
#node[data-shape="bolt"] {
|
|
136
|
+
clip-path: polygon(44% 0, 78% 0, 56% 38%, 82% 38%, 31% 100%, 45% 52%, 20% 52%);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
#node[data-shape="brace"],
|
|
140
|
+
#node[data-shape="brace-r"],
|
|
141
|
+
#node[data-shape="braces"] {
|
|
142
|
+
border-color: transparent;
|
|
143
|
+
font-size: 15px;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
#node[data-shape="brace"]::before,
|
|
147
|
+
#node[data-shape="braces"]::before {
|
|
148
|
+
content: "{";
|
|
149
|
+
font-size: 42px;
|
|
150
|
+
line-height: 1;
|
|
151
|
+
margin-right: 6px;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
#node[data-shape="brace-r"]::after,
|
|
155
|
+
#node[data-shape="braces"]::after {
|
|
156
|
+
content: "}";
|
|
157
|
+
font-size: 42px;
|
|
158
|
+
line-height: 1;
|
|
159
|
+
margin-left: 6px;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
#node[data-shape="datastore"] {
|
|
163
|
+
border-left-color: transparent;
|
|
164
|
+
border-right-color: transparent;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
#node[data-shape="delay"] {
|
|
168
|
+
border-radius: 0 999px 999px 0;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
#node[data-shape="h-cyl"],
|
|
172
|
+
#node[data-shape="lin-cyl"] {
|
|
173
|
+
border-radius: 22% / 50%;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
#node[data-shape="curv-trap"] {
|
|
177
|
+
clip-path: polygon(10% 0, 100% 0, 90% 100%, 0 100%);
|
|
178
|
+
border-radius: 45% 8% 45% 8% / 16%;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
#node[data-shape="div-rect"],
|
|
182
|
+
#node[data-shape="lin-rect"],
|
|
183
|
+
#node[data-shape="win-pane"] {
|
|
184
|
+
box-shadow: inset 0 -16px 0 rgba(0, 0, 0, 0.08);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#node[data-shape="doc"],
|
|
188
|
+
#node[data-shape="lin-doc"],
|
|
189
|
+
#node[data-shape="docs"],
|
|
190
|
+
#node[data-shape="tag-doc"] {
|
|
191
|
+
border-radius: 0 0 16px 16px / 0 0 8px 8px;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#node[data-shape="tri"] {
|
|
195
|
+
clip-path: polygon(50% 0, 100% 100%, 0 100%);
|
|
196
|
+
padding-top: 20px;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
#node[data-shape="flip-tri"] {
|
|
200
|
+
clip-path: polygon(0 0, 100% 0, 50% 100%);
|
|
201
|
+
padding-bottom: 20px;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
#node[data-shape="fork"] {
|
|
205
|
+
min-height: 16px;
|
|
206
|
+
background: #333;
|
|
207
|
+
color: white;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
#node[data-shape="f-circ"] {
|
|
211
|
+
background: #333;
|
|
212
|
+
border-radius: 50%;
|
|
213
|
+
color: white;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
#node[data-shape="notch-pent"] {
|
|
217
|
+
clip-path: polygon(12% 0, 88% 0, 100% 50%, 88% 100%, 12% 100%, 0 50%);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
#node[data-shape="sl-rect"] {
|
|
221
|
+
clip-path: polygon(12% 0, 100% 0, 88% 100%, 0 100%);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
#node[data-shape="flag"] {
|
|
225
|
+
clip-path: polygon(0 0, 100% 0, 86% 50%, 100% 100%, 0 100%);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
#node[data-shape="bow-rect"] {
|
|
229
|
+
clip-path: polygon(0 0, 35% 0, 50% 50%, 65% 0, 100% 0, 100% 100%, 65% 100%, 50% 50%, 35% 100%, 0 100%);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
#node[data-shape="fr-circ"] {
|
|
233
|
+
border-radius: 50%;
|
|
234
|
+
box-shadow: inset 0 0 0 4px white, inset 0 0 0 6px #333;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
#node[data-shape="cross-circ"] {
|
|
238
|
+
border-radius: 50%;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
#node[data-shape="cross-circ"]::before {
|
|
242
|
+
content: "×";
|
|
243
|
+
font-size: 28px;
|
|
244
|
+
margin-right: 4px;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
#node[data-shape="tag-rect"],
|
|
248
|
+
#node[data-shape="tag-doc"],
|
|
249
|
+
#node[data-shape="notch-rect"] {
|
|
250
|
+
clip-path: polygon(10% 0, 100% 0, 100% 100%, 10% 100%, 0 50%);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
#node[data-shape="text"] {
|
|
254
|
+
background: transparent;
|
|
255
|
+
border-color: transparent;
|
|
256
|
+
}
|
|
257
|
+
`;
|
|
258
|
+
|
|
259
|
+
static override readonly template = html`<div id="node"></div>`;
|
|
260
|
+
|
|
261
|
+
static readonly is = "mermaid-node";
|
|
262
|
+
|
|
263
|
+
public nodeId: string = "A";
|
|
264
|
+
public label: string = "Node";
|
|
265
|
+
public shape: MermaidNodeShape = MermaidNodeShape.rectangle;
|
|
266
|
+
|
|
267
|
+
private _node: HTMLDivElement;
|
|
268
|
+
|
|
269
|
+
static readonly properties = {
|
|
270
|
+
nodeId: String,
|
|
271
|
+
label: String,
|
|
272
|
+
shape: MermaidNodeShape,
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
constructor() {
|
|
276
|
+
super();
|
|
277
|
+
this._restoreCachedInititalValues();
|
|
278
|
+
this._node = this._getDomElement<HTMLDivElement>("node");
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async ready() {
|
|
282
|
+
this._parseAttributesToProperties();
|
|
283
|
+
this._render();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
private _render() {
|
|
287
|
+
const label = this.getAttribute("label") ?? this.label;
|
|
288
|
+
const nodeId = this.getAttribute("node-id") ?? this.nodeId;
|
|
289
|
+
const shape = this.getAttribute("shape") as MermaidNodeShape ?? this.shape;
|
|
290
|
+
renderMarkdownLabel(this._node, label || nodeId);
|
|
291
|
+
this._node.dataset.shape = shape;
|
|
292
|
+
this._node.style.clipPath = "";
|
|
293
|
+
this._node.style.boxShadow = "";
|
|
294
|
+
switch (shape) {
|
|
295
|
+
case MermaidNodeShape.round:
|
|
296
|
+
this._node.style.borderRadius = "12px";
|
|
297
|
+
break;
|
|
298
|
+
case MermaidNodeShape.stadium:
|
|
299
|
+
this._node.style.borderRadius = "999px";
|
|
300
|
+
break;
|
|
301
|
+
case MermaidNodeShape.circle:
|
|
302
|
+
case MermaidNodeShape.doubleCircle:
|
|
303
|
+
case MermaidNodeShape.framedCircle:
|
|
304
|
+
case MermaidNodeShape.crossedCircle:
|
|
305
|
+
case MermaidNodeShape.filledCircle:
|
|
306
|
+
this._node.style.borderRadius = "50%";
|
|
307
|
+
break;
|
|
308
|
+
case MermaidNodeShape.cylinder:
|
|
309
|
+
case MermaidNodeShape.horizontalCylinder:
|
|
310
|
+
case MermaidNodeShape.linedCylinder:
|
|
311
|
+
this._node.style.borderRadius = "50% / 14%";
|
|
312
|
+
break;
|
|
313
|
+
default:
|
|
314
|
+
this._node.style.borderRadius = "0";
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
public createMermaid() {
|
|
320
|
+
const id = sanitizeId(this.getAttribute("node-id") ?? this.nodeId);
|
|
321
|
+
const label = formatMermaidLabel((this.getAttribute("label") ?? this.label) || id);
|
|
322
|
+
const shape = this.getAttribute("shape") as MermaidNodeShape ?? this.shape;
|
|
323
|
+
|
|
324
|
+
switch (shape) {
|
|
325
|
+
case MermaidNodeShape.round:
|
|
326
|
+
return `${id}(${label})`;
|
|
327
|
+
case MermaidNodeShape.stadium:
|
|
328
|
+
return `${id}([${label}])`;
|
|
329
|
+
case MermaidNodeShape.circle:
|
|
330
|
+
return `${id}((${label}))`;
|
|
331
|
+
case MermaidNodeShape.decision:
|
|
332
|
+
return `${id}{${label}}`;
|
|
333
|
+
case MermaidNodeShape.subroutine:
|
|
334
|
+
return `${id}[[${label}]]`;
|
|
335
|
+
case MermaidNodeShape.cylinder:
|
|
336
|
+
return `${id}[(${label})]`;
|
|
337
|
+
case MermaidNodeShape.asymmetric:
|
|
338
|
+
return `${id}>${label}]`;
|
|
339
|
+
case MermaidNodeShape.hexagon:
|
|
340
|
+
return `${id}{{${label}}}`;
|
|
341
|
+
case MermaidNodeShape.parallelogram:
|
|
342
|
+
return `${id}[/${label}/]`;
|
|
343
|
+
case MermaidNodeShape.parallelogramAlt:
|
|
344
|
+
return `${id}[\\${label}\\]`;
|
|
345
|
+
case MermaidNodeShape.trapezoid:
|
|
346
|
+
return `${id}[/${label}\\]`;
|
|
347
|
+
case MermaidNodeShape.trapezoidAlt:
|
|
348
|
+
return `${id}[\\${label}/]`;
|
|
349
|
+
case MermaidNodeShape.doubleCircle:
|
|
350
|
+
return `${id}(((${label})))`;
|
|
351
|
+
default:
|
|
352
|
+
if (isExpandedShape(shape))
|
|
353
|
+
return `${id}@{ shape: ${shape}, label: ${label} }`;
|
|
354
|
+
return `${id}[${label}]`;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function isExpandedShape(shape: string) {
|
|
360
|
+
return !!shape && shape !== MermaidNodeShape.rectangle;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function sanitizeId(id: string) {
|
|
364
|
+
return (id || "A").replace(/[^a-zA-Z0-9_]/g, "_");
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function escapeLabel(label: string) {
|
|
368
|
+
return (label || "").replaceAll("\"", "#quot;");
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function formatMermaidLabel(label: string) {
|
|
372
|
+
const escaped = escapeLabel(label);
|
|
373
|
+
if (shouldWriteMarkdownLabel(escaped))
|
|
374
|
+
return "\"`" + escaped + "`\"";
|
|
375
|
+
return escaped.includes("`") ? `"${escaped}"` : escaped;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export function stripMarkdownLabel(label: string) {
|
|
379
|
+
return normalizeLabelLineBreaks((label || "").replace(/^`|`$/g, ""));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function renderMarkdownLabel(container: HTMLElement | SVGTextElement, label: string) {
|
|
383
|
+
container.replaceChildren();
|
|
384
|
+
const normalizedLabel = stripMarkdownLabel(label);
|
|
385
|
+
const lines = splitMarkdownLines(normalizedLabel);
|
|
386
|
+
for (let i = 0; i < lines.length; i++) {
|
|
387
|
+
if (i > 0)
|
|
388
|
+
container.appendChild(document.createElement("br"));
|
|
389
|
+
renderInlineMarkdown(container, lines[i]);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function renderInlineMarkdown(container: Element, text: string) {
|
|
394
|
+
let position = 0;
|
|
395
|
+
while (position < text.length) {
|
|
396
|
+
const nextDelimiter = findNextMarkdownDelimiter(text, position);
|
|
397
|
+
if (!nextDelimiter) {
|
|
398
|
+
appendText(container, text.substring(position));
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (nextDelimiter.index > position)
|
|
403
|
+
appendText(container, text.substring(position, nextDelimiter.index));
|
|
404
|
+
|
|
405
|
+
const end = text.indexOf(nextDelimiter.token, nextDelimiter.index + nextDelimiter.token.length);
|
|
406
|
+
if (end < 0) {
|
|
407
|
+
appendText(container, text.substring(nextDelimiter.index, nextDelimiter.index + nextDelimiter.token.length));
|
|
408
|
+
position = nextDelimiter.index + nextDelimiter.token.length;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const element = document.createElement(nextDelimiter.token.length === 2 ? "strong" : "em");
|
|
413
|
+
renderInlineMarkdown(element, text.substring(nextDelimiter.index + nextDelimiter.token.length, end));
|
|
414
|
+
container.appendChild(element);
|
|
415
|
+
position = end + nextDelimiter.token.length;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function findNextMarkdownDelimiter(text: string, start: number) {
|
|
420
|
+
const tokens = ["**", "__", "*", "_"];
|
|
421
|
+
let result: { token: string; index: number } = null;
|
|
422
|
+
for (const token of tokens) {
|
|
423
|
+
const index = text.indexOf(token, start);
|
|
424
|
+
if (index >= 0 && (!result || index < result.index || (index === result.index && token.length > result.token.length)))
|
|
425
|
+
result = { token, index };
|
|
426
|
+
}
|
|
427
|
+
return result;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function appendText(container: Element, text: string) {
|
|
431
|
+
if (text)
|
|
432
|
+
container.appendChild(document.createTextNode(text));
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function splitMarkdownLines(text: string) {
|
|
436
|
+
const lines: string[] = [];
|
|
437
|
+
let start = 0;
|
|
438
|
+
for (let i = 0; i < text.length; i++) {
|
|
439
|
+
if (text[i] === "\n") {
|
|
440
|
+
lines.push(text.substring(start, i));
|
|
441
|
+
start = i + 1;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
lines.push(text.substring(start));
|
|
445
|
+
return lines;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function shouldWriteMarkdownLabel(label: string) {
|
|
449
|
+
return label.includes("\n") || label.includes("**") || label.includes("_") || label.includes("*");
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
export function normalizeLabelLineBreaks(label: string) {
|
|
453
|
+
return label.replaceAll("<br>", "\n").replaceAll("<br/>", "\n").replaceAll("<br />", "\n");
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
customElements.define(MermaidNode.is, MermaidNode);
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
|
|
2
|
+
import { escapeLabel, renderMarkdownLabel, sanitizeId } from "./mermaid-node.js";
|
|
3
|
+
|
|
4
|
+
type RequirementNodeKind = "requirement" | "element";
|
|
5
|
+
|
|
6
|
+
export class MermaidRequirementNode extends BaseCustomWebComponentConstructorAppend {
|
|
7
|
+
static override readonly style = css`
|
|
8
|
+
* {
|
|
9
|
+
box-sizing: border-box;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
:host {
|
|
13
|
+
background: #fff;
|
|
14
|
+
border: 2px solid #333;
|
|
15
|
+
color: #111;
|
|
16
|
+
display: block;
|
|
17
|
+
font-family: Arial, sans-serif;
|
|
18
|
+
font-size: 12px;
|
|
19
|
+
overflow: hidden;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
#header {
|
|
23
|
+
border-bottom: 1px solid #666;
|
|
24
|
+
padding: 6px 8px;
|
|
25
|
+
text-align: center;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
#stereotype {
|
|
29
|
+
display: block;
|
|
30
|
+
font-size: 11px;
|
|
31
|
+
line-height: 1.25;
|
|
32
|
+
margin-bottom: 2px;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
#name {
|
|
36
|
+
font-weight: 600;
|
|
37
|
+
line-height: 1.25;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
#body {
|
|
41
|
+
display: grid;
|
|
42
|
+
gap: 4px;
|
|
43
|
+
padding: 8px;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.row {
|
|
47
|
+
display: grid;
|
|
48
|
+
grid-template-columns: max-content 1fr;
|
|
49
|
+
gap: 6px;
|
|
50
|
+
min-width: 0;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.label {
|
|
54
|
+
color: #555;
|
|
55
|
+
font-weight: 600;
|
|
56
|
+
white-space: nowrap;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.value {
|
|
60
|
+
min-width: 0;
|
|
61
|
+
overflow-wrap: anywhere;
|
|
62
|
+
white-space: pre-line;
|
|
63
|
+
}
|
|
64
|
+
`;
|
|
65
|
+
|
|
66
|
+
static override readonly template = html`
|
|
67
|
+
<div id="header">
|
|
68
|
+
<span id="stereotype"></span>
|
|
69
|
+
<div id="name"></div>
|
|
70
|
+
</div>
|
|
71
|
+
<div id="body"></div>`;
|
|
72
|
+
|
|
73
|
+
static readonly is = "mermaid-requirement-node";
|
|
74
|
+
|
|
75
|
+
public nodeKind: RequirementNodeKind = "requirement";
|
|
76
|
+
public requirementType: string = "requirement";
|
|
77
|
+
public requirementId: string = "Req";
|
|
78
|
+
public label: string = "Req";
|
|
79
|
+
public text: string = "";
|
|
80
|
+
public risk: string = "low";
|
|
81
|
+
public verifyMethod: string = "test";
|
|
82
|
+
public elementType: string = "";
|
|
83
|
+
public docRef: string = "";
|
|
84
|
+
|
|
85
|
+
private _body: HTMLDivElement;
|
|
86
|
+
private _name: HTMLDivElement;
|
|
87
|
+
private _stereotype: HTMLSpanElement;
|
|
88
|
+
|
|
89
|
+
static readonly properties = {
|
|
90
|
+
nodeKind: String,
|
|
91
|
+
requirementType: String,
|
|
92
|
+
requirementId: String,
|
|
93
|
+
label: String,
|
|
94
|
+
text: String,
|
|
95
|
+
risk: String,
|
|
96
|
+
verifyMethod: String,
|
|
97
|
+
elementType: String,
|
|
98
|
+
docRef: String,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
constructor() {
|
|
102
|
+
super();
|
|
103
|
+
this._restoreCachedInititalValues();
|
|
104
|
+
this._body = this._getDomElement<HTMLDivElement>("body");
|
|
105
|
+
this._name = this._getDomElement<HTMLDivElement>("name");
|
|
106
|
+
this._stereotype = this._getDomElement<HTMLSpanElement>("stereotype");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async ready() {
|
|
110
|
+
this._parseAttributesToProperties();
|
|
111
|
+
this._render();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
|
|
115
|
+
if (oldValue !== newValue) {
|
|
116
|
+
this._parseAttributesToProperties();
|
|
117
|
+
this._render();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private _render() {
|
|
122
|
+
const kind = this.getAttribute("node-kind") as RequirementNodeKind ?? this.nodeKind;
|
|
123
|
+
const name = this.getAttribute("label") ?? this.label;
|
|
124
|
+
this._stereotype.textContent = kind === "element" ? "<<Element>>" : "<<" + getRequirementTypeLabel(this.getAttribute("requirement-type") ?? this.requirementType) + ">>";
|
|
125
|
+
renderMarkdownLabel(this._name, name);
|
|
126
|
+
this._body.replaceChildren();
|
|
127
|
+
|
|
128
|
+
if (kind === "element") {
|
|
129
|
+
this._appendRow("Type", this.getAttribute("element-type") ?? this.elementType);
|
|
130
|
+
this._appendRow("DocRef", this.getAttribute("doc-ref") ?? this.docRef);
|
|
131
|
+
} else {
|
|
132
|
+
this._appendRow("Id", this.getAttribute("requirement-id") ?? this.requirementId);
|
|
133
|
+
this._appendRow("Text", this.getAttribute("text") ?? this.text);
|
|
134
|
+
this._appendRow("Risk", this.getAttribute("risk") ?? this.risk);
|
|
135
|
+
this._appendRow("Verification", this.getAttribute("verify-method") ?? this.verifyMethod);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private _appendRow(label: string, value: string) {
|
|
140
|
+
if (!value)
|
|
141
|
+
return;
|
|
142
|
+
|
|
143
|
+
const row = document.createElement("div");
|
|
144
|
+
row.className = "row";
|
|
145
|
+
const labelElement = document.createElement("span");
|
|
146
|
+
labelElement.className = "label";
|
|
147
|
+
labelElement.textContent = label + ":";
|
|
148
|
+
const valueElement = document.createElement("span");
|
|
149
|
+
valueElement.className = "value";
|
|
150
|
+
renderMarkdownLabel(valueElement, value);
|
|
151
|
+
row.append(labelElement, valueElement);
|
|
152
|
+
this._body.appendChild(row);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public createMermaid() {
|
|
156
|
+
const kind = this.getAttribute("node-kind") as RequirementNodeKind ?? this.nodeKind;
|
|
157
|
+
const name = sanitizeId(this.getAttribute("label") ?? this.label);
|
|
158
|
+
if (kind === "element") {
|
|
159
|
+
const lines = [`element ${name} {`];
|
|
160
|
+
const type = this.getAttribute("element-type") ?? this.elementType;
|
|
161
|
+
const docRef = this.getAttribute("doc-ref") ?? this.docRef;
|
|
162
|
+
if (type)
|
|
163
|
+
lines.push("type: " + formatRequirementValue(type));
|
|
164
|
+
if (docRef)
|
|
165
|
+
lines.push("docRef: " + formatRequirementValue(docRef));
|
|
166
|
+
lines.push("}");
|
|
167
|
+
return lines.join("\n");
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const requirementType = this.getAttribute("requirement-type") ?? this.requirementType;
|
|
171
|
+
const lines = [`${requirementType} ${name} {`];
|
|
172
|
+
lines.push("id: " + formatRequirementValue(this.getAttribute("requirement-id") ?? this.requirementId));
|
|
173
|
+
lines.push("text: " + formatRequirementValue(this.getAttribute("text") ?? this.text));
|
|
174
|
+
lines.push("risk: " + formatRequirementValue(this.getAttribute("risk") ?? this.risk));
|
|
175
|
+
lines.push("verifymethod: " + formatRequirementValue(this.getAttribute("verify-method") ?? this.verifyMethod));
|
|
176
|
+
lines.push("}");
|
|
177
|
+
return lines.join("\n");
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
customElements.define(MermaidRequirementNode.is, MermaidRequirementNode);
|
|
182
|
+
|
|
183
|
+
function getRequirementTypeLabel(type: string) {
|
|
184
|
+
switch (type) {
|
|
185
|
+
case "functionalRequirement":
|
|
186
|
+
return "Functional Requirement";
|
|
187
|
+
case "interfaceRequirement":
|
|
188
|
+
return "Interface Requirement";
|
|
189
|
+
case "performanceRequirement":
|
|
190
|
+
return "Performance Requirement";
|
|
191
|
+
case "physicalRequirement":
|
|
192
|
+
return "Physical Requirement";
|
|
193
|
+
case "designConstraint":
|
|
194
|
+
return "Design Constraint";
|
|
195
|
+
default:
|
|
196
|
+
return "Requirement";
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function formatRequirementValue(value: string) {
|
|
201
|
+
return escapeLabel(value ?? "");
|
|
202
|
+
}
|