diff --git a/src/simulator/src/hotkey_binder/model/utils.js b/src/simulator/src/hotkey_binder/model/utils.js deleted file mode 100644 index 8a2a8a8b..00000000 --- a/src/simulator/src/hotkey_binder/model/utils.js +++ /dev/null @@ -1,67 +0,0 @@ -Storage.prototype.set = function (key, obj) { - return this.setItem(key, JSON.stringify(obj)) -} - -Storage.prototype.get = function (key) { - return JSON.parse(this.getItem(key)) -} - -Object.size = function (obj) { - var size = 0, - key - for (key in obj) { - if (obj.hasOwnProperty(key)) size++ - } - return size -} - -export const getKey = (obj, val) => - Object.keys(obj).find((key) => obj[key] === val) - -export const getOS = () => { - let OSName = '' - if (navigator.appVersion.indexOf('Win') != -1) OSName = 'Windows' - if (navigator.appVersion.indexOf('Mac') != -1) OSName = 'MacOS' - if (navigator.appVersion.indexOf('X11') != -1) OSName = 'UNIX' - if (navigator.appVersion.indexOf('Linux') != -1) OSName = 'Linux' - return OSName -} - -export const checkRestricted = (key) => { - const restrictedKeys = [ - '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', - ] - if (getOS == 'macOS') { - restrictedKeys.forEach((value, i) => { - if (value.split(' + ')[0] == 'Ctrl'); - restrictedKeys[i] = - value.split(' + ')[0] == 'Ctrl' - ? value.replace('Ctrl', 'Meta') - : value - }) - } - return restrictedKeys.includes(key) -} diff --git a/src/simulator/src/hotkey_binder/model/utils.ts b/src/simulator/src/hotkey_binder/model/utils.ts new file mode 100644 index 00000000..23a833fa --- /dev/null +++ b/src/simulator/src/hotkey_binder/model/utils.ts @@ -0,0 +1,80 @@ +// Extend Storage interface to add type-safe set and get methods +interface Storage { + set(key: string, obj: T): void; + get(key: string): T | null; +} + +// Add type-safe set method to Storage prototype +Storage.prototype.set = function(key: string, obj: T): void { + this.setItem(key, JSON.stringify(obj)); +} + +// Add type-safe get method to Storage prototype +Storage.prototype.get = function(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): number { + return Object.keys(obj).length; +} + +// Find key by value in an object +export function getKey>(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 = { + '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); +} \ No newline at end of file