Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update class diagram to v3 using new renderer #5880

Merged
merged 54 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
e3c426d
Initial work on updating to new renderer
yari-dewalt Aug 27, 2024
27f7b61
Add more tests / samples
yari-dewalt Aug 28, 2024
8745daa
Add handling for escape type
yari-dewalt Aug 28, 2024
191e421
Adjust classNode text when label is set
yari-dewalt Aug 28, 2024
20a58d6
Add special handling to avoid markdown to HTML issues
yari-dewalt Aug 28, 2024
9ac20a5
Add styling compatibility to shape
yari-dewalt Aug 28, 2024
bd4ca9a
Add support for classifiers in text attribute
yari-dewalt Aug 28, 2024
9718650
Add classifier test diagram
yari-dewalt Aug 28, 2024
b67c931
Update snapshot to include new data
yari-dewalt Aug 28, 2024
b7790ba
Add diagrams for testing
yari-dewalt Sep 13, 2024
3eb338d
Add default option for hideEmptyBox in config
yari-dewalt Sep 13, 2024
2ec9f0a
Add support for new renderer
yari-dewalt Sep 13, 2024
ca93280
Change types
yari-dewalt Sep 13, 2024
3782b69
Add support for classDef statement
yari-dewalt Sep 13, 2024
5f11cd4
Add classBox shape
yari-dewalt Sep 13, 2024
a43ee60
Add fixes to utils
yari-dewalt Sep 13, 2024
149f991
Fix typo
yari-dewalt Sep 13, 2024
b105c7b
Add node look and fix text attribute on class
yari-dewalt Sep 18, 2024
94d2a0e
Cleanup
yari-dewalt Sep 18, 2024
745495c
Refactor and cleanup logic for classBox shape
yari-dewalt Sep 18, 2024
25337dc
Update text logic and change workaround for markdown
yari-dewalt Sep 18, 2024
31546b0
Fix note text
yari-dewalt Sep 18, 2024
bed301a
Update tests
yari-dewalt Sep 18, 2024
1fcebab
Change cssDiagram parameter to 'classDiagram'
yari-dewalt Sep 18, 2024
6a99a1a
Update HTML file and add rendering tests
yari-dewalt Sep 19, 2024
41f2286
Add adjustments for text and useHtmlLabels
yari-dewalt Sep 19, 2024
3b8bd6b
Include classRenderer.js to fix lint error
yari-dewalt Sep 20, 2024
bd0a00d
Update config yaml so ClassDiagramConfig can be properly updated
yari-dewalt Sep 20, 2024
48e5dee
Fix edge markers being flipped
yari-dewalt Sep 23, 2024
dd62322
Add default class support
yari-dewalt Sep 23, 2024
f031abc
Support lollipop interface feature
yari-dewalt Sep 23, 2024
3e697e2
Update and fix logic for lollipop interfaces
yari-dewalt Sep 24, 2024
1067929
Fix conditional logic
yari-dewalt Sep 24, 2024
fa46267
Update documentation
yari-dewalt Sep 24, 2024
91e3da1
Update tests to take into account 'default' class
yari-dewalt Sep 24, 2024
d424a44
Merge branch 'develop' into update-class-diagram
yari-dewalt Oct 8, 2024
11fb806
[autofix.ci] apply automated fixes
autofix-ci[bot] Oct 8, 2024
86c7532
Merge branch 'develop' into update-class-diagram
yari-dewalt Oct 16, 2024
bfea287
Register and update classBox shape
yari-dewalt Oct 17, 2024
b7ef8b2
Refactor to make code more clear
yari-dewalt Oct 17, 2024
6041729
Add suport for start and end labels (cardinality) in elk renderer
yari-dewalt Oct 17, 2024
cf2c7f7
Update tests
yari-dewalt Oct 17, 2024
6f506c3
[autofix.ci] apply automated fixes
autofix-ci[bot] Oct 17, 2024
74695d2
Fix undesired wrapping on notes
yari-dewalt Oct 18, 2024
b7d66bb
Add elk and handDrawn specific test files
yari-dewalt Oct 18, 2024
10f11e0
Merge branch 'update-class-diagram' of github.com:yari-dewalt/mermaid…
yari-dewalt Oct 18, 2024
0e819ae
Change cssClasses to use concatenated string instead of array to pick…
yari-dewalt Oct 18, 2024
bfdd0f2
Fix tests to work with new cssClasses attribute
yari-dewalt Oct 18, 2024
75e796f
Merge branch 'develop' into update-class-diagram
yari-dewalt Oct 21, 2024
64bc7f4
Add fix for flipped edge markers
yari-dewalt Oct 21, 2024
be3da0b
Update testing file
yari-dewalt Oct 21, 2024
fd6b875
Fix subgraph / cluster node width based off of label
yari-dewalt Oct 21, 2024
b315d70
Merge branch 'develop' into update-class-diagram and fix conflicts
yari-dewalt Oct 28, 2024
75ca802
Merge branch 'develop' into update-class-diagram
knsv Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,037 changes: 1,037 additions & 0 deletions cypress/integration/rendering/classDiagram-elk-v3.spec.js

