mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-11-19 20:24:16 +01:00
Merge branch 'develop' into origin/3258_Flowchart_nodeSpacing_Subgraph
This commit is contained in:
@@ -39,15 +39,10 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@braintree/sanitize-url": "^6.0.1",
|
||||
"cytoscape": "^3.23.0",
|
||||
"cytoscape-cose-bilkent": "^4.1.0",
|
||||
"cytoscape-fcose": "^2.1.0",
|
||||
"d3": "^7.0.0",
|
||||
"khroma": "^2.0.0",
|
||||
"non-layered-tidy-tree-layout": "^2.0.2"
|
||||
"khroma": "^2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cytoscape": "^3.19.9",
|
||||
"concurrently": "^8.0.0",
|
||||
"rimraf": "^5.0.0",
|
||||
"mermaid": "workspace:*"
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
],
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@zenuml/core": "^3.0.6"
|
||||
"@zenuml/core": "^3.17.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mermaid": "workspace:^"
|
||||
|
||||
@@ -56,7 +56,7 @@ export const draw = async function (text: string, id: string) {
|
||||
// @ts-expect-error @zenuml/core@3.0.0 exports the wrong type for ZenUml
|
||||
const zenuml = new ZenUml(app);
|
||||
// default is a theme name. More themes to be added and will be configurable in the future
|
||||
await zenuml.render(text, 'theme-mermaid');
|
||||
await zenuml.render(text, { theme: 'default', mode: 'static' });
|
||||
|
||||
const { width, height } = window.getComputedStyle(container);
|
||||
log.debug('zenuml diagram size', width, height);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "mermaid",
|
||||
"version": "10.7.0",
|
||||
"version": "10.8.0",
|
||||
"description": "Markdown-ish syntax for generating flowcharts, sequence diagrams, class diagrams, gantt charts and git graphs.",
|
||||
"type": "module",
|
||||
"module": "./dist/mermaid.core.mjs",
|
||||
@@ -33,7 +33,7 @@
|
||||
"docs:dev": "pnpm docs:pre:vitepress && concurrently \"pnpm --filter ./src/vitepress dev\" \"tsx scripts/docs.cli.mts --watch --vitepress\"",
|
||||
"docs:dev:docker": "pnpm docs:pre:vitepress && concurrently \"pnpm --filter ./src/vitepress dev:docker\" \"tsx scripts/docs.cli.mts --watch --vitepress\"",
|
||||
"docs:serve": "pnpm docs:build:vitepress && vitepress serve src/vitepress",
|
||||
"docs:spellcheck": "cspell --config ../../cSpell.json \"src/docs/**/*.md\"",
|
||||
"docs:spellcheck": "cspell \"src/docs/**/*.md\"",
|
||||
"docs:release-version": "tsx scripts/update-release-version.mts",
|
||||
"docs:verify-version": "tsx scripts/update-release-version.mts --verify",
|
||||
"types:build-config": "tsx scripts/create-types-from-json-schema.mts",
|
||||
@@ -62,15 +62,15 @@
|
||||
"@braintree/sanitize-url": "^6.0.1",
|
||||
"@types/d3-scale": "^4.0.3",
|
||||
"@types/d3-scale-chromatic": "^3.0.0",
|
||||
"cytoscape": "^3.23.0",
|
||||
"cytoscape": "^3.28.1",
|
||||
"cytoscape-cose-bilkent": "^4.1.0",
|
||||
"cytoscape-fcose": "^2.1.0",
|
||||
"d3": "^7.4.0",
|
||||
"d3-sankey": "^0.12.3",
|
||||
"dagre-d3-es": "7.0.10",
|
||||
"dayjs": "^1.11.7",
|
||||
"dompurify": "^3.0.5",
|
||||
"elkjs": "^0.9.0",
|
||||
"katex": "^0.16.9",
|
||||
"khroma": "^2.0.0",
|
||||
"lodash-es": "^4.17.21",
|
||||
"mdast-util-from-markdown": "^1.3.0",
|
||||
@@ -90,6 +90,7 @@
|
||||
"@types/d3-shape": "^3.1.1",
|
||||
"@types/dompurify": "^3.0.2",
|
||||
"@types/jsdom": "^21.1.1",
|
||||
"@types/katex": "^0.16.7",
|
||||
"@types/lodash-es": "^4.17.7",
|
||||
"@types/micromatch": "^4.0.2",
|
||||
"@types/prettier": "^2.7.2",
|
||||
@@ -101,7 +102,6 @@
|
||||
"chokidar": "^3.5.3",
|
||||
"concurrently": "^8.0.1",
|
||||
"cpy-cli": "^4.2.0",
|
||||
"cspell": "^6.31.1",
|
||||
"csstree-validator": "^3.0.0",
|
||||
"globby": "^13.1.4",
|
||||
"jison": "^0.4.18",
|
||||
@@ -122,8 +122,8 @@
|
||||
"typescript": "^5.0.4",
|
||||
"unist-util-flatmap": "^1.0.0",
|
||||
"unist-util-visit": "^4.1.2",
|
||||
"vitepress": "^1.0.0-alpha.72",
|
||||
"vitepress-plugin-search": "^1.0.4-alpha.20"
|
||||
"vitepress": "^1.0.0-rc.40",
|
||||
"vitepress-plugin-search": "^1.0.4-alpha.22"
|
||||
},
|
||||
"files": [
|
||||
"dist/",
|
||||
|
||||
@@ -127,6 +127,14 @@ export interface MermaidConfig {
|
||||
*
|
||||
*/
|
||||
secure?: string[];
|
||||
/**
|
||||
* This option specifies if Mermaid can expect the dependent to include KaTeX stylesheets for browsers
|
||||
* without their own MathML implementation. If this option is disabled and MathML is not supported, the math
|
||||
* equations are replaced with a warning. If this option is enabled and MathML is not supported, Mermaid will
|
||||
* fall back to legacy rendering for KaTeX.
|
||||
*
|
||||
*/
|
||||
legacyMathML?: boolean;
|
||||
/**
|
||||
* This option controls if the generated ids of nodes in the SVG are
|
||||
* generated randomly or based on a seed.
|
||||
@@ -161,10 +169,20 @@ export interface MermaidConfig {
|
||||
gitGraph?: GitGraphDiagramConfig;
|
||||
c4?: C4DiagramConfig;
|
||||
sankey?: SankeyDiagramConfig;
|
||||
block?: BlockDiagramConfig;
|
||||
dompurifyConfig?: DOMPurifyConfiguration;
|
||||
wrap?: boolean;
|
||||
fontSize?: number;
|
||||
}
|
||||
/**
|
||||
* The object containing configurations specific for block diagrams.
|
||||
*
|
||||
* This interface was referenced by `MermaidConfig`'s JSON-Schema
|
||||
* via the `definition` "BlockDiagramConfig".
|
||||
*/
|
||||
export interface BlockDiagramConfig extends BaseDiagramConfig {
|
||||
padding?: number;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `MermaidConfig`'s JSON-Schema
|
||||
* via the `definition` "BaseDiagramConfig".
|
||||
|
||||
@@ -24,7 +24,7 @@ flowchart
|
||||
|
||||
The new nodes C1 and C2 are a special type of nodes, clusterNodes. ClusterNodes have have the nodes in the cluster including the cluster attached in a graph object.
|
||||
|
||||
When rendering this diagram it it beeing rendered recursively. The diagram is rendered by the dagre-mermaid:render function which in turn will be used to render the node C1 and the node C2. The result of those renderings will be inserted as nodes in the "root" diagram. With this recursive approach it would be possible to have different layout direction for each cluster.
|
||||
When rendering this diagram it is being rendered recursively. The diagram is rendered by the dagre-mermaid:render function which in turn will be used to render the node C1 and the node C2. The result of those renderings will be inserted as nodes in the "root" diagram. With this recursive approach it would be possible to have different layout direction for each cluster.
|
||||
|
||||
```
|
||||
{ clusterNode: true, graph }
|
||||
|
||||
248
packages/mermaid/src/dagre-wrapper/blockArrowHelper.ts
Normal file
248
packages/mermaid/src/dagre-wrapper/blockArrowHelper.ts
Normal file
@@ -0,0 +1,248 @@
|
||||
import type { Direction } from '../../src/diagrams/block/blockTypes.js';
|
||||
|
||||
const expandAndDeduplicateDirections = (directions: Direction[]) => {
|
||||
const uniqueDirections = new Set();
|
||||
|
||||
for (const direction of directions) {
|
||||
switch (direction) {
|
||||
case 'x':
|
||||
uniqueDirections.add('right');
|
||||
uniqueDirections.add('left');
|
||||
break;
|
||||
case 'y':
|
||||
uniqueDirections.add('up');
|
||||
uniqueDirections.add('down');
|
||||
break;
|
||||
default:
|
||||
uniqueDirections.add(direction);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return uniqueDirections;
|
||||
};
|
||||
export const getArrowPoints = (
|
||||
duplicatedDirections: Direction[],
|
||||
bbox: { width: number; height: number },
|
||||
node: any
|
||||
) => {
|
||||
// Expand and deduplicate the provided directions.
|
||||
// for instance: x, right => right, left
|
||||
const directions = expandAndDeduplicateDirections(duplicatedDirections);
|
||||
|
||||
// Factor to divide height for some calculations.
|
||||
const f = 2;
|
||||
|
||||
// Calculated height of the bounding box, accounting for node padding.
|
||||
const height = bbox.height + 2 * node.padding;
|
||||
// Midpoint calculation based on height.
|
||||
const midpoint = height / f;
|
||||
// Calculated width of the bounding box, accounting for additional width and node padding.
|
||||
const width = bbox.width + 2 * midpoint + node.padding;
|
||||
// Padding to use, half of the node padding.
|
||||
const padding = node.padding / 2;
|
||||
|
||||
// Initialize an empty array to store points for the arrow.
|
||||
const points = [];
|
||||
|
||||
if (
|
||||
directions.has('right') &&
|
||||
directions.has('left') &&
|
||||
directions.has('up') &&
|
||||
directions.has('down')
|
||||
) {
|
||||
// SQUARE
|
||||
return [
|
||||
// Bottom
|
||||
{ x: 0, y: 0 },
|
||||
{ x: midpoint, y: 0 },
|
||||
{ x: width / 2, y: 2 * padding },
|
||||
{ x: width - midpoint, y: 0 },
|
||||
{ x: width, y: 0 },
|
||||
|
||||
// Right
|
||||
{ x: width, y: -height / 3 },
|
||||
{ x: width + 2 * padding, y: -height / 2 },
|
||||
{ x: width, y: (-2 * height) / 3 },
|
||||
{ x: width, y: -height },
|
||||
|
||||
// Top
|
||||
{ x: width - midpoint, y: -height },
|
||||
{ x: width / 2, y: -height - 2 * padding },
|
||||
{ x: midpoint, y: -height },
|
||||
|
||||
// Left
|
||||
{ x: 0, y: -height },
|
||||
{ x: 0, y: (-2 * height) / 3 },
|
||||
{ x: -2 * padding, y: -height / 2 },
|
||||
{ x: 0, y: -height / 3 },
|
||||
];
|
||||
}
|
||||
if (directions.has('right') && directions.has('left') && directions.has('up')) {
|
||||
// RECTANGLE_VERTICAL (Top Open)
|
||||
return [
|
||||
{ x: midpoint, y: 0 },
|
||||
{ x: width - midpoint, y: 0 },
|
||||
{ x: width, y: -height / 2 },
|
||||
{ x: width - midpoint, y: -height },
|
||||
{ x: midpoint, y: -height },
|
||||
{ x: 0, y: -height / 2 },
|
||||
];
|
||||
}
|
||||
if (directions.has('right') && directions.has('left') && directions.has('down')) {
|
||||
// RECTANGLE_VERTICAL (Bottom Open)
|
||||
return [
|
||||
{ x: 0, y: 0 },
|
||||
{ x: midpoint, y: -height },
|
||||
{ x: width - midpoint, y: -height },
|
||||
{ x: width, y: 0 },
|
||||
];
|
||||
}
|
||||
if (directions.has('right') && directions.has('up') && directions.has('down')) {
|
||||
// RECTANGLE_HORIZONTAL (Right Open)
|
||||
return [
|
||||
{ x: 0, y: 0 },
|
||||
{ x: width, y: -midpoint },
|
||||
{ x: width, y: -height + midpoint },
|
||||
{ x: 0, y: -height },
|
||||
];
|
||||
}
|
||||
if (directions.has('left') && directions.has('up') && directions.has('down')) {
|
||||
// RECTANGLE_HORIZONTAL (Left Open)
|
||||
return [
|
||||
{ x: width, y: 0 },
|
||||
{ x: 0, y: -midpoint },
|
||||
{ x: 0, y: -height + midpoint },
|
||||
{ x: width, y: -height },
|
||||
];
|
||||
}
|
||||
if (directions.has('right') && directions.has('left')) {
|
||||
// HORIZONTAL_LINE
|
||||
return [
|
||||
{ x: midpoint, y: 0 },
|
||||
{ x: midpoint, y: -padding },
|
||||
{ x: width - midpoint, y: -padding },
|
||||
{ x: width - midpoint, y: 0 },
|
||||
{ x: width, y: -height / 2 },
|
||||
{ x: width - midpoint, y: -height },
|
||||
{ x: width - midpoint, y: -height + padding },
|
||||
{ x: midpoint, y: -height + padding },
|
||||
{ x: midpoint, y: -height },
|
||||
{ x: 0, y: -height / 2 },
|
||||
];
|
||||
}
|
||||
if (directions.has('up') && directions.has('down')) {
|
||||
// VERTICAL_LINE
|
||||
return [
|
||||
// Bottom center
|
||||
{ x: width / 2, y: 0 },
|
||||
// Left pont of bottom arrow
|
||||
{ x: 0, y: -padding },
|
||||
{ x: midpoint, y: -padding },
|
||||
// Left top over vertical section
|
||||
{ x: midpoint, y: -height + padding },
|
||||
{ x: 0, y: -height + padding },
|
||||
// Top of arrow
|
||||
{ x: width / 2, y: -height },
|
||||
{ x: width, y: -height + padding },
|
||||
// Top of right vertical bar
|
||||
{ x: width - midpoint, y: -height + padding },
|
||||
{ x: width - midpoint, y: -padding },
|
||||
{ x: width, y: -padding },
|
||||
];
|
||||
}
|
||||
if (directions.has('right') && directions.has('up')) {
|
||||
// ANGLE_RT
|
||||
return [
|
||||
{ x: 0, y: 0 },
|
||||
{ x: width, y: -midpoint },
|
||||
{ x: 0, y: -height },
|
||||
];
|
||||
}
|
||||
if (directions.has('right') && directions.has('down')) {
|
||||
// ANGLE_RB
|
||||
return [
|
||||
{ x: 0, y: 0 },
|
||||
{ x: width, y: 0 },
|
||||
{ x: 0, y: -height },
|
||||
];
|
||||
}
|
||||
if (directions.has('left') && directions.has('up')) {
|
||||
// ANGLE_LT
|
||||
return [
|
||||
{ x: width, y: 0 },
|
||||
{ x: 0, y: -midpoint },
|
||||
{ x: width, y: -height },
|
||||
];
|
||||
}
|
||||
if (directions.has('left') && directions.has('down')) {
|
||||
// ANGLE_LB
|
||||
return [
|
||||
{ x: width, y: 0 },
|
||||
{ x: 0, y: 0 },
|
||||
{ x: width, y: -height },
|
||||
];
|
||||
}
|
||||
if (directions.has('right')) {
|
||||
// ARROW_RIGHT
|
||||
return [
|
||||
{ x: midpoint, y: -padding },
|
||||
{ x: midpoint, y: -padding },
|
||||
{ x: width - midpoint, y: -padding },
|
||||
{ x: width - midpoint, y: 0 },
|
||||
{ x: width, y: -height / 2 },
|
||||
{ x: width - midpoint, y: -height },
|
||||
{ x: width - midpoint, y: -height + padding },
|
||||
// top left corner of arrow
|
||||
{ x: midpoint, y: -height + padding },
|
||||
{ x: midpoint, y: -height + padding },
|
||||
];
|
||||
}
|
||||
if (directions.has('left')) {
|
||||
// ARROW_LEFT
|
||||
return [
|
||||
{ x: midpoint, y: 0 },
|
||||
{ x: midpoint, y: -padding },
|
||||
// Two points, the right corners
|
||||
{ x: width - midpoint, y: -padding },
|
||||
{ x: width - midpoint, y: -height + padding },
|
||||
{ x: midpoint, y: -height + padding },
|
||||
{ x: midpoint, y: -height },
|
||||
{ x: 0, y: -height / 2 },
|
||||
];
|
||||
}
|
||||
if (directions.has('up')) {
|
||||
// ARROW_TOP
|
||||
return [
|
||||
// Bottom center
|
||||
{ x: midpoint, y: -padding },
|
||||
// Left top over vertical section
|
||||
{ x: midpoint, y: -height + padding },
|
||||
{ x: 0, y: -height + padding },
|
||||
// Top of arrow
|
||||
{ x: width / 2, y: -height },
|
||||
{ x: width, y: -height + padding },
|
||||
// Top of right vertical bar
|
||||
{ x: width - midpoint, y: -height + padding },
|
||||
{ x: width - midpoint, y: -padding },
|
||||
];
|
||||
}
|
||||
if (directions.has('down')) {
|
||||
// ARROW_BOTTOM
|
||||
return [
|
||||
// Bottom center
|
||||
{ x: width / 2, y: 0 },
|
||||
// Left pont of bottom arrow
|
||||
{ x: 0, y: -padding },
|
||||
{ x: midpoint, y: -padding },
|
||||
// Left top over vertical section
|
||||
{ x: midpoint, y: -height + padding },
|
||||
{ x: width - midpoint, y: -height + padding },
|
||||
{ x: width - midpoint, y: -padding },
|
||||
{ x: width, y: -padding },
|
||||
];
|
||||
}
|
||||
|
||||
// POINT
|
||||
return [{ x: 0, y: 0 }];
|
||||
};
|
||||
@@ -69,13 +69,13 @@ const rect = (parent, node) => {
|
||||
if (useHtmlLabels) {
|
||||
label.attr(
|
||||
'transform',
|
||||
// This puts the labal on top of the box instead of inside it
|
||||
// This puts the label on top of the box instead of inside it
|
||||
`translate(${node.x - bbox.width / 2}, ${node.y - node.height / 2 + subGraphTitleTopMargin})`
|
||||
);
|
||||
} else {
|
||||
label.attr(
|
||||
'transform',
|
||||
// This puts the labal on top of the box instead of inside it
|
||||
// This puts the label on top of the box instead of inside it
|
||||
`translate(${node.x}, ${node.y - node.height / 2 + subGraphTitleTopMargin})`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -56,11 +56,11 @@ const createLabel = (_vertexText, style, isTitle, isNode) => {
|
||||
if (evaluate(getConfig().flowchart.htmlLabels)) {
|
||||
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
|
||||
vertexText = vertexText.replace(/\\n|\n/g, '<br />');
|
||||
log.info('vertexText' + vertexText);
|
||||
log.debug('vertexText' + vertexText);
|
||||
const node = {
|
||||
isNode,
|
||||
label: decodeEntities(vertexText).replace(
|
||||
/fa[blrs]?:fa-[\w-]+/g,
|
||||
/fa[blrs]?:fa-[\w-]+/g, // cspell: disable-line
|
||||
(s) => `<i class='${s.replace(':', ' ')}'></i>`
|
||||
),
|
||||
labelStyle: style.replace('fill:', 'color:'),
|
||||
|
||||
@@ -28,7 +28,6 @@ export const insertEdgeLabel = (elem, edge) => {
|
||||
addSvgBackground: true,
|
||||
})
|
||||
: createLabel(edge.label, edge.labelStyle);
|
||||
log.info('abc82', edge, edge.labelType);
|
||||
|
||||
// Create outer g, edgeLabel, this will be positioned after graph layout
|
||||
const edgeLabel = elem.insert('g').attr('class', 'edgeLabel');
|
||||
@@ -135,7 +134,7 @@ function setTerminalWidth(fo, value) {
|
||||
}
|
||||
|
||||
export const positionEdgeLabel = (edge, paths) => {
|
||||
log.info('Moving label abc78 ', edge.id, edge.label, edgeLabels[edge.id]);
|
||||
log.debug('Moving label abc88 ', edge.id, edge.label, edgeLabels[edge.id], paths);
|
||||
let path = paths.updatedPath ? paths.updatedPath : paths.originalPath;
|
||||
const siteConfig = getConfig();
|
||||
const { subGraphTitleTotalMargin } = getSubGraphTitleMargins(siteConfig);
|
||||
@@ -146,7 +145,7 @@ export const positionEdgeLabel = (edge, paths) => {
|
||||
if (path) {
|
||||
// // debugger;
|
||||
const pos = utils.calcLabelPosition(path);
|
||||
log.info(
|
||||
log.debug(
|
||||
'Moving label ' + edge.label + ' from (',
|
||||
x,
|
||||
',',
|
||||
@@ -155,7 +154,7 @@ export const positionEdgeLabel = (edge, paths) => {
|
||||
pos.x,
|
||||
',',
|
||||
pos.y,
|
||||
') abc78'
|
||||
') abc88'
|
||||
);
|
||||
if (paths.updatedPath) {
|
||||
x = pos.x;
|
||||
@@ -221,7 +220,6 @@ export const positionEdgeLabel = (edge, paths) => {
|
||||
};
|
||||
|
||||
const outsideNode = (node, point) => {
|
||||
// log.warn('Checking bounds ', node, point);
|
||||
const x = node.x;
|
||||
const y = node.y;
|
||||
const dx = Math.abs(point.x - x);
|
||||
@@ -235,7 +233,7 @@ const outsideNode = (node, point) => {
|
||||
};
|
||||
|
||||
export const intersection = (node, outsidePoint, insidePoint) => {
|
||||
log.warn(`intersection calc abc89:
|
||||
log.debug(`intersection calc abc89:
|
||||
outsidePoint: ${JSON.stringify(outsidePoint)}
|
||||
insidePoint : ${JSON.stringify(insidePoint)}
|
||||
node : x:${node.x} y:${node.y} w:${node.width} h:${node.height}`);
|
||||
@@ -248,29 +246,11 @@ export const intersection = (node, outsidePoint, insidePoint) => {
|
||||
let r = insidePoint.x < outsidePoint.x ? w - dx : w + dx;
|
||||
const h = node.height / 2;
|
||||
|
||||
// const edges = {
|
||||
// x1: x - w,
|
||||
// x2: x + w,
|
||||
// y1: y - h,
|
||||
// y2: y + h
|
||||
// };
|
||||
|
||||
// if (
|
||||
// outsidePoint.x === edges.x1 ||
|
||||
// outsidePoint.x === edges.x2 ||
|
||||
// outsidePoint.y === edges.y1 ||
|
||||
// outsidePoint.y === edges.y2
|
||||
// ) {
|
||||
// log.warn('abc89 calc equals on edge', outsidePoint, edges);
|
||||
// return outsidePoint;
|
||||
// }
|
||||
|
||||
const Q = Math.abs(outsidePoint.y - insidePoint.y);
|
||||
const R = Math.abs(outsidePoint.x - insidePoint.x);
|
||||
// log.warn();
|
||||
|
||||
if (Math.abs(y - outsidePoint.y) * w > Math.abs(x - outsidePoint.x) * h) {
|
||||
// Intersection is top or bottom of rect.
|
||||
// let q = insidePoint.y < outsidePoint.y ? outsidePoint.y - h - y : y - h - outsidePoint.y;
|
||||
let q = insidePoint.y < outsidePoint.y ? outsidePoint.y - h - y : y - h - outsidePoint.y;
|
||||
r = (R * q) / Q;
|
||||
const res = {
|
||||
@@ -289,7 +269,7 @@ export const intersection = (node, outsidePoint, insidePoint) => {
|
||||
res.y = outsidePoint.y;
|
||||
}
|
||||
|
||||
log.warn(`abc89 topp/bott calc, Q ${Q}, q ${q}, R ${R}, r ${r}`, res);
|
||||
log.debug(`abc89 topp/bott calc, Q ${Q}, q ${q}, R ${R}, r ${r}`, res); // cspell: disable-line
|
||||
|
||||
return res;
|
||||
} else {
|
||||
@@ -306,7 +286,7 @@ export const intersection = (node, outsidePoint, insidePoint) => {
|
||||
let _x = insidePoint.x < outsidePoint.x ? insidePoint.x + R - r : insidePoint.x - R + r;
|
||||
// let _x = insidePoint.x < outsidePoint.x ? insidePoint.x + R - r : outsidePoint.x + r;
|
||||
let _y = insidePoint.y < outsidePoint.y ? insidePoint.y + q : insidePoint.y - q;
|
||||
log.warn(`sides calc abc89, Q ${Q}, q ${q}, R ${R}, r ${r}`, { _x, _y });
|
||||
log.debug(`sides calc abc89, Q ${Q}, q ${q}, R ${R}, r ${r}`, { _x, _y });
|
||||
if (r === 0) {
|
||||
_x = outsidePoint.x;
|
||||
_y = outsidePoint.y;
|
||||
@@ -326,25 +306,20 @@ export const intersection = (node, outsidePoint, insidePoint) => {
|
||||
* and return an update path ending by the border of the node.
|
||||
*
|
||||
* @param {Array} _points
|
||||
* @param {any} boundryNode
|
||||
* @param {any} boundaryNode
|
||||
* @returns {Array} Points
|
||||
*/
|
||||
const cutPathAtIntersect = (_points, boundryNode) => {
|
||||
log.warn('abc88 cutPathAtIntersect', _points, boundryNode);
|
||||
const cutPathAtIntersect = (_points, boundaryNode) => {
|
||||
log.debug('abc88 cutPathAtIntersect', _points, boundaryNode);
|
||||
let points = [];
|
||||
let lastPointOutside = _points[0];
|
||||
let isInside = false;
|
||||
_points.forEach((point) => {
|
||||
// const node = clusterDb[edge.toCluster].node;
|
||||
log.info('abc88 checking point', point, boundryNode);
|
||||
|
||||
// check if point is inside the boundary rect
|
||||
if (!outsideNode(boundryNode, point) && !isInside) {
|
||||
if (!outsideNode(boundaryNode, point) && !isInside) {
|
||||
// First point inside the rect found
|
||||
// Calc the intersection coord between the point anf the last point outside the rect
|
||||
const inter = intersection(boundryNode, lastPointOutside, point);
|
||||
log.warn('abc88 inside', point, lastPointOutside, inter);
|
||||
log.warn('abc88 intersection', inter);
|
||||
const inter = intersection(boundaryNode, lastPointOutside, point);
|
||||
|
||||
// // Check case where the intersection is the same as the last point
|
||||
let pointPresent = false;
|
||||
@@ -354,14 +329,11 @@ const cutPathAtIntersect = (_points, boundryNode) => {
|
||||
// // if (!pointPresent) {
|
||||
if (!points.some((e) => e.x === inter.x && e.y === inter.y)) {
|
||||
points.push(inter);
|
||||
} else {
|
||||
log.warn('abc88 no intersect', inter, points);
|
||||
}
|
||||
// points.push(inter);
|
||||
|
||||
isInside = true;
|
||||
} else {
|
||||
// Outside
|
||||
log.warn('abc88 outside', point, lastPointOutside);
|
||||
lastPointOutside = point;
|
||||
// points.push(point);
|
||||
if (!isInside) {
|
||||
@@ -369,67 +341,31 @@ const cutPathAtIntersect = (_points, boundryNode) => {
|
||||
}
|
||||
}
|
||||
});
|
||||
log.warn('abc88 returning points', points);
|
||||
return points;
|
||||
};
|
||||
|
||||
export const insertEdge = function (elem, e, edge, clusterDb, diagramType, graph, id) {
|
||||
let points = edge.points;
|
||||
log.debug('abc88 InsertEdge: edge=', edge, 'e=', e);
|
||||
let pointsHasChanged = false;
|
||||
const tail = graph.node(e.v);
|
||||
var head = graph.node(e.w);
|
||||
|
||||
log.info('abc88 InsertEdge: ', edge);
|
||||
if (head.intersect && tail.intersect) {
|
||||
if (head?.intersect && tail?.intersect) {
|
||||
points = points.slice(1, edge.points.length - 1);
|
||||
points.unshift(tail.intersect(points[0]));
|
||||
log.info(
|
||||
'Last point',
|
||||
points[points.length - 1],
|
||||
head,
|
||||
head.intersect(points[points.length - 1])
|
||||
);
|
||||
points.push(head.intersect(points[points.length - 1]));
|
||||
}
|
||||
|
||||
if (edge.toCluster) {
|
||||
log.info('to cluster abc88', clusterDb[edge.toCluster]);
|
||||
log.debug('to cluster abc88', clusterDb[edge.toCluster]);
|
||||
points = cutPathAtIntersect(edge.points, clusterDb[edge.toCluster].node);
|
||||
// log.trace('edge', edge);
|
||||
// points = [];
|
||||
// let lastPointOutside; // = edge.points[0];
|
||||
// let isInside = false;
|
||||
// edge.points.forEach(point => {
|
||||
// const node = clusterDb[edge.toCluster].node;
|
||||
// log.warn('checking from', edge.fromCluster, point, node);
|
||||
|
||||
// if (!outsideNode(node, point) && !isInside) {
|
||||
// log.trace('inside', edge.toCluster, point, lastPointOutside);
|
||||
|
||||
// // First point inside the rect
|
||||
// const inter = intersection(node, lastPointOutside, point);
|
||||
|
||||
// let pointPresent = false;
|
||||
// points.forEach(p => {
|
||||
// pointPresent = pointPresent || (p.x === inter.x && p.y === inter.y);
|
||||
// });
|
||||
// // if (!pointPresent) {
|
||||
// if (!points.find(e => e.x === inter.x && e.y === inter.y)) {
|
||||
// points.push(inter);
|
||||
// } else {
|
||||
// log.warn('no intersect', inter, points);
|
||||
// }
|
||||
// isInside = true;
|
||||
// } else {
|
||||
// // outside
|
||||
// lastPointOutside = point;
|
||||
// if (!isInside) points.push(point);
|
||||
// }
|
||||
// });
|
||||
pointsHasChanged = true;
|
||||
}
|
||||
|
||||
if (edge.fromCluster) {
|
||||
log.info('from cluster abc88', clusterDb[edge.fromCluster]);
|
||||
log.debug('from cluster abc88', clusterDb[edge.fromCluster]);
|
||||
points = cutPathAtIntersect(points.reverse(), clusterDb[edge.fromCluster].node).reverse();
|
||||
|
||||
pointsHasChanged = true;
|
||||
@@ -507,8 +443,6 @@ export const insertEdge = function (elem, e, edge, clusterDb, diagramType, graph
|
||||
url = url.replace(/\(/g, '\\(');
|
||||
url = url.replace(/\)/g, '\\)');
|
||||
}
|
||||
log.info('arrowTypeStart', edge.arrowTypeStart);
|
||||
log.info('arrowTypeEnd', edge.arrowTypeEnd);
|
||||
|
||||
addEdgeMarkers(svgPath, edge, url, id, diagramType);
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ import { log } from '../logger.js';
|
||||
import { getSubGraphTitleMargins } from '../utils/subGraphTitleMargins.js';
|
||||
import { getConfig } from '../diagram-api/diagramAPI.js';
|
||||
|
||||
const recursiveRender = async (_elem, graph, diagramtype, id, parentCluster, siteConfig) => {
|
||||
const recursiveRender = async (_elem, graph, diagramType, id, parentCluster, siteConfig) => {
|
||||
log.info('Graph in recursive render: XXX', graphlibJson.write(graph), parentCluster);
|
||||
const dir = graph.graph().rankdir;
|
||||
log.trace('Dir in recursive render - dir:', dir);
|
||||
@@ -65,7 +65,7 @@ const recursiveRender = async (_elem, graph, diagramtype, id, parentCluster, sit
|
||||
const o = await recursiveRender(
|
||||
nodes,
|
||||
node.graph,
|
||||
diagramtype,
|
||||
diagramType,
|
||||
id,
|
||||
graph.node(v),
|
||||
siteConfig
|
||||
@@ -103,7 +103,7 @@ const recursiveRender = async (_elem, graph, diagramtype, id, parentCluster, sit
|
||||
log.info('Edge ' + e.v + ' -> ' + e.w + ': ', e, ' ', JSON.stringify(graph.edge(e)));
|
||||
|
||||
// Check if link is either from or to a cluster
|
||||
log.info('Fix', clusterDb, 'ids:', e.v, e.w, 'Translateing: ', clusterDb[e.v], clusterDb[e.w]);
|
||||
log.info('Fix', clusterDb, 'ids:', e.v, e.w, 'Translating: ', clusterDb[e.v], clusterDb[e.w]);
|
||||
insertEdgeLabel(edgeLabels, edge);
|
||||
});
|
||||
|
||||
@@ -155,7 +155,7 @@ const recursiveRender = async (_elem, graph, diagramtype, id, parentCluster, sit
|
||||
log.info('Edge ' + e.v + ' -> ' + e.w + ': ' + JSON.stringify(edge), edge);
|
||||
|
||||
edge.points.forEach((point) => (point.y += subGraphTitleTotalMargin / 2));
|
||||
const paths = insertEdge(edgePaths, e, edge, clusterDb, diagramtype, graph, id);
|
||||
const paths = insertEdge(edgePaths, e, edge, clusterDb, diagramType, graph, id);
|
||||
positionEdgeLabel(edge, paths);
|
||||
});
|
||||
|
||||
@@ -169,8 +169,8 @@ const recursiveRender = async (_elem, graph, diagramtype, id, parentCluster, sit
|
||||
return { elem, diff };
|
||||
};
|
||||
|
||||
export const render = async (elem, graph, markers, diagramtype, id) => {
|
||||
insertMarkers(elem, markers, diagramtype, id);
|
||||
export const render = async (elem, graph, markers, diagramType, id) => {
|
||||
insertMarkers(elem, markers, diagramType, id);
|
||||
clearNodes();
|
||||
clearEdges();
|
||||
clearClusters();
|
||||
@@ -181,7 +181,7 @@ export const render = async (elem, graph, markers, diagramtype, id) => {
|
||||
log.warn('Graph after:', JSON.stringify(graphlibJson.write(graph)));
|
||||
// log.warn('Graph ever after:', graphlibJson.write(graph.node('A').graph));
|
||||
const siteConfig = getConfig();
|
||||
await recursiveRender(elem, graph, diagramtype, id, undefined, siteConfig);
|
||||
await recursiveRender(elem, graph, diagramType, id, undefined, siteConfig);
|
||||
};
|
||||
|
||||
// const shapeDefinitions = {};
|
||||
|
||||
@@ -13,11 +13,11 @@ export const clear = () => {
|
||||
clusterDb = {};
|
||||
};
|
||||
|
||||
const isDescendant = (id, ancenstorId) => {
|
||||
// if (id === ancenstorId) return true;
|
||||
const isDescendant = (id, ancestorId) => {
|
||||
// if (id === ancestorId) return true;
|
||||
|
||||
log.trace('In isDecendant', ancenstorId, ' ', id, ' = ', descendants[ancenstorId].includes(id));
|
||||
if (descendants[ancenstorId].includes(id)) {
|
||||
log.trace('In isDescendant', ancestorId, ' ', id, ' = ', descendants[ancestorId].includes(id));
|
||||
if (descendants[ancestorId].includes(id)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ const isDescendant = (id, ancenstorId) => {
|
||||
};
|
||||
|
||||
const edgeInCluster = (edge, clusterId) => {
|
||||
log.info('Decendants of ', clusterId, ' is ', descendants[clusterId]);
|
||||
log.info('Descendants of ', clusterId, ' is ', descendants[clusterId]);
|
||||
log.info('Edge is ', edge);
|
||||
// Edges to/from the cluster is not in the cluster, they are in the parent
|
||||
if (edge.v === clusterId) {
|
||||
@@ -36,7 +36,7 @@ const edgeInCluster = (edge, clusterId) => {
|
||||
}
|
||||
|
||||
if (!descendants[clusterId]) {
|
||||
log.debug('Tilt, ', clusterId, ',not in decendants');
|
||||
log.debug('Tilt, ', clusterId, ',not in descendants');
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
@@ -244,7 +244,7 @@ export const adjustClustersAndEdges = (graph, depth) => {
|
||||
// d1 xor d2 - if either d1 is true and d2 is false or the other way around
|
||||
if (d1 ^ d2) {
|
||||
log.warn('Edge: ', edge, ' leaves cluster ', id);
|
||||
log.warn('Decendants of XXX ', id, ': ', descendants[id]);
|
||||
log.warn('Descendants of XXX ', id, ': ', descendants[id]);
|
||||
clusterDb[id].externalConnections = true;
|
||||
}
|
||||
}
|
||||
@@ -286,6 +286,7 @@ export const adjustClustersAndEdges = (graph, depth) => {
|
||||
clusterDb[e.w]
|
||||
);
|
||||
if (clusterDb[e.v] && clusterDb[e.w] && clusterDb[e.v] === clusterDb[e.w]) {
|
||||
// cspell:ignore trixing
|
||||
log.warn('Fixing and trixing link to self - removing XXX', e.v, e.w, e.name);
|
||||
log.warn('Fixing and trixing - removing XXX', e.v, e.w, e.name);
|
||||
v = getAnchorId(e.v);
|
||||
@@ -337,7 +338,7 @@ export const adjustClustersAndEdges = (graph, depth) => {
|
||||
|
||||
// Remove references to extracted cluster
|
||||
// graph.edges().forEach(edge => {
|
||||
// if (isDecendant(edge.v, clusterId) || isDecendant(edge.w, clusterId)) {
|
||||
// if (isDescendant(edge.v, clusterId) || isDescendant(edge.w, clusterId)) {
|
||||
// graph.removeEdge(edge);
|
||||
// }
|
||||
// });
|
||||
|
||||
@@ -6,6 +6,7 @@ import intersect from './intersect/index.js';
|
||||
import createLabel from './createLabel.js';
|
||||
import note from './shapes/note.js';
|
||||
import { evaluate } from '../diagrams/common/common.js';
|
||||
import { getArrowPoints } from './blockArrowHelper.js';
|
||||
|
||||
const formatClass = (str) => {
|
||||
if (str) {
|
||||
@@ -30,6 +31,7 @@ const question = async (parent, node) => {
|
||||
const w = bbox.width + node.padding;
|
||||
const h = bbox.height + node.padding;
|
||||
const s = w + h;
|
||||
|
||||
const points = [
|
||||
{ x: s / 2, y: 0 },
|
||||
{ x: s, y: -s / 2 },
|
||||
@@ -117,6 +119,27 @@ const hexagon = async (parent, node) => {
|
||||
return shapeSvg;
|
||||
};
|
||||
|
||||
const block_arrow = async (parent, node) => {
|
||||
const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true);
|
||||
|
||||
const f = 2;
|
||||
const h = bbox.height + 2 * node.padding;
|
||||
const m = h / f;
|
||||
const w = bbox.width + 2 * m + node.padding;
|
||||
|
||||
const points = getArrowPoints(node.directions, bbox, node);
|
||||
|
||||
const blockArrow = insertPolygonShape(shapeSvg, w, h, points);
|
||||
blockArrow.attr('style', node.style);
|
||||
updateNodeBounds(node, blockArrow);
|
||||
|
||||
node.intersect = function (point) {
|
||||
return intersect.polygon(node, points, point);
|
||||
};
|
||||
|
||||
return shapeSvg;
|
||||
};
|
||||
|
||||
const rect_left_inv_arrow = async (parent, node) => {
|
||||
const { shapeSvg, bbox } = await labelHelper(
|
||||
parent,
|
||||
@@ -374,17 +397,64 @@ const rect = async (parent, node) => {
|
||||
|
||||
// const totalWidth = bbox.width + node.padding * 2;
|
||||
// const totalHeight = bbox.height + node.padding * 2;
|
||||
const totalWidth = bbox.width + node.padding;
|
||||
const totalHeight = bbox.height + node.padding;
|
||||
const totalWidth = node.positioned ? node.width : bbox.width + node.padding;
|
||||
const totalHeight = node.positioned ? node.height : bbox.height + node.padding;
|
||||
const x = node.positioned ? -totalWidth / 2 : -bbox.width / 2 - halfPadding;
|
||||
const y = node.positioned ? -totalHeight / 2 : -bbox.height / 2 - halfPadding;
|
||||
rect
|
||||
.attr('class', 'basic label-container')
|
||||
.attr('style', node.style)
|
||||
.attr('rx', node.rx)
|
||||
.attr('ry', node.ry)
|
||||
// .attr('x', -bbox.width / 2 - node.padding)
|
||||
// .attr('y', -bbox.height / 2 - node.padding)
|
||||
.attr('x', -bbox.width / 2 - halfPadding)
|
||||
.attr('y', -bbox.height / 2 - halfPadding)
|
||||
.attr('x', x)
|
||||
.attr('y', y)
|
||||
.attr('width', totalWidth)
|
||||
.attr('height', totalHeight);
|
||||
|
||||
if (node.props) {
|
||||
const propKeys = new Set(Object.keys(node.props));
|
||||
if (node.props.borders) {
|
||||
applyNodePropertyBorders(rect, node.props.borders, totalWidth, totalHeight);
|
||||
propKeys.delete('borders');
|
||||
}
|
||||
propKeys.forEach((propKey) => {
|
||||
log.warn(`Unknown node property ${propKey}`);
|
||||
});
|
||||
}
|
||||
|
||||
updateNodeBounds(node, rect);
|
||||
|
||||
node.intersect = function (point) {
|
||||
return intersect.rect(node, point);
|
||||
};
|
||||
|
||||
return shapeSvg;
|
||||
};
|
||||
|
||||
const composite = async (parent, node) => {
|
||||
const { shapeSvg, bbox, halfPadding } = await labelHelper(
|
||||
parent,
|
||||
node,
|
||||
'node ' + node.classes,
|
||||
true
|
||||
);
|
||||
|
||||
// add the rect
|
||||
const rect = shapeSvg.insert('rect', ':first-child');
|
||||
|
||||
// const totalWidth = bbox.width + node.padding * 2;
|
||||
// const totalHeight = bbox.height + node.padding * 2;
|
||||
const totalWidth = node.positioned ? node.width : bbox.width + node.padding;
|
||||
const totalHeight = node.positioned ? node.height : bbox.height + node.padding;
|
||||
const x = node.positioned ? -totalWidth / 2 : -bbox.width / 2 - halfPadding;
|
||||
const y = node.positioned ? -totalHeight / 2 : -bbox.height / 2 - halfPadding;
|
||||
rect
|
||||
.attr('class', 'basic cluster composite label-container')
|
||||
.attr('style', node.style)
|
||||
.attr('rx', node.rx)
|
||||
.attr('ry', node.ry)
|
||||
.attr('x', x)
|
||||
.attr('y', y)
|
||||
.attr('width', totalWidth)
|
||||
.attr('height', totalHeight);
|
||||
|
||||
@@ -1031,6 +1101,7 @@ const class_box = (parent, node) => {
|
||||
|
||||
const shapes = {
|
||||
rhombus: question,
|
||||
composite,
|
||||
question,
|
||||
rect,
|
||||
labelRect,
|
||||
@@ -1040,6 +1111,7 @@ const shapes = {
|
||||
doublecircle,
|
||||
stadium,
|
||||
hexagon,
|
||||
block_arrow,
|
||||
rect_left_inv_arrow,
|
||||
lean_right,
|
||||
lean_left,
|
||||
@@ -1082,6 +1154,9 @@ export const insertNode = async (elem, node, dir) => {
|
||||
if (node.class) {
|
||||
el.attr('class', 'node default ' + node.class);
|
||||
}
|
||||
// MC Special
|
||||
newEl.attr('data-node', 'true');
|
||||
newEl.attr('data-id', node.id);
|
||||
|
||||
nodeElems[node.id] = newEl;
|
||||
|
||||
@@ -1099,7 +1174,6 @@ export const clear = () => {
|
||||
|
||||
export const positionNode = (node) => {
|
||||
const el = nodeElems[node.id];
|
||||
|
||||
log.trace(
|
||||
'Transforming node',
|
||||
node.diff,
|
||||
|
||||
@@ -115,6 +115,7 @@ export const labelHelper = async (parent, node, _classes, isNode) => {
|
||||
label.attr('transform', 'translate(' + -bbox.width / 2 + ', ' + -bbox.height / 2 + ')');
|
||||
}
|
||||
label.insert('rect', ':first-child');
|
||||
|
||||
return { shapeSvg, bbox, halfPadding, label };
|
||||
};
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import flowchartElk from '../diagrams/flowchart/elk/detector.js';
|
||||
import timeline from '../diagrams/timeline/detector.js';
|
||||
import mindmap from '../diagrams/mindmap/detector.js';
|
||||
import sankey from '../diagrams/sankey/sankeyDetector.js';
|
||||
import block from '../diagrams/block/blockDetector.js';
|
||||
import { registerLazyLoadedDiagrams } from './detectType.js';
|
||||
import { registerDiagram } from './diagramAPI.js';
|
||||
|
||||
@@ -87,6 +88,7 @@ export const addDiagrams = () => {
|
||||
journey,
|
||||
quadrantChart,
|
||||
sankey,
|
||||
xychart
|
||||
xychart,
|
||||
block
|
||||
);
|
||||
};
|
||||
|
||||
@@ -32,7 +32,7 @@ export interface DiagramDB {
|
||||
getDiagramTitle?: () => string;
|
||||
setAccTitle?: (title: string) => void;
|
||||
getAccTitle?: () => string;
|
||||
setAccDescription?: (describetion: string) => void;
|
||||
setAccDescription?: (description: string) => void;
|
||||
getAccDescription?: () => string;
|
||||
|
||||
setDisplayMode?: (title: string) => void;
|
||||
|
||||
311
packages/mermaid/src/diagrams/block/blockDB.ts
Normal file
311
packages/mermaid/src/diagrams/block/blockDB.ts
Normal file
@@ -0,0 +1,311 @@
|
||||
import type { DiagramDB } from '../../diagram-api/types.js';
|
||||
import type { BlockConfig, BlockType, Block, ClassDef } from './blockTypes.js';
|
||||
import * as configApi from '../../config.js';
|
||||
import { clear as commonClear } from '../common/commonDb.js';
|
||||
import { log } from '../../logger.js';
|
||||
import clone from 'lodash-es/clone.js';
|
||||
|
||||
// Initialize the node database for simple lookups
|
||||
let blockDatabase: Record<string, Block> = {};
|
||||
let edgeList: Block[] = [];
|
||||
let edgeCount: Record<string, number> = {};
|
||||
|
||||
const COLOR_KEYWORD = 'color';
|
||||
const FILL_KEYWORD = 'fill';
|
||||
const BG_FILL = 'bgFill';
|
||||
const STYLECLASS_SEP = ',';
|
||||
|
||||
let classes = {} as Record<string, ClassDef>;
|
||||
|
||||
/**
|
||||
* Called when the parser comes across a (style) class definition
|
||||
* @example classDef my-style fill:#f96;
|
||||
*
|
||||
* @param id - the id of this (style) class
|
||||
* @param styleAttributes - the string with 1 or more style attributes (each separated by a comma)
|
||||
*/
|
||||
export const addStyleClass = function (id: string, styleAttributes = '') {
|
||||
// create a new style class object with this id
|
||||
if (classes[id] === undefined) {
|
||||
classes[id] = { id: id, styles: [], textStyles: [] }; // This is a classDef
|
||||
}
|
||||
const foundClass = classes[id];
|
||||
if (styleAttributes !== undefined && styleAttributes !== null) {
|
||||
styleAttributes.split(STYLECLASS_SEP).forEach((attrib) => {
|
||||
// remove any trailing ;
|
||||
const fixedAttrib = attrib.replace(/([^;]*);/, '$1').trim();
|
||||
|
||||
// replace some style keywords
|
||||
if (attrib.match(COLOR_KEYWORD)) {
|
||||
const newStyle1 = fixedAttrib.replace(FILL_KEYWORD, BG_FILL);
|
||||
const newStyle2 = newStyle1.replace(COLOR_KEYWORD, FILL_KEYWORD);
|
||||
foundClass.textStyles.push(newStyle2);
|
||||
}
|
||||
foundClass.styles.push(fixedAttrib);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Called when the parser comes across a style definition
|
||||
* @example style my-block-id fill:#f96;
|
||||
*
|
||||
* @param id - the id of the block to style
|
||||
* @param styles - the string with 1 or more style attributes (each separated by a comma)
|
||||
*/
|
||||
export const addStyle2Node = function (id: string, styles = '') {
|
||||
const foundBlock = blockDatabase[id];
|
||||
if (styles !== undefined && styles !== null) {
|
||||
foundBlock.styles = styles.split(STYLECLASS_SEP);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Add a CSS/style class to the block with the given id.
|
||||
* If the block isn't already in the list of known blocks, add it.
|
||||
* Might be called by parser when a CSS/style class should be applied to a block
|
||||
*
|
||||
* @param itemIds - The id or a list of ids of the item(s) to apply the css class to
|
||||
* @param cssClassName - CSS class name
|
||||
*/
|
||||
export const setCssClass = function (itemIds: string, cssClassName: string) {
|
||||
itemIds.split(',').forEach(function (id: string) {
|
||||
let foundBlock = blockDatabase[id];
|
||||
if (foundBlock === undefined) {
|
||||
const trimmedId = id.trim();
|
||||
blockDatabase[trimmedId] = { id: trimmedId, type: 'na', children: [] } as Block;
|
||||
foundBlock = blockDatabase[trimmedId];
|
||||
}
|
||||
if (!foundBlock.classes) {
|
||||
foundBlock.classes = [];
|
||||
}
|
||||
foundBlock.classes.push(cssClassName);
|
||||
});
|
||||
};
|
||||
|
||||
const populateBlockDatabase = (_blockList: Block[] | Block[][], parent: Block): void => {
|
||||
const blockList = _blockList.flat();
|
||||
const children = [];
|
||||
for (const block of blockList) {
|
||||
if (block.type === 'classDef') {
|
||||
addStyleClass(block.id, block.css);
|
||||
continue;
|
||||
}
|
||||
if (block.type === 'applyClass') {
|
||||
setCssClass(block.id, block?.styleClass || '');
|
||||
continue;
|
||||
}
|
||||
if (block.type === 'applyStyles') {
|
||||
if (block?.stylesStr) {
|
||||
addStyle2Node(block.id, block?.stylesStr);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (block.type === 'column-setting') {
|
||||
parent.columns = block.columns || -1;
|
||||
} else if (block.type === 'edge') {
|
||||
if (edgeCount[block.id]) {
|
||||
edgeCount[block.id]++;
|
||||
} else {
|
||||
edgeCount[block.id] = 1;
|
||||
}
|
||||
block.id = edgeCount[block.id] + '-' + block.id;
|
||||
edgeList.push(block);
|
||||
} else {
|
||||
if (!block.label) {
|
||||
if (block.type === 'composite') {
|
||||
block.label = '';
|
||||
// log.debug('abc89 composite', block);
|
||||
} else {
|
||||
block.label = block.id;
|
||||
}
|
||||
}
|
||||
const newBlock = !blockDatabase[block.id];
|
||||
if (newBlock) {
|
||||
blockDatabase[block.id] = block;
|
||||
} else {
|
||||
// Add newer relevant data to aggregated node
|
||||
if (block.type !== 'na') {
|
||||
blockDatabase[block.id].type = block.type;
|
||||
}
|
||||
if (block.label !== block.id) {
|
||||
blockDatabase[block.id].label = block.label;
|
||||
}
|
||||
}
|
||||
|
||||
if (block.children) {
|
||||
populateBlockDatabase(block.children, block);
|
||||
}
|
||||
if (block.type === 'space') {
|
||||
// log.debug('abc95 space', block);
|
||||
const w = block.width || 1;
|
||||
for (let j = 0; j < w; j++) {
|
||||
const newBlock = clone(block);
|
||||
newBlock.id = newBlock.id + '-' + j;
|
||||
blockDatabase[newBlock.id] = newBlock;
|
||||
children.push(newBlock);
|
||||
}
|
||||
} else if (newBlock) {
|
||||
children.push(block);
|
||||
}
|
||||
}
|
||||
}
|
||||
parent.children = children;
|
||||
};
|
||||
|
||||
let blocks: Block[] = [];
|
||||
let rootBlock = { id: 'root', type: 'composite', children: [], columns: -1 } as Block;
|
||||
|
||||
const clear = (): void => {
|
||||
log.debug('Clear called');
|
||||
commonClear();
|
||||
rootBlock = { id: 'root', type: 'composite', children: [], columns: -1 } as Block;
|
||||
blockDatabase = { root: rootBlock };
|
||||
blocks = [] as Block[];
|
||||
classes = {} as Record<string, ClassDef>;
|
||||
|
||||
edgeList = [];
|
||||
edgeCount = {};
|
||||
};
|
||||
|
||||
export function typeStr2Type(typeStr: string) {
|
||||
log.debug('typeStr2Type', typeStr);
|
||||
switch (typeStr) {
|
||||
case '[]':
|
||||
return 'square';
|
||||
case '()':
|
||||
log.debug('we have a round');
|
||||
return 'round';
|
||||
case '(())':
|
||||
return 'circle';
|
||||
case '>]':
|
||||
return 'rect_left_inv_arrow';
|
||||
case '{}':
|
||||
return 'diamond';
|
||||
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';
|
||||
case '<[]>':
|
||||
return 'block_arrow';
|
||||
default:
|
||||
return 'na';
|
||||
}
|
||||
}
|
||||
|
||||
export function edgeTypeStr2Type(typeStr: string): string {
|
||||
log.debug('typeStr2Type', typeStr);
|
||||
switch (typeStr) {
|
||||
case '==':
|
||||
return 'thick';
|
||||
default:
|
||||
return 'normal';
|
||||
}
|
||||
}
|
||||
|
||||
export function edgeStrToEdgeData(typeStr: string): string {
|
||||
switch (typeStr.trim()) {
|
||||
case '--x':
|
||||
return 'arrow_cross';
|
||||
case '--o':
|
||||
return 'arrow_circle';
|
||||
default:
|
||||
return 'arrow_point';
|
||||
}
|
||||
}
|
||||
|
||||
let cnt = 0;
|
||||
export const generateId = () => {
|
||||
cnt++;
|
||||
return 'id-' + Math.random().toString(36).substr(2, 12) + '-' + cnt;
|
||||
};
|
||||
|
||||
const setHierarchy = (block: Block[]): void => {
|
||||
rootBlock.children = block;
|
||||
populateBlockDatabase(block, rootBlock);
|
||||
blocks = rootBlock.children;
|
||||
};
|
||||
|
||||
const getColumns = (blockId: string): number => {
|
||||
const block = blockDatabase[blockId];
|
||||
if (!block) {
|
||||
return -1;
|
||||
}
|
||||
if (block.columns) {
|
||||
return block.columns;
|
||||
}
|
||||
if (!block.children) {
|
||||
return -1;
|
||||
}
|
||||
return block.children.length;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns all the blocks as a flat array
|
||||
* @returns
|
||||
*/
|
||||
const getBlocksFlat = () => {
|
||||
return [...Object.values(blockDatabase)];
|
||||
};
|
||||
/**
|
||||
* Returns the the hierarchy of blocks
|
||||
* @returns
|
||||
*/
|
||||
const getBlocks = () => {
|
||||
return blocks || [];
|
||||
};
|
||||
|
||||
const getEdges = () => {
|
||||
return edgeList;
|
||||
};
|
||||
const getBlock = (id: string) => {
|
||||
return blockDatabase[id];
|
||||
};
|
||||
|
||||
const setBlock = (block: Block) => {
|
||||
blockDatabase[block.id] = block;
|
||||
};
|
||||
|
||||
const getLogger = () => console;
|
||||
|
||||
/**
|
||||
* Return all of the style classes
|
||||
*/
|
||||
export const getClasses = function () {
|
||||
return classes;
|
||||
};
|
||||
|
||||
const db = {
|
||||
getConfig: () => configApi.getConfig().block,
|
||||
typeStr2Type: typeStr2Type,
|
||||
edgeTypeStr2Type: edgeTypeStr2Type,
|
||||
edgeStrToEdgeData,
|
||||
getLogger,
|
||||
getBlocksFlat,
|
||||
getBlocks,
|
||||
getEdges,
|
||||
setHierarchy,
|
||||
getBlock,
|
||||
setBlock,
|
||||
getColumns,
|
||||
getClasses,
|
||||
clear,
|
||||
generateId,
|
||||
} as const;
|
||||
|
||||
export type BlockDB = typeof db & DiagramDB;
|
||||
export default db;
|
||||
20
packages/mermaid/src/diagrams/block/blockDetector.ts
Normal file
20
packages/mermaid/src/diagrams/block/blockDetector.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-api/types.js';
|
||||
|
||||
const id = 'block';
|
||||
|
||||
const detector: DiagramDetector = (txt) => {
|
||||
return /^\s*block-beta/.test(txt);
|
||||
};
|
||||
|
||||
const loader = async () => {
|
||||
const { diagram } = await import('./blockDiagram.js');
|
||||
return { id, diagram };
|
||||
};
|
||||
|
||||
const plugin: ExternalDiagramDefinition = {
|
||||
id,
|
||||
detector,
|
||||
loader,
|
||||
};
|
||||
|
||||
export default plugin;
|
||||
13
packages/mermaid/src/diagrams/block/blockDiagram.ts
Normal file
13
packages/mermaid/src/diagrams/block/blockDiagram.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { DiagramDefinition } from '../../diagram-api/types.js';
|
||||
// @ts-ignore: jison doesn't export types
|
||||
import parser from './parser/block.jison';
|
||||
import db from './blockDB.js';
|
||||
import flowStyles from './styles.js';
|
||||
import renderer from './blockRenderer.js';
|
||||
|
||||
export const diagram: DiagramDefinition = {
|
||||
parser,
|
||||
db,
|
||||
renderer,
|
||||
styles: flowStyles,
|
||||
};
|
||||
96
packages/mermaid/src/diagrams/block/blockRenderer.ts
Normal file
96
packages/mermaid/src/diagrams/block/blockRenderer.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import type { Diagram } from '../../Diagram.js';
|
||||
import * as configApi from '../../config.js';
|
||||
import { calculateBlockSizes, insertBlocks, insertEdges } from './renderHelpers.js';
|
||||
import { layout } from './layout.js';
|
||||
import type { MermaidConfig, BaseDiagramConfig } from '../../config.type.js';
|
||||
import insertMarkers from '../../dagre-wrapper/markers.js';
|
||||
import {
|
||||
select as d3select,
|
||||
scaleOrdinal as d3scaleOrdinal,
|
||||
schemeTableau10 as d3schemeTableau10,
|
||||
} from 'd3';
|
||||
import type { ContainerElement } from 'd3';
|
||||
import { log } from '../../logger.js';
|
||||
import type { BlockDB } from './blockDB.js';
|
||||
import type { Block } from './blockTypes.js';
|
||||
import { configureSvgSize } from '../../setupGraphViewbox.js';
|
||||
|
||||
/**
|
||||
* Returns the all the styles from classDef statements in the graph definition.
|
||||
*
|
||||
* @param text - The text with the classes
|
||||
* @param diagObj - The diagram object
|
||||
* @returns ClassDef - The styles
|
||||
*/
|
||||
export const getClasses = function (text: any, diagObj: any) {
|
||||
return diagObj.db.getClasses();
|
||||
};
|
||||
|
||||
export const draw = async function (
|
||||
text: string,
|
||||
id: string,
|
||||
_version: string,
|
||||
diagObj: Diagram
|
||||
): Promise<void> {
|
||||
const { securityLevel, block: conf } = configApi.getConfig();
|
||||
const db = diagObj.db as BlockDB;
|
||||
let sandboxElement: any;
|
||||
if (securityLevel === 'sandbox') {
|
||||
sandboxElement = d3select('#i' + id);
|
||||
}
|
||||
const root =
|
||||
securityLevel === 'sandbox'
|
||||
? d3select<HTMLBodyElement, unknown>(sandboxElement.nodes()[0].contentDocument.body)
|
||||
: d3select<HTMLBodyElement, unknown>('body');
|
||||
|
||||
const svg =
|
||||
securityLevel === 'sandbox'
|
||||
? root.select<SVGSVGElement>(`[id="${id}"]`)
|
||||
: d3select<SVGSVGElement, unknown>(`[id="${id}"]`);
|
||||
|
||||
// Define the supported markers for the diagram
|
||||
const markers = ['point', 'circle', 'cross'];
|
||||
|
||||
// Add the marker definitions to the svg as marker tags
|
||||
// insertMarkers(svg, markers, diagObj.type, diagObj.arrowMarkerAbsolute);
|
||||
// insertMarkers(svg, markers, diagObj.type, true);
|
||||
insertMarkers(svg, markers, diagObj.type, id);
|
||||
|
||||
const bl = db.getBlocks();
|
||||
const blArr = db.getBlocksFlat();
|
||||
const edges = db.getEdges();
|
||||
|
||||
const nodes = svg.insert('g').attr('class', 'block');
|
||||
await calculateBlockSizes(nodes, bl, db);
|
||||
const bounds = layout(db);
|
||||
await insertBlocks(nodes, bl, db);
|
||||
await insertEdges(nodes, edges, blArr, db, id);
|
||||
|
||||
// log.debug('Here', bl);
|
||||
|
||||
// Establish svg dimensions and get width and height
|
||||
//
|
||||
// const bounds2 = nodes.node().getBoundingClientRect();
|
||||
// Why, oh why ????
|
||||
if (bounds) {
|
||||
const bounds2 = bounds;
|
||||
const magicFactor = Math.max(1, Math.round(0.125 * (bounds2.width / bounds2.height)));
|
||||
const height = bounds2.height + magicFactor + 10;
|
||||
const width = bounds2.width + 10;
|
||||
const { useMaxWidth } = conf as Exclude<MermaidConfig['block'], undefined>;
|
||||
configureSvgSize(svg, height, width, !!useMaxWidth);
|
||||
log.debug('Here Bounds', bounds, bounds2);
|
||||
svg.attr(
|
||||
'viewBox',
|
||||
`${bounds2.x - 5} ${bounds2.y - 5} ${bounds2.width + 10} ${bounds2.height + 10}`
|
||||
);
|
||||
}
|
||||
|
||||
// Get color scheme for the graph
|
||||
const colorScheme = d3scaleOrdinal(d3schemeTableau10);
|
||||
};
|
||||
|
||||
export default {
|
||||
draw,
|
||||
getClasses,
|
||||
};
|
||||
68
packages/mermaid/src/diagrams/block/blockTypes.ts
Normal file
68
packages/mermaid/src/diagrams/block/blockTypes.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
export type { BlockDiagramConfig as BlockConfig } from '../../config.type.js';
|
||||
|
||||
export type BlockType =
|
||||
| 'na'
|
||||
| 'column-setting'
|
||||
| 'edge'
|
||||
| 'round'
|
||||
| 'block_arrow'
|
||||
| 'space'
|
||||
| 'square'
|
||||
| 'diamond'
|
||||
| 'hexagon'
|
||||
| 'odd'
|
||||
| 'lean_right'
|
||||
| 'lean_left'
|
||||
| 'trapezoid'
|
||||
| 'inv_trapezoid'
|
||||
| 'rect_left_inv_arrow'
|
||||
| 'odd_right'
|
||||
| 'circle'
|
||||
| 'ellipse'
|
||||
| 'stadium'
|
||||
| 'subroutine'
|
||||
| 'cylinder'
|
||||
| 'group'
|
||||
| 'doublecircle'
|
||||
| 'classDef'
|
||||
| 'applyClass'
|
||||
| 'applyStyles'
|
||||
| 'composite';
|
||||
|
||||
export interface Block {
|
||||
// When the block have the type edge, the start and end are the id of the source and target blocks
|
||||
start?: string;
|
||||
end?: string;
|
||||
arrowTypeEnd?: string;
|
||||
arrowTypeStart?: string;
|
||||
width?: number;
|
||||
id: string;
|
||||
label?: string;
|
||||
intersect?: any;
|
||||
parent?: Block;
|
||||
type?: BlockType;
|
||||
children: Block[];
|
||||
size?: {
|
||||
width: number;
|
||||
height: number;
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
node?: any;
|
||||
columns?: number; // | TBlockColumnsDefaultValue;
|
||||
classes?: string[];
|
||||
directions?: string[];
|
||||
css?: string;
|
||||
styleClass?: string;
|
||||
styles?: string[];
|
||||
stylesStr?: string;
|
||||
widthInColumns?: number;
|
||||
}
|
||||
|
||||
export interface ClassDef {
|
||||
id: string;
|
||||
textStyles: string[];
|
||||
styles: string[];
|
||||
}
|
||||
|
||||
export type Direction = 'up' | 'down' | 'left' | 'right' | 'x' | 'y';
|
||||
8
packages/mermaid/src/diagrams/block/blockUtils.ts
Normal file
8
packages/mermaid/src/diagrams/block/blockUtils.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export const prepareTextForParsing = (text: string): string => {
|
||||
const textToParse = text
|
||||
.replaceAll(/^[^\S\n\r]+|[^\S\n\r]+$/g, '') // remove all trailing spaces for each row
|
||||
.replaceAll(/([\n\r])+/g, '\n') // remove empty lines duplicated
|
||||
.trim();
|
||||
|
||||
return textToParse;
|
||||
};
|
||||
12
packages/mermaid/src/diagrams/block/layout.spec.ts
Normal file
12
packages/mermaid/src/diagrams/block/layout.spec.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { calculateBlockPosition } from './layout.js';
|
||||
|
||||
describe('Layout', function () {
|
||||
it('should calculate position correctly', () => {
|
||||
expect(calculateBlockPosition(2, 0)).toEqual({ px: 0, py: 0 });
|
||||
expect(calculateBlockPosition(2, 1)).toEqual({ px: 1, py: 0 });
|
||||
expect(calculateBlockPosition(2, 2)).toEqual({ px: 0, py: 1 });
|
||||
expect(calculateBlockPosition(2, 3)).toEqual({ px: 1, py: 1 });
|
||||
expect(calculateBlockPosition(2, 4)).toEqual({ px: 0, py: 2 });
|
||||
expect(calculateBlockPosition(1, 3)).toEqual({ px: 0, py: 3 });
|
||||
});
|
||||
});
|
||||
329
packages/mermaid/src/diagrams/block/layout.ts
Normal file
329
packages/mermaid/src/diagrams/block/layout.ts
Normal file
@@ -0,0 +1,329 @@
|
||||
import type { BlockDB } from './blockDB.js';
|
||||
import type { Block } from './blockTypes.js';
|
||||
import { log } from '../../logger.js';
|
||||
import { getConfig } from '../../diagram-api/diagramAPI.js';
|
||||
const padding = getConfig()?.block?.padding || 8;
|
||||
|
||||
interface BlockPosition {
|
||||
px: number;
|
||||
py: number;
|
||||
}
|
||||
|
||||
export function calculateBlockPosition(columns: number, position: number): BlockPosition {
|
||||
// log.debug('calculateBlockPosition abc89', columns, position);
|
||||
// Ensure that columns is a positive integer
|
||||
if (columns === 0 || !Number.isInteger(columns)) {
|
||||
throw new Error('Columns must be an integer !== 0.');
|
||||
}
|
||||
|
||||
// Ensure that position is a non-negative integer
|
||||
if (position < 0 || !Number.isInteger(position)) {
|
||||
throw new Error('Position must be a non-negative integer.' + position);
|
||||
}
|
||||
|
||||
if (columns < 0) {
|
||||
// Auto columns is set
|
||||
return { px: position, py: 0 };
|
||||
}
|
||||
if (columns === 1) {
|
||||
// Auto columns is set
|
||||
return { px: 0, py: position };
|
||||
}
|
||||
// Calculate posX and posY
|
||||
const px = position % columns;
|
||||
const py = Math.floor(position / columns);
|
||||
// log.debug('calculateBlockPosition abc89', columns, position, '=> (', px, py, ')');
|
||||
return { px, py };
|
||||
}
|
||||
|
||||
const getMaxChildSize = (block: Block) => {
|
||||
let maxWidth = 0;
|
||||
let maxHeight = 0;
|
||||
// find max width of children
|
||||
// log.debug('getMaxChildSize abc95 (start) parent:', block.id);
|
||||
for (const child of block.children) {
|
||||
const { width, height, x, y } = child.size || { width: 0, height: 0, x: 0, y: 0 };
|
||||
log.debug(
|
||||
'getMaxChildSize abc95 child:',
|
||||
child.id,
|
||||
'width:',
|
||||
width,
|
||||
'height:',
|
||||
height,
|
||||
'x:',
|
||||
x,
|
||||
'y:',
|
||||
y,
|
||||
child.type
|
||||
);
|
||||
if (child.type === 'space') {
|
||||
continue;
|
||||
}
|
||||
if (width > maxWidth) {
|
||||
maxWidth = width / (block.widthInColumns || 1);
|
||||
}
|
||||
if (height > maxHeight) {
|
||||
maxHeight = height;
|
||||
}
|
||||
}
|
||||
return { width: maxWidth, height: maxHeight };
|
||||
};
|
||||
|
||||
function setBlockSizes(block: Block, db: BlockDB, siblingWidth = 0, siblingHeight = 0) {
|
||||
log.debug(
|
||||
'setBlockSizes abc95 (start)',
|
||||
block.id,
|
||||
block?.size?.x,
|
||||
'block width =',
|
||||
block?.size,
|
||||
'sieblingWidth',
|
||||
siblingWidth
|
||||
);
|
||||
if (!block?.size?.width) {
|
||||
block.size = {
|
||||
width: siblingWidth,
|
||||
height: siblingHeight,
|
||||
x: 0,
|
||||
y: 0,
|
||||
};
|
||||
}
|
||||
let maxWidth = 0;
|
||||
let maxHeight = 0;
|
||||
|
||||
if (block.children?.length > 0) {
|
||||
for (const child of block.children) {
|
||||
setBlockSizes(child, db);
|
||||
}
|
||||
// find max width of children
|
||||
const childSize = getMaxChildSize(block);
|
||||
maxWidth = childSize.width;
|
||||
maxHeight = childSize.height;
|
||||
log.debug('setBlockSizes abc95 maxWidth of', block.id, ':s children is ', maxWidth, maxHeight);
|
||||
|
||||
// set width of block to max width of children
|
||||
for (const child of block.children) {
|
||||
if (child.size) {
|
||||
log.debug(
|
||||
`abc95 Setting size of children of ${block.id} id=${child.id} ${maxWidth} ${maxHeight} ${child.size}`
|
||||
);
|
||||
child.size.width =
|
||||
maxWidth * (child.widthInColumns || 1) + padding * ((child.widthInColumns || 1) - 1);
|
||||
child.size.height = maxHeight;
|
||||
child.size.x = 0;
|
||||
child.size.y = 0;
|
||||
|
||||
log.debug(
|
||||
`abc95 updating size of ${block.id} children child:${child.id} maxWidth:${maxWidth} maxHeight:${maxHeight}`
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const child of block.children) {
|
||||
setBlockSizes(child, db, maxWidth, maxHeight);
|
||||
}
|
||||
|
||||
const columns = block.columns || -1;
|
||||
let numItems = 0;
|
||||
for (const child of block.children) {
|
||||
numItems += child.widthInColumns || 1;
|
||||
}
|
||||
|
||||
// The width and height in number blocks
|
||||
let xSize = block.children.length;
|
||||
if (columns > 0 && columns < numItems) {
|
||||
xSize = columns;
|
||||
}
|
||||
|
||||
const w = block.widthInColumns || 1;
|
||||
|
||||
const ySize = Math.ceil(numItems / xSize);
|
||||
|
||||
let width = xSize * (maxWidth + padding) + padding;
|
||||
let height = ySize * (maxHeight + padding) + padding;
|
||||
// If maxWidth
|
||||
if (width < siblingWidth) {
|
||||
log.debug(
|
||||
`Detected to small siebling: abc95 ${block.id} sieblingWidth ${siblingWidth} sieblingHeight ${siblingHeight} width ${width}`
|
||||
);
|
||||
width = siblingWidth;
|
||||
height = siblingHeight;
|
||||
const childWidth = (siblingWidth - xSize * padding - padding) / xSize;
|
||||
const childHeight = (siblingHeight - ySize * padding - padding) / ySize;
|
||||
// cspell:ignore indata
|
||||
log.debug('Size indata abc88', block.id, 'childWidth', childWidth, 'maxWidth', maxWidth);
|
||||
log.debug('Size indata abc88', block.id, 'childHeight', childHeight, 'maxHeight', maxHeight);
|
||||
log.debug('Size indata abc88 xSize', xSize, 'padding', padding);
|
||||
|
||||
// set width of block to max width of children
|
||||
for (const child of block.children) {
|
||||
if (child.size) {
|
||||
child.size.width = childWidth;
|
||||
child.size.height = childHeight;
|
||||
child.size.x = 0;
|
||||
child.size.y = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.debug(
|
||||
`abc95 (finale calc) ${block.id} xSize ${xSize} ySize ${ySize} columns ${columns}${
|
||||
block.children.length
|
||||
} width=${Math.max(width, block.size?.width || 0)}`
|
||||
);
|
||||
if (width < (block?.size?.width || 0)) {
|
||||
width = block?.size?.width || 0;
|
||||
|
||||
// Grow children to fit
|
||||
const num = columns > 0 ? Math.min(block.children.length, columns) : block.children.length;
|
||||
if (num > 0) {
|
||||
const childWidth = (width - num * padding - padding) / num;
|
||||
log.debug('abc95 (growing to fit) width', block.id, width, block.size?.width, childWidth);
|
||||
for (const child of block.children) {
|
||||
if (child.size) {
|
||||
child.size.width = childWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
block.size = {
|
||||
width,
|
||||
height,
|
||||
x: 0,
|
||||
y: 0,
|
||||
};
|
||||
}
|
||||
|
||||
log.debug(
|
||||
'setBlockSizes abc94 (done)',
|
||||
block.id,
|
||||
block?.size?.x,
|
||||
block?.size?.width,
|
||||
block?.size?.y,
|
||||
block?.size?.height
|
||||
);
|
||||
}
|
||||
|
||||
function layoutBlocks(block: Block, db: BlockDB) {
|
||||
log.debug(
|
||||
`abc85 layout blocks (=>layoutBlocks) ${block.id} x: ${block?.size?.x} y: ${block?.size?.y} width: ${block?.size?.width}`
|
||||
);
|
||||
const columns = block.columns || -1;
|
||||
log.debug('layoutBlocks columns abc95', block.id, '=>', columns, block);
|
||||
if (
|
||||
block.children && // find max width of children
|
||||
block.children.length > 0
|
||||
) {
|
||||
const width = block?.children[0]?.size?.width || 0;
|
||||
const widthOfChildren = block.children.length * width + (block.children.length - 1) * padding;
|
||||
|
||||
log.debug('widthOfChildren 88', widthOfChildren, 'posX');
|
||||
|
||||
// let first = true;
|
||||
let columnPos = 0;
|
||||
log.debug('abc91 block?.size?.x', block.id, block?.size?.x);
|
||||
let startingPosX = block?.size?.x ? block?.size?.x + (-block?.size?.width / 2 || 0) : -padding;
|
||||
let rowPos = 0;
|
||||
for (const child of block.children) {
|
||||
const parent = block;
|
||||
|
||||
if (!child.size) {
|
||||
continue;
|
||||
}
|
||||
const { width, height } = child.size;
|
||||
const { px, py } = calculateBlockPosition(columns, columnPos);
|
||||
if (py != rowPos) {
|
||||
rowPos = py;
|
||||
startingPosX = block?.size?.x ? block?.size?.x + (-block?.size?.width / 2 || 0) : -padding;
|
||||
log.debug('New row in layout for block', block.id, ' and child ', child.id, rowPos);
|
||||
}
|
||||
log.debug(
|
||||
`abc89 layout blocks (child) id: ${child.id} Pos: ${columnPos} (px, py) ${px},${py} (${parent?.size?.x},${parent?.size?.y}) parent: ${parent.id} width: ${width}${padding}`
|
||||
);
|
||||
if (parent.size) {
|
||||
const halfWidth = width / 2;
|
||||
child.size.x = startingPosX + padding + halfWidth;
|
||||
|
||||
// cspell:ignore pyid
|
||||
log.debug(
|
||||
`abc91 layout blocks (calc) px, pyid:${
|
||||
child.id
|
||||
} startingPos=X${startingPosX} new startingPosX${
|
||||
child.size.x
|
||||
} ${halfWidth} padding=${padding} width=${width} halfWidth=${halfWidth} => x:${
|
||||
child.size.x
|
||||
} y:${child.size.y} ${child.widthInColumns} (width * (child?.w || 1)) / 2 ${
|
||||
(width * (child?.widthInColumns || 1)) / 2
|
||||
}`
|
||||
);
|
||||
|
||||
startingPosX = child.size.x + halfWidth;
|
||||
|
||||
child.size.y =
|
||||
parent.size.y - parent.size.height / 2 + py * (height + padding) + height / 2 + padding;
|
||||
|
||||
log.debug(
|
||||
`abc88 layout blocks (calc) px, pyid:${
|
||||
child.id
|
||||
}startingPosX${startingPosX}${padding}${halfWidth}=>x:${child.size.x}y:${child.size.y}${
|
||||
child.widthInColumns
|
||||
}(width * (child?.w || 1)) / 2${(width * (child?.widthInColumns || 1)) / 2}`
|
||||
);
|
||||
}
|
||||
|
||||
// posY += height + padding;
|
||||
if (child.children) {
|
||||
layoutBlocks(child, db);
|
||||
}
|
||||
columnPos += child?.widthInColumns || 1;
|
||||
log.debug('abc88 columnsPos', child, columnPos);
|
||||
}
|
||||
}
|
||||
log.debug(
|
||||
`layout blocks (<==layoutBlocks) ${block.id} x: ${block?.size?.x} y: ${block?.size?.y} width: ${block?.size?.width}`
|
||||
);
|
||||
}
|
||||
|
||||
function findBounds(
|
||||
block: Block,
|
||||
{ minX, minY, maxX, maxY } = { minX: 0, minY: 0, maxX: 0, maxY: 0 }
|
||||
) {
|
||||
if (block.size && block.id !== 'root') {
|
||||
const { x, y, width, height } = block.size;
|
||||
if (x - width / 2 < minX) {
|
||||
minX = x - width / 2;
|
||||
}
|
||||
if (y - height / 2 < minY) {
|
||||
minY = y - height / 2;
|
||||
}
|
||||
if (x + width / 2 > maxX) {
|
||||
maxX = x + width / 2;
|
||||
}
|
||||
if (y + height / 2 > maxY) {
|
||||
maxY = y + height / 2;
|
||||
}
|
||||
}
|
||||
if (block.children) {
|
||||
for (const child of block.children) {
|
||||
({ minX, minY, maxX, maxY } = findBounds(child, { minX, minY, maxX, maxY }));
|
||||
}
|
||||
}
|
||||
return { minX, minY, maxX, maxY };
|
||||
}
|
||||
|
||||
export function layout(db: BlockDB) {
|
||||
const root = db.getBlock('root');
|
||||
if (!root) {
|
||||
return;
|
||||
}
|
||||
|
||||
setBlockSizes(root, db, 0, 0);
|
||||
layoutBlocks(root, db);
|
||||
// Position blocks relative to parents
|
||||
// positionBlock(root, root, db);
|
||||
log.debug('getBlocks', JSON.stringify(root, null, 2));
|
||||
|
||||
const { minX, minY, maxX, maxY } = findBounds(root);
|
||||
|
||||
const height = maxY - minY;
|
||||
const width = maxX - minX;
|
||||
return { x: minX, y: minY, width, height };
|
||||
}
|
||||
290
packages/mermaid/src/diagrams/block/parser/block.jison
Normal file
290
packages/mermaid/src/diagrams/block/parser/block.jison
Normal file
@@ -0,0 +1,290 @@
|
||||
/** mermaid */
|
||||
|
||||
//---------------------------------------------------------
|
||||
// We support csv format as defined here:
|
||||
// https://www.ietf.org/rfc/rfc4180.txt
|
||||
// There are some minor changes for compliance with jison
|
||||
// We also parse only 3 columns: source,target,value
|
||||
// And allow blank lines for visual purposes
|
||||
//---------------------------------------------------------
|
||||
|
||||
%lex
|
||||
%x acc_title
|
||||
%x acc_descr
|
||||
%x acc_descr_multiline
|
||||
%x string
|
||||
%x space
|
||||
%x md_string
|
||||
%x NODE
|
||||
%x BLOCK_ARROW
|
||||
%x ARROW_DIR
|
||||
%x LLABEL
|
||||
%x CLASS
|
||||
%x CLASS_STYLE
|
||||
%x CLASSDEF
|
||||
%x CLASSDEFID
|
||||
%x STYLE_STMNT
|
||||
%x STYLE_DEFINITION
|
||||
|
||||
|
||||
// as per section 6.1 of RFC 2234 [2]
|
||||
COMMA \u002C
|
||||
CR \u000D
|
||||
LF \u000A
|
||||
CRLF \u000D\u000A
|
||||
|
||||
|
||||
%%
|
||||
|
||||
"block-beta" { return 'BLOCK_DIAGRAM_KEY'; }
|
||||
"block"\s+ { yy.getLogger().debug('Found space-block'); return 'block';}
|
||||
"block"\n+ { yy.getLogger().debug('Found nl-block'); return 'block';}
|
||||
"block:" { yy.getLogger().debug('Found space-block'); return 'id-block';}
|
||||
// \s*\%\%.* { yy.getLogger().debug('Found comment',yytext); }
|
||||
[\s]+ { yy.getLogger().debug('.', yytext); /* skip all whitespace */ }
|
||||
[\n]+ {yy.getLogger().debug('_', yytext); /* skip all whitespace */ }
|
||||
// [\n] return 'NL';
|
||||
<INITIAL>({CRLF}|{LF}) { return 'NL' }
|
||||
"columns"\s+"auto" { yytext=-1; return 'COLUMNS'; }
|
||||
"columns"\s+[\d]+ { yytext = yytext.replace(/columns\s+/,''); yy.getLogger().debug('COLUMNS (LEX)', yytext); return 'COLUMNS'; }
|
||||
["][`] { this.pushState("md_string");}
|
||||
<md_string>[^`"]+ { return "MD_STR";}
|
||||
<md_string>[`]["] { this.popState();}
|
||||
["] this.pushState("string");
|
||||
<string>["] { yy.getLogger().debug('LEX: POPPING STR:', yytext);this.popState();}
|
||||
<string>[^"]* { yy.getLogger().debug('LEX: STR end:', yytext); return "STR";}
|
||||
space[:]\d+ { yytext = yytext.replace(/space\:/,'');yy.getLogger().debug('SPACE NUM (LEX)', yytext); return 'SPACE_BLOCK'; }
|
||||
space { yytext = '1'; yy.getLogger().debug('COLUMNS (LEX)', yytext); return 'SPACE_BLOCK'; }
|
||||
"default" return 'DEFAULT';
|
||||
"linkStyle" return 'LINKSTYLE';
|
||||
"interpolate" return 'INTERPOLATE';
|
||||
|
||||
"classDef"\s+ { this.pushState('CLASSDEF'); return 'classDef'; }
|
||||
<CLASSDEF>DEFAULT\s+ { this.popState(); this.pushState('CLASSDEFID'); return 'DEFAULT_CLASSDEF_ID' }
|
||||
<CLASSDEF>\w+\s+ { this.popState(); this.pushState('CLASSDEFID'); return 'CLASSDEF_ID' }
|
||||
<CLASSDEFID>[^\n]* { this.popState(); return 'CLASSDEF_STYLEOPTS' }
|
||||
|
||||
"class"\s+ { this.pushState('CLASS'); return 'class'; }
|
||||
<CLASS>(\w+)+((","\s*\w+)*) { this.popState(); this.pushState('CLASS_STYLE'); return 'CLASSENTITY_IDS' }
|
||||
<CLASS_STYLE>[^\n]* { this.popState(); return 'STYLECLASS' }
|
||||
|
||||
"style"\s+ { this.pushState('STYLE_STMNT'); return 'style'; }
|
||||
<STYLE_STMNT>(\w+)+((","\s*\w+)*) { this.popState(); this.pushState('STYLE_DEFINITION'); return 'STYLE_ENTITY_IDS' }
|
||||
<STYLE_DEFINITION>[^\n]* { this.popState(); return 'STYLE_DEFINITION_DATA' }
|
||||
|
||||
accTitle\s*":"\s* { this.pushState("acc_title");return 'acc_title'; }
|
||||
<acc_title>(?!\n|;|#)*[^\n]* { this.popState(); return "acc_title_value"; }
|
||||
accDescr\s*":"\s* { this.pushState("acc_descr");return 'acc_descr'; }
|
||||
<acc_descr>(?!\n|;|#)*[^\n]* { this.popState(); return "acc_descr_value"; }
|
||||
accDescr\s*"{"\s* { this.pushState("acc_descr_multiline");}
|
||||
<acc_descr_multiline>[\}] { this.popState(); }
|
||||
<acc_descr_multiline>[^\}]* return "acc_descr_multiline_value";
|
||||
"end"\b\s* return 'end';
|
||||
|
||||
// Node end of shape
|
||||
<NODE>"(((" { this.popState();yy.getLogger().debug('Lex: (('); return "NODE_DEND"; }
|
||||
<NODE>")))" { this.popState();yy.getLogger().debug('Lex: (('); return "NODE_DEND"; }
|
||||
<NODE>[\)]\) { this.popState();yy.getLogger().debug('Lex: ))'); return "NODE_DEND"; }
|
||||
<NODE>"}}" { this.popState();yy.getLogger().debug('Lex: (('); return "NODE_DEND"; }
|
||||
<NODE>"}" { this.popState();yy.getLogger().debug('Lex: (('); return "NODE_DEND"; }
|
||||
<NODE>"(-" { this.popState();yy.getLogger().debug('Lex: (-'); return "NODE_DEND"; }
|
||||
<NODE>"-)" { this.popState();yy.getLogger().debug('Lex: -)'); return "NODE_DEND"; }
|
||||
<NODE>"((" { this.popState();yy.getLogger().debug('Lex: (('); return "NODE_DEND"; }
|
||||
<NODE>"]]" { this.popState();yy.getLogger().debug('Lex: ]]'); return "NODE_DEND"; }
|
||||
<NODE>"(" { this.popState();yy.getLogger().debug('Lex: ('); return "NODE_DEND"; }
|
||||
<NODE>"])" { this.popState();yy.getLogger().debug('Lex: ])'); return "NODE_DEND"; }
|
||||
<NODE>"\\]" { this.popState();yy.getLogger().debug('Lex: /]'); return "NODE_DEND"; }
|
||||
<NODE>"/]" { this.popState();yy.getLogger().debug('Lex: /]'); return "NODE_DEND"; }
|
||||
<NODE>")]" { this.popState();yy.getLogger().debug('Lex: )]'); return "NODE_DEND"; }
|
||||
<NODE>[\)] { this.popState();yy.getLogger().debug('Lex: )'); return "NODE_DEND"; }
|
||||
<NODE>\]\> { this.popState();yy.getLogger().debug('Lex: ]>'); return "NODE_DEND"; }
|
||||
<NODE>[\]] { this.popState();yy.getLogger().debug('Lex: ]'); return "NODE_DEND"; }
|
||||
|
||||
// Start of nodes with shapes and description
|
||||
"-)" { yy.getLogger().debug('Lexa: -)'); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"(-" { yy.getLogger().debug('Lexa: (-'); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"))" { yy.getLogger().debug('Lexa: ))'); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
")" { yy.getLogger().debug('Lexa: )'); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"(((" { yy.getLogger().debug('Lex: ((('); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"((" { yy.getLogger().debug('Lexa: )'); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"{{" { yy.getLogger().debug('Lexa: )'); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"{" { yy.getLogger().debug('Lexa: )'); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
">" { yy.getLogger().debug('Lexc: >'); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"([" { yy.getLogger().debug('Lexa: (['); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"(" { yy.getLogger().debug('Lexa: )'); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"[[" { this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"[|" { this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"[(" { this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
")))" { this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"[\\" { this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"[/" { this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"[\\" { this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
"[" { yy.getLogger().debug('Lexa: ['); this.pushState('NODE');return 'NODE_DSTART'; }
|
||||
|
||||
"<[" { this.pushState('BLOCK_ARROW');yy.getLogger().debug('LEX ARR START');return 'BLOCK_ARROW_START'; }
|
||||
|
||||
[^\(\[\n\-\)\{\}\s\<\>:]+ { yy.getLogger().debug('Lex: NODE_ID', yytext);return 'NODE_ID'; }
|
||||
<<EOF>> { yy.getLogger().debug('Lex: EOF', yytext);return 'EOF'; }
|
||||
|
||||
// Handling of strings in node
|
||||
<BLOCK_ARROW>["][`] { this.pushState("md_string");}
|
||||
<NODE>["][`] { this.pushState("md_string");}
|
||||
<md_string>[^`"]+ { return "NODE_DESCR";}
|
||||
<md_string>[`]["] { this.popState();}
|
||||
<NODE>["] { yy.getLogger().debug('Lex: Starting string');this.pushState("string");}
|
||||
<BLOCK_ARROW>["] { yy.getLogger().debug('LEX ARR: Starting string');this.pushState("string");}
|
||||
<string>[^"]+ { yy.getLogger().debug('LEX: NODE_DESCR:', yytext); return "NODE_DESCR";}
|
||||
<string>["] {yy.getLogger().debug('LEX POPPING');this.popState();}
|
||||
|
||||
<BLOCK_ARROW>"]>"\s*"(" { yy.getLogger().debug('Lex: =>BAE'); this.pushState('ARROW_DIR'); }
|
||||
<ARROW_DIR>","?\s*right\s* { yytext = yytext.replace(/^,\s*/, ''); yy.getLogger().debug('Lex (right): dir:',yytext);return "DIR"; }
|
||||
<ARROW_DIR>","?\s*left\s* { yytext = yytext.replace(/^,\s*/, ''); yy.getLogger().debug('Lex (left):',yytext);return "DIR"; }
|
||||
<ARROW_DIR>","?\s*x\s* { yytext = yytext.replace(/^,\s*/, ''); yy.getLogger().debug('Lex (x):',yytext); return "DIR"; }
|
||||
<ARROW_DIR>","?\s*y\s* { yytext = yytext.replace(/^,\s*/, ''); yy.getLogger().debug('Lex (y):',yytext); return "DIR"; }
|
||||
<ARROW_DIR>","?\s*up\s* { yytext = yytext.replace(/^,\s*/, ''); yy.getLogger().debug('Lex (up):',yytext); return "DIR"; }
|
||||
<ARROW_DIR>","?\s*down\s* { yytext = yytext.replace(/^,\s*/, ''); yy.getLogger().debug('Lex (down):',yytext); return "DIR"; }
|
||||
<ARROW_DIR>")"\s* { yytext=']>';yy.getLogger().debug('Lex (ARROW_DIR end):',yytext);this.popState();this.popState();return "BLOCK_ARROW_END"; }
|
||||
|
||||
// Edges
|
||||
\s*[xo<]?\-\-+[-xo>]\s* { yy.getLogger().debug('Lex: LINK', '#'+yytext+'#'); return 'LINK'; }
|
||||
\s*[xo<]?\=\=+[=xo>]\s* { yy.getLogger().debug('Lex: LINK', yytext); return 'LINK'; }
|
||||
\s*[xo<]?\-?\.+\-[xo>]?\s* { yy.getLogger().debug('Lex: LINK', yytext); return 'LINK'; }
|
||||
\s*\~\~[\~]+\s* { yy.getLogger().debug('Lex: LINK', yytext); return 'LINK'; }
|
||||
\s*[xo<]?\-\-\s* { yy.getLogger().debug('Lex: START_LINK', yytext);this.pushState("LLABEL");return 'START_LINK'; }
|
||||
\s*[xo<]?\=\=\s* { yy.getLogger().debug('Lex: START_LINK', yytext);this.pushState("LLABEL");return 'START_LINK'; }
|
||||
\s*[xo<]?\-\.\s* { yy.getLogger().debug('Lex: START_LINK', yytext);this.pushState("LLABEL");return 'START_LINK'; }
|
||||
<LLABEL>["][`] { this.pushState("md_string");}
|
||||
<LLABEL>["] { yy.getLogger().debug('Lex: Starting string');this.pushState("string"); return "LINK_LABEL";}
|
||||
<LLABEL>\s*[xo<]?\-\-+[-xo>]\s* { this.popState(); yy.getLogger().debug('Lex: LINK', '#'+yytext+'#'); return 'LINK'; }
|
||||
<LLABEL>\s*[xo<]?\=\=+[=xo>]\s* { this.popState(); yy.getLogger().debug('Lex: LINK', yytext); return 'LINK'; }
|
||||
<LLABEL>\s*[xo<]?\-?\.+\-[xo>]?\s* { this.popState(); yy.getLogger().debug('Lex: LINK', yytext); return 'LINK'; }
|
||||
':'\d+ { yy.getLogger().debug('Lex: COLON', yytext); yytext=yytext.slice(1);return 'SIZE'; }
|
||||
|
||||
/lex
|
||||
|
||||
%left '^'
|
||||
%start start
|
||||
|
||||
%% // language grammar
|
||||
|
||||
spaceLines
|
||||
: SPACELINE
|
||||
| spaceLines SPACELINE
|
||||
| spaceLines NL
|
||||
;
|
||||
|
||||
separator
|
||||
: NL
|
||||
{yy.getLogger().debug('Rule: separator (NL) ');}
|
||||
| SPACE
|
||||
{yy.getLogger().debug('Rule: separator (Space) ');}
|
||||
| EOF
|
||||
{yy.getLogger().debug('Rule: separator (EOF) ');}
|
||||
;
|
||||
|
||||
start: BLOCK_DIAGRAM_KEY document EOF
|
||||
{ yy.getLogger().debug("Rule: hierarchy: ", $2); yy.setHierarchy($2); }
|
||||
;
|
||||
|
||||
|
||||
stop
|
||||
: NL {yy.getLogger().debug('Stop NL ');}
|
||||
| EOF {yy.getLogger().debug('Stop EOF ');}
|
||||
// | SPACELINE
|
||||
| stop NL {yy.getLogger().debug('Stop NL2 ');}
|
||||
| stop EOF {yy.getLogger().debug('Stop EOF2 ');}
|
||||
;
|
||||
|
||||
//array of statements
|
||||
document
|
||||
: statement { yy.getLogger().debug("Rule: statement: ", $1); typeof $1.length === 'number'?$$ = $1:$$ = [$1]; }
|
||||
| statement document { yy.getLogger().debug("Rule: statement #2: ", $1); $$ = [$1].concat($2); }
|
||||
;
|
||||
|
||||
link
|
||||
: LINK
|
||||
{ yy.getLogger().debug("Rule: link: ", $1, yytext); $$={edgeTypeStr: $1, label:''}; }
|
||||
| START_LINK LINK_LABEL STR LINK
|
||||
{ yy.getLogger().debug("Rule: LABEL link: ", $1, $3, $4); $$={edgeTypeStr: $4, label:$3}; }
|
||||
;
|
||||
|
||||
statement
|
||||
: nodeStatement
|
||||
| columnsStatement
|
||||
| SPACE_BLOCK
|
||||
{ const num=parseInt($1); const spaceId = yy.generateId(); $$ = { id: spaceId, type:'space', label:'', width: num, children: [] }}
|
||||
| blockStatement
|
||||
| classDefStatement
|
||||
| cssClassStatement
|
||||
| styleStatement
|
||||
;
|
||||
|
||||
nodeStatement
|
||||
: nodeStatement link node {
|
||||
yy.getLogger().debug('Rule: (nodeStatement link node) ', $1, $2, $3, ' typestr: ',$2.edgeTypeStr);
|
||||
const edgeData = yy.edgeStrToEdgeData($2.edgeTypeStr)
|
||||
$$ = [
|
||||
{id: $1.id, label: $1.label, type:$1.type, directions: $1.directions},
|
||||
{id: $1.id + '-' + $3.id, start: $1.id, end: $3.id, label: $2.label, type: 'edge', directions: $3.directions, arrowTypeEnd: edgeData, arrowTypeStart: 'arrow_open' },
|
||||
{id: $3.id, label: $3.label, type: yy.typeStr2Type($3.typeStr), directions: $3.directions}
|
||||
];
|
||||
}
|
||||
| node SIZE { yy.getLogger().debug('Rule: nodeStatement (abc88 node size) ', $1, $2); $$ = {id: $1.id, label: $1.label, type: yy.typeStr2Type($1.typeStr), directions: $1.directions, widthInColumns: parseInt($2,10)}; }
|
||||
| node { yy.getLogger().debug('Rule: nodeStatement (node) ', $1); $$ = {id: $1.id, label: $1.label, type: yy.typeStr2Type($1.typeStr), directions: $1.directions, widthInColumns:1}; }
|
||||
;
|
||||
|
||||
|
||||
columnsStatement
|
||||
: COLUMNS { yy.getLogger().debug('APA123', this? this:'na'); yy.getLogger().debug("COLUMNS: ", $1); $$ = {type: 'column-setting', columns: $1 === 'auto'?-1:parseInt($1) } }
|
||||
;
|
||||
|
||||
blockStatement
|
||||
: id-block nodeStatement document end { yy.getLogger().debug('Rule: id-block statement : ', $2, $3); const id2 = yy.generateId(); $$ = { ...$2, type:'composite', children: $3 }; }
|
||||
| block document end { yy.getLogger().debug('Rule: blockStatement : ', $1, $2, $3); const id = yy.generateId(); $$ = { id, type:'composite', label:'', children: $2 }; }
|
||||
;
|
||||
|
||||
node
|
||||
: NODE_ID
|
||||
{ yy.getLogger().debug("Rule: node (NODE_ID separator): ", $1); $$ = { id: $1 }; }
|
||||
| NODE_ID nodeShapeNLabel
|
||||
{
|
||||
yy.getLogger().debug("Rule: node (NODE_ID nodeShapeNLabel separator): ", $1, $2);
|
||||
$$ = { id: $1, label: $2.label, typeStr: $2.typeStr, directions: $2.directions };
|
||||
}
|
||||
;
|
||||
|
||||
dirList: DIR { yy.getLogger().debug("Rule: dirList: ", $1); $$ = [$1]; }
|
||||
| DIR dirList { yy.getLogger().debug("Rule: dirList: ", $1, $2); $$ = [$1].concat($2); }
|
||||
;
|
||||
|
||||
nodeShapeNLabel
|
||||
: NODE_DSTART STR NODE_DEND
|
||||
{ yy.getLogger().debug("Rule: nodeShapeNLabel: ", $1, $2, $3); $$ = { typeStr: $1 + $3, label: $2 }; }
|
||||
| BLOCK_ARROW_START STR dirList BLOCK_ARROW_END
|
||||
{ yy.getLogger().debug("Rule: BLOCK_ARROW nodeShapeNLabel: ", $1, $2, " #3:",$3, $4); $$ = { typeStr: $1 + $4, label: $2, directions: $3}; }
|
||||
;
|
||||
|
||||
|
||||
classDefStatement
|
||||
: classDef CLASSDEF_ID CLASSDEF_STYLEOPTS {
|
||||
$$ = { type: 'classDef', id: $2.trim(), css: $3.trim() };
|
||||
}
|
||||
| classDef DEFAULT CLASSDEF_STYLEOPTS {
|
||||
$$ = { type: 'classDef', id: $2.trim(), css: $3.trim() };
|
||||
}
|
||||
;
|
||||
|
||||
cssClassStatement
|
||||
: class CLASSENTITY_IDS STYLECLASS {
|
||||
//log.debug('apply class: id(s): ',$2, ' style class: ', $3);
|
||||
$$={ type: 'applyClass', id: $2.trim(), styleClass: $3.trim() };
|
||||
}
|
||||
;
|
||||
|
||||
styleStatement
|
||||
: style STYLE_ENTITY_IDS STYLE_DEFINITION_DATA {
|
||||
$$={ type: 'applyStyles', id: $2.trim(), stylesStr: $3.trim() };
|
||||
}
|
||||
;
|
||||
|
||||
%%
|
||||
409
packages/mermaid/src/diagrams/block/parser/block.spec.ts
Normal file
409
packages/mermaid/src/diagrams/block/parser/block.spec.ts
Normal file
@@ -0,0 +1,409 @@
|
||||
// @ts-ignore: jison doesn't export types
|
||||
import block from './block.jison';
|
||||
import db from '../blockDB.js';
|
||||
import { cleanupComments } from '../../../diagram-api/comments.js';
|
||||
import { prepareTextForParsing } from '../blockUtils.js';
|
||||
import { setConfig } from '../../../config.js';
|
||||
|
||||
describe('Block diagram', function () {
|
||||
describe('when parsing an block diagram graph it should handle > ', function () {
|
||||
beforeEach(function () {
|
||||
block.parser.yy = db;
|
||||
block.parser.yy.clear();
|
||||
block.parser.yy.getLogger = () => console;
|
||||
});
|
||||
|
||||
it('a diagram with a node', async () => {
|
||||
const str = `block-beta
|
||||
id
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(1);
|
||||
expect(blocks[0].id).toBe('id');
|
||||
expect(blocks[0].label).toBe('id');
|
||||
});
|
||||
it('a node with a square shape and a label', async () => {
|
||||
const str = `block-beta
|
||||
id["A label"]
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(1);
|
||||
expect(blocks[0].id).toBe('id');
|
||||
expect(blocks[0].label).toBe('A label');
|
||||
expect(blocks[0].type).toBe('square');
|
||||
});
|
||||
it('a diagram with multiple nodes', async () => {
|
||||
const str = `block-beta
|
||||
id1
|
||||
id2
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
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('na');
|
||||
expect(blocks[1].id).toBe('id2');
|
||||
expect(blocks[1].label).toBe('id2');
|
||||
expect(blocks[1].type).toBe('na');
|
||||
});
|
||||
it('a diagram with multiple nodes', async () => {
|
||||
const str = `block-beta
|
||||
id1
|
||||
id2
|
||||
id3
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
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('na');
|
||||
expect(blocks[1].id).toBe('id2');
|
||||
expect(blocks[1].label).toBe('id2');
|
||||
expect(blocks[1].type).toBe('na');
|
||||
expect(blocks[2].id).toBe('id3');
|
||||
expect(blocks[2].label).toBe('id3');
|
||||
expect(blocks[2].type).toBe('na');
|
||||
});
|
||||
|
||||
it('a node with a square shape and a label', async () => {
|
||||
const str = `block-beta
|
||||
id["A label"]
|
||||
id2`;
|
||||
|
||||
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('na');
|
||||
});
|
||||
it('a diagram with multiple nodes with edges abc123', async () => {
|
||||
const str = `block-beta
|
||||
id1["first"] --> id2["second"]
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
const blocks = db.getBlocks();
|
||||
const edges = db.getEdges();
|
||||
expect(blocks.length).toBe(2);
|
||||
expect(edges.length).toBe(1);
|
||||
expect(edges[0].start).toBe('id1');
|
||||
expect(edges[0].end).toBe('id2');
|
||||
expect(edges[0].arrowTypeEnd).toBe('arrow_point');
|
||||
});
|
||||
it('a diagram with multiple nodes with edges abc123', async () => {
|
||||
const str = `block-beta
|
||||
id1["first"] -- "a label" --> id2["second"]
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
const blocks = db.getBlocks();
|
||||
const edges = db.getEdges();
|
||||
expect(blocks.length).toBe(2);
|
||||
expect(edges.length).toBe(1);
|
||||
expect(edges[0].start).toBe('id1');
|
||||
expect(edges[0].end).toBe('id2');
|
||||
expect(edges[0].arrowTypeEnd).toBe('arrow_point');
|
||||
expect(edges[0].label).toBe('a label');
|
||||
});
|
||||
it('a diagram with column statements', async () => {
|
||||
const str = `block-beta
|
||||
columns 2
|
||||
block1["Block 1"]
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
expect(db.getColumns('root')).toBe(2);
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(1);
|
||||
});
|
||||
it('a diagram withput column statements', async () => {
|
||||
const str = `block-beta
|
||||
block1["Block 1"]
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
expect(db.getColumns('root')).toBe(-1);
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(1);
|
||||
});
|
||||
it('a diagram with auto column statements', async () => {
|
||||
const str = `block-beta
|
||||
columns auto
|
||||
block1["Block 1"]
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
expect(db.getColumns('root')).toBe(-1);
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(1);
|
||||
});
|
||||
|
||||
it('blocks next to each other', async () => {
|
||||
const str = `block-beta
|
||||
columns 2
|
||||
block1["Block 1"]
|
||||
block2["Block 2"]
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
|
||||
const blocks = db.getBlocks();
|
||||
expect(db.getColumns('root')).toBe(2);
|
||||
expect(blocks.length).toBe(2);
|
||||
});
|
||||
|
||||
it('blocks on top of each other', async () => {
|
||||
const str = `block-beta
|
||||
columns 1
|
||||
block1["Block 1"]
|
||||
block2["Block 2"]
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
|
||||
const blocks = db.getBlocks();
|
||||
expect(db.getColumns('root')).toBe(1);
|
||||
expect(blocks.length).toBe(2);
|
||||
});
|
||||
|
||||
it('compound blocks 2', async () => {
|
||||
const str = `block-beta
|
||||
block
|
||||
aBlock["ABlock"]
|
||||
bBlock["BBlock"]
|
||||
end
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(1);
|
||||
|
||||
expect(blocks[0].children.length).toBe(2);
|
||||
expect(blocks[0].id).not.toBe(undefined);
|
||||
expect(blocks[0].label).toBe('');
|
||||
expect(blocks[0].type).toBe('composite');
|
||||
|
||||
const aBlock = blocks[0].children[0];
|
||||
|
||||
expect(aBlock.id).not.toBe(aBlock);
|
||||
expect(aBlock.label).toBe('ABlock');
|
||||
expect(aBlock.type).toBe('square');
|
||||
|
||||
const bBlock = blocks[0].children[1];
|
||||
expect(bBlock.id).not.toBe(bBlock);
|
||||
expect(bBlock.label).toBe('BBlock');
|
||||
expect(bBlock.type).toBe('square');
|
||||
});
|
||||
it('compound blocks of compound blocks', async () => {
|
||||
const str = `block-beta
|
||||
block
|
||||
aBlock["ABlock"]
|
||||
block
|
||||
bBlock["BBlock"]
|
||||
end
|
||||
end
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
const blocks = db.getBlocks();
|
||||
|
||||
const aBlock = blocks[0].children[0];
|
||||
const secondComposite = blocks[0].children[1];
|
||||
const bBlock = blocks[0].children[1].children[0];
|
||||
|
||||
expect(blocks[0].children.length).toBe(2);
|
||||
expect(blocks[0].id).not.toBe(undefined);
|
||||
expect(blocks[0].label).toBe('');
|
||||
expect(blocks[0].type).toBe('composite');
|
||||
|
||||
expect(secondComposite.children.length).toBe(1);
|
||||
expect(secondComposite.id).not.toBe(undefined);
|
||||
expect(secondComposite.label).toBe('');
|
||||
expect(secondComposite.type).toBe('composite');
|
||||
|
||||
expect(aBlock.id).not.toBe(aBlock);
|
||||
expect(aBlock.label).toBe('ABlock');
|
||||
expect(aBlock.type).toBe('square');
|
||||
|
||||
expect(bBlock.id).not.toBe(bBlock);
|
||||
expect(bBlock.label).toBe('BBlock');
|
||||
expect(bBlock.type).toBe('square');
|
||||
});
|
||||
it('compound blocks with title', async () => {
|
||||
const str = `block-beta
|
||||
block:compoundBlock["Compound block"]
|
||||
columns 1
|
||||
block2["Block 2"]
|
||||
end
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(1);
|
||||
|
||||
const compoundBlock = blocks[0];
|
||||
const block2 = compoundBlock.children[0];
|
||||
|
||||
expect(compoundBlock.children.length).toBe(1);
|
||||
expect(compoundBlock.id).toBe('compoundBlock');
|
||||
expect(compoundBlock.label).toBe('Compound block');
|
||||
expect(compoundBlock.type).toBe('composite');
|
||||
|
||||
expect(block2.id).toBe('block2');
|
||||
expect(block2.label).toBe('Block 2');
|
||||
expect(block2.type).toBe('square');
|
||||
});
|
||||
it('blocks mixed with compound blocks', async () => {
|
||||
const str = `block-beta
|
||||
columns 1
|
||||
block1["Block 1"]
|
||||
|
||||
block
|
||||
columns 2
|
||||
block2["Block 2"]
|
||||
block3["Block 3"]
|
||||
end
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(2);
|
||||
|
||||
const compoundBlock = blocks[1];
|
||||
const block2 = compoundBlock.children[0];
|
||||
|
||||
expect(compoundBlock.children.length).toBe(2);
|
||||
|
||||
expect(block2.id).toBe('block2');
|
||||
expect(block2.label).toBe('Block 2');
|
||||
expect(block2.type).toBe('square');
|
||||
});
|
||||
|
||||
it('Arrow blocks', async () => {
|
||||
const str = `block-beta
|
||||
columns 3
|
||||
block1["Block 1"]
|
||||
blockArrow<[" "]>(right)
|
||||
block2["Block 2"]`;
|
||||
|
||||
block.parse(str);
|
||||
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(3);
|
||||
|
||||
const block1 = blocks[0];
|
||||
const blockArrow = blocks[1];
|
||||
const block2 = blocks[2];
|
||||
|
||||
expect(block1.id).toBe('block1');
|
||||
expect(blockArrow.id).toBe('blockArrow');
|
||||
expect(block2.id).toBe('block2');
|
||||
expect(block2.label).toBe('Block 2');
|
||||
expect(block2.type).toBe('square');
|
||||
expect(blockArrow.type).toBe('block_arrow');
|
||||
expect(blockArrow.directions).toContain('right');
|
||||
});
|
||||
it('Arrow blocks with multiple points', async () => {
|
||||
const str = `block-beta
|
||||
columns 1
|
||||
A
|
||||
blockArrow<[" "]>(up, down)
|
||||
block
|
||||
columns 3
|
||||
B
|
||||
C
|
||||
D
|
||||
end`;
|
||||
|
||||
block.parse(str);
|
||||
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(3);
|
||||
|
||||
const blockArrow = blocks[1];
|
||||
expect(blockArrow.type).toBe('block_arrow');
|
||||
expect(blockArrow.directions).toContain('up');
|
||||
expect(blockArrow.directions).toContain('down');
|
||||
expect(blockArrow.directions).not.toContain('right');
|
||||
});
|
||||
it('blocks with different widths', async () => {
|
||||
const str = `block-beta
|
||||
columns 3
|
||||
one["One Slot"]
|
||||
two["Two slots"]:2
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(2);
|
||||
const one = blocks[0];
|
||||
const two = blocks[1];
|
||||
expect(two.widthInColumns).toBe(2);
|
||||
});
|
||||
it('empty blocks', async () => {
|
||||
const str = `block-beta
|
||||
columns 3
|
||||
space
|
||||
middle["In the middle"]
|
||||
space
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(3);
|
||||
const sp1 = blocks[0];
|
||||
const middle = blocks[1];
|
||||
const sp2 = blocks[2];
|
||||
expect(sp1.type).toBe('space');
|
||||
expect(sp2.type).toBe('space');
|
||||
expect(middle.label).toBe('In the middle');
|
||||
});
|
||||
it('classDef statements applied to a block', async () => {
|
||||
const str = `block-beta
|
||||
classDef black color:#ffffff, fill:#000000;
|
||||
|
||||
mc["Memcache"]
|
||||
class mc black
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(1);
|
||||
const mc = blocks[0];
|
||||
expect(mc.classes).toContain('black');
|
||||
const classes = db.getClasses();
|
||||
const black = classes.black;
|
||||
expect(black.id).toBe('black');
|
||||
expect(black.styles[0]).toEqual('color:#ffffff');
|
||||
});
|
||||
it('style statements applied to a block', async () => {
|
||||
const str = `block-beta
|
||||
columns 1
|
||||
B["A wide one in the middle"]
|
||||
style B fill:#f9F,stroke:#333,stroke-width:4px
|
||||
`;
|
||||
|
||||
block.parse(str);
|
||||
const blocks = db.getBlocks();
|
||||
expect(blocks.length).toBe(1);
|
||||
const B = blocks[0];
|
||||
expect(B.styles).toContain('fill:#f9F');
|
||||
});
|
||||
});
|
||||
});
|
||||
261
packages/mermaid/src/diagrams/block/renderHelpers.ts
Normal file
261
packages/mermaid/src/diagrams/block/renderHelpers.ts
Normal file
@@ -0,0 +1,261 @@
|
||||
import { getStylesFromArray } from '../../utils.js';
|
||||
import { insertNode, positionNode } from '../../dagre-wrapper/nodes.js';
|
||||
import { insertEdge, insertEdgeLabel, positionEdgeLabel } from '../../dagre-wrapper/edges.js';
|
||||
import * as graphlib from 'dagre-d3-es/src/graphlib/index.js';
|
||||
import { getConfig } from '../../config.js';
|
||||
import type { ContainerElement } from 'd3';
|
||||
import type { Block } from './blockTypes.js';
|
||||
import type { BlockDB } from './blockDB.js';
|
||||
|
||||
interface Node {
|
||||
classes: string;
|
||||
}
|
||||
|
||||
function getNodeFromBlock(block: Block, db: BlockDB, positioned = false) {
|
||||
const vertex = block;
|
||||
|
||||
let classStr = 'default';
|
||||
if ((vertex?.classes?.length || 0) > 0) {
|
||||
classStr = (vertex?.classes || []).join(' ');
|
||||
}
|
||||
classStr = classStr + ' flowchart-label';
|
||||
|
||||
// We create a SVG label, either by delegating to addHtmlLabel or manually
|
||||
let radius = 0;
|
||||
let shape = '';
|
||||
let layoutOptions = {};
|
||||
let padding;
|
||||
// Set the shape based parameters
|
||||
switch (vertex.type) {
|
||||
case 'round':
|
||||
radius = 5;
|
||||
shape = 'rect';
|
||||
break;
|
||||
case 'composite':
|
||||
radius = 0;
|
||||
shape = 'composite';
|
||||
padding = 0;
|
||||
break;
|
||||
case 'square':
|
||||
shape = 'rect';
|
||||
break;
|
||||
case 'diamond':
|
||||
shape = 'question';
|
||||
layoutOptions = {
|
||||
portConstraints: 'FIXED_SIDE',
|
||||
};
|
||||
break;
|
||||
case 'hexagon':
|
||||
shape = 'hexagon';
|
||||
break;
|
||||
case 'block_arrow':
|
||||
shape = 'block_arrow';
|
||||
break;
|
||||
case 'odd':
|
||||
shape = 'rect_left_inv_arrow';
|
||||
break;
|
||||
case 'lean_right':
|
||||
shape = 'lean_right';
|
||||
break;
|
||||
case 'lean_left':
|
||||
shape = 'lean_left';
|
||||
break;
|
||||
case 'trapezoid':
|
||||
shape = 'trapezoid';
|
||||
break;
|
||||
case 'inv_trapezoid':
|
||||
shape = 'inv_trapezoid';
|
||||
break;
|
||||
case 'rect_left_inv_arrow':
|
||||
shape = 'rect_left_inv_arrow';
|
||||
break;
|
||||
case 'circle':
|
||||
shape = 'circle';
|
||||
break;
|
||||
case 'ellipse':
|
||||
shape = 'ellipse';
|
||||
break;
|
||||
case 'stadium':
|
||||
shape = 'stadium';
|
||||
break;
|
||||
case 'subroutine':
|
||||
shape = 'subroutine';
|
||||
break;
|
||||
case 'cylinder':
|
||||
shape = 'cylinder';
|
||||
break;
|
||||
case 'group':
|
||||
shape = 'rect';
|
||||
break;
|
||||
case 'doublecircle':
|
||||
shape = 'doublecircle';
|
||||
break;
|
||||
default:
|
||||
shape = 'rect';
|
||||
}
|
||||
|
||||
const styles = getStylesFromArray(vertex?.styles || []);
|
||||
|
||||
// Use vertex id as text in the box if no text is provided by the graph definition
|
||||
const vertexText = vertex.label;
|
||||
|
||||
const bounds = vertex.size || { width: 0, height: 0, x: 0, y: 0 };
|
||||
// Add the node
|
||||
const node = {
|
||||
labelStyle: styles.labelStyle,
|
||||
shape: shape,
|
||||
labelText: vertexText,
|
||||
rx: radius,
|
||||
ry: radius,
|
||||
class: classStr,
|
||||
style: styles.style,
|
||||
id: vertex.id,
|
||||
directions: vertex.directions,
|
||||
width: bounds.width,
|
||||
height: bounds.height,
|
||||
x: bounds.x,
|
||||
y: bounds.y,
|
||||
positioned,
|
||||
intersect: undefined,
|
||||
type: vertex.type,
|
||||
padding: padding ?? (getConfig()?.block?.padding || 0),
|
||||
};
|
||||
return node;
|
||||
}
|
||||
async function calculateBlockSize(
|
||||
elem: d3.Selection<SVGGElement, unknown, HTMLElement, any>,
|
||||
block: any,
|
||||
db: any
|
||||
) {
|
||||
const node = getNodeFromBlock(block, db, false);
|
||||
if (node.type === 'group') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the element to the DOM to size it
|
||||
const nodeEl = await insertNode(elem, node);
|
||||
const boundingBox = nodeEl.node().getBBox();
|
||||
const obj = db.getBlock(node.id);
|
||||
obj.size = { width: boundingBox.width, height: boundingBox.height, x: 0, y: 0, node: nodeEl };
|
||||
db.setBlock(obj);
|
||||
nodeEl.remove();
|
||||
}
|
||||
type ActionFun = typeof calculateBlockSize;
|
||||
|
||||
export async function insertBlockPositioned(elem: any, block: Block, db: any) {
|
||||
const node = getNodeFromBlock(block, db, true);
|
||||
// Add the element to the DOM to size it
|
||||
const obj = db.getBlock(node.id);
|
||||
if (obj.type !== 'space') {
|
||||
const nodeEl = await insertNode(elem, node);
|
||||
block.intersect = node?.intersect;
|
||||
positionNode(node);
|
||||
}
|
||||
}
|
||||
|
||||
export async function performOperations(
|
||||
elem: d3.Selection<SVGGElement, unknown, HTMLElement, any>,
|
||||
blocks: Block[],
|
||||
db: BlockDB,
|
||||
operation: ActionFun
|
||||
) {
|
||||
for (const block of blocks) {
|
||||
await operation(elem, block, db);
|
||||
if (block.children) {
|
||||
await performOperations(elem, block.children, db, operation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function calculateBlockSizes(elem: any, blocks: Block[], db: BlockDB) {
|
||||
await performOperations(elem, blocks, db, calculateBlockSize);
|
||||
}
|
||||
|
||||
export async function insertBlocks(
|
||||
elem: d3.Selection<SVGGElement, unknown, HTMLElement, any>,
|
||||
blocks: Block[],
|
||||
db: BlockDB
|
||||
) {
|
||||
await performOperations(elem, blocks, db, insertBlockPositioned);
|
||||
}
|
||||
|
||||
export async function insertEdges(
|
||||
elem: any,
|
||||
edges: Block[],
|
||||
blocks: Block[],
|
||||
db: BlockDB,
|
||||
id: string
|
||||
) {
|
||||
const g = new graphlib.Graph({
|
||||
multigraph: true,
|
||||
compound: true,
|
||||
});
|
||||
g.setGraph({
|
||||
rankdir: 'TB',
|
||||
nodesep: 10,
|
||||
ranksep: 10,
|
||||
marginx: 8,
|
||||
marginy: 8,
|
||||
});
|
||||
|
||||
for (const block of blocks) {
|
||||
if (block.size) {
|
||||
g.setNode(block.id, {
|
||||
width: block.size.width,
|
||||
height: block.size.height,
|
||||
intersect: block.intersect,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const edge of edges) {
|
||||
// elem, e, edge, clusterDb, diagramType, graph;
|
||||
if (edge.start && edge.end) {
|
||||
const startBlock = db.getBlock(edge.start);
|
||||
const endBlock = db.getBlock(edge.end);
|
||||
|
||||
if (startBlock?.size && endBlock?.size) {
|
||||
const start = startBlock.size;
|
||||
const end = endBlock.size;
|
||||
const points = [
|
||||
{ x: start.x, y: start.y },
|
||||
{ x: start.x + (end.x - start.x) / 2, y: start.y + (end.y - start.y) / 2 },
|
||||
{ x: end.x, y: end.y },
|
||||
];
|
||||
// edge.points = points;
|
||||
await insertEdge(
|
||||
elem,
|
||||
{ v: edge.start, w: edge.end, name: edge.id },
|
||||
{
|
||||
...edge,
|
||||
arrowTypeEnd: edge.arrowTypeEnd,
|
||||
arrowTypeStart: edge.arrowTypeStart,
|
||||
points,
|
||||
classes: 'edge-thickness-normal edge-pattern-solid flowchart-link LS-a1 LE-b1',
|
||||
},
|
||||
undefined,
|
||||
'block',
|
||||
g,
|
||||
id
|
||||
);
|
||||
if (edge.label) {
|
||||
await insertEdgeLabel(elem, {
|
||||
...edge,
|
||||
label: edge.label,
|
||||
labelStyle: 'stroke: #333; stroke-width: 1.5px;fill:none;',
|
||||
arrowTypeEnd: edge.arrowTypeEnd,
|
||||
arrowTypeStart: edge.arrowTypeStart,
|
||||
points,
|
||||
classes: 'edge-thickness-normal edge-pattern-solid flowchart-link LS-a1 LE-b1',
|
||||
});
|
||||
await positionEdgeLabel(
|
||||
{ ...edge, x: points[1].x, y: points[1].y },
|
||||
{
|
||||
originalPath: points,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
147
packages/mermaid/src/diagrams/block/styles.ts
Normal file
147
packages/mermaid/src/diagrams/block/styles.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
import * as khroma from 'khroma';
|
||||
|
||||
/** Returns the styles given options */
|
||||
export interface BlockChartStyleOptions {
|
||||
arrowheadColor: string;
|
||||
border2: string;
|
||||
clusterBkg: string;
|
||||
clusterBorder: string;
|
||||
edgeLabelBackground: string;
|
||||
fontFamily: string;
|
||||
lineColor: string;
|
||||
mainBkg: string;
|
||||
nodeBorder: string;
|
||||
nodeTextColor: string;
|
||||
tertiaryColor: string;
|
||||
textColor: string;
|
||||
titleColor: string;
|
||||
}
|
||||
|
||||
const fade = (color: string, opacity: number) => {
|
||||
// @ts-ignore TODO: incorrect types from khroma
|
||||
const channel = khroma.channel;
|
||||
|
||||
const r = channel(color, 'r');
|
||||
const g = channel(color, 'g');
|
||||
const b = channel(color, 'b');
|
||||
|
||||
// @ts-ignore incorrect types from khroma
|
||||
return khroma.rgba(r, g, b, opacity);
|
||||
};
|
||||
|
||||
const getStyles = (options: BlockChartStyleOptions) =>
|
||||
`.label {
|
||||
font-family: ${options.fontFamily};
|
||||
color: ${options.nodeTextColor || options.textColor};
|
||||
}
|
||||
.cluster-label text {
|
||||
fill: ${options.titleColor};
|
||||
}
|
||||
.cluster-label span,p {
|
||||
color: ${options.titleColor};
|
||||
}
|
||||
|
||||
|
||||
|
||||
.label text,span,p {
|
||||
fill: ${options.nodeTextColor || options.textColor};
|
||||
color: ${options.nodeTextColor || options.textColor};
|
||||
}
|
||||
|
||||
.node rect,
|
||||
.node circle,
|
||||
.node ellipse,
|
||||
.node polygon,
|
||||
.node path {
|
||||
fill: ${options.mainBkg};
|
||||
stroke: ${options.nodeBorder};
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.flowchart-label text {
|
||||
text-anchor: middle;
|
||||
}
|
||||
// .flowchart-label .text-outer-tspan {
|
||||
// text-anchor: middle;
|
||||
// }
|
||||
// .flowchart-label .text-inner-tspan {
|
||||
// text-anchor: start;
|
||||
// }
|
||||
|
||||
.node .label {
|
||||
text-align: center;
|
||||
}
|
||||
.node.clickable {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.arrowheadPath {
|
||||
fill: ${options.arrowheadColor};
|
||||
}
|
||||
|
||||
.edgePath .path {
|
||||
stroke: ${options.lineColor};
|
||||
stroke-width: 2.0px;
|
||||
}
|
||||
|
||||
.flowchart-link {
|
||||
stroke: ${options.lineColor};
|
||||
fill: none;
|
||||
}
|
||||
|
||||
.edgeLabel {
|
||||
background-color: ${options.edgeLabelBackground};
|
||||
rect {
|
||||
opacity: 0.5;
|
||||
background-color: ${options.edgeLabelBackground};
|
||||
fill: ${options.edgeLabelBackground};
|
||||
}
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
/* For html labels only */
|
||||
.labelBkg {
|
||||
background-color: ${fade(options.edgeLabelBackground, 0.5)};
|
||||
// background-color:
|
||||
}
|
||||
|
||||
.node .cluster {
|
||||
// fill: ${fade(options.mainBkg, 0.5)};
|
||||
fill: ${fade(options.clusterBkg, 0.5)};
|
||||
stroke: ${fade(options.clusterBorder, 0.2)};
|
||||
box-shadow: rgba(50, 50, 93, 0.25) 0px 13px 27px -5px, rgba(0, 0, 0, 0.3) 0px 8px 16px -8px;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
|
||||
.cluster text {
|
||||
fill: ${options.titleColor};
|
||||
}
|
||||
|
||||
.cluster span,p {
|
||||
color: ${options.titleColor};
|
||||
}
|
||||
/* .cluster div {
|
||||
color: ${options.titleColor};
|
||||
} */
|
||||
|
||||
div.mermaidTooltip {
|
||||
position: absolute;
|
||||
text-align: center;
|
||||
max-width: 200px;
|
||||
padding: 2px;
|
||||
font-family: ${options.fontFamily};
|
||||
font-size: 12px;
|
||||
background: ${options.tertiaryColor};
|
||||
border: 1px solid ${options.border2};
|
||||
border-radius: 2px;
|
||||
pointer-events: none;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.flowchartTitleText {
|
||||
text-anchor: middle;
|
||||
font-size: 18px;
|
||||
fill: ${options.textColor};
|
||||
}
|
||||
`;
|
||||
|
||||
export default getStyles;
|
||||
@@ -11,7 +11,7 @@ let c4ShapeArray = [];
|
||||
let boundaryParseStack = [''];
|
||||
let currentBoundaryParse = 'global';
|
||||
let parentBoundaryParse = '';
|
||||
let boundarys = [
|
||||
let boundaries = [
|
||||
{
|
||||
alias: 'global',
|
||||
label: { text: 'global' },
|
||||
@@ -312,12 +312,12 @@ export const addPersonOrSystemBoundary = function (alias, label, type, tags, lin
|
||||
}
|
||||
|
||||
let boundary = {};
|
||||
const old = boundarys.find((boundary) => boundary.alias === alias);
|
||||
const old = boundaries.find((boundary) => boundary.alias === alias);
|
||||
if (old && alias === old.alias) {
|
||||
boundary = old;
|
||||
} else {
|
||||
boundary.alias = alias;
|
||||
boundarys.push(boundary);
|
||||
boundaries.push(boundary);
|
||||
}
|
||||
|
||||
// Don't allow null labels, either
|
||||
@@ -368,12 +368,12 @@ export const addContainerBoundary = function (alias, label, type, tags, link) {
|
||||
}
|
||||
|
||||
let boundary = {};
|
||||
const old = boundarys.find((boundary) => boundary.alias === alias);
|
||||
const old = boundaries.find((boundary) => boundary.alias === alias);
|
||||
if (old && alias === old.alias) {
|
||||
boundary = old;
|
||||
} else {
|
||||
boundary.alias = alias;
|
||||
boundarys.push(boundary);
|
||||
boundaries.push(boundary);
|
||||
}
|
||||
|
||||
// Don't allow null labels, either
|
||||
@@ -433,12 +433,12 @@ export const addDeploymentNode = function (
|
||||
}
|
||||
|
||||
let boundary = {};
|
||||
const old = boundarys.find((boundary) => boundary.alias === alias);
|
||||
const old = boundaries.find((boundary) => boundary.alias === alias);
|
||||
if (old && alias === old.alias) {
|
||||
boundary = old;
|
||||
} else {
|
||||
boundary.alias = alias;
|
||||
boundarys.push(boundary);
|
||||
boundaries.push(boundary);
|
||||
}
|
||||
|
||||
// Don't allow null labels, either
|
||||
@@ -514,7 +514,7 @@ export const updateElStyle = function (
|
||||
) {
|
||||
let old = c4ShapeArray.find((element) => element.alias === elementName);
|
||||
if (old === undefined) {
|
||||
old = boundarys.find((element) => element.alias === elementName);
|
||||
old = boundaries.find((element) => element.alias === elementName);
|
||||
if (old === undefined) {
|
||||
return;
|
||||
}
|
||||
@@ -697,14 +697,19 @@ export const getC4ShapeKeys = function (parentBoundary) {
|
||||
return Object.keys(getC4ShapeArray(parentBoundary));
|
||||
};
|
||||
|
||||
export const getBoundarys = function (parentBoundary) {
|
||||
export const getBoundaries = function (parentBoundary) {
|
||||
if (parentBoundary === undefined || parentBoundary === null) {
|
||||
return boundarys;
|
||||
return boundaries;
|
||||
} else {
|
||||
return boundarys.filter((boundary) => boundary.parentBoundary === parentBoundary);
|
||||
return boundaries.filter((boundary) => boundary.parentBoundary === parentBoundary);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link getBoundaries} instead
|
||||
*/
|
||||
export const getBoundarys = getBoundaries;
|
||||
|
||||
export const getRels = function () {
|
||||
return rels;
|
||||
};
|
||||
@@ -723,7 +728,7 @@ export const autoWrap = function () {
|
||||
|
||||
export const clear = function () {
|
||||
c4ShapeArray = [];
|
||||
boundarys = [
|
||||
boundaries = [
|
||||
{
|
||||
alias: 'global',
|
||||
label: { text: 'global' },
|
||||
@@ -804,6 +809,7 @@ export default {
|
||||
getC4ShapeArray,
|
||||
getC4Shape,
|
||||
getC4ShapeKeys,
|
||||
getBoundaries,
|
||||
getBoundarys,
|
||||
getCurrentBoundaryParse,
|
||||
getParentBoundaryParse,
|
||||
|
||||
@@ -542,15 +542,15 @@ function drawInsideBoundary(
|
||||
);
|
||||
}
|
||||
parentBoundaryAlias = currentBoundary.alias;
|
||||
let nextCurrentBoundarys = diagObj.db.getBoundarys(parentBoundaryAlias);
|
||||
let nextCurrentBoundaries = diagObj.db.getBoundarys(parentBoundaryAlias);
|
||||
|
||||
if (nextCurrentBoundarys.length > 0) {
|
||||
if (nextCurrentBoundaries.length > 0) {
|
||||
// draw boundary inside currentBoundary
|
||||
drawInsideBoundary(
|
||||
diagram,
|
||||
parentBoundaryAlias,
|
||||
currentBounds,
|
||||
nextCurrentBoundarys,
|
||||
nextCurrentBoundaries,
|
||||
diagObj
|
||||
);
|
||||
}
|
||||
|
||||
@@ -687,3 +687,5 @@ export default {
|
||||
insertComputerIcon,
|
||||
insertClockIcon,
|
||||
};
|
||||
|
||||
// cspell:ignoreRegExp /'Mstartx.*/g
|
||||
|
||||
@@ -193,6 +193,7 @@ export const draw = function (text, id, _version, diagObj) {
|
||||
const relations = diagObj.db.getRelations();
|
||||
relations.forEach(function (relation) {
|
||||
log.info(
|
||||
// cspell:ignore tjoho
|
||||
'tjoho' + getGraphId(relation.id1) + getGraphId(relation.id2) + JSON.stringify(relation)
|
||||
);
|
||||
g.setEdge(
|
||||
|
||||
@@ -289,6 +289,83 @@ const processSet = (input: string): string => {
|
||||
return chars.join('');
|
||||
};
|
||||
|
||||
// TODO: find a better method for detecting support. This interface was added in the MathML 4 spec.
|
||||
// Firefox versions between [4,71] (0.47%) and Safari versions between [5,13.4] (0.17%) don't have this interface implemented but MathML is supported
|
||||
export const isMathMLSupported = () => window.MathMLElement !== undefined;
|
||||
|
||||
export const katexRegex = /\$\$(.*)\$\$/g;
|
||||
|
||||
/**
|
||||
* Whether or not a text has KaTeX delimiters
|
||||
*
|
||||
* @param text - The text to test
|
||||
* @returns Whether or not the text has KaTeX delimiters
|
||||
*/
|
||||
export const hasKatex = (text: string): boolean => (text.match(katexRegex)?.length ?? 0) > 0;
|
||||
|
||||
/**
|
||||
* Computes the minimum dimensions needed to display a div containing MathML
|
||||
*
|
||||
* @param text - The text to test
|
||||
* @param config - Configuration for Mermaid
|
||||
* @returns Object containing \{width, height\}
|
||||
*/
|
||||
export const calculateMathMLDimensions = async (text: string, config: MermaidConfig) => {
|
||||
text = await renderKatex(text, config);
|
||||
const divElem = document.createElement('div');
|
||||
divElem.innerHTML = text;
|
||||
divElem.id = 'katex-temp';
|
||||
divElem.style.visibility = 'hidden';
|
||||
divElem.style.position = 'absolute';
|
||||
divElem.style.top = '0';
|
||||
const body = document.querySelector('body');
|
||||
body?.insertAdjacentElement('beforeend', divElem);
|
||||
const dim = { width: divElem.clientWidth, height: divElem.clientHeight };
|
||||
divElem.remove();
|
||||
return dim;
|
||||
};
|
||||
|
||||
/**
|
||||
* Attempts to render and return the KaTeX portion of a string with MathML
|
||||
*
|
||||
* @param text - The text to test
|
||||
* @param config - Configuration for Mermaid
|
||||
* @returns String containing MathML if KaTeX is supported, or an error message if it is not and stylesheets aren't present
|
||||
*/
|
||||
export const renderKatex = async (text: string, config: MermaidConfig): Promise<string> => {
|
||||
if (!hasKatex(text)) {
|
||||
return text;
|
||||
}
|
||||
|
||||
if (!isMathMLSupported() && !config.legacyMathML) {
|
||||
return text.replace(katexRegex, 'MathML is unsupported in this environment.');
|
||||
}
|
||||
|
||||
const { default: katex } = await import('katex');
|
||||
return text
|
||||
.split(lineBreakRegex)
|
||||
.map((line) =>
|
||||
hasKatex(line)
|
||||
? `
|
||||
<div style="display: flex; align-items: center; justify-content: center; white-space: nowrap;">
|
||||
${line}
|
||||
</div>
|
||||
`
|
||||
: `<div>${line}</div>`
|
||||
)
|
||||
.join('')
|
||||
.replace(katexRegex, (_, c) =>
|
||||
katex
|
||||
.renderToString(c, {
|
||||
throwOnError: true,
|
||||
displayMode: true,
|
||||
output: isMathMLSupported() ? 'mathml' : 'htmlAndMathml',
|
||||
})
|
||||
.replace(/\n/g, ' ')
|
||||
.replace(/<annotation.*<\/annotation>/g, '')
|
||||
);
|
||||
};
|
||||
|
||||
export default {
|
||||
getRows,
|
||||
sanitizeText,
|
||||
|
||||
@@ -11,6 +11,7 @@ export interface RectData {
|
||||
ry?: number;
|
||||
attrs?: Record<string, string | number>;
|
||||
anchor?: string;
|
||||
name?: string;
|
||||
}
|
||||
|
||||
export interface Bound {
|
||||
|
||||
@@ -21,6 +21,9 @@ export const drawRect = (element: SVG | Group, rectData: RectData): D3RectElemen
|
||||
rectElement.attr('stroke', rectData.stroke);
|
||||
rectElement.attr('width', rectData.width);
|
||||
rectElement.attr('height', rectData.height);
|
||||
if (rectData.name) {
|
||||
rectElement.attr('name', rectData.name);
|
||||
}
|
||||
rectData.rx !== undefined && rectElement.attr('rx', rectData.rx);
|
||||
rectData.ry !== undefined && rectElement.attr('ry', rectData.ry);
|
||||
|
||||
|
||||
@@ -4,20 +4,20 @@ import { selectSvgElement } from '../../rendering-util/selectSvgElement.js';
|
||||
import { configureSvgSize } from '../../setupGraphViewbox.js';
|
||||
|
||||
/**
|
||||
* Draws a an info picture in the tag with id: id based on the graph definition in text.
|
||||
* Draws an info picture in the tag with id: id based on the graph definition in text.
|
||||
*
|
||||
* @param _text - Mermaid graph definition.
|
||||
* @param id - The text for the error
|
||||
* @param version - The version
|
||||
*/
|
||||
export const draw = (_text: string, id: string, version: string) => {
|
||||
log.debug('renering svg for syntax error\n');
|
||||
|
||||
log.debug('rendering svg for syntax error\n');
|
||||
const svg: SVG = selectSvgElement(id);
|
||||
const g: Group = svg.append('g');
|
||||
|
||||
svg.attr('viewBox', '0 0 2412 512');
|
||||
configureSvgSize(svg, 100, 512, true);
|
||||
|
||||
const g: Group = svg.append('g');
|
||||
g.append('path')
|
||||
.attr('class', 'error-icon')
|
||||
.attr(
|
||||
|
||||
@@ -93,13 +93,13 @@ export const addVertices = async function (vert, svgId, root, doc, diagObj, pare
|
||||
},
|
||||
];
|
||||
|
||||
let radious = 0;
|
||||
let radius = 0;
|
||||
let _shape = '';
|
||||
let layoutOptions = {};
|
||||
// Set the shape based parameters
|
||||
switch (vertex.type) {
|
||||
case 'round':
|
||||
radious = 5;
|
||||
radius = 5;
|
||||
_shape = 'rect';
|
||||
break;
|
||||
case 'square':
|
||||
@@ -163,8 +163,8 @@ export const addVertices = async function (vert, svgId, root, doc, diagObj, pare
|
||||
shape: _shape,
|
||||
labelText: vertexText,
|
||||
labelType: vertex.labelType,
|
||||
rx: radious,
|
||||
ry: radious,
|
||||
rx: radius,
|
||||
ry: radius,
|
||||
class: classStr,
|
||||
style: styles.style,
|
||||
id: vertex.id,
|
||||
@@ -309,13 +309,12 @@ const getNextPosition = (position, edgeDirection, graphDirection) => {
|
||||
},
|
||||
};
|
||||
portPos.TD = portPos.TB;
|
||||
log.info('abc88', graphDirection, edgeDirection, position);
|
||||
return portPos[graphDirection][edgeDirection][position];
|
||||
// return 'south';
|
||||
};
|
||||
|
||||
const getNextPort = (node, edgeDirection, graphDirection) => {
|
||||
log.info('getNextPort abc88', { node, edgeDirection, graphDirection });
|
||||
log.info('getNextPort', { node, edgeDirection, graphDirection });
|
||||
if (!portPos[node]) {
|
||||
switch (graphDirection) {
|
||||
case 'TB':
|
||||
|
||||
@@ -29,7 +29,7 @@ let direction;
|
||||
let version; // As in graph
|
||||
|
||||
// Functions to be run after graph rendering
|
||||
let funs = [];
|
||||
let funs = []; // cspell:ignore funs
|
||||
|
||||
const sanitizeText = (txt) => common.sanitizeText(txt, config);
|
||||
|
||||
@@ -40,10 +40,10 @@ const sanitizeText = (txt) => common.sanitizeText(txt, config);
|
||||
* @public
|
||||
*/
|
||||
export const lookUpDomId = function (id) {
|
||||
const veritceKeys = Object.keys(vertices);
|
||||
for (const veritceKey of veritceKeys) {
|
||||
if (vertices[veritceKey].id === id) {
|
||||
return vertices[veritceKey].domId;
|
||||
const vertexKeys = Object.keys(vertices);
|
||||
for (const vertexKey of vertexKeys) {
|
||||
if (vertices[vertexKey].id === id) {
|
||||
return vertices[vertexKey].domId;
|
||||
}
|
||||
}
|
||||
return id;
|
||||
@@ -165,8 +165,8 @@ export const addSingleLink = function (_start, _end, type) {
|
||||
throw new Error(
|
||||
`Edge limit exceeded. ${edges.length} edges found, but the limit is ${config.maxEdges}.
|
||||
|
||||
Initialize mermaid with maxEdges set to a higher number to allow more edges.
|
||||
You cannot set this config via configuration inside the diagram as it is a secure config.
|
||||
Initialize mermaid with maxEdges set to a higher number to allow more edges.
|
||||
You cannot set this config via configuration inside the diagram as it is a secure config.
|
||||
You have to call mermaid.initialize.`
|
||||
);
|
||||
}
|
||||
@@ -426,7 +426,7 @@ const setupToolTips = function (element) {
|
||||
const el = select(this);
|
||||
const title = el.attr('title');
|
||||
|
||||
// Dont try to draw a tooltip if no data is provided
|
||||
// Don't try to draw a tooltip if no data is provided
|
||||
if (title === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import utils from '../../utils.js';
|
||||
import { render } from '../../dagre-wrapper/index.js';
|
||||
import { addHtmlLabel } from 'dagre-d3-es/src/dagre-js/label/add-html-label.js';
|
||||
import { log } from '../../logger.js';
|
||||
import common, { evaluate } from '../common/common.js';
|
||||
import common, { evaluate, renderKatex } from '../common/common.js';
|
||||
import { interpolateToCurve, getStylesFromArray } from '../../utils.js';
|
||||
import { setupGraphViewbox } from '../../setupGraphViewbox.js';
|
||||
|
||||
@@ -27,12 +27,12 @@ export const setConf = function (cnf) {
|
||||
* @param doc
|
||||
* @param diagObj
|
||||
*/
|
||||
export const addVertices = function (vert, g, svgId, root, doc, diagObj) {
|
||||
export const addVertices = async function (vert, g, svgId, root, doc, diagObj) {
|
||||
const svg = root.select(`[id="${svgId}"]`);
|
||||
const keys = Object.keys(vert);
|
||||
|
||||
// Iterate through each item in the vertex object (containing all the vertices found) in the graph definition
|
||||
keys.forEach(function (id) {
|
||||
for (const id of keys) {
|
||||
const vertex = vert[id];
|
||||
|
||||
/**
|
||||
@@ -59,10 +59,7 @@ export const addVertices = function (vert, g, svgId, root, doc, diagObj) {
|
||||
if (evaluate(getConfig().flowchart.htmlLabels)) {
|
||||
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
|
||||
const node = {
|
||||
label: vertexText.replace(
|
||||
/fa[blrs]?:fa-[\w-]+/g,
|
||||
(s) => `<i class='${s.replace(':', ' ')}'></i>`
|
||||
),
|
||||
label: vertexText,
|
||||
};
|
||||
vertexNode = addHtmlLabel(svg, node).node();
|
||||
vertexNode.parentNode.removeChild(vertexNode);
|
||||
@@ -84,12 +81,12 @@ export const addVertices = function (vert, g, svgId, root, doc, diagObj) {
|
||||
}
|
||||
}
|
||||
|
||||
let radious = 0;
|
||||
let radius = 0;
|
||||
let _shape = '';
|
||||
// Set the shape based parameters
|
||||
switch (vertex.type) {
|
||||
case 'round':
|
||||
radious = 5;
|
||||
radius = 5;
|
||||
_shape = 'rect';
|
||||
break;
|
||||
case 'square':
|
||||
@@ -143,14 +140,16 @@ export const addVertices = function (vert, g, svgId, root, doc, diagObj) {
|
||||
default:
|
||||
_shape = 'rect';
|
||||
}
|
||||
const labelText = await renderKatex(vertexText, getConfig());
|
||||
|
||||
// Add the node
|
||||
g.setNode(vertex.id, {
|
||||
labelStyle: styles.labelStyle,
|
||||
shape: _shape,
|
||||
labelText: vertexText,
|
||||
labelText,
|
||||
labelType: vertex.labelType,
|
||||
rx: radious,
|
||||
ry: radious,
|
||||
rx: radius,
|
||||
ry: radius,
|
||||
class: classStr,
|
||||
style: styles.style,
|
||||
id: vertex.id,
|
||||
@@ -170,9 +169,9 @@ export const addVertices = function (vert, g, svgId, root, doc, diagObj) {
|
||||
labelStyle: styles.labelStyle,
|
||||
labelType: vertex.labelType,
|
||||
shape: _shape,
|
||||
labelText: vertexText,
|
||||
rx: radious,
|
||||
ry: radious,
|
||||
labelText,
|
||||
rx: radius,
|
||||
ry: radius,
|
||||
class: classStr,
|
||||
style: styles.style,
|
||||
id: vertex.id,
|
||||
@@ -183,7 +182,7 @@ export const addVertices = function (vert, g, svgId, root, doc, diagObj) {
|
||||
props: vertex.props,
|
||||
padding: getConfig().flowchart.padding,
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -193,7 +192,7 @@ export const addVertices = function (vert, g, svgId, root, doc, diagObj) {
|
||||
* @param {object} g The graph object
|
||||
* @param diagObj
|
||||
*/
|
||||
export const addEdges = function (edges, g, diagObj) {
|
||||
export const addEdges = async function (edges, g, diagObj) {
|
||||
log.info('abc78 edges = ', edges);
|
||||
let cnt = 0;
|
||||
let linkIdCnt = {};
|
||||
@@ -207,7 +206,7 @@ export const addEdges = function (edges, g, diagObj) {
|
||||
defaultLabelStyle = defaultStyles.labelStyle;
|
||||
}
|
||||
|
||||
edges.forEach(function (edge) {
|
||||
for (const edge of edges) {
|
||||
cnt++;
|
||||
|
||||
// Identify Link
|
||||
@@ -315,9 +314,8 @@ export const addEdges = function (edges, g, diagObj) {
|
||||
edgeData.arrowheadStyle = 'fill: #333';
|
||||
edgeData.labelpos = 'c';
|
||||
}
|
||||
|
||||
edgeData.labelType = edge.labelType;
|
||||
edgeData.label = edge.text.replace(common.lineBreakRegex, '\n');
|
||||
edgeData.label = await renderKatex(edge.text.replace(common.lineBreakRegex, '\n'), getConfig());
|
||||
|
||||
if (edge.style === undefined) {
|
||||
edgeData.style = edgeData.style || 'stroke: #333; stroke-width: 1.5px;fill:none;';
|
||||
@@ -330,7 +328,7 @@ export const addEdges = function (edges, g, diagObj) {
|
||||
|
||||
// Add the edge to the graph
|
||||
g.setEdge(edge.start, edge.end, edgeData, cnt);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -349,6 +347,8 @@ export const getClasses = function (text, diagObj) {
|
||||
*
|
||||
* @param text
|
||||
* @param id
|
||||
* @param _version
|
||||
* @param diagObj
|
||||
*/
|
||||
|
||||
export const draw = async function (text, id, _version, diagObj) {
|
||||
@@ -425,8 +425,8 @@ export const draw = async function (text, id, _version, diagObj) {
|
||||
g.setParent(subG.nodes[j], subG.id);
|
||||
}
|
||||
}
|
||||
addVertices(vert, g, id, root, doc, diagObj);
|
||||
addEdges(edges, g, diagObj);
|
||||
await addVertices(vert, g, id, root, doc, diagObj);
|
||||
await addEdges(edges, g, diagObj);
|
||||
|
||||
// Add custom shapes
|
||||
// flowChartShapes.addToRenderV2(addShape);
|
||||
|
||||
@@ -15,7 +15,7 @@ describe('when using mermaid and ', function () {
|
||||
flowDb.clear();
|
||||
flowDb.setGen('gen-2');
|
||||
});
|
||||
it('should handle edges with text', () => {
|
||||
it('should handle edges with text', async () => {
|
||||
parser.parse('graph TD;A-->|text ex|B;');
|
||||
flowDb.getVertices();
|
||||
const edges = flowDb.getEdges();
|
||||
@@ -29,7 +29,7 @@ describe('when using mermaid and ', function () {
|
||||
},
|
||||
};
|
||||
|
||||
flowRenderer.addEdges(edges, mockG, diag);
|
||||
await flowRenderer.addEdges(edges, mockG, diag);
|
||||
});
|
||||
|
||||
it('should handle edges without text', async function () {
|
||||
@@ -45,10 +45,10 @@ describe('when using mermaid and ', function () {
|
||||
},
|
||||
};
|
||||
|
||||
flowRenderer.addEdges(edges, mockG, diag);
|
||||
await flowRenderer.addEdges(edges, mockG, diag);
|
||||
});
|
||||
|
||||
it('should handle open-ended edges', () => {
|
||||
it('should handle open-ended edges', async () => {
|
||||
parser.parse('graph TD;A---B;');
|
||||
flowDb.getVertices();
|
||||
const edges = flowDb.getEdges();
|
||||
@@ -61,10 +61,10 @@ describe('when using mermaid and ', function () {
|
||||
},
|
||||
};
|
||||
|
||||
flowRenderer.addEdges(edges, mockG, diag);
|
||||
await flowRenderer.addEdges(edges, mockG, diag);
|
||||
});
|
||||
|
||||
it('should handle edges with styles defined', () => {
|
||||
it('should handle edges with styles defined', async () => {
|
||||
parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;');
|
||||
flowDb.getVertices();
|
||||
const edges = flowDb.getEdges();
|
||||
@@ -78,9 +78,9 @@ describe('when using mermaid and ', function () {
|
||||
},
|
||||
};
|
||||
|
||||
flowRenderer.addEdges(edges, mockG, diag);
|
||||
await flowRenderer.addEdges(edges, mockG, diag);
|
||||
});
|
||||
it('should handle edges with interpolation defined', () => {
|
||||
it('should handle edges with interpolation defined', async () => {
|
||||
parser.parse('graph TD;A---B; linkStyle 0 interpolate basis');
|
||||
flowDb.getVertices();
|
||||
const edges = flowDb.getEdges();
|
||||
@@ -94,9 +94,9 @@ describe('when using mermaid and ', function () {
|
||||
},
|
||||
};
|
||||
|
||||
flowRenderer.addEdges(edges, mockG, diag);
|
||||
await flowRenderer.addEdges(edges, mockG, diag);
|
||||
});
|
||||
it('should handle edges with text and styles defined', () => {
|
||||
it('should handle edges with text and styles defined', async () => {
|
||||
parser.parse('graph TD;A---|the text|B; linkStyle 0 stroke:val1,stroke-width:val2;');
|
||||
flowDb.getVertices();
|
||||
const edges = flowDb.getEdges();
|
||||
@@ -111,10 +111,10 @@ describe('when using mermaid and ', function () {
|
||||
},
|
||||
};
|
||||
|
||||
flowRenderer.addEdges(edges, mockG, diag);
|
||||
await flowRenderer.addEdges(edges, mockG, diag);
|
||||
});
|
||||
|
||||
it('should set fill to "none" by default when handling edges', () => {
|
||||
it('should set fill to "none" by default when handling edges', async () => {
|
||||
parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;');
|
||||
flowDb.getVertices();
|
||||
const edges = flowDb.getEdges();
|
||||
@@ -128,10 +128,10 @@ describe('when using mermaid and ', function () {
|
||||
},
|
||||
};
|
||||
|
||||
flowRenderer.addEdges(edges, mockG, diag);
|
||||
await flowRenderer.addEdges(edges, mockG, diag);
|
||||
});
|
||||
|
||||
it('should not set fill to none if fill is set in linkStyle', () => {
|
||||
it('should not set fill to none if fill is set in linkStyle', async () => {
|
||||
parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2,fill:blue;');
|
||||
flowDb.getVertices();
|
||||
const edges = flowDb.getEdges();
|
||||
@@ -144,7 +144,7 @@ describe('when using mermaid and ', function () {
|
||||
},
|
||||
};
|
||||
|
||||
flowRenderer.addEdges(edges, mockG, diag);
|
||||
await flowRenderer.addEdges(edges, mockG, diag);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@ import { render as Render } from 'dagre-d3-es';
|
||||
import { applyStyle } from 'dagre-d3-es/src/dagre-js/util.js';
|
||||
import { addHtmlLabel } from 'dagre-d3-es/src/dagre-js/label/add-html-label.js';
|
||||
import { log } from '../../logger.js';
|
||||
import common, { evaluate } from '../common/common.js';
|
||||
import common, { evaluate, renderKatex } from '../common/common.js';
|
||||
import { interpolateToCurve, getStylesFromArray } from '../../utils.js';
|
||||
import { setupGraphViewbox } from '../../setupGraphViewbox.js';
|
||||
import flowChartShapes from './flowChartShapes.js';
|
||||
@@ -28,13 +28,13 @@ export const setConf = function (cnf) {
|
||||
* @param _doc
|
||||
* @param diagObj
|
||||
*/
|
||||
export const addVertices = function (vert, g, svgId, root, _doc, diagObj) {
|
||||
export const addVertices = async function (vert, g, svgId, root, _doc, diagObj) {
|
||||
const svg = !root ? select(`[id="${svgId}"]`) : root.select(`[id="${svgId}"]`);
|
||||
const doc = !_doc ? document : _doc;
|
||||
const keys = Object.keys(vert);
|
||||
|
||||
// Iterate through each item in the vertex object (containing all the vertices found) in the graph definition
|
||||
keys.forEach(function (id) {
|
||||
for (const id of keys) {
|
||||
const vertex = vert[id];
|
||||
|
||||
/**
|
||||
@@ -57,9 +57,12 @@ export const addVertices = function (vert, g, svgId, root, _doc, diagObj) {
|
||||
if (evaluate(getConfig().flowchart.htmlLabels)) {
|
||||
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
|
||||
const node = {
|
||||
label: vertexText.replace(
|
||||
/fa[blrs]?:fa-[\w-]+/g,
|
||||
(s) => `<i class='${s.replace(':', ' ')}'></i>`
|
||||
label: await renderKatex(
|
||||
vertexText.replace(
|
||||
/fa[blrs]?:fa-[\w-]+/g, // cspell:disable-line
|
||||
(s) => `<i class='${s.replace(':', ' ')}'></i>`
|
||||
),
|
||||
getConfig()
|
||||
),
|
||||
};
|
||||
vertexNode = addHtmlLabel(svg, node).node();
|
||||
@@ -81,12 +84,12 @@ export const addVertices = function (vert, g, svgId, root, _doc, diagObj) {
|
||||
vertexNode = svgLabel;
|
||||
}
|
||||
|
||||
let radious = 0;
|
||||
let radius = 0;
|
||||
let _shape = '';
|
||||
// Set the shape based parameters
|
||||
switch (vertex.type) {
|
||||
case 'round':
|
||||
radious = 5;
|
||||
radius = 5;
|
||||
_shape = 'rect';
|
||||
break;
|
||||
case 'square':
|
||||
@@ -144,13 +147,13 @@ export const addVertices = function (vert, g, svgId, root, _doc, diagObj) {
|
||||
labelStyle: styles.labelStyle,
|
||||
shape: _shape,
|
||||
label: vertexNode,
|
||||
rx: radious,
|
||||
ry: radious,
|
||||
rx: radius,
|
||||
ry: radius,
|
||||
class: classStr,
|
||||
style: styles.style,
|
||||
id: diagObj.db.lookUpDomId(vertex.id),
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -160,7 +163,7 @@ export const addVertices = function (vert, g, svgId, root, _doc, diagObj) {
|
||||
* @param {object} g The graph object
|
||||
* @param diagObj
|
||||
*/
|
||||
export const addEdges = function (edges, g, diagObj) {
|
||||
export const addEdges = async function (edges, g, diagObj) {
|
||||
let cnt = 0;
|
||||
|
||||
let defaultStyle;
|
||||
@@ -172,7 +175,7 @@ export const addEdges = function (edges, g, diagObj) {
|
||||
defaultLabelStyle = defaultStyles.labelStyle;
|
||||
}
|
||||
|
||||
edges.forEach(function (edge) {
|
||||
for (const edge of edges) {
|
||||
cnt++;
|
||||
|
||||
// Identify Link
|
||||
@@ -239,9 +242,12 @@ export const addEdges = function (edges, g, diagObj) {
|
||||
edgeData.labelType = 'html';
|
||||
edgeData.label = `<span id="L-${linkId}" class="edgeLabel L-${linkNameStart}' L-${linkNameEnd}" style="${
|
||||
edgeData.labelStyle
|
||||
}">${edge.text.replace(
|
||||
/fa[blrs]?:fa-[\w-]+/g,
|
||||
(s) => `<i class='${s.replace(':', ' ')}'></i>`
|
||||
}">${await renderKatex(
|
||||
edge.text.replace(
|
||||
/fa[blrs]?:fa-[\w-]+/g, // cspell:disable-line
|
||||
(s) => `<i class='${s.replace(':', ' ')}'></i>`
|
||||
),
|
||||
getConfig()
|
||||
)}</span>`;
|
||||
} else {
|
||||
edgeData.labelType = 'text';
|
||||
@@ -261,7 +267,7 @@ export const addEdges = function (edges, g, diagObj) {
|
||||
|
||||
// Add the edge to the graph
|
||||
g.setEdge(diagObj.db.lookUpDomId(edge.start), diagObj.db.lookUpDomId(edge.end), edgeData, cnt);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -284,7 +290,7 @@ export const getClasses = function (text, diagObj) {
|
||||
* @param _version
|
||||
* @param diagObj
|
||||
*/
|
||||
export const draw = function (text, id, _version, diagObj) {
|
||||
export const draw = async function (text, id, _version, diagObj) {
|
||||
log.info('Drawing flowchart');
|
||||
const { securityLevel, flowchart: conf } = getConfig();
|
||||
let sandboxElement;
|
||||
@@ -350,8 +356,8 @@ export const draw = function (text, id, _version, diagObj) {
|
||||
g.setParent(diagObj.db.lookUpDomId(subG.nodes[j]), diagObj.db.lookUpDomId(subG.id));
|
||||
}
|
||||
}
|
||||
addVertices(vert, g, id, root, doc, diagObj);
|
||||
addEdges(edges, g, diagObj);
|
||||
await addVertices(vert, g, id, root, doc, diagObj);
|
||||
await addEdges(edges, g, diagObj);
|
||||
|
||||
// Create the renderer
|
||||
const render = new Render();
|
||||
|
||||
@@ -27,7 +27,7 @@ describe('the flowchart renderer', function () {
|
||||
['cylinder', 'cylinder'],
|
||||
['group', 'rect'],
|
||||
].forEach(function ([type, expectedShape, expectedRadios = 0]) {
|
||||
it(`should add the correct shaped node to the graph for vertex type ${type}`, function () {
|
||||
it(`should add the correct shaped node to the graph for vertex type ${type}`, async function () {
|
||||
const fakeDiag = {
|
||||
db: {
|
||||
lookUpDomId: () => {
|
||||
@@ -41,7 +41,7 @@ describe('the flowchart renderer', function () {
|
||||
addedNodes.push([id, object]);
|
||||
},
|
||||
};
|
||||
addVertices(
|
||||
await addVertices(
|
||||
{
|
||||
v1: {
|
||||
type,
|
||||
@@ -70,7 +70,7 @@ describe('the flowchart renderer', function () {
|
||||
['Multi<br>Line', 'Multi<br/>Line', 'Multi<br />Line', 'Multi<br\t/>Line'].forEach(function (
|
||||
labelText
|
||||
) {
|
||||
it('should handle multiline texts with different line breaks', function () {
|
||||
it('should handle multiline texts with different line breaks', async function () {
|
||||
const addedNodes = [];
|
||||
const fakeDiag = {
|
||||
db: {
|
||||
@@ -84,7 +84,7 @@ describe('the flowchart renderer', function () {
|
||||
addedNodes.push([id, object]);
|
||||
},
|
||||
};
|
||||
addVertices(
|
||||
await addVertices(
|
||||
{
|
||||
v1: {
|
||||
type: 'rect',
|
||||
@@ -121,7 +121,7 @@ describe('the flowchart renderer', function () {
|
||||
'color:#ccc;text-align:center;',
|
||||
],
|
||||
].forEach(function ([style, expectedStyle, expectedLabelStyle]) {
|
||||
it(`should add the styles to style and/or labelStyle for style ${style}`, function () {
|
||||
it(`should add the styles to style and/or labelStyle for style ${style}`, async function () {
|
||||
const addedNodes = [];
|
||||
const fakeDiag = {
|
||||
db: {
|
||||
@@ -135,7 +135,7 @@ describe('the flowchart renderer', function () {
|
||||
addedNodes.push([id, object]);
|
||||
},
|
||||
};
|
||||
addVertices(
|
||||
await addVertices(
|
||||
{
|
||||
v1: {
|
||||
type: 'rect',
|
||||
@@ -160,7 +160,7 @@ describe('the flowchart renderer', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it(`should add default class to all nodes which do not have another class assigned`, function () {
|
||||
it(`should add default class to all nodes which do not have another class assigned`, async function () {
|
||||
const addedNodes = [];
|
||||
const mockG = {
|
||||
setNode: function (id, object) {
|
||||
@@ -174,7 +174,7 @@ describe('the flowchart renderer', function () {
|
||||
},
|
||||
},
|
||||
};
|
||||
addVertices(
|
||||
await addVertices(
|
||||
{
|
||||
v1: {
|
||||
type: 'rect',
|
||||
@@ -206,7 +206,7 @@ describe('the flowchart renderer', function () {
|
||||
});
|
||||
|
||||
describe('when adding edges to a graph', function () {
|
||||
it('should handle multiline texts and set centered label position', function () {
|
||||
it('should handle multiline texts and set centered label position', async function () {
|
||||
const addedEdges = [];
|
||||
const fakeDiag = {
|
||||
db: {
|
||||
@@ -220,7 +220,7 @@ describe('the flowchart renderer', function () {
|
||||
addedEdges.push(data);
|
||||
},
|
||||
};
|
||||
addEdges(
|
||||
await addEdges(
|
||||
[
|
||||
{ text: 'Multi<br>Line' },
|
||||
{ text: 'Multi<br/>Line' },
|
||||
@@ -251,7 +251,7 @@ describe('the flowchart renderer', function () {
|
||||
'fill:red;',
|
||||
],
|
||||
].forEach(function ([style, expectedStyle, expectedLabelStyle]) {
|
||||
it(`should add the styles to style and/or labelStyle for style ${style}`, function () {
|
||||
it(`should add the styles to style and/or labelStyle for style ${style}`, async function () {
|
||||
const addedEdges = [];
|
||||
const fakeDiag = {
|
||||
db: {
|
||||
@@ -265,7 +265,7 @@ describe('the flowchart renderer', function () {
|
||||
addedEdges.push(data);
|
||||
},
|
||||
};
|
||||
addEdges([{ style: style, text: 'styling' }], mockG, fakeDiag);
|
||||
await addEdges([{ style: style, text: 'styling' }], mockG, fakeDiag);
|
||||
|
||||
expect(addedEdges).toHaveLength(1);
|
||||
expect(addedEdges[0]).toHaveProperty('style', expectedStyle);
|
||||
|
||||
@@ -292,15 +292,15 @@ graphConfig
|
||||
| NEWLINE graphConfig
|
||||
| GRAPH NODIR
|
||||
{ yy.setDirection('TB');$$ = 'TB';}
|
||||
| GRAPH DIR FirstStmtSeperator
|
||||
| GRAPH DIR FirstStmtSeparator
|
||||
{ yy.setDirection($DIR);$$ = $DIR;}
|
||||
// | GRAPH SPACE TAGEND FirstStmtSeperator
|
||||
// | GRAPH SPACE TAGEND FirstStmtSeparator
|
||||
// { yy.setDirection("LR");$$ = $TAGEND;}
|
||||
// | GRAPH SPACE TAGSTART FirstStmtSeperator
|
||||
// | GRAPH SPACE TAGSTART FirstStmtSeparator
|
||||
// { yy.setDirection("RL");$$ = $TAGSTART;}
|
||||
// | GRAPH SPACE UP FirstStmtSeperator
|
||||
// | GRAPH SPACE UP FirstStmtSeparator
|
||||
// { yy.setDirection("BT");$$ = $UP;}
|
||||
// | GRAPH SPACE DOWN FirstStmtSeperator
|
||||
// | GRAPH SPACE DOWN FirstStmtSeparator
|
||||
// { yy.setDirection("TB");$$ = $DOWN;}
|
||||
;
|
||||
|
||||
@@ -310,7 +310,7 @@ ending: endToken ending
|
||||
|
||||
endToken: NEWLINE | SPACE | EOF;
|
||||
|
||||
FirstStmtSeperator
|
||||
FirstStmtSeparator
|
||||
: SEMI | NEWLINE | spaceList NEWLINE ;
|
||||
|
||||
|
||||
@@ -328,8 +328,8 @@ spaceList
|
||||
;
|
||||
|
||||
statement
|
||||
: verticeStatement separator
|
||||
{ /* console.warn('finat vs', $verticeStatement.nodes); */ $$=$verticeStatement.nodes}
|
||||
: vertexStatement separator
|
||||
{ /* console.warn('finat vs', $vertexStatement.nodes); */ $$=$vertexStatement.nodes}
|
||||
| styleStatement separator
|
||||
{$$=[];}
|
||||
| linkStyleStatement separator
|
||||
@@ -357,10 +357,10 @@ statement
|
||||
separator: NEWLINE | SEMI | EOF ;
|
||||
|
||||
|
||||
verticeStatement: verticeStatement link node
|
||||
{ /* console.warn('vs',$verticeStatement.stmt,$node); */ yy.addLink($verticeStatement.stmt,$node,$link); $$ = { stmt: $node, nodes: $node.concat($verticeStatement.nodes) } }
|
||||
| verticeStatement link node spaceList
|
||||
{ /* console.warn('vs',$verticeStatement.stmt,$node); */ yy.addLink($verticeStatement.stmt,$node,$link); $$ = { stmt: $node, nodes: $node.concat($verticeStatement.nodes) } }
|
||||
vertexStatement: vertexStatement link node
|
||||
{ /* console.warn('vs',$vertexStatement.stmt,$node); */ yy.addLink($vertexStatement.stmt,$node,$link); $$ = { stmt: $node, nodes: $node.concat($vertexStatement.nodes) } }
|
||||
| vertexStatement link node spaceList
|
||||
{ /* console.warn('vs',$vertexStatement.stmt,$node); */ yy.addLink($vertexStatement.stmt,$node,$link); $$ = { stmt: $node, nodes: $node.concat($vertexStatement.nodes) } }
|
||||
|node spaceList {/*console.warn('noda', $node);*/ $$ = {stmt: $node, nodes:$node }}
|
||||
|node { /*console.warn('noda', $node);*/ $$ = {stmt: $node, nodes:$node }}
|
||||
;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import flowDb from '../flowDb.js';
|
||||
import flow from './flow.jison';
|
||||
import { setConfig } from '../../../config.js';
|
||||
import { cleanupComments } from '../../../diagram-api/comments.js';
|
||||
import { setConfig } from '../../../config.js';
|
||||
|
||||
setConfig({
|
||||
securityLevel: 'strict',
|
||||
|
||||
@@ -66,6 +66,12 @@ const getStyles = (options: FlowChartStyleOptions) =>
|
||||
// text-anchor: start;
|
||||
// }
|
||||
|
||||
.node .katex path {
|
||||
fill: #000;
|
||||
stroke: #000;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
|
||||
.node .label {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
@@ -332,6 +332,7 @@ const getStartDate = function (prevTime, dateFormat, str) {
|
||||
* @returns {[value: number, unit: dayjs.ManipulateType]} Arguments to pass to `dayjs.add()`
|
||||
*/
|
||||
const parseDuration = function (str) {
|
||||
// cspell:disable-next-line
|
||||
const statement = /^(\d+(?:\.\d+)?)([Mdhmswy]|ms)$/.exec(str.trim());
|
||||
if (statement !== null) {
|
||||
return [Number.parseFloat(statement[1]), statement[2]];
|
||||
|
||||
@@ -178,7 +178,7 @@ export const draw = function (text, id, version, diagObj) {
|
||||
// tasks are created based on their order of startTime
|
||||
taskArray.sort(taskCompare);
|
||||
|
||||
makeGant(taskArray, w, h);
|
||||
makeGantt(taskArray, w, h);
|
||||
|
||||
configureSvgSize(svg, h, w, conf.useMaxWidth);
|
||||
|
||||
@@ -194,7 +194,7 @@ export const draw = function (text, id, version, diagObj) {
|
||||
* @param pageWidth
|
||||
* @param pageHeight
|
||||
*/
|
||||
function makeGant(tasks, pageWidth, pageHeight) {
|
||||
function makeGantt(tasks, pageWidth, pageHeight) {
|
||||
const barHeight = conf.barHeight;
|
||||
const gap = barHeight + conf.barGap;
|
||||
const topPadding = conf.topPadding;
|
||||
@@ -695,12 +695,12 @@ export const draw = function (text, id, version, diagObj) {
|
||||
function vertLabels(theGap, theTopPad) {
|
||||
let prevGap = 0;
|
||||
|
||||
const numOccurances = Object.keys(categoryHeights).map((d) => [d, categoryHeights[d]]);
|
||||
const numOccurrences = Object.keys(categoryHeights).map((d) => [d, categoryHeights[d]]);
|
||||
|
||||
svg
|
||||
.append('g') // without doing this, impossible to put grid lines behind text
|
||||
.selectAll('text')
|
||||
.data(numOccurances)
|
||||
.data(numOccurrences)
|
||||
.enter()
|
||||
.append(function (d) {
|
||||
const rows = d[0].split(common.lineBreakRegex);
|
||||
@@ -725,7 +725,7 @@ export const draw = function (text, id, version, diagObj) {
|
||||
.attr('y', function (d, i) {
|
||||
if (i > 0) {
|
||||
for (let j = 0; j < i; j++) {
|
||||
prevGap += numOccurances[i - 1][1];
|
||||
prevGap += numOccurrences[i - 1][1];
|
||||
return (d[1] * theGap) / 2 + prevGap * theGap + theTopPad;
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -36,8 +36,8 @@ function getId() {
|
||||
// * @param otherCommit
|
||||
// */
|
||||
// eslint-disable-next-line @cspell/spellchecker
|
||||
// function isfastforwardable(currentCommit, otherCommit) {
|
||||
// log.debug('Entering isfastforwardable:', currentCommit.id, otherCommit.id);
|
||||
// function isFastForwardable(currentCommit, otherCommit) {
|
||||
// log.debug('Entering isFastForwardable:', currentCommit.id, otherCommit.id);
|
||||
// let cnt = 0;
|
||||
// while (currentCommit.seq <= otherCommit.seq && currentCommit !== otherCommit && cnt < 1000) {
|
||||
// cnt++;
|
||||
@@ -46,8 +46,8 @@ function getId() {
|
||||
// if (Array.isArray(otherCommit.parent)) {
|
||||
// log.debug('In merge commit:', otherCommit.parent);
|
||||
// return (
|
||||
// isfastforwardable(currentCommit, commits[otherCommit.parent[0]]) ||
|
||||
// isfastforwardable(currentCommit, commits[otherCommit.parent[1]])
|
||||
// isFastForwardable(currentCommit, commits[otherCommit.parent[0]]) ||
|
||||
// isFastForwardable(currentCommit, commits[otherCommit.parent[1]])
|
||||
// );
|
||||
// } else {
|
||||
// otherCommit = commits[otherCommit.parent];
|
||||
@@ -64,7 +64,7 @@ function getId() {
|
||||
// function isReachableFrom(currentCommit, otherCommit) {
|
||||
// const currentSeq = currentCommit.seq;
|
||||
// const otherSeq = otherCommit.seq;
|
||||
// if (currentSeq > otherSeq) return isfastforwardable(otherCommit, currentCommit);
|
||||
// if (currentSeq > otherSeq) return isFastForwardable(otherCommit, currentCommit);
|
||||
// return false;
|
||||
// }
|
||||
|
||||
@@ -231,7 +231,7 @@ export const merge = function (otherBranch, custom_id, override_type, custom_tag
|
||||
// log.debug('Already merged');
|
||||
// return;
|
||||
// }
|
||||
// if (isfastforwardable(currentCommit, otherCommit)) {
|
||||
// if (isFastForwardable(currentCommit, otherCommit)) {
|
||||
// branches[curBranch] = branches[otherBranch];
|
||||
// head = commits[branches[curBranch]];
|
||||
// } else {
|
||||
|
||||
@@ -295,7 +295,7 @@ function renderCommitHistory(svg, commitId, branches, direction) {
|
||||
}
|
||||
|
||||
if (Array.isArray(commitId)) {
|
||||
logger.debug('found merge commmit', commitId);
|
||||
logger.debug('found merge commit', commitId);
|
||||
renderCommitHistory(svg, commitId[0], branches, direction);
|
||||
branchNum++;
|
||||
renderCommitHistory(svg, commitId[1], branches, direction);
|
||||
|
||||
@@ -84,7 +84,7 @@ options
|
||||
| NL
|
||||
;
|
||||
body
|
||||
: /*emmpty*/ {$$ = []}
|
||||
: /*empty*/ {$$ = []}
|
||||
| body line {$1.push($2); $$=$1;}
|
||||
;
|
||||
line
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
// @ts-ignore: JISON doesn't support types
|
||||
import mindmapParser from './parser/mindmap.jison';
|
||||
import * as mindmapDb from './mindmapDb.js';
|
||||
import mindmapRenderer from './mindmapRenderer.js';
|
||||
import mindmapStyles from './styles.js';
|
||||
import parser from './parser/mindmap.jison';
|
||||
import db from './mindmapDb.js';
|
||||
import renderer from './mindmapRenderer.js';
|
||||
import styles from './styles.js';
|
||||
import type { DiagramDefinition } from '../../diagram-api/types.js';
|
||||
|
||||
export const diagram = {
|
||||
db: mindmapDb,
|
||||
renderer: mindmapRenderer,
|
||||
parser: mindmapParser,
|
||||
styles: mindmapStyles,
|
||||
export const diagram: DiagramDefinition = {
|
||||
db,
|
||||
renderer,
|
||||
parser,
|
||||
styles,
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// @ts-expect-error No types available for JISON
|
||||
import { parser as mindmap } from './parser/mindmap.jison';
|
||||
import * as mindmapDB from './mindmapDb.js';
|
||||
import mindmapDB from './mindmapDb.js';
|
||||
// Todo fix utils functions for tests
|
||||
import { setLogLevel } from '../../diagram-api/diagramAPI.js';
|
||||
|
||||
@@ -11,7 +12,7 @@ describe('when parsing a mindmap ', function () {
|
||||
});
|
||||
describe('hiearchy', function () {
|
||||
it('MMP-1 should handle a simple root definition abc122', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root`;
|
||||
|
||||
mindmap.parse(str);
|
||||
@@ -19,7 +20,7 @@ describe('when parsing a mindmap ', function () {
|
||||
expect(mindmap.yy.getMindmap().descr).toEqual('root');
|
||||
});
|
||||
it('MMP-2 should handle a hierachial mindmap definition', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root
|
||||
child1
|
||||
child2
|
||||
@@ -34,7 +35,7 @@ describe('when parsing a mindmap ', function () {
|
||||
});
|
||||
|
||||
it('3 should handle a simple root definition with a shape and without an id abc123', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
(root)`;
|
||||
|
||||
mindmap.parse(str);
|
||||
@@ -43,7 +44,7 @@ describe('when parsing a mindmap ', function () {
|
||||
});
|
||||
|
||||
it('MMP-4 should handle a deeper hierachial mindmap definition', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root
|
||||
child1
|
||||
leaf1
|
||||
@@ -58,40 +59,27 @@ describe('when parsing a mindmap ', function () {
|
||||
expect(mm.children[1].descr).toEqual('child2');
|
||||
});
|
||||
it('5 Multiple roots are illegal', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root
|
||||
fakeRoot`;
|
||||
|
||||
try {
|
||||
mindmap.parse(str);
|
||||
// Fail test if above expression doesn't throw anything.
|
||||
expect(true).toBe(false);
|
||||
} catch (e) {
|
||||
expect(e.message).toBe(
|
||||
'There can be only one root. No parent could be found for ("fakeRoot")'
|
||||
);
|
||||
}
|
||||
expect(() => mindmap.parse(str)).toThrow(
|
||||
'There can be only one root. No parent could be found for ("fakeRoot")'
|
||||
);
|
||||
});
|
||||
it('MMP-6 real root in wrong place', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root
|
||||
fakeRoot
|
||||
realRootWrongPlace`;
|
||||
|
||||
try {
|
||||
mindmap.parse(str);
|
||||
// Fail test if above expression doesn't throw anything.
|
||||
expect(true).toBe(false);
|
||||
} catch (e) {
|
||||
expect(e.message).toBe(
|
||||
'There can be only one root. No parent could be found for ("fakeRoot")'
|
||||
);
|
||||
}
|
||||
expect(() => mindmap.parse(str)).toThrow(
|
||||
'There can be only one root. No parent could be found for ("fakeRoot")'
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('nodes', function () {
|
||||
it('MMP-7 should handle an id and type for a node definition', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root[The root]
|
||||
`;
|
||||
|
||||
@@ -102,7 +90,7 @@ describe('when parsing a mindmap ', function () {
|
||||
expect(mm.type).toEqual(mindmap.yy.nodeType.RECT);
|
||||
});
|
||||
it('MMP-8 should handle an id and type for a node definition', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root
|
||||
theId(child1)`;
|
||||
|
||||
@@ -116,7 +104,7 @@ describe('when parsing a mindmap ', function () {
|
||||
expect(child.type).toEqual(mindmap.yy.nodeType.ROUNDED_RECT);
|
||||
});
|
||||
it('MMP-9 should handle an id and type for a node definition', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root
|
||||
theId(child1)`;
|
||||
|
||||
@@ -130,7 +118,7 @@ root
|
||||
expect(child.type).toEqual(mindmap.yy.nodeType.ROUNDED_RECT);
|
||||
});
|
||||
it('MMP-10 multiple types (circle)', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root((the root))
|
||||
`;
|
||||
|
||||
@@ -142,7 +130,7 @@ root
|
||||
});
|
||||
|
||||
it('MMP-11 multiple types (cloud)', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root)the root(
|
||||
`;
|
||||
|
||||
@@ -153,7 +141,7 @@ root
|
||||
expect(mm.type).toEqual(mindmap.yy.nodeType.CLOUD);
|
||||
});
|
||||
it('MMP-12 multiple types (bang)', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root))the root((
|
||||
`;
|
||||
|
||||
@@ -165,7 +153,7 @@ root
|
||||
});
|
||||
|
||||
it('MMP-12-a multiple types (hexagon)', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root{{the root}}
|
||||
`;
|
||||
|
||||
@@ -178,7 +166,7 @@ root
|
||||
});
|
||||
describe('decorations', function () {
|
||||
it('MMP-13 should be possible to set an icon for the node', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root[The root]
|
||||
::icon(bomb)
|
||||
`;
|
||||
@@ -192,7 +180,7 @@ root
|
||||
expect(mm.icon).toEqual('bomb');
|
||||
});
|
||||
it('MMP-14 should be possible to set classes for the node', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root[The root]
|
||||
:::m-4 p-8
|
||||
`;
|
||||
@@ -206,7 +194,7 @@ root
|
||||
expect(mm.class).toEqual('m-4 p-8');
|
||||
});
|
||||
it('MMP-15 should be possible to set both classes and icon for the node', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root[The root]
|
||||
:::m-4 p-8
|
||||
::icon(bomb)
|
||||
@@ -222,7 +210,7 @@ root
|
||||
expect(mm.icon).toEqual('bomb');
|
||||
});
|
||||
it('MMP-16 should be possible to set both classes and icon for the node', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root[The root]
|
||||
::icon(bomb)
|
||||
:::m-4 p-8
|
||||
@@ -240,7 +228,7 @@ root
|
||||
});
|
||||
describe('descriptions', function () {
|
||||
it('MMP-17 should be possible to use node syntax in the descriptions', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root["String containing []"]
|
||||
`;
|
||||
mindmap.parse(str);
|
||||
@@ -249,7 +237,7 @@ root
|
||||
expect(mm.descr).toEqual('String containing []');
|
||||
});
|
||||
it('MMP-18 should be possible to use node syntax in the descriptions in children', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root["String containing []"]
|
||||
child1["String containing ()"]
|
||||
`;
|
||||
@@ -261,7 +249,7 @@ root
|
||||
expect(mm.children[0].descr).toEqual('String containing ()');
|
||||
});
|
||||
it('MMP-19 should be possible to have a child after a class assignment', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root(Root)
|
||||
Child(Child)
|
||||
:::hot
|
||||
@@ -281,7 +269,7 @@ root
|
||||
});
|
||||
});
|
||||
it('MMP-20 should be possible to have meaningless empty rows in a mindmap abc124', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root(Root)
|
||||
Child(Child)
|
||||
a(a)
|
||||
@@ -300,7 +288,7 @@ root
|
||||
expect(child.children[1].nodeId).toEqual('b');
|
||||
});
|
||||
it('MMP-21 should be possible to have comments in a mindmap', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root(Root)
|
||||
Child(Child)
|
||||
a(a)
|
||||
@@ -321,7 +309,7 @@ root
|
||||
});
|
||||
|
||||
it('MMP-22 should be possible to have comments at the end of a line', function () {
|
||||
let str = `mindmap
|
||||
const str = `mindmap
|
||||
root(Root)
|
||||
Child(Child)
|
||||
a(a) %% This is a comment
|
||||
@@ -339,7 +327,7 @@ root
|
||||
expect(child.children[1].nodeId).toEqual('b');
|
||||
});
|
||||
it('MMP-23 Rows with only spaces should not interfere', function () {
|
||||
let str = 'mindmap\nroot\n A\n \n\n B';
|
||||
const str = 'mindmap\nroot\n A\n \n\n B';
|
||||
mindmap.parse(str);
|
||||
const mm = mindmap.yy.getMindmap();
|
||||
expect(mm.nodeId).toEqual('root');
|
||||
@@ -351,7 +339,7 @@ root
|
||||
expect(child2.nodeId).toEqual('B');
|
||||
});
|
||||
it('MMP-24 Handle rows above the mindmap declarations', function () {
|
||||
let str = '\n \nmindmap\nroot\n A\n \n\n B';
|
||||
const str = '\n \nmindmap\nroot\n A\n \n\n B';
|
||||
mindmap.parse(str);
|
||||
const mm = mindmap.yy.getMindmap();
|
||||
expect(mm.nodeId).toEqual('root');
|
||||
@@ -363,7 +351,7 @@ root
|
||||
expect(child2.nodeId).toEqual('B');
|
||||
});
|
||||
it('MMP-25 Handle rows above the mindmap declarations, no space', function () {
|
||||
let str = '\n\n\nmindmap\nroot\n A\n \n\n B';
|
||||
const str = '\n\n\nmindmap\nroot\n A\n \n\n B';
|
||||
mindmap.parse(str);
|
||||
const mm = mindmap.yy.getMindmap();
|
||||
expect(mm.nodeId).toEqual('root');
|
||||
@@ -1,19 +1,21 @@
|
||||
import { getConfig } from '../../diagram-api/diagramAPI.js';
|
||||
import { sanitizeText as _sanitizeText } from '../../diagrams/common/common.js';
|
||||
import type { D3Element } from '../../mermaidAPI.js';
|
||||
import { sanitizeText } from '../../diagrams/common/common.js';
|
||||
import { log } from '../../logger.js';
|
||||
import type { MindmapNode } from './mindmapTypes.js';
|
||||
import defaultConfig from '../../defaultConfig.js';
|
||||
|
||||
export const sanitizeText = (text) => _sanitizeText(text, getConfig());
|
||||
|
||||
let nodes = [];
|
||||
let nodes: MindmapNode[] = [];
|
||||
let cnt = 0;
|
||||
let elements = {};
|
||||
export const clear = () => {
|
||||
let elements: Record<number, D3Element> = {};
|
||||
|
||||
const clear = () => {
|
||||
nodes = [];
|
||||
cnt = 0;
|
||||
elements = {};
|
||||
};
|
||||
|
||||
const getParent = function (level) {
|
||||
const getParent = function (level: number) {
|
||||
for (let i = nodes.length - 1; i >= 0; i--) {
|
||||
if (nodes[i].level < level) {
|
||||
return nodes[i];
|
||||
@@ -23,34 +25,32 @@ const getParent = function (level) {
|
||||
return null;
|
||||
};
|
||||
|
||||
export const getMindmap = () => {
|
||||
const getMindmap = () => {
|
||||
return nodes.length > 0 ? nodes[0] : null;
|
||||
};
|
||||
export const addNode = (level, id, descr, type) => {
|
||||
|
||||
const addNode = (level: number, id: string, descr: string, type: number) => {
|
||||
log.info('addNode', level, id, descr, type);
|
||||
const conf = getConfig();
|
||||
let padding: number = conf.mindmap?.padding ?? defaultConfig.mindmap.padding;
|
||||
switch (type) {
|
||||
case nodeType.ROUNDED_RECT:
|
||||
case nodeType.RECT:
|
||||
case nodeType.HEXAGON:
|
||||
padding *= 2;
|
||||
}
|
||||
|
||||
const node = {
|
||||
id: cnt++,
|
||||
nodeId: sanitizeText(id),
|
||||
nodeId: sanitizeText(id, conf),
|
||||
level,
|
||||
descr: sanitizeText(descr),
|
||||
descr: sanitizeText(descr, conf),
|
||||
type,
|
||||
children: [],
|
||||
width: getConfig().mindmap.maxNodeWidth,
|
||||
};
|
||||
switch (node.type) {
|
||||
case nodeType.ROUNDED_RECT:
|
||||
node.padding = 2 * conf.mindmap.padding;
|
||||
break;
|
||||
case nodeType.RECT:
|
||||
node.padding = 2 * conf.mindmap.padding;
|
||||
break;
|
||||
case nodeType.HEXAGON:
|
||||
node.padding = 2 * conf.mindmap.padding;
|
||||
break;
|
||||
default:
|
||||
node.padding = conf.mindmap.padding;
|
||||
}
|
||||
width: conf.mindmap?.maxNodeWidth ?? defaultConfig.mindmap.maxNodeWidth,
|
||||
padding,
|
||||
} satisfies MindmapNode;
|
||||
|
||||
const parent = getParent(level);
|
||||
if (parent) {
|
||||
parent.children.push(node);
|
||||
@@ -62,22 +62,14 @@ export const addNode = (level, id, descr, type) => {
|
||||
nodes.push(node);
|
||||
} else {
|
||||
// Syntax error ... there can only bee one root
|
||||
let error = new Error(
|
||||
throw new Error(
|
||||
'There can be only one root. No parent could be found for ("' + node.descr + '")'
|
||||
);
|
||||
error.hash = {
|
||||
text: 'branch ' + name,
|
||||
token: 'branch ' + name,
|
||||
line: '1',
|
||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||
expected: ['"checkout ' + name + '"'],
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const nodeType = {
|
||||
const nodeType = {
|
||||
DEFAULT: 0,
|
||||
NO_BORDER: 0,
|
||||
ROUNDED_RECT: 1,
|
||||
@@ -88,7 +80,7 @@ export const nodeType = {
|
||||
HEXAGON: 6,
|
||||
};
|
||||
|
||||
export const getType = (startStr, endStr) => {
|
||||
const getType = (startStr: string, endStr: string): number => {
|
||||
log.debug('In get type', startStr, endStr);
|
||||
switch (startStr) {
|
||||
case '[':
|
||||
@@ -108,21 +100,25 @@ export const getType = (startStr, endStr) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const setElementForId = (id, element) => {
|
||||
const setElementForId = (id: number, element: D3Element) => {
|
||||
elements[id] = element;
|
||||
};
|
||||
|
||||
export const decorateNode = (decoration) => {
|
||||
const node = nodes[nodes.length - 1];
|
||||
if (decoration && decoration.icon) {
|
||||
node.icon = sanitizeText(decoration.icon);
|
||||
const decorateNode = (decoration?: { class?: string; icon?: string }) => {
|
||||
if (!decoration) {
|
||||
return;
|
||||
}
|
||||
if (decoration && decoration.class) {
|
||||
node.class = sanitizeText(decoration.class);
|
||||
const config = getConfig();
|
||||
const node = nodes[nodes.length - 1];
|
||||
if (decoration.icon) {
|
||||
node.icon = sanitizeText(decoration.icon, config);
|
||||
}
|
||||
if (decoration.class) {
|
||||
node.class = sanitizeText(decoration.class, config);
|
||||
}
|
||||
};
|
||||
|
||||
export const type2Str = (type) => {
|
||||
const type2Str = (type: number) => {
|
||||
switch (type) {
|
||||
case nodeType.DEFAULT:
|
||||
return 'no-border';
|
||||
@@ -137,19 +133,27 @@ export const type2Str = (type) => {
|
||||
case nodeType.BANG:
|
||||
return 'bang';
|
||||
case nodeType.HEXAGON:
|
||||
return 'hexgon';
|
||||
return 'hexgon'; // cspell: disable-line
|
||||
default:
|
||||
return 'no-border';
|
||||
}
|
||||
};
|
||||
|
||||
export let parseError;
|
||||
export const setErrorHandler = (handler) => {
|
||||
parseError = handler;
|
||||
};
|
||||
|
||||
// Expose logger to grammar
|
||||
export const getLogger = () => log;
|
||||
const getLogger = () => log;
|
||||
const getElementById = (id: number) => elements[id];
|
||||
|
||||
export const getNodeById = (id) => nodes[id];
|
||||
export const getElementById = (id) => elements[id];
|
||||
const db = {
|
||||
clear,
|
||||
addNode,
|
||||
getMindmap,
|
||||
nodeType,
|
||||
getType,
|
||||
setElementForId,
|
||||
decorateNode,
|
||||
type2Str,
|
||||
getLogger,
|
||||
getElementById,
|
||||
} as const;
|
||||
|
||||
export default db;
|
||||
@@ -1,36 +1,53 @@
|
||||
/** Created by knut on 14-12-11. */
|
||||
import { select } from 'd3';
|
||||
import { log } from '../../logger.js';
|
||||
import { getConfig } from '../../diagram-api/diagramAPI.js';
|
||||
import { setupGraphViewbox } from '../../setupGraphViewbox.js';
|
||||
import svgDraw from './svgDraw.js';
|
||||
import cytoscape from 'cytoscape/dist/cytoscape.umd.js';
|
||||
import cytoscape from 'cytoscape';
|
||||
// @ts-expect-error No types available
|
||||
import coseBilkent from 'cytoscape-cose-bilkent';
|
||||
import * as db from './mindmapDb.js';
|
||||
import { select } from 'd3';
|
||||
import type { MermaidConfig } from '../../config.type.js';
|
||||
import { getConfig } from '../../diagram-api/diagramAPI.js';
|
||||
import type { DrawDefinition } from '../../diagram-api/types.js';
|
||||
import { log } from '../../logger.js';
|
||||
import type { D3Element } from '../../mermaidAPI.js';
|
||||
import { selectSvgElement } from '../../rendering-util/selectSvgElement.js';
|
||||
import { setupGraphViewbox } from '../../setupGraphViewbox.js';
|
||||
import type { FilledMindMapNode, MindmapDB, MindmapNode } from './mindmapTypes.js';
|
||||
import { drawNode, positionNode } from './svgDraw.js';
|
||||
import defaultConfig from '../../defaultConfig.js';
|
||||
|
||||
// Inject the layout algorithm into cytoscape
|
||||
cytoscape.use(coseBilkent);
|
||||
|
||||
/**
|
||||
* @param {any} svg The svg element to draw the diagram onto
|
||||
* @param {object} mindmap The mindmap data and hierarchy
|
||||
* @param section
|
||||
* @param {object} conf The configuration object
|
||||
*/
|
||||
function drawNodes(svg, mindmap, section, conf) {
|
||||
svgDraw.drawNode(svg, mindmap, section, conf);
|
||||
function drawNodes(
|
||||
db: MindmapDB,
|
||||
svg: D3Element,
|
||||
mindmap: FilledMindMapNode,
|
||||
section: number,
|
||||
conf: MermaidConfig
|
||||
) {
|
||||
drawNode(db, svg, mindmap, section, conf);
|
||||
if (mindmap.children) {
|
||||
mindmap.children.forEach((child, index) => {
|
||||
drawNodes(svg, child, section < 0 ? index : section, conf);
|
||||
drawNodes(db, svg, child, section < 0 ? index : section, conf);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param edgesEl
|
||||
* @param cy
|
||||
*/
|
||||
function drawEdges(edgesEl, cy) {
|
||||
declare module 'cytoscape' {
|
||||
interface EdgeSingular {
|
||||
_private: {
|
||||
bodyBounds: unknown;
|
||||
rscratch: {
|
||||
startX: number;
|
||||
startY: number;
|
||||
midX: number;
|
||||
midY: number;
|
||||
endX: number;
|
||||
endY: number;
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function drawEdges(edgesEl: D3Element, cy: cytoscape.Core) {
|
||||
cy.edges().map((edge, id) => {
|
||||
const data = edge.data();
|
||||
if (edge[0]._private.bodyBounds) {
|
||||
@@ -47,17 +64,11 @@ function drawEdges(edgesEl, cy) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mindmap The mindmap data and hierarchy
|
||||
* @param cy
|
||||
* @param conf The configuration object
|
||||
* @param level
|
||||
*/
|
||||
function addNodes(mindmap, cy, conf, level) {
|
||||
function addNodes(mindmap: MindmapNode, cy: cytoscape.Core, conf: MermaidConfig, level: number) {
|
||||
cy.add({
|
||||
group: 'nodes',
|
||||
data: {
|
||||
id: mindmap.id,
|
||||
id: mindmap.id.toString(),
|
||||
labelText: mindmap.descr,
|
||||
height: mindmap.height,
|
||||
width: mindmap.width,
|
||||
@@ -67,8 +78,8 @@ function addNodes(mindmap, cy, conf, level) {
|
||||
type: mindmap.type,
|
||||
},
|
||||
position: {
|
||||
x: mindmap.x,
|
||||
y: mindmap.y,
|
||||
x: mindmap.x!,
|
||||
y: mindmap.y!,
|
||||
},
|
||||
});
|
||||
if (mindmap.children) {
|
||||
@@ -88,12 +99,7 @@ function addNodes(mindmap, cy, conf, level) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @param conf
|
||||
* @param cy
|
||||
*/
|
||||
function layoutMindmap(node, conf) {
|
||||
function layoutMindmap(node: MindmapNode, conf: MermaidConfig): Promise<cytoscape.Core> {
|
||||
return new Promise((resolve) => {
|
||||
// Add temporary render element
|
||||
const renderEl = select('body').append('div').attr('id', 'cy').attr('style', 'display:none');
|
||||
@@ -122,8 +128,8 @@ function layoutMindmap(node, conf) {
|
||||
|
||||
cy.layout({
|
||||
name: 'cose-bilkent',
|
||||
// @ts-ignore Types for cose-bilkent are not correct?
|
||||
quality: 'proof',
|
||||
// headless: true,
|
||||
styleEnabled: false,
|
||||
animate: false,
|
||||
}).run();
|
||||
@@ -133,18 +139,13 @@ function layoutMindmap(node, conf) {
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param node
|
||||
* @param cy
|
||||
* @param positionedMindmap
|
||||
* @param conf
|
||||
*/
|
||||
function positionNodes(cy) {
|
||||
|
||||
function positionNodes(db: MindmapDB, cy: cytoscape.Core) {
|
||||
cy.nodes().map((node, id) => {
|
||||
const data = node.data();
|
||||
data.x = node.position().x;
|
||||
data.y = node.position().y;
|
||||
svgDraw.positionNode(data);
|
||||
positionNode(db, data);
|
||||
const el = db.getElementById(data.nodeId);
|
||||
log.info('Id:', id, 'Position: (', node.position().x, ', ', node.position().y, ')', data);
|
||||
el.attr(
|
||||
@@ -155,38 +156,19 @@ function positionNodes(cy) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws a an info picture in the tag with id: id based on the graph definition in text.
|
||||
*
|
||||
* @param {any} text
|
||||
* @param {any} id
|
||||
* @param {any} version
|
||||
* @param diagObj
|
||||
*/
|
||||
export const draw: DrawDefinition = async (text, id, _version, diagObj) => {
|
||||
log.debug('Rendering mindmap diagram\n' + text);
|
||||
|
||||
const db = diagObj.db as MindmapDB;
|
||||
const mm = db.getMindmap();
|
||||
if (!mm) {
|
||||
return;
|
||||
}
|
||||
|
||||
export const draw = async (text, id, version, diagObj) => {
|
||||
const conf = getConfig();
|
||||
|
||||
conf.htmlLabels = false;
|
||||
|
||||
log.debug('Rendering mindmap diagram\n' + text, diagObj.parser);
|
||||
|
||||
const securityLevel = getConfig().securityLevel;
|
||||
// Handle root and Document for when rendering in sandbox mode
|
||||
let sandboxElement;
|
||||
if (securityLevel === 'sandbox') {
|
||||
sandboxElement = select('#i' + id);
|
||||
}
|
||||
const root =
|
||||
securityLevel === 'sandbox'
|
||||
? select(sandboxElement.nodes()[0].contentDocument.body)
|
||||
: select('body');
|
||||
// Parse the graph definition
|
||||
|
||||
const svg = root.select('#' + id);
|
||||
|
||||
svg.append('g');
|
||||
const mm = diagObj.db.getMindmap();
|
||||
const svg = selectSvgElement(id);
|
||||
|
||||
// Draw the graph and start with drawing the nodes without proper position
|
||||
// this gives us the size of the nodes and we can set the positions later
|
||||
@@ -195,18 +177,23 @@ export const draw = async (text, id, version, diagObj) => {
|
||||
edgesElem.attr('class', 'mindmap-edges');
|
||||
const nodesElem = svg.append('g');
|
||||
nodesElem.attr('class', 'mindmap-nodes');
|
||||
drawNodes(nodesElem, mm, -1, conf);
|
||||
drawNodes(db, nodesElem, mm as FilledMindMapNode, -1, conf);
|
||||
|
||||
// Next step is to layout the mindmap, giving each node a position
|
||||
|
||||
const cy = await layoutMindmap(mm, conf);
|
||||
|
||||
// // After this we can draw, first the edges and the then nodes with the correct position
|
||||
drawEdges(edgesElem, cy, conf);
|
||||
positionNodes(cy, conf);
|
||||
// After this we can draw, first the edges and the then nodes with the correct position
|
||||
drawEdges(edgesElem, cy);
|
||||
positionNodes(db, cy);
|
||||
|
||||
// Setup the view box and size of the svg element
|
||||
setupGraphViewbox(undefined, svg, conf.mindmap.padding, conf.mindmap.useMaxWidth);
|
||||
setupGraphViewbox(
|
||||
undefined,
|
||||
svg,
|
||||
conf.mindmap?.padding ?? defaultConfig.mindmap.padding,
|
||||
conf.mindmap?.useMaxWidth ?? defaultConfig.mindmap.useMaxWidth
|
||||
);
|
||||
};
|
||||
|
||||
export default {
|
||||
22
packages/mermaid/src/diagrams/mindmap/mindmapTypes.ts
Normal file
22
packages/mermaid/src/diagrams/mindmap/mindmapTypes.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { RequiredDeep } from 'type-fest';
|
||||
import type mindmapDb from './mindmapDb.js';
|
||||
|
||||
export interface MindmapNode {
|
||||
id: number;
|
||||
nodeId: string;
|
||||
level: number;
|
||||
descr: string;
|
||||
type: number;
|
||||
children: MindmapNode[];
|
||||
width: number;
|
||||
padding: number;
|
||||
section?: number;
|
||||
height?: number;
|
||||
class?: string;
|
||||
icon?: string;
|
||||
x?: number;
|
||||
y?: number;
|
||||
}
|
||||
|
||||
export type FilledMindMapNode = RequiredDeep<MindmapNode>;
|
||||
export type MindmapDB = typeof mindmapDb;
|
||||
@@ -1,6 +1,8 @@
|
||||
// @ts-expect-error Incorrect khroma types
|
||||
import { darken, lighten, isDark } from 'khroma';
|
||||
import type { DiagramStylesProvider } from '../../diagram-api/types.js';
|
||||
|
||||
const genSections = (options) => {
|
||||
const genSections: DiagramStylesProvider = (options) => {
|
||||
let sections = '';
|
||||
|
||||
for (let i = 0; i < options.THEME_COLOR_LIMIT; i++) {
|
||||
@@ -49,7 +51,8 @@ const genSections = (options) => {
|
||||
return sections;
|
||||
};
|
||||
|
||||
const getStyles = (options) =>
|
||||
// TODO: These options seem incorrect.
|
||||
const getStyles: DiagramStylesProvider = (options) =>
|
||||
`
|
||||
.edge {
|
||||
stroke-width: 3;
|
||||
@@ -1,55 +1,20 @@
|
||||
import { select } from 'd3';
|
||||
import * as db from './mindmapDb.js';
|
||||
import type { D3Element } from '../../mermaidAPI.js';
|
||||
import { createText } from '../../rendering-util/createText.js';
|
||||
import type { FilledMindMapNode, MindmapDB } from './mindmapTypes.js';
|
||||
import type { Point } from '../../types.js';
|
||||
import { parseFontSize } from '../../utils.js';
|
||||
import type { MermaidConfig } from '../../config.type.js';
|
||||
|
||||
const MAX_SECTIONS = 12;
|
||||
|
||||
/**
|
||||
* @param {string} text The text to be wrapped
|
||||
* @param {number} width The max width of the text
|
||||
*/
|
||||
function wrap(text, width) {
|
||||
text.each(function () {
|
||||
var text = select(this),
|
||||
words = text
|
||||
.text()
|
||||
.split(/(\s+|<br\/>)/)
|
||||
.reverse(),
|
||||
word,
|
||||
line = [],
|
||||
lineHeight = 1.1, // ems
|
||||
y = text.attr('y'),
|
||||
dy = parseFloat(text.attr('dy')),
|
||||
tspan = text
|
||||
.text(null)
|
||||
.append('tspan')
|
||||
.attr('x', 0)
|
||||
.attr('y', y)
|
||||
.attr('dy', dy + 'em');
|
||||
for (let j = 0; j < words.length; j++) {
|
||||
word = words[words.length - 1 - j];
|
||||
line.push(word);
|
||||
tspan.text(line.join(' ').trim());
|
||||
if (tspan.node().getComputedTextLength() > width || word === '<br/>') {
|
||||
line.pop();
|
||||
tspan.text(line.join(' ').trim());
|
||||
if (word === '<br/>') {
|
||||
line = [''];
|
||||
} else {
|
||||
line = [word];
|
||||
}
|
||||
type ShapeFunction = (
|
||||
db: MindmapDB,
|
||||
elem: D3Element,
|
||||
node: FilledMindMapNode,
|
||||
section?: number
|
||||
) => void;
|
||||
|
||||
tspan = text
|
||||
.append('tspan')
|
||||
.attr('x', 0)
|
||||
.attr('y', y)
|
||||
.attr('dy', lineHeight + 'em')
|
||||
.text(word);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const defaultBkg = function (elem, node, section) {
|
||||
const defaultBkg: ShapeFunction = function (db, elem, node, section) {
|
||||
const rd = 5;
|
||||
elem
|
||||
.append('path')
|
||||
@@ -71,7 +36,7 @@ const defaultBkg = function (elem, node, section) {
|
||||
.attr('y2', node.height);
|
||||
};
|
||||
|
||||
const rectBkg = function (elem, node) {
|
||||
const rectBkg: ShapeFunction = function (db, elem, node) {
|
||||
elem
|
||||
.append('rect')
|
||||
.attr('id', 'node-' + node.id)
|
||||
@@ -80,7 +45,7 @@ const rectBkg = function (elem, node) {
|
||||
.attr('width', node.width);
|
||||
};
|
||||
|
||||
const cloudBkg = function (elem, node) {
|
||||
const cloudBkg: ShapeFunction = function (db, elem, node) {
|
||||
const w = node.width;
|
||||
const h = node.height;
|
||||
const r1 = 0.15 * w;
|
||||
@@ -111,7 +76,7 @@ const cloudBkg = function (elem, node) {
|
||||
);
|
||||
};
|
||||
|
||||
const bangBkg = function (elem, node) {
|
||||
const bangBkg: ShapeFunction = function (db, elem, node) {
|
||||
const w = node.width;
|
||||
const h = node.height;
|
||||
const r = 0.15 * w;
|
||||
@@ -143,7 +108,7 @@ const bangBkg = function (elem, node) {
|
||||
);
|
||||
};
|
||||
|
||||
const circleBkg = function (elem, node) {
|
||||
const circleBkg: ShapeFunction = function (db, elem, node) {
|
||||
elem
|
||||
.append('circle')
|
||||
.attr('id', 'node-' + node.id)
|
||||
@@ -151,15 +116,13 @@ const circleBkg = function (elem, node) {
|
||||
.attr('r', node.width / 2);
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param parent
|
||||
* @param w
|
||||
* @param h
|
||||
* @param points
|
||||
* @param node
|
||||
*/
|
||||
function insertPolygonShape(parent, w, h, points, node) {
|
||||
function insertPolygonShape(
|
||||
parent: D3Element,
|
||||
w: number,
|
||||
h: number,
|
||||
points: Point[],
|
||||
node: FilledMindMapNode
|
||||
) {
|
||||
return parent
|
||||
.insert('polygon', ':first-child')
|
||||
.attr(
|
||||
@@ -173,12 +136,16 @@ function insertPolygonShape(parent, w, h, points, node) {
|
||||
.attr('transform', 'translate(' + (node.width - w) / 2 + ', ' + h + ')');
|
||||
}
|
||||
|
||||
const hexagonBkg = function (elem, node) {
|
||||
const hexagonBkg: ShapeFunction = function (
|
||||
_db: MindmapDB,
|
||||
elem: D3Element,
|
||||
node: FilledMindMapNode
|
||||
) {
|
||||
const h = node.height;
|
||||
const f = 4;
|
||||
const m = h / f;
|
||||
const w = node.width - node.padding + 2 * m;
|
||||
const points = [
|
||||
const points: Point[] = [
|
||||
{ x: m, y: 0 },
|
||||
{ x: w - m, y: 0 },
|
||||
{ x: w, y: -h / 2 },
|
||||
@@ -186,10 +153,10 @@ const hexagonBkg = function (elem, node) {
|
||||
{ x: m, y: -h },
|
||||
{ x: 0, y: -h / 2 },
|
||||
];
|
||||
const shapeSvg = insertPolygonShape(elem, w, h, points, node);
|
||||
insertPolygonShape(elem, w, h, points, node);
|
||||
};
|
||||
|
||||
const roundedRectBkg = function (elem, node) {
|
||||
const roundedRectBkg: ShapeFunction = function (db, elem, node) {
|
||||
elem
|
||||
.append('rect')
|
||||
.attr('id', 'node-' + node.id)
|
||||
@@ -201,13 +168,20 @@ const roundedRectBkg = function (elem, node) {
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {object} elem The D3 dom element in which the node is to be added
|
||||
* @param {object} node The node to be added
|
||||
* @param fullSection
|
||||
* @param {object} conf The configuration object
|
||||
* @returns {number} The height nodes dom element
|
||||
* @param db - The database
|
||||
* @param elem - The D3 dom element in which the node is to be added
|
||||
* @param node - The node to be added
|
||||
* @param fullSection - ?
|
||||
* @param conf - The configuration object
|
||||
* @returns The height nodes dom element
|
||||
*/
|
||||
export const drawNode = function (elem, node, fullSection, conf) {
|
||||
export const drawNode = function (
|
||||
db: MindmapDB,
|
||||
elem: D3Element,
|
||||
node: FilledMindMapNode,
|
||||
fullSection: number,
|
||||
conf: MermaidConfig
|
||||
): number {
|
||||
const htmlLabels = conf.htmlLabels;
|
||||
const section = fullSection % (MAX_SECTIONS - 1);
|
||||
const nodeElem = elem.append('g');
|
||||
@@ -235,10 +209,9 @@ export const drawNode = function (elem, node, fullSection, conf) {
|
||||
.attr('dominant-baseline', 'middle')
|
||||
.attr('text-anchor', 'middle');
|
||||
}
|
||||
// .call(wrap, node.width);
|
||||
const bbox = textElem.node().getBBox();
|
||||
const fontSize = conf.fontSize.replace ? conf.fontSize.replace('px', '') : conf.fontSize;
|
||||
node.height = bbox.height + fontSize * 1.1 * 0.5 + node.padding;
|
||||
const [fontSize] = parseFontSize(conf.fontSize);
|
||||
node.height = bbox.height + fontSize! * 1.1 * 0.5 + node.padding;
|
||||
node.width = bbox.width + 2 * node.padding;
|
||||
if (node.icon) {
|
||||
if (node.type === db.nodeType.CIRCLE) {
|
||||
@@ -294,60 +267,34 @@ export const drawNode = function (elem, node, fullSection, conf) {
|
||||
|
||||
switch (node.type) {
|
||||
case db.nodeType.DEFAULT:
|
||||
defaultBkg(bkgElem, node, section, conf);
|
||||
defaultBkg(db, bkgElem, node, section);
|
||||
break;
|
||||
case db.nodeType.ROUNDED_RECT:
|
||||
roundedRectBkg(bkgElem, node, section, conf);
|
||||
roundedRectBkg(db, bkgElem, node, section);
|
||||
break;
|
||||
case db.nodeType.RECT:
|
||||
rectBkg(bkgElem, node, section, conf);
|
||||
rectBkg(db, bkgElem, node, section);
|
||||
break;
|
||||
case db.nodeType.CIRCLE:
|
||||
bkgElem.attr('transform', 'translate(' + node.width / 2 + ', ' + +node.height / 2 + ')');
|
||||
circleBkg(bkgElem, node, section, conf);
|
||||
circleBkg(db, bkgElem, node, section);
|
||||
break;
|
||||
case db.nodeType.CLOUD:
|
||||
cloudBkg(bkgElem, node, section, conf);
|
||||
cloudBkg(db, bkgElem, node, section);
|
||||
break;
|
||||
case db.nodeType.BANG:
|
||||
bangBkg(bkgElem, node, section, conf);
|
||||
bangBkg(db, bkgElem, node, section);
|
||||
break;
|
||||
case db.nodeType.HEXAGON:
|
||||
hexagonBkg(bkgElem, node, section, conf);
|
||||
hexagonBkg(db, bkgElem, node, section);
|
||||
break;
|
||||
}
|
||||
|
||||
// Position the node to its coordinate
|
||||
// if (typeof node.x !== 'undefined' && typeof node.y !== 'undefined') {
|
||||
// nodeElem.attr('transform', 'translate(' + node.x + ',' + node.y + ')');
|
||||
// }
|
||||
db.setElementForId(node.id, nodeElem);
|
||||
return node.height;
|
||||
};
|
||||
|
||||
export const drawEdge = function drawEdge(edgesElem, mindmap, parent, depth, fullSection) {
|
||||
const section = fullSection % (MAX_SECTIONS - 1);
|
||||
const sx = parent.x + parent.width / 2;
|
||||
const sy = parent.y + parent.height / 2;
|
||||
const ex = mindmap.x + mindmap.width / 2;
|
||||
const ey = mindmap.y + mindmap.height / 2;
|
||||
const mx = ex > sx ? sx + Math.abs(sx - ex) / 2 : sx - Math.abs(sx - ex) / 2;
|
||||
const my = ey > sy ? sy + Math.abs(sy - ey) / 2 : sy - Math.abs(sy - ey) / 2;
|
||||
const qx = ex > sx ? Math.abs(sx - mx) / 2 + sx : -Math.abs(sx - mx) / 2 + sx;
|
||||
const qy = ey > sy ? Math.abs(sy - my) / 2 + sy : -Math.abs(sy - my) / 2 + sy;
|
||||
|
||||
edgesElem
|
||||
.append('path')
|
||||
.attr(
|
||||
'd',
|
||||
parent.direction === 'TB' || parent.direction === 'BT'
|
||||
? `M${sx},${sy} Q${sx},${qy} ${mx},${my} T${ex},${ey}`
|
||||
: `M${sx},${sy} Q${qx},${sy} ${mx},${my} T${ex},${ey}`
|
||||
)
|
||||
.attr('class', 'edge section-edge-' + section + ' edge-depth-' + depth);
|
||||
};
|
||||
|
||||
export const positionNode = function (node) {
|
||||
export const positionNode = function (db: MindmapDB, node: FilledMindMapNode) {
|
||||
const nodeElem = db.getElementById(node.id);
|
||||
|
||||
const x = node.x || 0;
|
||||
@@ -355,5 +302,3 @@ export const positionNode = function (node) {
|
||||
// Position the node to its coordinate
|
||||
nodeElem.attr('transform', 'translate(' + x + ',' + y + ')');
|
||||
};
|
||||
|
||||
export default { drawNode, positionNode, drawEdge };
|
||||
@@ -51,7 +51,7 @@ export interface PieDB extends DiagramDB {
|
||||
getDiagramTitle: () => string;
|
||||
setAccTitle: (title: string) => void;
|
||||
getAccTitle: () => string;
|
||||
setAccDescription: (describetion: string) => void;
|
||||
setAccDescription: (description: string) => void;
|
||||
getAccDescription: () => string;
|
||||
|
||||
// diagram db
|
||||
|
||||
@@ -53,7 +53,7 @@ export interface QuadrantBuildType {
|
||||
borderLines?: QuadrantLineType[];
|
||||
}
|
||||
|
||||
export interface quadrantBuilderData {
|
||||
export interface QuadrantBuilderData {
|
||||
titleText: string;
|
||||
quadrant1Text: string;
|
||||
quadrant2Text: string;
|
||||
@@ -116,7 +116,7 @@ interface CalculateSpaceData {
|
||||
export class QuadrantBuilder {
|
||||
private config: QuadrantBuilderConfig;
|
||||
private themeConfig: QuadrantBuilderThemeConfig;
|
||||
private data: quadrantBuilderData;
|
||||
private data: QuadrantBuilderData;
|
||||
|
||||
constructor() {
|
||||
this.config = this.getDefaultConfig();
|
||||
@@ -124,7 +124,7 @@ export class QuadrantBuilder {
|
||||
this.data = this.getDefaultData();
|
||||
}
|
||||
|
||||
getDefaultData(): quadrantBuilderData {
|
||||
getDefaultData(): QuadrantBuilderData {
|
||||
return {
|
||||
titleText: '',
|
||||
quadrant1Text: '',
|
||||
@@ -194,7 +194,7 @@ export class QuadrantBuilder {
|
||||
log.info('clear called');
|
||||
}
|
||||
|
||||
setData(data: Partial<quadrantBuilderData>) {
|
||||
setData(data: Partial<QuadrantBuilderData>) {
|
||||
this.data = { ...this.data, ...data };
|
||||
}
|
||||
|
||||
|
||||
@@ -362,6 +362,8 @@ export const draw = (text, id, _version, diagObj) => {
|
||||
svg.attr('viewBox', `${svgBounds.x - padding} ${svgBounds.y - padding} ${width} ${height}`);
|
||||
};
|
||||
|
||||
// cspell:ignore txts
|
||||
|
||||
export default {
|
||||
draw,
|
||||
};
|
||||
|
||||
@@ -11,14 +11,13 @@
|
||||
%lex
|
||||
|
||||
%options case-insensitive
|
||||
%options easy_keword_rules
|
||||
|
||||
%x escaped_text
|
||||
%x csv
|
||||
|
||||
// as per section 6.1 of RFC 2234 [2]
|
||||
COMMA \u002C
|
||||
CR \u000D
|
||||
CR \u000D
|
||||
LF \u000A
|
||||
CRLF \u000D\u000A
|
||||
ESCAPED_QUOTE \u0022
|
||||
@@ -32,7 +31,7 @@ TEXTDATA [\u0020-\u0021\u0023-\u002B\u002D-\u007E]
|
||||
<INITIAL,csv>({CRLF}|{LF}) { return 'NEWLINE' }
|
||||
<INITIAL,csv>{COMMA} { return 'COMMA' }
|
||||
<INITIAL,csv>{DQUOTE} { this.pushState('escaped_text'); return 'DQUOTE'; }
|
||||
<INITIAL,csv>{TEXTDATA}* { return 'NON_ESCAPED_TEXT' }
|
||||
<INITIAL,csv>{TEXTDATA}* { return 'NON_ESCAPED_TEXT' }
|
||||
<INITIAL,csv,escaped_text>{DQUOTE}(?!{DQUOTE}) {this.popState('escaped_text'); return 'DQUOTE'; } // unescaped DQUOTE closes string
|
||||
<INITIAL,csv,escaped_text>({TEXTDATA}|{COMMA}|{CR}|{LF}|{DQUOTE}{DQUOTE})* { return 'ESCAPED_TEXT'; }
|
||||
|
||||
@@ -65,5 +64,3 @@ field
|
||||
escaped: DQUOTE ESCAPED_TEXT DQUOTE { $$=$ESCAPED_TEXT; };
|
||||
|
||||
non_escaped: NON_ESCAPED_TEXT { $$=$NON_ESCAPED_TEXT; };
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { Diagram } from '../../Diagram.js';
|
||||
import { getConfig, defaultConfig } from '../../diagram-api/diagramAPI.js';
|
||||
|
||||
import {
|
||||
select as d3select,
|
||||
scaleOrdinal as d3scaleOrdinal,
|
||||
|
||||
@@ -209,7 +209,7 @@ Note right of Bob: Bob thinks
|
||||
Bob-->Alice: I am good thanks!`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers
|
||||
expect(diagram.db.showSequenceNumbers()).toBe(false);
|
||||
});
|
||||
it('should show sequence numbers when autonumber is enabled', async () => {
|
||||
@@ -221,7 +221,7 @@ Note right of Bob: Bob thinks
|
||||
Bob-->Alice: I am good thanks!`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers
|
||||
expect(diagram.db.showSequenceNumbers()).toBe(true);
|
||||
});
|
||||
|
||||
@@ -1648,7 +1648,7 @@ participant Alice`;
|
||||
// mermaidAPI.reinitialize({ sequence: { textPlacement: textPlacement } });
|
||||
await mermaidAPI.parse(str);
|
||||
// diagram.renderer.setConf(mermaidAPI.getConfig().sequence);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
@@ -1679,7 +1679,7 @@ Note over Alice: Alice thinks
|
||||
|
||||
expect(mermaidAPI.getConfig().sequence.mirrorActors).toBeFalsy();
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
@@ -1695,7 +1695,7 @@ participant Alice
|
||||
Note left of Alice: Alice thinks`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(-(conf.width / 2) - conf.actorMargin / 2);
|
||||
@@ -1711,7 +1711,7 @@ participant Alice
|
||||
Note right of Alice: Alice thinks`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
@@ -1726,7 +1726,7 @@ sequenceDiagram
|
||||
Alice->Bob: Hello Bob, how are you?`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
@@ -1744,7 +1744,7 @@ end
|
||||
Alice->Bob: Hello Bob, how are you?`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
@@ -1759,7 +1759,7 @@ sequenceDiagram
|
||||
Alice->Bob: Hello Bob, how are you?`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
const mermaid = mermaidAPI.getConfig();
|
||||
@@ -1779,7 +1779,7 @@ wrap
|
||||
Alice->Bob: Hello Bob, how are you?`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const msgs = diagram.db.getMessages();
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
@@ -1800,7 +1800,7 @@ Note over Bob,Alice: Looks back
|
||||
`;
|
||||
// mermaidAPI.initialize({logLevel:0})
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
@@ -1815,7 +1815,7 @@ Alice->Bob: Hello Bob, how are you?
|
||||
Bob->Alice: Fine!`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
@@ -1831,7 +1831,7 @@ Note right of Bob: Bob thinks
|
||||
Bob->Alice: Fine!`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
@@ -1850,7 +1850,7 @@ Note left of Alice: Bob thinks
|
||||
Bob->Alice: Fine!`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(-(conf.width / 2) - conf.actorMargin / 2);
|
||||
@@ -1867,7 +1867,7 @@ Note left of Alice: Bob thinks
|
||||
Bob->>Alice: Fine!`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
const msgs = diagram.db.getMessages();
|
||||
@@ -1888,7 +1888,7 @@ Note left of Alice: Bob thinks
|
||||
Bob->>Alice: Fine!`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
const msgs = diagram.db.getMessages();
|
||||
@@ -1911,7 +1911,7 @@ Note left of Alice: Bob thinks
|
||||
Bob->>Alice: Fine!`;
|
||||
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
const msgs = diagram.db.getMessages();
|
||||
@@ -1933,7 +1933,7 @@ Note left of Alice: Bob thinks
|
||||
Bob->>Alice: Fine!`;
|
||||
// mermaidAPI.initialize({ logLevel: 0 });
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
const msgs = diagram.db.getMessages();
|
||||
@@ -1957,7 +1957,7 @@ loop Cheers
|
||||
Bob->Alice: Fine!
|
||||
end`;
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
@@ -1975,7 +1975,7 @@ end`;
|
||||
end
|
||||
`;
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
expect(bounds.starty).toBe(0);
|
||||
@@ -2022,7 +2022,7 @@ sequenceDiagram
|
||||
participant Alice`;
|
||||
diagram.renderer.bounds.init();
|
||||
await mermaidAPI.parse(str);
|
||||
diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram);
|
||||
|
||||
const { bounds, models } = diagram.renderer.bounds.getBounds();
|
||||
expect(bounds.startx).toBe(0);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// @ts-nocheck TODO: fix file
|
||||
import { select } from 'd3';
|
||||
import svgDraw, { ACTOR_TYPE_WIDTH, drawText, fixLifeLineHeights } from './svgDraw.js';
|
||||
import svgDraw, { drawKatex, ACTOR_TYPE_WIDTH, drawText, fixLifeLineHeights } from './svgDraw.js';
|
||||
import { log } from '../../logger.js';
|
||||
import common from '../common/common.js';
|
||||
import common, { calculateMathMLDimensions, hasKatex } from '../common/common.js';
|
||||
import * as svgDrawCommon from '../common/svgDrawCommon.js';
|
||||
import { getConfig } from '../../diagram-api/diagramAPI.js';
|
||||
import assignWithDepth from '../../assignWithDepth.js';
|
||||
@@ -237,7 +237,7 @@ interface NoteModel {
|
||||
* @param elem - The diagram to draw to.
|
||||
* @param noteModel - Note model options.
|
||||
*/
|
||||
const drawNote = function (elem: any, noteModel: NoteModel) {
|
||||
const drawNote = async function (elem: any, noteModel: NoteModel) {
|
||||
bounds.bumpVerticalPos(conf.boxMargin);
|
||||
noteModel.height = conf.boxMargin;
|
||||
noteModel.starty = bounds.getVerticalPos();
|
||||
@@ -263,7 +263,7 @@ const drawNote = function (elem: any, noteModel: NoteModel) {
|
||||
textObj.textMargin = conf.noteMargin;
|
||||
textObj.valign = 'center';
|
||||
|
||||
const textElem = drawText(g, textObj);
|
||||
const textElem = hasKatex(textObj.text) ? await drawKatex(g, textObj) : drawText(g, textObj);
|
||||
|
||||
const textHeight = Math.round(
|
||||
textElem
|
||||
@@ -311,15 +311,20 @@ const actorFont = (cnf) => {
|
||||
* @param msgModel - The model containing fields describing a message
|
||||
* @returns `lineStartY` - The Y coordinate at which the message line starts
|
||||
*/
|
||||
function boundMessage(_diagram, msgModel): number {
|
||||
async function boundMessage(_diagram, msgModel): Promise<number> {
|
||||
bounds.bumpVerticalPos(10);
|
||||
const { startx, stopx, message } = msgModel;
|
||||
const lines = common.splitBreaks(message).length;
|
||||
const textDims = utils.calculateTextDimensions(message, messageFont(conf));
|
||||
const lineHeight = textDims.height / lines;
|
||||
msgModel.height += lineHeight;
|
||||
const isKatexMsg = hasKatex(message);
|
||||
const textDims = isKatexMsg
|
||||
? await calculateMathMLDimensions(message, getConfig())
|
||||
: utils.calculateTextDimensions(message, messageFont(conf));
|
||||
|
||||
bounds.bumpVerticalPos(lineHeight);
|
||||
if (!isKatexMsg) {
|
||||
const lineHeight = textDims.height / lines;
|
||||
msgModel.height += lineHeight;
|
||||
bounds.bumpVerticalPos(lineHeight);
|
||||
}
|
||||
|
||||
let lineStartY;
|
||||
let totalOffset = textDims.height - 10;
|
||||
@@ -360,7 +365,7 @@ function boundMessage(_diagram, msgModel): number {
|
||||
* @param lineStartY - The Y coordinate at which the message line starts
|
||||
* @param diagObj - The diagram object.
|
||||
*/
|
||||
const drawMessage = function (diagram, msgModel, lineStartY: number, diagObj: Diagram) {
|
||||
const drawMessage = async function (diagram, msgModel, lineStartY: number, diagObj: Diagram) {
|
||||
const { startx, stopx, starty, message, type, sequenceIndex, sequenceVisible } = msgModel;
|
||||
const textDims = utils.calculateTextDimensions(message, messageFont(conf));
|
||||
const textObj = svgDrawCommon.getTextObj();
|
||||
@@ -378,7 +383,9 @@ const drawMessage = function (diagram, msgModel, lineStartY: number, diagObj: Di
|
||||
textObj.textMargin = conf.wrapPadding;
|
||||
textObj.tspan = false;
|
||||
|
||||
drawText(diagram, textObj);
|
||||
hasKatex(textObj.text)
|
||||
? await drawKatex(diagram, textObj, { startx, stopx, starty: lineStartY })
|
||||
: drawText(diagram, textObj);
|
||||
|
||||
const textWidth = textDims.width;
|
||||
|
||||
@@ -478,7 +485,7 @@ const drawMessage = function (diagram, msgModel, lineStartY: number, diagObj: Di
|
||||
}
|
||||
};
|
||||
|
||||
const addActorRenderingData = function (
|
||||
const addActorRenderingData = async function (
|
||||
diagram,
|
||||
actors,
|
||||
createdActors,
|
||||
@@ -548,12 +555,12 @@ const addActorRenderingData = function (
|
||||
bounds.bumpVerticalPos(maxHeight);
|
||||
};
|
||||
|
||||
export const drawActors = function (diagram, actors, actorKeys, isFooter) {
|
||||
export const drawActors = async function (diagram, actors, actorKeys, isFooter) {
|
||||
if (!isFooter) {
|
||||
for (const actorKey of actorKeys) {
|
||||
const actor = actors[actorKey];
|
||||
// Draw the box with the attached line
|
||||
svgDraw.drawActor(diagram, actor, conf, false);
|
||||
await svgDraw.drawActor(diagram, actor, conf, false);
|
||||
}
|
||||
} else {
|
||||
let maxHeight = 0;
|
||||
@@ -563,7 +570,7 @@ export const drawActors = function (diagram, actors, actorKeys, isFooter) {
|
||||
if (!actor.stopy) {
|
||||
actor.stopy = bounds.getVerticalPos();
|
||||
}
|
||||
const height = svgDraw.drawActor(diagram, actor, conf, true);
|
||||
const height = await svgDraw.drawActor(diagram, actor, conf, true);
|
||||
maxHeight = common.getMax(maxHeight, height);
|
||||
}
|
||||
bounds.bumpVerticalPos(maxHeight + conf.boxMargin);
|
||||
@@ -746,7 +753,7 @@ function adjustCreatedDestroyedData(
|
||||
* @param _version - Mermaid version from package.json
|
||||
* @param diagObj - A standard diagram containing the db and the text and type etc of the diagram
|
||||
*/
|
||||
export const draw = function (_text: string, id: string, _version: string, diagObj: Diagram) {
|
||||
export const draw = async function (_text: string, id: string, _version: string, diagObj: Diagram) {
|
||||
const { securityLevel, sequence } = getConfig();
|
||||
conf = sequence;
|
||||
// Handle root and Document for when rendering in sandbox mode
|
||||
@@ -776,8 +783,8 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
const title = diagObj.db.getDiagramTitle();
|
||||
const hasBoxes = diagObj.db.hasAtLeastOneBox();
|
||||
const hasBoxTitles = diagObj.db.hasAtLeastOneBoxWithTitle();
|
||||
const maxMessageWidthPerActor = getMaxMessageWidthPerActor(actors, messages, diagObj);
|
||||
conf.height = calculateActorMargins(actors, maxMessageWidthPerActor, boxes);
|
||||
const maxMessageWidthPerActor = await getMaxMessageWidthPerActor(actors, messages, diagObj);
|
||||
conf.height = await calculateActorMargins(actors, maxMessageWidthPerActor, boxes);
|
||||
|
||||
svgDraw.insertComputerIcon(diagram);
|
||||
svgDraw.insertDatabaseIcon(diagram);
|
||||
@@ -799,8 +806,8 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
actorKeys = actorKeys.filter((actorKey) => newActors.has(actorKey));
|
||||
}
|
||||
|
||||
addActorRenderingData(diagram, actors, createdActors, actorKeys, 0, messages, false);
|
||||
const loopWidths = calculateLoopBounds(messages, actors, maxMessageWidthPerActor, diagObj);
|
||||
await addActorRenderingData(diagram, actors, createdActors, actorKeys, 0, messages, false);
|
||||
const loopWidths = await calculateLoopBounds(messages, actors, maxMessageWidthPerActor, diagObj);
|
||||
|
||||
// The arrow head definition is attached to the svg once
|
||||
svgDraw.insertArrowHead(diagram);
|
||||
@@ -834,14 +841,15 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
let sequenceIndexStep = 1;
|
||||
const messagesToDraw = [];
|
||||
const backgrounds = [];
|
||||
messages.forEach(function (msg, index) {
|
||||
let index = 0;
|
||||
for (const msg of messages) {
|
||||
let loopModel, noteModel, msgModel;
|
||||
|
||||
switch (msg.type) {
|
||||
case diagObj.db.LINETYPE.NOTE:
|
||||
bounds.resetVerticalPos();
|
||||
noteModel = msg.noteModel;
|
||||
drawNote(diagram, noteModel);
|
||||
await drawNote(diagram, noteModel);
|
||||
break;
|
||||
case diagObj.db.LINETYPE.ACTIVE_START:
|
||||
bounds.newActivation(msg, diagram, actors);
|
||||
@@ -860,7 +868,7 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
break;
|
||||
case diagObj.db.LINETYPE.LOOP_END:
|
||||
loopModel = bounds.endLoop();
|
||||
svgDraw.drawLoop(diagram, loopModel, 'loop', conf);
|
||||
await svgDraw.drawLoop(diagram, loopModel, 'loop', conf);
|
||||
bounds.bumpVerticalPos(loopModel.stopy - bounds.getVerticalPos());
|
||||
bounds.models.addLoop(loopModel);
|
||||
break;
|
||||
@@ -886,7 +894,7 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
break;
|
||||
case diagObj.db.LINETYPE.OPT_END:
|
||||
loopModel = bounds.endLoop();
|
||||
svgDraw.drawLoop(diagram, loopModel, 'opt', conf);
|
||||
await svgDraw.drawLoop(diagram, loopModel, 'opt', conf);
|
||||
bounds.bumpVerticalPos(loopModel.stopy - bounds.getVerticalPos());
|
||||
bounds.models.addLoop(loopModel);
|
||||
break;
|
||||
@@ -910,7 +918,7 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
break;
|
||||
case diagObj.db.LINETYPE.ALT_END:
|
||||
loopModel = bounds.endLoop();
|
||||
svgDraw.drawLoop(diagram, loopModel, 'alt', conf);
|
||||
await svgDraw.drawLoop(diagram, loopModel, 'alt', conf);
|
||||
bounds.bumpVerticalPos(loopModel.stopy - bounds.getVerticalPos());
|
||||
bounds.models.addLoop(loopModel);
|
||||
break;
|
||||
@@ -936,7 +944,7 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
break;
|
||||
case diagObj.db.LINETYPE.PAR_END:
|
||||
loopModel = bounds.endLoop();
|
||||
svgDraw.drawLoop(diagram, loopModel, 'par', conf);
|
||||
await svgDraw.drawLoop(diagram, loopModel, 'par', conf);
|
||||
bounds.bumpVerticalPos(loopModel.stopy - bounds.getVerticalPos());
|
||||
bounds.models.addLoop(loopModel);
|
||||
break;
|
||||
@@ -969,7 +977,7 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
break;
|
||||
case diagObj.db.LINETYPE.CRITICAL_END:
|
||||
loopModel = bounds.endLoop();
|
||||
svgDraw.drawLoop(diagram, loopModel, 'critical', conf);
|
||||
await svgDraw.drawLoop(diagram, loopModel, 'critical', conf);
|
||||
bounds.bumpVerticalPos(loopModel.stopy - bounds.getVerticalPos());
|
||||
bounds.models.addLoop(loopModel);
|
||||
break;
|
||||
@@ -984,7 +992,7 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
break;
|
||||
case diagObj.db.LINETYPE.BREAK_END:
|
||||
loopModel = bounds.endLoop();
|
||||
svgDraw.drawLoop(diagram, loopModel, 'break', conf);
|
||||
await svgDraw.drawLoop(diagram, loopModel, 'break', conf);
|
||||
bounds.bumpVerticalPos(loopModel.stopy - bounds.getVerticalPos());
|
||||
bounds.models.addLoop(loopModel);
|
||||
break;
|
||||
@@ -994,7 +1002,7 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
msgModel.starty = bounds.getVerticalPos();
|
||||
msgModel.sequenceIndex = sequenceIndex;
|
||||
msgModel.sequenceVisible = diagObj.db.showSequenceNumbers();
|
||||
const lineStartY = boundMessage(diagram, msgModel);
|
||||
const lineStartY = await boundMessage(diagram, msgModel);
|
||||
adjustCreatedDestroyedData(
|
||||
msg,
|
||||
msgModel,
|
||||
@@ -1026,20 +1034,23 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
) {
|
||||
sequenceIndex = sequenceIndex + sequenceIndexStep;
|
||||
}
|
||||
});
|
||||
index++;
|
||||
}
|
||||
|
||||
log.debug('createdActors', createdActors);
|
||||
log.debug('destroyedActors', destroyedActors);
|
||||
await drawActors(diagram, actors, actorKeys, false);
|
||||
|
||||
drawActors(diagram, actors, actorKeys, false);
|
||||
messagesToDraw.forEach((e) => drawMessage(diagram, e.messageModel, e.lineStartY, diagObj));
|
||||
for (const e of messagesToDraw) {
|
||||
await drawMessage(diagram, e.messageModel, e.lineStartY, diagObj);
|
||||
}
|
||||
if (conf.mirrorActors) {
|
||||
drawActors(diagram, actors, actorKeys, true);
|
||||
await drawActors(diagram, actors, actorKeys, true);
|
||||
}
|
||||
backgrounds.forEach((e) => svgDraw.drawBackgroundRect(diagram, e));
|
||||
fixLifeLineHeights(diagram, actors, actorKeys, conf);
|
||||
|
||||
bounds.models.boxes.forEach(function (box) {
|
||||
for (const box of bounds.models.boxes) {
|
||||
box.height = bounds.getVerticalPos() - box.y;
|
||||
bounds.insert(box.x, box.y, box.x + box.width, box.height);
|
||||
box.startx = box.x;
|
||||
@@ -1047,8 +1058,8 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
box.stopx = box.startx + box.width;
|
||||
box.stopy = box.starty + box.height;
|
||||
box.stroke = 'rgb(0,0,0, 0.5)';
|
||||
svgDraw.drawBox(diagram, box, conf);
|
||||
});
|
||||
await svgDraw.drawBox(diagram, box, conf);
|
||||
}
|
||||
|
||||
if (hasBoxes) {
|
||||
bounds.bumpVerticalPos(conf.boxMargin);
|
||||
@@ -1114,25 +1125,25 @@ export const draw = function (_text: string, id: string, _version: string, diagO
|
||||
* @param diagObj - The diagram object.
|
||||
* @returns The max message width of each actor.
|
||||
*/
|
||||
function getMaxMessageWidthPerActor(
|
||||
async function getMaxMessageWidthPerActor(
|
||||
actors: { [id: string]: any },
|
||||
messages: any[],
|
||||
diagObj: Diagram
|
||||
): { [id: string]: number } {
|
||||
): Promise<{ [id: string]: number }> {
|
||||
const maxMessageWidthPerActor = {};
|
||||
|
||||
messages.forEach(function (msg) {
|
||||
for (const msg of messages) {
|
||||
if (actors[msg.to] && actors[msg.from]) {
|
||||
const actor = actors[msg.to];
|
||||
|
||||
// If this is the first actor, and the message is left of it, no need to calculate the margin
|
||||
if (msg.placement === diagObj.db.PLACEMENT.LEFTOF && !actor.prevActor) {
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
|
||||
// If this is the last actor, and the message is right of it, no need to calculate the margin
|
||||
if (msg.placement === diagObj.db.PLACEMENT.RIGHTOF && !actor.nextActor) {
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
|
||||
const isNote = msg.placement !== undefined;
|
||||
@@ -1142,7 +1153,9 @@ function getMaxMessageWidthPerActor(
|
||||
const wrappedMessage = msg.wrap
|
||||
? utils.wrapLabel(msg.message, conf.width - 2 * conf.wrapPadding, textFont)
|
||||
: msg.message;
|
||||
const messageDimensions = utils.calculateTextDimensions(wrappedMessage, textFont);
|
||||
const messageDimensions = hasKatex(wrappedMessage)
|
||||
? await calculateMathMLDimensions(msg.message, getConfig())
|
||||
: utils.calculateTextDimensions(wrappedMessage, textFont);
|
||||
const messageWidth = messageDimensions.width + 2 * conf.wrapPadding;
|
||||
|
||||
/*
|
||||
@@ -1207,7 +1220,7 @@ function getMaxMessageWidthPerActor(
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
log.debug('maxMessageWidthPerActor:', maxMessageWidthPerActor);
|
||||
return maxMessageWidthPerActor;
|
||||
@@ -1238,13 +1251,13 @@ const getRequiredPopupWidth = function (actor) {
|
||||
* @param actorToMessageWidth - A map of actor key → max message width it holds
|
||||
* @param boxes - The boxes around the actors if any
|
||||
*/
|
||||
function calculateActorMargins(
|
||||
async function calculateActorMargins(
|
||||
actors: { [id: string]: any },
|
||||
actorToMessageWidth: ReturnType<typeof getMaxMessageWidthPerActor>,
|
||||
actorToMessageWidth: Awaited<ReturnType<typeof getMaxMessageWidthPerActor>>,
|
||||
boxes
|
||||
) {
|
||||
let maxHeight = 0;
|
||||
Object.keys(actors).forEach((prop) => {
|
||||
for (const prop of Object.keys(actors)) {
|
||||
const actor = actors[prop];
|
||||
if (actor.wrap) {
|
||||
actor.description = utils.wrapLabel(
|
||||
@@ -1253,14 +1266,17 @@ function calculateActorMargins(
|
||||
actorFont(conf)
|
||||
);
|
||||
}
|
||||
const actDims = utils.calculateTextDimensions(actor.description, actorFont(conf));
|
||||
const actDims = hasKatex(actor.description)
|
||||
? await calculateMathMLDimensions(actor.description, getConfig())
|
||||
: utils.calculateTextDimensions(actor.description, actorFont(conf));
|
||||
|
||||
actor.width = actor.wrap
|
||||
? conf.width
|
||||
: common.getMax(conf.width, actDims.width + 2 * conf.wrapPadding);
|
||||
|
||||
actor.height = actor.wrap ? common.getMax(actDims.height, conf.height) : conf.height;
|
||||
maxHeight = common.getMax(maxHeight, actor.height);
|
||||
});
|
||||
}
|
||||
|
||||
for (const actorKey in actorToMessageWidth) {
|
||||
const actor = actors[actorKey];
|
||||
@@ -1311,15 +1327,17 @@ function calculateActorMargins(
|
||||
return common.getMax(maxHeight, conf.height);
|
||||
}
|
||||
|
||||
const buildNoteModel = function (msg, actors, diagObj) {
|
||||
const buildNoteModel = async function (msg, actors, diagObj) {
|
||||
const startx = actors[msg.from].x;
|
||||
const stopx = actors[msg.to].x;
|
||||
const shouldWrap = msg.wrap && msg.message;
|
||||
|
||||
let textDimensions = utils.calculateTextDimensions(
|
||||
shouldWrap ? utils.wrapLabel(msg.message, conf.width, noteFont(conf)) : msg.message,
|
||||
noteFont(conf)
|
||||
);
|
||||
let textDimensions: { width: number; height: number; lineHeight?: number } = hasKatex(msg.message)
|
||||
? await calculateMathMLDimensions(msg.message, getConfig())
|
||||
: utils.calculateTextDimensions(
|
||||
shouldWrap ? utils.wrapLabel(msg.message, conf.width, noteFont(conf)) : msg.message,
|
||||
noteFont(conf)
|
||||
);
|
||||
const noteModel = {
|
||||
width: shouldWrap
|
||||
? conf.width
|
||||
@@ -1477,12 +1495,12 @@ const buildMessageModel = function (msg, actors, diagObj) {
|
||||
};
|
||||
};
|
||||
|
||||
const calculateLoopBounds = function (messages, actors, _maxWidthPerActor, diagObj) {
|
||||
const calculateLoopBounds = async function (messages, actors, _maxWidthPerActor, diagObj) {
|
||||
const loops = {};
|
||||
const stack = [];
|
||||
let current, noteModel, msgModel;
|
||||
|
||||
messages.forEach(function (msg) {
|
||||
for (const msg of messages) {
|
||||
msg.id = utils.random({ length: 10 });
|
||||
switch (msg.type) {
|
||||
case diagObj.db.LINETYPE.LOOP_START:
|
||||
@@ -1545,7 +1563,7 @@ const calculateLoopBounds = function (messages, actors, _maxWidthPerActor, diagO
|
||||
}
|
||||
const isNote = msg.placement !== undefined;
|
||||
if (isNote) {
|
||||
noteModel = buildNoteModel(msg, actors, diagObj);
|
||||
noteModel = await buildNoteModel(msg, actors, diagObj);
|
||||
msg.noteModel = noteModel;
|
||||
stack.forEach((stk) => {
|
||||
current = stk;
|
||||
@@ -1584,7 +1602,7 @@ const calculateLoopBounds = function (messages, actors, _maxWidthPerActor, diagO
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
bounds.activations = [];
|
||||
log.debug('Loop type widths:', loops);
|
||||
return loops;
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import common from '../common/common.js';
|
||||
import common, { calculateMathMLDimensions, hasKatex, renderKatex } from '../common/common.js';
|
||||
import * as svgDrawCommon from '../common/svgDrawCommon.js';
|
||||
import { addFunction } from '../../interactionDb.js';
|
||||
import { ZERO_WIDTH_SPACE, parseFontSize } from '../../utils.js';
|
||||
import { sanitizeUrl } from '@braintree/sanitize-url';
|
||||
import * as configApi from '../../config.js';
|
||||
|
||||
export const ACTOR_TYPE_WIDTH = 18 * 2;
|
||||
const TOP_ACTOR_CLASS = 'actor-top';
|
||||
const BOTTOM_ACTOR_CLASS = 'actor-bottom';
|
||||
|
||||
export const drawRect = function (elem, rectData) {
|
||||
return svgDrawCommon.drawRect(elem, rectData);
|
||||
@@ -73,14 +76,55 @@ export const drawPopup = function (elem, actor, minMenuWidth, textAttrs, forceMe
|
||||
return { height: rectData.height + linkY, width: menuWidth };
|
||||
};
|
||||
|
||||
const popupMenuToggle = function (popid) {
|
||||
const popupMenuToggle = function (popId) {
|
||||
return (
|
||||
"var pu = document.getElementById('" +
|
||||
popid +
|
||||
popId +
|
||||
"'); if (pu != null) { pu.style.display = pu.style.display == 'block' ? 'none' : 'block'; }"
|
||||
);
|
||||
};
|
||||
|
||||
export const drawKatex = async function (elem, textData, msgModel = null) {
|
||||
let textElem = elem.append('foreignObject');
|
||||
const lines = await renderKatex(textData.text, configApi.getConfig());
|
||||
|
||||
const divElem = textElem
|
||||
.append('xhtml:div')
|
||||
.attr('style', 'width: fit-content;')
|
||||
.attr('xmlns', 'http://www.w3.org/1999/xhtml')
|
||||
.html(lines);
|
||||
const dim = divElem.node().getBoundingClientRect();
|
||||
|
||||
textElem.attr('height', Math.round(dim.height)).attr('width', Math.round(dim.width));
|
||||
|
||||
if (textData.class === 'noteText') {
|
||||
const rectElem = elem.node().firstChild;
|
||||
|
||||
rectElem.setAttribute('height', dim.height + 2 * textData.textMargin);
|
||||
const rectDim = rectElem.getBBox();
|
||||
|
||||
textElem
|
||||
.attr('x', Math.round(rectDim.x + rectDim.width / 2 - dim.width / 2))
|
||||
.attr('y', Math.round(rectDim.y + rectDim.height / 2 - dim.height / 2));
|
||||
} else if (msgModel) {
|
||||
let { startx, stopx, starty } = msgModel;
|
||||
if (startx > stopx) {
|
||||
const temp = startx;
|
||||
startx = stopx;
|
||||
stopx = temp;
|
||||
}
|
||||
|
||||
textElem.attr('x', Math.round(startx + Math.abs(startx - stopx) / 2 - dim.width / 2));
|
||||
if (textData.class === 'loopText') {
|
||||
textElem.attr('y', Math.round(starty));
|
||||
} else {
|
||||
textElem.attr('y', Math.round(starty - dim.height));
|
||||
}
|
||||
}
|
||||
|
||||
return [textElem];
|
||||
};
|
||||
|
||||
export const drawText = function (elem, textData) {
|
||||
let prevTextHeight = 0;
|
||||
let textHeight = 0;
|
||||
@@ -280,13 +324,13 @@ export const fixLifeLineHeights = (diagram, actors, actorKeys, conf) => {
|
||||
* @param {any} conf - DrawText implementation discriminator object
|
||||
* @param {boolean} isFooter - If the actor is the footer one
|
||||
*/
|
||||
const drawActorTypeParticipant = function (elem, actor, conf, isFooter) {
|
||||
const drawActorTypeParticipant = async function (elem, actor, conf, isFooter) {
|
||||
const actorY = isFooter ? actor.stopy : actor.starty;
|
||||
const center = actor.x + actor.width / 2;
|
||||
const centerY = actorY + 5;
|
||||
|
||||
const boxpluslineGroup = elem.append('g').lower();
|
||||
var g = boxpluslineGroup;
|
||||
const boxplusLineGroup = elem.append('g').lower();
|
||||
var g = boxplusLineGroup;
|
||||
|
||||
if (!isFooter) {
|
||||
actorCnt++;
|
||||
@@ -304,7 +348,7 @@ const drawActorTypeParticipant = function (elem, actor, conf, isFooter) {
|
||||
.attr('stroke-width', '0.5px')
|
||||
.attr('stroke', '#999');
|
||||
|
||||
g = boxpluslineGroup.append('g');
|
||||
g = boxplusLineGroup.append('g');
|
||||
actor.actorCnt = actorCnt;
|
||||
|
||||
if (actor.links != null) {
|
||||
@@ -319,6 +363,11 @@ const drawActorTypeParticipant = function (elem, actor, conf, isFooter) {
|
||||
} else {
|
||||
rect.fill = '#eaeaea';
|
||||
}
|
||||
if (isFooter) {
|
||||
cssclass += ` ${BOTTOM_ACTOR_CLASS}`;
|
||||
} else {
|
||||
cssclass += ` ${TOP_ACTOR_CLASS}`;
|
||||
}
|
||||
rect.x = actor.x;
|
||||
rect.y = actorY;
|
||||
rect.width = actor.width;
|
||||
@@ -326,6 +375,7 @@ const drawActorTypeParticipant = function (elem, actor, conf, isFooter) {
|
||||
rect.class = cssclass;
|
||||
rect.rx = 3;
|
||||
rect.ry = 3;
|
||||
rect.name = actor.name;
|
||||
const rectElem = drawRect(g, rect);
|
||||
actor.rectData = rect;
|
||||
|
||||
@@ -338,7 +388,7 @@ const drawActorTypeParticipant = function (elem, actor, conf, isFooter) {
|
||||
}
|
||||
}
|
||||
|
||||
_drawTextCandidateFunc(conf)(
|
||||
await _drawTextCandidateFunc(conf, hasKatex(actor.description))(
|
||||
actor.description,
|
||||
g,
|
||||
rect.x,
|
||||
@@ -359,7 +409,7 @@ const drawActorTypeParticipant = function (elem, actor, conf, isFooter) {
|
||||
return height;
|
||||
};
|
||||
|
||||
const drawActorTypeActor = function (elem, actor, conf, isFooter) {
|
||||
const drawActorTypeActor = async function (elem, actor, conf, isFooter) {
|
||||
const actorY = isFooter ? actor.stopy : actor.starty;
|
||||
const center = actor.x + actor.width / 2;
|
||||
const centerY = actorY + 80;
|
||||
@@ -383,7 +433,14 @@ const drawActorTypeActor = function (elem, actor, conf, isFooter) {
|
||||
actor.actorCnt = actorCnt;
|
||||
}
|
||||
const actElem = elem.append('g');
|
||||
actElem.attr('class', 'actor-man');
|
||||
let cssClass = 'actor-man';
|
||||
if (isFooter) {
|
||||
cssClass += ` ${BOTTOM_ACTOR_CLASS}`;
|
||||
} else {
|
||||
cssClass += ` ${TOP_ACTOR_CLASS}`;
|
||||
}
|
||||
actElem.attr('class', cssClass);
|
||||
actElem.attr('name', actor.name);
|
||||
|
||||
const rect = svgDrawCommon.getNoteRect();
|
||||
rect.x = actor.x;
|
||||
@@ -433,7 +490,7 @@ const drawActorTypeActor = function (elem, actor, conf, isFooter) {
|
||||
const bounds = actElem.node().getBBox();
|
||||
actor.height = bounds.height;
|
||||
|
||||
_drawTextCandidateFunc(conf)(
|
||||
await _drawTextCandidateFunc(conf, hasKatex(actor.description))(
|
||||
actor.description,
|
||||
actElem,
|
||||
rect.x,
|
||||
@@ -447,21 +504,21 @@ const drawActorTypeActor = function (elem, actor, conf, isFooter) {
|
||||
return actor.height;
|
||||
};
|
||||
|
||||
export const drawActor = function (elem, actor, conf, isFooter) {
|
||||
export const drawActor = async function (elem, actor, conf, isFooter) {
|
||||
switch (actor.type) {
|
||||
case 'actor':
|
||||
return drawActorTypeActor(elem, actor, conf, isFooter);
|
||||
return await drawActorTypeActor(elem, actor, conf, isFooter);
|
||||
case 'participant':
|
||||
return drawActorTypeParticipant(elem, actor, conf, isFooter);
|
||||
return await drawActorTypeParticipant(elem, actor, conf, isFooter);
|
||||
}
|
||||
};
|
||||
|
||||
export const drawBox = function (elem, box, conf) {
|
||||
const boxplustextGroup = elem.append('g');
|
||||
const g = boxplustextGroup;
|
||||
export const drawBox = async function (elem, box, conf) {
|
||||
const boxplusTextGroup = elem.append('g');
|
||||
const g = boxplusTextGroup;
|
||||
drawBackgroundRect(g, box);
|
||||
if (box.name) {
|
||||
_drawTextCandidateFunc(conf)(
|
||||
await _drawTextCandidateFunc(conf)(
|
||||
box.name,
|
||||
g,
|
||||
box.x,
|
||||
@@ -508,7 +565,7 @@ export const drawActivation = function (elem, bounds, verticalPos, conf, actorAc
|
||||
* @param {any} conf - Diagram configuration
|
||||
* @returns {any}
|
||||
*/
|
||||
export const drawLoop = function (elem, loopModel, labelText, conf) {
|
||||
export const drawLoop = async function (elem, loopModel, labelText, conf) {
|
||||
const {
|
||||
boxMargin,
|
||||
boxTextMargin,
|
||||
@@ -570,10 +627,10 @@ export const drawLoop = function (elem, loopModel, labelText, conf) {
|
||||
txt.fontWeight = fontWeight;
|
||||
txt.wrap = true;
|
||||
|
||||
let textElem = drawText(g, txt);
|
||||
let textElem = hasKatex(txt.text) ? await drawKatex(g, txt, loopModel) : drawText(g, txt);
|
||||
|
||||
if (loopModel.sectionTitles !== undefined) {
|
||||
loopModel.sectionTitles.forEach(function (item, idx) {
|
||||
for (const [idx, item] of Object.entries(loopModel.sectionTitles)) {
|
||||
if (item.message) {
|
||||
txt.text = item.message;
|
||||
txt.x = loopModel.startx + (loopModel.stopx - loopModel.startx) / 2;
|
||||
@@ -586,7 +643,13 @@ export const drawLoop = function (elem, loopModel, labelText, conf) {
|
||||
txt.fontSize = fontSize;
|
||||
txt.fontWeight = fontWeight;
|
||||
txt.wrap = loopModel.wrap;
|
||||
textElem = drawText(g, txt);
|
||||
|
||||
if (hasKatex(txt.text)) {
|
||||
loopModel.starty = loopModel.sections[idx].y;
|
||||
await drawKatex(g, txt, loopModel);
|
||||
} else {
|
||||
drawText(g, txt);
|
||||
}
|
||||
let sectionHeight = Math.round(
|
||||
textElem
|
||||
.map((te) => (te._groups || te)[0][0].getBBox().height)
|
||||
@@ -594,7 +657,7 @@ export const drawLoop = function (elem, loopModel, labelText, conf) {
|
||||
);
|
||||
loopModel.sections[idx].height += sectionHeight - (boxMargin + boxTextMargin);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
loopModel.height = Math.round(loopModel.stopy - loopModel.starty);
|
||||
@@ -871,6 +934,41 @@ const _drawTextCandidateFunc = (function () {
|
||||
_setTextAttrs(text, textAttrs);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param content
|
||||
* @param g
|
||||
* @param x
|
||||
* @param y
|
||||
* @param width
|
||||
* @param height
|
||||
* @param textAttrs
|
||||
* @param conf
|
||||
*/
|
||||
async function byKatex(content, g, x, y, width, height, textAttrs, conf) {
|
||||
// TODO duplicate render calls, optimize
|
||||
|
||||
const dim = await calculateMathMLDimensions(content, configApi.getConfig());
|
||||
const s = g.append('switch');
|
||||
const f = s
|
||||
.append('foreignObject')
|
||||
.attr('x', x + width / 2 - dim.width / 2)
|
||||
.attr('y', y + height / 2 - dim.height / 2)
|
||||
.attr('width', dim.width)
|
||||
.attr('height', dim.height);
|
||||
|
||||
const text = f.append('xhtml:div').style('height', '100%').style('width', '100%');
|
||||
|
||||
text
|
||||
.append('div')
|
||||
.style('text-align', 'center')
|
||||
.style('vertical-align', 'middle')
|
||||
.html(await renderKatex(content, configApi.getConfig()));
|
||||
|
||||
byTspan(content, s, x, y, width, height, textAttrs, conf);
|
||||
_setTextAttrs(text, textAttrs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} toText
|
||||
* @param {any} fromTextAttrsDict
|
||||
@@ -883,7 +981,10 @@ const _drawTextCandidateFunc = (function () {
|
||||
}
|
||||
}
|
||||
|
||||
return function (conf) {
|
||||
return function (conf, hasKatex = false) {
|
||||
if (hasKatex) {
|
||||
return byKatex;
|
||||
}
|
||||
return conf.textPlacement === 'fo' ? byFo : conf.textPlacement === 'old' ? byText : byTspan;
|
||||
};
|
||||
})();
|
||||
|
||||
@@ -206,7 +206,7 @@ export const addTitleAndBox = (g, stateDef, altBkg) => {
|
||||
g.insert('rect', ':first-child')
|
||||
.attr('x', startX)
|
||||
.attr('y', lineY)
|
||||
.attr('class', altBkg ? 'alt-composit' : 'composit')
|
||||
.attr('class', altBkg ? 'alt-composit' : 'composit') // cspell:disable-line
|
||||
.attr('width', width)
|
||||
.attr(
|
||||
'height',
|
||||
@@ -482,11 +482,11 @@ export const drawEdge = function (elem, path, relation) {
|
||||
.attr('x', x)
|
||||
.attr('y', y + titleHeight);
|
||||
|
||||
const boundstmp = title.node().getBBox();
|
||||
maxWidth = Math.max(maxWidth, boundstmp.width);
|
||||
minX = Math.min(minX, boundstmp.x);
|
||||
const boundsTmp = title.node().getBBox();
|
||||
maxWidth = Math.max(maxWidth, boundsTmp.width);
|
||||
minX = Math.min(minX, boundsTmp.x);
|
||||
|
||||
log.info(boundstmp.x, x, y + titleHeight);
|
||||
log.info(boundsTmp.x, x, y + titleHeight);
|
||||
|
||||
if (titleHeight === 0) {
|
||||
const titleBox = title.node().getBBox();
|
||||
|
||||
@@ -260,7 +260,7 @@ export const addState = function (
|
||||
if (classes) {
|
||||
log.info('Setting state classes', trimmedId, classes);
|
||||
const classesList = typeof classes === 'string' ? [classes] : classes;
|
||||
classesList.forEach((klass) => setCssClass(trimmedId, klass.trim()));
|
||||
classesList.forEach((cssClass) => setCssClass(trimmedId, cssClass.trim()));
|
||||
}
|
||||
|
||||
if (styles) {
|
||||
|
||||
@@ -202,4 +202,7 @@ g.stateGroup line {
|
||||
}
|
||||
`;
|
||||
|
||||
// todo: change composit to composite
|
||||
// cspell:ignore composit
|
||||
|
||||
export default getStyles;
|
||||
|
||||
@@ -53,7 +53,7 @@ export const addTask = function (period, length, event) {
|
||||
};
|
||||
|
||||
export const addEvent = function (event) {
|
||||
// fetch current task with currnetTaskId
|
||||
// fetch current task with currentTaskId
|
||||
const currentTask = rawTasks.find((task) => task.id === currentTaskId - 1);
|
||||
//add event to the events array
|
||||
currentTask.events.push(event);
|
||||
|
||||
@@ -27,14 +27,14 @@ export function getAxis(
|
||||
axisThemeConfig: XYChartAxisThemeConfig,
|
||||
tmpSVGGroup: Group
|
||||
): Axis {
|
||||
const textDimansionCalculator = new TextDimensionCalculatorWithFont(tmpSVGGroup);
|
||||
const textDimensionCalculator = new TextDimensionCalculatorWithFont(tmpSVGGroup);
|
||||
if (isBandAxisData(data)) {
|
||||
return new BandAxis(
|
||||
axisConfig,
|
||||
axisThemeConfig,
|
||||
data.categories,
|
||||
data.title,
|
||||
textDimansionCalculator
|
||||
textDimensionCalculator
|
||||
);
|
||||
}
|
||||
return new LinearAxis(
|
||||
@@ -42,6 +42,6 @@ export function getAxis(
|
||||
axisThemeConfig,
|
||||
[data.min, data.max],
|
||||
data.title,
|
||||
textDimansionCalculator
|
||||
textDimensionCalculator
|
||||
);
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ export class Orchestrator {
|
||||
}
|
||||
}
|
||||
|
||||
private calculateHorizonatalSpace() {
|
||||
private calculateHorizontalSpace() {
|
||||
let availableWidth = this.chartConfig.width;
|
||||
let availableHeight = this.chartConfig.height;
|
||||
let titleYEnd = 0;
|
||||
@@ -174,7 +174,7 @@ export class Orchestrator {
|
||||
|
||||
private calculateSpace() {
|
||||
if (this.chartConfig.chartOrientation === 'horizontal') {
|
||||
this.calculateHorizonatalSpace();
|
||||
this.calculateHorizontalSpace();
|
||||
} else {
|
||||
this.calculateVerticalSpace();
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import { teamMembers } from '../contributors';
|
||||
<p text-lg max-w-200 text-center leading-7>
|
||||
<Contributors />
|
||||
<br />
|
||||
<a href="https://discord.gg/wwtabKgp8y" rel="noopener noreferrer">Join the community</a>
|
||||
<a href="https://discord.gg/AgrbSrBer3" rel="noopener noreferrer">Join the community</a>
|
||||
and get involved!
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -56,7 +56,7 @@ export default defineConfig({
|
||||
{ icon: 'github', link: 'https://github.com/mermaid-js/mermaid' },
|
||||
{
|
||||
icon: 'discord',
|
||||
link: 'https://discord.gg/wwtabKgp8y',
|
||||
link: 'https://discord.gg/AgrbSrBer3',
|
||||
},
|
||||
{
|
||||
icon: {
|
||||
@@ -146,13 +146,14 @@ function sidebarSyntax() {
|
||||
{ text: 'Pie Chart', link: '/syntax/pie' },
|
||||
{ text: 'Quadrant Chart', link: '/syntax/quadrantChart' },
|
||||
{ text: 'Requirement Diagram', link: '/syntax/requirementDiagram' },
|
||||
{ text: 'Gitgraph (Git) Diagram 🔥', link: '/syntax/gitgraph' },
|
||||
{ text: 'Gitgraph (Git) Diagram', link: '/syntax/gitgraph' },
|
||||
{ text: 'C4 Diagram 🦺⚠️', link: '/syntax/c4' },
|
||||
{ text: 'Mindmaps 🔥', link: '/syntax/mindmap' },
|
||||
{ text: 'Timeline 🔥', link: '/syntax/timeline' },
|
||||
{ text: 'Zenuml 🔥', link: '/syntax/zenuml' },
|
||||
{ text: 'Sankey 🔥', link: '/syntax/sankey' },
|
||||
{ text: 'Mindmaps', link: '/syntax/mindmap' },
|
||||
{ text: 'Timeline', link: '/syntax/timeline' },
|
||||
{ text: 'Zenuml', link: '/syntax/zenuml' },
|
||||
{ text: 'Sankey', link: '/syntax/sankey' },
|
||||
{ text: 'XYChart 🔥', link: '/syntax/xyChart' },
|
||||
{ text: 'Block Diagram 🔥', link: '/syntax/block' },
|
||||
{ text: 'Other Examples', link: '/syntax/examples' },
|
||||
],
|
||||
},
|
||||
@@ -171,6 +172,7 @@ function sidebarConfig() {
|
||||
{ text: 'Mermaid Configuration Options', link: '/config/schema-docs/config' },
|
||||
{ text: 'Directives', link: '/config/directives' },
|
||||
{ text: 'Theming', link: '/config/theming' },
|
||||
{ text: 'Math', link: '/config/math' },
|
||||
{ text: 'Accessibility', link: '/config/accessibility' },
|
||||
{ text: 'Mermaid CLI', link: '/config/mermaidCLI' },
|
||||
{ text: 'FAQ', link: '/config/faq' },
|
||||
|
||||
@@ -8,14 +8,12 @@ import Contributors from '../components/Contributors.vue';
|
||||
import HomePage from '../components/HomePage.vue';
|
||||
// @ts-ignore
|
||||
import TopBar from '../components/TopBar.vue';
|
||||
|
||||
import { getRedirect } from './redirect.js';
|
||||
|
||||
import { h } from 'vue';
|
||||
|
||||
import Theme from 'vitepress/theme';
|
||||
import '../style/main.css';
|
||||
import 'uno.css';
|
||||
import type { EnhanceAppContext } from 'vitepress';
|
||||
|
||||
export default {
|
||||
...DefaultTheme,
|
||||
@@ -26,19 +24,22 @@ export default {
|
||||
'home-features-after': () => h(HomePage),
|
||||
});
|
||||
},
|
||||
enhanceApp({ app, router }) {
|
||||
enhanceApp({ app, router }: EnhanceAppContext) {
|
||||
// register global components
|
||||
app.component('Mermaid', Mermaid);
|
||||
app.component('Contributors', Contributors);
|
||||
router.onBeforeRouteChange = (to) => {
|
||||
try {
|
||||
const newPath = getRedirect(to);
|
||||
const url = new URL(window.location.origin + to);
|
||||
const newPath = getRedirect(url);
|
||||
if (newPath) {
|
||||
console.log(`Redirecting to ${newPath} from ${window.location}`);
|
||||
// router.go isn't loading the ID properly.
|
||||
window.location.href = `/${newPath}`;
|
||||
}
|
||||
} catch (e) {}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
@@ -57,5 +57,5 @@ test.each([
|
||||
'configure/faq.html#frequently-asked-questions',
|
||||
], // with hash
|
||||
])('should process url %s to %s', (link: string, path: string) => {
|
||||
expect(getRedirect(link)).toBe(path);
|
||||
expect(getRedirect(new URL(link))).toBe(path);
|
||||
});
|
||||
|
||||
@@ -104,8 +104,7 @@ const urlRedirectMap: Record<string, string> = {
|
||||
* @param link - The old documentation URL.
|
||||
* @returns The new documentation path.
|
||||
*/
|
||||
export const getRedirect = (link: string): string | undefined => {
|
||||
const url = new URL(link);
|
||||
export const getRedirect = (url: URL): string | undefined => {
|
||||
// Redirects for deprecated vitepress URLs
|
||||
if (url.pathname in urlRedirectMap) {
|
||||
return `${urlRedirectMap[url.pathname]}${url.hash}`;
|
||||
|
||||
@@ -130,7 +130,7 @@ You might see _lint_ or _formatting_ warnings. Those are ok during this step.
|
||||
|
||||
## Workflow
|
||||
|
||||
Contributing process is very simple and strightforward:
|
||||
Contributing process is very simple and straightforward:
|
||||
|
||||
```mermaid-nocode
|
||||
flowchart LR
|
||||
@@ -371,13 +371,13 @@ If the users have no way to know that things have changed, then you haven't real
|
||||
Likewise, if users don't know that there is a new feature that you've implemented, it will forever remain unknown and unused.
|
||||
|
||||
The documentation has to be updated for users to know that things have been changed and added!
|
||||
If you are adding a new feature, add `(v<MERMAID_RELEASE_VERSION>+)` in the title or description. It will be replaced automatically with the current version number when the release happens.
|
||||
If you are adding a new feature, add `(v10.8.0+)` in the title or description. It will be replaced automatically with the current version number when the release happens.
|
||||
|
||||
eg: `# Feature Name (v<MERMAID_RELEASE_VERSION>+)`
|
||||
eg: `# Feature Name (v10.8.0+)`
|
||||
|
||||
We know it can sometimes be hard to code _and_ write user documentation.
|
||||
|
||||
Create another issue specifically for the documentation.
|
||||
Create another issue specifically for the documentation.
|
||||
You will need to help with the PR, but definitely ask for help if you feel stuck.
|
||||
When it feels hard to write stuff out, explaining it to someone and having that person ask you clarifying questions can often be 80% of the work!
|
||||
|
||||
@@ -403,7 +403,7 @@ The contents of [mermaid.js.org](https://mermaid.js.org/) are based on the docs
|
||||
flowchart LR
|
||||
classDef default fill:#fff,color:black,stroke:black
|
||||
|
||||
source["Edit /packages/mermaid/src/docs"] -- automatic processing--> published["View /docs which will be publised on Official Website"]
|
||||
source["Edit /packages/mermaid/src/docs"] -- automatic processing--> published["View /docs which will be published on Official Website"]
|
||||
```
|
||||
|
||||
### Running the Documentation Website Locally
|
||||
@@ -521,3 +521,5 @@ You have successfully submitted your improvements! What is next?
|
||||
- When a release is ready, the `release/x.x.x` branch will be created, extensively tested and knsv will be in charge of the release process.
|
||||
|
||||
Thanks for you help!
|
||||
|
||||
<!--- cspell:ignore florbs --->
|
||||
|
||||
@@ -39,7 +39,7 @@ Where to start:
|
||||
- You could work on a new feature! [These](https://github.com/mermaid-js/mermaid/issues?q=is%3Aissue+is%3Aopen+label%3A%22Area%3A+Development%22+label%3A%22Type%3A+Enhancement%22+label%3A%22Status%3A+Approved%22+) are some ideas!
|
||||
- You could confirm the bugs in [these issues](https://github.com/mermaid-js/mermaid/issues?q=is%3Aissue+is%3Aopen+label%3A%22Status%3A+Triage%22++label%3A%22Type%3A+Bug+%2F+Error%22).
|
||||
|
||||
[Join our slack community if you want closer contact!](https://join.slack.com/t/mermaid-talk/shared_invite/enQtNzc4NDIyNzk4OTAyLWVhYjQxOTI2OTg4YmE1ZmJkY2Y4MTU3ODliYmIwOTY3NDJlYjA0YjIyZTdkMDMyZTUwOGI0NjEzYmEwODcwOTE)
|
||||
[You can join our Discord server if you want closer contact!](https://discord.gg/AgrbSrBer3)
|
||||
|
||||
## A Question Or a Suggestion?
|
||||
|
||||
@@ -53,6 +53,6 @@ If you have faced a vulnerability [report it to us](./security.md).
|
||||
|
||||
Don't get daunted if it is hard in the beginning. We have a great community with only encouraging words. So, if you get stuck, ask for help and hints in the Slack forum. If you want to show off something good, show it off there.
|
||||
|
||||
[Join our Slack community if you want closer contact!](https://join.slack.com/t/mermaid-talk/shared_invite/enQtNzc4NDIyNzk4OTAyLWVhYjQxOTI2OTg4YmE1ZmJkY2Y4MTU3ODliYmIwOTY3NDJlYjA0YjIyZTdkMDMyZTUwOGI0NjEzYmEwODcwOTE)
|
||||
[You can join our Discord server if you want closer contact!](https://discord.gg/AgrbSrBer3)
|
||||
|
||||

|
||||
|
||||
@@ -13,7 +13,7 @@ Mermaid will automatically insert the [aria-roledescription](#aria-roledescripti
|
||||
|
||||
The [aria-roledescription](https://www.w3.org/TR/wai-aria-1.1/#aria-roledescription) for the SVG HTML element is set to the diagram type key. (Note this may be slightly different than the keyword used for the diagram in the diagram text.)
|
||||
|
||||
For example: The diagram type key for a state diagram is "stateDiagram". Here (a part of) the HTML of the SVG tag that shows the automatically inserted aria-roledscription set to "stateDiagram". _(Note that some of the SVG attributes and the SVG contents are omitted for clarity.):_
|
||||
For example: The diagram type key for a state diagram is "stateDiagram". Here (a part of) the HTML of the SVG tag that shows the automatically inserted aria-roledescription set to "stateDiagram". _(Note that some of the SVG attributes and the SVG contents are omitted for clarity.):_
|
||||
|
||||
```html
|
||||
<svg
|
||||
|
||||
62
packages/mermaid/src/docs/config/math.md
Normal file
62
packages/mermaid/src/docs/config/math.md
Normal file
@@ -0,0 +1,62 @@
|
||||
# Math Configuration (v<MERMAID_RELEASE_VERSION>+)
|
||||
|
||||
Mermaid supports rendering mathematical expressions through the [KaTeX](https://katex.org/) typesetter.
|
||||
|
||||
## Usage
|
||||
|
||||
To render math within a diagram, surround the mathematical expression with the `$$` delimiter.
|
||||
|
||||
Note that at the moment, the only supported diagrams are below:
|
||||
|
||||
### Flowcharts
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
A["$$x^2$$"] -->|"$$\sqrt{x+3}$$"| B("$$\frac{1}{2}$$")
|
||||
A -->|"$$\overbrace{a+b+c}^{\text{note}}$$"| C("$$\pi r^2$$")
|
||||
B --> D("$$x = \begin{cases} a &\text{if } b \\ c &\text{if } d \end{cases}$$")
|
||||
C --> E("$$x(t)=c_1\begin{bmatrix}-\cos{t}+\sin{t}\\ 2\cos{t} \end{bmatrix}e^{2t}$$")
|
||||
```
|
||||
|
||||
### Sequence
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
autonumber
|
||||
participant 1 as $$\alpha$$
|
||||
participant 2 as $$\beta$$
|
||||
1->>2: Solve: $$\sqrt{2+2}$$
|
||||
2-->>1: Answer: $$2$$
|
||||
Note right of 2: $$\sqrt{2+2}=\sqrt{4}=2$$
|
||||
```
|
||||
|
||||
## Legacy Support
|
||||
|
||||
By default, MathML is used for rendering mathematical expressions. If you have users on [unsupported browsers](https://caniuse.com/?search=mathml), `legacyMathML` can be set in the config to fall back to CSS rendering. Note that **you must provide KaTeX's stylesheets on your own** as they do not come bundled with Mermaid.
|
||||
|
||||
Example with legacy mode enabled (the latest version of KaTeX's stylesheet can be found on their [docs](https://katex.org/docs/browser.html)):
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
|
||||
<html lang="en">
|
||||
<head>
|
||||
<!-- Please ensure the stylesheet's version matches with the KaTeX version in your package-lock -->
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://cdn.jsdelivr.net/npm/katex@{version_number}/dist/katex.min.css"
|
||||
integrity="sha384-{hash}"
|
||||
crossorigin="anonymous"
|
||||
/>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script type="module">
|
||||
import mermaid from './mermaid.esm.mjs';
|
||||
mermaid.initialize({
|
||||
legacyMathML: true,
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
@@ -47,7 +47,7 @@ Example of `init` directive setting the `theme` to `forest`:
|
||||
a --> b
|
||||
```
|
||||
|
||||
> **Reminder**: the only theme that can be customed is the `base` theme. The following section covers how to use `themeVariables` for customizations.
|
||||
> **Reminder**: the only theme that can be customized is the `base` theme. The following section covers how to use `themeVariables` for customizations.
|
||||
|
||||
## Customizing Themes with `themeVariables`
|
||||
|
||||
|
||||
@@ -437,3 +437,8 @@ mermaid_config.startOnLoad = true;
|
||||
```warning
|
||||
This way of setting the configuration is deprecated. Instead the preferred way is to use the initialize method. This functionality is only kept for backwards compatibility.
|
||||
```
|
||||
|
||||
<!---
|
||||
cspell:locale en,en-gb
|
||||
cspell:ignore pumbaa
|
||||
--->
|
||||
|
||||
@@ -90,6 +90,8 @@ Blogging frameworks and platforms
|
||||
|
||||
Content Management Systems/Enterprise Content Management
|
||||
|
||||
- [ApostropheCMS](https://apostrophecms.com/)
|
||||
- [Extension for Mermaid.js](https://github.com/BoDonkey/mermaid-extension)
|
||||
- [Grav CMS](https://getgrav.org/)
|
||||
- [Mermaid Diagrams Plugin](https://github.com/DanielFlaum/grav-plugin-mermaid-diagrams)
|
||||
- [GitLab Markdown Adapter](https://github.com/Goutte/grav-plugin-gitlab-markdown-adapter)
|
||||
@@ -210,23 +212,24 @@ Communication tools and platforms
|
||||
|
||||
### Browser Extensions
|
||||
|
||||
| Name | Chrome Web Store | Firefox Add-ons | Opera | Edge | Source/Repository |
|
||||
| ------------------------ | ------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
|
||||
| GitHub + Mermaid | - | [🦊🔗](https://addons.mozilla.org/firefox/addon/github-mermaid/) | - | - | [🐙🔗](https://github.com/BackMarket/github-mermaid-extension) |
|
||||
| Asciidoctor Live Preview | [🎡🔗](https://chrome.google.com/webstore/detail/asciidoctorjs-live-previe/iaalpfgpbocpdfblpnhhgllgbdbchmia) | - | - | [🌀🔗](https://microsoftedge.microsoft.com/addons/detail/asciidoctorjs-live-previ/pefkelkanablhjdekgdahplkccnbdggd?hl=en-US) | - |
|
||||
| Diagram Tab | - | - | - | - | [🐙🔗](https://github.com/khafast/diagramtab) |
|
||||
| Markdown Diagrams | [🎡🔗](https://chrome.google.com/webstore/detail/markdown-diagrams/pmoglnmodacnbbofbgcagndelmgaclel/) | [🦊🔗](https://addons.mozilla.org/en-US/firefox/addon/markdown-diagrams/) | [🔴🔗](https://addons.opera.com/en/extensions/details/markdown-diagrams/) | [🌀🔗](https://microsoftedge.microsoft.com/addons/detail/markdown-diagrams/hceenoomhhdkjjijnmlclkpenkapfihe) | [🐙🔗](https://github.com/marcozaccari/markdown-diagrams-browser-extension/tree/master/doc/examples) |
|
||||
| Markdown Viewer | - | [🦊🔗](https://addons.mozilla.org/en-US/firefox/addon/markdown-viewer-chrome/) | - | - | [🐙🔗](https://github.com/simov/markdown-viewer) |
|
||||
| Extensions for Mermaid | - | - | [🔴🔗](https://addons.opera.com/en/extensions/details/extensions-for-mermaid/) | - | [🐙🔗](https://github.com/Stefan-S/mermaid-extension) |
|
||||
| Chrome Diagrammer | [🎡🔗](https://chrome.google.com/webstore/detail/chrome-diagrammer/bkpbgjmkomfoakfklcjeoegkklgjnnpk) | - | - | - | - |
|
||||
| Mermaid Diagrams | [🎡🔗](https://chrome.google.com/webstore/detail/mermaid-diagrams/phfcghedmopjadpojhmmaffjmfiakfil) | - | - | - | - |
|
||||
| Monkeys | [🎡🔗](https://chrome.google.com/webstore/detail/monkeys-mermaid-for-githu/cplfdpoajbclbgphaphphcldamfkjlgi) | - | - | - | - |
|
||||
| Mermaid Previewer | [🎡🔗](https://chrome.google.com/webstore/detail/mermaid-previewer/oidjnlhbegipkcklbdfnbkikplpghfdl) | - | - | - | - |
|
||||
| Name | Chrome Web Store | Firefox Add-ons | Opera | Edge | Source/Repository |
|
||||
| ------------------------ | ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
|
||||
| GitHub + Mermaid | - | [🦊🔗](https://addons.mozilla.org/firefox/addon/github-mermaid/) | - | - | [🐙🔗](https://github.com/BackMarket/github-mermaid-extension) |
|
||||
| Asciidoctor Live Preview | [🎡🔗](https://chromewebstore.google.com/detail/asciidoctorjs-live-previe/iaalpfgpbocpdfblpnhhgllgbdbchmia) | - | - | [🌀🔗](https://microsoftedge.microsoft.com/addons/detail/asciidoctorjs-live-previ/pefkelkanablhjdekgdahplkccnbdggd?hl=en-US) | - |
|
||||
| Diagram Tab | - | - | - | - | [🐙🔗](https://github.com/khafast/diagramtab) |
|
||||
| Markdown Diagrams | [🎡🔗](https://chromewebstore.google.com/detail/markdown-diagrams/pmoglnmodacnbbofbgcagndelmgaclel/) | [🦊🔗](https://addons.mozilla.org/en-US/firefox/addon/markdown-diagrams/) | [🔴🔗](https://addons.opera.com/en/extensions/details/markdown-diagrams/) | [🌀🔗](https://microsoftedge.microsoft.com/addons/detail/markdown-diagrams/hceenoomhhdkjjijnmlclkpenkapfihe) | [🐙🔗](https://github.com/marcozaccari/markdown-diagrams-browser-extension/tree/master/doc/examples) |
|
||||
| Markdown Viewer | - | [🦊🔗](https://addons.mozilla.org/en-US/firefox/addon/markdown-viewer-chrome/) | - | - | [🐙🔗](https://github.com/simov/markdown-viewer) |
|
||||
| Extensions for Mermaid | - | - | [🔴🔗](https://addons.opera.com/en/extensions/details/extensions-for-mermaid/) | - | [🐙🔗](https://github.com/Stefan-S/mermaid-extension) |
|
||||
| Chrome Diagrammer | [🎡🔗](https://chromewebstore.google.com/detail/chrome-diagrammer/bkpbgjmkomfoakfklcjeoegkklgjnnpk) | - | - | - | - |
|
||||
| Mermaid Diagrams | [🎡🔗](https://chromewebstore.google.com/detail/mermaid-diagrams/phfcghedmopjadpojhmmaffjmfiakfil) | - | - | - | - |
|
||||
| Monkeys | [🎡🔗](https://chromewebstore.google.com/detail/monkeys-mermaid-for-githu/cplfdpoajbclbgphaphphcldamfkjlgi) | - | - | - | - |
|
||||
| Mermaid Previewer | [🎡🔗](https://chromewebstore.google.com/detail/mermaid-previewer/oidjnlhbegipkcklbdfnbkikplpghfdl) | - | - | - | - |
|
||||
|
||||
### Other
|
||||
|
||||
- [Bisheng](https://www.npmjs.com/package/bisheng)
|
||||
- [bisheng-plugin-mermaid](https://github.com/yct21/bisheng-plugin-mermaid)
|
||||
- [Blazorade Mermaid: Render Mermaid diagrams in Blazor applications](https://github.com/Blazorade/Blazorade-Mermaid/wiki)
|
||||
- [Codemia: A tool to practice system design problems](https://codemia.io) ✅
|
||||
- [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||
- [Rendering Mermaid graphs](https://github.com/elixir-lang/ex_doc#rendering-mermaid-graphs)
|
||||
@@ -243,5 +246,5 @@ Communication tools and platforms
|
||||
- [reveal-ck-mermaid-plugin](https://github.com/tmtm/reveal-ck-mermaid-plugin)
|
||||
- [mermaid-isomorphic](https://github.com/remcohaszing/mermaid-isomorphic)
|
||||
- [mermaid-server: Generate diagrams using a HTTP request](https://github.com/TomWright/mermaid-server)
|
||||
- [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||
- [Rendering Mermaid graphs](https://github.com/elixir-lang/ex_doc#rendering-mermaid-graphs)
|
||||
|
||||
<!--- cspell:ignore Blazorade --->
|
||||
|
||||
@@ -18,7 +18,7 @@ Currently pending [IANA](https://www.iana.org/) recognition.
|
||||
|
||||
### Mermaid Discord workspace
|
||||
|
||||
We would love to see what you create with Mermaid. Please share your creations with us in our [Discord](https://discord.gg/wwtabKgp8y) server [#showcase](https://discord.com/channels/1079455296289788015/1079502635054399649) channel.
|
||||
We would love to see what you create with Mermaid. Please share your creations with us in our [Discord](https://discord.gg/AgrbSrBer3) server [#showcase](https://discord.com/channels/1079455296289788015/1079502635054399649) channel.
|
||||
|
||||
### Add to Mermaid Ecosystem
|
||||
|
||||
|
||||
@@ -74,3 +74,5 @@ graph LR;
|
||||
**Output**
|
||||
|
||||

|
||||
|
||||
<!--- cspell:ignore Elle Jaoude Neurodiverse graphbytes --->
|
||||
|
||||
@@ -17,7 +17,7 @@ sequenceDiagram
|
||||
participant Alice
|
||||
participant Bob
|
||||
Alice->>John: Hello John, how are you?
|
||||
loop Healthcheck
|
||||
loop HealthCheck
|
||||
John->>John: Fight against hypochondria
|
||||
end
|
||||
Note right of John: Rational thoughts <br/>prevail!
|
||||
|
||||
@@ -16,7 +16,7 @@ It is a JavaScript based diagramming and charting tool that renders Markdown-ins
|
||||
[](https://coveralls.io/github/mermaid-js/mermaid?branch=master)
|
||||
[](https://www.jsdelivr.com/package/npm/mermaid)
|
||||
[](https://www.npmjs.com/package/mermaid)
|
||||
[](https://discord.gg/wwtabKgp8y)
|
||||
[](https://discord.gg/AgrbSrBer3)
|
||||
[](https://twitter.com/mermaidjs_)
|
||||
|
||||
</div>
|
||||
|
||||
@@ -12,19 +12,6 @@ Create flowchart nodes, connect them with edges, update shapes, change colors, a
|
||||
|
||||
Read more about it in our latest [BLOG POST](https://www.mermaidchart.com/blog/posts/mermaid-chart-releases-new-visual-editor-for-flowcharts) and watch a [DEMO VIDEO](https://www.youtube.com/watch?v=5aja0gijoO0) on our YouTube page.
|
||||
|
||||
## 🎉 Mermaid Chart is running a Holiday promotion
|
||||
|
||||
### Use <span class="text-[#FE3470]">HOLIDAYS2023</span> to get a 14-day free trial and 25% off a Pro subscription
|
||||
|
||||
With a Pro subscription, you get access to:
|
||||
|
||||
- AI functionality
|
||||
- Team collaboration and multi-user editing
|
||||
- Unlimited diagrams and presentations
|
||||
- And more!
|
||||
|
||||
Redeem the promo code on the [Mermaid Chart website](https://www.mermaidchart.com/app/user/billing/checkout?coupon=HOLIDAYS2023).
|
||||
|
||||
## 📖 Blog posts
|
||||
|
||||
Visit our [Blog](./blog.md) to see the latest blog posts.
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
# Blog
|
||||
|
||||
## [How one data scientist uses Mermaid Chart to quickly and easily build flowcharts](https://www.mermaidchart.com/blog/posts/customer-spotlight-ari-tal/)
|
||||
|
||||
23 January 2024 · 4 mins
|
||||
|
||||
Read about how Ari Tal, a data scientist and founder of Leveling Up with XAI, utilizes Mermaid Chart for its easy-to-use flowchart creation capabilities to enhance his work in explainable AI (XAI).
|
||||
|
||||
## [Introducing Mermaid Chart’s JetBrains IDE Extension](https://www.mermaidchart.com/blog/posts/introducing-mermaid-charts-jetbrains-ide-extension/)
|
||||
|
||||
20 December 2023 · 5 mins
|
||||
|
||||
@@ -23,16 +23,16 @@
|
||||
"devDependencies": {
|
||||
"@iconify-json/carbon": "^1.1.16",
|
||||
"@unocss/reset": "^0.58.0",
|
||||
"@vite-pwa/vitepress": "^0.3.0",
|
||||
"@vite-pwa/vitepress": "^0.4.0",
|
||||
"@vitejs/plugin-vue": "^4.2.1",
|
||||
"fast-glob": "^3.2.12",
|
||||
"https-localhost": "^4.7.1",
|
||||
"pathe": "^1.1.0",
|
||||
"unocss": "^0.58.0",
|
||||
"unplugin-vue-components": "^0.26.0",
|
||||
"vite": "^4.4.12",
|
||||
"vite-plugin-pwa": "^0.17.0",
|
||||
"vitepress": "1.0.0-rc.39",
|
||||
"vite": "^4.5.2",
|
||||
"vite-plugin-pwa": "^0.19.0",
|
||||
"vitepress": "1.0.0-rc.42",
|
||||
"workbox-window": "^7.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
492
packages/mermaid/src/docs/syntax/block.md
Normal file
492
packages/mermaid/src/docs/syntax/block.md
Normal file
@@ -0,0 +1,492 @@
|
||||
---
|
||||
title: Block Diagram Syntax
|
||||
outline: 'deep' # shows all h3 headings in outline in Vitepress
|
||||
---
|
||||
|
||||
# Block Diagrams Documentation
|
||||
|
||||
## Introduction to Block Diagrams
|
||||
|
||||
```mermaid
|
||||
block-beta
|
||||
columns 1
|
||||
db(("DB"))
|
||||
blockArrowId6<[" "]>(down)
|
||||
block:ID
|
||||
A
|
||||
B["A wide one in the middle"]
|
||||
C
|
||||
end
|
||||
space
|
||||
D
|
||||
ID --> D
|
||||
C --> D
|
||||
style B fill:#969,stroke:#333,stroke-width:4px
|
||||
```
|
||||
|
||||
### Definition and Purpose
|
||||
|
||||
Block diagrams are an intuitive and efficient way to represent complex systems, processes, or architectures visually. They are composed of blocks and connectors, where blocks represent the fundamental components or functions, and connectors show the relationship or flow between these components. This method of diagramming is essential in various fields such as engineering, software development, and process management.
|
||||
|
||||
The primary purpose of block diagrams is to provide a high-level view of a system, allowing for easy understanding and analysis without delving into the intricate details of each component. This makes them particularly useful for simplifying complex systems and for explaining the overall structure and interaction of components within a system.
|
||||
|
||||
Many people use mermaid flowcharts for this purpose. A side-effect of this is that the automatic layout sometimes move shapes to positions that the diagram maker does not want. Block diagrams use a different approach. In this diagram we give the author full control over where the shapes are positioned.
|
||||
|
||||
### General Use Cases
|
||||
|
||||
Block diagrams have a wide range of applications across various industries and disciplines. Some of the key use cases include:
|
||||
|
||||
- **Software Architecture**: In software development, block diagrams can be used to illustrate the architecture of a software application. This includes showing how different modules or services interact, data flow, and high-level component interaction.
|
||||
|
||||
- **Network Diagrams**: Block diagrams are ideal for representing network architectures in IT and telecommunications. They can depict how different network devices and services are interconnected, including routers, switches, firewalls, and the flow of data across the network.
|
||||
|
||||
- **Process Flowcharts**: In business and manufacturing, block diagrams can be employed to create process flowcharts. These flowcharts represent various stages of a business or manufacturing process, helping to visualize the sequence of steps, decision points, and the flow of control.
|
||||
|
||||
- **Electrical Systems**: Engineers use block diagrams to represent electrical systems and circuitry. They can illustrate the high-level structure of an electrical system, the interaction between different electrical components, and the flow of electrical currents.
|
||||
|
||||
- **Educational Purposes**: Block diagrams are also extensively used in educational materials to explain complex concepts and systems in a simplified manner. They help in breaking down and visualizing scientific theories, engineering principles, and technological systems.
|
||||
|
||||
These examples demonstrate the versatility of block diagrams in providing clear and concise representations of complex systems. Their simplicity and clarity make them a valuable tool for professionals across various fields to communicate complex ideas effectively.
|
||||
|
||||
In the following sections, we will delve into the specifics of creating and manipulating block diagrams using Mermaid, covering everything from basic syntax to advanced configurations and styling.
|
||||
|
||||
Creating block diagrams with Mermaid is straightforward and accessible. This section introduces the basic syntax and structure needed to start building simple diagrams. Understanding these foundational concepts is key to efficiently utilizing Mermaid for more complex diagramming tasks.
|
||||
|
||||
### Simple Block Diagrams
|
||||
|
||||
#### Basic Structure
|
||||
|
||||
At its core, a block diagram consists of blocks representing different entities or components. In Mermaid, these blocks are easily created using simple text labels. The most basic form of a block diagram can be a series of blocks without any connectors.
|
||||
|
||||
**Example - Simple Block Diagram**:
|
||||
To create a simple block diagram with three blocks labeled 'a', 'b', and 'c', the syntax is as follows:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
a b c
|
||||
```
|
||||
|
||||
This example will produce a horizontal sequence of three blocks. Each block is automatically spaced and aligned for optimal readability.
|
||||
|
||||
### Defining the number of columns to use
|
||||
|
||||
#### Column Usage
|
||||
|
||||
While simple block diagrams are linear and straightforward, more complex systems may require a structured layout. Mermaid allows for the organization of blocks into multiple columns, facilitating the creation of more intricate and detailed diagrams.
|
||||
|
||||
**Example - Multi-Column Diagram:**
|
||||
In scenarios where you need to distribute blocks across multiple columns, you can specify the number of columns and arrange the blocks accordingly. Here's how to create a block diagram with three columns and four blocks, where the fourth block appears in a second row:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
columns 3
|
||||
a b c d
|
||||
```
|
||||
|
||||
This syntax instructs Mermaid to arrange the blocks 'a', 'b', 'c', and 'd' across three columns, wrapping to the next row as needed. This feature is particularly useful for representing layered or multi-tiered systems, such as network layers or hierarchical structures.
|
||||
|
||||
These basic building blocks of Mermaid's block diagrams provide a foundation for more complex diagramming. The simplicity of the syntax allows for quick creation and iteration of diagrams, making it an efficient tool for visualizing ideas and concepts. In the next section, we'll explore advanced block configuration options, including setting block widths and creating composite blocks.
|
||||
|
||||
## 3. Advanced Block Configuration
|
||||
|
||||
Building upon the basics, this section delves into more advanced features of block diagramming in Mermaid. These features allow for greater flexibility and complexity in diagram design, accommodating a wider range of use cases and scenarios.
|
||||
|
||||
### Setting Block Width
|
||||
|
||||
#### Spanning Multiple Columns
|
||||
|
||||
In more complex diagrams, you may need blocks that span multiple columns to emphasize certain components or to represent larger entities. Mermaid allows for the adjustment of block widths to cover multiple columns, enhancing the diagram's readability and structure.
|
||||
|
||||
**Example - Block Spanning Multiple Columns**:
|
||||
To create a block diagram where one block spans across two columns, you can specify the desired width for each block:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
columns 3
|
||||
a["A label"] b:2 c:2 d
|
||||
```
|
||||
|
||||
In this example, the block labeled "A wide one" spans two columns, while blocks 'b', 'c', and 'd' are allocated their own columns. This flexibility in block sizing is crucial for accurately representing systems with components of varying significance or size.
|
||||
|
||||
### Creating Composite Blocks
|
||||
|
||||
#### Nested Blocks
|
||||
|
||||
Composite blocks, or blocks within blocks, are an advanced feature in Mermaid's block diagram syntax. They allow for the representation of nested or hierarchical systems, where one component encompasses several subcomponents.
|
||||
|
||||
**Example - Composite Blocks:**
|
||||
Creating a composite block involves defining a parent block and then nesting other blocks within it. Here's how to define a composite block with nested elements:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
block
|
||||
D
|
||||
end
|
||||
A["A: I am a wide one"]
|
||||
```
|
||||
|
||||
In this syntax, 'D' is a nested block within a larger parent block. This feature is particularly useful for depicting complex structures, such as a server with multiple services or a department within a larger organizational framework.
|
||||
|
||||
### Column Width Dynamics
|
||||
|
||||
#### Adjusting Widths
|
||||
|
||||
Mermaid also allows for dynamic adjustment of column widths based on the content of the blocks. The width of the columns is determined by the widest block in the column, ensuring that the diagram remains balanced and readable.
|
||||
|
||||
**Example - Dynamic Column Widths:**
|
||||
In diagrams with varying block sizes, Mermaid automatically adjusts the column widths to fit the largest block in each column. Here's an example:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
columns 3
|
||||
a:3
|
||||
block:group1:2
|
||||
columns 2
|
||||
h i j k
|
||||
end
|
||||
g
|
||||
block:group2:3
|
||||
%% columns auto (default)
|
||||
l m n o p q r
|
||||
end
|
||||
```
|
||||
|
||||
This example demonstrates how Mermaid dynamically adjusts the width of the columns to accommodate the widest block, in this case, 'a' and the composite block 'e'. This dynamic adjustment is essential for creating visually balanced and easy-to-understand diagrams.
|
||||
|
||||
With these advanced configuration options, Mermaid's block diagrams can be tailored to represent a wide array of complex systems and structures. The flexibility offered by these features enables users to create diagrams that are both informative and visually appealing. In the following sections, we will explore further capabilities, including different block shapes and linking options.
|
||||
|
||||
## 4. Block Varieties and Shapes
|
||||
|
||||
Mermaid's block diagrams are not limited to standard rectangular shapes. A variety of block shapes are available, allowing for a more nuanced and tailored representation of different types of information or entities. This section outlines the different block shapes you can use in Mermaid and their specific applications.
|
||||
|
||||
### Standard and Special Block Shapes
|
||||
|
||||
Mermaid supports a range of block shapes to suit different diagramming needs, from basic geometric shapes to more specialized forms.
|
||||
|
||||
#### Example - Round Edged Block
|
||||
|
||||
To create a block with round edges, which can be used to represent a softer or more flexible component:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1("This is the text in the box")
|
||||
```
|
||||
|
||||
#### Example - Stadium-Shaped Block
|
||||
|
||||
A stadium-shaped block, resembling an elongated circle, can be used for components that are process-oriented:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1(["This is the text in the box"])
|
||||
```
|
||||
|
||||
#### Example - Subroutine Shape
|
||||
|
||||
For representing subroutines or contained processes, a block with double vertical lines is useful:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1[["This is the text in the box"]]
|
||||
```
|
||||
|
||||
#### Example - Cylindrical Shape
|
||||
|
||||
The cylindrical shape is ideal for representing databases or storage components:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1[("Database")]
|
||||
```
|
||||
|
||||
#### Example - Circle Shape
|
||||
|
||||
A circle can be used for centralized or pivotal components:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1(("This is the text in the circle"))
|
||||
```
|
||||
|
||||
#### Example - Asymmetric, Rhombus, and Hexagon Shapes
|
||||
|
||||
For decision points, use a rhombus, and for unique or specialized processes, asymmetric and hexagon shapes can be utilized:
|
||||
|
||||
**Asymmetric**
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1>"This is the text in the box"]
|
||||
```
|
||||
|
||||
**Rhombus**
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1{"This is the text in the box"}
|
||||
```
|
||||
|
||||
**Hexagon**
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1{{"This is the text in the box"}}
|
||||
```
|
||||
|
||||
#### Example - Parallelogram and Trapezoid Shapes
|
||||
|
||||
Parallelogram and trapezoid shapes are perfect for inputs/outputs and transitional processes:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1[/"This is the text in the box"/]
|
||||
id2[\"This is the text in the box"\]
|
||||
A[/"Christmas"\]
|
||||
B[\"Go shopping"/]
|
||||
```
|
||||
|
||||
#### Example - Double Circle
|
||||
|
||||
For highlighting critical or high-priority components, a double circle can be effective:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1((("This is the text in the circle")))
|
||||
```
|
||||
|
||||
### Block Arrows and Space Blocks
|
||||
|
||||
Mermaid also offers unique shapes like block arrows and space blocks for directional flow and spacing.
|
||||
|
||||
#### Example - Block Arrows
|
||||
|
||||
Block arrows can visually indicate direction or flow within a process:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
blockArrowId<["Label"]>(right)
|
||||
blockArrowId2<["Label"]>(left)
|
||||
blockArrowId3<["Label"]>(up)
|
||||
blockArrowId4<["Label"]>(down)
|
||||
blockArrowId5<["Label"]>(x)
|
||||
blockArrowId6<["Label"]>(y)
|
||||
blockArrowId6<["Label"]>(x, down)
|
||||
```
|
||||
|
||||
#### Example - Space Blocks
|
||||
|
||||
Space blocks can be used to create intentional empty spaces in the diagram, which is useful for layout and readability:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
columns 3
|
||||
a space b
|
||||
c d e
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
ida space:3 idb idc
|
||||
```
|
||||
|
||||
Note that you can set how many columns the space block occupied using the number notation `space:num` where num is a number indicating the num columns width. You can also use `space` which defaults to one column.
|
||||
|
||||
The variety of shapes and special blocks in Mermaid enhances the expressive power of block diagrams, allowing for more accurate and context-specific representations. These options give users the flexibility to create diagrams that are both informative and visually appealing. In the next sections, we will explore the ways to connect these blocks and customize their appearance.
|
||||
|
||||
### Standard and Special Block Shapes
|
||||
|
||||
Discuss the various shapes available for blocks, including standard shapes and special forms like block arrows and space blocks.
|
||||
|
||||
## 5. Connecting Blocks with Edges
|
||||
|
||||
One of the key features of block diagrams in Mermaid is the ability to connect blocks using various types of edges or links. This section explores the different ways blocks can be interconnected to represent relationships and flows between components.
|
||||
|
||||
### Basic Linking and Arrow Types
|
||||
|
||||
The most fundamental aspect of connecting blocks is the use of arrows or links. These connectors depict the relationships or the flow of information between the blocks. Mermaid offers a range of arrow types to suit different diagramming needs.
|
||||
|
||||
**Example - Basic Links**
|
||||
|
||||
A simple link with an arrow can be created to show direction or flow from one block to another:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
A space B
|
||||
A-->B
|
||||
```
|
||||
|
||||
This example illustrates a direct connection from block 'A' to block 'B', using a straightforward arrow.
|
||||
|
||||
This syntax creates a line connecting 'A' and 'B', implying a relationship or connection without indicating a specific direction.
|
||||
|
||||
### Text on Links
|
||||
|
||||
In addition to connecting blocks, it's often necessary to describe or label the relationship. Mermaid allows for the inclusion of text on links, providing context to the connections.
|
||||
|
||||
Example - Text with Links
|
||||
To add text to a link, the syntax includes the text within the link definition:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
A space:2 B
|
||||
A-- "X" -->B
|
||||
```
|
||||
|
||||
This example show how to add descriptive text to the links, enhancing the information conveyed by the diagram.
|
||||
|
||||
Example - Edges and Styles:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
columns 1
|
||||
db(("DB"))
|
||||
blockArrowId6<[" "]>(down)
|
||||
block:ID
|
||||
A
|
||||
B["A wide one in the middle"]
|
||||
C
|
||||
end
|
||||
space
|
||||
D
|
||||
ID --> D
|
||||
C --> D
|
||||
style B fill:#939,stroke:#333,stroke-width:4px
|
||||
```
|
||||
|
||||
## 6. Styling and Customization
|
||||
|
||||
Beyond the structure and layout of block diagrams, Mermaid offers extensive styling options. These customization features allow for the creation of more visually distinctive and informative diagrams. This section covers how to apply individual styles to blocks and how to use classes for consistent styling across multiple elements.
|
||||
|
||||
### Individual Block Styling
|
||||
|
||||
Mermaid enables detailed styling of individual blocks, allowing you to apply various CSS properties such as color, stroke, and border thickness. This feature is especially useful for highlighting specific parts of a diagram or for adhering to certain visual themes.
|
||||
|
||||
#### Example - Styling a Single Block
|
||||
|
||||
To apply custom styles to a block, you can use the `style` keyword followed by the block identifier and the desired CSS properties:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
id1 space id2
|
||||
id1("Start")-->id2("Stop")
|
||||
style id1 fill:#636,stroke:#333,stroke-width:4px
|
||||
style id2 fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5
|
||||
```
|
||||
|
||||
In this example, a class named 'blue' is defined and applied to block 'A', while block 'B' receives individual styling. This demonstrates the flexibility of Mermaid in applying both shared and unique styles within the same diagram.
|
||||
|
||||
The ability to style blocks individually or through classes provides a powerful tool for enhancing the visual impact and clarity of block diagrams. Whether emphasizing certain elements or maintaining a cohesive design across the diagram, these styling capabilities are central to effective diagramming. The next sections will present practical examples and use cases, followed by tips for troubleshooting common issues.
|
||||
|
||||
### 7. Practical Examples and Use Cases
|
||||
|
||||
The versatility of Mermaid's block diagrams becomes evident when applied to real-world scenarios. This section provides practical examples demonstrating the application of various features discussed in previous sections. These examples showcase how block diagrams can be used to represent complex systems and processes in an accessible and informative manner.
|
||||
|
||||
### Detailed Examples Illustrating Various Features
|
||||
|
||||
Combining the elements of structure, linking, and styling, we can create comprehensive diagrams that serve specific purposes in different contexts.
|
||||
|
||||
#### Example - System Architecture
|
||||
|
||||
Illustrating a simple software system architecture with interconnected components:
|
||||
|
||||
```mermaid
|
||||
block-beta
|
||||
columns 3
|
||||
Frontend blockArrowId6<[" "]>(right) Backend
|
||||
space:2 down<[" "]>(down)
|
||||
Disk left<[" "]>(left) Database[("Database")]
|
||||
|
||||
classDef front fill:#696,stroke:#333;
|
||||
classDef back fill:#969,stroke:#333;
|
||||
class Frontend front
|
||||
class Backend,Database back
|
||||
```
|
||||
|
||||
This example shows a basic architecture with a frontend, backend, and database. The blocks are styled to differentiate between types of components.
|
||||
|
||||
#### Example - Business Process Flow
|
||||
|
||||
Representing a business process flow with decision points and multiple stages:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
columns 3
|
||||
Start(("Start")) space:2
|
||||
down<[" "]>(down) space:2
|
||||
Decision{{"Make Decision"}} right<["Yes"]>(right) Process1["Process A"]
|
||||
downAgain<["No"]>(down) space r3<["Done"]>(down)
|
||||
Process2["Process B"] r2<["Done"]>(right) End(("End"))
|
||||
|
||||
style Start fill:#969;
|
||||
style End fill:#696;
|
||||
```
|
||||
|
||||
These practical examples and scenarios underscore the utility of Mermaid block diagrams in simplifying and effectively communicating complex information across various domains.
|
||||
|
||||
The next section, 'Troubleshooting and Common Issues', will provide insights into resolving common challenges encountered when working with Mermaid block diagrams, ensuring a smooth diagramming experience.
|
||||
|
||||
## 8. Troubleshooting and Common Issues
|
||||
|
||||
Working with Mermaid block diagrams can sometimes present challenges, especially as the complexity of the diagrams increases. This section aims to provide guidance on resolving common issues and offers tips for managing more intricate diagram structures.
|
||||
|
||||
### Common Syntax Errors
|
||||
|
||||
Understanding and avoiding common syntax errors is key to a smooth experience with Mermaid diagrams.
|
||||
|
||||
#### Example - Incorrect Linking
|
||||
|
||||
A common mistake is incorrect linking syntax, which can lead to unexpected results or broken diagrams:
|
||||
|
||||
```
|
||||
block-beta
|
||||
A - B
|
||||
```
|
||||
|
||||
**Correction**:
|
||||
Ensure that links between blocks are correctly specified with arrows (--> or ---) to define the direction and type of connection. Also remember that one of the fundaments for block diagram is to give the author full control of where the boxes are positioned so in the example you need to add a space between the boxes:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
A space B
|
||||
A --> B
|
||||
```
|
||||
|
||||
#### Example - Misplaced Styling
|
||||
|
||||
Applying styles in the wrong context or with incorrect syntax can lead to blocks not being styled as intended:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
A
|
||||
style A fill#969;
|
||||
```
|
||||
|
||||
**Correction:**
|
||||
Correct the syntax by ensuring proper separation of style properties with commas and using the correct CSS property format:
|
||||
|
||||
```mermaid-example
|
||||
block-beta
|
||||
A
|
||||
style A fill:#969,stroke:#333;
|
||||
|
||||
```
|
||||
|
||||
### Tips for Complex Diagram Structures
|
||||
|
||||
Managing complexity in Mermaid diagrams involves planning and employing best practices.
|
||||
|
||||
#### Modular Design
|
||||
|
||||
Break down complex diagrams into smaller, more manageable components. This approach not only makes the diagram easier to understand but also simplifies the creation and maintenance process.
|
||||
|
||||
#### Consistent Styling
|
||||
|
||||
Use classes to maintain consistent styling across similar elements. This not only saves time but also ensures a cohesive and professional appearance.
|
||||
|
||||
#### Comments and Documentation
|
||||
|
||||
Use comments with `%%` within the Mermaid syntax to document the purpose of various parts of the diagram. This practice is invaluable for maintaining clarity, especially when working in teams or returning to a diagram after some time.
|
||||
|
||||
With these troubleshooting tips and best practices, you can effectively manage and resolve common issues in Mermaid block diagrams. The final section, 'Conclusion', will summarize the key points covered in this documentation and invite user feedback for continuous improvement.
|
||||
@@ -135,7 +135,7 @@ The following unfinished features are not supported in the short term.
|
||||
- [x] Rel_L, Rel_Left
|
||||
- [x] Rel_R, Rel_Right
|
||||
- [x] Rel_Back
|
||||
- [x] RelIndex \* Compatible with C4-Plantuml syntax, but ignores the index parameter. The sequence number is determined by the order in which the rel statements are written.
|
||||
- [x] RelIndex \* Compatible with C4-PlantUML syntax, but ignores the index parameter. The sequence number is determined by the order in which the rel statements are written.
|
||||
|
||||
- [ ] Custom tags/stereotypes support and skin param updates
|
||||
- [ ] AddElementTag(tagStereo, ?bgColor, ?fontColor, ?borderColor, ?shadowing, ?shape, ?sprite, ?techn, ?legendText, ?legendSprite): Introduces a new element tag. The styles of the tagged elements are updated and the tag is displayed in the calculated legend.
|
||||
@@ -218,7 +218,7 @@ UpdateRelStyle(customerA, bankA, $offsetY="60")
|
||||
Person(customer, Customer, "A customer of the bank, with personal bank accounts", $tags="v1.0")
|
||||
|
||||
Container_Boundary(c1, "Internet Banking") {
|
||||
Container(spa, "Single-Page App", "JavaScript, Angular", "Provides all the Internet banking functionality to cutomers via their web browser")
|
||||
Container(spa, "Single-Page App", "JavaScript, Angular", "Provides all the Internet banking functionality to customers via their web browser")
|
||||
Container_Ext(mobile_app, "Mobile App", "C#, Xamarin", "Provides a limited subset of the Internet banking functionality to customers via their mobile device")
|
||||
Container(web_app, "Web Application", "Java, Spring MVC", "Delivers the static content and the Internet banking SPA")
|
||||
ContainerDb(database, "Database", "SQL Database", "Stores user registration information, hashed auth credentials, access logs, etc.")
|
||||
@@ -367,3 +367,5 @@ UpdateRelStyle(customerA, bankA, $offsetY="60")
|
||||
UpdateRelStyle(db, db2, $offsetY="-10")
|
||||
|
||||
```
|
||||
|
||||
<!--- cspell:ignore bigbank bigbankdb techn mbsfacade --->
|
||||
|
||||
@@ -143,7 +143,7 @@ class BankAccount{
|
||||
|
||||
#### Generic Types
|
||||
|
||||
Generics can be representated as part of a class definition, and for class members/return types. In order to denote an item as generic, you enclose that type within `~` (**tilde**). **Nested** type declarations such as `List<List<int>>` are supported, though generics that include a comma are currently not supported. (such as `List<List<K, V>>`)
|
||||
Generics can be represented as part of a class definition, and for class members/return types. In order to denote an item as generic, you enclose that type within `~` (**tilde**). **Nested** type declarations such as `List<List<int>>` are supported, though generics that include a comma are currently not supported. (such as `List<List<K, V>>`)
|
||||
|
||||
> _note_ when a generic is used within a class definition, the generic type is NOT considered part of the class name. i.e.: for any syntax which required you to reference the class name, you need to drop the type part of the definition. This also means that mermaid does not currently support having two classes with the same name, but different generic types.
|
||||
|
||||
|
||||
@@ -217,3 +217,5 @@ The following CSS class selectors are available for richer styling:
|
||||
| `.er.relationshipLabel` | The label for a relationship |
|
||||
| `.er.relationshipLabelBox` | The box surrounding a relationship label |
|
||||
| `.er.relationshipLine` | The line representing a relationship between entities |
|
||||
|
||||
<!--- cspell:locale en,en-gb --->
|
||||
|
||||
@@ -96,7 +96,7 @@ sequenceDiagram
|
||||
participant Alice
|
||||
participant Bob
|
||||
Alice->>John: Hello John, how are you?
|
||||
loop Healthcheck
|
||||
loop HealthCheck
|
||||
John->>John: Fight against hypochondria
|
||||
end
|
||||
Note right of John: Rational thoughts<br/>prevail...
|
||||
@@ -156,3 +156,5 @@ gitGraph:
|
||||
branch b2
|
||||
commit
|
||||
```
|
||||
|
||||
<!--- cspell:ignore Ashish newbranch --->
|
||||
|
||||
@@ -836,3 +836,5 @@ mermaid.flowchartConfig = {
|
||||
width: 100%
|
||||
}
|
||||
```
|
||||
|
||||
<!--- cspell:ignore lagom --->
|
||||
|
||||
@@ -221,12 +221,12 @@ gantt
|
||||
```
|
||||
|
||||
```warning
|
||||
`millisecond` and `second` support was added in vMERMAID_RELEASE_VERSION
|
||||
`millisecond` and `second` support was added in v10.3.0
|
||||
```
|
||||
|
||||
## Output in compact mode
|
||||
|
||||
The compact mode allows you to display multiple tasks in the same row. Compact mode can be enabled for a gantt chart by setting the display mode of the graph via preceeding YAML settings.
|
||||
The compact mode allows you to display multiple tasks in the same row. Compact mode can be enabled for a gantt chart by setting the display mode of the graph via preceding YAML settings.
|
||||
|
||||
```mermaid
|
||||
---
|
||||
|
||||
@@ -277,6 +277,7 @@ In Mermaid, you have the option to configure the gitgraph diagram. You can confi
|
||||
- `showCommitLabel` : Boolean, default is `true`. If set to `false`, the commit labels are not shown in the diagram.
|
||||
- `mainBranchName` : String, default is `main`. The name of the default/root branch.
|
||||
- `mainBranchOrder` : Position of the main branch in the list of branches. default is `0`, meaning, by default `main` branch is the first in the order.
|
||||
- `parallelCommits`: Boolean, default is `false`. If set to `true`, commits x distance away from the parent are shown at the same level in the diagram.
|
||||
|
||||
Let's look at them one by one.
|
||||
|
||||
@@ -568,6 +569,46 @@ Usage example:
|
||||
commit
|
||||
```
|
||||
|
||||
## Parallel commits (v10.8.0+)
|
||||
|
||||
Commits in Mermaid display temporal information in gitgraph by default. For example if two commits are one commit away from its parent, the commit that was made earlier is rendered closer to its parent. You can turn this off by enabling the `parallelCommits` flag.
|
||||
|
||||
### Temporal Commits (default, `parallelCommits: false`)
|
||||
|
||||
```mermaid-example
|
||||
---
|
||||
config:
|
||||
gitGraph:
|
||||
parallelCommits: false
|
||||
---
|
||||
gitGraph:
|
||||
commit
|
||||
branch develop
|
||||
commit
|
||||
commit
|
||||
checkout main
|
||||
commit
|
||||
commit
|
||||
```
|
||||
|
||||
### Parallel commits (`parallelCommits: true`)
|
||||
|
||||
```mermaid-example
|
||||
---
|
||||
config:
|
||||
gitGraph:
|
||||
parallelCommits: true
|
||||
---
|
||||
gitGraph:
|
||||
commit
|
||||
branch develop
|
||||
commit
|
||||
commit
|
||||
checkout main
|
||||
commit
|
||||
commit
|
||||
```
|
||||
|
||||
## Themes
|
||||
|
||||
Mermaid supports a bunch of pre-defined themes which you can use to find the right one for you. PS: you can actually override an existing theme's variable to get your own custom theme going. Learn more about theming your diagram [here](../config/theming.md).
|
||||
@@ -959,7 +1000,7 @@ See how the commit label color and background color are changed to the values sp
|
||||
|
||||
### Customizing Commit Label Font Size
|
||||
|
||||
You can customize commit using the `commitLabelFontSize` theme variables for changing in the font soze of the commit label .
|
||||
You can customize commit using the `commitLabelFontSize` theme variables for changing in the font size of the commit label .
|
||||
|
||||
Example:
|
||||
Now let's override the default values for the `commitLabelFontSize` variable:
|
||||
@@ -989,7 +1030,7 @@ See how the commit label font size changed.
|
||||
|
||||
### Customizing Tag Label Font Size
|
||||
|
||||
You can customize commit using the `tagLabelFontSize` theme variables for changing in the font soze of the tag label .
|
||||
You can customize commit using the `tagLabelFontSize` theme variables for changing in the font size of the tag label .
|
||||
|
||||
Example:
|
||||
Now let's override the default values for the `tagLabelFontSize` variable:
|
||||
|
||||
@@ -204,3 +204,8 @@ From version 9.4.0 you can simplify this code to:
|
||||
```
|
||||
|
||||
You can also refer the implementation in the live editor [here](https://github.com/mermaid-js/mermaid-live-editor/blob/develop/src/lib/util/mermaid.ts) to see how the async loading is done.
|
||||
|
||||
<!---
|
||||
cspell:locale en,en-gb
|
||||
cspell:ignore Buzan
|
||||
--->
|
||||
|
||||
@@ -156,3 +156,5 @@ This example uses all features of the diagram.
|
||||
test_entity3 - verifies -> test_req5
|
||||
test_req <- copies - test_entity2
|
||||
```
|
||||
|
||||
<!--- cspell:ignore reqs --->
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user