-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new-shapes and picking up node data from node data statement
- Loading branch information
Showing
10 changed files
with
417 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/mermaid/src/rendering-util/rendering-elements/shapes/anchor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { log } from '$root/logger.js'; | ||
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'; | ||
|
||
export const anchor = async (parent: SVGAElement, node: Node): Promise<SVGAElement> => { | ||
const { labelStyles, nodeStyles } = styles2String(node); | ||
node.labelStyle = labelStyles; | ||
// const { shapeSvg, bbox, halfPadding } = await labelHelper(parent, node, getNodeClasses(node)); | ||
const classes = getNodeClasses(node); | ||
let cssClasses = classes; | ||
if (!classes) { | ||
cssClasses = 'anchor'; | ||
} | ||
const shapeSvg = parent | ||
.insert('g') | ||
.attr('class', cssClasses) | ||
.attr('id', node.domId || node.id); | ||
|
||
const radius = 1; | ||
let circleElem; | ||
const { cssStyles } = node; | ||
|
||
// if (node.look === 'handdrawn') { | ||
// @ts-ignore - rough is not typed | ||
const rc = rough.svg(shapeSvg); | ||
const options = userNodeOverrides(node, { fill: 'black', stroke: 'none', fillStyle: 'solid' }); | ||
|
||
if (node.look !== 'handdrawn') { | ||
options.roughness = 0; | ||
} | ||
const roughNode = rc.circle(0, 0, radius * 2, options); | ||
|
||
console.log('IPI roughNode:', options); | ||
|
||
circleElem = shapeSvg.insert(() => roughNode, ':first-child'); | ||
circleElem.attr('class', 'anchor').attr('style', cssStyles); | ||
// } else { | ||
// circleElem = shapeSvg | ||
// .insert('circle', ':first-child') | ||
// .attr('class', 'basic label-container') | ||
// .attr('style', nodeStyles) | ||
// .attr('r', radius) | ||
// .attr('cx', 0) | ||
// .attr('cy', 0); | ||
// } | ||
|
||
updateNodeBounds(node, circleElem); | ||
|
||
node.intersect = function (point) { | ||
log.info('Circle intersect', node, radius, point); | ||
return intersect.circle(node, radius, point); | ||
}; | ||
|
||
return shapeSvg; | ||
}; |
76 changes: 76 additions & 0 deletions
76
packages/mermaid/src/rendering-util/rendering-elements/shapes/card.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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'; | ||
|
||
import { insertPolygonShape } from './insertPolygonShape.js'; | ||
import { createPathFromPoints } from './util.js'; | ||
|
||
// const createPathFromPoints = (points: { x: number; y: number }[]): string => { | ||
// const pointStrings = points.map((p, i) => `${i === 0 ? 'M' : 'L'}${p.x},${p.y}`); | ||
// pointStrings.push('Z'); | ||
// return pointStrings.join(' '); | ||
// }; | ||
|
||
export async function card(parent: SVGAElement, node: Node): Promise<SVGAElement> { | ||
const { labelStyles, nodeStyles } = styles2String(node); | ||
node.labelStyle = labelStyles; | ||
const { shapeSvg, bbox } = await labelHelper(parent, node, getNodeClasses(node)); | ||
|
||
const f = 4; | ||
const h = bbox.height + node.padding; | ||
// const m = h / f; | ||
const m = 10; | ||
const padding = 12; | ||
const w = bbox.width + node.padding + padding; | ||
const left = 0; | ||
const right = w; | ||
const top = -h; | ||
const bottom = 0; | ||
// const w = bbox.width + 2 * m + node.padding; | ||
const points = [ | ||
{ x: left + padding, y: top }, | ||
{ x: right, y: top }, | ||
{ x: right, y: bottom }, | ||
{ x: left, y: bottom }, | ||
{ x: left, y: top + padding }, | ||
{ x: left + padding, y: top }, | ||
]; | ||
|
||
let polygon: d3.Selection<SVGPolygonElement | SVGGElement, unknown, null, undefined>; | ||
const { cssStyles } = node; | ||
|
||
if (node.look === 'handdrawn') { | ||
// @ts-ignore - rough is not typed | ||
const rc = rough.svg(shapeSvg); | ||
const options = userNodeOverrides(node, {}); | ||
const pathData = createPathFromPoints(points); | ||
const roughNode = rc.path(pathData, options); | ||
|
||
polygon = shapeSvg | ||
.insert(() => roughNode, ':first-child') | ||
.attr('transform', `translate(${-w / 2}, ${h / 2})`); | ||
|
||
if (cssStyles) { | ||
polygon.attr('style', cssStyles); | ||
} | ||
} else { | ||
polygon = insertPolygonShape(shapeSvg, w, h, points); | ||
} | ||
|
||
if (nodeStyles) { | ||
polygon.attr('style', nodeStyles); | ||
} | ||
|
||
updateNodeBounds(node, polygon); | ||
|
||
node.intersect = function (point) { | ||
return intersect.polygon(node, points, point); | ||
}; | ||
|
||
return shapeSvg; | ||
} |
121 changes: 121 additions & 0 deletions
121
packages/mermaid/src/rendering-util/rendering-elements/shapes/document.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
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'; | ||
|
||
export const createCylinderPathD = ( | ||
x: number, | ||
y: number, | ||
width: number, | ||
height: number, | ||
rx: number, | ||
ry: number | ||
): string => { | ||
return [ | ||
`M${x},${y + ry}`, | ||
`a${rx},${ry} 0,0,0 ${width},0`, | ||
`a${rx},${ry} 0,0,0 ${-width},0`, | ||
`l0,${height}`, | ||
`a${rx},${ry} 0,0,0 ${width},0`, | ||
`l0,${-height}`, | ||
].join(' '); | ||
}; | ||
export const createOuterCylinderPathD = ( | ||
x: number, | ||
y: number, | ||
width: number, | ||
height: number, | ||
rx: number, | ||
ry: number | ||
): string => { | ||
return [ | ||
`M${x},${y + ry}`, | ||
`M${x + width},${y + ry}`, | ||
`a${rx},${ry} 0,0,0 ${-width},0`, | ||
`l0,${height}`, | ||
`a${rx},${ry} 0,0,0 ${width},0`, | ||
`l0,${-height}`, | ||
].join(' '); | ||
}; | ||
export const createInnerCylinderPathD = ( | ||
x: number, | ||
y: number, | ||
width: number, | ||
height: number, | ||
rx: number, | ||
ry: number | ||
): string => { | ||
return [`M${x - width / 2},${-height / 2}`, `a${rx},${ry} 0,0,0 ${width},0`].join(' '); | ||
}; | ||
export const cylinder = 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 rx = w / 2; | ||
const ry = rx / (2.5 + w / 50); | ||
const h = bbox.height + ry + node.padding; | ||
|
||
let cylinder: d3.Selection<SVGPathElement | SVGGElement, unknown, null, undefined>; | ||
const { cssStyles } = node; | ||
|
||
if (node.look === 'handdrawn') { | ||
// @ts-ignore - rough is not typed | ||
const rc = rough.svg(shapeSvg); | ||
const outerPathData = createOuterCylinderPathD(0, 0, w, h, rx, ry); | ||
const innerPathData = createInnerCylinderPathD(0, ry, w, h, rx, ry); | ||
const outerNode = rc.path(outerPathData, userNodeOverrides(node, {})); | ||
const innerLine = rc.path(innerPathData, userNodeOverrides(node, { fill: 'none' })); | ||
|
||
cylinder = shapeSvg.insert(() => innerLine, ':first-child'); | ||
cylinder = shapeSvg.insert(() => outerNode, ':first-child'); | ||
cylinder.attr('class', 'basic label-container'); | ||
if (cssStyles) { | ||
cylinder.attr('style', cssStyles); | ||
} | ||
} else { | ||
const pathData = createCylinderPathD(0, 0, w, h, rx, ry); | ||
cylinder = shapeSvg | ||
.insert('path', ':first-child') | ||
.attr('d', pathData) | ||
.attr('class', 'basic label-container') | ||
.attr('style', cssStyles) | ||
.attr('style', nodeStyles); | ||
} | ||
|
||
cylinder.attr('label-offset-y', ry); | ||
cylinder.attr('transform', `translate(${-w / 2}, ${-(h / 2 + ry)})`); | ||
|
||
updateNodeBounds(node, cylinder); | ||
|
||
node.intersect = function (point) { | ||
const pos = intersect.rect(node, point); | ||
const x = pos.x - (node.x ?? 0); | ||
|
||
if ( | ||
rx != 0 && | ||
(Math.abs(x) < (node.width ?? 0) / 2 || | ||
(Math.abs(x) == (node.width ?? 0) / 2 && | ||
Math.abs(pos.y - (node.y ?? 0)) > (node.height ?? 0) / 2 - ry)) | ||
) { | ||
let y = ry * ry * (1 - (x * x) / (rx * rx)); | ||
if (y != 0) { | ||
y = Math.sqrt(y); | ||
} | ||
y = ry - y; | ||
if (point.y - (node.y ?? 0) > 0) { | ||
y = -y; | ||
} | ||
|
||
pos.y += y; | ||
} | ||
|
||
return pos; | ||
}; | ||
|
||
return shapeSvg; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.