Large diffs are not rendered by default.

1,041 changes: 1,041 additions & 0 deletions cypress/integration/rendering/classDiagram-handDrawn-v3.spec.js

Large diffs are not rendered by default.

1,031 changes: 1,031 additions & 0 deletions cypress/integration/rendering/classDiagram-v3.spec.js

Large diffs are not rendered by default.

663 changes: 663 additions & 0 deletions cypress/platform/yari.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/config/setup/interfaces/mermaid.LayoutData.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#### Defined in

[packages/mermaid/src/rendering-util/types.ts:125](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L125)
[packages/mermaid/src/rendering-util/types.ts:128](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L128)

---

Expand All @@ -30,7 +30,7 @@

#### Defined in

[packages/mermaid/src/rendering-util/types.ts:124](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L124)
[packages/mermaid/src/rendering-util/types.ts:127](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L127)

---

Expand All @@ -40,4 +40,4 @@

#### Defined in

[packages/mermaid/src/rendering-util/types.ts:123](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L123)
[packages/mermaid/src/rendering-util/types.ts:126](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L126)
2 changes: 1 addition & 1 deletion docs/config/setup/modules/defaultConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#### Defined in

[packages/mermaid/src/defaultConfig.ts:267](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L267)
[packages/mermaid/src/defaultConfig.ts:270](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L270)

---

Expand Down
244 changes: 133 additions & 111 deletions docs/syntax/classDiagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,51 @@ And `Link` can be one of:
| -- | Solid |
| .. | Dashed |

### Lollipop Interfaces

Classes can also be given a special relation type that defines a lollipop interface on the class. A lollipop interface is defined using the following syntax:

- `bar ()-- foo`
- `foo --() bar`

The interface (bar) with the lollipop connects to the class (foo).

Note: Each interface that is defined is unique and is meant to not be shared between classes / have multiple edges connecting to it.

```mermaid-example
classDiagram
bar ()-- foo
```

```mermaid
classDiagram
bar ()-- foo
```

```mermaid-example
classDiagram
class Class01 {
int amount
draw()
}
Class01 --() bar
Class02 --() bar

foo ()-- Class01
```

```mermaid
classDiagram
class Class01 {
int amount
draw()
}
Class01 --() bar
Class02 --() bar

foo ()-- Class01
```

## Define Namespace

A namespace groups classes.
Expand Down Expand Up @@ -776,10 +821,12 @@ Beginner's tip—a full example using interactive links in an HTML page:

## Styling

### Styling a node (v10.7.0+)
### Styling a node

It is possible to apply specific styles such as a thicker border or a different background color to an individual node using the `style` keyword.

Note that notes and namespaces cannot be styled individually but do support themes.

```mermaid-example
classDiagram
class Animal
Expand All @@ -799,172 +846,147 @@ classDiagram
#### Classes

More convenient than defining the style every time is to define a class of styles and attach this class to the nodes that
should have a different look. This is done by predefining classes in css styles that can be applied from the graph definition using the `cssClass` statement or the `:::` short hand.
should have a different look.

```html
<style>
.styleClass > rect {
fill: #ff0000;
stroke: #ffff00;
stroke-width: 4px;
}
</style>
A class definition looks like the example below:

```
classDef className fill:#f9f,stroke:#333,stroke-width:4px;
```

Also, it is possible to define style to multiple classes in one statement:

```
classDef firstClassName,secondClassName font-size:12pt;
```

