From 63ee1467194004efe5df9a62c7476b238e5d4f81 Mon Sep 17 00:00:00 2001 From: Tero Date: Wed, 7 Oct 2026 10:06:19 +0300 Subject: [PATCH] Add configurable keyboard shortcuts Bump version to 0.2.1 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- README.md | 2 +- TODO.md | 2 +- index.html | 1 + package-lock.json | 4 +- package.json | 3 +- scripts/prepare-android-web.cjs | 1 + service-worker.js | 4 +- shortcuts.js | 102 ++++++++++++++++++++++++++++++++ styles.css | 1 + www/index.html | 1 + www/service-worker.js | 4 +- www/shortcuts.js | 102 ++++++++++++++++++++++++++++++++ www/styles.css | 1 + 13 files changed, 219 insertions(+), 9 deletions(-) create mode 100644 shortcuts.js create mode 100644 www/shortcuts.js diff --git a/README.md b/README.md index 7b6fbb3..9962379 100644 --- a/README.md +++ b/README.md @@ -31,4 +31,4 @@ On Windows, `py -m http.server 8000` is also available on many Python installati ## Current scope -Mail supports multiple IMAP/SMTP accounts, switching between them, and saving message attachments. CalDAV and CardDAV servers can be added from the Calendar and Contacts views; credentials are kept in the OS-encrypted credential store, and events and contacts can sync and be edited on the server. Local contacts and events remain available without a DAV connection. Your display name is saved locally and appears in the greeting. Use the **+** beside **Folders** in the sidebar to create IMAP folders on the server; your custom folders appear in the sidebar and can be opened like any other folder. +Mail supports multiple IMAP/SMTP accounts, switching between them, and saving message attachments. CalDAV and CardDAV servers can be added from the Calendar and Contacts views; credentials are kept in the OS-encrypted credential store, and events and contacts can sync and be edited on the server. Local contacts and events remain available without a DAV connection. Your display name is saved locally and appears in the greeting. Use the **+** beside **Folders** in the sidebar to create IMAP folders on the server; your custom folders appear in the sidebar and can be opened like any other folder. Keyboard shortcuts for compose, search, refresh, archive, delete, star, reply, forward, and message navigation can be changed in **Settings**. diff --git a/TODO.md b/TODO.md index cacec31..d8ae615 100644 --- a/TODO.md +++ b/TODO.md @@ -16,7 +16,7 @@ - [x] Cache recent messages and save drafts offline; queue supported sends and message actions for retry when connectivity returns. - [ ] Group related messages into conversations, with controls to expand or collapse threads. - [ ] Add attachment previews and a dedicated view to find and manage attachments across mailboxes. -- [ ] Add configurable keyboard shortcuts for common actions such as compose, archive, reply, and search. +- [x] Add configurable keyboard shortcuts for common actions such as compose, archive, reply, and search. - [x] Warn about suspicious sender addresses and links; require confirmation before opening external links. - [x] Add per-account rules to sort, archive, label, or flag new incoming messages by sender and/or subject. - [x] Add per-account signatures, notification preferences, and sync settings. diff --git a/index.html b/index.html index d3c0a2e..b7cb926 100644 --- a/index.html +++ b/index.html @@ -70,5 +70,6 @@ + diff --git a/package-lock.json b/package-lock.json index 72595f5..96b7fa5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ep-mail", - "version": "0.2.0", + "version": "0.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ep-mail", - "version": "0.2.0", + "version": "0.2.1", "dependencies": { "@capacitor/android": "^8.5.2", "@capacitor/core": "^8.5.2", diff --git a/package.json b/package.json index b398d95..1a469b9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ep-mail", - "version": "0.2.0", + "version": "0.2.1", "private": true, "description": "Eternity Project mail client", "author": "Eternity Project", @@ -41,6 +41,7 @@ "productivity.js", "account-ui.js", "dav-ui.js", + "shortcuts.js", "icon.svg", "logo-ep-daylight.svg", "logo-ep-wasteland.svg", diff --git a/scripts/prepare-android-web.cjs b/scripts/prepare-android-web.cjs index b46511c..19453ef 100644 --- a/scripts/prepare-android-web.cjs +++ b/scripts/prepare-android-web.cjs @@ -13,6 +13,7 @@ const assets = [ 'productivity.js', 'account-ui.js', 'dav-ui.js', + 'shortcuts.js', 'icon.svg', 'logo-ep-daylight.svg', 'logo-ep-wasteland.svg', diff --git a/service-worker.js b/service-worker.js index 243fa0d..c732401 100644 --- a/service-worker.js +++ b/service-worker.js @@ -1,5 +1,5 @@ -const CACHE='ep-mail-v3'; -const FILES=['./','./index.html','./styles.css','./branding.css','./app.js','./platform-bridge.js','./mail-ui.js','./productivity.js','./snooze-ui.js','./account-ui.js','./dav-ui.js','./manifest.webmanifest','./icon.svg','./logo-ep-daylight.svg','./logo-ep-wasteland.svg']; +const CACHE='ep-mail-v4'; +const FILES=['./','./index.html','./styles.css','./branding.css','./app.js','./platform-bridge.js','./mail-ui.js','./productivity.js','./snooze-ui.js','./account-ui.js','./dav-ui.js','./shortcuts.js','./manifest.webmanifest','./icon.svg','./logo-ep-daylight.svg','./logo-ep-wasteland.svg']; self.addEventListener('install',event=>event.waitUntil(caches.open(CACHE).then(cache=>cache.addAll(FILES)))); self.addEventListener('activate',event=>event.waitUntil(caches.keys().then(keys=>Promise.all(keys.filter(k=>k!==CACHE).map(k=>caches.delete(k)))))); self.addEventListener('fetch',event=>{if(event.request.method!=='GET')return;event.respondWith(caches.match(event.request).then(cached=>cached||fetch(event.request).then(response=>{if(new URL(event.request.url).origin===location.origin){const copy=response.clone();caches.open(CACHE).then(cache=>cache.put(event.request,copy))}return response}).catch(()=>caches.match('./index.html'))))}); diff --git a/shortcuts.js b/shortcuts.js new file mode 100644 index 0000000..995c7d6 --- /dev/null +++ b/shortcuts.js @@ -0,0 +1,102 @@ +// 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 = `

