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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support member definition to initialize class #4786

Merged
merged 4 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions packages/mermaid/src/diagrams/class/classDb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck - don't check until handle it
import type { Selection } from 'd3';
import { select } from 'd3';
import { log } from '../../logger.js';
Expand Down Expand Up @@ -71,21 +70,21 @@ export const setClassLabel = function (id: string, label: string) {
* @public
*/
export const addClass = function (id: string) {
const classId = splitClassNameAndType(id);
const { className, type } = splitClassNameAndType(id);
// Only add class if not exists
if (classes[classId.className] !== undefined) {
if (Object.hasOwn(classes, className)) {
return;
}

classes[classId.className] = {
id: classId.className,
type: classId.type,
label: classId.className,
classes[className] = {
id: className,
type: type,
label: className,
cssClasses: [],
methods: [],
members: [],
annotations: [],
domId: MERMAID_DOM_ID_PREFIX + classId.className + '-' + classCounter,
domId: MERMAID_DOM_ID_PREFIX + className + '-' + classCounter,
} as ClassNode;

classCounter++;
Expand Down Expand Up @@ -175,6 +174,8 @@ export const addAnnotation = function (className: string, annotation: string) {
* @public
*/
export const addMember = function (className: string, member: string) {
addClass(className);

const validatedClassName = splitClassNameAndType(className).className;
const theClass = classes[validatedClassName];

Expand Down Expand Up @@ -368,6 +369,7 @@ export const relationType = {
const setupToolTips = function (element: Element) {
let tooltipElem: Selection<HTMLDivElement, unknown, HTMLElement, unknown> =
select('.mermaidTooltip');
// @ts-expect-error - Incorrect types
if ((tooltipElem._groups || tooltipElem)[0][0] === null) {
tooltipElem = select('body').append('div').attr('class', 'mermaidTooltip').style('opacity', 0);
}
Expand All @@ -377,7 +379,6 @@ 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
Expand All @@ -387,6 +388,7 @@ const setupToolTips = function (element: Element) {
// @ts-ignore - getBoundingClientRect is not part of the d3 type definition
const rect = this.getBoundingClientRect();

// @ts-expect-error - Incorrect types
tooltipElem.transition().duration(200).style('opacity', '.9');
tooltipElem
.text(el.attr('title'))
Expand All @@ -396,8 +398,8 @@ const setupToolTips = function (element: Element) {
el.classed('hover', true);
})
.on('mouseout', function () {
// @ts-expect-error - Incorrect types
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);
});
Expand Down
16 changes: 16 additions & 0 deletions packages/mermaid/src/diagrams/class/classDiagram.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,22 @@ describe('given a class diagram with members and methods ', function () {
parser.parse(str);
});

it('should handle direct member declaration', function () {
const str = 'classDiagram\n' + 'Car : wheels';

parser.parse(str);
expect(classDb.getClasses()).toHaveProperty('Car');
expect(classDb.getClasses()['Car']['members']).toContain('wheels');
});

it('should handle direct member declaration with type', function () {
const str = 'classDiagram\n' + 'Car : int wheels';

parser.parse(str);
expect(classDb.getClasses()).toHaveProperty('Car');
expect(classDb.getClasses()['Car']['members']).toContain('int wheels');
});

it('should handle simple member declaration with type', function () {
const str = 'classDiagram\n' + 'class Car\n' + 'Car : int wheels';

Expand Down