mermaid/packages/mermaid/src/dagre-wrapper/shapes/util.js

148 lines
4.3 KiB
JavaScript
Raw Normal View History

import createLabel from '../createLabel.js';
import { createText } from '../../rendering-util/createText.js';
import { getConfig } from '../../diagram-api/diagramAPI.js';
2020-05-27 19:38:30 +02:00
import { select } from 'd3';
import { evaluate, sanitizeText } from '../../diagrams/common/common.js';
2023-11-03 08:55:26 +01:00
import { decodeEntities } from '../../utils.js';
2023-03-30 22:13:31 +02:00
export const labelHelper = async (parent, node, _classes, isNode) => {
let classes;
const useHtmlLabels = node.useHtmlLabels || evaluate(getConfig().flowchart.htmlLabels);
if (!_classes) {
classes = 'node default';
} else {
classes = _classes;
}
// Add outer g element
const shapeSvg = parent
.insert('g')
.attr('class', classes)
.attr('id', node.domId || node.id);
// Create the label and insert it after the rect
2021-07-15 11:35:12 +02:00
const label = shapeSvg.insert('g').attr('class', 'label').attr('style', node.labelStyle);
2022-10-02 20:32:50 +02:00
// Replace labelText with default value if undefined
let labelText;
2022-11-22 20:35:08 +01:00
if (node.labelText === undefined) {
2022-10-02 20:32:50 +02:00
labelText = '';
} else {
labelText = typeof node.labelText === 'string' ? node.labelText : node.labelText[0];
}
2021-11-30 20:28:51 +01:00
const textNode = label.node();
let text;
if (node.labelType === 'markdown') {
// text = textNode;
text = createText(label, sanitizeText(decodeEntities(labelText), getConfig()), {
useHtmlLabels,
width: node.width || getConfig().flowchart.wrappingWidth,
classes: 'markdown-node-label',
});
} else {
text = textNode.appendChild(
2021-10-21 19:37:48 +02:00
createLabel(
2021-11-30 20:28:51 +01:00
sanitizeText(decodeEntities(labelText), getConfig()),
2021-10-21 19:37:48 +02:00
node.labelStyle,
false,
isNode
)
);
}
// Get the size of the label
2020-05-27 19:38:30 +02:00
let bbox = text.getBBox();
2023-03-30 22:13:31 +02:00
const halfPadding = node.padding / 2;
2020-05-27 19:38:30 +02:00
2021-06-03 20:47:24 +02:00
if (evaluate(getConfig().flowchart.htmlLabels)) {
2020-05-27 19:38:30 +02:00
const div = text.children[0];
const dv = select(text);
2023-03-30 22:13:31 +02:00
// if there are images, need to wait for them to load before getting the bounding box
const images = div.getElementsByTagName('img');
if (images) {
const noImgText = labelText.replace(/<img[^>]*>/g, '').trim() === '';
await Promise.all(
[...images].map(
(img) =>
2023-06-16 16:32:57 +02:00
new Promise((res) => {
/**
*
*/
function setupImage() {
2023-03-30 22:13:31 +02:00
img.style.display = 'flex';
img.style.flexDirection = 'column';
if (noImgText) {
// default size if no text
const bodyFontSize = getConfig().fontSize
? getConfig().fontSize
: window.getComputedStyle(document.body).fontSize;
const enlargingFactor = 5;
2023-11-22 14:40:05 +01:00
const width = parseInt(bodyFontSize, 10) * enlargingFactor + 'px';
img.style.minWidth = width;
img.style.maxWidth = width;
2023-03-30 22:13:31 +02:00
} else {
img.style.width = '100%';
}
res(img);
2023-06-16 16:32:57 +02:00
}
2023-06-16 18:01:52 +02:00
setTimeout(() => {
if (img.complete) {
setupImage();
}
});
2023-06-16 16:32:57 +02:00
img.addEventListener('error', setupImage);
img.addEventListener('load', setupImage);
})
2023-03-30 22:13:31 +02:00
)
);
}
2020-05-27 19:38:30 +02:00
bbox = div.getBoundingClientRect();
dv.attr('width', bbox.width);
dv.attr('height', bbox.height);
}
// Center the label
if (useHtmlLabels) {
label.attr('transform', 'translate(' + -bbox.width / 2 + ', ' + -bbox.height / 2 + ')');
} else {
label.attr('transform', 'translate(' + 0 + ', ' + -bbox.height / 2 + ')');
}
if (node.centerLabel) {
label.attr('transform', 'translate(' + -bbox.width / 2 + ', ' + -bbox.height / 2 + ')');
}
label.insert('rect', ':first-child');
return { shapeSvg, bbox, halfPadding, label };
};
export const updateNodeBounds = (node, element) => {
const bbox = element.node().getBBox();
node.width = bbox.width;
node.height = bbox.height;
};
/**
* @param parent
* @param w
* @param h
* @param points
*/
export function insertPolygonShape(parent, w, h, points) {
return parent
.insert('polygon', ':first-child')
.attr(
'points',
points
2021-07-15 11:35:12 +02:00
.map(function (d) {
return d.x + ',' + d.y;
})
.join(' ')
)
.attr('class', 'label-container')
.attr('transform', 'translate(' + -w / 2 + ',' + h / 2 + ')');
}