🎨 Improve code readability by reformatting functions and cleaning up whitespace
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Keyboard Shortcuts Composable
|
||||
* Manages keyboard event listeners and shortcuts
|
||||
*/
|
||||
|
||||
import { onMounted, onUnmounted } from 'vue';
|
||||
|
||||
/**
|
||||
* Register keyboard shortcuts
|
||||
* @param {Object} shortcuts - Map of key combinations to handlers
|
||||
* @returns {Object} Control functions
|
||||
*
|
||||
* Example shortcuts object:
|
||||
* {
|
||||
* 'ctrl+f': () => focusSearch(),
|
||||
* 'ctrl+c': () => copySelected(),
|
||||
* 'escape': () => clearSelection()
|
||||
* }
|
||||
*/
|
||||
export function useKeyboardShortcuts(shortcuts = {}) {
|
||||
const handleKeyDown = (event) => {
|
||||
const key = event.key.toLowerCase();
|
||||
const ctrl = event.ctrlKey || event.metaKey; // Support both Ctrl and Cmd
|
||||
const shift = event.shiftKey;
|
||||
const alt = event.altKey;
|
||||
|
||||
// Build combination string
|
||||
let combination = '';
|
||||
if (ctrl) combination += 'ctrl+';
|
||||
if (shift) combination += 'shift+';
|
||||
if (alt) combination += 'alt+';
|
||||
combination += key;
|
||||
|
||||
// Also check without modifiers
|
||||
const simpleKey = key;
|
||||
|
||||
// Try to find and execute handler
|
||||
const handler = shortcuts[combination] || shortcuts[simpleKey];
|
||||
|
||||
if (handler) {
|
||||
event.preventDefault();
|
||||
handler(event);
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
window.addEventListener('keydown', handleKeyDown);
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener('keydown', handleKeyDown);
|
||||
});
|
||||
|
||||
return {
|
||||
// Can add control functions here if needed
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user