-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: vendor vis-network-react, update failing test
- vis-network-react is distributed as CommonJS instead of ESM
- Loading branch information
1 parent
01072bc
commit a59fca5
Showing
6 changed files
with
159 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/** | ||
* All credit goes to the authors of the vis-network-react package at https://www.npmjs.com/package/vis-network-react | ||
* | ||
* This package is distributed as a commonjs module, so it is not compatible with Vite's default ESM module loading. | ||
* Therefore, I have vendored the code directly into this project. | ||
*/ | ||
import differenceWith from "lodash/differenceWith"; | ||
import isEqual from "lodash/isEqual"; | ||
import PropTypes from "prop-types"; | ||
import React, { useEffect, useRef } from "react"; | ||
import { DataSet } from "vis-data/peer/esm/vis-data"; | ||
import { Network } from "vis-network/peer/esm/vis-network"; | ||
|
||
import "vis-network/styles/vis-network.css"; | ||
|
||
const defaultOptions = { | ||
physics: { | ||
stabilization: false, | ||
}, | ||
autoResize: false, | ||
edges: { | ||
smooth: false, | ||
color: "#000000", | ||
width: 0.5, | ||
arrows: { | ||
to: { | ||
enabled: true, | ||
scaleFactor: 0.5, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const Graph = ({ | ||
data, | ||
options = defaultOptions, | ||
events = {}, | ||
style = { width: "100%", height: "100%" }, | ||
getNetwork, | ||
getNodes, | ||
getEdges, | ||
}) => { | ||
const nodes = useRef(new DataSet(data.nodes)); | ||
const edges = useRef(new DataSet(data.edges)); | ||
const network = useRef(null); | ||
const container = useRef(null); | ||
|
||
useEffect(() => { | ||
network.current = new Network( | ||
container.current, | ||
{ nodes: nodes.current, edges: edges.current }, | ||
options | ||
); | ||
|
||
if (getNetwork) { | ||
getNetwork(network.current); | ||
} | ||
|
||
if (getNodes) { | ||
getNodes(nodes.current); | ||
} | ||
|
||
if (getEdges) { | ||
getEdges(edges.current); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
const nodesChange = !isEqual(nodes.current, data.nodes); | ||
const edgesChange = !isEqual(edges.current, data.edges); | ||
|
||
if (nodesChange) { | ||
const idIsEqual = (n1, n2) => n1.id === n2.id; | ||
const nodesRemoved = differenceWith( | ||
nodes.current.get(), | ||
data.nodes, | ||
idIsEqual | ||
); | ||
const nodesAdded = differenceWith( | ||
data.nodes, | ||
nodes.current.get(), | ||
idIsEqual | ||
); | ||
const nodesChanged = differenceWith( | ||
differenceWith(data.nodes, nodes.current.get(), isEqual), | ||
nodesAdded | ||
); | ||
|
||
nodes.current.remove(nodesRemoved); | ||
nodes.current.add(nodesAdded); | ||
nodes.current.update(nodesChanged); | ||
} | ||
|
||
if (edgesChange) { | ||
const edgesRemoved = differenceWith( | ||
edges.current.get(), | ||
data.edges, | ||
isEqual | ||
); | ||
const edgesAdded = differenceWith( | ||
data.edges, | ||
edges.current.get(), | ||
isEqual | ||
); | ||
const edgesChanged = differenceWith( | ||
differenceWith(data.edges, edges.current.get(), isEqual), | ||
edgesAdded | ||
); | ||
edges.current.remove(edgesRemoved); | ||
edges.current.add(edgesAdded); | ||
edges.current.update(edgesChanged); | ||
} | ||
|
||
if ((nodesChange || edgesChange) && getNetwork) { | ||
getNetwork(network.current); | ||
} | ||
|
||
if (nodesChange && getNodes) { | ||
getNodes(nodes.current); | ||
} | ||
|
||
if (edgesChange && getEdges) { | ||
getEdges(edges.current); | ||
} | ||
}, [data]); | ||
|
||
useEffect(() => { | ||
network.current.setOptions(options); | ||
}, [options]); | ||
|
||
useEffect(() => { | ||
// Add user provied events to network | ||
for (const eventName of Object.keys(events)) { | ||
network.current.on(eventName, events[eventName]); | ||
} | ||
|
||
return () => { | ||
for (const eventName of Object.keys(events)) { | ||
network.current.off(eventName, events[eventName]); | ||
} | ||
}; | ||
}, [events]); | ||
|
||
return <div ref={container} style={style}/>; | ||
}; | ||
|
||
Graph.propTypes = { | ||
data: PropTypes.object, | ||
options: PropTypes.object, | ||
events: PropTypes.object, | ||
style: PropTypes.object, | ||
getNetwork: PropTypes.func, | ||
getNodes: PropTypes.func, | ||
getEdges: PropTypes.func, | ||
}; | ||
|
||
export default Graph; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters