From ff6988e875dbc96e5a4b726a33ad58fdb8822e5f Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Tue, 30 Jan 2024 09:51:40 +0530 Subject: [PATCH] chore: Address review comments Co-authored-by: Reda Al Sulais --- .../mermaid/src/diagrams/mindmap/mindmapDb.ts | 5 ++-- .../src/diagrams/mindmap/mindmapRenderer.ts | 24 +++++-------------- .../mermaid/src/diagrams/mindmap/svgDraw.ts | 8 +++---- packages/mermaid/src/utils.ts | 4 ++++ 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/packages/mermaid/src/diagrams/mindmap/mindmapDb.ts b/packages/mermaid/src/diagrams/mindmap/mindmapDb.ts index e2b453a0d..2a55a9f7b 100644 --- a/packages/mermaid/src/diagrams/mindmap/mindmapDb.ts +++ b/packages/mermaid/src/diagrams/mindmap/mindmapDb.ts @@ -31,7 +31,7 @@ const getMindmap = () => { const addNode = (level: number, id: string, descr: string, type: number) => { log.info('addNode', level, id, descr, type); const conf = getConfig(); - let padding: number = conf.mindmap?.padding ?? 10; + let padding: number = conf.mindmap.padding; switch (type) { case nodeType.ROUNDED_RECT: case nodeType.RECT: @@ -61,10 +61,9 @@ const addNode = (level: number, id: string, descr: string, type: number) => { nodes.push(node); } else { // 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 + '")' ); - throw error; } } }; diff --git a/packages/mermaid/src/diagrams/mindmap/mindmapRenderer.ts b/packages/mermaid/src/diagrams/mindmap/mindmapRenderer.ts index 6b9871567..72136fdb4 100644 --- a/packages/mermaid/src/diagrams/mindmap/mindmapRenderer.ts +++ b/packages/mermaid/src/diagrams/mindmap/mindmapRenderer.ts @@ -11,6 +11,8 @@ import type { MermaidConfig } from '../../config.type.js'; import type { Diagram } from '../../Diagram.js'; import type { D3Element } from '../../mermaidAPI.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 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 db = diagObj.db as MindmapDB; 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(); if (!mm) { return; } + const svg = selectSvgElement(id); + // 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 diff --git a/packages/mermaid/src/diagrams/mindmap/svgDraw.ts b/packages/mermaid/src/diagrams/mindmap/svgDraw.ts index 797a3fd99..a7d4679bc 100644 --- a/packages/mermaid/src/diagrams/mindmap/svgDraw.ts +++ b/packages/mermaid/src/diagrams/mindmap/svgDraw.ts @@ -3,6 +3,8 @@ import { createText } from '../../rendering-util/createText.js'; import type { FilledMindMapNode, MindmapDB } from './mindmapTypes.js'; import type { MermaidConfigWithDefaults } from '../../config.js'; import type { Point } from '../../types.js'; +import { parseFontSize } from '../../utils.js'; + const MAX_SECTIONS = 12; type ShapeFunction = ( @@ -207,11 +209,9 @@ export const drawNode = function ( .attr('dominant-baseline', 'middle') .attr('text-anchor', 'middle'); } - // .call(wrap, node.width); const bbox = textElem.node().getBBox(); - // @ts-expect-error TODO: Check if fontSize can be string? - const fontSize = conf.fontSize.replace ? conf.fontSize.replace('px', '') : conf.fontSize; - node.height = bbox.height + fontSize * 1.1 * 0.5 + node.padding; + const [fontSize] = parseFontSize(conf.fontSize); + node.height = bbox.height + fontSize! * 1.1 * 0.5 + node.padding; node.width = bbox.width + 2 * node.padding; if (node.icon) { if (node.type === db.nodeType.CIRCLE) { diff --git a/packages/mermaid/src/utils.ts b/packages/mermaid/src/utils.ts index b90d33f47..06aca5ab2 100644 --- a/packages/mermaid/src/utils.ts +++ b/packages/mermaid/src/utils.ts @@ -925,3 +925,7 @@ export const encodeEntities = function (text: string): string { export const decodeEntities = function (text: string): string { return text.replace(/fl°°/g, '&#').replace(/fl°/g, '&').replace(/¶ß/g, ';'); }; + +export const isString = (value: unknown): value is string => { + return typeof value === 'string'; +};