mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-21 00:09:51 +02:00
Add adjustments for text and useHtmlLabels
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
import { select } from 'd3';
|
import { select } from 'd3';
|
||||||
import { getConfig } from '../../config.js';
|
import { getConfig } from '../../config.js';
|
||||||
import { getNodeClasses } from '../../rendering-util/rendering-elements/shapes/util.js';
|
import { getNodeClasses } from '../../rendering-util/rendering-elements/shapes/util.js';
|
||||||
import { calculateTextHeight, calculateTextWidth, decodeEntities } from '../../utils.js';
|
import { calculateTextWidth, decodeEntities } from '../../utils.js';
|
||||||
import type { ClassMember, ClassNode } from './classTypes.js';
|
import type { ClassMember, ClassNode } from './classTypes.js';
|
||||||
import { sanitizeText } from '../../diagram-api/diagramAPI.js';
|
import { sanitizeText } from '../../diagram-api/diagramAPI.js';
|
||||||
import { createText } from '../../rendering-util/createText.js';
|
import { createText } from '../../rendering-util/createText.js';
|
||||||
import { hasKatex } from '../common/common.js';
|
import { evaluate, hasKatex } from '../common/common.js';
|
||||||
import type { Node } from '../../rendering-util/types.js';
|
import type { Node } from '../../rendering-util/types.js';
|
||||||
import type { MermaidConfig } from '../../config.type.js';
|
import type { MermaidConfig } from '../../config.type.js';
|
||||||
|
|
||||||
@@ -14,16 +14,16 @@ export const textHelper = async (
|
|||||||
parent: SVGAElement,
|
parent: SVGAElement,
|
||||||
node: any,
|
node: any,
|
||||||
config: MermaidConfig,
|
config: MermaidConfig,
|
||||||
|
useHtmlLabels: boolean,
|
||||||
GAP = config.class!.padding ?? 12
|
GAP = config.class!.padding ?? 12
|
||||||
) => {
|
) => {
|
||||||
|
const TEXT_PADDING = !useHtmlLabels ? 3 : 0;
|
||||||
const shapeSvg = parent
|
const shapeSvg = parent
|
||||||
// @ts-ignore: Ignore error for using .insert on SVGAElement
|
// @ts-ignore: Ignore error for using .insert on SVGAElement
|
||||||
.insert('g')
|
.insert('g')
|
||||||
.attr('class', getNodeClasses(node))
|
.attr('class', getNodeClasses(node))
|
||||||
.attr('id', node.domId || node.id);
|
.attr('id', node.domId || node.id);
|
||||||
|
|
||||||
const TEXT_PADDING = 6;
|
|
||||||
|
|
||||||
let annotationGroup = null;
|
let annotationGroup = null;
|
||||||
let labelGroup = null;
|
let labelGroup = null;
|
||||||
let membersGroup = null;
|
let membersGroup = null;
|
||||||
@@ -50,16 +50,16 @@ export const textHelper = async (
|
|||||||
membersGroup = shapeSvg.insert('g').attr('class', 'members-group text');
|
membersGroup = shapeSvg.insert('g').attr('class', 'members-group text');
|
||||||
let yOffset = 0;
|
let yOffset = 0;
|
||||||
for (const member of node.members) {
|
for (const member of node.members) {
|
||||||
await addText(membersGroup, member, yOffset, [member.parseClassifier()]);
|
const height = await addText(membersGroup, member, yOffset, [member.parseClassifier()]);
|
||||||
yOffset += calculateTextHeight(member.text, config) + TEXT_PADDING;
|
yOffset += height + TEXT_PADDING;
|
||||||
}
|
}
|
||||||
membersGroupHeight = membersGroup.node().getBBox().height;
|
membersGroupHeight = membersGroup.node().getBBox().height;
|
||||||
|
|
||||||
methodsGroup = shapeSvg.insert('g').attr('class', 'methods-group text');
|
methodsGroup = shapeSvg.insert('g').attr('class', 'methods-group text');
|
||||||
let methodsYOffset = 0;
|
let methodsYOffset = 0;
|
||||||
for (const method of node.methods) {
|
for (const method of node.methods) {
|
||||||
await addText(methodsGroup, method, methodsYOffset, [method.parseClassifier()]);
|
const height = await addText(methodsGroup, method, methodsYOffset, [method.parseClassifier()]);
|
||||||
methodsYOffset += calculateTextHeight(method.text, config) + TEXT_PADDING;
|
methodsYOffset += height + TEXT_PADDING;
|
||||||
}
|
}
|
||||||
|
|
||||||
let bbox = shapeSvg.node().getBBox();
|
let bbox = shapeSvg.node().getBBox();
|
||||||
@@ -99,7 +99,8 @@ const addText = async (
|
|||||||
) => {
|
) => {
|
||||||
const textEl = parentGroup.insert('g').attr('class', 'label').attr('style', styles.join('; '));
|
const textEl = parentGroup.insert('g').attr('class', 'label').attr('style', styles.join('; '));
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
let useHtmlLabels = config.class?.htmlLabels ?? config.htmlLabels ?? true;
|
let useHtmlLabels =
|
||||||
|
'useHtmlLabels' in node ? node.useHtmlLabels : (evaluate(config.htmlLabels) ?? true);
|
||||||
|
|
||||||
let textContent = '';
|
let textContent = '';
|
||||||
// Support regular node type (.label) and classNodes (.text)
|
// Support regular node type (.label) and classNodes (.text)
|
||||||
@@ -133,8 +134,10 @@ const addText = async (
|
|||||||
let numberOfLines = 1;
|
let numberOfLines = 1;
|
||||||
|
|
||||||
if (!useHtmlLabels) {
|
if (!useHtmlLabels) {
|
||||||
// Undo font-weight
|
// Undo font-weight normal
|
||||||
select(text).selectAll('tspan').attr('font-weight', '');
|
if (styles.includes('font-weight: bolder')) {
|
||||||
|
select(text).selectAll('tspan').attr('font-weight', '');
|
||||||
|
}
|
||||||
|
|
||||||
numberOfLines = text.children.length;
|
numberOfLines = text.children.length;
|
||||||
|
|
||||||
@@ -212,4 +215,5 @@ const addText = async (
|
|||||||
|
|
||||||
// Center text and offset by yOffset
|
// Center text and offset by yOffset
|
||||||
textEl.attr('transform', 'translate(0,' + (-bbox.height / (2 * numberOfLines) + yOffset) + ')');
|
textEl.attr('transform', 'translate(0,' + (-bbox.height / (2 * numberOfLines) + yOffset) + ')');
|
||||||
|
return bbox.height;
|
||||||
};
|
};
|
||||||
|
@@ -7,19 +7,20 @@ import rough from 'roughjs';
|
|||||||
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
|
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
|
||||||
import intersect from '../intersect/index.js';
|
import intersect from '../intersect/index.js';
|
||||||
import { textHelper } from '../../../diagrams/class/shapeUtil.js';
|
import { textHelper } from '../../../diagrams/class/shapeUtil.js';
|
||||||
|
import { evaluate } from '../../../diagrams/common/common.js';
|
||||||
|
|
||||||
export const classBox = async (parent: SVGAElement, node: Node): Promise<SVGAElement> => {
|
export const classBox = async (parent: SVGAElement, node: Node): Promise<SVGAElement> => {
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
const PADDING = config.class!.padding ?? 12;
|
const PADDING = config.class!.padding ?? 12;
|
||||||
const GAP = PADDING;
|
const GAP = PADDING;
|
||||||
const useHtmlLabels = config.class?.htmlLabels ?? config.htmlLabels ?? true;
|
const useHtmlLabels = node.useHtmlLabels ?? evaluate(config.htmlLabels) ?? true;
|
||||||
// Treat node as classNode
|
// Treat node as classNode
|
||||||
const classNode = node as unknown as ClassNode;
|
const classNode = node as unknown as ClassNode;
|
||||||
classNode.annotations = classNode.annotations ?? [];
|
classNode.annotations = classNode.annotations ?? [];
|
||||||
classNode.members = classNode.members ?? [];
|
classNode.members = classNode.members ?? [];
|
||||||
classNode.methods = classNode.methods ?? [];
|
classNode.methods = classNode.methods ?? [];
|
||||||
|
|
||||||
const { shapeSvg, bbox } = await textHelper(parent, node, config, GAP);
|
const { shapeSvg, bbox } = await textHelper(parent, node, config, useHtmlLabels, GAP);
|
||||||
|
|
||||||
const { labelStyles, nodeStyles } = styles2String(node);
|
const { labelStyles, nodeStyles } = styles2String(node);
|
||||||
node.labelStyle = labelStyles;
|
node.labelStyle = labelStyles;
|
||||||
|
Reference in New Issue
Block a user