mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-16 13:59:54 +02:00
1119 Add ability to define return type for methods
Updated ClassRenderer to check for `[]` to indicate return type for method. Small refactor to split out logic for determining method display text and style. Updated documentation
This commit is contained in:
@@ -105,7 +105,7 @@ Naming convention: a class name should be composed of alphanumeric (unicode allo
|
|||||||
|
|
||||||
UML provides mechanisms to represent class members, such as attributes and methods, and additional information about them.
|
UML provides mechanisms to represent class members, such as attributes and methods, and additional information about them.
|
||||||
|
|
||||||
Mermaid distinguishes between attributes and functions/methods based on if the **parenthesis** `()` are present or not. The ones with `()` are treated as functions/methods, and others as attributes.
|
Mermaid distinguishes between attributes and functions/methods based on if the **parenthesis** `()` are present or not. The ones with `()` are treated as functions/methods, and others as attributes. To indicate a return type for a method, enclose the type within **square brackets** `[]`
|
||||||
|
|
||||||
|
|
||||||
There are two ways to define the members of a class, and regardless of whichever syntax is used to define the members, the output will still be same. The two different ways are :
|
There are two ways to define the members of a class, and regardless of whichever syntax is used to define the members, the output will still be same. The two different ways are :
|
||||||
@@ -115,15 +115,15 @@ There are two ways to define the members of a class, and regardless of whichever
|
|||||||
class BankAccount
|
class BankAccount
|
||||||
BankAccount : +String owner
|
BankAccount : +String owner
|
||||||
BankAccount : +BigDecimal balance
|
BankAccount : +BigDecimal balance
|
||||||
BankAccount : +deposit(amount)
|
BankAccount : +deposit(amount) [bool]
|
||||||
BankAccount : +withdrawl(amount)
|
BankAccount : +withdrawal(amount)
|
||||||
```
|
```
|
||||||
``` mermaid
|
``` mermaid
|
||||||
classDiagram
|
classDiagram
|
||||||
class BankAccount
|
class BankAccount
|
||||||
BankAccount : +String owner
|
BankAccount : +String owner
|
||||||
BankAccount : +BigDecimal balance
|
BankAccount : +BigDecimal balance
|
||||||
BankAccount : +deposit(amount)
|
BankAccount : +deposit(amount) : bool
|
||||||
BankAccount : +withdrawl(amount)
|
BankAccount : +withdrawl(amount)
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -132,7 +132,7 @@ There are two ways to define the members of a class, and regardless of whichever
|
|||||||
class BankAccount{
|
class BankAccount{
|
||||||
+String owner
|
+String owner
|
||||||
+BigDecimal balance
|
+BigDecimal balance
|
||||||
+deposit(amount)
|
+deposit(amount) [bool]
|
||||||
+withdrawl(amount)
|
+withdrawl(amount)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -141,7 +141,7 @@ class BankAccount{
|
|||||||
class BankAccount{
|
class BankAccount{
|
||||||
+String owner
|
+String owner
|
||||||
+BigDecimal balance
|
+BigDecimal balance
|
||||||
+deposit(amount)
|
+deposit(amount) : bool
|
||||||
+withdrawl(amount)
|
+withdrawl(amount)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -288,23 +288,14 @@ const drawClass = function(elem, classDef) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const addTspan = function(textEl, txt, isFirst) {
|
const addTspan = function(textEl, txt, isFirst) {
|
||||||
|
let isMethod = txt.indexOf(')') > 1;
|
||||||
let displayText = txt;
|
let displayText = txt;
|
||||||
let cssStyle = '';
|
let cssStyle = '';
|
||||||
let methodEnd = txt.indexOf(')') + 1;
|
|
||||||
|
|
||||||
if (methodEnd > 1 && methodEnd <= txt.length) {
|
if (isMethod) {
|
||||||
let classifier = txt.substring(methodEnd);
|
let method = buildDisplayTextForMethod(txt);
|
||||||
|
displayText = method.displayText;
|
||||||
switch (classifier) {
|
cssStyle = method.cssStyle;
|
||||||
case '*':
|
|
||||||
cssStyle = 'font-style:italic;';
|
|
||||||
break;
|
|
||||||
case '$':
|
|
||||||
cssStyle = 'text-decoration:underline;';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
displayText = txt.substring(0, methodEnd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const tSpan = textEl
|
const tSpan = textEl
|
||||||
@@ -322,6 +313,40 @@ const drawClass = function(elem, classDef) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const id = classDef.id;
|
const id = classDef.id;
|
||||||
|
const buildDisplayTextForMethod = function(txt) {
|
||||||
|
let cssStyle = '';
|
||||||
|
let methodEnd = txt.indexOf(')') + 1;
|
||||||
|
let methodName = txt.substring(0, methodEnd);
|
||||||
|
|
||||||
|
let classifier = txt.substring(methodEnd, methodEnd + 1);
|
||||||
|
|
||||||
|
switch (classifier) {
|
||||||
|
case '*':
|
||||||
|
cssStyle = 'font-style:italic;';
|
||||||
|
break;
|
||||||
|
case '$':
|
||||||
|
cssStyle = 'text-decoration:underline;';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let method = {
|
||||||
|
methodname: methodName,
|
||||||
|
displayText: methodName,
|
||||||
|
cssStyle: cssStyle
|
||||||
|
};
|
||||||
|
|
||||||
|
let returnTypeStart = txt.indexOf('[') + 1;
|
||||||
|
let returnTypeEnd = txt.indexOf(']');
|
||||||
|
|
||||||
|
if (returnTypeStart > 1 && returnTypeEnd > returnTypeStart) {
|
||||||
|
let returnType = txt.substring(returnTypeStart, returnTypeEnd);
|
||||||
|
|
||||||
|
method.displayText = methodName + ' : ' + returnType;
|
||||||
|
}
|
||||||
|
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
const classInfo = {
|
const classInfo = {
|
||||||
id: id,
|
id: id,
|
||||||
label: classDef.id,
|
label: classDef.id,
|
||||||
|
Reference in New Issue
Block a user