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

Cleanup prior to making logic changes #6041

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/lucky-points-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'mermaid': minor
---

Rename internal fields for class diagrams to match documentation and usage
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
4 changes: 2 additions & 2 deletions cypress/integration/rendering/classDiagram-elk-v3.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,12 @@ class Class10
{ logLevel: 1, htmlLabels: true, layout: 'elk' }
);
});
it('ELK: should render a class with a text label, members and annotation', () => {
it('ELK: should render a class with a text label, attribute and annotation', () => {
imgSnapshotTest(
`classDiagram
class C1["Class 1 with text label"] {
<<interface>>
+member1
+attribute1
}
C1 --> C2`,
{ logLevel: 1, htmlLabels: true, layout: 'elk' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,12 @@ class Class10
{ logLevel: 1, htmlLabels: true, look: 'handDrawn' }
);
});
it('HD: should render a class with a text label, members and annotation', () => {
it('HD: should render a class with a text label, attribute and annotation', () => {
imgSnapshotTest(
`classDiagram
class C1["Class 1 with text label"] {
<<interface>>
+member1
+attribute1
}
C1 --> C2`,
{ logLevel: 1, htmlLabels: true, look: 'handDrawn' }
Expand Down
4 changes: 2 additions & 2 deletions cypress/integration/rendering/classDiagram-v2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,12 @@ class Class10
C1 --> C2`
);
});
it('should render a class with a text label, members and annotation', () => {
it('should render a class with a text label, attribute and annotation', () => {
imgSnapshotTest(
`classDiagram
class C1["Class 1 with text label"] {
<<interface>>
+member1
+attribute1
}
C1 --> C2`
);
Expand Down
4 changes: 2 additions & 2 deletions cypress/integration/rendering/classDiagram-v3.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,12 @@ class Class10
C1 --> C2`
);
});
it('should render a class with a text label, members and annotation', () => {
it('should render a class with a text label, attribute and annotation', () => {
imgSnapshotTest(
`classDiagram
class C1["Class 1 with text label"] {
<<interface>>
+member1
+attribute1
}
C1 --> C2`
);
Expand Down
6 changes: 3 additions & 3 deletions cypress/integration/rendering/classDiagram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ describe('Class diagram', () => {
class \`This\nTitle\nHas\nMany\nNewlines\` {
+String Also
-Stirng Many
#int Members
#int attribute
+And()
-Many()
#Methods()
Expand All @@ -444,7 +444,7 @@ describe('Class diagram', () => {
class \`This\nTitle\nHas\nMany\nNewlines\` {
+String Also
-Stirng Many
#int Members
#int attribute
+And()
-Many()
#Methods()
Expand All @@ -460,7 +460,7 @@ describe('Class diagram', () => {
class \`This\nTitle\nHas\nMany\nNewlines\` {
+String Also
-Stirng Many
#int Members
#int attribute
+And()
-Many()
#Methods()
Expand Down
1 change: 1 addition & 0 deletions packages/mermaid-layout-elk/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface LabelData {
interface NodeWithVertex extends Omit<Node, 'domId'> {
children?: unknown[];
labelData?: LabelData;

domId?: Node['domId'] | SVGGroup | d3.Selection<SVGAElement, unknown, Element | null, unknown>;
}

Expand Down
18 changes: 9 additions & 9 deletions packages/mermaid/src/dagre-wrapper/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,8 @@ const class_box = (parent, node) => {
maxWidth = classTitleBBox.width;
}
const classAttributes = [];
node.classData.members.forEach((member) => {
const parsedInfo = member.getDisplayDetails();
node.classData.attributes.forEach((attribute) => {
const parsedInfo = attribute.getDisplayDetails();
let parsedText = parsedInfo.displayText;
if (getConfig().flowchart.htmlLabels) {
parsedText = parsedText.replace(/</g, '&lt;').replace(/>/g, '&gt;');
Expand Down Expand Up @@ -984,8 +984,8 @@ const class_box = (parent, node) => {
maxHeight += lineHeight;

const classMethods = [];
node.classData.methods.forEach((member) => {
const parsedInfo = member.getDisplayDetails();
node.classData.methods.forEach((method) => {
const parsedInfo = method.getDisplayDetails();
let displayText = parsedInfo.displayText;
if (getConfig().flowchart.htmlLabels) {
displayText = displayText.replace(/</g, '&lt;').replace(/>/g, '&gt;');
Expand Down Expand Up @@ -1059,9 +1059,9 @@ const class_box = (parent, node) => {
((-1 * maxHeight) / 2 + verticalPos + lineHeight / 2) +
')'
);
//get the height of the bounding box of each member if exists
const memberBBox = lbl?.getBBox();
verticalPos += (memberBBox?.height ?? 0) + rowPadding;
//get the height of the bounding box of each attribute if exists
const fieldBBox = lbl?.getBBox();
verticalPos += (fieldBBox?.height ?? 0) + rowPadding;
});

verticalPos += lineHeight;
Expand All @@ -1079,8 +1079,8 @@ const class_box = (parent, node) => {
'transform',
'translate( ' + -maxWidth / 2 + ', ' + ((-1 * maxHeight) / 2 + verticalPos) + ')'
);
const memberBBox = lbl?.getBBox();
verticalPos += (memberBBox?.height ?? 0) + rowPadding;
const methodBBox = lbl?.getBBox();
verticalPos += (methodBBox?.height ?? 0) + rowPadding;
});

rect
Expand Down
106 changes: 45 additions & 61 deletions packages/mermaid/src/diagrams/class/classDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,18 @@ let functions: any[] = [];

const sanitizeText = (txt: string) => common.sanitizeText(txt, getConfig());

const splitClassNameAndType = function (_id: string) {
const id = common.sanitizeText(_id, getConfig());
const splitClassIdAndType = function (_id: string) {
const id = sanitizeText(_id);
let genericType = '';
let className = id;
let classId = id;

if (id.indexOf('~') > 0) {
const split = id.split('~');
className = sanitizeText(split[0]);
classId = sanitizeText(split[0]);
genericType = sanitizeText(split[1]);
}

return { className: className, type: genericType };
};

export const setClassLabel = function (_id: string, label: string) {
const id = common.sanitizeText(_id, getConfig());
if (label) {
label = sanitizeText(label);
}

const { className } = splitClassNameAndType(id);
classes.get(className)!.label = label;
classes.get(className)!.text =
`${label}${classes.get(className)!.type ? `<${classes.get(className)!.type}>` : ''}`;
return { classId, type: genericType };
};

/**
Expand All @@ -72,28 +60,33 @@ export const setClassLabel = function (_id: string, label: string) {
* @param id - Id of the class to add
* @public
*/
export const addClass = function (_id: string) {
const id = common.sanitizeText(_id, getConfig());
const { className, type } = splitClassNameAndType(id);
// Only add class if not exists
if (classes.has(className)) {
export const addClass = function (_id: string, label?: string) {
const id = sanitizeText(_id);
const { classId, type } = splitClassIdAndType(id);
let newLabel = classId;

if (classes.has(classId)) {
return;
}
// alert('Adding class: ' + className);
const name = common.sanitizeText(className, getConfig());
// alert('Adding class after: ' + name);
classes.set(name, {
id: name,

if (label) {
newLabel = sanitizeText(label);
}

const text = `${newLabel}${type ? `&lt;${type}&gt;` : ''}`;

classes.set(classId, {
id: classId,
type: type,
label: name,
text: `${name}${type ? `&lt;${type}&gt;` : ''}`,
label: newLabel,
text: text,
shape: 'classBox',
cssClasses: 'default',
methods: [],
members: [],
attributes: [],
annotations: [],
styles: [],
domId: MERMAID_DOM_ID_PREFIX + name + '-' + classCounter,
domId: `${MERMAID_DOM_ID_PREFIX}${classId}-${classCounter}`,
} as ClassNode);

classCounter++;
Expand All @@ -116,7 +109,7 @@ const addInterface = function (label: string, classId: string) {
* @public
*/
export const lookUpDomId = function (_id: string): string {
const id = common.sanitizeText(_id, getConfig());
const id = sanitizeText(_id);
if (classes.has(id)) {
return classes.get(id)!.domId;
}
Expand Down Expand Up @@ -182,18 +175,12 @@ export const addRelation = function (classRelation: ClassRelation) {
addClass(classRelation.id2);
}

classRelation.id1 = splitClassNameAndType(classRelation.id1).className;
classRelation.id2 = splitClassNameAndType(classRelation.id2).className;
classRelation.id1 = splitClassIdAndType(classRelation.id1).classId;
classRelation.id2 = splitClassIdAndType(classRelation.id2).classId;

classRelation.relationTitle1 = common.sanitizeText(
classRelation.relationTitle1.trim(),
getConfig()
);
classRelation.relationTitle1 = sanitizeText(classRelation.relationTitle1.trim());

classRelation.relationTitle2 = common.sanitizeText(
classRelation.relationTitle2.trim(),
getConfig()
);
classRelation.relationTitle2 = sanitizeText(classRelation.relationTitle2.trim());

relations.push(classRelation);
};
Expand All @@ -202,29 +189,29 @@ export const addRelation = function (classRelation: ClassRelation) {
* Adds an annotation to the specified class Annotations mark special properties of the given type
* (like 'interface' or 'service')
*
* @param className - The class name
* @param classId - The class name
* @param annotation - The name of the annotation without any brackets
* @public
*/
export const addAnnotation = function (className: string, annotation: string) {
const validatedClassName = splitClassNameAndType(className).className;
const validatedClassName = splitClassIdAndType(className).classId;
classes.get(validatedClassName)!.annotations.push(annotation);
};

/**
* Adds a member to the specified class
*
* @param className - The class name
* @param classId - The class name
* @param member - The full name of the member. If the member is enclosed in `<<brackets>>` it is
* treated as an annotation If the member is ending with a closing bracket ) it is treated as a
* method Otherwise the member will be treated as a normal property
* @public
*/
export const addMember = function (className: string, member: string) {
addClass(className);
export const addMember = function (classId: string, member: string) {
addClass(classId);

const validatedClassName = splitClassNameAndType(className).className;
const theClass = classes.get(validatedClassName)!;
const validatedClassId = splitClassIdAndType(classId).classId;
const theClass = classes.get(validatedClassId)!;

if (typeof member === 'string') {
// Member can contain white spaces, we trim them out
Expand All @@ -237,7 +224,7 @@ export const addMember = function (className: string, member: string) {
//its a method
theClass.methods.push(new ClassMember(memberString, 'method'));
} else if (memberString) {
theClass.members.push(new ClassMember(memberString, 'attribute'));
theClass.attributes.push(new ClassMember(memberString, 'attribute'));
}
}
};
Expand Down Expand Up @@ -377,7 +364,7 @@ export const setClickEvent = function (ids: string, functionName: string, functi
};

const setClickFunc = function (_domId: string, functionName: string, functionArgs: string) {
const domId = common.sanitizeText(_domId, getConfig());
const domId = sanitizeText(_domId);
const config = getConfig();
if (config.securityLevel !== 'loose') {
return;
Expand Down Expand Up @@ -520,17 +507,15 @@ const getNamespaces = function (): NamespaceMap {
* Function called by parser when a namespace definition has been found.
*
* @param id - Id of the namespace to add
* @param classNames - Ids of the class to add
* @param classIds - Ids of the class to add
* @public
*/
export const addClassesToNamespace = function (id: string, classNames: string[]) {
if (!namespaces.has(id)) {
return;
}
for (const name of classNames) {
const { className } = splitClassNameAndType(name);
classes.get(className)!.parent = id;
namespaces.get(id)!.classes.set(className, classes.get(className)!);
export const addClassesToNamespace = function (_id: string, classIds: string[]) {
addNamespace(_id);
for (const id of classIds) {
const { classId } = splitClassIdAndType(id);
classes.get(classId)!.parent = _id;
namespaces.get(_id)!.classes.set(classId, classes.get(classId)!);
}
};

Expand Down Expand Up @@ -726,7 +711,6 @@ export default {
lookUpDomId,
setDiagramTitle,
getDiagramTitle,
setClassLabel,
addNamespace,
addClassesToNamespace,
getNamespace,
Expand Down
Loading
Loading