2
2
import { ref , toRef , watch } from ' vue'
3
3
import UserSelection from ' ../../components/UserSelection/UserSelection.vue'
4
4
import NodesSelection from ' ../../components/NodesSelection/NodesSelection.vue'
5
- import type { NodeChange } from ' ../../types'
5
+ import type { EdgeChange , NodeChange } from ' ../../types'
6
6
import { SelectionMode } from ' ../../types'
7
7
import { useKeyPress , useVueFlow } from ' ../../composables'
8
- import { getEventPosition , getNodesInside , getSelectionChanges } from ' ../../utils'
8
+ import { areSetsEqual , getEventPosition , getNodesInside , getSelectionChanges } from ' ../../utils'
9
9
import { getMousePosition } from ' ./utils'
10
10
11
11
const { isSelecting, selectionKeyPressed } = defineProps <{ isSelecting: boolean ; selectionKeyPressed: boolean }>()
@@ -30,18 +30,18 @@ const {
30
30
multiSelectionActive,
31
31
edgeLookup,
32
32
nodeLookup,
33
+ connectionLookup,
34
+ defaultEdgeOptions,
33
35
} = useVueFlow ()
34
36
35
37
const container = ref <HTMLDivElement | null >(null )
36
38
37
- const prevSelectedNodesCount = ref ( 0 )
39
+ const selectedNodeIds = ref < Set < string >>( new Set () )
38
40
39
- const prevSelectedEdgesCount = ref ( 0 )
41
+ const selectedEdgeIds = ref < Set < string >>( new Set () )
40
42
41
43
const containerBounds = ref <DOMRect >()
42
44
43
- const edgeIdLookup = ref <Map <string , Set <string >>>(new Map ())
44
-
45
45
const hasActiveSelection = toRef (() => elementsSelectable .value && (isSelecting || userSelectionActive .value ))
46
46
47
47
// Used to prevent click events when the user lets go of the selectionKey during a selection
@@ -78,14 +78,6 @@ function wrapHandler(handler: Function, containerRef: HTMLDivElement | null) {
78
78
}
79
79
}
80
80
81
- function resetUserSelection() {
82
- userSelectionActive .value = false
83
- userSelectionRect .value = null
84
-
85
- prevSelectedNodesCount .value = 0
86
- prevSelectedEdgesCount .value = 0
87
- }
88
-
89
81
function onClick(event : MouseEvent ) {
90
82
if (selectionInProgress ) {
91
83
selectionInProgress = false
@@ -129,12 +121,6 @@ function onPointerDown(event: PointerEvent) {
129
121
130
122
selectionStarted = true
131
123
selectionInProgress = false
132
- edgeIdLookup .value = new Map ()
133
-
134
- for (const [id, edge] of edgeLookup .value ) {
135
- edgeIdLookup .value .set (edge .source , edgeIdLookup .value .get (edge .source )?.add (id ) || new Set ([id ]))
136
- edgeIdLookup .value .set (edge .target , edgeIdLookup .value .get (edge .target )?.add (id ) || new Set ([id ]))
137
- }
138
124
139
125
removeSelectedElements ()
140
126
@@ -169,38 +155,39 @@ function onPointerMove(event: PointerEvent) {
169
155
height: Math .abs (mouseY - startY ),
170
156
}
171
157
172
- const selectedNodes = getNodesInside (
173
- nodes .value ,
174
- nextUserSelectRect ,
175
- viewport .value ,
176
- selectionMode .value === SelectionMode .Partial ,
177
- true ,
178
- )
179
-
180
- const selectedEdgeIds = new Set <string >()
181
- const selectedNodeIds = new Set <string >()
158
+ const prevSelectedNodeIds = selectedNodeIds .value
159
+ const prevSelectedEdgeIds = selectedEdgeIds .value
182
160
183
- for (const selectedNode of selectedNodes ) {
184
- selectedNodeIds .add (selectedNode .id )
161
+ selectedNodeIds .value = new Set (
162
+ getNodesInside (nodes .value , nextUserSelectRect , viewport .value , selectionMode .value === SelectionMode .Partial , true ).map (
163
+ (node ) => node .id ,
164
+ ),
165
+ )
185
166
186
- const edgeIds = edgeIdLookup .value .get (selectedNode .id )
167
+ selectedEdgeIds .value = new Set ()
168
+ const edgesSelectable = defaultEdgeOptions .value ?.selectable ?? true
187
169
188
- if (edgeIds ) {
189
- for (const edgeId of edgeIds ) {
190
- selectedEdgeIds .add (edgeId )
170
+ // We look for all edges connected to the selected nodes
171
+ for (const nodeId of selectedNodeIds .value ) {
172
+ const connections = connectionLookup .value .get (nodeId )
173
+ if (! connections ) {
174
+ continue
175
+ }
176
+ for (const { edgeId } of connections .values ()) {
177
+ const edge = edgeLookup .value .get (edgeId )
178
+ if (edge && (edge .selectable ?? edgesSelectable )) {
179
+ selectedEdgeIds .value .add (edgeId )
191
180
}
192
181
}
193
182
}
194
183
195
- if (prevSelectedNodesCount .value !== selectedNodeIds .size ) {
196
- prevSelectedNodesCount .value = selectedNodeIds .size
197
- const changes = getSelectionChanges (nodeLookup .value , selectedNodeIds , true ) as NodeChange []
184
+ if (! areSetsEqual (prevSelectedNodeIds , selectedNodeIds .value )) {
185
+ const changes = getSelectionChanges (nodeLookup .value , selectedNodeIds .value , true ) as NodeChange []
198
186
emits .nodesChange (changes )
199
187
}
200
188
201
- if (prevSelectedEdgesCount .value !== selectedEdgeIds .size ) {
202
- prevSelectedEdgesCount .value = selectedEdgeIds .size
203
- const changes = getSelectionChanges (edgeLookup .value , selectedEdgeIds )
189
+ if (! areSetsEqual (prevSelectedEdgeIds , selectedEdgeIds .value )) {
190
+ const changes = getSelectionChanges (edgeLookup .value , selectedEdgeIds .value ) as EdgeChange []
204
191
emits .edgesChange (changes )
205
192
}
206
193
@@ -222,11 +209,9 @@ function onPointerUp(event: PointerEvent) {
222
209
onClick (event )
223
210
}
224
211
225
- if (prevSelectedNodesCount .value > 0 ) {
226
- nodesSelectionActive .value = true
227
- }
228
-
229
- resetUserSelection ()
212
+ userSelectionActive .value = false
213
+ userSelectionRect .value = null
214
+ nodesSelectionActive .value = selectedNodeIds .value .size > 0
230
215
231
216
emits .selectionEnd (event )
232
217
0 commit comments