feat(rg): removeElements helper

This commit is contained in:
moklick
2019-07-25 18:27:41 +02:00
parent d587b1340d
commit bb17c2fcb6
4 changed files with 36 additions and 17 deletions
+10
View File
@@ -242,6 +242,14 @@
var isNode = function isNode(element) { var isNode = function isNode(element) {
return !element.source && !element.target; return !element.source && !element.target;
}; };
var removeElements = function removeElements(elements, elementsToRemove) {
var nodeIdsToRemove = elementsToRemove.filter(isNode).map(function (n) {
return n.id;
});
return elements.filter(function (e) {
return !nodeIdsToRemove.includes(e.id) && !nodeIdsToRemove.includes(e.target) && !nodeIdsToRemove.includes(e.source);
});
};
var parseElements = function parseElements(e) { var parseElements = function parseElements(e) {
e.type = e.type || 'default'; e.type = e.type || 'default';
@@ -33266,10 +33274,12 @@
var isNode$1 = isNode; var isNode$1 = isNode;
var isEdge$1 = isEdge; var isEdge$1 = isEdge;
var removeElements$1 = removeElements;
exports.default = ReactGraph; exports.default = ReactGraph;
exports.isEdge = isEdge$1; exports.isEdge = isEdge$1;
exports.isNode = isNode$1; exports.isNode = isNode$1;
exports.removeElements = removeElements$1;
Object.defineProperty(exports, '__esModule', { value: true }); Object.defineProperty(exports, '__esModule', { value: true });
+5 -14
View File
@@ -1,6 +1,6 @@
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import Graph, { isEdge, isNode } from '../src'; import Graph, { isEdge, removeElements } from '../src';
// import Graph from '../dist/ReactGraph'; // import Graph from '../dist/ReactGraph';
const SpecialNode = ({ data, onChange, styles }) => ( const SpecialNode = ({ data, onChange, styles }) => (
@@ -57,12 +57,11 @@ class App extends PureComponent {
} }
onLoad(graphInstance) { onLoad(graphInstance) {
this.graphInstance = graphInstance;
console.log('graph loaded:', this.graphInstance); console.log('graph loaded:', this.graphInstance);
window.rg = graphInstance; window.rg = graphInstance;
this.graphInstance.fitView();
this.graphInstance = graphInstance;
this.graphInstance.fitView();
this.setState({ this.setState({
graphLoaded: true graphLoaded: true
}); });
@@ -98,17 +97,9 @@ class App extends PureComponent {
this.graphInstance.zoomOut(); this.graphInstance.zoomOut();
} }
onElementsRemove(elements) { onElementsRemove(elementsToRemove) {
const nodeIds = elements.filter(isNode).map(n => n.id);
this.setState(prevState => ({ this.setState(prevState => ({
elements: prevState.elements.filter(e => { elements: removeElements(prevState.elements, elementsToRemove)
return (
!nodeIds.includes(e.id) &&
!nodeIds.includes(e.target) &&
!nodeIds.includes(e.source)
);
})
})); }));
} }
+13
View File
@@ -1,6 +1,19 @@
export const isEdge = element => element.source && element.target; export const isEdge = element => element.source && element.target;
export const isNode = element => !element.source && !element.target; export const isNode = element => !element.source && !element.target;
export const removeElements = (elements, elementsToRemove) => {
const nodeIdsToRemove = elementsToRemove.filter(isNode).map(n => n.id);
return elements.filter(e => {
return (
!nodeIdsToRemove.includes(e.id) &&
!nodeIdsToRemove.includes(e.target) &&
!nodeIdsToRemove.includes(e.source)
);
});
}
export const parseElements = e => { export const parseElements = e => {
e.type = e.type || 'default'; e.type = e.type || 'default';
+8 -3
View File
@@ -1,7 +1,12 @@
import ReactGraph from './ReactGraph'; import ReactGraph from './ReactGraph';
import { isNode as isNodeRG , isEdge as isEdgeRG } from './graph-utils'; import {
isNode as _isNode ,
isEdge as _isEdge,
removeElements as _removeElements
} from './graph-utils';
export const isNode = isNodeRG; export const isNode = _isNode;
export const isEdge = isEdgeRG; export const isEdge = _isEdge;
export const removeElements = _removeElements;
export default ReactGraph; export default ReactGraph;