fix: Remove repeated config calls

This commit is contained in:
Sidharth Vinod
2024-03-23 14:58:55 +05:30
parent f907ac30c6
commit be37f2c576
6 changed files with 41 additions and 29 deletions

View File

@@ -124,7 +124,7 @@ export const setConfig = (conf: MermaidConfig): MermaidConfig => {
* | --------- | ------------------------- | ----------- | ------------------------------ | * | --------- | ------------------------- | ----------- | ------------------------------ |
* | getConfig | Obtains the currentConfig | Get Request | Any Values from current Config | * | getConfig | Obtains the currentConfig | Get Request | Any Values from current Config |
* *
* **Notes**: Returns **any** the currentConfig * **Notes**: Avoid calling this function repeatedly. Instead, store the result in a variable and use it, and pass it down to function calls.
* *
* @returns The currentConfig * @returns The currentConfig
*/ */

View File

@@ -30,7 +30,7 @@ const rect = (parent, node) => {
// .appendChild(createLabel(node.labelText, node.labelStyle, undefined, true)); // .appendChild(createLabel(node.labelText, node.labelStyle, undefined, true));
const text = const text =
node.labelType === 'markdown' node.labelType === 'markdown'
? createText(label, node.labelText, { style: node.labelStyle, useHtmlLabels }) ? createText(label, node.labelText, { style: node.labelStyle, useHtmlLabels }, siteConfig)
: label.node().appendChild(createLabel(node.labelText, node.labelStyle, undefined, true)); : label.node().appendChild(createLabel(node.labelText, node.labelStyle, undefined, true));
// Get the size of the label // Get the size of the label

View File

@@ -18,15 +18,21 @@ export const clear = () => {
}; };
export const insertEdgeLabel = (elem, edge) => { export const insertEdgeLabel = (elem, edge) => {
const useHtmlLabels = evaluate(getConfig().flowchart.htmlLabels); const config = getConfig();
const useHtmlLabels = evaluate(config.flowchart.htmlLabels);
// Create the actual text element // Create the actual text element
const labelElement = const labelElement =
edge.labelType === 'markdown' edge.labelType === 'markdown'
? createText(elem, edge.label, { ? createText(
elem,
edge.label,
{
style: edge.labelStyle, style: edge.labelStyle,
useHtmlLabels, useHtmlLabels,
addSvgBackground: true, addSvgBackground: true,
}) },
config
)
: createLabel(edge.label, edge.labelStyle); : createLabel(edge.label, edge.labelStyle);
// Create outer g, edgeLabel, this will be positioned after graph layout // Create outer g, edgeLabel, this will be positioned after graph layout

View File

@@ -6,8 +6,9 @@ import { evaluate, sanitizeText } from '../../diagrams/common/common.js';
import { decodeEntities } from '../../utils.js'; import { decodeEntities } from '../../utils.js';
export const labelHelper = async (parent, node, _classes, isNode) => { export const labelHelper = async (parent, node, _classes, isNode) => {
const config = getConfig();
let classes; let classes;
const useHtmlLabels = node.useHtmlLabels || evaluate(getConfig().flowchart.htmlLabels); const useHtmlLabels = node.useHtmlLabels || evaluate(config.flowchart.htmlLabels);
if (!_classes) { if (!_classes) {
classes = 'node default'; classes = 'node default';
} else { } else {
@@ -35,26 +36,26 @@ export const labelHelper = async (parent, node, _classes, isNode) => {
let text; let text;
if (node.labelType === 'markdown') { if (node.labelType === 'markdown') {
// text = textNode; // text = textNode;
text = createText(label, sanitizeText(decodeEntities(labelText), getConfig()), { text = createText(
label,
sanitizeText(decodeEntities(labelText), config),
{
useHtmlLabels, useHtmlLabels,
width: node.width || getConfig().flowchart.wrappingWidth, width: node.width || config.flowchart.wrappingWidth,
classes: 'markdown-node-label', classes: 'markdown-node-label',
}); },
config
);
} else { } else {
text = textNode.appendChild( text = textNode.appendChild(
createLabel( createLabel(sanitizeText(decodeEntities(labelText), config), node.labelStyle, false, isNode)
sanitizeText(decodeEntities(labelText), getConfig()),
node.labelStyle,
false,
isNode
)
); );
} }
// 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 / 2;
if (evaluate(getConfig().flowchart.htmlLabels)) { if (evaluate(config.flowchart.htmlLabels)) {
const div = text.children[0]; const div = text.children[0];
const dv = select(text); const dv = select(text);
@@ -76,8 +77,8 @@ export const labelHelper = async (parent, node, _classes, isNode) => {
if (noImgText) { if (noImgText) {
// default size if no text // default size if no text
const bodyFontSize = getConfig().fontSize const bodyFontSize = config.fontSize
? getConfig().fontSize ? config.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 width = parseInt(bodyFontSize, 10) * enlargingFactor + 'px';

View File

@@ -196,11 +196,16 @@ export const drawNode = function (
// Create the wrapped text element // Create the wrapped text element
const textElem = nodeElem.append('g'); const textElem = nodeElem.append('g');
const description = node.descr.replace(/(<br\/*>)/g, '\n'); const description = node.descr.replace(/(<br\/*>)/g, '\n');
const newEl = createText(textElem, description, { const newEl = createText(
textElem,
description,
{
useHtmlLabels: htmlLabels, useHtmlLabels: htmlLabels,
width: node.width, width: node.width,
classes: 'mindmap-node-label', classes: 'mindmap-node-label',
}); },
conf
);
if (!htmlLabels) { if (!htmlLabels) {
textElem textElem

View File

@@ -183,7 +183,7 @@ export const createText = (
width = 200, width = 200,
addSvgBackground = false, addSvgBackground = false,
} = {}, } = {},
config: MermaidConfig = {} config: MermaidConfig
) => { ) => {
log.info('createText', text, style, isTitle, classes, useHtmlLabels, isNode, addSvgBackground); log.info('createText', text, style, isTitle, classes, useHtmlLabels, isNode, addSvgBackground);
if (useHtmlLabels) { if (useHtmlLabels) {