2023-07-10 22:33:11 +02:00
|
|
|
// import type { BlockDB } from './blockTypes.js';
|
|
|
|
import type { DiagramDB } from '../../diagram-api/types.js';
|
2023-08-28 12:51:49 +02:00
|
|
|
import { BlockConfig, BlockType, Block, Link } from './blockTypes.js';
|
2023-07-07 13:12:18 +02:00
|
|
|
|
2023-07-05 12:01:37 +02:00
|
|
|
import * as configApi from '../../config.js';
|
2023-07-07 13:12:18 +02:00
|
|
|
// import common from '../common/common.js';
|
2023-07-05 12:01:37 +02:00
|
|
|
import {
|
2023-07-07 13:12:18 +02:00
|
|
|
// setAccTitle,
|
|
|
|
// getAccTitle,
|
|
|
|
// getAccDescription,
|
|
|
|
// setAccDescription,
|
|
|
|
// setDiagramTitle,
|
|
|
|
// getDiagramTitle,
|
2023-07-05 12:01:37 +02:00
|
|
|
clear as commonClear,
|
|
|
|
} from '../../commonDb.js';
|
2023-08-28 12:51:49 +02:00
|
|
|
import { log } from '../../logger.js';
|
2023-07-05 12:01:37 +02:00
|
|
|
|
2023-08-28 12:51:49 +02:00
|
|
|
// 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 => {
|
2023-09-01 16:22:23 +02:00
|
|
|
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) {
|
2023-10-03 12:56:47 +02:00
|
|
|
if (block.type === 'composite') {
|
2023-10-03 14:19:08 +02:00
|
|
|
block.label = '';
|
2023-10-03 12:56:47 +02:00
|
|
|
} else {
|
|
|
|
block.label = block.id;
|
|
|
|
}
|
2023-09-01 15:33:38 +02:00
|
|
|
}
|
|
|
|
blockDatabase[block.id] = block;
|
|
|
|
|
|
|
|
if (block.children) {
|
|
|
|
populateBlockDatabase(block.children, block);
|
|
|
|
}
|
2023-09-01 16:22:23 +02:00
|
|
|
if (block.type !== 'column-setting') {
|
|
|
|
children.push(block);
|
|
|
|
}
|
2023-09-01 15:33:38 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-01 16:22:23 +02:00
|
|
|
parent.children = children;
|
2023-09-01 15:33:38 +02:00
|
|
|
};
|
2023-07-10 22:33:11 +02:00
|
|
|
|
2023-07-05 12:01:37 +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;
|
2023-07-05 12:01:37 +02:00
|
|
|
|
|
|
|
const clear = (): void => {
|
2023-08-28 12:51:49 +02:00
|
|
|
log.info('Clear called');
|
2023-07-05 12:01:37 +02:00
|
|
|
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[];
|
2023-07-05 12:01:37 +02:00
|
|
|
};
|
|
|
|
|
2023-08-28 12:51:49 +02:00
|
|
|
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-08-28 12:51:49 +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);
|
2023-09-01 16:22:23 +02:00
|
|
|
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;
|
|
|
|
}
|
2023-08-28 12:51:49 +02:00
|
|
|
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 = () => {
|
|
|
|
return blocks || [];
|
|
|
|
};
|
2023-09-05 11:13:27 +02:00
|
|
|
type IGetBlock = (id: string) => Block | undefined;
|
|
|
|
const getBlock: IGetBlock = (id: string) => {
|
|
|
|
return blockDatabase[id];
|
|
|
|
};
|
|
|
|
type ISetBlock = (block: Block) => void;
|
|
|
|
const setBlock: ISetBlock = (block: Block) => {
|
|
|
|
blockDatabase[block.id] = block;
|
|
|
|
};
|
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;
|
2023-09-05 11:13:27 +02:00
|
|
|
getBlock: IGetBlock;
|
|
|
|
setBlock: ISetBlock;
|
2023-07-11 01:51:10 +02:00
|
|
|
getLinks: IGetLinks;
|
2023-08-08 15:56:02 +02:00
|
|
|
getColumns: IGetColumns;
|
2023-08-28 12:51:49 +02:00
|
|
|
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,
|
2023-08-28 12:51:49 +02:00
|
|
|
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-09-05 11:13:27 +02:00
|
|
|
getBlock,
|
|
|
|
setBlock,
|
2023-07-07 13:12:18 +02:00
|
|
|
// getAccTitle,
|
|
|
|
// setAccTitle,
|
|
|
|
// getAccDescription,
|
|
|
|
// setAccDescription,
|
|
|
|
// getDiagramTitle,
|
|
|
|
// setDiagramTitle,
|
2023-08-08 15:56:02 +02:00
|
|
|
getColumns,
|
2023-07-05 12:01:37 +02:00
|
|
|
clear,
|
2023-09-01 15:33:38 +02:00
|
|
|
generateId,
|
2023-07-05 12:01:37 +02:00
|
|
|
};
|
2023-07-07 13:12:18 +02:00
|
|
|
|
2023-07-10 22:33:11 +02:00
|
|
|
export default db;
|