@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.
@@ -0,0 +1,139 @@
1
+ import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
2
+ import { boundsFromWaypoints, decodeWaypoints, midpointFromWaypoints, pathDataFromWaypoints } from "../services/mermaidGeometry.js";
3
+ import { sanitizeId } from "./mermaid-node.js";
4
+
5
+ export class MermaidRequirementRelationship extends BaseCustomWebComponentConstructorAppend {
6
+ static override readonly style = css`
7
+ :host {
8
+ display: block;
9
+ overflow: visible;
10
+ pointer-events: auto;
11
+ position: absolute;
12
+ z-index: 4;
13
+ }
14
+
15
+ svg {
16
+ height: 100%;
17
+ overflow: visible;
18
+ width: 100%;
19
+ }
20
+
21
+ path {
22
+ fill: none;
23
+ stroke: #333;
24
+ stroke-linecap: round;
25
+ stroke-linejoin: round;
26
+ stroke-width: 2;
27
+ vector-effect: non-scaling-stroke;
28
+ }
29
+
30
+ #marker-shape {
31
+ fill: #333;
32
+ }
33
+
34
+ text {
35
+ fill: #333;
36
+ font-family: Arial, sans-serif;
37
+ font-size: 12px;
38
+ pointer-events: none;
39
+ }
40
+ `;
41
+
42
+ static override readonly template = html`
43
+ <svg id="svg" viewBox="0 0 10 10" preserveAspectRatio="none">
44
+ <defs>
45
+ <marker id="marker" viewBox="0 0 12 12" markerWidth="10" markerHeight="10" refX="10.5" refY="6" orient="auto" markerUnits="userSpaceOnUse">
46
+ <path id="marker-shape" d="M 1 1 L 10.5 6 L 1 11 z"></path>
47
+ </marker>
48
+ </defs>
49
+ <path id="path" marker-end="url(#marker)"></path>
50
+ <text id="label"></text>
51
+ </svg>`;
52
+
53
+ static readonly is = "mermaid-requirement-relationship";
54
+
55
+ public from: string = "source";
56
+ public to: string = "target";
57
+ public relationshipType: string = "satisfies";
58
+ public syntaxDirection: string = "forward";
59
+ public waypoints: string = "";
60
+ private _previewWaypoints: string;
61
+
62
+ private _label: SVGTextElement;
63
+ private _path: SVGPathElement;
64
+ private _svg: SVGSVGElement;
65
+
66
+ static readonly properties = {
67
+ from: String,
68
+ to: String,
69
+ relationshipType: String,
70
+ syntaxDirection: String,
71
+ waypoints: String,
72
+ }
73
+
74
+ constructor() {
75
+ super();
76
+ this._restoreCachedInititalValues();
77
+ this._label = this._getDomElement<SVGTextElement>("label");
78
+ this._path = this._getDomElement<SVGPathElement>("path");
79
+ this._svg = this._getDomElement<SVGSVGElement>("svg");
80
+ }
81
+
82
+ async ready() {
83
+ this._parseAttributesToProperties();
84
+ this._render();
85
+ }
86
+
87
+ attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
88
+ if (oldValue !== newValue) {
89
+ this._parseAttributesToProperties();
90
+ this._render();
91
+ }
92
+ }
93
+
94
+ public setPreviewWaypoints(waypoints: string | null) {
95
+ this._previewWaypoints = waypoints;
96
+ this._render();
97
+ }
98
+
99
+ private _render() {
100
+ let waypoints = decodeWaypoints(this._previewWaypoints ?? this.getAttribute("waypoints") ?? this.waypoints);
101
+ if (waypoints.length < 2)
102
+ waypoints = [{ x: 0, y: 12 }, { x: 180, y: 12 }];
103
+
104
+ const padding = 16;
105
+ const bounds = boundsFromWaypoints(waypoints);
106
+ const width = Math.max(24, Math.round(bounds.width + padding * 2));
107
+ const height = Math.max(24, Math.round(bounds.height + padding * 2));
108
+ const offsetX = (width - bounds.width) / 2;
109
+ const offsetY = (height - bounds.height) / 2;
110
+
111
+ this.style.left = `${Math.round(bounds.x - offsetX)}px`;
112
+ this.style.top = `${Math.round(bounds.y - offsetY)}px`;
113
+ this.style.width = `${width}px`;
114
+ this.style.height = `${height}px`;
115
+
116
+ const shifted = waypoints.map(point => ({ x: point.x - bounds.x + offsetX, y: point.y - bounds.y + offsetY }));
117
+ const middle = midpointFromWaypoints(shifted);
118
+ const relationshipType = this.getAttribute("relationship-type") ?? this.relationshipType;
119
+
120
+ this._svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
121
+ this._path.setAttribute("d", pathDataFromWaypoints(shifted));
122
+ this._label.textContent = relationshipType;
123
+ this._label.setAttribute("x", middle.x.toString());
124
+ this._label.setAttribute("y", (middle.y - 8).toString());
125
+ this._label.setAttribute("text-anchor", "middle");
126
+ }
127
+
128
+ public createMermaid() {
129
+ const from = sanitizeId(this.getAttribute("from") ?? this.from);
130
+ const to = sanitizeId(this.getAttribute("to") ?? this.to);
131
+ const relationshipType = this.getAttribute("relationship-type") ?? this.relationshipType;
132
+ const syntaxDirection = this.getAttribute("syntax-direction") ?? this.syntaxDirection;
133
+ if (syntaxDirection === "reverse")
134
+ return `${to} <- ${relationshipType} - ${from}`;
135
+ return `${from} - ${relationshipType} -> ${to}`;
136
+ }
137
+ }
138
+
139
+ customElements.define(MermaidRequirementRelationship.is, MermaidRequirementRelationship);
@@ -0,0 +1,145 @@
1
+ import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
2
+ import { escapeLabel, sanitizeId } from "./mermaid-node.js";
3
+
4
+ enum MermaidSequenceMessageType {
5
+ solid = "solid",
6
+ dotted = "dotted",
7
+ open = "open",
8
+ cross = "cross",
9
+ async = "async",
10
+ }
11
+
12
+ export class MermaidSequenceMessage extends BaseCustomWebComponentConstructorAppend {
13
+ static override readonly style = css`
14
+ * {
15
+ box-sizing: border-box;
16
+ }
17
+
18
+ :host {
19
+ display: block;
20
+ overflow: visible;
21
+ pointer-events: auto;
22
+ position: absolute;
23
+ z-index: 4;
24
+ }
25
+
26
+ svg {
27
+ height: 100%;
28
+ overflow: visible;
29
+ width: 100%;
30
+ }
31
+
32
+ path {
33
+ fill: none;
34
+ stroke: #333;
35
+ stroke-linecap: round;
36
+ stroke-linejoin: round;
37
+ stroke-width: 2;
38
+ vector-effect: non-scaling-stroke;
39
+ }
40
+
41
+ #marker-shape {
42
+ fill: #333;
43
+ }
44
+
45
+ text {
46
+ fill: #333;
47
+ font-family: Arial, sans-serif;
48
+ font-size: 12px;
49
+ pointer-events: none;
50
+ }
51
+ `;
52
+
53
+ static override readonly template = html`
54
+ <svg id="svg" viewBox="0 0 10 10" preserveAspectRatio="none">
55
+ <defs>
56
+ <marker id="marker" viewBox="0 0 12 12" markerWidth="10" markerHeight="10" refX="10.5" refY="6" orient="auto" markerUnits="userSpaceOnUse">
57
+ <path id="marker-shape" d="M 1 1 L 10.5 6 L 1 11 z"></path>
58
+ </marker>
59
+ </defs>
60
+ <path id="path"></path>
61
+ <text id="label"></text>
62
+ </svg>`;
63
+
64
+ static readonly is = "mermaid-sequence-message";
65
+
66
+ public from: string = "Alice";
67
+ public to: string = "Bob";
68
+ public label: string = "Message";
69
+ public messageType: MermaidSequenceMessageType = MermaidSequenceMessageType.solid;
70
+ public connector: string = "->>";
71
+
72
+ private _label: SVGTextElement;
73
+ private _path: SVGPathElement;
74
+ private _svg: SVGSVGElement;
75
+
76
+ static readonly properties = {
77
+ from: String,
78
+ to: String,
79
+ label: String,
80
+ messageType: MermaidSequenceMessageType,
81
+ connector: String,
82
+ }
83
+
84
+ constructor() {
85
+ super();
86
+ this._restoreCachedInititalValues();
87
+ this._label = this._getDomElement<SVGTextElement>("label");
88
+ this._path = this._getDomElement<SVGPathElement>("path");
89
+ this._svg = this._getDomElement<SVGSVGElement>("svg");
90
+ }
91
+
92
+ async ready() {
93
+ this._parseAttributesToProperties();
94
+ this._render();
95
+ }
96
+
97
+ attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
98
+ if (oldValue !== newValue) {
99
+ this._parseAttributesToProperties();
100
+ this._render();
101
+ }
102
+ }
103
+
104
+ private _render() {
105
+ const width = Math.max(1, parseFloat(this.style.width || "220"));
106
+ const height = Math.max(28, parseFloat(this.style.height || "36"));
107
+ const y = height / 2;
108
+ const messageType = this.getAttribute("message-type") as MermaidSequenceMessageType ?? this.messageType;
109
+ const label = this.getAttribute("label") ?? this.label ?? "";
110
+
111
+ this._svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
112
+ this._path.setAttribute("d", `M 0 ${y} L ${width} ${y}`);
113
+ this._path.setAttribute("marker-end", messageType === MermaidSequenceMessageType.open ? "" : "url(#marker)");
114
+ this._path.style.strokeDasharray = messageType === MermaidSequenceMessageType.dotted ? "4 4" : "";
115
+ this._label.textContent = label;
116
+ this._label.setAttribute("x", (width / 2).toString());
117
+ this._label.setAttribute("y", (y - 8).toString());
118
+ this._label.setAttribute("text-anchor", "middle");
119
+ }
120
+
121
+ public createMermaid() {
122
+ const from = sanitizeId(this.getAttribute("from") ?? this.from);
123
+ const to = sanitizeId(this.getAttribute("to") ?? this.to);
124
+ const label = this.getAttribute("label") ?? this.label ?? "";
125
+ const connector = this.getAttribute("connector") ?? getConnector(this.getAttribute("message-type") as MermaidSequenceMessageType ?? this.messageType);
126
+ return `${from}${connector}${to}: ${escapeLabel(label)}`;
127
+ }
128
+ }
129
+
130
+ function getConnector(messageType: MermaidSequenceMessageType) {
131
+ switch (messageType) {
132
+ case MermaidSequenceMessageType.dotted:
133
+ return "-->>";
134
+ case MermaidSequenceMessageType.open:
135
+ return "->";
136
+ case MermaidSequenceMessageType.cross:
137
+ return "-x";
138
+ case MermaidSequenceMessageType.async:
139
+ return "-)";
140
+ default:
141
+ return "->>";
142
+ }
143
+ }
144
+
145
+ customElements.define(MermaidSequenceMessage.is, MermaidSequenceMessage);
@@ -0,0 +1,80 @@
1
+ import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
2
+ import { escapeLabel, sanitizeId } from "./mermaid-node.js";
3
+
4
+ enum MermaidSequenceParticipantType {
5
+ participant = "participant",
6
+ actor = "actor",
7
+ }
8
+
9
+ export class MermaidSequenceParticipant extends BaseCustomWebComponentConstructorAppend {
10
+ static override readonly style = css`
11
+ * {
12
+ box-sizing: border-box;
13
+ }
14
+
15
+ #participant {
16
+ align-items: center;
17
+ background: white;
18
+ border: 2px solid #333;
19
+ color: #111;
20
+ display: flex;
21
+ font-family: Arial, sans-serif;
22
+ font-size: 14px;
23
+ height: 100%;
24
+ justify-content: center;
25
+ min-height: 42px;
26
+ min-width: 120px;
27
+ overflow: hidden;
28
+ padding: 6px 10px;
29
+ pointer-events: none;
30
+ text-align: center;
31
+ width: 100%;
32
+ }
33
+
34
+ #participant[data-participant-type="actor"] {
35
+ border-radius: 999px;
36
+ }
37
+ `;
38
+
39
+ static override readonly template = html`<div id="participant"></div>`;
40
+
41
+ static readonly is = "mermaid-sequence-participant";
42
+
43
+ public participantId: string = "Alice";
44
+ public label: string = "Alice";
45
+ public participantType: MermaidSequenceParticipantType = MermaidSequenceParticipantType.participant;
46
+
47
+ private _participant: HTMLDivElement;
48
+
49
+ static readonly properties = {
50
+ participantId: String,
51
+ label: String,
52
+ participantType: MermaidSequenceParticipantType,
53
+ }
54
+
55
+ constructor() {
56
+ super();
57
+ this._restoreCachedInititalValues();
58
+ this._participant = this._getDomElement<HTMLDivElement>("participant");
59
+ }
60
+
61
+ async ready() {
62
+ this._parseAttributesToProperties();
63
+ this._render();
64
+ }
65
+
66
+ private _render() {
67
+ this._participant.textContent = this.getAttribute("label") ?? this.label ?? this.getAttribute("participant-id") ?? this.participantId;
68
+ this._participant.dataset.participantType = this.getAttribute("participant-type") ?? this.participantType;
69
+ }
70
+
71
+ public createMermaid() {
72
+ const id = sanitizeId(this.getAttribute("participant-id") ?? this.participantId);
73
+ const label = this.getAttribute("label") ?? this.label ?? id;
74
+ const type = this.getAttribute("participant-type") ?? this.participantType;
75
+ const keyword = type === MermaidSequenceParticipantType.actor ? "actor" : "participant";
76
+ return label && label !== id ? `${keyword} ${id} as ${escapeLabel(label)}` : `${keyword} ${id}`;
77
+ }
78
+ }
79
+
80
+ customElements.define(MermaidSequenceParticipant.is, MermaidSequenceParticipant);
@@ -0,0 +1,80 @@
1
+ import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
2
+ import { escapeLabel, sanitizeId } from "./mermaid-node.js";
3
+
4
+ export class MermaidSubgraph extends BaseCustomWebComponentConstructorAppend {
5
+ static override readonly style = css`
6
+ :host {
7
+ background: rgba(245, 245, 245, 0.55);
8
+ border: 2px dashed #777;
9
+ color: #222;
10
+ display: block;
11
+ font-family: Arial, sans-serif;
12
+ font-size: 13px;
13
+ overflow: visible;
14
+ padding-top: 28px;
15
+ }
16
+
17
+ #title {
18
+ background: rgba(255, 255, 255, 0.85);
19
+ border-bottom: 1px solid #aaa;
20
+ font-weight: 600;
21
+ left: 0;
22
+ overflow: hidden;
23
+ padding: 5px 8px;
24
+ pointer-events: none;
25
+ position: absolute;
26
+ right: 0;
27
+ text-overflow: ellipsis;
28
+ top: 0;
29
+ white-space: nowrap;
30
+ }
31
+ `;
32
+
33
+ static override readonly template = html`<div id="title"></div><slot></slot>`;
34
+
35
+ static readonly is = "mermaid-subgraph";
36
+
37
+ public subgraphId: string = "";
38
+ public override title: string = "Subgraph";
39
+ public direction: string = "";
40
+
41
+ private _titleElement: HTMLDivElement;
42
+
43
+ static readonly properties = {
44
+ subgraphId: String,
45
+ title: String,
46
+ direction: String,
47
+ }
48
+
49
+ constructor() {
50
+ super();
51
+ this._restoreCachedInititalValues();
52
+ this._titleElement = this._getDomElement<HTMLDivElement>("title");
53
+ }
54
+
55
+ async ready() {
56
+ this._parseAttributesToProperties();
57
+ this._render();
58
+ }
59
+
60
+ attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
61
+ if (oldValue !== newValue) {
62
+ this._parseAttributesToProperties();
63
+ this._render();
64
+ }
65
+ }
66
+
67
+ private _render() {
68
+ this._titleElement.textContent = this.getAttribute("title") ?? this.title;
69
+ }
70
+
71
+ public createMermaidStart() {
72
+ const id = this.getAttribute("subgraph-id") ?? this.subgraphId;
73
+ const title = this.getAttribute("title") ?? this.title;
74
+ if (id && title && id !== title)
75
+ return `subgraph ${sanitizeId(id)}[${escapeLabel(title)}]`;
76
+ return `subgraph ${escapeLabel(title || id || "Subgraph")}`;
77
+ }
78
+ }
79
+
80
+ customElements.define(MermaidSubgraph.is, MermaidSubgraph);
@@ -0,0 +1,47 @@
1
+ import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
2
+ import { IUiCommand, InstanceServiceContainer, ServiceContainer } from "@node-projects/web-component-designer";
3
+ import { IDemoView } from "@node-projects/web-component-designer/src/elements/widgets/demoView/IDemoView.js";
4
+
5
+ export class MermaidDemoView extends BaseCustomWebComponentConstructorAppend implements IDemoView {
6
+ static override readonly template = html`<div id="preview"></div>`;
7
+
8
+ static override readonly style = css`
9
+ :host {
10
+ background: white;
11
+ display: block;
12
+ height: 100%;
13
+ overflow: auto;
14
+ position: relative;
15
+ width: 100%;
16
+ }
17
+
18
+ #preview {
19
+ box-sizing: border-box;
20
+ min-height: 100%;
21
+ padding: 24px;
22
+ width: 100%;
23
+ }
24
+ `;
25
+
26
+ executeCommand(command: IUiCommand) {
27
+ }
28
+
29
+ canExecuteCommand(command: IUiCommand) {
30
+ return false;
31
+ }
32
+
33
+ dispose(): void {
34
+ }
35
+
36
+ async display(serviceContainer: ServiceContainer, instanceServiceContainer: InstanceServiceContainer, code: string, style: string) {
37
+ const preview = this._getDomElement<HTMLDivElement>("preview");
38
+ const mermaid = await import("mermaid");
39
+ const id = "mermaid-preview-" + crypto.randomUUID();
40
+
41
+ mermaid.default.initialize({ startOnLoad: false, securityLevel: "strict" });
42
+ const result = await mermaid.default.render(id, code || "flowchart TD\n A[Node]");
43
+ preview.innerHTML = result.svg;
44
+ }
45
+ }
46
+
47
+ customElements.define("node-projects-mermaid-demo-view", MermaidDemoView);
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "composite": true,
5
+ "outDir": "./dist",
6
+ "rootDir": "./src",
7
+ "noEmit": false,
8
+ "resolveJsonModule": true
9
+ },
10
+ "include": [
11
+ "src/**/*.ts",
12
+ "src/**/*.js",
13
+ "src/**/*.json"
14
+ ],
15
+ "references": [
16
+ {
17
+ "path": "../web-component-designer/tsconfig.json"
18
+ }
19
+ ]
20
+ }