Keyboard shortcuts

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.

${shortcutActions.map(action => + ``).join('')}`; + fields.append(section); + section.addEventListener('keydown', event => { + const input = event.target.closest('.shortcut-input'); + if (!input || ['Tab', 'Escape'].includes(event.key)) return; + event.preventDefault(); + if (['Backspace', 'Delete'].includes(event.key)) { input.value = ''; return; } + const combo = eventShortcut(event); + if (combo) input.value = combo; + }); + $('#reset-shortcuts').onclick = () => { + section.querySelectorAll('.shortcut-input').forEach(input => { + input.value = shortcutActions.find(action => action.id === input.dataset.action).fallback; + }); + }; + const baseSave = save.onclick; + save.onclick = event => { + const values = {}; + const used = new Map(); + for (const input of section.querySelectorAll('.shortcut-input')) { + const value = normalizeShortcut(input.value); + const label = shortcutActions.find(action => action.id === input.dataset.action).label; + if (value && used.has(value)) { toast(`“${value}” is used by both ${used.get(value)} and ${label}`); return; } + if (value) used.set(value, label); + values[input.dataset.action] = value; + } + localStorage.setItem(shortcutStorageKey, JSON.stringify(values)); + baseSave?.call(save, event); + }; + }; +} diff --git a/styles.css b/styles.css index 1b93048..8ed52bf 100644 --- a/styles.css +++ b/styles.css @@ -126,3 +126,4 @@ body.ep-daylight .row-check:checked{accent-color:#526a2c} body.ep-daylight .mail-safety-warning{background:#fff6df;color:#5b3c17}body.ep-daylight .mail-link-risky{background:#fff8ea;border-color:#d69b43} .folder-add{border:0;background:transparent;color:#929990;font-size:18px;cursor:pointer}.folder-add:hover{color:#487354}.section-label+.folder-nav:empty{display:none}.custom-folder span:last-child{overflow:hidden;text-overflow:ellipsis;white-space:nowrap} +.shortcut-settings h3{margin:16px 0 4px;font-size:13px}.shortcut-settings code{font-family:monospace}.shortcut-input{text-transform:none}.shortcut-settings #reset-shortcuts{margin-top:8px} diff --git a/www/index.html b/www/index.html index d3c0a2e..b7cb926 100644 --- a/www/index.html +++ b/www/index.html @@ -70,5 +70,6 @@ + diff --git a/www/service-worker.js b/www/service-worker.js index 243fa0d..c732401 100644 --- a/www/service-worker.js +++ b/www/service-worker.js @@ -1,5 +1,5 @@ -const CACHE='ep-mail-v3'; -const FILES=['./','./index.html','./styles.css','./branding.css','./app.js','./platform-bridge.js','./mail-ui.js','./productivity.js','./snooze-ui.js','./account-ui.js','./dav-ui.js','./manifest.webmanifest','./icon.svg','./logo-ep-daylight.svg','./logo-ep-wasteland.svg']; +const CACHE='ep-mail-v4'; +const FILES=['./','./index.html','./styles.css','./branding.css','./app.js','./platform-bridge.js','./mail-ui.js','./productivity.js','./snooze-ui.js','./account-ui.js','./dav-ui.js','./shortcuts.js','./manifest.webmanifest','./icon.svg','./logo-ep-daylight.svg','./logo-ep-wasteland.svg']; self.addEventListener('install',event=>event.waitUntil(caches.open(CACHE).then(cache=>cache.addAll(FILES)))); self.addEventListener('activate',event=>event.waitUntil(caches.keys().then(keys=>Promise.all(keys.filter(k=>k!==CACHE).map(k=>caches.delete(k)))))); self.addEventListener('fetch',event=>{if(event.request.method!=='GET')return;event.respondWith(caches.match(event.request).then(cached=>cached||fetch(event.request).then(response=>{if(new URL(event.request.url).origin===location.origin){const copy=response.clone();caches.open(CACHE).then(cache=>cache.put(event.request,copy))}return response}).catch(()=>caches.match('./index.html'))))}); diff --git a/www/shortcuts.js b/www/shortcuts.js new file mode 100644 index 0000000..995c7d6 --- /dev/null +++ b/www/shortcuts.js @@ -0,0 +1,102 @@ +// 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 = `

