mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-18 06:49:47 +02:00
before refactoring
This commit is contained in:
@@ -21,17 +21,19 @@ import { log } from '../../logger.js';
|
|||||||
let nodeDatabase: Record<string, Node> = {};
|
let nodeDatabase: Record<string, Node> = {};
|
||||||
const blockDatabase: Record<string, Block> = {};
|
const blockDatabase: Record<string, Block> = {};
|
||||||
|
|
||||||
// Function to get a node by its ID
|
// Function to get a node by its id
|
||||||
export const getNodeById = (id: string): Node | undefined => {
|
type IGetNodeById = (id: string) => Block | undefined;
|
||||||
return nodeDatabase[id];
|
export const getNodeById = (id: string): Block | undefined => {
|
||||||
|
console.log(id, nodeDatabase);
|
||||||
|
return blockDatabase[id];
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: Convert to generic TreeNode type? Convert to class?
|
// TODO: Convert to generic TreeNode type? Convert to class?
|
||||||
|
|
||||||
let rootBlock = { ID: 'root', children: [] as Block[], columns: -1 };
|
let rootBlock = { id: 'root', children: [] as Block[], columns: -1 };
|
||||||
let blocks: Block[] = [];
|
let blocks: Block[] = [];
|
||||||
const links: Link[] = [];
|
const links: Link[] = [];
|
||||||
// let rootBlock = { ID: 'root', children: [], columns: -1 } as Block;
|
// let rootBlock = { id: 'root', children: [], columns: -1 } as Block;
|
||||||
let currentBlock = rootBlock;
|
let currentBlock = rootBlock;
|
||||||
|
|
||||||
const clear = (): void => {
|
const clear = (): void => {
|
||||||
@@ -39,10 +41,10 @@ const clear = (): void => {
|
|||||||
// rootBlocks = [];
|
// rootBlocks = [];
|
||||||
blocks = [] as Block[];
|
blocks = [] as Block[];
|
||||||
commonClear();
|
commonClear();
|
||||||
rootBlock = { ID: 'root', children: [], columns: -1 };
|
rootBlock = { id: 'root', children: [], columns: -1 };
|
||||||
currentBlock = rootBlock;
|
currentBlock = rootBlock;
|
||||||
nodeDatabase = {};
|
nodeDatabase = {};
|
||||||
blockDatabase[rootBlock.ID] = rootBlock;
|
blockDatabase[rootBlock.id] = rootBlock;
|
||||||
};
|
};
|
||||||
|
|
||||||
// type IAddBlock = (block: Block) => Block;
|
// type IAddBlock = (block: Block) => Block;
|
||||||
@@ -71,22 +73,39 @@ export function typeStr2Type(typeStr: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type IAddBlock = (id: string, label: string, type: BlockType) => Block;
|
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
|
// Function to add a node to the database
|
||||||
export const addBlock = (id: string, _label?: string, type?: BlockType) => {
|
export const addBlock = (_id: string, _label?: string, type?: BlockType) => {
|
||||||
log.info('addNode called:', id, _label, type);
|
let id = _id;
|
||||||
|
if (!_id) {
|
||||||
|
id = generateId();
|
||||||
|
}
|
||||||
const label = _label || id;
|
const label = _label || id;
|
||||||
const node: Block = {
|
const node: Block = {
|
||||||
ID: id,
|
id: id,
|
||||||
label,
|
label,
|
||||||
type: type || 'square',
|
type: type || 'square',
|
||||||
|
children: [],
|
||||||
};
|
};
|
||||||
blockDatabase[node.ID] = node;
|
blockDatabase[node.id] = node;
|
||||||
currentBlock.children ??= [];
|
// currentBlock.children ??= [];
|
||||||
currentBlock.children.push(node);
|
// currentBlock.children.push(node);
|
||||||
|
// console.log('currentBlock', currentBlock.children, nodeDatabase);
|
||||||
|
console.log('addNode called:', id, label, type, node);
|
||||||
return node;
|
return node;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type ISetHierarchy = (block: Block[]) => void;
|
||||||
|
const setHierarchy = (block: Block[]): void => {
|
||||||
|
blocks = block;
|
||||||
|
};
|
||||||
|
|
||||||
type IAddLink = (link: Link) => Link;
|
type IAddLink = (link: Link) => Link;
|
||||||
const addLink: IAddLink = (link: Link): Link => {
|
const addLink: IAddLink = (link: Link): Link => {
|
||||||
links.push(link);
|
links.push(link);
|
||||||
@@ -101,7 +120,7 @@ const setColumns = (columnsStr: string): void => {
|
|||||||
|
|
||||||
const getBlock = (id: string, blocks: Block[]): Block | undefined => {
|
const getBlock = (id: string, blocks: Block[]): Block | undefined => {
|
||||||
for (const block of blocks) {
|
for (const block of blocks) {
|
||||||
if (block.ID === id) {
|
if (block.id === id) {
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
if (block.children) {
|
if (block.children) {
|
||||||
@@ -113,9 +132,9 @@ const getBlock = (id: string, blocks: Block[]): Block | undefined => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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];
|
||||||
if (!block) {
|
if (!block) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -129,7 +148,11 @@ const getColumns = (blockID: string): number => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type IGetBlocks = () => Block[];
|
type IGetBlocks = () => Block[];
|
||||||
const getBlocks: IGetBlocks = () => rootBlock.children || [];
|
const getBlocks: IGetBlocks = () => {
|
||||||
|
// console.log('Block in test', rootBlock.children || []);
|
||||||
|
console.log('Block in test', blocks, blocks[0].id);
|
||||||
|
return blocks || [];
|
||||||
|
};
|
||||||
|
|
||||||
type IGetLinks = () => Link[];
|
type IGetLinks = () => Link[];
|
||||||
const getLinks: IGetLinks = () => links;
|
const getLinks: IGetLinks = () => links;
|
||||||
@@ -148,6 +171,8 @@ export interface BlockDB extends DiagramDB {
|
|||||||
setColumns: ISetColumns;
|
setColumns: ISetColumns;
|
||||||
getColumns: IGetColumns;
|
getColumns: IGetColumns;
|
||||||
typeStr2Type: ITypeStr2Type;
|
typeStr2Type: ITypeStr2Type;
|
||||||
|
setHierarchy: ISetHierarchy;
|
||||||
|
getNodeById: IGetNodeById;
|
||||||
}
|
}
|
||||||
|
|
||||||
const db: BlockDB = {
|
const db: BlockDB = {
|
||||||
@@ -158,6 +183,8 @@ const db: BlockDB = {
|
|||||||
getLogger, // TODO: remove
|
getLogger, // TODO: remove
|
||||||
getBlocks,
|
getBlocks,
|
||||||
getLinks,
|
getLinks,
|
||||||
|
setHierarchy,
|
||||||
|
getNodeById,
|
||||||
// getAccTitle,
|
// getAccTitle,
|
||||||
// setAccTitle,
|
// setAccTitle,
|
||||||
// getAccDescription,
|
// getAccDescription,
|
||||||
|
@@ -21,14 +21,15 @@ export type BlockType =
|
|||||||
| 'subroutine'
|
| 'subroutine'
|
||||||
| 'cylinder'
|
| 'cylinder'
|
||||||
| 'group'
|
| 'group'
|
||||||
| 'doublecircle';
|
| 'doublecircle'
|
||||||
|
| 'composite';
|
||||||
|
|
||||||
export interface Block {
|
export interface Block {
|
||||||
ID: string;
|
id: string;
|
||||||
label?: string;
|
label?: string;
|
||||||
parent?: Block;
|
parent?: Block;
|
||||||
type?: BlockType;
|
type?: BlockType;
|
||||||
children?: Block[];
|
children: Block[];
|
||||||
columns?: number; // | TBlockColumnsDefaultValue;
|
columns?: number; // | TBlockColumnsDefaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -137,7 +137,9 @@ seperator
|
|||||||
{yy.getLogger().info('Rule: seperator (EOF) ');}
|
{yy.getLogger().info('Rule: seperator (EOF) ');}
|
||||||
;
|
;
|
||||||
|
|
||||||
start: BLOCK_DIAGRAM_KEY document EOF;
|
start: BLOCK_DIAGRAM_KEY document EOF
|
||||||
|
{console.log('This is the hierarchy ', JSON.stringify($2, null, 2)); yy.setHierarchy($2); }
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
stop
|
stop
|
||||||
@@ -148,9 +150,10 @@ stop
|
|||||||
| stop EOF {yy.getLogger().info('Stop EOF2 ');}
|
| stop EOF {yy.getLogger().info('Stop EOF2 ');}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
//array of statements
|
||||||
document
|
document
|
||||||
: statement { yy.getLogger().info("Rule: statement: ", $1);}
|
: statement { yy.getLogger().info("Rule: statement: ", $1); $$ = [$1]; }
|
||||||
| statement document { yy.getLogger().info("Rule: document statement: ", $1);}
|
| statement document { yy.getLogger().info("Rule: document statement: ", $1, $2); $$ = [$1].concat($2); }
|
||||||
;
|
;
|
||||||
|
|
||||||
link
|
link
|
||||||
@@ -177,8 +180,8 @@ statement
|
|||||||
;
|
;
|
||||||
|
|
||||||
nodeStatement
|
nodeStatement
|
||||||
: nodeStatement link node { yy.getLogger().info('Rule: nodeStatement (nodeStatement link node) '); yy.addBlock($1.id);}
|
: nodeStatement link node { yy.getLogger().info('Rule: nodeStatement (nodeStatement link node) '); yy.addBlock($1.id); $$ = {id: $1.id}; }
|
||||||
| node { yy.getLogger().info('Rule: nodeStatement (node) ', $1); yy.addBlock($1.id, $1.label, yy.typeStr2Type($1)); }
|
| node { yy.getLogger().info('Rule: nodeStatement (node) ', $1); yy.addBlock($1.id, $1.label, yy.typeStr2Type($1)); $$ = {id: $1.id}; }
|
||||||
;
|
;
|
||||||
|
|
||||||
columnsStatement
|
columnsStatement
|
||||||
@@ -186,7 +189,7 @@ columnsStatement
|
|||||||
;
|
;
|
||||||
|
|
||||||
blockStatement
|
blockStatement
|
||||||
: block document end { yy.getLogger().info('Rule: blockStatement : ', $1); }
|
: block document end { console.log('Rule: blockStatement : ', $1, $2, $3); const block = yy.addBlock(undefined, undefined, 'composite'); $$ = { id: block.id, children: $2 }; }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
|
@@ -43,7 +43,14 @@ describe('Block diagram', function () {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
block.parse(str);
|
block.parse(str);
|
||||||
// Todo: DB check that the we have two nodes and that the root block has two columns
|
const blocks = db.getBlocks();
|
||||||
|
expect(blocks.length).toBe(2);
|
||||||
|
expect(blocks[0].ID).toBe('id1');
|
||||||
|
expect(blocks[0].label).toBe('id1');
|
||||||
|
expect(blocks[0].type).toBe('square');
|
||||||
|
expect(blocks[1].ID).toBe('id2');
|
||||||
|
expect(blocks[1].label).toBe('id2');
|
||||||
|
expect(blocks[1].type).toBe('square');
|
||||||
});
|
});
|
||||||
it('a diagram with multiple nodes', async () => {
|
it('a diagram with multiple nodes', async () => {
|
||||||
const str = `block-beta
|
const str = `block-beta
|
||||||
@@ -53,7 +60,17 @@ describe('Block diagram', function () {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
block.parse(str);
|
block.parse(str);
|
||||||
// Todo: DB check that the we have two nodes and that the root block has three columns
|
const blocks = db.getBlocks();
|
||||||
|
expect(blocks.length).toBe(3);
|
||||||
|
expect(blocks[0].ID).toBe('id1');
|
||||||
|
expect(blocks[0].label).toBe('id1');
|
||||||
|
expect(blocks[0].type).toBe('square');
|
||||||
|
expect(blocks[1].ID).toBe('id2');
|
||||||
|
expect(blocks[1].label).toBe('id2');
|
||||||
|
expect(blocks[1].type).toBe('square');
|
||||||
|
expect(blocks[2].ID).toBe('id3');
|
||||||
|
expect(blocks[2].label).toBe('id3');
|
||||||
|
expect(blocks[2].type).toBe('square');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('a node with a square shape and a label', async () => {
|
it('a node with a square shape and a label', async () => {
|
||||||
@@ -62,6 +79,14 @@ describe('Block diagram', function () {
|
|||||||
id2`;
|
id2`;
|
||||||
|
|
||||||
block.parse(str);
|
block.parse(str);
|
||||||
|
const blocks = db.getBlocks();
|
||||||
|
expect(blocks.length).toBe(2);
|
||||||
|
expect(blocks[0].ID).toBe('id');
|
||||||
|
expect(blocks[0].label).toBe('A label');
|
||||||
|
expect(blocks[0].type).toBe('square');
|
||||||
|
expect(blocks[1].ID).toBe('id2');
|
||||||
|
expect(blocks[1].label).toBe('id2');
|
||||||
|
expect(blocks[1].type).toBe('square');
|
||||||
});
|
});
|
||||||
it('a diagram with multiple nodes with edges', async () => {
|
it('a diagram with multiple nodes with edges', async () => {
|
||||||
const str = `block-beta
|
const str = `block-beta
|
||||||
@@ -124,14 +149,59 @@ describe('Block diagram', function () {
|
|||||||
// Todo: DB check that the we have two blocks and that the root block has one column
|
// Todo: DB check that the we have two blocks and that the root block has one column
|
||||||
});
|
});
|
||||||
|
|
||||||
it('compound blocks', async () => {
|
it('compound blocks 2', async () => {
|
||||||
const str = `block-beta
|
const str = `block-beta
|
||||||
block
|
block
|
||||||
aBlock["Block"]
|
aBlock["Block"]
|
||||||
|
bBlock["Block"]
|
||||||
end
|
end
|
||||||
`;
|
`;
|
||||||
|
|
||||||
block.parse(str);
|
block.parse(str);
|
||||||
|
const blocks = db.getBlocks();
|
||||||
|
console.log('blocks', blocks);
|
||||||
|
expect(blocks.length).toBe(1);
|
||||||
|
expect(blocks[0].children.length).toBe(2);
|
||||||
|
expect(blocks[0].id).toBe('id');
|
||||||
|
expect(blocks[0].label).toBe('A label');
|
||||||
|
expect(blocks[0].type).toBe('square');
|
||||||
|
// expect(blocks[1].ID).toBe('id2');
|
||||||
|
// expect(blocks[1].label).toBe('id2');
|
||||||
|
// expect(blocks[1].type).toBe('square');
|
||||||
|
});
|
||||||
|
it.only('compound blocks', async () => {
|
||||||
|
const str = `block-beta
|
||||||
|
block
|
||||||
|
aBlock["ABlock"]
|
||||||
|
block
|
||||||
|
bBlock["BBlock"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
`;
|
||||||
|
|
||||||
|
block.parse(str);
|
||||||
|
const blocks = db.getBlocks();
|
||||||
|
|
||||||
|
const aBlockPos = blocks[0].children[0];
|
||||||
|
const bBlockPos = blocks[0].children[1].children[0];
|
||||||
|
|
||||||
|
const root = db.getNodeById(blocks[0].id);
|
||||||
|
expect(blocks.length).toBe(1);
|
||||||
|
expect(blocks[0].id).not.toBe(undefined);
|
||||||
|
expect(root?.label).toBe(blocks[0].id);
|
||||||
|
expect(blocks[0].children.length).toBe(2);
|
||||||
|
expect(root?.type).toBe('composite');
|
||||||
|
|
||||||
|
const aBlock = db.getNodeById(aBlockPos.id);
|
||||||
|
console.log('aBlock', aBlock);
|
||||||
|
expect(aBlock?.label).toBe('ABlock');
|
||||||
|
expect(aBlock?.type).toBe('square');
|
||||||
|
|
||||||
|
const bBlock = db.getNodeById(bBlockPos.id);
|
||||||
|
|
||||||
|
expect(bBlock.id).toBe('bBlock');
|
||||||
|
expect(bBlock.label).toBe('BBlock');
|
||||||
|
expect(bBlock.type).toBe('square');
|
||||||
});
|
});
|
||||||
it.skip('compound blocks with title', async () => {
|
it.skip('compound blocks with title', async () => {
|
||||||
const str = `block
|
const str = `block
|
||||||
|
Reference in New Issue
Block a user