From 04b15e7ece375182e5686364479ddc804d5564f2 Mon Sep 17 00:00:00 2001 From: saurabhg772244 Date: Wed, 7 Aug 2024 20:06:18 +0530 Subject: [PATCH] added crossed circle --- cypress/platform/saurabh.html | 123 ++++++++++++++++++ .../rendering-elements/nodes.js | 2 + .../shapes/crossedCircle.ts | 70 ++++++++++ 3 files changed, 195 insertions(+) create mode 100644 cypress/platform/saurabh.html create mode 100644 packages/mermaid/src/rendering-util/rendering-elements/shapes/crossedCircle.ts diff --git a/cypress/platform/saurabh.html b/cypress/platform/saurabh.html new file mode 100644 index 0000000000..b788ffe887 --- /dev/null +++ b/cypress/platform/saurabh.html @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + +
+flowchart
+     
+      A@{ shape: stateStart }@
+      B@{ shape: crossedCircle, label: "This is crossed circle"  }@
+      C
+      D
+      A ---> B
+      C --> B
+      B --> D
+     
+      
+  
+ + + + diff --git a/packages/mermaid/src/rendering-util/rendering-elements/nodes.js b/packages/mermaid/src/rendering-util/rendering-elements/nodes.js index 5f7e3964ab..60f1ee7580 100644 --- a/packages/mermaid/src/rendering-util/rendering-elements/nodes.js +++ b/packages/mermaid/src/rendering-util/rendering-elements/nodes.js @@ -32,6 +32,7 @@ import { curvedTrapezoid } from './shapes/curvedTrapezoid.js'; import { slopedRect } from './shapes/slopedRect.js'; import { bowTieRect } from './shapes/bowTieRect.js'; import { dividedRect } from './shapes/dividedRect.js'; +import { crossedCircle } from './shapes/crossedCircle.js'; const shapes = { state, @@ -67,6 +68,7 @@ const shapes = { slopedRect, bowTieRect, dividedRect, + crossedCircle, }; const nodeElems = new Map(); diff --git a/packages/mermaid/src/rendering-util/rendering-elements/shapes/crossedCircle.ts b/packages/mermaid/src/rendering-util/rendering-elements/shapes/crossedCircle.ts new file mode 100644 index 0000000000..e97354cf66 --- /dev/null +++ b/packages/mermaid/src/rendering-util/rendering-elements/shapes/crossedCircle.ts @@ -0,0 +1,70 @@ +import { log } from '$root/logger.js'; +import { labelHelper, getNodeClasses, updateNodeBounds } from './util.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'; +import intersect from '../intersect/index.js'; + +function createLine(cx: number, cy: number, r: number) { + const xAxis45 = Math.cos(Math.PI / 4); // cosine of 45 degrees + const yAxis45 = Math.sin(Math.PI / 4); // sine of 45 degrees + const lineLength = r * 2; + + const pointQ1 = { x: cx + (lineLength / 2) * xAxis45, y: cy + (lineLength / 2) * yAxis45 }; // Quadrant I + const pointQ2 = { x: cx - (lineLength / 2) * xAxis45, y: cy + (lineLength / 2) * yAxis45 }; // Quadrant II + const pointQ3 = { x: cx - (lineLength / 2) * xAxis45, y: cy - (lineLength / 2) * yAxis45 }; // Quadrant III + const pointQ4 = { x: cx + (lineLength / 2) * xAxis45, y: cy - (lineLength / 2) * yAxis45 }; // Quadrant IV + + return `M ${pointQ2.x},${pointQ2.y} L ${pointQ4.x},${pointQ4.y} + M ${pointQ1.x},${pointQ1.y} L ${pointQ3.x},${pointQ3.y}`; +} + +export const crossedCircle = async (parent: SVGAElement, node: Node) => { + const { labelStyles, nodeStyles } = styles2String(node); + node.labelStyle = labelStyles; + const { shapeSvg, bbox, halfPadding } = await labelHelper(parent, node, getNodeClasses(node)); + const cy = 0; + const cx = 0; + const radius = Math.max(bbox.width, bbox.height) / 2 + halfPadding; + 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 circleNode = rc.circle(cx, cy, radius * 2, options); + const linePath = createLine(cx, -cy, radius); + const lineNode = rc.path(linePath, options); + + const crossedCircle = shapeSvg.insert('g', ':first-child'); + crossedCircle.insert(() => circleNode, ':first-child'); + crossedCircle.insert(() => lineNode); + + crossedCircle.attr('class', 'basic label-container'); + + if (cssStyles) { + crossedCircle.attr('style', cssStyles); + } + + if (nodeStyles) { + crossedCircle.attr('style', nodeStyles); + } + + updateNodeBounds(node, crossedCircle); + + node.intersect = function (point) { + log.info('crossedCircle intersect', node, { radius, point }); + const pos = intersect.circle(node, radius, point); + return pos; + }; + + return shapeSvg; +};