Some cleanup

This commit is contained in:
Knut Sveidqvist
2023-09-01 15:40:49 +02:00
parent 8a55b212a2
commit e52de6c279

View File

@@ -15,8 +15,6 @@ import {
} from '../../commonDb.js'; } from '../../commonDb.js';
import { log } from '../../logger.js'; import { log } from '../../logger.js';
// export type TBlockColumnsDefaultValue = 'H'; // Do we support something else, like 'auto' | 0?
// Initialize the node database for simple lookups // Initialize the node database for simple lookups
let blockDatabase: Record<string, Block> = {}; let blockDatabase: Record<string, Block> = {};
@@ -38,14 +36,6 @@ const populateBlockDatabase = (blockList: Block[], parent: Block): void => {
} }
}; };
// Function to get a node by its id
type IGetNodeById = (id: string) => Block | undefined;
export const getBlockById = (id: string): Block | undefined => {
return blockDatabase[id];
};
// TODO: Convert to generic TreeNode type? Convert to class?
let blocks: Block[] = []; let blocks: Block[] = [];
const links: Link[] = []; const links: Link[] = [];
let rootBlock = { id: 'root', type: 'composite', children: [], columns: -1 } as Block; let rootBlock = { id: 'root', type: 'composite', children: [], columns: -1 } as Block;
@@ -78,28 +68,6 @@ export const generateId = () => {
return 'id-' + Math.random().toString(36).substr(2, 12) + '-' + 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
export const addBlock = (_id: string, _label?: string, type?: BlockType) => {
let id = _id;
if (!_id) {
id = generateId();
}
const label = _label || id;
const node: Block = {
id: id,
label,
type: type || 'square',
children: [],
};
blockDatabase[node.id] = node;
// currentBlock.children ??= [];
// currentBlock.children.push(node);
// log.info('currentBlock', currentBlock.children, nodeDatabase);
log.info('addNode called:', id, label, type, node);
return node;
};
type ISetHierarchy = (block: Block[]) => void; type ISetHierarchy = (block: Block[]) => void;
const setHierarchy = (block: Block[]): void => { const setHierarchy = (block: Block[]): void => {
populateBlockDatabase(block, rootBlock); populateBlockDatabase(block, rootBlock);
@@ -113,25 +81,6 @@ const addLink: IAddLink = (link: Link): Link => {
return link; return link;
}; };
type ISetColumns = (columnsStr: string) => void;
const setColumns = (columnsStr: string): void => {
const columns = columnsStr === 'auto' ? -1 : parseInt(columnsStr);
};
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; type IGetColumns = (blockid: string) => number;
const getColumns = (blockid: string): number => { const getColumns = (blockid: string): number => {
const block = blockDatabase[blockid]; const block = blockDatabase[blockid];
@@ -149,7 +98,6 @@ const getColumns = (blockid: string): number => {
type IGetBlocks = () => Block[]; type IGetBlocks = () => Block[];
const getBlocks: IGetBlocks = () => { const getBlocks: IGetBlocks = () => {
// log.info('Block in test', rootBlock.children || []);
log.info('Block in test', blocks, blocks[0].id); log.info('Block in test', blocks, blocks[0].id);
return blocks || []; return blocks || [];
}; };
@@ -163,36 +111,30 @@ const getLogger: IGetLogger = () => console;
export interface BlockDB extends DiagramDB { export interface BlockDB extends DiagramDB {
clear: () => void; clear: () => void;
getConfig: () => BlockConfig | undefined; getConfig: () => BlockConfig | undefined;
addBlock: IAddBlock;
addLink: IAddLink; addLink: IAddLink;
getLogger: IGetLogger; getLogger: IGetLogger;
getBlocks: IGetBlocks; getBlocks: IGetBlocks;
getLinks: IGetLinks; getLinks: IGetLinks;
setColumns: ISetColumns;
getColumns: IGetColumns; getColumns: IGetColumns;
typeStr2Type: ITypeStr2Type; typeStr2Type: ITypeStr2Type;
setHierarchy: ISetHierarchy; setHierarchy: ISetHierarchy;
getBlockById: IGetNodeById;
generateId: IGenerateId; generateId: IGenerateId;
} }
const db: BlockDB = { const db: BlockDB = {
getConfig: () => configApi.getConfig().block, getConfig: () => configApi.getConfig().block,
addBlock: addBlock,
addLink: addLink, addLink: addLink,
typeStr2Type: typeStr2Type, typeStr2Type: typeStr2Type,
getLogger, // TODO: remove getLogger, // TODO: remove
getBlocks, getBlocks,
getLinks, getLinks,
setHierarchy, setHierarchy,
getBlockById,
// getAccTitle, // getAccTitle,
// setAccTitle, // setAccTitle,
// getAccDescription, // getAccDescription,
// setAccDescription, // setAccDescription,
// getDiagramTitle, // getDiagramTitle,
// setDiagramTitle, // setDiagramTitle,
setColumns,
getColumns, getColumns,
clear, clear,
generateId, generateId,