Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ export const WorkflowBlock = memo(function WorkflowBlock({
edge.source,
edge.target
),
isEdgeSelected: (edge.data as { isSelected?: boolean } | undefined)?.isSelected,
})
if (!isHighlighted) continue
if (edge.source === id) keys.push(edge.sourceHandle || 'source')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ const WorkflowEdgeComponent = (props: WorkflowEdgeProps) => {
source,
target
)
const isEdgeSelected = Boolean((data as { isSelected?: boolean } | undefined)?.isSelected)
const shouldHighlightEdge = isEdgeHighlighted({
isEndpointSelected: isConnectedToSelection,
isConnectedToEditor,
isEdgeSelected,
})

const previewExecutionStatus = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @vitest-environment jsdom
*/

import { act } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { describe, expect, it, vi } from 'vitest'
import { useShiftSelectionLock } from '@/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-shift-selection-lock'

function renderShiftSelectionLock() {
let api: ReturnType<typeof useShiftSelectionLock> | null = null
const host = document.createElement('div')
const root: Root = createRoot(host)

function Probe() {
api = useShiftSelectionLock({ isHandMode: false })
return null
}

act(() => root.render(<Probe />))
if (!api) throw new Error('hook did not render')

return { api, unmount: () => act(() => root.unmount()) }
}

