From 933f3f391c3f7d6b0c6fe31fbcadc2b19cfbc68c Mon Sep 17 00:00:00 2001 From: omkarht Date: Fri, 2 Aug 2024 17:15:43 +0530 Subject: [PATCH] feat: added new Bowtie Rectangle shape --- .../rendering-elements/nodes.js | 2 + .../rendering-elements/shapes/bowTieRect.ts | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 packages/mermaid/src/rendering-util/rendering-elements/shapes/bowTieRect.ts diff --git a/packages/mermaid/src/rendering-util/rendering-elements/nodes.js b/packages/mermaid/src/rendering-util/rendering-elements/nodes.js index 866dff9aa..b6f0b0b7f 100644 --- a/packages/mermaid/src/rendering-util/rendering-elements/nodes.js +++ b/packages/mermaid/src/rendering-util/rendering-elements/nodes.js @@ -30,6 +30,7 @@ import { triangle } from './shapes/triangle.js'; import { halfRoundedRectangle } from './shapes/halfRoundedRectangle.js'; import { curvedTrapezoid } from './shapes/curvedTrapezoid.js'; import { slopedRect } from './shapes/slopedRect.js'; +import { bowTieRect } from './shapes/bowTieRect.js'; const shapes = { state, @@ -63,6 +64,7 @@ const shapes = { halfRoundedRectangle, curvedTrapezoid, slopedRect, + bowTieRect, }; const nodeElems = new Map(); diff --git a/packages/mermaid/src/rendering-util/rendering-elements/shapes/bowTieRect.ts b/packages/mermaid/src/rendering-util/rendering-elements/shapes/bowTieRect.ts new file mode 100644 index 000000000..9e671df93 --- /dev/null +++ b/packages/mermaid/src/rendering-util/rendering-elements/shapes/bowTieRect.ts @@ -0,0 +1,55 @@ +import { labelHelper, updateNodeBounds, getNodeClasses } 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 createBowTieRectPathD(x: number, y: number, totalWidth: number, totalHeight: number) { + return `M ${x},${y + totalHeight} A ${totalHeight} ${totalHeight} 0 0 1 ${x} ${y} H${x + totalWidth} A ${totalHeight} ${totalHeight} 0 0 0 ${x + totalWidth} ${y + totalHeight}Z`; +} + +export const bowTieRect = 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 + 20; + const h = bbox.height + node.padding; + + let shape: d3.Selection; + const { cssStyles } = node; + + if (node.look === 'handdrawn') { + // @ts-ignore - rough is not typed + const rc = rough.svg(shapeSvg); + const pathData = createBowTieRectPathD(0, 0, w, h); + const shapeNode = rc.path(pathData, userNodeOverrides(node, {})); + + shape = shapeSvg.insert(() => shapeNode, ':first-child'); + shape.attr('class', 'basic label-container'); + if (cssStyles) { + shape.attr('style', cssStyles); + } + } else { + const pathData = createBowTieRectPathD(0, 0, w, h); + shape = shapeSvg + .insert('path', ':first-child') + .attr('d', pathData) + .attr('class', 'basic label-container') + .attr('style', cssStyles) + .attr('style', nodeStyles); + } + + shape.attr('transform', `translate(${-w / 2}, ${-h / 2})`); + + updateNodeBounds(node, shape); + + node.intersect = function (point) { + const pos = intersect.rect(node, point); + return pos; + }; + + return shapeSvg; +};