Skip to content

Commit 7245574

Browse files
committed
refactor: centralize diagramId field and setter into ScopedDiagramDB base class
Extract shared `diagramId` field and `setDiagramId` setter from FlowDB and ClassDB into a new abstract base class `ScopedDiagramDB`. Both DB classes now extend it instead of duplicating the field and method. https://claude.ai/code/session_01FPVnyf54nFQNQnhZ7dSWCE
1 parent d2205a6 commit 7245574

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { DiagramDB } from './types.js';
2+
3+
/**
4+
* Base class for diagram DBs that need scoped DOM element IDs.
5+
* Provides the diagramId field and setter used to prefix domIds
6+
* for uniqueness across multiple diagrams on the same page.
7+
*/
8+
export abstract class ScopedDiagramDB implements DiagramDB {
9+
protected diagramId = '';
10+
11+
public setDiagramId(svgElementId: string) {
12+
this.diagramId = svgElementId;
13+
}
14+
}

packages/mermaid/src/diagram-api/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export interface DiagramDB {
3939
getDirection?: () => string | undefined;
4040
setDirection?: (dir: DiagramOrientation) => void;
4141
setDisplayMode?: (title: string) => void;
42-
setDiagramId?: (svgElementId: string) => void;
4342
bindFunctions?: (element: Element) => void;
4443
}
4544

packages/mermaid/src/diagrams/class/classDb.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ import type {
2626
Interface,
2727
} from './classTypes.js';
2828
import type { Node, Edge } from '../../rendering-util/types.js';
29-
import type { DiagramDB } from '../../diagram-api/types.js';
29+
import { ScopedDiagramDB } from '../../diagram-api/DiagramDBBase.js';
3030
import DOMPurify from 'dompurify';
3131

3232
const MERMAID_DOM_ID_PREFIX = 'classId-';
3333
let classCounter = 0;
3434

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

37-
export class ClassDB implements DiagramDB {
37+
export class ClassDB extends ScopedDiagramDB {
3838
private relations: ClassRelation[] = [];
3939
private classes: ClassMap = new Map<string, ClassNode>();
4040
private readonly styleClasses = new Map<string, StyleClass>();
@@ -43,12 +43,12 @@ export class ClassDB implements DiagramDB {
4343
// private static classCounter = 0;
4444
private namespaces = new Map<string, NamespaceNode>();
4545
private namespaceCounter = 0;
46-
private diagramId = '';
4746

4847
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
4948
private functions: Function[] = [];
5049

5150
constructor() {
51+
super();
5252
this.functions.push(this.setupToolTips.bind(this));
5353
this.clear();
5454

@@ -144,14 +144,6 @@ export class ClassDB implements DiagramDB {
144144
this.interfaces.push(classInterface);
145145
}
146146

147-
/**
148-
* Sets the diagram's SVG element ID, used to prefix domIds for uniqueness
149-
* across multiple diagrams on the same page.
150-
*/
151-
public setDiagramId(svgElementId: string) {
152-
this.diagramId = svgElementId;
153-
}
154-
155147
/**
156148
* Function to lookup domId from id in the graph definition.
157149
* When diagramId is set, returns the prefixed version for DOM uniqueness.

packages/mermaid/src/diagrams/flowchart/flowDb.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { select } from 'd3';
22
import * as yaml from 'js-yaml';
33
import { getConfig, defaultConfig } from '../../diagram-api/diagramAPI.js';
4-
import type { DiagramDB } from '../../diagram-api/types.js';
4+
import { ScopedDiagramDB } from '../../diagram-api/DiagramDBBase.js';
55
import { log } from '../../logger.js';
66
import { isValidShape, type ShapeID } from '../../rendering-util/rendering-elements/shapes.js';
77
import type { Edge, Node } from '../../rendering-util/types.js';
@@ -35,10 +35,9 @@ interface LinkData {
3535
const MERMAID_DOM_ID_PREFIX = 'flowchart-';
3636

3737
// We are using arrow functions assigned to class instance fields instead of methods as they are required by flow JISON
38-
export class FlowDB implements DiagramDB {
38+
export class FlowDB extends ScopedDiagramDB {
3939
private vertexCounter = 0;
4040
private config = getConfig();
41-
private diagramId = '';
4241
private vertices = new Map<string, FlowVertex>();
4342
private edges: FlowEdge[] & { defaultInterpolate?: string; defaultStyle?: string[] } = [];
4443
private classes = new Map<string, FlowClass>();
@@ -56,6 +55,7 @@ export class FlowDB implements DiagramDB {
5655
private funs: ((element: Element) => void)[] = []; // cspell:ignore funs
5756

5857
constructor() {
58+
super();
5959
this.funs.push(this.setupToolTips.bind(this));
6060

6161
// Needed for JISON since it only supports direct properties
@@ -87,14 +87,6 @@ export class FlowDB implements DiagramDB {
8787
return common.sanitizeText(txt, this.config);
8888
}
8989

90-
/**
91-
* Sets the diagram's SVG element ID, used to prefix domIds for uniqueness
92-
* across multiple diagrams on the same page.
93-
*/
94-
public setDiagramId(svgElementId: string) {
95-
this.diagramId = svgElementId;
96-
}
97-
9890
/**
9991
* Function to lookup domId from id in the graph definition.
10092
* When diagramId is set, returns the prefixed version for DOM uniqueness.

0 commit comments

Comments
 (0)