// Configurable keyboard shortcuts for common mail actions. const shortcutStorageKey = 'daybreak-shortcuts'; const shortcutActions = [ { id: 'compose', label: 'Compose', fallback: 'c', run: () => $('#compose')?.click() }, { id: 'search', label: 'Search', fallback: '/', run: () => $('#search')?.focus() }, { id: 'refresh', label: 'Refresh', fallback: 'r', run: () => $('#refresh')?.click() }, { id: 'archive', label: 'Archive message', fallback: 'e', run: () => $('#reader-archive')?.click() }, { id: 'delete', label: 'Delete message', fallback: '#', run: () => $('#reader-delete')?.click() }, { id: 'star', label: 'Star message', fallback: 's', run: () => $('#reader-star')?.click() }, { id: 'reply', label: 'Reply', fallback: 'a', run: () => $('#reply')?.click() }, { id: 'forward', label: 'Forward', fallback: 'f', run: () => $('#forward')?.click() }, { id: 'next', label: 'Next message', fallback: 'j', run: () => moveMessageSelection(1) }, { id: 'previous', label: 'Previous message', fallback: 'k', run: () => moveMessageSelection(-1) } ]; function normalizeShortcut(value) { const text = String(value || '').trim(); if (!text) return ''; const parts = text.split('+').map(part => part.trim()).filter(Boolean); const key = parts.pop() || '+'; const mods = ['Ctrl', 'Alt', 'Shift'].filter(mod => parts.some(part => part.toLowerCase() === mod.toLowerCase())); return [...mods, key.length === 1 ? key.toLowerCase() : key].join('+'); } function loadShortcuts() { let saved = {}; try { saved = JSON.parse(localStorage.getItem(shortcutStorageKey)) || {}; } catch {} return Object.fromEntries(shortcutActions.map(action => [ action.id, typeof saved[action.id] === 'string' ? normalizeShortcut(saved[action.id]) : action.fallback ])); } function eventShortcut(event) { if (['Control', 'Alt', 'Shift', 'Meta'].includes(event.key)) return ''; const mods = [event.ctrlKey || event.metaKey ? 'Ctrl' : '', event.altKey ? 'Alt' : '']; const printable = event.key.length === 1; if (event.shiftKey && !printable) mods.push('Shift'); return normalizeShortcut([...mods.filter(Boolean), event.key === ' ' ? 'Space' : event.key].join('+')); } function moveMessageSelection(step) { if (!messages.length) return; const index = messages.findIndex(message => message.id === activeMessage); const next = messages[Math.min(messages.length - 1, Math.max(0, index + step))]; if (next) { openMessage(next.id); $(`.message[data-id="${next.id}"]`)?.scrollIntoView({ block: 'nearest' }); } } document.addEventListener('keydown', event => { if (event.defaultPrevented || event.isComposing || event.key === 'Escape') return; const target = event.target; if (target?.closest?.('input, textarea, select, [contenteditable="true"]') || $('#modal-root')?.children.length) return; const pressed = eventShortcut(event); if (!pressed) return; const shortcuts = loadShortcuts(); const action = shortcutActions.find(item => shortcuts[item.id] && shortcuts[item.id] === pressed); if (!action) return; event.preventDefault(); action.run(); }); if (typeof openPreferences === 'function') { const baseOpenPreferences = openPreferences; openPreferences = function () { baseOpenPreferences(); const fields = $('#modal-root .compose-fields'); const save = $('#save-appearance'); if (!fields || !save) return; const shortcuts = loadShortcuts(); const section = document.createElement('div'); section.className = 'shortcut-settings'; section.innerHTML = `
Type a key or combination such as c, Ctrl+Enter or Alt+n. Leave blank to disable. Shortcuts are inactive while typing or in dialogs.