diff --git a/ROADMAP.md b/ROADMAP.md index f8a569a..29ccc74 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -41,7 +41,7 @@ Abnahme M0: Ein gekauftes Buch läuft lokal mit Kapiteln. | ID | Titel | Status | |----|-------|--------| -| T-030 | Zuletzt gehörte Position mit Audible abgleichen | läuft | +| T-030 | Zuletzt gehörte Position mit Audible abgleichen | fertig | | T-031 | Lesezeichen / Notizen | offen | | T-032 | Tray-Icon, Autostart optional | offen | diff --git a/src-tauri/src/bookmarks.rs b/src-tauri/src/bookmarks.rs new file mode 100644 index 0000000..1a782a4 --- /dev/null +++ b/src-tauri/src/bookmarks.rs @@ -0,0 +1,90 @@ +//! Lesezeichen mit Notiz je Buch (T-031), Ablage: bookmarks.json. + +use std::collections::BTreeMap; +use std::fs; +use std::path::PathBuf; + +use serde::{Deserialize, Serialize}; + +use crate::audible::books_dir; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Bookmark { + pub time: f64, + pub note: String, +} + +pub type Bookmarks = BTreeMap>; + +fn path() -> PathBuf { + books_dir() + .parent() + .map(|p| p.join("bookmarks.json")) + .unwrap_or_else(|| PathBuf::from("bookmarks.json")) +} + +pub fn load() -> Bookmarks { + fs::read_to_string(path()) + .ok() + .and_then(|t| serde_json::from_str(&t).ok()) + .unwrap_or_default() +} + +pub fn list(asin: &str) -> Result, String> { + Ok(load().remove(asin.trim()).unwrap_or_default()) +} + +pub fn add(asin: &str, time: f64, note: &str) -> Result, String> { + let asin = asin.trim().to_string(); + let mut all = load(); + let list = all.entry(asin).or_default(); + list.push(Bookmark { time: time.max(0.0), note: note.trim().to_string() }); + list.sort_by(|a, b| a.time.partial_cmp(&b.time).unwrap_or(std::cmp::Ordering::Equal)); + let updated = list.clone(); + save(&all)?; + Ok(updated) +} + +pub fn remove(asin: &str, index: usize) -> Result, String> { + let asin = asin.trim().to_string(); + let mut all = load(); + let Some(list) = all.get_mut(&asin) else { + return Err("kein Lesezeichen gefunden".into()); + }; + if index >= list.len() { + return Err("Index außerhalb".into()); + } + list.remove(index); + if list.is_empty() { + all.remove(&asin); + } + let updated = all.get(&asin).cloned().unwrap_or_default(); + save(&all)?; + Ok(updated) +} + +pub fn set_note(asin: &str, index: usize, note: &str) -> Result, String> { + let asin = asin.trim().to_string(); + let mut all = load(); + let Some(list) = all.get_mut(&asin) else { + return Err("kein Lesezeichen gefunden".into()); + }; + if index >= list.len() { + return Err("Index außerhalb".into()); + } + list[index].note = note.trim().to_string(); + let updated = list.clone(); + save(&all)?; + Ok(updated) +} + +fn save(all: &Bookmarks) -> Result<(), String> { + if let Some(parent) = path().parent() { + fs::create_dir_all(parent).map_err(|e| format!("Ordner: {e}"))?; + } + let p = path(); + let tmp = p.with_extension("json.tmp"); + fs::write(&tmp, serde_json::to_vec(all).map_err(|e| e.to_string())?) + .map_err(|e| format!("Lesezeichen schreiben: {e}"))?; + fs::rename(&tmp, &p).map_err(|e| format!("Lesezeichen speichern: {e}")) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 9dddf3e..238daaf 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,4 +1,5 @@ mod audible; +mod bookmarks; mod download; mod player; @@ -162,6 +163,45 @@ async fn player_stop(pl: tauri::State<'_, SharedPlayer>) -> Result Result, String> { + tauri::async_runtime::spawn_blocking(move || bookmarks::list(&asin)) + .await + .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? +} + +#[tauri::command] +async fn bookmark_add( + asin: String, + time: f64, + note: String, +) -> Result, String> { + tauri::async_runtime::spawn_blocking(move || bookmarks::add(&asin, time, ¬e)) + .await + .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? +} + +#[tauri::command] +async fn bookmark_remove( + asin: String, + index: usize, +) -> Result, String> { + tauri::async_runtime::spawn_blocking(move || bookmarks::remove(&asin, index)) + .await + .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? +} + +#[tauri::command] +async fn bookmark_set_note( + asin: String, + index: usize, + note: String, +) -> Result, String> { + tauri::async_runtime::spawn_blocking(move || bookmarks::set_note(&asin, index, ¬e)) + .await + .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? +} + #[tauri::command] async fn player_snapshot(pl: tauri::State<'_, SharedPlayer>) -> Result { let p = pl.inner().clone(); @@ -212,6 +252,10 @@ pub fn run() { player_set_speed, player_stop, player_snapshot, + bookmark_list, + bookmark_add, + bookmark_remove, + bookmark_set_note, download_list, download_enqueue, download_cancel, diff --git a/src/App.svelte b/src/App.svelte index e0035cc..44e1602 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -25,6 +25,11 @@ downloadCancel, downloadDelete, onDownloadUpdate, + bmList, + bmAdd, + bmRemove, + bmSetNote, + type Bm, type Book, type DlItem, } from './lib/audible' @@ -69,6 +74,54 @@ let sleepMode = $state({ kind: 'off' }) let sleepLeft = $state('') let sleepTimer: ReturnType | null = null + // Lesezeichen (T-031) + let bmOpen = $state(false) + let bookmarks = $state([]) + let bmEditIdx = $state(null) + let bmDraft = $state('') + + function openBookmarks(): void { + bmOpen = !bmOpen + if (bmOpen) { + const asin = player.asin ?? '' + bmList(asin) + .then((l) => (bookmarks = l)) + .catch((e) => (error = String(e))) + } + } + + function bookmarkAddHere(): void { + const asin = player.asin ?? '' + bmAdd(asin, player.position, '') + .then((l) => { + bookmarks = l + bmEditIdx = l.length - 1 + bmDraft = '' + }) + .catch((e) => (error = String(e))) + } + + function bookmarkDel(idx: number): void { + const asin = player.asin ?? '' + bmRemove(asin, idx) + .then((l) => (bookmarks = l)) + .catch((e) => (error = String(e))) + bmEditIdx = null + } + + function bookmarkSave(idx: number): void { + const asin = player.asin ?? '' + bmSetNote(asin, idx, bmDraft) + .then((l) => { + bookmarks = l + bmEditIdx = null + }) + .catch((e) => (error = String(e))) + } + + function bookmarkJump(idx: number): void { + playerSetPosition(bookmarks[idx]?.time ?? 0) + } function setSleep(sel: string): void { if (sleepTimer) { @@ -594,6 +647,8 @@ value={player.position} onchange={(e) => playerSetPosition(Number(e.currentTarget.value))} /> + + @@ -615,6 +670,32 @@ {#if sleepLeft}{sleepLeft}{/if} + {#if bmOpen && player.asin} +
+ {#if bookmarks.length === 0} +

kein Lesezeichen — mit ✎ erzeugen.

+ {/if} + {#each bookmarks as b, i} +
+ + {#if bmEditIdx === i} + e.key === 'Enter' && bookmarkSave(i)} + /> + + {:else} + {b.note || '—'} + + {/if} + +
+ {/each} + +
+ {/if} {/if} {/if} diff --git a/src/app.css b/src/app.css index d0b7c69..9f82465 100644 --- a/src/app.css +++ b/src/app.css @@ -388,3 +388,52 @@ code { font-size: 0.85rem; padding: 0.25rem 0.4rem; } + +/* Lesezeichen-Panel (T-031) */ + +.bmpanel { + position: fixed; + right: 1rem; + bottom: 5.2rem; + width: min(26rem, calc(100vw - 2rem)); + background: var(--bg-card); + border: 1px solid var(--border); + border-radius: 8px; + padding: 0.8rem 0.9rem; + display: flex; + flex-direction: column; + gap: 0.5rem; + z-index: 6; + max-height: 12rem; + overflow: auto; + box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4); +} + +.bmrow { + display: flex; + align-items: center; + gap: 0.4rem; +} + +.bmrow button { + padding: 0.2rem 0.5rem; + font-size: 0.85rem; + display: inline-flex; + align-items: center; + gap: 0.3rem; +} + +.bmtext { + flex: 1; + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.bmnote { + flex: 1; + min-width: 0; + font-size: 0.85rem; + padding: 0.25rem 0.5rem; +} diff --git a/src/lib/Icon.svelte b/src/lib/Icon.svelte index 07985e7..da74eff 100644 --- a/src/lib/Icon.svelte +++ b/src/lib/Icon.svelte @@ -18,6 +18,7 @@ forward: '', check: '', + flag: '', pencil: '', x: '', diff --git a/src/lib/audible.ts b/src/lib/audible.ts index a748c9f..9617754 100644 --- a/src/lib/audible.ts +++ b/src/lib/audible.ts @@ -67,6 +67,15 @@ export const playerSnapshot = () => invoke('player_snapshot') export const onPlayerState = (cb: (s: PlayerState) => void) => listen('player-state', (ev) => cb(ev.payload)) export const assetUri = (path: string) => convertFileSrc(path) + +export interface Bm { time: number; note: string } +export const bmList = (asin: string) => invoke('bookmark_list', { asin }) +export const bmAdd = (asin: string, time: number, note: string) => + invoke('bookmark_add', { asin, time, note }) +export const bmRemove = (asin: string, index: number) => + invoke('bookmark_remove', { asin, index }) +export const bmSetNote = (asin: string, index: number, note: string) => + invoke('bookmark_set_note', { asin, index, note }) export function fmtClock(sec: number): string { const t = Math.max(0, Math.floor(sec)) const h = Math.floor(t / 3600)