diff --git a/packages/mermaid/src/rendering-util/rendering-elements/nodes.js b/packages/mermaid/src/rendering-util/rendering-elements/nodes.js index dfdf5a8655..866dff9aa7 100644 --- a/packages/mermaid/src/rendering-util/rendering-elements/nodes.js +++ b/packages/mermaid/src/rendering-util/rendering-elements/nodes.js @@ -29,6 +29,7 @@ import { labelRect } from './shapes/labelRect.js'; import { triangle } from './shapes/triangle.js'; import { halfRoundedRectangle } from './shapes/halfRoundedRectangle.js'; import { curvedTrapezoid } from './shapes/curvedTrapezoid.js'; +import { slopedRect } from './shapes/slopedRect.js'; const shapes = { state, @@ -61,6 +62,7 @@ const shapes = { triangle, halfRoundedRectangle, curvedTrapezoid, + slopedRect, }; const nodeElems = new Map(); diff --git a/packages/mermaid/src/rendering-util/rendering-elements/shapes/slopedRect.ts b/packages/mermaid/src/rendering-util/rendering-elements/shapes/slopedRect.ts new file mode 100644 index 0000000000..720a7ce16b --- /dev/null +++ b/packages/mermaid/src/rendering-util/rendering-elements/shapes/slopedRect.ts @@ -0,0 +1,68 @@ +import { labelHelper, updateNodeBounds, getNodeClasses, createPathFromPoints } from './util.js'; +import intersect from '../intersect/index.js'; +import type { Node } from '$root/rendering-util/types.d.ts'; +import { + styles2String, + userNodeOverrides, +} from '$root/rendering-util/rendering-elements/shapes/handdrawnStyles.js'; +import rough from 'roughjs'; + +function createSlopedRectPathD(x: number, y: number, totalWidth: number, totalHeight: number) { + const rw = totalWidth; + const rh = totalHeight; + + const points = [ + { x, y }, + { x: x + rw, y: y - rh * 0.6 }, + { x: x + rw, y: y + rh }, + { x, y: y + rh }, + ]; + + const rectPath = createPathFromPoints(points); + + return rectPath; +} + +export const slopedRect = async (parent: SVGAElement, node: Node) => { + const { labelStyles, nodeStyles } = styles2String(node); + node.labelStyle = labelStyles; + const { shapeSvg, bbox } = await labelHelper(parent, node, getNodeClasses(node)); + const w = bbox.width + node.padding; + const h = bbox.height + node.padding; + + const { cssStyles } = node; + + // @ts-ignore - rough is not typed + const rc = rough.svg(shapeSvg); + const options = userNodeOverrides(node, {}); + + if (node.look !== 'handdrawn') { + options.roughness = 0; + options.fillStyle = 'solid'; + } + + const pathData = createSlopedRectPathD(0, 0, w, h); + const shapeNode = rc.path(pathData, options); + + const polygon = shapeSvg.insert(() => shapeNode, ':first-child'); + polygon.attr('class', 'basic label-container'); + + if (cssStyles) { + polygon.attr('style', cssStyles); + } + + if (nodeStyles) { + polygon.attr('style', nodeStyles); + } + + polygon.attr('transform', `translate(${-w / 2}, ${-h / 2})`); + + updateNodeBounds(node, polygon); + + node.intersect = function (point) { + const pos = intersect.polygon(node, point); + return pos; + }; + + return shapeSvg; +};