fix: Class label not visible if class is already defined

This commit is contained in:
Sidharth Vinod
2023-02-24 13:46:56 +05:30
parent 716a4d2cbc
commit b13707fa7b
4 changed files with 26 additions and 9 deletions

View File

@@ -539,4 +539,13 @@ class C13["With Città foreign language"]
`
);
});
it('should render classLabel if class has already been defined earlier', () => {
imgSnapshotTest(
`classDiagram
Animal <|-- Duck
class Duck["Duck with text label"]
`
);
});
});

View File

@@ -37,7 +37,7 @@
</pre>
<script type="module">
import mermaid from '../packages/mermaid';
import mermaid from '../packages/mermaid/src/mermaid';
mermaid.initialize({
theme: 'forest',
// themeCSS: '.node rect { fill: red; }',

View File

@@ -1,3 +1,4 @@
// @ts-expect-error - d3 types issue
import { select, Selection } from 'd3';
import { log } from '../../logger';
import * as configApi from '../../config';
@@ -44,28 +45,32 @@ const splitClassNameAndType = function (id: string) {
return { className: className, type: genericType };
};
export const setClassLabel = function (id: string, label: string) {
if (label) {
label = sanitizeText(label);
}
const { className } = splitClassNameAndType(id);
classes[className].label = label;
};
/**
* Function called by parser when a node definition has been found.
*
* @param id - Id of the class to add
* @param label - Optional label of the class
* @public
*/
export const addClass = function (id: string, label?: string) {
export const addClass = function (id: string) {
const classId = splitClassNameAndType(id);
// Only add class if not exists
if (classes[classId.className] !== undefined) {
return;
}
if (label) {
label = sanitizeText(label);
}
classes[classId.className] = {
id: classId.className,
type: classId.type,
label: label ?? classId.className,
label: classId.className,
cssClasses: [],
methods: [],
members: [],
@@ -354,6 +359,7 @@ const setupToolTips = function (element: Element) {
const nodes = svg.selectAll('g.node');
nodes
.on('mouseover', function () {
// @ts-expect-error - select is not part of the d3 type definition
const el = select(this);
const title = el.attr('title');
// Don't try to draw a tooltip if no data is provided
@@ -373,6 +379,7 @@ const setupToolTips = function (element: Element) {
})
.on('mouseout', function () {
tooltipElem.transition().duration(500).style('opacity', 0);
// @ts-expect-error - select is not part of the d3 type definition
const el = select(this);
el.classed('hover', false);
});
@@ -417,4 +424,5 @@ export default {
lookUpDomId,
setDiagramTitle,
getDiagramTitle,
setClassLabel,
};

View File

@@ -288,7 +288,7 @@ classStatement
classIdentifier
: CLASS className {$$=$2; yy.addClass($2);}
| CLASS className classLabel {$$=$2; yy.addClass($2, $3);}
| CLASS className classLabel {$$=$2; yy.addClass($2);yy.setClassLabel($2, $3);}
;
annotationStatement