mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-22 00:40:22 +02:00
Refactors treemap DB to class-based state management on-behalf-of: @MermaidChart shubham-mermaid@mermaidchart.com
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
'mermaid': chore
|
'mermaid': patch
|
||||||
---
|
---
|
||||||
|
|
||||||
chore: updated treemapdb to use class based approach
|
patch: updated treemapdb to use class based approach
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import type { DiagramDB } from '../../diagram-api/types.js';
|
import type { DiagramDB } from '../../diagram-api/types.js';
|
||||||
import { ImperativeState } from '../../utils/imperativeState.js';
|
import type { DiagramStyleClassDef } from '../../diagram-api/types.js';
|
||||||
import type { TreemapData, TreemapDiagramConfig, TreemapNode } from './types.js';
|
import type { TreemapDiagramConfig, TreemapNode } from './types.js';
|
||||||
import DEFAULT_CONFIG from '../../defaultConfig.js';
|
import DEFAULT_CONFIG from '../../defaultConfig.js';
|
||||||
import { getConfig as commonGetConfig } from '../../config.js';
|
import { getConfig as commonGetConfig } from '../../config.js';
|
||||||
import { cleanAndMerge } from '../../utils.js';
|
import { cleanAndMerge } from '../../utils.js';
|
||||||
@@ -15,29 +15,19 @@ import {
|
|||||||
setDiagramTitle,
|
setDiagramTitle,
|
||||||
} from '../common/commonDb.js';
|
} from '../common/commonDb.js';
|
||||||
export class TreeMapDB implements DiagramDB {
|
export class TreeMapDB implements DiagramDB {
|
||||||
private data: TreemapData;
|
private nodes: TreemapNode[] = [];
|
||||||
private state: ImperativeState<TreemapData>;
|
private levels: Map<TreemapNode, number> = new Map<TreemapNode, number>();
|
||||||
|
private outerNodes: TreemapNode[] = [];
|
||||||
constructor() {
|
private classes: Map<string, DiagramStyleClassDef> = new Map<string, DiagramStyleClassDef>();
|
||||||
this.data = {
|
private root?: TreemapNode;
|
||||||
nodes: [],
|
|
||||||
levels: new Map(),
|
|
||||||
outerNodes: [],
|
|
||||||
classes: new Map(),
|
|
||||||
};
|
|
||||||
|
|
||||||
this.state = new ImperativeState<TreemapData>(() => structuredClone(this.data));
|
|
||||||
}
|
|
||||||
|
|
||||||
public getNodes() {
|
public getNodes() {
|
||||||
return this.state.records.nodes;
|
return this.nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getConfig() {
|
public getConfig() {
|
||||||
// Use type assertion with unknown as intermediate step
|
|
||||||
const defaultConfig = DEFAULT_CONFIG as unknown as { treemap: Required<TreemapDiagramConfig> };
|
const defaultConfig = DEFAULT_CONFIG as unknown as { treemap: Required<TreemapDiagramConfig> };
|
||||||
const userConfig = commonGetConfig() as unknown as { treemap?: Partial<TreemapDiagramConfig> };
|
const userConfig = commonGetConfig() as unknown as { treemap?: Partial<TreemapDiagramConfig> };
|
||||||
|
|
||||||
return cleanAndMerge({
|
return cleanAndMerge({
|
||||||
...defaultConfig.treemap,
|
...defaultConfig.treemap,
|
||||||
...(userConfig.treemap ?? {}),
|
...(userConfig.treemap ?? {}),
|
||||||
@@ -45,31 +35,21 @@ export class TreeMapDB implements DiagramDB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public addNode(node: TreemapNode, level: number) {
|
public addNode(node: TreemapNode, level: number) {
|
||||||
const data = this.state.records;
|
this.nodes.push(node);
|
||||||
data.nodes.push(node);
|
this.levels.set(node, level);
|
||||||
data.levels.set(node, level);
|
|
||||||
|
|
||||||
if (level === 0) {
|
if (level === 0) {
|
||||||
data.outerNodes.push(node);
|
this.outerNodes.push(node);
|
||||||
}
|
this.root ??= node;
|
||||||
|
|
||||||
// Set the root node if this is a level 0 node and we don't have a root yet
|
|
||||||
if (level === 0 && !data.root) {
|
|
||||||
data.root = node;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getRoot() {
|
public getRoot() {
|
||||||
return { name: '', children: this.state.records.outerNodes };
|
return { name: '', children: this.outerNodes };
|
||||||
}
|
}
|
||||||
|
|
||||||
public addClass(id: string, _style: string) {
|
public addClass(id: string, _style: string) {
|
||||||
// const classes = this.state.records.classes;
|
const styleClass = this.classes.get(id) ?? { id, styles: [], textStyles: [] };
|
||||||
const styleClass = this.state.records.classes.get(id) ?? { id, styles: [], textStyles: [] };
|
|
||||||
this.state.records.classes.set(id, styleClass);
|
|
||||||
|
|
||||||
const styles = _style.replace(/\\,/g, '§§§').replace(/,/g, ';').replace(/§§§/g, ',').split(';');
|
const styles = _style.replace(/\\,/g, '§§§').replace(/,/g, ';').replace(/§§§/g, ',').split(';');
|
||||||
|
|
||||||
if (styles) {
|
if (styles) {
|
||||||
styles.forEach((s) => {
|
styles.forEach((s) => {
|
||||||
if (isLabelStyle(s)) {
|
if (isLabelStyle(s)) {
|
||||||
@@ -86,27 +66,30 @@ export class TreeMapDB implements DiagramDB {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
this.classes.set(id, styleClass);
|
||||||
this.state.records.classes.set(id, styleClass);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getClasses() {
|
public getClasses() {
|
||||||
return this.state.records.classes;
|
return this.classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getStylesForClass(classSelector: string): string[] {
|
public getStylesForClass(classSelector: string): string[] {
|
||||||
return this.state.records.classes.get(classSelector)?.styles ?? [];
|
return this.classes.get(classSelector)?.styles ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public clear = () => {
|
public clear = () => {
|
||||||
commonClear();
|
commonClear();
|
||||||
this.state.reset();
|
this.nodes = [];
|
||||||
|
this.levels = new Map();
|
||||||
|
this.outerNodes = [];
|
||||||
|
this.classes = new Map();
|
||||||
|
this.root = undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
public setAccTitle = setAccTitle;
|
public setAccTitle = setAccTitle;
|
||||||
public setAccDescription = setAccDescription;
|
|
||||||
public setDiagramTitle = setDiagramTitle;
|
|
||||||
public getAccTitle = getAccTitle;
|
public getAccTitle = getAccTitle;
|
||||||
public getAccDescription = getAccDescription;
|
public setDiagramTitle = setDiagramTitle;
|
||||||
public getDiagramTitle = getDiagramTitle;
|
public getDiagramTitle = getDiagramTitle;
|
||||||
|
public getAccDescription = getAccDescription;
|
||||||
|
public setAccDescription = setAccDescription;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user