diff --git a/cptr/frontend/src/lib/components/VoiceNoteModal.svelte b/cptr/frontend/src/lib/components/VoiceNoteModal.svelte index 92b78c0..97944ce 100644 --- a/cptr/frontend/src/lib/components/VoiceNoteModal.svelte +++ b/cptr/frontend/src/lib/components/VoiceNoteModal.svelte @@ -4,7 +4,6 @@ import { toast } from 'svelte-sonner'; import Modal from './Modal.svelte'; import Spinner from './common/Spinner.svelte'; - import Icon from './Icon.svelte'; import { writeFile } from '$lib/apis/files'; import { fetchJSON } from '$lib/apis'; import { transcribeEnabled } from '$lib/stores/audio'; @@ -24,7 +23,7 @@ let fileName = $state(''); let audioBlob = $state(null); - let waveformBars = $state(new Array(24).fill(2)); + let waveformBars = $state(new Array(40).fill(2)); let mediaRecorder: MediaRecorder | null = null; let audioContext: AudioContext | null = null; @@ -32,6 +31,7 @@ let animFrame = 0; let timerInterval = 0; let chunks: Blob[] = []; + let cancelled = false; let filenameInput: HTMLInputElement | undefined = $state(); // ── IndexedDB ─────────────────────────────────────────────── @@ -52,7 +52,10 @@ const db = await openIDB(); const tx = db.transaction(IDB_STORE, 'readwrite'); tx.objectStore(IDB_STORE).put({ id, blob, directory, createdAt: Date.now() }); - return new Promise((r, j) => { tx.oncomplete = () => r(); tx.onerror = () => j(tx.error); }); + return new Promise((r, j) => { + tx.oncomplete = () => r(); + tx.onerror = () => j(tx.error); + }); } async function clearFromIDB(id: string) { @@ -96,8 +99,8 @@ const tick = () => { if (!analyser) return; analyser.getByteFrequencyData(buf); - waveformBars = Array.from({ length: 24 }, (_, i) => - Math.max(2, (buf[Math.floor((i / 24) * buf.length)] / 255) * 24) + waveformBars = Array.from({ length: 40 }, (_, i) => + Math.max(2, (buf[Math.floor((i / 40) * buf.length)] / 255) * 20) ); animFrame = requestAnimationFrame(tick); }; @@ -105,12 +108,15 @@ chunks = []; mediaRecorder = new MediaRecorder(stream, { mimeType: mimeType() }); - mediaRecorder.ondataavailable = (e) => { if (e.data.size > 0) chunks.push(e.data); }; + mediaRecorder.ondataavailable = (e) => { + if (e.data.size > 0) chunks.push(e.data); + }; mediaRecorder.onstop = () => { - audioBlob = new Blob(chunks, { type: mediaRecorder?.mimeType || 'audio/webm' }); stream.getTracks().forEach((t) => t.stop()); cancelAnimationFrame(animFrame); audioContext?.close(); + if (cancelled) return; + audioBlob = new Blob(chunks, { type: mediaRecorder?.mimeType || 'audio/webm' }); handleComplete(); }; mediaRecorder.start(250); @@ -127,7 +133,11 @@ if (mediaRecorder?.state !== 'inactive') mediaRecorder?.stop(); } - function cancel() { stop(); onclose(); } + function cancel() { + cancelled = true; + stop(); + onclose(); + } // ── Save-first flow ───────────────────────────────────────── @@ -138,7 +148,9 @@ phase = 'processing'; // 1. IndexedDB safety - try { await saveToIDB(id, audioBlob); } catch {} + try { + await saveToIDB(id, audioBlob); + } catch {} // 2. Upload audio const audioName = `${id}.${ext(audioBlob)}`; @@ -157,7 +169,10 @@ try { const tf = new FormData(); tf.append('file', new File([audioBlob], audioName, { type: audioBlob.type })); - const res = await fetchJSON<{ text: string }>('/api/audio/transcribe', { method: 'POST', body: tf }); + const res = await fetchJSON<{ text: string }>('/api/audio/transcribe', { + method: 'POST', + body: tf + }); transcript = res.text || ''; } catch (err: any) { transcript = ''; @@ -179,7 +194,13 @@ const n = fileName.trim(); const audioFile = `${n}.${ext(audioBlob!)}`; const mdPath = `${directory}/${n}.md`; - const date = new Date().toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: '2-digit' }); + const date = new Date().toLocaleDateString('en-US', { + month: 'short', + day: 'numeric', + year: 'numeric', + hour: 'numeric', + minute: '2-digit' + }); let md = `# ${date}\n\n*${fmt(elapsed)}*\n\n`; if (transcript) md += `${transcript}\n\n`; @@ -206,36 +227,50 @@ - +
{ if (phase === 'naming' && e.key === 'Enter') { e.preventDefault(); save(); } }} + class="px-4 py-3.5" + onkeydown={(e) => { + if (phase === 'naming' && e.key === 'Enter') { + e.preventDefault(); + save(); + } + }} > {#if phase === 'recording'} -
- {#each waveformBars as h} -
- {/each} -
- -
- {fmt(elapsed)} -
- - +
+
+ {#each waveformBars as h} +
+ {/each}
+ {fmt(elapsed)}
+
+ + +
{:else if phase === 'processing'}
- Processing… + Processing…
- {:else if phase === 'naming'} {#if transcript} -

- {transcript.slice(0, 160)}{transcript.length > 160 ? '…' : ''} +

+ {transcript.slice(0, 140)}{transcript.length > 140 ? '…' : ''}

{/if} @@ -249,18 +284,20 @@ spellcheck="false" class="block w-full bg-transparent text-[13px] text-gray-700 dark:text-gray-300 placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none py-0.5" /> -

{directory.split('/').pop()}

+

+ {directory.split('/').pop()} +

-
+
+ >Save →
- {:else if phase === 'done'} -

Saved ✓

+

Saved ✓

{/if}
diff --git a/cptr/frontend/src/lib/stores/keybindings.ts b/cptr/frontend/src/lib/stores/keybindings.ts index befe833..47dccdd 100644 --- a/cptr/frontend/src/lib/stores/keybindings.ts +++ b/cptr/frontend/src/lib/stores/keybindings.ts @@ -75,7 +75,7 @@ export const DEFAULT_KEYBINDINGS: Record = { openSettings: 'Cmd+.', toggleSplit: 'Cmd+\\', toggleSidebar: 'Cmd+Shift+S', - voiceNote: 'Alt+R' + voiceNote: 'Cmd+Shift+M' }; // ── Store ───────────────────────────────────────────────────────