diff --git a/src/DagreReact.tsx b/src/DagreReact.tsx index 8fb8ab3..50182db 100644 --- a/src/DagreReact.tsx +++ b/src/DagreReact.tsx @@ -1,6 +1,6 @@ -import * as React from "react"; +import * as React from 'react'; -import { Graph } from "./graph"; +import { Graph } from './graph'; import { RecursivePartial, ShapesDefinition, @@ -12,17 +12,17 @@ import { ReportSize, Size, NodeOptions, - EdgeOptions -} from "./types"; -import { builtInShapes, getShapeDefinition } from "./shapes/shapes"; -import { builtInNodeLabels, getNodeLabel } from "./nodelabels"; -import { builtInEdgeLabels, getEdgeLabel } from "./edgelabels"; -import { getPathGenerator, builtInPaths } from "./paths"; -import { ValueCache } from "./valuecache"; -import { builtInMarkers, getMarkerComponent } from "./markers"; -import Node from "./Node"; -import Edge from "./Edge"; -import EdgeLabel from "./EdgeLabel"; + EdgeOptions, +} from './types'; +import { builtInShapes, getShapeDefinition } from './shapes/shapes'; +import { builtInNodeLabels, getNodeLabel } from './nodelabels'; +import { builtInEdgeLabels, getEdgeLabel } from './edgelabels'; +import { getPathGenerator, builtInPaths } from './paths'; +import { ValueCache } from './valuecache'; +import { builtInMarkers, getMarkerComponent } from './markers'; +import Node from './Node'; +import Edge from './Edge'; +import EdgeLabel from './EdgeLabel'; export interface DagreReactProps { customShapes: ShapesDefinition; @@ -37,9 +37,20 @@ export interface DagreReactProps { graphLayoutComplete: (width?: number, height?: number) => void; graphOptions: GraphOptions; stage: number; - renderNode?: (node: NodeOptions, reportSize: ReportSize, valueCache: ValueCache) => React.ReactElement; - renderEdge?: (index: number, edgeMeta: EdgeOptions) => React.ReactElement; - renderEdgeLabel?: (index: number, edgeMeta: EdgeOptions, reportSize: ReportSize) => React.ReactElement; + renderNode?: ( + node: NodeOptions, + reportSize: ReportSize, + valueCache: ValueCache + ) => React.ReactElement; + renderEdge?: ( + index: number, + edgeMeta: EdgeOptions + ) => React.ReactElement; + renderEdgeLabel?: ( + index: number, + edgeMeta: EdgeOptions, + reportSize: ReportSize + ) => React.ReactElement; } export interface GraphOptions { @@ -92,7 +103,12 @@ export default class DagreReact extends React.Component< super(props); const graph = new Graph(); graph.setGraphLabelOptions(props.graphOptions); - graph.setGraphData(props.nodes, props.edges, props.defaultNodeConfig, props.defaultEdgeConfig); + graph.setGraphData( + props.nodes, + props.edges, + props.defaultNodeConfig, + props.defaultEdgeConfig + ); this.state = { pathGenerators: { ...builtInPaths, ...props.customPathGenerators }, @@ -101,7 +117,7 @@ export default class DagreReact extends React.Component< edgeLabels: { ...builtInEdgeLabels, ...props.customEdgeLabels }, shapes: { ...builtInShapes, ...props.customShapes }, graph: graph, - previousStage: props.stage + previousStage: props.stage, }; this.valueCache = new ValueCache(); @@ -120,7 +136,7 @@ export default class DagreReact extends React.Component< return { graph, - previousStage: nextProps.stage + previousStage: nextProps.stage, }; } @@ -144,7 +160,10 @@ export default class DagreReact extends React.Component< // console.log("Forcing an update"); this.state.graph.layout(); this.adjustIntersections(); - this.props.graphLayoutComplete(this.state.graph.graph.graph().width, this.state.graph.graph.graph().height); + this.props.graphLayoutComplete( + this.state.graph.graph.graph().width, + this.state.graph.graph.graph().height + ); this.forceUpdate(); } } @@ -208,7 +227,7 @@ export default class DagreReact extends React.Component< shape: (innerSize: Size) => ( ), - label: () => + label: () => , }} ); @@ -243,9 +262,7 @@ export default class DagreReact extends React.Component< edgeMeta={edgeMeta} reportSize={reportSize} > - { () => - - } + {() => } ); }; @@ -260,11 +277,11 @@ export default class DagreReact extends React.Component< const to = graph.graphNodeById(edgeMeta.to); if (!from || !to) { - throw new Error("graph node not found from edge"); + throw new Error('graph node not found from edge'); } if (!edgeMeta.points) { throw new Error( - "points should be set before adjustIntersections is called" + 'points should be set before adjustIntersections is called' ); } diff --git a/src/Edge.tsx b/src/Edge.tsx index 9d662f9..65d2e82 100644 --- a/src/Edge.tsx +++ b/src/Edge.tsx @@ -1,9 +1,6 @@ -import * as React from "react"; +import * as React from 'react'; -import { - MarkerComponent, - EdgeOptions -} from "./types"; +import { MarkerComponent, EdgeOptions } from './types'; type EdgeProps = { edgeMeta: EdgeOptions; @@ -11,11 +8,7 @@ type EdgeProps = { index: number; }; -const Edge: React.FC = ({ - index, - edgeMeta, - markerComponent, -}) => { +const Edge: React.FC = ({ index, edgeMeta, markerComponent }) => { if (!edgeMeta || !edgeMeta.points) { return null; } @@ -29,7 +22,7 @@ const Edge: React.FC = ({ className={edgeMeta.styles.edge.className} d={edgeMeta.path} markerEnd={`url(#${markerId})`} - style={edgeMeta.styles.edge.styles || { stroke: "#000", fill: "none" }} + style={edgeMeta.styles.edge.styles || { stroke: '#000', fill: 'none' }} > diff --git a/src/EdgeLabel.tsx b/src/EdgeLabel.tsx index e8638b4..faae798 100644 --- a/src/EdgeLabel.tsx +++ b/src/EdgeLabel.tsx @@ -1,9 +1,9 @@ -import * as React from "react"; +import * as React from 'react'; -import useSize from "./useSize"; -import { EdgeOptions, ReportSize} from "./types"; +import useSize from './useSize'; +import { EdgeOptions, ReportSize } from './types'; -declare module "react" { +declare module 'react' { interface HTMLAttributes { xmlns?: string; } @@ -20,7 +20,7 @@ const EdgeLabel: React.FC = ({ html, edgeMeta, reportSize, - children + children, }) => { const targetRef = React.useRef(null); const labelRef = React.useRef(null); @@ -55,10 +55,11 @@ const EdgeLabel: React.FC = ({ {html ? ( -
-
- {children()} -
+
+
{children()}
) : ( diff --git a/src/Node.tsx b/src/Node.tsx index 88de3f4..7ea5790 100644 --- a/src/Node.tsx +++ b/src/Node.tsx @@ -1,10 +1,10 @@ -import * as React from "react"; +import * as React from 'react'; -import useSize from "./useSize"; -import { NodeOptions, ReportSize, Size } from "./types"; -import { ValueCache } from "./valuecache"; +import useSize from './useSize'; +import { NodeOptions, ReportSize, Size } from './types'; +import { ValueCache } from './valuecache'; -declare module "react" { +declare module 'react' { interface HTMLAttributes { xmlns?: string; } @@ -26,7 +26,7 @@ const Node: React.FC = ({ reportSize, valueCache, html, - children + children, }) => { const targetRef = React.useRef(null); const labelRef = React.useRef(null); @@ -36,18 +36,18 @@ const Node: React.FC = ({ const labelWithPaddingSize = { width: labelSize.width, - height: labelSize.height + height: labelSize.height, }; const padding = node.styles.node.padding || { top: 0, bottom: 0, left: 0, - right: 0 + right: 0, }; if ( !node.label || (node.label && - node.label !== "" && + node.label !== '' && labelSize.width > 0 && labelSize.height > 0) ) { @@ -112,7 +112,7 @@ const Node: React.FC = ({
{children.label()}
diff --git a/src/config_defaults.ts b/src/config_defaults.ts index 82cf72e..a37e6e7 100644 --- a/src/config_defaults.ts +++ b/src/config_defaults.ts @@ -6,44 +6,43 @@ export const defaultNodeConfig = { top: 0, bottom: 0, left: 0, - right: 0 - } + right: 0, + }, }, shape: { className: undefined, - styles: { fillOpacity: 0, stroke: "#000" }, + styles: { fillOpacity: 0, stroke: '#000' }, cornerRadius: 5, }, label: { className: undefined, - styles: undefined - } + styles: undefined, + }, }, - labelType: "text", - shape: "rect", + labelType: 'text', + shape: 'rect', meta: {}, }; - -export const defaultEdgeConfig = { +export const defaultEdgeConfig = { styles: { label: { className: undefined, - styles: undefined + styles: undefined, }, edge: { className: undefined, - styles: undefined + styles: undefined, }, marker: { className: undefined, - styles: undefined + styles: undefined, }, }, - labelType: "text", - labelPos: "r", + labelType: 'text', + labelPos: 'r', labelOffset: 10, - pathType: "normal", - markerType: "normal", + pathType: 'normal', + markerType: 'normal', meta: {}, -} +}; diff --git a/src/edgelabels/Text.tsx b/src/edgelabels/Text.tsx index 24d634e..c730f31 100644 --- a/src/edgelabels/Text.tsx +++ b/src/edgelabels/Text.tsx @@ -1,14 +1,15 @@ -import * as React from "react"; -import {CustomEdgeLabelProps} from "../types"; +import * as React from 'react'; +import { CustomEdgeLabelProps } from '../types'; -export const Text: React.FC = ({edgeMeta}) => { +export const Text: React.FC = ({ edgeMeta }) => { return ( - + - {edgeMeta.label || ""} + {edgeMeta.label || ''} ); -} - - +}; diff --git a/src/edgelabels/index.ts b/src/edgelabels/index.ts index b6f0251..fd2e440 100644 --- a/src/edgelabels/index.ts +++ b/src/edgelabels/index.ts @@ -1,14 +1,17 @@ -import { Text } from "./Text"; -import { EdgeLabelsDefinition, EdgeLabelDefinition } from "../types"; +import { Text } from './Text'; +import { EdgeLabelsDefinition, EdgeLabelDefinition } from '../types'; export const builtInEdgeLabels: EdgeLabelsDefinition = { text: { renderer: Text, - html: false - } + html: false, + }, }; -export const getEdgeLabel = (type: string, edgeLabels: EdgeLabelsDefinition): EdgeLabelDefinition => { +export const getEdgeLabel = ( + type: string, + edgeLabels: EdgeLabelsDefinition +): EdgeLabelDefinition => { if (edgeLabels[type]) { return edgeLabels[type]; } diff --git a/src/graph.ts b/src/graph.ts index da4078d..0cfa6aa 100644 --- a/src/graph.ts +++ b/src/graph.ts @@ -3,14 +3,11 @@ import { GraphLabel, layout as dagreLayout, Edge, - GraphEdge -} from "dagre"; -import defaultsDeep from "lodash/defaultsDeep"; -import { - defaultNodeConfig, - defaultEdgeConfig -} from "./config_defaults"; -import { NodeOptions, EdgeOptions, RecursivePartial } from "./types"; + GraphEdge, +} from 'dagre'; +import defaultsDeep from 'lodash/defaultsDeep'; +import { defaultNodeConfig, defaultEdgeConfig } from './config_defaults'; +import { NodeOptions, EdgeOptions, RecursivePartial } from './types'; export class Graph { graph: graphlib.Graph; @@ -18,7 +15,6 @@ export class Graph { edges: Array; dirty: boolean; - constructor() { this.graph = new graphlib.Graph(); diff --git a/src/index.tsx b/src/index.tsx index bcf0155..05e26ca 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,8 +1,8 @@ -export { default as DagreReact } from "./DagreReact"; -export { ValueCache } from "./valuecache"; -export { default as Node } from "./Node"; -export { default as Edge } from "./Edge"; -export { default as EdgeLabel } from "./EdgeLabel"; -export { Rect } from "./shapes/Rect"; -export { Text as NodeTextLabel } from "./nodelabels/Text"; -export * from "./types"; +export { default as DagreReact } from './DagreReact'; +export { ValueCache } from './valuecache'; +export { default as Node } from './Node'; +export { default as Edge } from './Edge'; +export { default as EdgeLabel } from './EdgeLabel'; +export { Rect } from './shapes/Rect'; +export { Text as NodeTextLabel } from './nodelabels/Text'; +export * from './types'; diff --git a/src/markers/NormalMarker.tsx b/src/markers/NormalMarker.tsx index eac6a2f..8d41b05 100644 --- a/src/markers/NormalMarker.tsx +++ b/src/markers/NormalMarker.tsx @@ -1,8 +1,8 @@ -import * as React from "react"; -import {MarkerProps} from "../types"; +import * as React from 'react'; +import { MarkerProps } from '../types'; export const NormalMarker: React.FC = ({ edgeMeta, markerId }) => { - return ( + return ( = ({ edgeMeta, markerId }) => { style={ edgeMeta.styles.marker.styles || { strokeWidth: 1, - strokeDasharray: "1, 0" + strokeDasharray: '1, 0', } } > ); -} +}; diff --git a/src/markers/UndirectedMarker.tsx b/src/markers/UndirectedMarker.tsx index fccab8a..5a7554c 100644 --- a/src/markers/UndirectedMarker.tsx +++ b/src/markers/UndirectedMarker.tsx @@ -1,8 +1,11 @@ -import * as React from "react"; -import {MarkerProps} from "../types"; +import * as React from 'react'; +import { MarkerProps } from '../types'; -export const UndirectedMarker: React.FC = ({ edgeMeta, markerId }) => { - return ( +export const UndirectedMarker: React.FC = ({ + edgeMeta, + markerId, +}) => { + return ( = ({ edgeMeta, markerId }) style={ edgeMeta.styles.marker.styles || { strokeWidth: 1, - strokeDasharray: "1, 0" + strokeDasharray: '1, 0', } } > ); -} +}; diff --git a/src/markers/VeeMarker.tsx b/src/markers/VeeMarker.tsx index f12ce10..972c07b 100644 --- a/src/markers/VeeMarker.tsx +++ b/src/markers/VeeMarker.tsx @@ -1,7 +1,7 @@ -import * as React from "react"; -import { MarkerProps } from "../types"; +import * as React from 'react'; +import { MarkerProps } from '../types'; -export const VeeMarker: React.FC = ({ edgeMeta, markerId}) => { +export const VeeMarker: React.FC = ({ edgeMeta, markerId }) => { return ( = ({ edgeMeta, markerId}) => { style={ edgeMeta.styles.marker.styles || { strokeWidth: 1, - strokeDasharray: "1, 0" + strokeDasharray: '1, 0', } } > diff --git a/src/markers/index.ts b/src/markers/index.ts index f6874b2..2774e42 100644 --- a/src/markers/index.ts +++ b/src/markers/index.ts @@ -1,16 +1,19 @@ -import { NormalMarker } from "./NormalMarker"; -import { VeeMarker } from "./VeeMarker"; -import { UndirectedMarker } from "./UndirectedMarker"; +import { NormalMarker } from './NormalMarker'; +import { VeeMarker } from './VeeMarker'; +import { UndirectedMarker } from './UndirectedMarker'; -import {MarkerComponent, MarkerComponents} from "../types"; +import { MarkerComponent, MarkerComponents } from '../types'; export const builtInMarkers: MarkerComponents = { normal: NormalMarker, vee: VeeMarker, - undirected: UndirectedMarker + undirected: UndirectedMarker, }; -export const getMarkerComponent = (type: string, markerComponents: MarkerComponents): MarkerComponent => { +export const getMarkerComponent = ( + type: string, + markerComponents: MarkerComponents +): MarkerComponent => { if (markerComponents[type]) { return markerComponents[type]; } diff --git a/src/nodelabels/Text.tsx b/src/nodelabels/Text.tsx index b43615e..2dd3fb4 100644 --- a/src/nodelabels/Text.tsx +++ b/src/nodelabels/Text.tsx @@ -1,14 +1,15 @@ -import * as React from "react"; -import {CustomNodeLabelProps} from "../types"; +import * as React from 'react'; +import { CustomNodeLabelProps } from '../types'; -export const Text: React.FC = ({node}) => { +export const Text: React.FC = ({ node }) => { return ( - + {node.label} ); -} - - +}; diff --git a/src/nodelabels/index.ts b/src/nodelabels/index.ts index 36258e2..523ced3 100644 --- a/src/nodelabels/index.ts +++ b/src/nodelabels/index.ts @@ -1,14 +1,17 @@ -import { Text } from "./Text"; -import { NodeLabelsDefinition, NodeLabelDefinition } from "../types"; +import { Text } from './Text'; +import { NodeLabelsDefinition, NodeLabelDefinition } from '../types'; export const builtInNodeLabels: NodeLabelsDefinition = { text: { renderer: Text, - html: false - } + html: false, + }, }; -export const getNodeLabel = (type: string, nodeLabels: NodeLabelsDefinition): NodeLabelDefinition => { +export const getNodeLabel = ( + type: string, + nodeLabels: NodeLabelsDefinition +): NodeLabelDefinition => { if (nodeLabels[type]) { return nodeLabels[type]; } diff --git a/src/paths/index.ts b/src/paths/index.ts index 2cf7228..932634e 100644 --- a/src/paths/index.ts +++ b/src/paths/index.ts @@ -1,11 +1,14 @@ -import { generatePath } from "./normal"; -import {PathGenerator, PathGeneratorTypes} from "../types"; +import { generatePath } from './normal'; +import { PathGenerator, PathGeneratorTypes } from '../types'; export const builtInPaths: PathGeneratorTypes = { - normal: generatePath + normal: generatePath, }; -export const getPathGenerator = (type: string, pathGenerators: PathGeneratorTypes): PathGenerator => { +export const getPathGenerator = ( + type: string, + pathGenerators: PathGeneratorTypes +): PathGenerator => { if (pathGenerators[type]) { return pathGenerators[type]; } diff --git a/src/paths/normal.ts b/src/paths/normal.ts index 3988c69..a4af2f4 100644 --- a/src/paths/normal.ts +++ b/src/paths/normal.ts @@ -1,9 +1,9 @@ -import { Point } from "../types"; +import { Point } from '../types'; export const generatePath = (points: Array) => { - let path = ""; + let path = ''; for (let x = 0; x < points.length; ++x) { - path = path + (x === 0 ? "M" : "L") + points[x].x + "," + points[x].y; + path = path + (x === 0 ? 'M' : 'L') + points[x].x + ',' + points[x].y; } return path; diff --git a/src/shapes/Circle.tsx b/src/shapes/Circle.tsx index 722f96f..f809285 100644 --- a/src/shapes/Circle.tsx +++ b/src/shapes/Circle.tsx @@ -1,6 +1,6 @@ -import * as React from "react"; +import * as React from 'react'; -import { ShapeComponentProps } from "../types"; +import { ShapeComponentProps } from '../types'; export const Circle: React.FC = ({ node, innerSize }) => { if (!node || !innerSize || !(innerSize.width && innerSize.height)) { diff --git a/src/shapes/Diamond.tsx b/src/shapes/Diamond.tsx index edaab23..c409090 100644 --- a/src/shapes/Diamond.tsx +++ b/src/shapes/Diamond.tsx @@ -1,5 +1,5 @@ -import * as React from "react"; -import { ShapeComponentProps, Point, Size } from "../../src/types"; +import * as React from 'react'; +import { ShapeComponentProps, Point, Size } from '../../src/types'; export const calculateDiamondPoints = (size: Size): Array => { const width = (size.width * Math.SQRT2) / 2; @@ -9,11 +9,11 @@ export const calculateDiamondPoints = (size: Size): Array => { { x: 0, y: -height }, { x: -width, y: 0 }, { x: 0, y: height }, - { x: width, y: 0 } + { x: width, y: 0 }, ]; return points; -} +}; export const Diamond: React.FC = ({ node, innerSize }) => { if (!node || !innerSize || !(innerSize.width && innerSize.height)) { @@ -22,5 +22,10 @@ export const Diamond: React.FC = ({ node, innerSize }) => { const points = calculateDiamondPoints(innerSize); - return `${p.x}, ${p.y}`).join(" ")} />; + return ( + `${p.x}, ${p.y}`).join(' ')} + /> + ); }; diff --git a/src/shapes/Rect.tsx b/src/shapes/Rect.tsx index 3878655..4554644 100644 --- a/src/shapes/Rect.tsx +++ b/src/shapes/Rect.tsx @@ -1,6 +1,6 @@ -import * as React from "react"; +import * as React from 'react'; -import { ShapeComponentProps } from "../types"; +import { ShapeComponentProps } from '../types'; export const Rect: React.FC = ({ node, innerSize }) => { if (!node || !innerSize || !(innerSize.width && innerSize.height)) { @@ -15,8 +15,8 @@ export const Rect: React.FC = ({ node, innerSize }) => { data-tip="hello world" className={node.styles.shape.className} style={node.styles.shape.styles || {}} - rx={node.styles.shape.cornerRadius || 0 } - ry={node.styles.shape.cornerRadius || 0 } + rx={node.styles.shape.cornerRadius || 0} + ry={node.styles.shape.cornerRadius || 0} x={-halfWidth} y={-halfHeight} width={innerSize.width} diff --git a/src/shapes/intersects/diamond.ts b/src/shapes/intersects/diamond.ts index c10afd2..cdb5bd5 100644 --- a/src/shapes/intersects/diamond.ts +++ b/src/shapes/intersects/diamond.ts @@ -9,7 +9,7 @@ const intersectLine = (p1: Point, p2: Point, q1: Point, q2: Point) => { // Algorithm from J. Avro, (ed.) Graphics Gems, No 2, Morgan Kaufmann, 1994, // p7 and p473. let a1, a2, b1, b2, c1, c2; - let r1, r2 , r3, r4; + let r1, r2, r3, r4; let denom, offset, num; var x, y; @@ -17,36 +17,36 @@ const intersectLine = (p1: Point, p2: Point, q1: Point, q2: Point) => { // b1 y + c1 = 0. a1 = p2.y - p1.y; b1 = p1.x - p2.x; - c1 = (p2.x * p1.y) - (p1.x * p2.y); + c1 = p2.x * p1.y - p1.x * p2.y; // Compute r3 and r4. - r3 = ((a1 * q1.x) + (b1 * q1.y) + c1); - r4 = ((a1 * q2.x) + (b1 * q2.y) + c1); + r3 = a1 * q1.x + b1 * q1.y + c1; + r4 = a1 * q2.x + b1 * q2.y + c1; // Check signs of r3 and r4. If both point 3 and point 4 lie on // same side of line 1, the line segments do not intersect. - if ((r3 !== 0) && (r4 !== 0) && sameSign(r3, r4)) { + if (r3 !== 0 && r4 !== 0 && sameSign(r3, r4)) { return /*DONT_INTERSECT*/; } // Compute a2, b2, c2 where line joining points 3 and 4 is G(x,y) = a2 x + b2 y + c2 = 0 a2 = q2.y - q1.y; b2 = q1.x - q2.x; - c2 = (q2.x * q1.y) - (q1.x * q2.y); + c2 = q2.x * q1.y - q1.x * q2.y; // Compute r1 and r2 - r1 = (a2 * p1.x) + (b2 * p1.y) + c2; - r2 = (a2 * p2.x) + (b2 * p2.y) + c2; + r1 = a2 * p1.x + b2 * p1.y + c2; + r2 = a2 * p2.x + b2 * p2.y + c2; // Check signs of r1 and r2. If both point 1 and point 2 lie // on same side of second line segment, the line segments do // not intersect. - if ((r1 !== 0) && (r2 !== 0) && (sameSign(r1, r2))) { + if (r1 !== 0 && r2 !== 0 && sameSign(r1, r2)) { return /*DONT_INTERSECT*/; } // Line segments intersect: compute intersection point. - denom = (a1 * b2) - (a2 * b1); + denom = a1 * b2 - a2 * b1; if (denom === 0) { return /*COLLINEAR*/; } @@ -56,18 +56,18 @@ const intersectLine = (p1: Point, p2: Point, q1: Point, q2: Point) => { // The denom/2 is to get rounding instead of truncating. It // is added or subtracted to the numerator, depending upon the // sign of the numerator. - num = (b1 * c2) - (b2 * c1); - x = (num < 0) ? ((num - offset) / denom) : ((num + offset) / denom); + num = b1 * c2 - b2 * c1; + x = num < 0 ? (num - offset) / denom : (num + offset) / denom; - num = (a2 * c1) - (a1 * c2); - y = (num < 0) ? ((num - offset) / denom) : ((num + offset) / denom); + num = a2 * c1 - a1 * c2; + y = num < 0 ? (num - offset) / denom : (num + offset) / denom; return { x: x, y: y }; -} +}; const sameSign = (r1: number, r2: number): boolean => { return r1 * r2 > 0; -} +}; export const intersectDiamond = (node: NodeOptions, point: Point): Point => { const width = node.width! / 2; @@ -77,7 +77,7 @@ export const intersectDiamond = (node: NodeOptions, point: Point): Point => { { x: 0, y: -height }, { x: -width, y: 0 }, { x: 0, y: height }, - { x: width, y: 0 } + { x: width, y: 0 }, ]; var x1 = node.x!; @@ -93,20 +93,24 @@ export const intersectDiamond = (node: NodeOptions, point: Point): Point => { }); var left = x1 - node.width! / 2 - minX; - var top = y1 - node.height! / 2 - minY; + var top = y1 - node.height! / 2 - minY; for (var i = 0; i < polyPoints.length; i++) { var p1 = polyPoints[i]; var p2 = polyPoints[i < polyPoints.length - 1 ? i + 1 : 0]; - var intersect = intersectLine({ x: node.x!, y: node.y! }, point, - {x: left + p1.x, y: top + p1.y}, {x: left + p2.x, y: top + p2.y}); + var intersect = intersectLine( + { x: node.x!, y: node.y! }, + point, + { x: left + p1.x, y: top + p1.y }, + { x: left + p2.x, y: top + p2.y } + ); if (intersect) { intersections.push(intersect); } } if (!intersections.length) { - console.log("NO INTERSECTION FOUND, RETURN NODE CENTER", node); + console.log('NO INTERSECTION FOUND, RETURN NODE CENTER', node); return { x: node.x!, y: node.y! }; } @@ -121,8 +125,8 @@ export const intersectDiamond = (node: NodeOptions, point: Point): Point => { var qdy = q.y - point.y; var distq = Math.sqrt(qdx * qdx + qdy * qdy); - return (distp < distq) ? -1 : (distp === distq ? 0 : 1); + return distp < distq ? -1 : distp === distq ? 0 : 1; }); } return intersections[0]; -} +}; diff --git a/src/shapes/intersects/ellipse.ts b/src/shapes/intersects/ellipse.ts index 2477101..1cc892e 100644 --- a/src/shapes/intersects/ellipse.ts +++ b/src/shapes/intersects/ellipse.ts @@ -1,4 +1,4 @@ -import { Point, NodeOptions } from "../../types"; +import { Point, NodeOptions } from '../../types'; // intersections from https://github.com/dagrejs/dagre-d3 export const intersectEllipse = (node: NodeOptions, point: Point) => { diff --git a/src/shapes/intersects/rect.ts b/src/shapes/intersects/rect.ts index 49482b1..8311acc 100644 --- a/src/shapes/intersects/rect.ts +++ b/src/shapes/intersects/rect.ts @@ -1,4 +1,4 @@ -import { Point, NodeOptions } from "../../types"; +import { Point, NodeOptions } from '../../types'; // intersection code from https://github.com/dagrejs/dagre-d3 export const intersectRect = (node: NodeOptions, point: Point) => { diff --git a/src/shapes/shapes.ts b/src/shapes/shapes.ts index c9347e2..c9f55c1 100644 --- a/src/shapes/shapes.ts +++ b/src/shapes/shapes.ts @@ -1,27 +1,30 @@ -import { Rect } from "./Rect"; -import { Circle } from "./Circle"; -import { intersectRect } from "./intersects/rect"; -import { intersectEllipse } from "./intersects/ellipse"; -import { ShapesDefinition, ShapeDefinition } from "../types"; +import { Rect } from './Rect'; +import { Circle } from './Circle'; +import { intersectRect } from './intersects/rect'; +import { intersectEllipse } from './intersects/ellipse'; +import { ShapesDefinition, ShapeDefinition } from '../types'; import { Diamond } from './Diamond'; import { intersectDiamond } from './intersects/diamond'; export const builtInShapes: ShapesDefinition = { rect: { renderer: Rect, - intersection: intersectRect + intersection: intersectRect, }, circle: { renderer: Circle, - intersection: intersectEllipse + intersection: intersectEllipse, }, diamond: { renderer: Diamond, - intersection: intersectDiamond - } + intersection: intersectDiamond, + }, }; -export const getShapeDefinition = (shape: string, shapes: ShapesDefinition): ShapeDefinition => { +export const getShapeDefinition = ( + shape: string, + shapes: ShapesDefinition +): ShapeDefinition => { if (shapes[shape]) { return shapes[shape]; } diff --git a/src/types.ts b/src/types.ts index 728d57c..cc36382 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,5 @@ -import {ValueCache} from "./valuecache"; -import * as CSS from "csstype"; +import { ValueCache } from './valuecache'; +import * as CSS from 'csstype'; export type ReportSize = (width: number, height: number) => void; @@ -11,10 +11,11 @@ export type Size = { export type Point = { x: number; y: number }; export type RecursivePartial = { - [P in keyof T]?: - T[P] extends (infer U)[] ? RecursivePartial[] : - T[P] extends object ? RecursivePartial : - T[P]; + [P in keyof T]?: T[P] extends (infer U)[] + ? RecursivePartial[] + : T[P] extends object + ? RecursivePartial + : T[P]; }; export type ShapeComponentProps = { @@ -32,13 +33,13 @@ export type MarkerProps = { }; export type ShapesDefinition = { - [key: string]: ShapeDefinition; + [key: string]: ShapeDefinition; }; export type NodeLabelDefinition = { renderer: React.FC; html: boolean; -} +}; export type CustomEdgeLabelProps = { edgeMeta: EdgeOptions; @@ -49,19 +50,23 @@ export type NodeLabelsDefinition = { [key: string]: NodeLabelDefinition }; export type EdgeLabelDefinition = { renderer: React.FC; html: boolean; -} +}; -export type MarkerComponent = React.FC; -export type MarkerComponents = { [key: string]: MarkerComponent }; +export type MarkerComponent = React.FC; +export type MarkerComponents = { [key: string]: MarkerComponent }; -export type PathGenerator = (points: Array) => string; -export type PathGeneratorTypes = { [key: string]: PathGenerator }; +export type PathGenerator = (points: Array) => string; +export type PathGeneratorTypes = { [key: string]: PathGenerator }; export type EdgeLabelsDefinition = { [key: string]: EdgeLabelDefinition }; export type ShapeDefinition = { renderer: React.FC; - intersection: (node: NodeOptions, point: Point, valueCache: ValueCache) => Point; + intersection: ( + node: NodeOptions, + point: Point, + valueCache: ValueCache + ) => Point; }; export interface NodeOptions { @@ -95,14 +100,14 @@ export interface NodeOptions { y?: number; meta: { [key: string]: any; - } + }; } export interface EdgeOptions { from: string; to: string; label?: string; - labelPos: "l" | "r" | "c"; + labelPos: 'l' | 'r' | 'c'; labelOffset: number; labelType: string; markerType: string; @@ -117,18 +122,17 @@ export interface EdgeOptions { label: { className: string; styles: CSS.Properties; - }, + }; edge: { className: string; styles: CSS.Properties; - }, + }; marker: { className: string; styles: CSS.Properties; - }, - }, + }; + }; meta: { [key: string]: any; - } + }; } - diff --git a/src/useSize.ts b/src/useSize.ts index 05c874e..a582461 100644 --- a/src/useSize.ts +++ b/src/useSize.ts @@ -1,7 +1,7 @@ /* eslint-disable react-hooks/exhaustive-deps */ -import { useLayoutEffect, useState, RefObject } from "react"; +import { useLayoutEffect, useState, RefObject } from 'react'; -import { ReportSize } from "./types"; +import { ReportSize } from './types'; type Size = { width?: number; @@ -17,7 +17,7 @@ function useSize( ) { const [dim, setDim] = useState({ height: 0, - width: 0 + width: 0, }); useLayoutEffect(() => { @@ -40,21 +40,26 @@ function useSize( return; } - if (reportSize && (monitorSize && monitorSize.width && monitorSize.width > 0)) { - if (!size || (size.width !== width || size.height !== height)) { + if ( + reportSize && + monitorSize && + monitorSize.width && + monitorSize.width > 0 + ) { + if (!size || size.width !== width || size.height !== height) { reportSize(width, height); } } setDim({ width, height }); - }, [ + }, [ ref, tag, size ? size.width : undefined, size ? size.height : undefined, reportSize, monitorSize ? monitorSize.height : undefined, - monitorSize ? monitorSize.width : undefined + monitorSize ? monitorSize.width : undefined, ]); return dim; diff --git a/src/valuecache.ts b/src/valuecache.ts index 335a97a..74fdb3e 100644 --- a/src/valuecache.ts +++ b/src/valuecache.ts @@ -13,4 +13,3 @@ export class ValueCache { return this.values.get(key); } } -