Skip to content

Conversation

@RajdeepDs
Copy link

Description

Ensures the Notes editor’s link shortcut (ctrl+K / cmd+K) takes priority when focused, preventing the global command menu from triggering inside the editor.

Closes #14949

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Overview

Greptile Summary

This PR fixes a keyboard shortcut conflict where Ctrl+K/Cmd+K was triggering both the global command menu and the Notes editor's link insertion functionality. The fix adds priority handling in the useCommandMenuHotKeys hook by checking if the active element is within an editor context (identified by the .editor CSS class). When inside an editor, the global command menu shortcut is bypassed, allowing the editor's link shortcut to work correctly.

The implementation uses DOM traversal to detect editor context rather than React state management, suggesting the editor might be a third-party component. This ensures proper separation of concerns between global shortcuts and editor-specific functionality.

Important Files Changed

Changed Files
Filename Score Overview
packages/twenty-front/src/modules/command-menu/hooks/useCommandMenuHotKeys.ts 4/5 Added DOM-based editor context detection to prevent command menu shortcuts from interfering with editor link shortcuts

Confidence score: 4/5

  • This PR is safe to merge with low risk - it addresses a specific UX issue without breaking existing functionality
  • Score reflects solid implementation with proper event handling and fallback behavior, though DOM traversal approach could be more robust
  • Pay attention to the DOM traversal logic for editor detection - ensure the .editor class selector remains consistent across editor implementations

Sequence Diagram

sequenceDiagram
    participant User
    participant Browser
    participant useCommandMenuHotKeys
    participant CommandMenu
    participant KeyboardShortcutMenu
    participant RecordsSearch
    participant AISearch
    participant Editor

    User->>Browser: "Press Ctrl+K/Cmd+K"
    Browser->>useCommandMenuHotKeys: "Trigger hotkey callback"
    useCommandMenuHotKeys->>Editor: "Check if active element is in editor"
    
    alt Active element is in editor
        Editor-->>useCommandMenuHotKeys: "Return true (allow default behavior)"
        useCommandMenuHotKeys-->>Browser: "Do not prevent default"
        Browser->>Editor: "Execute editor's link shortcut"
    else Active element is not in editor
        useCommandMenuHotKeys->>Browser: "preventDefault()"
        useCommandMenuHotKeys->>KeyboardShortcutMenu: "closeKeyboardShortcutMenu()"
        useCommandMenuHotKeys->>CommandMenu: "toggleCommandMenu()"
    end

    User->>Browser: "Press /"
    Browser->>useCommandMenuHotKeys: "Trigger hotkey callback"
    useCommandMenuHotKeys->>RecordsSearch: "openRecordsSearchPage()"

    User->>Browser: "Press @"
    Browser->>useCommandMenuHotKeys: "Trigger hotkey callback"
    
    alt AI is enabled
        useCommandMenuHotKeys->>AISearch: "openAskAIPage()"
    else AI is disabled
        useCommandMenuHotKeys-->>Browser: "Do nothing"
    end

    User->>Browser: "Press Escape (in command menu)"
    Browser->>useCommandMenuHotKeys: "Trigger hotkey callback"
    useCommandMenuHotKeys->>CommandMenu: "goBackFromCommandMenu()"

    User->>Browser: "Press Backspace/Delete (in command menu)"
    Browser->>useCommandMenuHotKeys: "Trigger hotkey callback"
    
    alt Search is not empty
        useCommandMenuHotKeys-->>Browser: "Return (do nothing)"
    else Search is empty and on root page
        useCommandMenuHotKeys->>CommandMenu: "setGlobalCommandMenuContext()"
    else Search is empty and not on root page
        useCommandMenuHotKeys->>CommandMenu: "goBackFromCommandMenu()"
    end
Loading

1 file reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

return true;
}

if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'k') {
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Redundant condition check - the keys array already defines 'ctrl+k' and 'meta+k', so this manual key check is unnecessary

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/twenty-front/src/modules/command-menu/hooks/useCommandMenuHotKeys.ts
Line: 55:55

Comment:
**style:** Redundant condition check - the keys array already defines 'ctrl+k' and 'meta+k', so this manual key check is unnecessary

How can I resolve this? If you propose a fix, please make it concise.

const active = document.activeElement;
const isInEditor = active?.closest('.editor');
if (isInEditor !== null) {
return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider returning early with return instead of return true for consistency with other early returns in the codebase

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/twenty-front/src/modules/command-menu/hooks/useCommandMenuHotKeys.ts
Line: 52:52

Comment:
**style:** Consider returning early with `return` instead of `return true` for consistency with other early returns in the codebase

How can I resolve this? If you propose a fix, please make it concise.

@github-actions
Copy link
Contributor

Welcome!

Hello there, congrats on your first PR! We're excited to have you contributing to this project.
By submitting your Pull Request, you acknowledge that you agree with the terms of our Contributor License Agreement.

Generated by 🚫 dangerJS against 2ca59de

Comment on lines +49 to +59
const active = document.activeElement;
const isInEditor = active?.closest('.editor');
if (isInEditor !== null) {
return true;
}

if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'k') {
e.preventDefault();
closeKeyboardShortcutMenu();
toggleCommandMenu();
}

Choose a reason for hiding this comment

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

Potential bug: The useGlobalHotkeys hook prevents the default event action before the callback runs, blocking the editor's native ctrl+k shortcut from triggering.
  • Description: The useGlobalHotkeys hook for the ctrl+k shortcut is called without the preventDefault: false option. The hook's implementation stops the keyboard event's propagation and prevents its default action before the provided callback is executed. The callback logic, which checks if the user is inside an editor, runs too late to allow the event to proceed. As a consequence, the editor's native link insertion shortcut (ctrl+k) will not function, as the event is intercepted and stopped before it can reach the editor.

  • Suggested fix: Pass an options object with preventDefault: false to the useGlobalHotkeys hook. This ensures the callback executes before the event is handled. Then, inside the callback, manually call e.preventDefault() only when the condition to open the command menu is met (i.e., when not focused within the editor).
    severity: 0.65, confidence: 1.0

Did we get this right? 👍 / 👎 to inform future reviews.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 17, 2025

🚀 Preview Environment Ready!

Your preview environment is available at: http://bore.pub:56415

This environment will automatically shut down when the PR is closed or after 5 hours.

@bosiraphael
Copy link
Contributor

Hello @RajdeepDs, thank you for your contribution, but there's a much simpler way to fix this, I've raised a PR if you want to give it a look #15257

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cmd + K conflict in note when adding a link (cmd+k shortcut) and opening right drawer

3 participants