Skip to content

Commit

Permalink
Change: use Map for notes
Browse files Browse the repository at this point in the history
  • Loading branch information
kairi003 committed Sep 3, 2024
1 parent 58fd5dd commit 2d2add5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
18 changes: 14 additions & 4 deletions packages/mermaid/src/diagrams/class/classDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const MERMAID_DOM_ID_PREFIX = 'classId-';

let relations: ClassRelation[] = [];
let classes = new Map<string, ClassNode>();
let notes: ClassNote[] = [];
let notes = new Map<string, ClassNote>();
let classCounter = 0;
let namespaces = new Map<string, NamespaceNode>();
let namespaceCounter = 0;
Expand Down Expand Up @@ -108,7 +108,7 @@ export const lookUpDomId = function (_id: string): string {
export const clear = function () {
relations = [];
classes = new Map();
notes = [];
notes = new Map();
functions = [];
functions.push(setupToolTips);
namespaces = new Map();
Expand All @@ -129,6 +129,11 @@ export const getRelations = function (): ClassRelation[] {
return relations;
};

export const getNote = function (id: string | number) {
const key = typeof id === 'number' ? `note${id}` : id;
return notes.get(key)!;
}

export const getNotes = function () {
return notes;
};
Expand Down Expand Up @@ -200,12 +205,15 @@ export const addMembers = function (className: string, members: string[]) {
};

export const addNote = function (text: string, className: string) {
const index = notes.size;
const note = {
id: `note${notes.length}`,
id: `note${index}`,
class: className,
text: text,
index: index,
};
notes.push(note);
notes.set(note.id, note);
return note.id;
};

export const cleanupLabel = function (label: string) {
Expand Down Expand Up @@ -427,6 +435,7 @@ export const addNamespace = function (id: string) {
namespaces.set(id, {
id: id,
classes: new Map(),
notes: new Map(),
children: {},
domId: MERMAID_DOM_ID_PREFIX + id + '-' + namespaceCounter,
} as NamespaceNode);
Expand Down Expand Up @@ -485,6 +494,7 @@ export default {
clear,
getClass,
getClasses,
getNote,
getNotes,
addAnnotation,
addNote,
Expand Down
8 changes: 4 additions & 4 deletions packages/mermaid/src/diagrams/class/classDiagram.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class C13["With Città foreign language"]
note "This is a keyword: ${keyword}. It truly is."
`;
parser.parse(str);
expect(classDb.getNotes()[0].text).toEqual(`This is a keyword: ${keyword}. It truly is.`);
expect(classDb.getNote(0).text).toEqual(`This is a keyword: ${keyword}. It truly is.`);
});

it.each(keywords)(
Expand All @@ -337,7 +337,7 @@ class C13["With Città foreign language"]
note "${keyword}"`;

parser.parse(str);
expect(classDb.getNotes()[0].text).toEqual(`${keyword}`);
expect(classDb.getNote(0).text).toEqual(`${keyword}`);
}
);

Expand All @@ -351,7 +351,7 @@ class C13["With Città foreign language"]
`;

parser.parse(str);
expect(classDb.getNotes()[0].text).toEqual(`This is a keyword: ${keyword}. It truly is.`);
expect(classDb.getNote(0).text).toEqual(`This is a keyword: ${keyword}. It truly is.`);
});

it.each(keywords)(
Expand All @@ -366,7 +366,7 @@ class C13["With Città foreign language"]
`;

parser.parse(str);
expect(classDb.getNotes()[0].text).toEqual(`${keyword}`);
expect(classDb.getNote(0).text).toEqual(`${keyword}`);
}
);

Expand Down
6 changes: 3 additions & 3 deletions packages/mermaid/src/diagrams/class/classRenderer-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import utils, { getEdgeId } from '../../utils.js';
import { interpolateToCurve, getStylesFromArray } from '../../utils.js';
import { setupGraphViewbox } from '../../setupGraphViewbox.js';
import common from '../common/common.js';
import type { ClassRelation, ClassNote, ClassMap, NamespaceMap } from './classTypes.js';
import type { ClassRelation, ClassMap, ClassNoteMap, NamespaceMap } from './classTypes.js';
import type { EdgeData } from '../../types.js';

const sanitizeText = (txt: string) => common.sanitizeText(txt, getConfig());
Expand Down Expand Up @@ -144,7 +144,7 @@ export const addClasses = function (
* @param classes - Classes
*/
export const addNotes = function (
notes: ClassNote[],
notes: ClassNoteMap,
g: graphlib.Graph,
startEdgeId: number,
classes: ClassMap
Expand Down Expand Up @@ -329,7 +329,7 @@ export const draw = async function (text: string, id: string, _version: string,
const namespaces: NamespaceMap = diagObj.db.getNamespaces();
const classes: ClassMap = diagObj.db.getClasses();
const relations: ClassRelation[] = diagObj.db.getRelations();
const notes: ClassNote[] = diagObj.db.getNotes();
const notes: ClassNoteMap = diagObj.db.getNotes();
log.info(relations);
addNamespaces(namespaces, g, id, diagObj);
addClasses(classes, g, id, diagObj);
Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/diagrams/class/classRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export const draw = function (text, id, _version, diagObj) {
);
});

const notes = diagObj.db.getNotes();
const notes = diagObj.db.getNotes().values();
notes.forEach(function (note) {
log.debug(`Adding note: ${JSON.stringify(note)}`);
const node = svgDraw.drawNote(diagram, note, conf, diagObj);
Expand Down
3 changes: 3 additions & 0 deletions packages/mermaid/src/diagrams/class/classTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export interface ClassNote {
id: string;
class: string;
text: string;
index: number;
}

export interface ClassRelation {
Expand All @@ -158,8 +159,10 @@ export interface NamespaceNode {
id: string;
domId: string;
classes: ClassMap;
notes: ClassNoteMap;
children: NamespaceMap;
}

export type ClassMap = Map<string, ClassNode>;
export type ClassNoteMap = Map<string, ClassNote>;
export type NamespaceMap = Map<string, NamespaceNode>;

0 comments on commit 2d2add5

Please sign in to comment.