Skip to content

Commit

Permalink
refactor(core): use connection lookup to check for selected elements (#…
Browse files Browse the repository at this point in the history
…1737)

* refactor(core): use connection lookup to check for selected elements

Signed-off-by: braks <[email protected]>

* chore(changeset): add

Signed-off-by: braks <[email protected]>

---------

Signed-off-by: braks <[email protected]>
  • Loading branch information
bcakmakoglu authored Jan 8, 2025
1 parent b293e16 commit fbc1c26
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-impalas-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-flow/core": minor
---

Use connection lookup to check what elements should be selected by a user selection box.
79 changes: 32 additions & 47 deletions packages/core/src/container/Pane/Pane.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import { ref, toRef, watch } from 'vue'
import UserSelection from '../../components/UserSelection/UserSelection.vue'
import NodesSelection from '../../components/NodesSelection/NodesSelection.vue'
import type { NodeChange } from '../../types'
import type { EdgeChange, NodeChange } from '../../types'
import { SelectionMode } from '../../types'
import { useKeyPress, useVueFlow } from '../../composables'
import { getEventPosition, getNodesInside, getSelectionChanges } from '../../utils'
import { areSetsEqual, getEventPosition, getNodesInside, getSelectionChanges } from '../../utils'
import { getMousePosition } from './utils'
const { isSelecting, selectionKeyPressed } = defineProps<{ isSelecting: boolean; selectionKeyPressed: boolean }>()
Expand All @@ -30,18 +30,18 @@ const {
multiSelectionActive,
edgeLookup,
nodeLookup,
connectionLookup,
defaultEdgeOptions,
} = useVueFlow()
const container = ref<HTMLDivElement | null>(null)
const prevSelectedNodesCount = ref(0)
const selectedNodeIds = ref<Set<string>>(new Set())
const prevSelectedEdgesCount = ref(0)
const selectedEdgeIds = ref<Set<string>>(new Set())
const containerBounds = ref<DOMRect>()
const edgeIdLookup = ref<Map<string, Set<string>>>(new Map())
const hasActiveSelection = toRef(() => elementsSelectable.value && (isSelecting || userSelectionActive.value))
// Used to prevent click events when the user lets go of the selectionKey during a selection
Expand Down Expand Up @@ -78,14 +78,6 @@ function wrapHandler(handler: Function, containerRef: HTMLDivElement | null) {
}
}
function resetUserSelection() {
userSelectionActive.value = false
userSelectionRect.value = null
prevSelectedNodesCount.value = 0
prevSelectedEdgesCount.value = 0
}
function onClick(event: MouseEvent) {
if (selectionInProgress) {
selectionInProgress = false
Expand Down Expand Up @@ -129,12 +121,6 @@ function onPointerDown(event: PointerEvent) {
selectionStarted = true
selectionInProgress = false
edgeIdLookup.value = new Map()
for (const [id, edge] of edgeLookup.value) {
edgeIdLookup.value.set(edge.source, edgeIdLookup.value.get(edge.source)?.add(id) || new Set([id]))
edgeIdLookup.value.set(edge.target, edgeIdLookup.value.get(edge.target)?.add(id) || new Set([id]))
}
removeSelectedElements()
Expand Down Expand Up @@ -169,38 +155,39 @@ function onPointerMove(event: PointerEvent) {
height: Math.abs(mouseY - startY),
}
const selectedNodes = getNodesInside(
nodes.value,
nextUserSelectRect,
viewport.value,
selectionMode.value === SelectionMode.Partial,
true,
)
const selectedEdgeIds = new Set<string>()
const selectedNodeIds = new Set<string>()
const prevSelectedNodeIds = selectedNodeIds.value
const prevSelectedEdgeIds = selectedEdgeIds.value
for (const selectedNode of selectedNodes) {
selectedNodeIds.add(selectedNode.id)
selectedNodeIds.value = new Set(
getNodesInside(nodes.value, nextUserSelectRect, viewport.value, selectionMode.value === SelectionMode.Partial, true).map(
(node) => node.id,
),
)
const edgeIds = edgeIdLookup.value.get(selectedNode.id)
selectedEdgeIds.value = new Set()
const edgesSelectable = defaultEdgeOptions.value?.selectable ?? true
if (edgeIds) {
for (const edgeId of edgeIds) {
selectedEdgeIds.add(edgeId)
// We look for all edges connected to the selected nodes
for (const nodeId of selectedNodeIds.value) {
const connections = connectionLookup.value.get(nodeId)
if (!connections) {
continue
}
for (const { edgeId } of connections.values()) {
const edge = edgeLookup.value.get(edgeId)
if (edge && (edge.selectable ?? edgesSelectable)) {
selectedEdgeIds.value.add(edgeId)
}
}
}
if (prevSelectedNodesCount.value !== selectedNodeIds.size) {
prevSelectedNodesCount.value = selectedNodeIds.size
const changes = getSelectionChanges(nodeLookup.value, selectedNodeIds, true) as NodeChange[]
if (!areSetsEqual(prevSelectedNodeIds, selectedNodeIds.value)) {
const changes = getSelectionChanges(nodeLookup.value, selectedNodeIds.value, true) as NodeChange[]
emits.nodesChange(changes)
}
if (prevSelectedEdgesCount.value !== selectedEdgeIds.size) {
prevSelectedEdgesCount.value = selectedEdgeIds.size
const changes = getSelectionChanges(edgeLookup.value, selectedEdgeIds)
if (!areSetsEqual(prevSelectedEdgeIds, selectedEdgeIds.value)) {
const changes = getSelectionChanges(edgeLookup.value, selectedEdgeIds.value) as EdgeChange[]
emits.edgesChange(changes)
}
Expand All @@ -222,11 +209,9 @@ function onPointerUp(event: PointerEvent) {
onClick(event)
}
if (prevSelectedNodesCount.value > 0) {
nodesSelectionActive.value = true
}
resetUserSelection()
userSelectionActive.value = false
userSelectionRect.value = null
nodesSelectionActive.value = selectedNodeIds.value.size > 0
emits.selectionEnd(event)
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/utils/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,23 @@ export function areConnectionMapsEqual(a?: Map<string, Connection>, b?: Map<stri
return true
}

/**
* @internal
*/
export function areSetsEqual(a: Set<string>, b: Set<string>) {
if (a.size !== b.size) {
return false
}

for (const item of a) {
if (!b.has(item)) {
return false
}
}

return true
}

/**
* @internal
*/
Expand Down

0 comments on commit fbc1c26

Please sign in to comment.