// Command palette (Ctrl/Cmd-K) and quick capture. Uses the app's globals (allowed, TABS, icon, jfetch, openSheet, store, toast ...). /** "buy milk tomorrow" -> { title: 'buy milk', due: '2026-10-23' }. Understands today, tomorrow, a weekday name, "in N days" and YYYY-MM-DD at the end. */ function parseQuickTask(text) { let t = String(text || '').trim(), due = ''; const d0 = new Date(); d0.setHours(12, 0, 0, 0); const fmt = d => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`; const days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']; let m; if ((m = /\s+(\d{4}-\d{2}-\d{2})$/.exec(t))) { due = m[1]; t = t.slice(0, m.index); } else if ((m = /\s+in (\d{1,3}) days?$/i.exec(t))) { const d = new Date(d0); d.setDate(d.getDate() + +m[1]); due = fmt(d); t = t.slice(0, m.index); } else if ((m = /\s+(today|tomorrow|tonight)$/i.exec(t))) { const d = new Date(d0); if (/tomorrow/i.test(m[1])) d.setDate(d.getDate() + 1); due = fmt(d); t = t.slice(0, m.index); } else if ((m = /\s+(?:on |next )?(sun|mon|tue|wed|thu|fri|sat)[a-z]*$/i.exec(t))) { const d = new Date(d0), want = days.indexOf(m[1].toLowerCase()); let add = (want - d.getDay() + 7) % 7; if (add === 0) add = 7; d.setDate(d.getDate() + add); due = fmt(d); t = t.slice(0, m.index); } return { title: t.trim() || String(text || '').trim(), due }; } async function quickTask(text) { const { title, due } = parseQuickTask(text); if (!title) return; await jfetch('/api/tasks', { method: 'POST', body: JSON.stringify({ list: 'local:inbox', fields: { title, notes: '', due, done: false } }) }); toast('Task added' + (due ? ' (due ' + due + ')' : '')); if (location.hash.startsWith('#/tasks') && typeof viewTasks === 'function') viewTasks(); } /** Small sheet to capture something fast from any screen. */ function quickAdd() { const can = t => allowed(t); const el = openSheet('Quick add', '', `${can('tasks') ? `
` : ''} `); const qt = el.querySelector('#qt'); if (qt) qt.focus(); if (el.querySelector('#qf')) el.querySelector('#qf').onsubmit = async e => { e.preventDefault(); try { await quickTask(qt.value); closeSheet(); } catch (err) { el.querySelector('#qerr').textContent = err.message; } }; if (el.querySelector('#qe')) el.querySelector('#qe').onclick = () => { closeSheet(); newEvent(ymd(new Date())); }; if (el.querySelector('#ql')) el.querySelector('#ql').onclick = () => { closeSheet(); linkSheet(); }; } // ---- Command palette let palEl = null; function openPalette() { if (palEl) return closePalette(); const lastFocus = document.activeElement; palEl = document.createElement('div'); palEl.id = 'pal'; palEl.innerHTML = `Nothing matches.
'; input.setAttribute('aria-activedescendant', items.length ? 'po' + sel : ''); const on = list.querySelector('.on'); if (on) on.scrollIntoView({ block: 'nearest' }); }; const fetchLive = q => { // tasks and events from the server, shown under the commands clearTimeout(timer); live = []; if (q.length < 2 || !(allowed('tasks') || allowed('calendar'))) return; const mine = ++seq; timer = setTimeout(async () => { try { const r = await jfetch('/api/search?q=' + encodeURIComponent(q)); if (mine !== seq || !palEl) return; live = (r.tasks || []).slice(0, 4).map(t => ({ label: t.title, hint: 'Task' + (t.due ? ' · due ' + t.due : '') + (t.done ? ' · done' : ''), ic: 'check', run: go('#/tasks') })) .concat((r.events || []).slice(0, 4).map(e => ({ label: e.title, hint: 'Event · ' + (e.allDay ? e.start : new Date(e.start).toLocaleDateString([], { month: 'short', day: 'numeric' })), ic: 'cal', run: go('#/calendar') }))); build(); } catch {} }, 250); }; const run = n => { const it = items[n]; if (!it) return; closePalette(); setTimeout(it.run, 0); }; input.oninput = () => { sel = 0; fetchLive(input.value.trim()); build(); }; input.onkeydown = e => { if (e.key === 'ArrowDown') { e.preventDefault(); sel = (sel + 1) % Math.max(1, items.length); build(); } else if (e.key === 'ArrowUp') { e.preventDefault(); sel = (sel - 1 + items.length) % Math.max(1, items.length); build(); } else if (e.key === 'Enter') { e.preventDefault(); run(sel); } else if (e.key === 'Escape') { e.preventDefault(); closePalette(); } else if (e.key === 'Tab') e.preventDefault(); // keep focus in the box }; list.onclick = e => { const r = e.target.closest('.palrow'); if (r) run(+r.dataset.n); }; palEl.querySelector('.scrim').onclick = closePalette; palEl._restore = lastFocus; build(); input.focus(); requestAnimationFrame(() => palEl && palEl.classList.add('on')); } function closePalette() { if (!palEl) return; const r = palEl._restore; palEl.remove(); palEl = null; try { r && r.focus && r.focus(); } catch {} } addEventListener('keydown', e => { if ((e.metaKey || e.ctrlKey) && !e.shiftKey && !e.altKey && e.key.toLowerCase() === 'k') { if (document.body.classList.contains('editing') || !document.getElementById('nav')) return; // not on the sign-in page or inside the document editor e.preventDefault(); openPalette(); } });