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

135 lines
3.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 {
2023-08-08 15:56:02 +02:00
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-08-08 15:56:02 +02:00
const links: Link[] = [];
let rootBlock = { ID: 'root', children: [], columns: -1 } as Block;
let currentBlock: Block | undefined;
const clear = (): void => {
2023-08-08 15:56:02 +02:00
rootBlocks = [];
blocks = [];
commonClear();
2023-08-08 15:56:02 +02:00
rootBlock = { ID: 'root', children: [], columns: -1 };
currentBlock = rootBlock;
};
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 => {
2023-08-08 15:56:02 +02:00
if (parent) {
2023-07-11 01:51:10 +02:00
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-08-08 15:56:02 +02:00
type ISetColumns = (columnsStr: string) => void;
const setColumns = (columnsStr: string): void => {
const columns = columnsStr === 'auto' ? -1 : parseInt(columnsStr);
currentBlock!.columns = columns;
};
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 blocks = [rootBlock];
const block = getBlock(blockID, blocks);
if (!block) {
return -1;
}
return block.columns || -1;
};
2023-07-11 01:51:10 +02:00
type IGetBlocks = () => Block[];
2023-08-08 15:56:02 +02:00
const getBlocks: IGetBlocks = () => 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;
addBlock: IAddBlock;
addLink: IAddLink;
2023-07-11 01:51:10 +02:00
getLogger: IGetLogger;
getBlocks: IGetBlocks;
getLinks: IGetLinks;
2023-08-08 15:56:02 +02:00
setColumns: ISetColumns;
getColumns: IGetColumns;
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,
2023-08-08 15:56:02 +02:00
setColumns,
getColumns,
clear,
};
2023-07-07 13:12:18 +02:00
2023-07-10 22:33:11 +02:00
export default db;