refactor: convert shapes/util.js to TypeScript

Convert the
`packages/mermaid/src/rendering-util/rendering-elements/shapes/util.js`
file to TypeScript at:
`packages/mermaid/src/rendering-util/rendering-elements/shapes/util.ts`.
This commit is contained in:
Alois Klink
2024-10-18 00:12:56 +09:00
parent b0f4ace3a1
commit 62fd359c0e
2 changed files with 64 additions and 27 deletions

View File

@@ -204,7 +204,7 @@ export const createText = async (
width = 200, width = 200,
addSvgBackground = false, addSvgBackground = false,
} = {}, } = {},
config: MermaidConfig config?: MermaidConfig
) => { ) => {
log.debug( log.debug(
'XYZ createText', 'XYZ createText',

View File

@@ -1,12 +1,18 @@
import { createText } from '../../createText.js'; import { createText } from '../../createText.js';
import type { Node } from '../../types.js';
import { getConfig } from '../../../diagram-api/diagramAPI.js'; import { getConfig } from '../../../diagram-api/diagramAPI.js';
import { select } from 'd3'; import { select } from 'd3';
import { evaluate, sanitizeText } from '../../../diagrams/common/common.js'; import { evaluate, sanitizeText } from '../../../diagrams/common/common.js';
import { decodeEntities } from '../../../utils.js'; import { decodeEntities, handleUndefinedAttr } from '../../../utils.js';
import type { D3Selection, Point } from '../../../types.js';
export const labelHelper = async (parent, node, _classes) => { export const labelHelper = async <T extends SVGGraphicsElement>(
parent: D3Selection<T>,
node: Node,
_classes?: string
) => {
let cssClasses; let cssClasses;
const useHtmlLabels = node.useHtmlLabels || evaluate(getConfig().flowchart.htmlLabels); const useHtmlLabels = node.useHtmlLabels || evaluate(getConfig()?.flowchart?.htmlLabels);
if (!_classes) { if (!_classes) {
cssClasses = 'node default'; cssClasses = 'node default';
} else { } else {
@@ -20,7 +26,10 @@ export const labelHelper = async (parent, node, _classes) => {
.attr('id', node.domId || node.id); .attr('id', node.domId || node.id);
// Create the label and insert it after the rect // Create the label and insert it after the rect
const labelEl = shapeSvg.insert('g').attr('class', 'label').attr('style', node.labelStyle); const labelEl = shapeSvg
.insert('g')
.attr('class', 'label')
.attr('style', handleUndefinedAttr(node.labelStyle));
// Replace label with default value if undefined // Replace label with default value if undefined
let label; let label;
@@ -30,19 +39,19 @@ export const labelHelper = async (parent, node, _classes) => {
label = typeof node.label === 'string' ? node.label : node.label[0]; label = typeof node.label === 'string' ? node.label : node.label[0];
} }
let text; const text = await createText(labelEl, sanitizeText(decodeEntities(label), getConfig()), {
text = await createText(labelEl, sanitizeText(decodeEntities(label), getConfig()), {
useHtmlLabels, useHtmlLabels,
width: node.width || getConfig().flowchart.wrappingWidth, width: node.width || getConfig().flowchart?.wrappingWidth,
// @ts-expect-error -- This is currently not used. Should this be `classes` instead?
cssClasses: 'markdown-node-label', cssClasses: 'markdown-node-label',
style: node.labelStyle, style: node.labelStyle,
addSvgBackground: !!node.icon || !!node.img, addSvgBackground: !!node.icon || !!node.img,
}); });
// Get the size of the label // Get the size of the label
let bbox = text.getBBox(); let bbox = text.getBBox();
const halfPadding = node.padding / 2; const halfPadding = (node?.padding ?? 0) / 2;
if (evaluate(getConfig().flowchart.htmlLabels)) { if (evaluate(getConfig().flowchart?.htmlLabels)) {
const div = text.children[0]; const div = text.children[0];
const dv = select(text); const dv = select(text);
@@ -68,7 +77,11 @@ export const labelHelper = async (parent, node, _classes) => {
? getConfig().fontSize ? getConfig().fontSize
: window.getComputedStyle(document.body).fontSize; : window.getComputedStyle(document.body).fontSize;
const enlargingFactor = 5; const enlargingFactor = 5;
const width = parseInt(bodyFontSize, 10) * enlargingFactor + 'px'; const parsedBodyFontSize =
typeof bodyFontSize === 'number'
? bodyFontSize
: parseInt(bodyFontSize ?? '', 10);
const width = parsedBodyFontSize * enlargingFactor + 'px';
img.style.minWidth = width; img.style.minWidth = width;
img.style.maxWidth = width; img.style.maxWidth = width;
} else { } else {
@@ -106,19 +119,28 @@ export const labelHelper = async (parent, node, _classes) => {
return { shapeSvg, bbox, halfPadding, label: labelEl }; return { shapeSvg, bbox, halfPadding, label: labelEl };
}; };
export const updateNodeBounds = (node, element) => { export const updateNodeBounds = <T extends SVGGraphicsElement>(
const bbox = element.node().getBBox(); node: Node,
// D3Selection<SVGGElement> is for the roughjs case, D3Selection<T> is for the non-roughjs case
element: D3Selection<SVGGElement> | D3Selection<T>
) => {
const bbox = element.node()!.getBBox();
node.width = bbox.width; node.width = bbox.width;
node.height = bbox.height; node.height = bbox.height;
}; };
/** /**
* @param parent * @param parent - Parent element to append the polygon to
* @param w * @param w - Width of the polygon
* @param h * @param h - Height of the polygon
* @param points * @param points - Array of points to create the polygon
*/ */
export function insertPolygonShape(parent, w, h, points) { export function insertPolygonShape(
parent: D3Selection<SVGGElement>,
w: number,
h: number,
points: Point[]
) {
return parent return parent
.insert('polygon', ':first-child') .insert('polygon', ':first-child')
.attr( .attr(
@@ -133,16 +155,23 @@ export function insertPolygonShape(parent, w, h, points) {
.attr('transform', 'translate(' + -w / 2 + ',' + h / 2 + ')'); .attr('transform', 'translate(' + -w / 2 + ',' + h / 2 + ')');
} }
export const getNodeClasses = (node, extra) => export const getNodeClasses = (node: Node, extra?: string) =>
(node.look === 'handDrawn' ? 'rough-node' : 'node') + ' ' + node.cssClasses + ' ' + (extra || ''); (node.look === 'handDrawn' ? 'rough-node' : 'node') + ' ' + node.cssClasses + ' ' + (extra || '');
export function createPathFromPoints(points) { export function createPathFromPoints(points: Point[]) {
const pointStrings = points.map((p, i) => `${i === 0 ? 'M' : 'L'}${p.x},${p.y}`); const pointStrings = points.map((p, i) => `${i === 0 ? 'M' : 'L'}${p.x},${p.y}`);
pointStrings.push('Z'); pointStrings.push('Z');
return pointStrings.join(' '); return pointStrings.join(' ');
} }
export function generateFullSineWavePoints(x1, y1, x2, y2, amplitude, numCycles) { export function generateFullSineWavePoints(
x1: number,
y1: number,
x2: number,
y2: number,
amplitude: number,
numCycles: number
) {
const points = []; const points = [];
const steps = 50; // Number of segments to create a smooth curve const steps = 50; // Number of segments to create a smooth curve
const deltaX = x2 - x1; const deltaX = x2 - x1;
@@ -164,13 +193,21 @@ export function generateFullSineWavePoints(x1, y1, x2, y2, amplitude, numCycles)
return points; return points;
} }
/**
* @param centerX - x-coordinate of center of circle
* @param centerY - y-coordinate of center of circle
* @param radius - radius of circle
* @param numPoints - total points required
* @param startAngle - angle where arc will start
* @param endAngle - angle where arc will end
*/
export function generateCirclePoints( export function generateCirclePoints(
centerX, // x-coordinate of center of circle centerX: number,
centerY, // x-coordinate of center of circle centerY: number,
radius, // radius of circle radius: number,
numPoints, // total points required numPoints: number,
startAngle, // angle where arc will start startAngle: number,
endAngle // angle where arc will end endAngle: number
) { ) {
const points = []; const points = [];