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

96 lines
2.1 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 } 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';
2023-07-11 01:51:10 +02:00
// export type TBlockColumnsDefaultValue = 'H'; // Do we support something else, like 'auto' | 0?
2023-07-10 22:33:11 +02:00
2023-07-11 01:51:10 +02:00
// TODO: Convert to generic TreeNode type? Convert to class?
export interface Block {
ID?: string;
2023-07-10 22:33:11 +02:00
label?: string;
parent?: Block;
children?: Block[];
2023-07-11 01:51:10 +02:00
columns?: number; // | TBlockColumnsDefaultValue;
2023-07-10 22:33:11 +02:00
}
2023-07-11 01:51:10 +02:00
export interface Link {
2023-07-10 22:33:11 +02:00
source: Block;
target: Block;
}
2023-07-11 01:51:10 +02:00
let rootBlocks: Block[] = [];
let blocks: Block[] = [];
2023-07-10 22:33:11 +02:00
let links: Link[] = [];
const clear = (): void => {
blocks = [];
commonClear();
};
2023-07-10 22:33:11 +02:00
type IAddBlock = (block: Block) => Block;
2023-07-11 01:51:10 +02:00
const addBlock: IAddBlock = (block: Block, parent?: Block): Block => {
if(parent) {
parent.children ??= [];
parent.children.push(block);
} else {
rootBlocks.push(block);
}
2023-07-10 22:33:11 +02:00
blocks.push(block);
return block;
};
type IAddLink = (link: Link) => Link;
const addLink: IAddLink = (link: Link): Link => {
links.push(link);
return link;
};
2023-07-11 01:51:10 +02:00
type IGetBlocks = () => Block[];
const getBlocks:IGetBlocks = () => blocks;
type IGetLinks = () => Link[];
const getLinks:IGetLinks = () => links;
type IGetLogger = () => Console;
const getLogger:IGetLogger = () => console;
2023-07-10 22:33:11 +02:00
export interface BlockDB extends DiagramDB {
clear: () => void;
getConfig: () => BlockConfig | undefined;
addBlock: IAddBlock;
addLink: IAddLink;
2023-07-11 01:51:10 +02:00
getLogger: IGetLogger;
getBlocks: IGetBlocks;
getLinks: IGetLinks;
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
addBlock: addBlock,
addLink: addLink,
2023-07-11 01:51:10 +02:00
getLogger, // TODO: remove
getBlocks,
getLinks,
2023-07-07 13:12:18 +02:00
// getAccTitle,
// setAccTitle,
// getAccDescription,
// setAccDescription,
// getDiagramTitle,
// setDiagramTitle,
clear,
};
2023-07-07 13:12:18 +02:00
2023-07-10 22:33:11 +02:00
export default db;