diff --git a/packages/mermaid/src/mermaid.ts b/packages/mermaid/src/mermaid.ts index 6a5eb22d9..ae92a36f5 100644 --- a/packages/mermaid/src/mermaid.ts +++ b/packages/mermaid/src/mermaid.ts @@ -461,4 +461,5 @@ export default mermaid; export { calcIntersections, calcNodeIntersections, + calcIntersect, } from './rendering-util/layout-algorithms/fixed/index.js'; diff --git a/packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js b/packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js index 40296a64f..06072dc68 100644 --- a/packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js +++ b/packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js @@ -163,6 +163,38 @@ export const calcIntersections = (startNodeId, endNodeId, startNodeSize, endNode return []; }; +/** + * @param {string} nodeId + * @param {Pick} node + * @param {Point} point + * @returns {Promise | Point} + */ +export const calcIntersect = async (nodeId, point) => { + let node = nodeDB.get(nodeId); + + if (!node) { + // CReate new nodes in order not to require a rendered diagram + const fakeParent = document.createElementNS('http://www.w3.org/2000/svg', 'g'); + const parent = select(fakeParent); + node = Object.assign({}, point); + node.shape = 'rect'; + + await insertNode(parent, node, 'TB'); + + // Insert node will not give any widths as the element is not in the DOM + node.width = 50; + node.height = 50; + node.width = 50; + node.height = 50; + } + + if (!node) { + throw new Error("Start node doesn't exist in the nodeDB"); + } + const intersection = calcIntersectionPoint(node, point); + return intersection; +}; + const doRender = async (_elem, data4Layout, siteConfig, positions) => { const elem = _elem.insert('g').attr('class', 'root'); elem.insert('g').attr('class', 'clusters');