Keyboard shortcuts

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.

${shortcutActions.map(action => + ``).join('')}`; + fields.append(section); + section.addEventListener('keydown', event => { + const input = event.target.closest('.shortcut-input'); + if (!input || ['Tab', 'Escape'].includes(event.key)) return; + event.preventDefault(); + if (['Backspace', 'Delete'].includes(event.key)) { input.value = ''; return; } + const combo = eventShortcut(event); + if (combo) input.value = combo; + }); + $('#reset-shortcuts').onclick = () => { + section.querySelectorAll('.shortcut-input').forEach(input => { + input.value = shortcutActions.find(action => action.id === input.dataset.action).fallback; + }); + }; + const baseSave = save.onclick; + save.onclick = event => { + const values = {}; + const used = new Map(); + for (const input of section.querySelectorAll('.shortcut-input')) { + const value = normalizeShortcut(input.value); + const label = shortcutActions.find(action => action.id === input.dataset.action).label; + if (value && used.has(value)) { toast(`“${value}” is used by both ${used.get(value)} and ${label}`); return; } + if (value) used.set(value, label); + values[input.dataset.action] = value; + } + localStorage.setItem(shortcutStorageKey, JSON.stringify(values)); + baseSave?.call(save, event); + }; + }; +} diff --git a/www/styles.css b/www/styles.css index 1b93048..8ed52bf 100644 --- a/www/styles.css +++ b/www/styles.css @@ -126,3 +126,4 @@ body.ep-daylight .row-check:checked{accent-color:#526a2c} body.ep-daylight .mail-safety-warning{background:#fff6df;color:#5b3c17}body.ep-daylight .mail-link-risky{background:#fff8ea;border-color:#d69b43} .folder-add{border:0;background:transparent;color:#929990;font-size:18px;cursor:pointer}.folder-add:hover{color:#487354}.section-label+.folder-nav:empty{display:none}.custom-folder span:last-child{overflow:hidden;text-overflow:ellipsis;white-space:nowrap} +.shortcut-settings h3{margin:16px 0 4px;font-size:13px}.shortcut-settings code{font-family:monospace}.shortcut-input{text-transform:none}.shortcut-settings #reset-shortcuts{margin-top:8px}