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

refactor: JS to TS : src/simulator/src/hotkey_binder/model/utils.js #428

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
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
67 changes: 0 additions & 67 deletions src/simulator/src/hotkey_binder/model/utils.js

This file was deleted.

80 changes: 80 additions & 0 deletions src/simulator/src/hotkey_binder/model/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Extend Storage interface to add type-safe set and get methods
interface Storage {
set<T>(key: string, obj: T): void;
get<T>(key: string): T | null;
}

// Add type-safe set method to Storage prototype
Storage.prototype.set = function<T>(key: string, obj: T): void {
this.setItem(key, JSON.stringify(obj));
}

// Add type-safe get method to Storage prototype
Storage.prototype.get = function<T>(key: string): T | null {
const item = this.getItem(key);
if (!item) return null;
try {
return JSON.parse(item) as T;
} catch (e) {
console.error(`Failed to parse stored item ${key}:`, e);
return null;
}

}

// Type-safe object size function
export function objectSize(obj: Record<string, any>): number {
return Object.keys(obj).length;
}

// Find key by value in an object
export function getKey<T extends Record<string, any>>(obj: T, val: any): string | undefined {
return Object.keys(obj).find(key => {
const value = obj[key];
if (typeof value === 'object' && value !== null) {
return JSON.stringify(value) === JSON.stringify(val);
}
return value === val;
});
}

// OS detection patterns
const OS_PATTERNS: Record<string, RegExp> = {
'Windows': /windows/i,
'MacOS': /mac/i,
'Linux': /linux/i,
'UNIX': /x11/i
};
export function getOS(): string {
const userInput = `${navigator.platform} ${navigator.userAgent}`.toLowerCase();

for (const [os, pattern] of Object.entries(OS_PATTERNS)) {
if (pattern.test(userInput)) {
return os;
}
}

return '';
}
// Check for restricted key combinations
export function checkRestricted(key: string): boolean {
const restrictedKeys: string[] = [
'Ctrl + N', 'Ctrl + W', 'Ctrl + T', 'Ctrl + C', 'Ctrl + V',
'Ctrl + Delete', 'Ctrl + Backspace', 'Ctrl + /', 'Ctrl + \\',
'Ctrl + ]', "Ctrl + '", 'Ctrl + `', 'Ctrl + [', 'Ctrl + ~',
'Ctrl + Num1', 'Ctrl + Num2', 'Ctrl + Num3', 'Ctrl + Num4',
'Ctrl + Num5', 'Ctrl + Num6', 'Ctrl + Num*', 'Ctrl + Num/',
'Ctrl + Num.', 'Ctrl + Num0'
];

// Adjust for MacOS if needed
const modifiedKeys = getOS() === 'MacOS'
? restrictedKeys.map(value =>
value.startsWith('Ctrl')
? value.replace('Ctrl', 'Meta')
: value
)
: restrictedKeys;

return modifiedKeys.includes(key);
}