-
Notifications
You must be signed in to change notification settings - Fork 152
refactor: typescript defined addShortcit.ts, model.types.ts and plugins #488
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
Conversation
WalkthroughThis PR updates the hotkey binder module by refining the Changes
Sequence Diagram(s)sequenceDiagram
participant U as User/Caller
participant A as addShortcut
participant SH as Shortcut Plugin
U->>A: call addShortcut(keys, action)
A->>A: Determine callback based on action
A->>SH: Register shortcut with callback and options
SH-->>A: Confirm registration
A-->>U: Return void
sequenceDiagram
participant U as User
participant E as Key Event
participant K as KeyCode (Normalizer)
U->>E: Trigger keydown event
E->>K: translate_event(event)
K-->>E: Return standardized key data
E->>K: Process key state (key_down, is_down)
Possibly related PRs
Suggested reviewers
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
✅ Deploy Preview for circuitverse ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (3)
src/simulator/src/hotkey_binder/model/model.types.ts (1)
28-34
: Consider making some ShortcutOptions properties required.Properties like
disable_in_input
andpropagate
seem to have consistent usage patterns inaddShortcut.ts
. Consider making them required if they're always needed.export interface ShortcutOptions { type?: string - propagate?: boolean - disable_in_input?: boolean + propagate: boolean + disable_in_input: boolean target?: Document | string keycode?: number | false }src/simulator/src/hotkey_binder/model/addShortcut.ts (1)
25-91
: Consider using an action map instead of a switch statement.The switch statement, while exhaustive, could be replaced with an object map for better maintainability.
+const actionMap: Record<ActionType, () => void> = { + 'New Circuit': logixFunction.createNewCircuitScope, + 'Save Online': save, + 'Save Offline': saveOffline, + // ... other actions +}; export default function addShortcut(keys: string, action: ActionType): void { - let callback: () => void = () => console.error('No shortcut found..') - - switch (action) { - case 'New Circuit': - callback = logixFunction.createNewCircuitScope - break - // ... other cases - } + const callback = actionMap[action] ?? (() => console.error('No shortcut found..'));src/simulator/src/hotkey_binder/model/normalize/normalizer.plugin.ts (1)
237-244
: Add type guard for better type safety.The
is_down
function could benefit from a type guard to ensure type safety when checking modifiers.+function isModifierKey(code: number): boolean { + return code === KeyCode.CTRL || code === KeyCode.ALT || code === KeyCode.SHIFT; +} is_down: function(key: KeyEvent): boolean { const code = key.code; - if (code == KeyCode.CTRL) return current_keys.ctrl; - if (code == KeyCode.ALT) return current_keys.alt; - if (code == KeyCode.SHIFT) return current_keys.shift; + if (isModifierKey(code)) { + return current_keys[key_names[code].toLowerCase() as keyof CurrentKeys]; + } return current_keys.codes[code] !== undefined && same_modifiers(key, current_keys); },
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
src/simulator/src/hotkey_binder/model/addShortcut.ts
(2 hunks)src/simulator/src/hotkey_binder/model/model.types.ts
(1 hunks)src/simulator/src/hotkey_binder/model/normalize/normalizer.plugin.js
(0 hunks)src/simulator/src/hotkey_binder/model/normalize/normalizer.plugin.ts
(1 hunks)
💤 Files with no reviewable changes (1)
- src/simulator/src/hotkey_binder/model/normalize/normalizer.plugin.js
🔇 Additional comments (3)
src/simulator/src/hotkey_binder/model/model.types.ts (1)
4-26
: LGTM! Well-structured union type for actions.The
ActionType
union type provides a comprehensive and type-safe way to define valid shortcut actions. The string literals are descriptive and follow a consistent naming pattern.src/simulator/src/hotkey_binder/model/addShortcut.ts (1)
21-23
: LGTM! Well-typed function signature with proper error handling.The function signature is properly typed with TypeScript, and the default error callback is a good practice.
src/simulator/src/hotkey_binder/model/normalize/normalizer.plugin.ts (1)
1-14
: LGTM! Well-structured KeyCodeType interface.The interface provides a comprehensive type-safe API for key code operations.
const is_gecko = navigator.userAgent.indexOf('Gecko') != -1; | ||
const is_ie = navigator.userAgent.indexOf('MSIE') != -1; | ||
const is_windows = navigator.platform.indexOf('Win') != -1; | ||
const is_opera = window.opera && (window.opera as any).version() < 9.5; | ||
const is_konqueror = navigator.vendor && navigator.vendor.indexOf('KDE') != -1; | ||
const is_icab = navigator.vendor && navigator.vendor.indexOf('iCab') != -1; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Modernize browser detection logic.
The current browser detection approach using userAgent
and vendor
is outdated. Consider using feature detection instead.
-const is_gecko = navigator.userAgent.indexOf('Gecko') != -1;
-const is_ie = navigator.userAgent.indexOf('MSIE') != -1;
-const is_windows = navigator.platform.indexOf('Win') != -1;
-const is_opera = window.opera && (window.opera as any).version() < 9.5;
-const is_konqueror = navigator.vendor && navigator.vendor.indexOf('KDE') != -1;
-const is_icab = navigator.vendor && navigator.vendor.indexOf('iCab') != -1;
+const getKeyboardAPI = () => {
+ if ('KeyboardEvent' in window && 'code' in KeyboardEvent.prototype) {
+ return 'modern';
+ }
+ return 'legacy';
+};
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const is_gecko = navigator.userAgent.indexOf('Gecko') != -1; | |
const is_ie = navigator.userAgent.indexOf('MSIE') != -1; | |
const is_windows = navigator.platform.indexOf('Win') != -1; | |
const is_opera = window.opera && (window.opera as any).version() < 9.5; | |
const is_konqueror = navigator.vendor && navigator.vendor.indexOf('KDE') != -1; | |
const is_icab = navigator.vendor && navigator.vendor.indexOf('iCab') != -1; | |
const getKeyboardAPI = () => { | |
if ('KeyboardEvent' in window && 'code' in KeyboardEvent.prototype) { | |
return 'modern'; | |
} | |
return 'legacy'; | |
}; |
translate_event: function(e: KeyboardEvent): KeyEvent { | ||
const code = e.which || e.keyCode; | ||
return { | ||
code: KeyCode.translate_key_code(code), | ||
shift: e.shiftKey, | ||
alt: e.altKey, | ||
ctrl: e.ctrlKey, | ||
meta: e.metaKey, | ||
}; | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using KeyboardEvent.code for more reliable key identification.
The current implementation uses which
(deprecated) or keyCode
. Consider using the more modern code
property.
translate_event: function(e: KeyboardEvent): KeyEvent {
- const code = e.which || e.keyCode;
+ const code = e.code ? KeyCode.translate_key_code(e.code.charCodeAt(0)) : e.keyCode;
return {
code: KeyCode.translate_key_code(code),
shift: e.shiftKey,
alt: e.altKey,
ctrl: e.ctrlKey,
meta: e.metaKey,
};
},
Committable suggestion skipped: line range outside the PR's diff.
@vedant-jain03 please review my changes. |
#424 and #458 |
Hey @ThatDeparted2061 I'm sorry for the misunderstanding, I was not aware that you have worked on this. I'm closing this PR. |
Describe the changes you have made in this PR -
Files Affected:
Summary by CodeRabbit