mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-10-13 11:09:39 +02:00
Merge pull request #10 from Mermaid-Chart/export-calcIntersections-and-calcNodeIntersections
Export `calcIntersections, calcNodeIntersections`
This commit is contained in:
@@ -67,7 +67,7 @@
|
||||
|
||||
#### Defined in
|
||||
|
||||
[packages/mermaid/src/diagram-api/types.ts:153](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagram-api/types.ts#L153)
|
||||
[packages/mermaid/src/diagram-api/types.ts:157](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagram-api/types.ts#L157)
|
||||
|
||||
---
|
||||
|
||||
@@ -77,7 +77,7 @@
|
||||
|
||||
#### Defined in
|
||||
|
||||
[packages/mermaid/src/diagram-api/types.ts:155](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagram-api/types.ts#L155)
|
||||
[packages/mermaid/src/diagram-api/types.ts:159](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagram-api/types.ts#L159)
|
||||
|
||||
## Variables
|
||||
|
||||
@@ -88,3 +88,55 @@
|
||||
#### Defined in
|
||||
|
||||
[packages/mermaid/src/mermaid.ts:442](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L442)
|
||||
|
||||
## Functions
|
||||
|
||||
### calcIntersections
|
||||
|
||||
▸ **calcIntersections**(`startNodeId`, `endNodeId`, `startNodeSize`, `endNodeSize`): `IntersectionPoint`\[]
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type |
|
||||
| :-------------------- | :---------------------- |
|
||||
| `startNodeId` | `string` |
|
||||
| `endNodeId` | `undefined` \| `string` |
|
||||
| `startNodeSize` | `NodePosition` |
|
||||
| `endNodeSize` | `Object` |
|
||||
| `endNodeSize.height?` | `number` |
|
||||
| `endNodeSize.width?` | `number` |
|
||||
| `endNodeSize.x` | `number` |
|
||||
| `endNodeSize.y` | `number` |
|
||||
|
||||
#### Returns
|
||||
|
||||
`IntersectionPoint`\[]
|
||||
|
||||
**`Throws`**
|
||||
|
||||
If the start node doesn't exist in the nodeDB (e.g. `render` hasn't been called yet)
|
||||
|
||||
#### Defined in
|
||||
|
||||
[packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js:106](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js#L106)
|
||||
|
||||
---
|
||||
|
||||
### calcNodeIntersections
|
||||
|
||||
▸ **calcNodeIntersections**(`_node1`, `_node2`): `IntersectionPoint`\[] | `Promise`<`IntersectionPoint`\[]>
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type |
|
||||
| :------- | :------------------------------------------------------------------------------------------------ |
|
||||
| `_node1` | `Pick`<`Node`, `"width"` \| `"height"` \| `"x"` \| `"y"` \| `"id"` \| `"shape"` \| `"intersect"`> |
|
||||
| `_node2` | `Pick`<`Node`, `"width"` \| `"height"` \| `"x"` \| `"y"` \| `"id"` \| `"shape"` \| `"intersect"`> |
|
||||
|
||||
#### Returns
|
||||
|
||||
`IntersectionPoint`\[] | `Promise`<`IntersectionPoint`\[]>
|
||||
|
||||
#### Defined in
|
||||
|
||||
[packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js:78](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js#L78)
|
||||
|
@@ -111,7 +111,11 @@ interface Point {
|
||||
y: number;
|
||||
}
|
||||
|
||||
interface NodePosition extends Point {
|
||||
export interface IntersectionPoint extends Point {
|
||||
pos: 't' | 'b' | 'l' | 'r';
|
||||
}
|
||||
|
||||
export interface NodePosition extends Point {
|
||||
width?: number;
|
||||
height?: number;
|
||||
}
|
||||
|
@@ -457,3 +457,8 @@ const mermaid: Mermaid = {
|
||||
};
|
||||
|
||||
export default mermaid;
|
||||
|
||||
export {
|
||||
calcIntersections,
|
||||
calcNodeIntersections,
|
||||
} from './rendering-util/layout-algorithms/fixed/index.js';
|
||||
|
@@ -14,6 +14,14 @@ import {
|
||||
import { select } from 'd3';
|
||||
import { getConfig } from '../../../diagram-api/diagramAPI.js';
|
||||
|
||||
/**
|
||||
* @typedef {import('../../types.js').Node} Node
|
||||
* @typedef {import('../../../types.js').Point} Point
|
||||
* @typedef {import('../../../diagram-api/types.js').NodePosition} NodePosition
|
||||
* @typedef {import('../../../diagram-api/types.js').IntersectionPoint} IntersectionPoint
|
||||
*/
|
||||
|
||||
/** @type {Map<string, Node>} */
|
||||
let nodeDB = new Map();
|
||||
|
||||
// const fixInterSections = (points, startNodeId, endNodeId) => {
|
||||
@@ -29,6 +37,11 @@ let nodeDB = new Map();
|
||||
// return points;
|
||||
// };
|
||||
|
||||
/**
|
||||
* @param {Pick<Node, 'shape' | 'id' | 'intersect' | 'x' | 'y' | 'width' | 'height'>} node
|
||||
* @param {Point} point
|
||||
* @returns {IntersectionPoint}
|
||||
*/
|
||||
const calcIntersectionPoint = (node, point) => {
|
||||
const intersection = node.intersect(point);
|
||||
|
||||
@@ -56,7 +69,13 @@ const calcIntersectionPoint = (node, point) => {
|
||||
|
||||
return { x: intersection.x, y: intersection.y, pos };
|
||||
};
|
||||
const calcNodeIntersections = async (_node1, _node2) => {
|
||||
|
||||
/**
|
||||
* @param {Pick<Node, 'shape' | 'id' | 'intersect' | 'x' | 'y' | 'width' | 'height'>} _node1
|
||||
* @param {Pick<Node, 'shape' | 'id' | 'intersect' | 'x' | 'y' | 'width' | 'height'>} _node2
|
||||
* @returns {Promise<IntersectionPoint[]> | IntersectionPoint[]}
|
||||
*/
|
||||
export const calcNodeIntersections = async (_node1, _node2) => {
|
||||
// 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);
|
||||
@@ -75,8 +94,20 @@ const calcNodeIntersections = async (_node1, _node2) => {
|
||||
const endIntersection = calcIntersectionPoint(node2, { x: node1.x, y: node1.y });
|
||||
return [startIntersection, endIntersection];
|
||||
};
|
||||
const calcIntersections = (startNodeId, endNodeId, startNodeSize, endNodeSize) => {
|
||||
|
||||
/**
|
||||
* @param {string} startNodeId
|
||||
* @param {string | undefined} endNodeId
|
||||
* @param {NodePosition} startNodeSize
|
||||
* @param {{width?: number, height?: number, x: number, y: number}} endNodeSize
|
||||
* @returns {IntersectionPoint[]}
|
||||
* @throws {Error} If the start node doesn't exist in the nodeDB (e.g. `render` hasn't been called yet)
|
||||
*/
|
||||
export const calcIntersections = (startNodeId, endNodeId, startNodeSize, endNodeSize) => {
|
||||
const startNode = nodeDB.get(startNodeId);
|
||||
if (!startNode) {
|
||||
throw new Error("Start node doesn't exist in the nodeDB");
|
||||
}
|
||||
if (startNodeSize) {
|
||||
startNode.x = startNodeSize.x;
|
||||
startNode.y = startNodeSize.y;
|
||||
@@ -297,6 +328,8 @@ const doRender = async (_elem, data4Layout, siteConfig, positions) => {
|
||||
positionEdgeLabel(edge, paths);
|
||||
}
|
||||
if (window) {
|
||||
// TODO: Remove this now that we can do:
|
||||
// import { calcIntersections, calcNodeIntersections } from '@mermaid-chart/mermaid';
|
||||
window.calcIntersections = calcIntersections;
|
||||
window.calcNodeIntersections = calcNodeIntersections;
|
||||
}
|
||||
|
Reference in New Issue
Block a user