### Refactor keybindings to a component and replace deprecated detection method
- Extracted platform detection to its own reusable utility module - Eliminates deprecated Navigator.platform API usage; uses the User-Agent Client Hints API as the primary method when available - Handles iOS and Android devices correctlychore-refactor-javascript-to-modules
parent
fc21bc6c6d
commit
21c0d1ddc7
|
@ -1,42 +1,52 @@
|
|||
// Dynamically update keybindings or hotkeys
|
||||
function getPlatform() {
|
||||
if (/Mac/.test(navigator.platform)) {
|
||||
return "osx"
|
||||
} else if (/Win/.test(navigator.platform)) {
|
||||
return "win"
|
||||
} else if (/Linux/.test(navigator.platform)) {
|
||||
return "linux"
|
||||
} else {
|
||||
return "other"
|
||||
}
|
||||
}
|
||||
import { getPlatform } from './utils/user-agent-platform.js';
|
||||
|
||||
/**
|
||||
* Adds OS-specific class to component
|
||||
* @param {string} osClass - OS-specific class to add
|
||||
* @param {Object} options - Component options
|
||||
* @param {jQuery} options.$component - jQuery element reference
|
||||
*/
|
||||
function addOSClass(osClass, { $component }) {
|
||||
$component.addClass(osClass)
|
||||
$component.addClass(osClass);
|
||||
}
|
||||
|
||||
function updateKeyBindings({ $component }) {
|
||||
var osx = $component.data("osx")
|
||||
var linux = $component.data("linux")
|
||||
var win = $component.data("win")
|
||||
/**
|
||||
* Updates keybinding display based on detected platform
|
||||
* @param {Object} options - Component options
|
||||
* @param {jQuery} options.$component - jQuery element reference
|
||||
* @param {string} options.platform - Detected platform
|
||||
*/
|
||||
function updateKeyBindings({ $component, platform }) {
|
||||
const osx = $component.data('osx');
|
||||
const linux = $component.data('linux');
|
||||
const win = $component.data('win');
|
||||
|
||||
let keybind;
|
||||
|
||||
if (platform === "other") {
|
||||
if (win != linux) {
|
||||
var keybind = '<code class="osx">' + osx + '</code> for macOS, <code>' + linux + '</code> for Linux, and <code>' + win + '</code> for Windows';
|
||||
} else {
|
||||
var keybind = '<code>' + linux + '</code> for Linux and Windows and <code class="osx">' + osx + '</code> for macOS';
|
||||
}
|
||||
if (platform === 'other') {
|
||||
if (win !== linux) {
|
||||
keybind = `<code class="osx">${osx}</code> for macOS, <code>${linux}</code> for Linux, and <code>${win}</code> for Windows`;
|
||||
} else {
|
||||
var keybind = '<code>' + $component.data(platform) + '</code>'
|
||||
keybind = `<code>${linux}</code> for Linux and Windows and <code class="osx">${osx}</code> for macOS`;
|
||||
}
|
||||
} else {
|
||||
keybind = `<code>${$component.data(platform)}</code>`;
|
||||
}
|
||||
|
||||
$component.html(keybind)
|
||||
$component.html(keybind);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize and render platform-specific keybindings
|
||||
* @param {Object} options - Component options
|
||||
* @param {HTMLElement} options.component - DOM element
|
||||
* @returns {void}
|
||||
*/
|
||||
export default function KeyBinding({ component }) {
|
||||
// Initialize keybindings
|
||||
const platform = getPlatform();
|
||||
const $component = $(component);
|
||||
|
||||
addOSClass(platform, { $component });
|
||||
updateKeyBindings({ $component });
|
||||
}
|
||||
updateKeyBindings({ $component, platform });
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
* Platform detection utility functions
|
||||
* Provides methods for detecting user's operating system
|
||||
*/
|
||||
|
||||
/**
|
||||
* Detects user's operating system using modern techniques
|
||||
* Falls back to userAgent parsing when newer APIs aren't available
|
||||
* @returns {string} Operating system identifier ("osx", "win", "linux", or "other")
|
||||
*/
|
||||
export function getPlatform() {
|
||||
// Try to use modern User-Agent Client Hints API first (Chrome 89+, Edge 89+)
|
||||
if (navigator.userAgentData && navigator.userAgentData.platform) {
|
||||
const platform = navigator.userAgentData.platform.toLowerCase();
|
||||
|
||||
if (platform.includes('mac')) return 'osx';
|
||||
if (platform.includes('win')) return 'win';
|
||||
if (platform.includes('linux')) return 'linux';
|
||||
}
|
||||
|
||||
// Fall back to userAgent string parsing
|
||||
const userAgent = navigator.userAgent.toLowerCase();
|
||||
|
||||
if (userAgent.includes('mac') || userAgent.includes('iphone') || userAgent.includes('ipad')) return 'osx';
|
||||
if (userAgent.includes('win')) return 'win';
|
||||
if (userAgent.includes('linux') || userAgent.includes('android')) return 'linux';
|
||||
|
||||
return 'other';
|
||||
}
|
Loading…
Reference in New Issue