From b1b5ad3c9b0e156d38a7ef2f9012908e0852983d Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Fri, 28 Jun 2024 22:00:11 +0530 Subject: [PATCH] chore: Cleanup flowRenderer-v3-unified.ts --- .../src/diagrams/flowchart/flowRenderer-v2.js | 1 - .../flowchart/flowRenderer-v3-unified.ts | 78 ++++++++----------- 2 files changed, 32 insertions(+), 47 deletions(-) diff --git a/packages/mermaid/src/diagrams/flowchart/flowRenderer-v2.js b/packages/mermaid/src/diagrams/flowchart/flowRenderer-v2.js index 00db88f81..0fe29bec7 100644 --- a/packages/mermaid/src/diagrams/flowchart/flowRenderer-v2.js +++ b/packages/mermaid/src/diagrams/flowchart/flowRenderer-v2.js @@ -29,7 +29,6 @@ export const setConf = function (cnf) { */ export const addVertices = async function (vert, g, svgId, root, doc, diagObj) { const svg = root.select(`[id="${svgId}"]`); - // console.log('SVG:', svg, svg.node(), 'root:', root, root.node()); const keys = vert.keys(); diff --git a/packages/mermaid/src/diagrams/flowchart/flowRenderer-v3-unified.ts b/packages/mermaid/src/diagrams/flowchart/flowRenderer-v3-unified.ts index 0bd216966..f63ba31c5 100644 --- a/packages/mermaid/src/diagrams/flowchart/flowRenderer-v3-unified.ts +++ b/packages/mermaid/src/diagrams/flowchart/flowRenderer-v3-unified.ts @@ -9,16 +9,6 @@ import type { LayoutData } from '../../rendering-util/types.js'; import utils from '../../utils.js'; import { getDirection } from './flowDb.js'; -// Configuration -const conf: Record = {}; - -export const setConf = function (cnf: Record) { - const keys = Object.keys(cnf); - for (const key of keys) { - conf[key] = cnf[key]; - } -}; - export const getClasses = function ( text: string, diagramObj: any @@ -40,8 +30,6 @@ export const draw = async function (text: string, id: string, _version: string, // @ts-ignore - document is always available const doc = securityLevel === 'sandbox' ? sandboxElement.nodes()[0].contentDocument : document; - const DIR = getDirection(); - // The getData method provided in all supported diagrams is used to extract the data from the parsed structure // into the Layout data format log.debug('Before getData: '); @@ -52,7 +40,7 @@ export const draw = async function (text: string, id: string, _version: string, data4Layout.type = diag.type; data4Layout.layoutAlgorithm = layout; - data4Layout.direction = DIR; + data4Layout.direction = getDirection(); data4Layout.nodeSpacing = conf?.nodeSpacing || 50; data4Layout.rankSpacing = conf?.rankSpacing || 50; data4Layout.markers = ['point', 'circle', 'cross']; @@ -70,43 +58,41 @@ export const draw = async function (text: string, id: string, _version: string, setupViewPortForSVG(svg, padding, 'flowchart', conf?.useMaxWidth || false); // If node has a link, wrap it in an anchor SVG object. - data4Layout.nodes.forEach((vertex) => { - if (vertex.link) { - const node = select('#' + id + ' [id="' + vertex.id + '"]'); - if (node) { - const link = doc.createElementNS('http://www.w3.org/2000/svg', 'a'); - link.setAttributeNS('http://www.w3.org/2000/svg', 'class', vertex.cssClasses); - link.setAttributeNS('http://www.w3.org/2000/svg', 'rel', 'noopener'); - if (securityLevel === 'sandbox') { - link.setAttributeNS('http://www.w3.org/2000/svg', 'target', '_top'); - } else if (vertex.linkTarget) { - link.setAttributeNS('http://www.w3.org/2000/svg', 'target', vertex.linkTarget); - } - - const linkNode = node.insert(function () { - return link; - }, ':first-child'); - - const shape = node.select('.label-container'); - if (shape) { - linkNode.append(function () { - return shape.node(); - }); - } - - const label = node.select('.label'); - if (label) { - linkNode.append(function () { - return label.node(); - }); - } - } + for (const vertex of data4Layout.nodes) { + const node = select(`#${id} [id="${vertex.id}"]`); + if (!node || !vertex.link) { + continue; } - }); + const link = doc.createElementNS('http://www.w3.org/2000/svg', 'a'); + link.setAttributeNS('http://www.w3.org/2000/svg', 'class', vertex.cssClasses); + link.setAttributeNS('http://www.w3.org/2000/svg', 'rel', 'noopener'); + if (securityLevel === 'sandbox') { + link.setAttributeNS('http://www.w3.org/2000/svg', 'target', '_top'); + } else if (vertex.linkTarget) { + link.setAttributeNS('http://www.w3.org/2000/svg', 'target', vertex.linkTarget); + } + + const linkNode = node.insert(function () { + return link; + }, ':first-child'); + + const shape = node.select('.label-container'); + if (shape) { + linkNode.append(function () { + return shape.node(); + }); + } + + const label = node.select('.label'); + if (label) { + linkNode.append(function () { + return label.node(); + }); + } + } }; export default { - setConf, getClasses, draw, };