Skip to content

Commit 0a77c54

Browse files
committed
Add more utility functions to Selection object
1 parent 60df991 commit 0a77c54

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

.idea/react-grapher.iml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Background/Background.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export function Background({id, className, pattern, color, size, strokeWidth, an
100100
const sizeMul = typeof size === "number" ? size : patternSizeMap[size ?? "md"]
101101
const strokeWidthMul = strokeWidth ?? 1
102102
switch (pattern) {
103+
case null:
104+
case undefined:
103105
case "grid":
104106
return patternGrid(color, strokeWidthMul, sizeMul)
105107
case "lines":

src/components/ReactGrapher/ReactGrapher.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,9 @@ export function ReactGrapher<N, E>(props: ControlledGraphProps<N, E> | Uncontrol
587587
node: n,
588588
})
589589
}
590-
} else s.selection.setNodesSelection([])
590+
} else s.selection.deselectAllNodes() // if an unselected node is moved, move just that one, deselect the others
591591
// Deselect edges TODO should edges stay selected?
592-
s.selection.setEdgesSelection([])
592+
s.selection.deselectAllEdges()
593593

594594
sendChanges(changes, s)
595595
}
@@ -789,14 +789,16 @@ export function ReactGrapher<N, E>(props: ControlledGraphProps<N, E> | Uncontrol
789789
}
790790
}
791791
},
792-
// Null because it's set below
792+
// Doesn't matter because it's set below
793+
nodeBeingResized: false,
793794
getNode: null as any,
794795
getEdge: null as any,
795796
}), [id, props.static, config.nodesOverEdges])
796797
// This is not put in the useMemo above because nodes/edges objects changing should not trigger a context value change & subsequent re-renders
797798
// As the result of the function itself does not change
798799
internalContext.getNode = nodes.internalMap.get.bind(nodes.internalMap)
799800
internalContext.getEdge = edges.internalMap.get.bind(edges.internalMap)
801+
internalContext.nodeBeingResized = grabbed.type === "resizing"
800802

801803
// Context for other components outside the Viewport
802804
const grapherContext: GrapherContextValue = useMemo(() => ({

src/data/Selection.ts

+25
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@ export interface Selection {
3535
*/
3636
setEdgeSelected(node: string, selected: boolean, newSelection?: boolean): void
3737

38+
/**
39+
* Selects all nodes
40+
*/
41+
selectAllNodes(): void
42+
43+
/**
44+
* Deselects all nodes
45+
*/
46+
deselectAllNodes(): void
47+
48+
/**
49+
* Selects all edges
50+
*/
51+
selectAllEdges(): void
52+
53+
/**
54+
* Deselects all edges
55+
*/
56+
deselectAllEdges(): void
57+
58+
/**
59+
* Selects everything
60+
*/
61+
selectAll(): void
62+
3863
/**
3964
* Deselect all nodes and edges
4065
*/

src/hooks/useSelection.ts

+16
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ export function useSelection<N, E>(nodes: Nodes<N>, edges: Edges<E>): Selection
6060
if (selected) this.setEdgesSelection(edgesSelection.concat(id))
6161
}
6262
},
63+
selectAllNodes() {
64+
this.setNodesSelection(nodes.map(node => node.id))
65+
},
66+
deselectAllNodes() {
67+
this.setNodesSelection([])
68+
},
69+
selectAllEdges() {
70+
this.setEdgesSelection(edges.map(edge => edge.id))
71+
},
72+
deselectAllEdges() {
73+
this.setEdgesSelection([])
74+
},
75+
selectAll() {
76+
this.selectAllNodes()
77+
this.selectAllEdges()
78+
},
6379
deselectAll() {
6480
this.setNodesSelection([])
6581
this.setEdgesSelection([])

src/util/log.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
const TAG = "[ReactGrapher]"
33

44
function critical(...message: any) {
5-
console.error(TAG + " - CRITICAL: ", message)
5+
console.error(TAG + " - CRITICAL:", message)
66
}
77

88
function error(...message: any) {
9-
console.error(TAG + " - ERROR: ", ...message)
9+
console.error(TAG + " - ERROR:", ...message)
1010
}
1111

1212
function warn(...message: any) {
13-
console.warn(TAG + " - WARN: ", ...message)
13+
console.warn(TAG + " - WARN:", ...message)
1414
}
1515

1616
// Critical errors

0 commit comments

Comments
 (0)