Then attaching that class to a specific node:
Attachment of a class to a node is done as per below:

```
cssClass "nodeId1" styleClass;
cssClass "nodeId1" className;
```

It is also possible to attach a class to a list of nodes in one statement:

```
cssClass "nodeId1,nodeId2" styleClass;
cssClass "nodeId1,nodeId2" className;
```

A shorter form of adding a class is to attach the classname to the node using the `:::` operator:

```mermaid-example
classDiagram
class Animal:::styleClass
class Animal:::someclass
classDef someclass fill:#f96
```

```mermaid
classDiagram
class Animal:::styleClass
class Animal:::someclass
classDef someclass fill:#f96
```

Or:

```mermaid-example
classDiagram
class Animal:::styleClass {
class Animal:::someclass {
-int sizeInFeet
-canEat()
}
classDef someclass fill:#f96
```

```mermaid
classDiagram
class Animal:::styleClass {
class Animal:::someclass {
-int sizeInFeet
-canEat()
}
classDef someclass fill:#f96
```

?> cssClasses cannot be added using this shorthand method at the same time as a relation statement.

?> Due to limitations with existing markup for class diagrams, it is not currently possible to define css classes within the diagram itself. **_Coming soon!_**

### Default Styles

The main styling of the class diagram is done with a preset number of css classes. During rendering these classes are extracted from the file located at src/themes/class.scss. The classes used here are described below:
### Default class

| Class | Description |
| ------------------ | ----------------------------------------------------------------- |
| g.classGroup text | Styles for general class text |
| classGroup .title | Styles for general class title |
| g.classGroup rect | Styles for class diagram rectangle |
| g.classGroup line | Styles for class diagram line |
| .classLabel .box | Styles for class label box |
| .classLabel .label | Styles for class label text |
| composition | Styles for composition arrow head and arrow line |
| aggregation | Styles for aggregation arrow head and arrow line(dashed or solid) |
| dependency | Styles for dependency arrow head and arrow line |
If a class is named default it will be applied to all nodes. Specific styles and classes should be defined afterwards to override the applied default styling.

#### Sample stylesheet

```scss
body {
background: white;
}
```
classDef default fill:#f9f,stroke:#333,stroke-width:4px;
```

g.classGroup text {
fill: $nodeBorder;
stroke: none;
font-family: 'trebuchet ms', verdana, arial;
font-family: var(--mermaid-font-family);
font-size: 10px;
```mermaid-example
classDiagram
class Animal:::pink
class Mineral

.title {
font-weight: bolder;
}
}
classDef default fill:#f96,color:red
classDef pink color:#f9f
```

g.classGroup rect {
fill: $nodeBkg;
stroke: $nodeBorder;
}
```mermaid
classDiagram
class Animal:::pink
class Mineral

