Skip to content

Commit

Permalink
refactor: JS to TS : src/simulator/src/hotkey_binder/model/utils.js (#…
Browse files Browse the repository at this point in the history
…456)

* add and remove

* resolve

* resolve

* resolve

* resolve

* resolve

* reslove

* reslove

* reslove see

* resolve

* resolve
  • Loading branch information
ThatDeparted2061 authored Feb 7, 2025
1 parent a1218c3 commit 09f75e0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 67 deletions.
67 changes: 0 additions & 67 deletions src/simulator/src/hotkey_binder/model/utils.js

This file was deleted.

74 changes: 74 additions & 0 deletions src/simulator/src/hotkey_binder/model/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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);
}

0 comments on commit 09f75e0

Please sign in to comment.