From 8277579259cd327c40a14aae628538a6a793c8ab Mon Sep 17 00:00:00 2001 From: Knut Sveidqvist Date: Tue, 3 Dec 2024 10:49:43 +0100 Subject: [PATCH] #6097 Handling hourglass and lightningBolt --- .../rendering-elements/shapes/hourglass.ts | 12 ++++++--- .../shapes/lightningBolt.ts | 25 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/mermaid/src/rendering-util/rendering-elements/shapes/hourglass.ts b/packages/mermaid/src/rendering-util/rendering-elements/shapes/hourglass.ts index 579a32df0..3c0b82a31 100644 --- a/packages/mermaid/src/rendering-util/rendering-elements/shapes/hourglass.ts +++ b/packages/mermaid/src/rendering-util/rendering-elements/shapes/hourglass.ts @@ -54,9 +54,15 @@ export async function hourglass(parent: D3Selectio // label.attr('transform', `translate(${-bbox.width / 2}, ${(h/2)})`); // To transform text below hourglass shape node.calcIntersect = function (bounds: Bounds, point: Point) { - // TODO: Implement intersect for this shape - const radius = bounds.width / 2; - return intersect.circle(bounds, radius, point); + const { width: w, height: h } = bounds; + const points = [ + { x: 0, y: 0 }, + { x: w, y: 0 }, + { x: 0, y: h }, + { x: w, y: h }, + ]; + const res = intersect.polygon(bounds, points, point); + return { x: res.x - 0.5, y: res.y - 0.5 }; }; node.intersect = function (point) { diff --git a/packages/mermaid/src/rendering-util/rendering-elements/shapes/lightningBolt.ts b/packages/mermaid/src/rendering-util/rendering-elements/shapes/lightningBolt.ts index ea9d73504..b034421e2 100644 --- a/packages/mermaid/src/rendering-util/rendering-elements/shapes/lightningBolt.ts +++ b/packages/mermaid/src/rendering-util/rendering-elements/shapes/lightningBolt.ts @@ -7,7 +7,16 @@ import intersect from '../intersect/index.js'; import { createPathFromPoints } from './util.js'; import type { D3Selection } from '../../../types.js'; import type { Bounds, Point } from '../../../types.js'; - +function getPoints(width: number, height: number, gapX: number, gapY: number) { + return [ + { x: width, y: 0 }, + { x: 0, y: height / 2 + gapY / 2 }, + { x: width - 4 * gapX, y: height / 2 + gapY / 2 }, + { x: 0, y: height }, + { x: width, y: height / 2 - gapY / 2 }, + { x: 4 * gapX, y: height / 2 - gapY / 2 }, + ]; +} export function lightningBolt(parent: D3Selection, node: Node) { const { labelStyles, nodeStyles } = styles2String(node); node.label = ''; @@ -57,16 +66,20 @@ export function lightningBolt(parent: D3Selection< updateNodeBounds(node, lightningBolt); node.calcIntersect = function (bounds: Bounds, point: Point) { - // TODO: Implement intersect for this shape - const radius = bounds.width / 2; - return intersect.circle(bounds, radius, point); + const { width: w, height: h } = bounds; + const gapX = Math.max(5, w * 0.1); + const gapY = Math.max(5, h * 0.1); + const p = getPoints(w, h, gapX, gapY); + const res = intersect.polygon(bounds, p, point); + + return { x: res.x - 0.5, y: res.y - 0.5 }; }; node.intersect = function (point) { log.info('lightningBolt intersect', node, point); - const pos = intersect.polygon(node, points, point); + const res = intersect.polygon(node, points, point); - return pos; + return res; }; return shapeSvg;