Skip to content

Conversation

@kapetr
Copy link
Contributor

@kapetr kapetr commented Jan 28, 2026

Summary

The PR fixes issue where text selection in canvas didn’t work with keyboard.

Linked Issues

Refs #1874

Documentation

  • No Docs Needed: just a fix

Signed-off-by: Petr Kadlec <petr@puradesign.cz>
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jan 28, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @kapetr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where text selection within the canvas UI was not responsive to keyboard input. By introducing a keyup event listener and a generalized selection processing function, the changes ensure that users can now accurately select text using keyboard shortcuts, significantly improving accessibility and user experience for text manipulation in the canvas.

Highlights

  • Keyboard Selection Fix: Implemented keyboard event handling to correctly process text selections made using arrow keys and the Shift key within the canvas component.
  • Code Refactoring: Refactored the core selection processing logic into a new processSelection function, making it reusable for both mouse and keyboard events.
  • Utility Function: Added a new utility function isArrowKey to efficiently check if a given keyboard event key is an arrow key.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request fixes an issue with keyboard text selection in the canvas. The approach is sound, but I've identified an opportunity to make the implementation more robust and performant. My main feedback is on the handleKeyUp logic, which is currently too broad and could lead to unnecessary function calls. I've suggested a more precise implementation that also handles more selection keys and the 'Select All' shortcut.

Comment on lines +61 to +81
const handleKeyUp = (event: KeyboardEvent) => {
if (event.shiftKey || isArrowKey(event.key)) {
processSelection();
}
};

container.addEventListener('mouseup', handleMouseUp);
document.addEventListener('keyup', handleKeyUp);

return () => {
container.removeEventListener('mouseup', handleMouseUp);
document.removeEventListener('keyup', handleKeyUp);
};
}, [containerRef, onSelectionChange]);
}

const ARROW_KEYS = ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'];

export function isArrowKey(key: string): boolean {
return ARROW_KEYS.includes(key);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The current logic for handling keyboard selection can be improved for robustness and performance.

  1. The condition event.shiftKey is too broad. It will trigger processSelection for any key pressed with Shift (e.g., typing uppercase letters), causing unnecessary processing.
  2. isArrowKey is too specific and doesn't handle other common selection keys like Home, End, PageUp, and PageDown. Pressing these keys should also update the selection state (e.g., to clear it when pressed without Shift).

I suggest expanding the list of keys to include all navigation keys and refining the handleKeyUp logic to be more precise. This will make the code more performant, readable, and will handle more edge cases correctly. I've also included handling for the 'Select All' keyboard shortcut (Ctrl+A or Cmd+A).

    const handleKeyUp = (event: KeyboardEvent) => {
      const isSelectAll = (event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'a';
      if (isSelectionKey(event.key) || isSelectAll) {
        processSelection();
      }
    };

    container.addEventListener('mouseup', handleMouseUp);
    document.addEventListener('keyup', handleKeyUp);

    return () => {
      container.removeEventListener('mouseup', handleMouseUp);
      document.removeEventListener('keyup', handleKeyUp);
    };
  }, [containerRef, onSelectionChange]);
}

const SELECTION_KEYS = ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Home', 'End', 'PageUp', 'PageDown'];

export function isSelectionKey(key: string): boolean {
  return SELECTION_KEYS.includes(key);
}

@JanPokorny
Copy link
Collaborator

JanPokorny commented Jan 28, 2026

I wonder if you could simplify this through https://developer.mozilla.org/en-US/docs/Web/API/Document/selectionchange_event ? Gemini is right that by this approach you would still miss non-arrow keyboard navigation.

@kapetr
Copy link
Contributor Author

kapetr commented Jan 29, 2026

I wonder if you could simplify this through https://developer.mozilla.org/en-US/docs/Web/API/Document/selectionchange_event ? Gemini is right that by this approach you would still miss non-arrow keyboard navigation.

@JanPokorny That's where I started :) I abandoned the selectionchange event, because it gets triggered on every change, meaning it can happen 10, or 100 times when selecting text with mouse (or holding down arrow key). As opposed to the current solution that gets triggered only once, when the selection is done.
It could be optimized and throttled of course and the callback should be quite cheap in execution, so that's not that big of the issue, but it opens the tooltip immediately, when selection starts and keeps it opening through the whole process of selecting text, which I didn't like. I want to have it opened, when a selection is done.
But I agree using selectionchange would be more robust solution, I can imagine, it would react on other means of user input (speech?:)). And of course gemini is right, I'll improve it, if I stick to this solution.

}, [containerRef, onSelectionChange]);
}

const ARROW_KEYS = ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'];
Copy link
Contributor

@PetrBulanek PetrBulanek Jan 29, 2026

Choose a reason for hiding this comment

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

Not any issue, but you can use keycode-js that we have as a dependency (see apps/agentstack-ui/src/utils/form-utils.ts). :-)

@JanPokorny
Copy link
Collaborator

@kapetr Did you try debouncing it, with e.g. https://lodash.com/docs/#debounce ?

@kapetr
Copy link
Contributor Author

kapetr commented Jan 29, 2026

@JanPokorny I considered that, but that doesn't change the fact, that the tooltip with controls would pop up even if you're not done selecting (unless you're super fast). But I might have unnecessarily locked myself into the idea of showing the tooltip only after selection is finished.
I'll give it a shot with various debounce timeouts and see how the ui feels. I guess having more robust solution should take precedence over slightly nicer ui feedback :)

@JanPokorny
Copy link
Collaborator

@kapetr Maybe combine the approaches and avoid showing the tooltip when left click is held down, that avoids accidentally getting in the way of the user doing a mouse selection, and with keyboard selection it would just pop up when you're not pressing the keys fast enough 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants