fix onSelectionEnd behavior

This commit is contained in:
peterkogo
2025-10-27 15:53:06 +01:00
parent 5ec0cac7fa
commit 1f43ccc3ab
4 changed files with 12 additions and 8 deletions

View File

@@ -45,6 +45,8 @@ const BasicFlow = () => {
onMove={onMove} onMove={onMove}
onMoveEnd={onMoveEnd} onMoveEnd={onMoveEnd}
onPaneClick={(e) => console.log('pane click', e)} onPaneClick={(e) => console.log('pane click', e)}
onSelectionStart={(e) => console.log('on selection start', e)}
onSelectionEnd={(e) => console.log('on selection end', e)}
> >
<Background variant={BackgroundVariant.Cross} /> <Background variant={BackgroundVariant.Cross} />
<Controls /> <Controls />

View File

@@ -47,6 +47,8 @@
{onmove} {onmove}
{onmoveend} {onmoveend}
onpaneclick={(e) => console.log('on pane click', e)} onpaneclick={(e) => console.log('on pane click', e)}
onselectionend={(e) => console.log('on selection end', e)}
onselectionstart={(e) => console.log('on selection start', e)}
> >
<Controls /> <Controls />
<Background variant={BackgroundVariant.Dots} /> <Background variant={BackgroundVariant.Dots} />

View File

@@ -120,6 +120,7 @@ export function Pane({
const onClickCapture = (event: ReactMouseEvent) => { const onClickCapture = (event: ReactMouseEvent) => {
if (selectionInProgress.current) { if (selectionInProgress.current) {
event.stopPropagation(); event.stopPropagation();
selectionInProgress.current = false;
} }
}; };
@@ -202,6 +203,7 @@ export function Pane({
if (distance <= paneClickDistance) { if (distance <= paneClickDistance) {
return; return;
} }
onSelectionStart?.(event);
} }
selectionInProgress.current = true; selectionInProgress.current = true;
@@ -277,14 +279,9 @@ export function Pane({
userSelectionRect: null, userSelectionRect: null,
nodesSelectionActive: selectedNodeIds.current.size > 0, nodesSelectionActive: selectedNodeIds.current.size > 0,
}); });
onSelectionEnd?.(event);
/* if (selectionInProgress.current) {
* If the user kept holding the selectionKey during the selection, onSelectionEnd?.(event);
* we need to reset the selectionInProgress, so the next click event is not prevented
*/
if (selectionKeyPressed || selectionOnDrag) {
selectionInProgress.current = false;
} }
selectionStarted.current = false; selectionStarted.current = false;

View File

@@ -147,6 +147,7 @@
if (distance <= paneClickDistance) { if (distance <= paneClickDistance) {
return; return;
} }
onselectionstart?.(event);
} }
selectionInProgress = true; selectionInProgress = true;
@@ -220,7 +221,9 @@
store.selectionRectMode = 'nodes'; store.selectionRectMode = 'nodes';
} }
onselectionend?.(event); if (selectionInProgress) {
onselectionend?.(event);
}
selectionStarted = false; selectionStarted = false;
} }