From 239fad94ee142a114f8c3c710c1072c0821edbe4 Mon Sep 17 00:00:00 2001 From: Justin Greywolf Date: Mon, 4 Dec 2023 13:28:31 -0800 Subject: [PATCH 01/13] Grammar/rendering for setting style on class node --- .../rendering/classDiagram-v2.spec.js | 10 ++++++++ docs/syntax/classDiagram.md | 23 ++++++++++++++++- packages/mermaid/src/dagre-wrapper/nodes.js | 1 + .../mermaid/src/diagrams/class/classDb.ts | 20 +++++++++++++++ .../class/classDiagram-styles.spec.js | 13 ++++++++++ .../src/diagrams/class/classRenderer-v2.ts | 2 +- .../mermaid/src/diagrams/class/classTypes.ts | 1 + .../diagrams/class/parser/classDiagram.jison | 25 ++++++++++++++++++- 8 files changed, 92 insertions(+), 3 deletions(-) diff --git a/cypress/integration/rendering/classDiagram-v2.spec.js b/cypress/integration/rendering/classDiagram-v2.spec.js index 37e9cada02..20a1aea0ab 100644 --- a/cypress/integration/rendering/classDiagram-v2.spec.js +++ b/cypress/integration/rendering/classDiagram-v2.spec.js @@ -571,4 +571,14 @@ class C13["With Città foreign language"] { logLevel: 1, flowchart: { htmlLabels: false } } ); }); + it('should render a simple class diagram with style definition', () => { + imgSnapshotTest( + ` + classDiagram-v2 + class Class10 + style Class10 fill:#f9f,stroke:#333,stroke-width:4px + `, + { logLevel: 1, flowchart: { htmlLabels: false } } + ); + }); }); diff --git a/docs/syntax/classDiagram.md b/docs/syntax/classDiagram.md index 2f2c3da888..b2d5a8c897 100644 --- a/docs/syntax/classDiagram.md +++ b/docs/syntax/classDiagram.md @@ -768,7 +768,28 @@ Beginner's tip—a full example using interactive links in an HTML page: ### Styling a node -It is possible to apply specific styles such as a thicker border or a different background color to individual nodes. 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. +It is possible to apply specific styles such as a thicker border or a different background color to a node. + +```mermaid-example +classDiagram + class Animal + class Mineral + style Animal fill:#f9f,stroke:#333,stroke-width:4px + style Mineral fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5 +``` + +```mermaid +classDiagram + class Animal + class Mineral + style Animal fill:#f9f,stroke:#333,stroke-width:4px + style Mineral fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5 +``` + +#### 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. ```html