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

200 lines
5.0 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';
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
// Initialize the node database for simple lookups
let nodeDatabase: Record<string, Node> = {};
const blockDatabase: Record<string, Block> = {};
2023-07-10 22:33:11 +02:00
2023-09-01 14:06:13 +02:00
// Function to get a node by its id
type IGetNodeById = (id: string) => Block | undefined;
export const getNodeById = (id: string): Block | undefined => {
console.log(id, nodeDatabase);
return blockDatabase[id];
};
// TODO: Convert to generic TreeNode type? Convert to class?
2023-09-01 14:06:13 +02:00
let rootBlock = { id: 'root', children: [] as Block[], columns: -1 };
let blocks: Block[] = [];
2023-08-08 15:56:02 +02:00
const links: Link[] = [];
2023-09-01 14:06:13 +02:00
// let rootBlock = { id: 'root', children: [], columns: -1 } as Block;
let currentBlock = rootBlock;
const clear = (): void => {
log.info('Clear called');
// rootBlocks = [];
blocks = [] as Block[];
commonClear();
2023-09-01 14:06:13 +02:00
rootBlock = { id: 'root', children: [], columns: -1 };
2023-08-08 15:56:02 +02:00
currentBlock = rootBlock;
nodeDatabase = {};
2023-09-01 14:06:13 +02:00
blockDatabase[rootBlock.id] = rootBlock;
};
// type IAddBlock = (block: Block) => Block;
// const addBlock: IAddBlock = (block: Block, parent?: Block): Block => {
// log.info('addBlock', block, parent);
// if (parent) {
// parent.children ??= [];
// parent.children.push(block);
// } else {
// rootBlock.children.push(block);
// }
// blocks.push(block);
// return 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;
export const generateId = () => {
cnt++;
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
2023-09-01 14:06:13 +02:00
export const addBlock = (_id: string, _label?: string, type?: BlockType) => {
let id = _id;
if (!_id) {
id = generateId();
}
const label = _label || id;
const node: Block = {
2023-09-01 14:06:13 +02:00
id: id,
label,
type: type || 'square',
2023-09-01 14:06:13 +02:00
children: [],
};
2023-09-01 14:06:13 +02:00
blockDatabase[node.id] = node;
// currentBlock.children ??= [];
// currentBlock.children.push(node);
// console.log('currentBlock', currentBlock.children, nodeDatabase);
console.log('addNode called:', id, label, type, node);
return node;
2023-07-10 22:33:11 +02:00
};
2023-09-01 14:06:13 +02:00
type ISetHierarchy = (block: Block[]) => void;
const setHierarchy = (block: Block[]): void => {
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-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) {
2023-09-01 14:06:13 +02:00
if (block.id === id) {
2023-08-08 15:56:02 +02:00
return block;
}
if (block.children) {
const foundBlock = getBlock(id, block.children);
if (foundBlock) {
return foundBlock;
}
}
}
};
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 = () => {
// console.log('Block in test', rootBlock.children || []);
console.log('Block in test', blocks, blocks[0].id);
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;
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;
typeStr2Type: ITypeStr2Type;
2023-09-01 14:06:13 +02:00
setHierarchy: ISetHierarchy;
getNodeById: IGetNodeById;
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,
typeStr2Type: typeStr2Type,
2023-07-11 01:51:10 +02:00
getLogger, // TODO: remove
getBlocks,
getLinks,
2023-09-01 14:06:13 +02:00
setHierarchy,
getNodeById,
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;