mermaid/packages/mermaid/src/diagrams/block/blockDB.ts

149 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-07-10 22:33:11 +02:00
// import type { BlockDB } from './blockTypes.js';
import type { DiagramDB } from '../../diagram-api/types.js';
import { BlockConfig, BlockType, Block, Link } from './blockTypes.js';
2023-07-07 13:12:18 +02:00
import * as configApi from '../../config.js';
2023-07-07 13:12:18 +02:00
// import common from '../common/common.js';
import {
2023-07-07 13:12:18 +02:00
// setAccTitle,
// getAccTitle,
// getAccDescription,
// setAccDescription,
// setDiagramTitle,
// getDiagramTitle,
clear as commonClear,
} from '../../commonDb.js';
import { log } from '../../logger.js';
// Initialize the node database for simple lookups
2023-09-01 15:33:38 +02:00
let blockDatabase: Record<string, Block> = {};
const populateBlockDatabase = (blockList: Block[], parent: Block): void => {
const children = [];
2023-09-01 15:33:38 +02:00
for (const block of blockList) {
if (block.type === 'column-setting') {
const columns = block.columns || -1;
parent.columns = columns;
} else {
if (!block.label) {
block.label = block.id;
}
blockDatabase[block.id] = block;
if (block.children) {
populateBlockDatabase(block.children, block);
}
if (block.type !== 'column-setting') {
children.push(block);
}
2023-09-01 15:33:38 +02:00
}
}
parent.children = children;
2023-09-01 15:33:38 +02:00
};
2023-07-10 22:33:11 +02:00
let blocks: Block[] = [];
2023-08-08 15:56:02 +02:00
const links: Link[] = [];
2023-09-01 15:33:38 +02:00
let rootBlock = { id: 'root', type: 'composite', children: [], columns: -1 } as Block;
const clear = (): void => {
log.info('Clear called');
commonClear();
2023-09-01 15:33:38 +02:00
rootBlock = { id: 'root', type: 'composite', children: [], columns: -1 } as Block;
blockDatabase = { root: rootBlock };
blocks = [] as Block[];
};
type ITypeStr2Type = (typeStr: string) => BlockType;
export function typeStr2Type(typeStr: string) {
// TODO: add all types
switch (typeStr) {
case '[]':
return 'square';
case '()':
return 'round';
default:
return 'square';
2023-07-11 01:51:10 +02:00
}
}
2023-09-01 14:06:13 +02:00
let cnt = 0;
2023-09-01 15:33:38 +02:00
type IGenerateId = () => string;
2023-09-01 14:06:13 +02:00
export const generateId = () => {
cnt++;
return 'id-' + Math.random().toString(36).substr(2, 12) + '-' + cnt;
};
type ISetHierarchy = (block: Block[]) => void;
const setHierarchy = (block: Block[]): void => {
2023-09-01 15:33:38 +02:00
populateBlockDatabase(block, rootBlock);
log.debug('The hierarchy', JSON.stringify(block, null, 2));
2023-09-01 14:06:13 +02:00
blocks = block;
};
2023-07-10 22:33:11 +02:00
type IAddLink = (link: Link) => Link;
const addLink: IAddLink = (link: Link): Link => {
links.push(link);
return link;
};
2023-09-01 14:06:13 +02:00
type IGetColumns = (blockid: string) => number;
const getColumns = (blockid: string): number => {
const block = blockDatabase[blockid];
2023-08-08 15:56:02 +02:00
if (!block) {
return -1;
}
if (block.columns) {
return block.columns;
}
if (!block.children) {
return -1;
}
return block.children.length;
2023-08-08 15:56:02 +02:00
};
2023-07-11 01:51:10 +02:00
type IGetBlocks = () => Block[];
2023-09-01 14:06:13 +02:00
const getBlocks: IGetBlocks = () => {
2023-09-01 15:33:38 +02:00
log.info('Block in test', blocks, blocks[0].id);
2023-09-01 14:06:13 +02:00
return blocks || [];
};
2023-07-11 01:51:10 +02:00
type IGetLinks = () => Link[];
2023-08-08 15:56:02 +02:00
const getLinks: IGetLinks = () => links;
2023-07-11 01:51:10 +02:00
type IGetLogger = () => Console;
2023-08-08 15:56:02 +02:00
const getLogger: IGetLogger = () => console;
2023-07-11 01:51:10 +02:00
2023-07-10 22:33:11 +02:00
export interface BlockDB extends DiagramDB {
clear: () => void;
getConfig: () => BlockConfig | undefined;
addLink: IAddLink;
2023-07-11 01:51:10 +02:00
getLogger: IGetLogger;
getBlocks: IGetBlocks;
getLinks: IGetLinks;
2023-08-08 15:56:02 +02:00
getColumns: IGetColumns;
typeStr2Type: ITypeStr2Type;
2023-09-01 14:06:13 +02:00
setHierarchy: ISetHierarchy;
2023-09-01 15:33:38 +02:00
generateId: IGenerateId;
2023-07-10 22:33:11 +02:00
}
2023-07-07 13:12:18 +02:00
const db: BlockDB = {
2023-07-07 12:58:30 +02:00
getConfig: () => configApi.getConfig().block,
2023-07-10 22:33:11 +02:00
addLink: addLink,
typeStr2Type: typeStr2Type,
2023-07-11 01:51:10 +02:00
getLogger, // TODO: remove
getBlocks,
getLinks,
2023-09-01 14:06:13 +02:00
setHierarchy,
2023-07-07 13:12:18 +02:00
// getAccTitle,
// setAccTitle,
// getAccDescription,
// setAccDescription,
// getDiagramTitle,
// setDiagramTitle,
2023-08-08 15:56:02 +02:00
getColumns,
clear,
2023-09-01 15:33:38 +02:00
generateId,
};
2023-07-07 13:12:18 +02:00
2023-07-10 22:33:11 +02:00
export default db;