From 710653808064ded1378488dfcc413c96cac1dd80 Mon Sep 17 00:00:00 2001 From: Harsh Rao Date: Thu, 23 Jan 2025 23:29:38 +0530 Subject: [PATCH 1/9] add and remove --- .../src/hotkey_binder/model/utils.js | 67 ------------------- .../src/hotkey_binder/model/utils.ts | 59 ++++++++++++++++ 2 files changed, 59 insertions(+), 67 deletions(-) delete mode 100644 src/simulator/src/hotkey_binder/model/utils.js create mode 100644 src/simulator/src/hotkey_binder/model/utils.ts 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..653dabc0 --- /dev/null +++ b/src/simulator/src/hotkey_binder/model/utils.ts @@ -0,0 +1,59 @@ +// Extend Storage interface to add type-safe set and get methods +interface Storage { + set(key: string, obj: any): void; + get(key: string): any; +} + +// Add type-safe set method to Storage prototype +Storage.prototype.set = function(key: string, obj: any): void { + this.setItem(key, JSON.stringify(obj)); +} + +// Add type-safe get method to Storage prototype +Storage.prototype.get = function(key: string): any { + const item = this.getItem(key); + return item ? JSON.parse(item) : 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 => obj[key] === val); +} + +// Detect operating system +export function getOS(): string { + const userAgent = navigator.appVersion.toLowerCase(); + if (userAgent.includes('win')) return 'Windows'; + if (userAgent.includes('mac')) return 'MacOS'; + if (userAgent.includes('x11')) return 'UNIX'; + if (userAgent.includes('linux')) return 'Linux'; + 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 From f792a04c277daaf869ac0ad8fc63372ae2a6ea82 Mon Sep 17 00:00:00 2001 From: Harsh Rao Date: Mon, 27 Jan 2025 01:30:39 +0530 Subject: [PATCH 2/9] resolve --- src/simulator/src/hotkey_binder/model/utils.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/simulator/src/hotkey_binder/model/utils.ts b/src/simulator/src/hotkey_binder/model/utils.ts index 653dabc0..e4a06365 100644 --- a/src/simulator/src/hotkey_binder/model/utils.ts +++ b/src/simulator/src/hotkey_binder/model/utils.ts @@ -27,11 +27,19 @@ export function getKey>(obj: T, val: any): string // Detect operating system export function getOS(): string { - const userAgent = navigator.appVersion.toLowerCase(); - if (userAgent.includes('win')) return 'Windows'; - if (userAgent.includes('mac')) return 'MacOS'; - if (userAgent.includes('x11')) return 'UNIX'; - if (userAgent.includes('linux')) return 'Linux'; + // Use modern API with fallback + if (navigator.platform) { + const platform = navigator.platform.toLowerCase(); + if (platform.includes('windows')) return 'Windows'; + if (platform.includes('mac')) return 'MacOS'; + if (platform.includes('linux')) return 'Linux'; + } + // Fallback to platform + const platform = navigator.platform.toLowerCase(); + if (navigator.userAgent.includes('win')) return 'Windows'; + if (navigator.userAgent.includes('mac')) return 'MacOS'; + if (navigator.userAgent.includes('x11')) return 'UNIX'; + if (navigator.userAgent.includes('linux')) return 'Linux'; return ''; } From eb4ab5e06ad1a62c7791c230a9039e63a991d156 Mon Sep 17 00:00:00 2001 From: Harsh Rao Date: Mon, 27 Jan 2025 01:31:24 +0530 Subject: [PATCH 3/9] resolve --- src/simulator/src/hotkey_binder/model/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/simulator/src/hotkey_binder/model/utils.ts b/src/simulator/src/hotkey_binder/model/utils.ts index e4a06365..1449ba20 100644 --- a/src/simulator/src/hotkey_binder/model/utils.ts +++ b/src/simulator/src/hotkey_binder/model/utils.ts @@ -1,7 +1,7 @@ // Extend Storage interface to add type-safe set and get methods interface Storage { - set(key: string, obj: any): void; - get(key: string): any; + set(key: string, obj: T): void; + get(key: string): T | null; } // Add type-safe set method to Storage prototype From b68493506b58b72390cd1bd58921c7b8f6778dea Mon Sep 17 00:00:00 2001 From: Harsh Rao Date: Mon, 27 Jan 2025 01:34:17 +0530 Subject: [PATCH 4/9] resolve --- src/simulator/src/hotkey_binder/model/utils.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/simulator/src/hotkey_binder/model/utils.ts b/src/simulator/src/hotkey_binder/model/utils.ts index 1449ba20..99cba5ab 100644 --- a/src/simulator/src/hotkey_binder/model/utils.ts +++ b/src/simulator/src/hotkey_binder/model/utils.ts @@ -5,14 +5,21 @@ interface Storage { } // Add type-safe set method to Storage prototype -Storage.prototype.set = function(key: string, obj: any): void { +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): any { +Storage.prototype.get = function(key: string): T | null { const item = this.getItem(key); - return item ? JSON.parse(item) : null; + 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 From 0382e395feaa8d71382007c49eee2f5450d59c65 Mon Sep 17 00:00:00 2001 From: Harsh Rao Date: Tue, 28 Jan 2025 00:28:44 +0530 Subject: [PATCH 5/9] resolve --- .../src/hotkey_binder/model/utils.ts | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/simulator/src/hotkey_binder/model/utils.ts b/src/simulator/src/hotkey_binder/model/utils.ts index 99cba5ab..f02028d5 100644 --- a/src/simulator/src/hotkey_binder/model/utils.ts +++ b/src/simulator/src/hotkey_binder/model/utils.ts @@ -32,21 +32,34 @@ export function getKey>(obj: T, val: any): string return Object.keys(obj).find(key => obj[key] === val); } + // Detect operating system export function getOS(): string { - // Use modern API with fallback - if (navigator.platform) { - const platform = navigator.platform.toLowerCase(); - if (platform.includes('windows')) return 'Windows'; - if (platform.includes('mac')) return 'MacOS'; - if (platform.includes('linux')) return 'Linux'; - } - // Fallback to platform const platform = navigator.platform.toLowerCase(); - if (navigator.userAgent.includes('win')) return 'Windows'; - if (navigator.userAgent.includes('mac')) return 'MacOS'; - if (navigator.userAgent.includes('x11')) return 'UNIX'; - if (navigator.userAgent.includes('linux')) return 'Linux'; + const userAgent = navigator.userAgent.toLowerCase(); + + const osMap: Record = { + windows: 'Windows', + mac: 'MacOS', + linux: 'Linux', + x11: 'UNIX', + }; + + // Check platform first + for (const [key, value] of Object.entries(osMap)) { + if (platform.includes(key)) { + return value; + } + } + + // Fallback to user agent + for (const [key, value] of Object.entries(osMap)) { + if (userAgent.includes(key)) { + return value; + } + } + + // Default return if no match is found return ''; } From 33b8d311a7b048a7b64f59bc9e37939863e8de2e Mon Sep 17 00:00:00 2001 From: Harsh Rao Date: Tue, 28 Jan 2025 00:38:22 +0530 Subject: [PATCH 6/9] resolve --- .../src/hotkey_binder/model/utils.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/simulator/src/hotkey_binder/model/utils.ts b/src/simulator/src/hotkey_binder/model/utils.ts index f02028d5..5117f1a3 100644 --- a/src/simulator/src/hotkey_binder/model/utils.ts +++ b/src/simulator/src/hotkey_binder/model/utils.ts @@ -45,24 +45,31 @@ export function getOS(): string { x11: 'UNIX', }; - // Check platform first - for (const [key, value] of Object.entries(osMap)) { - if (platform.includes(key)) { - return value; + // Helper function to find OS based on a string (platform or userAgent) + const findOS = (str: string): string | undefined => { + for (const [key, value] of Object.entries(osMap)) { + if (str.includes(key)) { + return value; + } } + return undefined; + }; + + // Check platform first + const osFromPlatform = findOS(platform); + if (osFromPlatform) { + return osFromPlatform; } // Fallback to user agent - for (const [key, value] of Object.entries(osMap)) { - if (userAgent.includes(key)) { - return value; - } + const osFromUserAgent = findOS(userAgent); + if (osFromUserAgent) { + return osFromUserAgent; } // Default return if no match is found return ''; } - // Check for restricted key combinations export function checkRestricted(key: string): boolean { const restrictedKeys: string[] = [ From 26dd49c6ec7f1a7798cfc45066e639ec62fc7c00 Mon Sep 17 00:00:00 2001 From: Harsh Rao Date: Tue, 28 Jan 2025 00:53:38 +0530 Subject: [PATCH 7/9] reslove --- .../src/hotkey_binder/model/utils.ts | 45 ++++++------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/src/simulator/src/hotkey_binder/model/utils.ts b/src/simulator/src/hotkey_binder/model/utils.ts index 5117f1a3..ec1ced2b 100644 --- a/src/simulator/src/hotkey_binder/model/utils.ts +++ b/src/simulator/src/hotkey_binder/model/utils.ts @@ -33,41 +33,22 @@ export function getKey>(obj: T, val: any): string } -// Detect operating system +// OS detection patterns +const OS_PATTERNS: Record = { + 'Windows': /windows/i, + 'MacOS': /mac/i, + 'Linux': /linux/i, + 'UNIX': /x11/i +}; export function getOS(): string { - const platform = navigator.platform.toLowerCase(); - const userAgent = navigator.userAgent.toLowerCase(); - - const osMap: Record = { - windows: 'Windows', - mac: 'MacOS', - linux: 'Linux', - x11: 'UNIX', - }; - - // Helper function to find OS based on a string (platform or userAgent) - const findOS = (str: string): string | undefined => { - for (const [key, value] of Object.entries(osMap)) { - if (str.includes(key)) { - return value; - } + const userInput = `${navigator.platform} ${navigator.userAgent}`.toLowerCase(); + + for (const [os, pattern] of Object.entries(OS_PATTERNS)) { + if (pattern.test(userInput)) { + return os; } - return undefined; - }; - - // Check platform first - const osFromPlatform = findOS(platform); - if (osFromPlatform) { - return osFromPlatform; } - - // Fallback to user agent - const osFromUserAgent = findOS(userAgent); - if (osFromUserAgent) { - return osFromUserAgent; - } - - // Default return if no match is found + return ''; } // Check for restricted key combinations From 08cdb078a5934c719beb59febe9ad6fb7c857633 Mon Sep 17 00:00:00 2001 From: Harsh Rao Date: Tue, 28 Jan 2025 00:54:36 +0530 Subject: [PATCH 8/9] reslove --- src/simulator/src/hotkey_binder/model/utils.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/simulator/src/hotkey_binder/model/utils.ts b/src/simulator/src/hotkey_binder/model/utils.ts index ec1ced2b..cf30b77b 100644 --- a/src/simulator/src/hotkey_binder/model/utils.ts +++ b/src/simulator/src/hotkey_binder/model/utils.ts @@ -29,7 +29,13 @@ export function objectSize(obj: Record): number { // Find key by value in an object export function getKey>(obj: T, val: any): string | undefined { - return Object.keys(obj).find(key => obj[key] === val); + 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; + }); } From 22edf3e4e9df5162e03e1511c780d9183d0db012 Mon Sep 17 00:00:00 2001 From: Harsh Rao Date: Tue, 28 Jan 2025 00:56:59 +0530 Subject: [PATCH 9/9] reslove see --- src/simulator/src/hotkey_binder/model/utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/simulator/src/hotkey_binder/model/utils.ts b/src/simulator/src/hotkey_binder/model/utils.ts index cf30b77b..23a833fa 100644 --- a/src/simulator/src/hotkey_binder/model/utils.ts +++ b/src/simulator/src/hotkey_binder/model/utils.ts @@ -38,7 +38,6 @@ export function getKey>(obj: T, val: any): string }); } - // OS detection patterns const OS_PATTERNS: Record = { 'Windows': /windows/i,