Skip to content

Commit

Permalink
Merge pull request #10 from Layvier/linting
Browse files Browse the repository at this point in the history
Linting
  • Loading branch information
bobthekingofegypt committed Mar 12, 2021
2 parents 95769a0 + b721cb0 commit e1bb185
Show file tree
Hide file tree
Showing 27 changed files with 262 additions and 219 deletions.
69 changes: 43 additions & 26 deletions src/DagreReact.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand All @@ -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<any>;
renderEdge?: (index: number, edgeMeta: EdgeOptions) => React.ReactElement<any>;
renderEdgeLabel?: (index: number, edgeMeta: EdgeOptions, reportSize: ReportSize) => React.ReactElement<any>;
renderNode?: (
node: NodeOptions,
reportSize: ReportSize,
valueCache: ValueCache
) => React.ReactElement<any>;
renderEdge?: (
index: number,
edgeMeta: EdgeOptions
) => React.ReactElement<any>;
renderEdgeLabel?: (
index: number,
edgeMeta: EdgeOptions,
reportSize: ReportSize
) => React.ReactElement<any>;
}

export interface GraphOptions {
Expand Down Expand Up @@ -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 },
Expand All @@ -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();
Expand All @@ -120,7 +136,7 @@ export default class DagreReact extends React.Component<

return {
graph,
previousStage: nextProps.stage
previousStage: nextProps.stage,
};
}

Expand All @@ -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();
}
}
Expand Down Expand Up @@ -208,7 +227,7 @@ export default class DagreReact extends React.Component<
shape: (innerSize: Size) => (
<shape.renderer node={node} innerSize={innerSize} />
),
label: () => <nodeLabel.renderer node={node} />
label: () => <nodeLabel.renderer node={node} />,
}}
</Node>
);
Expand Down Expand Up @@ -243,9 +262,7 @@ export default class DagreReact extends React.Component<
edgeMeta={edgeMeta}
reportSize={reportSize}
>
{ () =>
<edgeLabel.renderer edgeMeta={edgeMeta} />
}
{() => <edgeLabel.renderer edgeMeta={edgeMeta} />}
</EdgeLabel>
);
};
Expand All @@ -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'
);
}

Expand Down
15 changes: 4 additions & 11 deletions src/Edge.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import * as React from "react";
import * as React from 'react';

import {
MarkerComponent,
EdgeOptions
} from "./types";
import { MarkerComponent, EdgeOptions } from './types';

type EdgeProps = {
edgeMeta: EdgeOptions;
markerComponent: MarkerComponent;
index: number;
};

const Edge: React.FC<EdgeProps> = ({
index,
edgeMeta,
markerComponent,
}) => {
const Edge: React.FC<EdgeProps> = ({ index, edgeMeta, markerComponent }) => {
if (!edgeMeta || !edgeMeta.points) {
return null;
}
Expand All @@ -29,7 +22,7 @@ const Edge: React.FC<EdgeProps> = ({
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' }}
></path>
<defs>
<MarkerComponent edgeMeta={edgeMeta} markerId={markerId} />
Expand Down
19 changes: 10 additions & 9 deletions src/EdgeLabel.tsx
Original file line number Diff line number Diff line change
@@ -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<T> {
xmlns?: string;
}
Expand All @@ -20,7 +20,7 @@ const EdgeLabel: React.FC<EdgeLabelProps> = ({
html,
edgeMeta,
reportSize,
children
children,
}) => {
const targetRef = React.useRef<SVGGElement>(null);
const labelRef = React.useRef<any>(null);
Expand Down Expand Up @@ -55,10 +55,11 @@ const EdgeLabel: React.FC<EdgeLabelProps> = ({
<g transform={`translate(${offsetX},${offsetY})`} className="label">
{html ? (
<foreignObject width={labelSize.width} height={labelSize.height}>
<div xmlns="http://www.w3.org/1999/xhtml" style={{"display": "inline-block"}}>
<div ref={labelRef}>
{children()}
</div>
<div
xmlns="http://www.w3.org/1999/xhtml"
style={{ display: 'inline-block' }}
>
<div ref={labelRef}>{children()}</div>
</div>
</foreignObject>
) : (
Expand Down
20 changes: 10 additions & 10 deletions src/Node.tsx
Original file line number Diff line number Diff line change
@@ -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<T> {
xmlns?: string;
}
Expand All @@ -26,7 +26,7 @@ const Node: React.FC<NodeProps> = ({
reportSize,
valueCache,
html,
children
children,
}) => {
const targetRef = React.useRef<SVGGElement>(null);
const labelRef = React.useRef<any>(null);
Expand All @@ -36,18 +36,18 @@ const Node: React.FC<NodeProps> = ({

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)
) {
Expand Down Expand Up @@ -112,7 +112,7 @@ const Node: React.FC<NodeProps> = ({
<foreignObject width={labelSize.width} height={labelSize.height}>
<div
xmlns="http://www.w3.org/1999/xhtml"
style={{ display: "inline-block" }}
style={{ display: 'inline-block' }}
>
<div ref={labelRef}>{children.label()}</div>
</div>
Expand Down
33 changes: 16 additions & 17 deletions src/config_defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
}
};
17 changes: 9 additions & 8 deletions src/edgelabels/Text.tsx
Original file line number Diff line number Diff line change
@@ -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<CustomEdgeLabelProps> = ({edgeMeta}) => {
export const Text: React.FC<CustomEdgeLabelProps> = ({ edgeMeta }) => {
return (
<text className={edgeMeta.styles.label.className} style={edgeMeta.styles.label.styles || {}}>
<text
className={edgeMeta.styles.label.className}
style={edgeMeta.styles.label.styles || {}}
>
<tspan xmlSpace="preserve" dy="1em" x="1">
{edgeMeta.label || ""}
{edgeMeta.label || ''}
</tspan>
</text>
);
}


};
13 changes: 8 additions & 5 deletions src/edgelabels/index.ts
Original file line number Diff line number Diff line change
@@ -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];
}
Expand Down
Loading

0 comments on commit e1bb185

Please sign in to comment.