Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix selection change callback not called on first pointer move #1339

Merged
merged 1 commit into from
Jan 6, 2023
Merged
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
30 changes: 14 additions & 16 deletions packages/lib/src/interactions/SelectionTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,24 @@ function SelectionTool(props: Props) {
useEffect(() => {
const { selection, isCancelled } = selectionState;

// Selection state is now defined => selection has started
if (!prevSelectionState?.selection && selection) {
onSelectionStartRef.current?.(selection);
return;
}

/* Selection state is now undefined => selection has ended.
* Invoke callback but only if the selection has ended successfully - i.e. if:
* - the selection was not cancelled with Escape,
* - and the modifier key was not released before the pointer. */
if (prevSelectionState?.selection && !selection && !isCancelled) {
onSelectionEndRef.current?.(prevSelectionState.selection);
return;
}
if (selection) {
// Previous selection was undefined and current selection is now defined => selection has started
if (!prevSelectionState?.selection) {
onSelectionStartRef.current?.(selection);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No return here, so onSelectionChange gets called.

}

// Selection remains defined => selection has changed
if (prevSelectionState?.selection && selection) {
// Either way, current selection is defined, so invoke change callback
onSelectionChangeRef.current?.(
isModifierKeyPressed ? selection : undefined // don't pass selection object if user is not pressing modifier key
);

return;
}

/* Previous selection was defined and current selection is now undefined => selection has ended.
* Invoke callback but only if selection wasn't cancelled. */
if (prevSelectionState?.selection && !isCancelled) {
onSelectionEndRef.current?.(prevSelectionState.selection);
}
}, [
prevSelectionState,
Expand Down