Skip to content

Commit 2a20a2e

Browse files
authored
feat: support classDef for styling nodes in flowchart (#71)
* feat: ssupport classDef for styling nodes in flowchart * update changelog
1 parent 73ab442 commit 2a20a2e

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
### Features
4+
5+
- Support ClassDef for styling the nodes by [@ad1992](https://github.com/ad1992) in https://github.com/excalidraw/mermaid-to-excalidraw/pull/71
6+
17
## v1.1.0 (2024-07-10)
28

39
## Library

playground/testcases/flowchart.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,15 @@ style id2 fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5
360360
`,
361361
type: "flowchart",
362362
},
363+
{
364+
name: "Styling a node using class",
365+
definition: `flowchart LR
366+
A:::foo & B:::bar --> C:::foobar
367+
classDef foo stroke:#1971c2, fill:#4dabf7
368+
classDef bar stroke:#d6336c, fill:#f783ac
369+
classDef foobar stroke:#00f stroke-width:2px`,
370+
type: "flowchart",
371+
},
363372
{
364373
name: "Classes",
365374
definition: `flowchart LR

src/parser/flowchart.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "../interfaces.js";
1313

1414
import type { Diagram } from "mermaid/dist/Diagram.js";
15+
import { DiagramStyleClassDef } from "mermaid/dist/diagram-api/types.js";
1516

1617
export interface Flowchart {
1718
type: "flowchart";
@@ -74,7 +75,11 @@ const parseSubGraph = (data: any, containerEl: Element): SubGraph => {
7475
};
7576
};
7677

77-
const parseVertex = (data: any, containerEl: Element): Vertex | undefined => {
78+
const parseVertex = (
79+
data: any,
80+
containerEl: Element,
81+
classes: { [key: string]: DiagramStyleClassDef }
82+
): Vertex | undefined => {
7883
// Find Vertex element
7984
const el: SVGSVGElement | null = containerEl.querySelector(
8085
`[id*="flowchart-${data.id}-"]`
@@ -117,6 +122,7 @@ const parseVertex = (data: any, containerEl: Element): Vertex | undefined => {
117122
const value = property.split(":")[1].trim();
118123
containerStyle[key] = value;
119124
});
125+
120126
const labelStyle: Vertex["labelStyle"] = {};
121127
labelStyleText?.split(";").forEach((property) => {
122128
if (!property) {
@@ -128,6 +134,19 @@ const parseVertex = (data: any, containerEl: Element): Vertex | undefined => {
128134
labelStyle[key] = value;
129135
});
130136

137+
if (data.classes) {
138+
const classDef = classes[data.classes];
139+
if (classDef) {
140+
classDef.styles?.forEach((style) => {
141+
const [key, value] = style.split(":");
142+
containerStyle[key.trim() as CONTAINER_STYLE_PROPERTY] = value.trim();
143+
});
144+
classDef.textStyles?.forEach((style) => {
145+
const [key, value] = style.split(":");
146+
labelStyle[key.trim() as LABEL_STYLE_PROPERTY] = value.trim();
147+
});
148+
}
149+
}
131150
return {
132151
id: data.id,
133152
labelType: data.labelType,
@@ -230,8 +249,9 @@ export const parseMermaidFlowChartDiagram = (
230249
//@ts-ignore
231250
const mermaidParser = diagram.parser.yy;
232251
const vertices = mermaidParser.getVertices();
252+
const classes = mermaidParser.getClasses();
233253
Object.keys(vertices).forEach((id) => {
234-
vertices[id] = parseVertex(vertices[id], containerEl);
254+
vertices[id] = parseVertex(vertices[id], containerEl, classes);
235255
});
236256

237257
// Track the count of edges based on the edge id

0 commit comments

Comments
 (0)