#3358 Adding support for space blocks and different shapes

This commit is contained in:
Knut Sveidqvist
2023-10-20 12:30:25 +02:00
parent 5619f8771b
commit f3f25c7874
6 changed files with 63 additions and 18 deletions

View File

@@ -358,7 +358,6 @@ const rect = async (parent, node) => {
}; };
const composite = async (parent, node) => { const composite = async (parent, node) => {
console.log('This got called');
const { shapeSvg, bbox, halfPadding } = await labelHelper( const { shapeSvg, bbox, halfPadding } = await labelHelper(
parent, parent,
node, node,
@@ -1075,7 +1074,6 @@ export const clear = () => {
}; };
export const positionNode = (node) => { export const positionNode = (node) => {
console.log('Node id = ', node.id);
const el = nodeElems[node.id]; const el = nodeElems[node.id];
log.trace( log.trace(
'Transforming node', 'Transforming node',

View File

@@ -13,6 +13,7 @@ import {
clear as commonClear, clear as commonClear,
} from '../../commonDb.js'; } from '../../commonDb.js';
import { log } from '../../logger.js'; import { log } from '../../logger.js';
import clone from 'lodash-es/clone.js';
// Initialize the node database for simple lookups // Initialize the node database for simple lookups
let blockDatabase: Record<string, Block> = {}; let blockDatabase: Record<string, Block> = {};
@@ -37,9 +38,18 @@ const populateBlockDatabase = (blockList: Block[], parent: Block): void => {
populateBlockDatabase(block.children, block); populateBlockDatabase(block.children, block);
} }
if (block.type === 'space') {
for (let j = 0; j < block.width; j++) {
const newBlock = clone(block);
newBlock.id = newBlock.id + '-' + j;
blockDatabase[newBlock.id] = newBlock;
children.push(newBlock);
}
} else {
children.push(block); children.push(block);
} }
} }
}
parent.children = children; parent.children = children;
}; };
@@ -57,12 +67,38 @@ const clear = (): void => {
type ITypeStr2Type = (typeStr: string) => BlockType; type ITypeStr2Type = (typeStr: string) => BlockType;
export function typeStr2Type(typeStr: string) { export function typeStr2Type(typeStr: string) {
log.debug('typeStr2Type', typeStr);
// TODO: add all types // TODO: add all types
switch (typeStr) { switch (typeStr) {
case '[]': case '[]':
return 'square'; return 'square';
case '()': case '()':
log.debug('we have a round');
return 'round'; return 'round';
case '(())':
return 'circle';
case '>]':
return 'rect_left_inv_arrow';
case '{}':
return 'question';
case '{{}}':
return 'hexagon';
case '([])':
return 'stadium';
case '[[]]':
return 'subroutine';
case '[()]':
return 'cylinder';
case '((()))':
return 'doublecircle';
case '[//]':
return 'lean_right';
case '[\\\\]':
return 'lean_left';
case '[/\\]':
return 'trapezoid';
case '[\\/]':
return 'inv_trapezoid';
default: default:
return 'square'; return 'square';
} }

View File

@@ -104,11 +104,8 @@ function calcBlockSizes(block: Block, db: BlockDB) {
block.children.length block.children.length
); );
const numChildren = block.children.length;
block.size = { block.size = {
// width: numChildren * (maxWidth + padding) + padding,
width: xSize * (maxWidth + padding) + padding, width: xSize * (maxWidth + padding) + padding,
// height: maxHeight + 2 * padding,
height: ySize * (maxHeight + padding) + padding, height: ySize * (maxHeight + padding) + padding,
x: 0, x: 0,
y: 0, y: 0,

View File

@@ -13,6 +13,7 @@
%x acc_descr %x acc_descr
%x acc_descr_multiline %x acc_descr_multiline
%x string %x string
%x space
%x md_string %x md_string
%x NODE %x NODE
@@ -43,6 +44,9 @@ CRLF \u000D\u000A
["] this.pushState("string"); ["] this.pushState("string");
<string>["] this.popState(); <string>["] this.popState();
<string>[^"]* return "STR"; <string>[^"]* return "STR";
space[:]\d+ { yytext = yytext.replace(/space\:/,'');yy.getLogger().info('SPACE NUM (LEX)', yytext); return 'SPACE_BLOCK'; }
space { yytext = '1'; yy.getLogger().info('COLUMNS (LEX)', yytext); return 'SPACE_BLOCK'; }
<string>[^"]* return "STR";
"style" return 'STYLE'; "style" return 'STYLE';
"default" return 'DEFAULT'; "default" return 'DEFAULT';
"linkStyle" return 'LINKSTYLE'; "linkStyle" return 'LINKSTYLE';
@@ -167,6 +171,8 @@ link
statement statement
: nodeStatement : nodeStatement
| columnsStatement | columnsStatement
| SPACE_BLOCK
{ const num=parseInt($1); const spaceId = yy.generateId(); $$ = { id: spaceId, type:'space', label:'', width: num, children: [] }}
| blockStatement | blockStatement
// SPACELIST node { yy.getLogger().info('Node: ',$2.id);yy.addNode($1.length, $2.id, $2.descr, $2.type); } // SPACELIST node { yy.getLogger().info('Node: ',$2.id);yy.addNode($1.length, $2.id, $2.descr, $2.type); }
// | SPACELIST ICON { yy.getLogger().info('Icon: ',$2);yy.decorateNode({icon: $2}); } // | SPACELIST ICON { yy.getLogger().info('Icon: ',$2);yy.decorateNode({icon: $2}); }
@@ -182,7 +188,7 @@ statement
nodeStatement nodeStatement
: nodeStatement link node { yy.getLogger().info('Rule: nodeStatement (nodeStatement link node) '); $$ = {id: $1.id}; } : nodeStatement link node { yy.getLogger().info('Rule: nodeStatement (nodeStatement link node) '); $$ = {id: $1.id}; }
| node { yy.getLogger().info('Rule: nodeStatement (node) ', $1); $$ = {id: $1.id, label: $1.label, type: yy.typeStr2Type($1)}; } | node { yy.getLogger().info('Rule: nodeStatement (node) ', $1); $$ = {id: $1.id, label: $1.label, type: yy.typeStr2Type($1.typeStr)}; }
; ;
columnsStatement columnsStatement

View File

@@ -163,7 +163,7 @@ describe('Block diagram', function () {
expect(blocks[0].children.length).toBe(2); expect(blocks[0].children.length).toBe(2);
expect(blocks[0].id).not.toBe(undefined); expect(blocks[0].id).not.toBe(undefined);
expect(blocks[0].label).toBe(blocks[0].id); expect(blocks[0].label).toBe('');
expect(blocks[0].type).toBe('composite'); expect(blocks[0].type).toBe('composite');
const aBlock = blocks[0].children[0]; const aBlock = blocks[0].children[0];
@@ -196,12 +196,12 @@ describe('Block diagram', function () {
expect(blocks[0].children.length).toBe(2); expect(blocks[0].children.length).toBe(2);
expect(blocks[0].id).not.toBe(undefined); expect(blocks[0].id).not.toBe(undefined);
expect(blocks[0].label).toBe(blocks[0].id); expect(blocks[0].label).toBe('');
expect(blocks[0].type).toBe('composite'); expect(blocks[0].type).toBe('composite');
expect(secondComposite.children.length).toBe(1); expect(secondComposite.children.length).toBe(1);
expect(secondComposite.id).not.toBe(undefined); expect(secondComposite.id).not.toBe(undefined);
expect(secondComposite.label).toBe(secondComposite.id); expect(secondComposite.label).toBe('');
expect(secondComposite.type).toBe('composite'); expect(secondComposite.type).toBe('composite');
expect(aBlock.id).not.toBe(aBlock); expect(aBlock.id).not.toBe(aBlock);
@@ -284,7 +284,7 @@ describe('Block diagram', function () {
block.parse(str); block.parse(str);
}); });
it.skip('empty blocks', async () => { it('empty blocks', async () => {
const str = `block-beta const str = `block-beta
columns 3 columns 3
space space
@@ -308,7 +308,7 @@ describe('Block diagram', function () {
columns 2 columns 2
mc["Memcache"]:2::black mc["Memcache"]:2::black
`; `;
const apa = 'apan hopar i träden';
block.parse(str); block.parse(str);
}); });

View File

@@ -22,15 +22,21 @@ function getNodeFromBlock(block: Block, db: BlockDB, positioned = false) {
let radious = 0; let radious = 0;
let _shape = ''; let _shape = '';
let layoutOptions = {}; let layoutOptions = {};
let padding;
// Set the shape based parameters // Set the shape based parameters
switch (vertex.type) { switch (vertex.type) {
case 'round': case 'round':
radious = 5; radious = 5;
_shape = 'rect'; _shape = 'rect';
break; break;
// case 'composite-subgraph':
// radious = 0;
// _shape = 'composite';
// break;
case 'composite': case 'composite':
radious = 4; radious = 0;
_shape = 'composite'; _shape = 'composite';
padding = 0;
break; break;
case 'square': case 'square':
_shape = 'rect'; _shape = 'rect';
@@ -119,7 +125,7 @@ function getNodeFromBlock(block: Block, db: BlockDB, positioned = false) {
positioned, positioned,
type: vertex.type, type: vertex.type,
// props: vertex.props, // props: vertex.props,
padding: getConfig()?.flowchart?.padding || 0, padding: padding ?? (getConfig()?.flowchart?.padding || 0),
}; };
return node; return node;
} }
@@ -139,16 +145,18 @@ async function calculateBlockSize(elem: any, block: any, db: any) {
nodeEl.remove(); nodeEl.remove();
} }
export async function insertBlockPositioned(elem: any, block: any, db: any) { export async function insertBlockPositioned(elem: any, block: Block, db: any) {
const node = getNodeFromBlock(block, db, true); const node = getNodeFromBlock(block, db, true);
// if (node.type === 'composite') { // if (node.type === 'composite') {
// return; // return;
// } // }
// Add the element to the DOM to size it // Add the element to the DOM to size it
const obj = db.getBlock(node.id); const obj = db.getBlock(node.id);
if (obj.type !== 'space') {
const nodeEl = await insertNode(elem, node); const nodeEl = await insertNode(elem, node);
positionNode(node); positionNode(node);
} }
}
export async function performOperations( export async function performOperations(
elem: ContainerElement, elem: ContainerElement,