docs/adding-new-shape.md
Warning
THIS IS AN AUTOGENERATED FILE. DO NOT EDIT.
Please edit the corresponding file in /packages/mermaid/src/docs/adding-new-shape.md.
This library provides a collection of custom SVG shapes, utilities, and helpers for generating diagram components. The shapes are designed to be used within an SVG container and include a variety of common and complex shapes.
Before starting with shape creation, it's essential to familiarize yourself with the utilities provided in the utils.ts file from packages/mermaid/src/rendering-util/rendering-elements/shapes/util.js. These utilities are designed to assist with various aspects of SVG shape manipulation and ensure consistent and accurate rendering.
labelHelperupdateNodeBoundsinsertPolygonShapegetNodeClassescreatePathFromPointsgenerateFullSineWavePointsTo utilize these utilities, simply import them from the utils.ts file into your shape creation script. These helpers will streamline the process of building and customizing SVG shapes, ensuring consistent results across your projects.
import {
labelHelper,
updateNodeBounds,
insertPolygonShape,
getNodeClasses,
createPathFromPoints,
generateFullSineWavePoints,
} from './utils.ts';
Here’s a basic example of how you might use some of these utilities:
import { labelHelper, insertPolygonShape } from './utils.ts';
const svgContainer = document.getElementById('svgContainer');
// Insert a polygon shape
insertPolygonShape(svgContainer /* shape-specific parameters */);
// Create and insert a label inside the shape
labelHelper(svgContainer /* label-specific parameters */);
To add a new shape:
Create the shape function: Create a new file of name of the shape and export a function in the shapes directory that generates your shape. The file and function should follow the pattern used in existing shapes and return an SVG element.
Example:
import { Node, RenderOptions } from '../../types.ts';
export const myNewShape = async (
parent: SVGAElement,
node: Node,
renderOptions: RenderOptions
) => {
// Create your shape here
const shape = parent.insert('g').attr('class', 'my-new-shape');
// Add other elements or styles as needed
return shape;
};
Register the shape: Add your shape to the shapes object in the main shapes module. This allows your shape to be recognized and used within the system.
Example:
import { myNewShape } from './shapes/myNewShape';
const shapes = {
...,
{
semanticName: 'My Shape',
name: 'Shape Name',
shortName: '<short-name>',
description: '<Description for the shape>',
aliases: ['<alias-one>', '<al-on>', '<alias-two>', '<al-two>'],
handler: myNewShape,
},
};
This contains algorithms and utilities for calculating intersection points for various shapes in SVG. Arrow intersection points are crucial for accurately determining where arrows connect with shapes. Ensuring precise intersection points enhances the clarity and accuracy of flowcharts and diagrams.
EllipseCalculates the intersection points for an ellipse.
Usage:
import intersectEllipse from './intersect-ellipse.js';
const intersection = intersectEllipse(node, rx, ry, point);
node: The SVG node element.rx: The x-radius of the ellipse.ry: The y-radius of the ellipse.point: The point from which the intersection is calculated.intersectRectCalculates the intersection points for a rectangle.
Usage:
import intersectRect from './intersect-rect.js';
const intersection = intersectRect(node, point);
node: The SVG node element.point: The point from which the intersection is calculated.intersectPolygonCalculates the intersection points for a polygon.
Usage:
import intersectPolygon from './intersect-polygon.js';
const intersection = intersectPolygon(node, polyPoints, point);
node: The SVG node element.polyPoints: Array of points defining the polygon.point: The point from which the intersection is calculated.To ensure the robustness of the flowchart shapes, there are implementation of comprehensive Cypress test cases in newShapes.spec.ts file. These tests cover various aspects such as:
bowTieRect, waveRectangle, trapezoidalPentagon, etc.classic and handDrawn).TB (Top -> Bottom)BT (Bottom -> Top)LR (Left -> Right)RL (Right -> Left)htmlLabels:true and htmlLabels:falseclassDef to apply custom classes and testing their impact.To run the Cypress tests, follow these steps:
Ensure you have all dependencies installed by running:
pnpm install
Start the Cypress test runner:
cypress open --env updateSnapshots=true
Select the test suite from the Cypress interface to run all the flowchart shape tests.