Refractoring Stage 6.6 Migrated productivity.js and shortcuts.js mail-state access and render/preferences hooks to the mail feature API.

This commit is contained in:
2026-10-16 21:03:40 +03:00
parent 84a63441ef
commit 38340442f0
21 changed files with 389 additions and 293 deletions
+24 -21
View File
@@ -1,14 +1,19 @@
// Configurable keyboard shortcuts for common mail actions.
const shortcutMailUi = window.epMailFeatures.mail;
const shortcutProductivityUi = window.epMailFeatures.productivity;
const selectShortcutUi = shortcutMailUi.select;
const toastShortcutUi = shortcutMailUi.toast;
const escapeShortcutUi = shortcutMailUi.escapeHtml;
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: 'compose', label: 'Compose', fallback: 'c', run: () => selectShortcutUi('#compose')?.click() },
{ id: 'search', label: 'Search', fallback: '/', run: () => selectShortcutUi('#search')?.focus() },
{ id: 'refresh', label: 'Refresh', fallback: 'r', run: () => selectShortcutUi('#refresh')?.click() },
{ id: 'archive', label: 'Archive message', fallback: 'e', run: () => selectShortcutUi('#reader-archive')?.click() },
{ id: 'delete', label: 'Delete message', fallback: '#', run: () => selectShortcutUi('#reader-delete')?.click() },
{ id: 'star', label: 'Star message', fallback: 's', run: () => selectShortcutUi('#reader-star')?.click() },
{ id: 'reply', label: 'Reply', fallback: 'a', run: () => selectShortcutUi('#reply')?.click() },
{ id: 'forward', label: 'Forward', fallback: 'f', run: () => selectShortcutUi('#forward')?.click() },
{ id: 'next', label: 'Next message', fallback: 'j', run: () => moveMessageSelection(1) },
{ id: 'previous', label: 'Previous message', fallback: 'k', run: () => moveMessageSelection(-1) }
];
@@ -39,16 +44,17 @@ function eventShortcut(event) {
}
function moveMessageSelection(step) {
const messages = shortcutMailUi.state.messages;
if (!messages.length) return;
const index = messages.findIndex(message => message.id === activeMessage);
const index = messages.findIndex(message => message.id === shortcutMailUi.state.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' }); }
if (next) { shortcutMailUi.openMessage(next.id); selectShortcutUi(`.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;
if (target?.closest?.('input, textarea, select, [contenteditable="true"]') || selectShortcutUi('#modal-root')?.children.length) return;
const pressed = eventShortcut(event);
if (!pressed) return;
const shortcuts = loadShortcuts();
@@ -58,18 +64,16 @@ document.addEventListener('keydown', event => {
action.run();
});
if (typeof openPreferences === 'function') {
const baseOpenPreferences = openPreferences;
openPreferences = function () {
shortcutProductivityUi.wrapOpenPreferences(baseOpenPreferences => function () {
baseOpenPreferences();
const fields = $('#modal-root .compose-fields');
const save = $('#save-appearance');
const fields = selectShortcutUi('#modal-root .compose-fields');
const save = selectShortcutUi('#save-appearance');
if (!fields || !save) return;
const shortcuts = loadShortcuts();
const section = document.createElement('div');
section.className = 'shortcut-settings';
section.innerHTML = `<h3>Keyboard shortcuts</h3><p class="account-note">Type a key or combination such as <code>c</code>, <code>Ctrl+Enter</code> or <code>Alt+n</code>. Leave blank to disable. Shortcuts are inactive while typing or in dialogs.</p>${shortcutActions.map(action =>
`<label class="compose-line"><span>${action.label}</span><input class="shortcut-input" data-action="${action.id}" maxlength="24" value="${esc(shortcuts[action.id])}" placeholder="None"></label>`).join('')}<button type="button" class="discard" id="reset-shortcuts">Reset to defaults</button>`;
`<label class="compose-line"><span>${action.label}</span><input class="shortcut-input" data-action="${action.id}" maxlength="24" value="${escapeShortcutUi(shortcuts[action.id])}" placeholder="None"></label>`).join('')}<button type="button" class="discard" id="reset-shortcuts">Reset to defaults</button>`;
fields.append(section);
section.addEventListener('keydown', event => {
const input = event.target.closest('.shortcut-input');
@@ -79,7 +83,7 @@ if (typeof openPreferences === 'function') {
const combo = eventShortcut(event);
if (combo) input.value = combo;
});
$('#reset-shortcuts').onclick = () => {
selectShortcutUi('#reset-shortcuts').onclick = () => {
section.querySelectorAll('.shortcut-input').forEach(input => {
input.value = shortcutActions.find(action => action.id === input.dataset.action).fallback;
});
@@ -91,12 +95,11 @@ if (typeof openPreferences === 'function') {
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.has(value)) { toastShortcutUi(`“${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);
};
};
}
});