Change: use Map for notes

This commit is contained in:
kairi003
2024-09-03 03:48:33 +09:00
parent 58fd5ddbaf
commit 2d2add5b44
5 changed files with 25 additions and 12 deletions

View File

@@ -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;
@@ -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();
@@ -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;
};
@@ -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) {
@@ -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);
@@ -485,6 +494,7 @@ export default {
clear,
getClass,
getClasses,
getNote,
getNotes,
addAnnotation,
addNote,

View File

@@ -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)(
@@ -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}`);
}
);
@@ -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)(
@@ -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}`);
}
);

View File

@@ -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());
@@ -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
@@ -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);

View File

@@ -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);

View File

@@ -136,6 +136,7 @@ export interface ClassNote {
id: string;
class: string;
text: string;
index: number;
}
export interface ClassRelation {
@@ -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>;