mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-06 17:16:43 +02:00
Add support for abstract methods
Added logic to allow rendering of a method name with italics or underline based on modifier at beginning of name to set css style
This commit is contained in:
@@ -169,7 +169,7 @@ describe('Class diagram', () => {
|
|||||||
`
|
`
|
||||||
classDiagram
|
classDiagram
|
||||||
Class01 <|-- AveryLongClass : Cool
|
Class01 <|-- AveryLongClass : Cool
|
||||||
Class01 : |someMethod()
|
Class01 : someMethod()*
|
||||||
`,
|
`,
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
@@ -181,7 +181,7 @@ describe('Class diagram', () => {
|
|||||||
`
|
`
|
||||||
classDiagram
|
classDiagram
|
||||||
Class01 <|-- AveryLongClass : Cool
|
Class01 <|-- AveryLongClass : Cool
|
||||||
Class01 : $someMethod()
|
Class01 : someMethod()$
|
||||||
`,
|
`,
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
|
@@ -155,9 +155,9 @@ To specify the visibility of a class member (i.e. any attribute or method), thes
|
|||||||
- `#` Protected
|
- `#` Protected
|
||||||
- `~` Package
|
- `~` Package
|
||||||
|
|
||||||
>_note_ you can also add additional _classifers_ to a method definition using the following notations (similar to visibliity above):
|
>_note_ you can also include additional _classifers_ to a method definition by adding the following notations to the end of the method, i.e.: after the `()`:
|
||||||
> - `|` Abstract e.g.: `|someAbstractMethod()`
|
> - `*` Abstract e.g.: `someAbstractMethod()*`
|
||||||
> - `$` Static e.g.: `$someStaticMethod()`
|
> - `$` Static e.g.: `someStaticMethod()$`
|
||||||
|
|
||||||
|
|
||||||
## Defining Relationship
|
## Defining Relationship
|
||||||
|
@@ -72,7 +72,7 @@ export const addMember = function(className, member) {
|
|||||||
if (memberString.startsWith('<<') && memberString.endsWith('>>')) {
|
if (memberString.startsWith('<<') && memberString.endsWith('>>')) {
|
||||||
// Remove leading and trailing brackets
|
// Remove leading and trailing brackets
|
||||||
theClass.annotations.push(memberString.substring(2, memberString.length - 2));
|
theClass.annotations.push(memberString.substring(2, memberString.length - 2));
|
||||||
} else if (memberString.endsWith(')')) {
|
} else if (memberString.indexOf(')') > 0) {
|
||||||
theClass.methods.push(memberString);
|
theClass.methods.push(memberString);
|
||||||
} else if (memberString) {
|
} else if (memberString) {
|
||||||
theClass.members.push(memberString);
|
theClass.members.push(memberString);
|
||||||
|
@@ -402,25 +402,25 @@ describe('class diagram, ', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should handle abstract methods', function () {
|
it('should handle abstract methods', function () {
|
||||||
const str = 'classDiagram\n' + 'class Class1\n' + 'Class1 : |someMethod()';
|
const str = 'classDiagram\n' + 'class Class1\n' + 'Class1 : someMethod()*';
|
||||||
parser.parse(str);
|
parser.parse(str);
|
||||||
|
|
||||||
const testClass = parser.yy.getClass('Class1');
|
const testClass = parser.yy.getClass('Class1');
|
||||||
expect(testClass.annotations.length).toBe(0);
|
expect(testClass.annotations.length).toBe(0);
|
||||||
expect(testClass.members.length).toBe(0);
|
expect(testClass.members.length).toBe(0);
|
||||||
expect(testClass.methods.length).toBe(1);
|
expect(testClass.methods.length).toBe(1);
|
||||||
expect(testClass.methods[0]).toBe('|someMethod()');
|
expect(testClass.methods[0]).toBe('someMethod()*');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle static methods', function () {
|
it('should handle static methods', function () {
|
||||||
const str = 'classDiagram\n' + 'class Class1\n' + 'Class1 : $someMethod()';
|
const str = 'classDiagram\n' + 'class Class1\n' + 'Class1 : someMethod()$';
|
||||||
parser.parse(str);
|
parser.parse(str);
|
||||||
|
|
||||||
const testClass = parser.yy.getClass('Class1');
|
const testClass = parser.yy.getClass('Class1');
|
||||||
expect(testClass.annotations.length).toBe(0);
|
expect(testClass.annotations.length).toBe(0);
|
||||||
expect(testClass.members.length).toBe(0);
|
expect(testClass.members.length).toBe(0);
|
||||||
expect(testClass.methods.length).toBe(1);
|
expect(testClass.methods.length).toBe(1);
|
||||||
expect(testClass.methods[0]).toBe('$someMethod()');
|
expect(testClass.methods[0]).toBe('someMethod()$');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -283,24 +283,28 @@ const drawClass = function(elem, classDef) {
|
|||||||
const addTspan = function(textEl, txt, isFirst) {
|
const addTspan = function(textEl, txt, isFirst) {
|
||||||
let displayText = txt;
|
let displayText = txt;
|
||||||
let cssStyle = '';
|
let cssStyle = '';
|
||||||
let classifier = txt.substring(0 , 1);
|
let methodEnd = txt.indexOf(')') + 1;
|
||||||
|
|
||||||
switch (classifier) {
|
if (methodEnd > 1 && methodEnd <= txt.length) {
|
||||||
case '|':
|
let classifier = txt.substring(methodEnd);
|
||||||
cssStyle = 'font-style:italic;';
|
|
||||||
displayText = txt.substring(1);
|
switch (classifier) {
|
||||||
break;
|
case '*':
|
||||||
case '$':
|
cssStyle = 'font-style:italic;';
|
||||||
cssStyle = 'text-decoration:underline;';
|
break;
|
||||||
displayText = txt.substring(1);
|
case '$':
|
||||||
break;
|
cssStyle = 'text-decoration:underline;';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
displayText = txt.substring(0, methodEnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tSpan = textEl
|
const tSpan = textEl
|
||||||
.append('tspan')
|
.append('tspan')
|
||||||
.attr('x', conf.padding)
|
.attr('x', conf.padding)
|
||||||
.text(displayText);
|
.text(displayText);
|
||||||
|
|
||||||
if (cssStyle !== '') {
|
if (cssStyle !== '') {
|
||||||
tSpan.attr('style', cssStyle);
|
tSpan.attr('style', cssStyle);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user