g.classGroup line {
stroke: $nodeBorder;
stroke-width: 1;
}
classDef default fill:#f96,color:red
classDef pink color:#f9f
```

.classLabel .box {
stroke: none;
stroke-width: 0;
fill: $nodeBkg;
opacity: 0.5;
}
### CSS Classes

.classLabel .label {
fill: $nodeBorder;
font-size: 10px;
}
It is also possible to predefine classes in CSS styles that can be applied from the graph definition as in the example
below:

.relation {
stroke: $nodeBorder;
stroke-width: 1;
fill: none;
}
**Example style**

@mixin composition {
fill: $nodeBorder;
stroke: $nodeBorder;
stroke-width: 1;
}
```html
<style>
.styleClass > * > g {
fill: #ff0000;
stroke: #ffff00;
stroke-width: 4px;
}
</style>
```

#compositionStart {
@include composition;
}
**Example definition**

#compositionEnd {
@include composition;
}
```mermaid-example
classDiagram
class Animal:::styleClass
```

@mixin aggregation {
fill: $nodeBkg;
stroke: $nodeBorder;
stroke-width: 1;
}
```mermaid
classDiagram
class Animal:::styleClass
```

#aggregationStart {
@include aggregation;
}
> cssClasses cannot be added using this shorthand method at the same time as a relation statement.

#aggregationEnd {
@include aggregation;
}
## Configuration

#dependencyStart {
@include composition;
}
### Members Box

#dependencyEnd {
@include composition;
}
It is possible to hide the empty members box of a class node.

#extensionStart {
@include composition;
}
This is done by changing the **hideEmptyMembersBox** value of the class diagram configuration. For more information on how to edit the Mermaid configuration see the [configuration page.](https://mermaid.js.org/config/configuration.html)

#extensionEnd {
@include composition;
}
```mermaid-example
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
class Duck
```

## Configuration

`Coming soon!`
```mermaid
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
class Duck
```
1 change: 1 addition & 0 deletions docs/syntax/flowchart.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ Below is a comprehensive list of the newly introduced shapes and their correspon
| **Semantic Name** | **Shape Name** | **Short Name** | **Description** | **Alias Supported** |
| --------------------------------- | ---------------------- | -------------- | ------------------------------ | ---------------------------------------------------------------- |
| Card | Notched Rectangle | `notch-rect` | Represents a card | `card`, `notched-rectangle` |
| Class Box | Class Box | `classBox` | Class Box | `class-box` |
| Collate | Hourglass | `hourglass` | Represents a collate operation | `collate`, `hourglass` |
| Com Link | Lightning Bolt | `bolt` | Communication link | `com-link`, `lightning-bolt` |
| Comment | Curly Brace | `brace` | Adds a comment | `brace-l`, `comment` |
Expand Down
6 changes: 6 additions & 0 deletions packages/mermaid-layout-elk/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
}: InternalHelpers,
{ algorithm }: RenderOptions
) => {
const nodeDb: Record<string, any> = {};

Check warning on line 39 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type
const clusterDb: Record<string, any> = {};

Check warning on line 40 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type

const addVertex = async (
nodeEl: SVGGroup,
Expand Down Expand Up @@ -112,21 +112,21 @@
const drawNodes = async (
relX: number,
relY: number,
nodeArray: any[],

Check warning on line 115 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type
svg: any,

Check warning on line 116 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type
subgraphsEl: SVGGroup,
depth: number
) => {
await Promise.all(
nodeArray.map(async function (node: {
id: string | number;
x: any;

Check warning on line 123 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type
y: any;

Check warning on line 124 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type
width: number;
labels: { width: any }[];

Check warning on line 126 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type
height: number;
isGroup: any;

Check warning on line 128 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type
labelData: any;

Check warning on line 129 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type
offset: { posX: number; posY: number };
shape: any;
domId: { node: () => any; attr: (arg0: string, arg1: string) => void };
Expand All @@ -149,6 +149,7 @@
const clusterNode = JSON.parse(JSON.stringify(node));
clusterNode.x = node.offset.posX + node.width / 2;
clusterNode.y = node.offset.posY + node.height / 2;
clusterNode.width = Math.max(clusterNode.width, node.labelData.width);
await insertCluster(subgraphEl, clusterNode);

log.debug('Id (UIO)= ', node.id, node.width, node.shape, node.labels);
Expand Down Expand Up @@ -275,6 +276,8 @@
interpolate: undefined;
style: undefined;
labelType: any;
startLabelRight?: string;
endLabelLeft?: string;
}) {
// Identify Link
const linkIdBase = edge.id; // 'L-' + edge.start + '-' + edge.end;
Expand Down Expand Up @@ -328,6 +331,9 @@
let style = '';
let labelStyle = '';

edgeData.startLabelRight = edge.startLabelRight;
edgeData.endLabelLeft = edge.endLabelLeft;

switch (edge.stroke) {
case 'normal':
style = 'fill:none;';
Expand Down
1 change: 1 addition & 0 deletions packages/mermaid/scripts/docs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ This Markdown should be kept.
"| **Semantic Name** | **Shape Name** | **Short Name** | **Description** | **Alias Supported** |
| --------------------------------- | ---------------------- | -------------- | ------------------------------ | ---------------------------------------------------------------- |
| Card | Notched Rectangle | \`notch-rect\` | Represents a card | \`card\`, \`notched-rectangle\` |
| Class Box | Class Box | \`classBox\` | Class Box | \`class-box\` |
| Collate | Hourglass | \`hourglass\` | Represents a collate operation | \`collate\`, \`hourglass\` |
| Com Link | Lightning Bolt | \`bolt\` | Communication link | \`com-link\`, \`lightning-bolt\` |
| Comment | Curly Brace | \`brace\` | Adds a comment | \`brace-l\`, \`comment\` |
Expand Down
Loading
Loading