mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-10 02:49:40 +02:00
chore: Address review comments
Co-authored-by: Reda Al Sulais <u.yokozuna@gmail.com>
This commit is contained in:
@@ -31,7 +31,7 @@ const getMindmap = () => {
|
|||||||
const addNode = (level: number, id: string, descr: string, type: number) => {
|
const addNode = (level: number, id: string, descr: string, type: number) => {
|
||||||
log.info('addNode', level, id, descr, type);
|
log.info('addNode', level, id, descr, type);
|
||||||
const conf = getConfig();
|
const conf = getConfig();
|
||||||
let padding: number = conf.mindmap?.padding ?? 10;
|
let padding: number = conf.mindmap.padding;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case nodeType.ROUNDED_RECT:
|
case nodeType.ROUNDED_RECT:
|
||||||
case nodeType.RECT:
|
case nodeType.RECT:
|
||||||
@@ -61,10 +61,9 @@ const addNode = (level: number, id: string, descr: string, type: number) => {
|
|||||||
nodes.push(node);
|
nodes.push(node);
|
||||||
} else {
|
} else {
|
||||||
// Syntax error ... there can only bee one root
|
// Syntax error ... there can only bee one root
|
||||||
const error = new Error(
|
throw new Error(
|
||||||
'There can be only one root. No parent could be found for ("' + node.descr + '")'
|
'There can be only one root. No parent could be found for ("' + node.descr + '")'
|
||||||
);
|
);
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@@ -11,6 +11,8 @@ import type { MermaidConfig } from '../../config.type.js';
|
|||||||
import type { Diagram } from '../../Diagram.js';
|
import type { Diagram } from '../../Diagram.js';
|
||||||
import type { D3Element } from '../../mermaidAPI.js';
|
import type { D3Element } from '../../mermaidAPI.js';
|
||||||
import type { MermaidConfigWithDefaults } from '../../config.js';
|
import type { MermaidConfigWithDefaults } from '../../config.js';
|
||||||
|
import { selectSvgElement } from '../../rendering-util/selectSvgElement.js';
|
||||||
|
import { DiagramRenderer, DrawDefinition } from '../../diagram-api/types.js';
|
||||||
|
|
||||||
// Inject the layout algorithm into cytoscape
|
// Inject the layout algorithm into cytoscape
|
||||||
cytoscape.use(coseBilkent);
|
cytoscape.use(coseBilkent);
|
||||||
@@ -158,34 +160,20 @@ function positionNodes(db: MindmapDB, cy: cytoscape.Core) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export const draw = async (text: string, id: string, version: string, diagObj: Diagram) => {
|
export const draw: DrawDefinition = async (text, id, _version, diagObj) => {
|
||||||
const conf = getConfig();
|
const conf = getConfig();
|
||||||
const db = diagObj.db as MindmapDB;
|
const db = diagObj.db as MindmapDB;
|
||||||
conf.htmlLabels = false;
|
conf.htmlLabels = false;
|
||||||
|
|
||||||
log.debug('Rendering mindmap diagram\n' + text, diagObj.parser);
|
log.debug('Rendering mindmap diagram\n' + text);
|
||||||
|
|
||||||
const securityLevel = conf.securityLevel;
|
|
||||||
// Handle root and Document for when rendering in sandbox mode
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
let sandboxElement: any;
|
|
||||||
if (securityLevel === 'sandbox') {
|
|
||||||
sandboxElement = select('#i' + id);
|
|
||||||
}
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
const root: any =
|
|
||||||
securityLevel === 'sandbox'
|
|
||||||
? select(sandboxElement.nodes()[0].contentDocument.body)
|
|
||||||
: select('body');
|
|
||||||
|
|
||||||
const svg = root.select('#' + id);
|
|
||||||
|
|
||||||
svg.append('g');
|
|
||||||
const mm = db.getMindmap();
|
const mm = db.getMindmap();
|
||||||
if (!mm) {
|
if (!mm) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const svg = selectSvgElement(id);
|
||||||
|
|
||||||
// Draw the graph and start with drawing the nodes without proper position
|
// Draw the graph and start with drawing the nodes without proper position
|
||||||
// this gives us the size of the nodes and we can set the positions later
|
// this gives us the size of the nodes and we can set the positions later
|
||||||
|
|
||||||
|
@@ -3,6 +3,8 @@ import { createText } from '../../rendering-util/createText.js';
|
|||||||
import type { FilledMindMapNode, MindmapDB } from './mindmapTypes.js';
|
import type { FilledMindMapNode, MindmapDB } from './mindmapTypes.js';
|
||||||
import type { MermaidConfigWithDefaults } from '../../config.js';
|
import type { MermaidConfigWithDefaults } from '../../config.js';
|
||||||
import type { Point } from '../../types.js';
|
import type { Point } from '../../types.js';
|
||||||
|
import { parseFontSize } from '../../utils.js';
|
||||||
|
|
||||||
const MAX_SECTIONS = 12;
|
const MAX_SECTIONS = 12;
|
||||||
|
|
||||||
type ShapeFunction = (
|
type ShapeFunction = (
|
||||||
@@ -207,11 +209,9 @@ export const drawNode = function (
|
|||||||
.attr('dominant-baseline', 'middle')
|
.attr('dominant-baseline', 'middle')
|
||||||
.attr('text-anchor', 'middle');
|
.attr('text-anchor', 'middle');
|
||||||
}
|
}
|
||||||
// .call(wrap, node.width);
|
|
||||||
const bbox = textElem.node().getBBox();
|
const bbox = textElem.node().getBBox();
|
||||||
// @ts-expect-error TODO: Check if fontSize can be string?
|
const [fontSize] = parseFontSize(conf.fontSize);
|
||||||
const fontSize = conf.fontSize.replace ? conf.fontSize.replace('px', '') : conf.fontSize;
|
node.height = bbox.height + fontSize! * 1.1 * 0.5 + node.padding;
|
||||||
node.height = bbox.height + fontSize * 1.1 * 0.5 + node.padding;
|
|
||||||
node.width = bbox.width + 2 * node.padding;
|
node.width = bbox.width + 2 * node.padding;
|
||||||
if (node.icon) {
|
if (node.icon) {
|
||||||
if (node.type === db.nodeType.CIRCLE) {
|
if (node.type === db.nodeType.CIRCLE) {
|
||||||
|
@@ -925,3 +925,7 @@ export const encodeEntities = function (text: string): string {
|
|||||||
export const decodeEntities = function (text: string): string {
|
export const decodeEntities = function (text: string): string {
|
||||||
return text.replace(/fl°°/g, '&#').replace(/fl°/g, '&').replace(/¶ß/g, ';');
|
return text.replace(/fl°°/g, '&#').replace(/fl°/g, '&').replace(/¶ß/g, ';');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const isString = (value: unknown): value is string => {
|
||||||
|
return typeof value === 'string';
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user