describe('useShiftSelectionLock', () => {
it('does not swallow Shift clicks on selectable elements inside the pane', () => {
const { api, unmount } = renderShiftSelectionLock()
const pane = document.createElement('div')
pane.className = 'react-flow__pane'
const edge = document.createElement('path')
edge.classList.add('react-flow__edge-interaction')
pane.appendChild(edge)
const preventDefault = vi.fn()

api.handleCanvasMouseDown({
shiftKey: true,
target: edge,
preventDefault,
} as unknown as React.MouseEvent)

expect(preventDefault).not.toHaveBeenCalled()
unmount()
})

it('still prevents native selection when Shift-drag starts on the pane background', () => {
const { api, unmount } = renderShiftSelectionLock()
const pane = document.createElement('div')
pane.className = 'react-flow__pane'
const preventDefault = vi.fn()

api.handleCanvasMouseDown({
shiftKey: true,
target: pane,
preventDefault,
} as unknown as React.MouseEvent)

expect(preventDefault).toHaveBeenCalledOnce()
unmount()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function useShiftSelectionLock({
if (!event.shiftKey) return

const target = event.target as HTMLElement | null
const isPaneTarget = Boolean(target?.closest('.react-flow__pane, .react-flow__selectionpane'))
const isPaneTarget = Boolean(target?.matches('.react-flow__pane, .react-flow__selectionpane'))

if (isPaneTarget && isHandMode) {
setIsShiftSelecting(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
import { describe, expect, it } from 'vitest'
import { isPositionalTriggerBlock } from '@/app/workspace/[workspaceId]/w/[workflowId]/utils/workflow-canvas-helpers'
import {
applyEdgeSelectionChanges,
getEdgeSelectionMapKey,
isPositionalTriggerBlock,
} from '@/app/workspace/[workspaceId]/w/[workflowId]/utils/workflow-canvas-helpers'

describe('edge selection helpers', () => {
it('keeps modifier selections and removes deselected edges', () => {
const selected = new Map([['edge-1-loop-1', 'edge-1']])
const selectionKeys = new Map([
['edge-1', 'edge-1-loop-1'],
['edge-2', 'edge-2-loop-1'],
])

const withSecondEdge = applyEdgeSelectionChanges(
selected,
[{ id: 'edge-2', type: 'select', selected: true }],
(edgeId) => selectionKeys.get(edgeId) ?? null
)
expect([...withSecondEdge]).toEqual([
['edge-1-loop-1', 'edge-1'],
['edge-2-loop-1', 'edge-2'],
])

const withoutFirstEdge = applyEdgeSelectionChanges(
withSecondEdge,
[{ id: 'edge-1', type: 'select', selected: false }],
(edgeId) => selectionKeys.get(edgeId) ?? null
)
expect([...withoutFirstEdge]).toEqual([['edge-2-loop-1', 'edge-2']])
})

it('uses nested context keys and ignores temporary edges', () => {
const key = getEdgeSelectionMapKey(
{ id: 'edge-1', source: 'source', target: 'target' },
[{ id: 'source', parentId: 'loop-1' }, { id: 'target' }],
{}
)
expect(key).toBe('edge-1-loop-1')

const selected = new Map<string, string>()
expect(
applyEdgeSelectionChanges(
selected,
[{ id: 'connection-block-selector-edge', type: 'select', selected: true }],
() => null
)
).toBe(selected)
})
})

describe('isPositionalTriggerBlock', () => {
it('returns false for a block nested in a subflow even with no incoming edges', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BLOCK_DIMENSIONS, CONTAINER_DIMENSIONS, getNoteBlockHeight } from '@sim/workflow-renderer'
import type { Edge, Node } from '@xyflow/react'
import type { Edge, EdgeChange, Node } from '@xyflow/react'
import { isEqual } from 'es-toolkit'
import { TriggerUtils } from '@/lib/workflows/triggers/triggers'
import { clampPositionToContainer } from '@/app/workspace/[workspaceId]/w/[workflowId]/utils/node-position-utils'
Expand Down Expand Up @@ -330,7 +330,7 @@ export function getNodeSelectionContextId(
return node.parentId || blocks[node.id]?.data?.parentId || null
}

export function getEdgeSelectionContextId(
function getEdgeSelectionContextId(
edge: Pick<Edge, 'source' | 'target'>,
nodes: Array<Pick<Node, 'id' | 'parentId'>>,
blocks: Record<string, { data?: { parentId?: string } }>
Expand All @@ -344,6 +344,44 @@ export function getEdgeSelectionContextId(
return null
}

/** Stable key for transient edge selection, including a nested subflow context when present. */
export function getEdgeSelectionMapKey(
edge: Pick<Edge, 'id' | 'source' | 'target'>,
nodes: Array<Pick<Node, 'id' | 'parentId'>>,
blocks: Record<string, { data?: { parentId?: string } }>
): string {
const contextId = getEdgeSelectionContextId(edge, nodes, blocks)
return contextId ? `${edge.id}-${contextId}` : edge.id
}

type EdgeSelectChange = Extract<EdgeChange, { type: 'select' }>

/** Applies React Flow selection changes to the canvas' transient edge-selection map. */
export function applyEdgeSelectionChanges(
current: Map<string, string>,
changes: EdgeSelectChange[],
getSelectionKey: (edgeId: string) => string | null
): Map<string, string> {
let next: Map<string, string> | null = null
const writable = () => (next ??= new Map(current))

for (const change of changes) {
if (change.selected) {
const selectionKey = getSelectionKey(change.id)
if (selectionKey && (next ?? current).get(selectionKey) !== change.id) {
writable().set(selectionKey, change.id)
}
continue
}

for (const [selectionKey, edgeId] of next ?? current) {
if (edgeId === change.id) writable().delete(selectionKey)
}
}

return next ?? current
}

export function resolveSelectionContextConflicts(
nodes: Node[],
blocks: Record<string, { data?: { parentId?: string } }>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const reactFlowStyles = [
'[&_.react-flow__selectionpane]:select-none',
String.raw`[&_.react-flow\_\_selection]:border-[var(--text-secondary)]!`,
String.raw`[&_.react-flow\_\_selection]:bg-[color-mix(in_oklch,var(--text-secondary)_8%,transparent)]!`,
String.raw`[&_.react-flow\_\_edge:focus-visible_.react-flow\_\_edge-path]:drop-shadow-[0_0_2px_var(--text-secondary)]`,
'[&_.react-flow__background]:hidden',
'[&_.react-flow__node-subflowNode.selected]:shadow-none!',
].join(' ')
Expand Down
Loading
Loading