58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
/**
|
|
* 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
|
|
};
|
|
}
|