mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-18 23:09:49 +02:00
Some cleanup
This commit is contained in:
@@ -15,8 +15,6 @@ import {
|
||||
} from '../../commonDb.js';
|
||||
import { log } from '../../logger.js';
|
||||
|
||||
// export type TBlockColumnsDefaultValue = 'H'; // Do we support something else, like 'auto' | 0?
|
||||
|
||||
// Initialize the node database for simple lookups
|
||||
let blockDatabase: Record<string, Block> = {};
|
||||
|
||||
@@ -38,14 +36,6 @@ const populateBlockDatabase = (blockList: Block[], parent: Block): void => {
|
||||
}
|
||||
};
|
||||
|
||||
// Function to get a node by its id
|
||||
type IGetNodeById = (id: string) => Block | undefined;
|
||||
export const getBlockById = (id: string): Block | undefined => {
|
||||
return blockDatabase[id];
|
||||
};
|
||||
|
||||
// TODO: Convert to generic TreeNode type? Convert to class?
|
||||
|
||||
let blocks: Block[] = [];
|
||||
const links: Link[] = [];
|
||||
let rootBlock = { id: 'root', type: 'composite', children: [], columns: -1 } as Block;
|
||||
@@ -78,28 +68,6 @@ export const generateId = () => {
|
||||
return 'id-' + Math.random().toString(36).substr(2, 12) + '-' + cnt;
|
||||
};
|
||||
|
||||
type IAddBlock = (_id: string, label: string, type: BlockType) => Block;
|
||||
// Function to add a node to the database
|
||||
export const addBlock = (_id: string, _label?: string, type?: BlockType) => {
|
||||
let id = _id;
|
||||
if (!_id) {
|
||||
id = generateId();
|
||||
}
|
||||
const label = _label || id;
|
||||
const node: Block = {
|
||||
id: id,
|
||||
label,
|
||||
type: type || 'square',
|
||||
children: [],
|
||||
};
|
||||
blockDatabase[node.id] = node;
|
||||
// currentBlock.children ??= [];
|
||||
// currentBlock.children.push(node);
|
||||
// log.info('currentBlock', currentBlock.children, nodeDatabase);
|
||||
log.info('addNode called:', id, label, type, node);
|
||||
return node;
|
||||
};
|
||||
|
||||
type ISetHierarchy = (block: Block[]) => void;
|
||||
const setHierarchy = (block: Block[]): void => {
|
||||
populateBlockDatabase(block, rootBlock);
|
||||
@@ -113,25 +81,6 @@ const addLink: IAddLink = (link: Link): Link => {
|
||||
return link;
|
||||
};
|
||||
|
||||
type ISetColumns = (columnsStr: string) => void;
|
||||
const setColumns = (columnsStr: string): void => {
|
||||
const columns = columnsStr === 'auto' ? -1 : parseInt(columnsStr);
|
||||
};
|
||||
|
||||
const getBlock = (id: string, blocks: Block[]): Block | undefined => {
|
||||
for (const block of blocks) {
|
||||
if (block.id === id) {
|
||||
return block;
|
||||
}
|
||||
if (block.children) {
|
||||
const foundBlock = getBlock(id, block.children);
|
||||
if (foundBlock) {
|
||||
return foundBlock;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
type IGetColumns = (blockid: string) => number;
|
||||
const getColumns = (blockid: string): number => {
|
||||
const block = blockDatabase[blockid];
|
||||
@@ -149,7 +98,6 @@ const getColumns = (blockid: string): number => {
|
||||
|
||||
type IGetBlocks = () => Block[];
|
||||
const getBlocks: IGetBlocks = () => {
|
||||
// log.info('Block in test', rootBlock.children || []);
|
||||
log.info('Block in test', blocks, blocks[0].id);
|
||||
return blocks || [];
|
||||
};
|
||||
@@ -163,36 +111,30 @@ const getLogger: IGetLogger = () => console;
|
||||
export interface BlockDB extends DiagramDB {
|
||||
clear: () => void;
|
||||
getConfig: () => BlockConfig | undefined;
|
||||
addBlock: IAddBlock;
|
||||
addLink: IAddLink;
|
||||
getLogger: IGetLogger;
|
||||
getBlocks: IGetBlocks;
|
||||
getLinks: IGetLinks;
|
||||
setColumns: ISetColumns;
|
||||
getColumns: IGetColumns;
|
||||
typeStr2Type: ITypeStr2Type;
|
||||
setHierarchy: ISetHierarchy;
|
||||
getBlockById: IGetNodeById;
|
||||
generateId: IGenerateId;
|
||||
}
|
||||
|
||||
const db: BlockDB = {
|
||||
getConfig: () => configApi.getConfig().block,
|
||||
addBlock: addBlock,
|
||||
addLink: addLink,
|
||||
typeStr2Type: typeStr2Type,
|
||||
getLogger, // TODO: remove
|
||||
getBlocks,
|
||||
getLinks,
|
||||
setHierarchy,
|
||||
getBlockById,
|
||||
// getAccTitle,
|
||||
// setAccTitle,
|
||||
// getAccDescription,
|
||||
// setAccDescription,
|
||||
// getDiagramTitle,
|
||||
// setDiagramTitle,
|
||||
setColumns,
|
||||
getColumns,
|
||||
clear,
|
||||
generateId,
|
||||
|
Reference in New Issue
Block a user