T-031: Lesezeichen mit Notiz — bookmarks.json, Panel in der Leiste (Sprung, Notiz editieren, delete)
This commit is contained in:
@ -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 |
|
||||
|
||||
|
||||
90
src-tauri/src/bookmarks.rs
Normal file
90
src-tauri/src/bookmarks.rs
Normal file
@ -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<String, Vec<Bookmark>>;
|
||||
|
||||
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<Vec<Bookmark>, String> {
|
||||
Ok(load().remove(asin.trim()).unwrap_or_default())
|
||||
}
|
||||
|
||||
pub fn add(asin: &str, time: f64, note: &str) -> Result<Vec<Bookmark>, 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<Vec<Bookmark>, 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<Vec<Bookmark>, 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}"))
|
||||
}
|
||||
@ -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<PlayerState,
|
||||
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn bookmark_list(asin: String) -> Result<Vec<bookmarks::Bookmark>, 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<Vec<bookmarks::Bookmark>, 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<Vec<bookmarks::Bookmark>, 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<Vec<bookmarks::Bookmark>, 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<PlayerState, String> {
|
||||
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,
|
||||
|
||||
@ -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<SleepMode>({ kind: 'off' })
|
||||
let sleepLeft = $state<string>('')
|
||||
let sleepTimer: ReturnType<typeof setInterval> | null = null
|
||||
// Lesezeichen (T-031)
|
||||
let bmOpen = $state(false)
|
||||
let bookmarks = $state<Bm[]>([])
|
||||
let bmEditIdx = $state<number | null>(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))}
|
||||
/>
|
||||
<button class="ghost iconbtn" title="Lesezeichen (Panel)" onclick={() => openBookmarks()}><Icon name="flag" size={14} /></button>
|
||||
<button class="ghost iconbtn" title="Bookmark an aktueller Stelle" onclick={() => bookmarkAddHere()}><Icon name="pencil" size={14} /></button>
|
||||
<button class="ghost" title="Schließen" onclick={() => playerStop()}><Icon name="x" /></button>
|
||||
<span class="sleepwrap">
|
||||
<Icon name="moon" size={13} />
|
||||
@ -615,6 +670,32 @@
|
||||
{#if sleepLeft}<span class="muted small">{sleepLeft}</span>{/if}
|
||||
</span>
|
||||
</footer>
|
||||
{#if bmOpen && player.asin}
|
||||
<div class="bmpanel">
|
||||
{#if bookmarks.length === 0}
|
||||
<p class="muted small">kein Lesezeichen — mit ✎ erzeugen.</p>
|
||||
{/if}
|
||||
{#each bookmarks as b, i}
|
||||
<div class="bmrow">
|
||||
<button class="ghost" title="Dorthin springen" onclick={() => bookmarkJump(i)}><Icon name="play" size={12} /> {fmtClock(b.time)}</button>
|
||||
{#if bmEditIdx === i}
|
||||
<input
|
||||
class="input bmnote"
|
||||
placeholder="Notiz…"
|
||||
bind:value={bmDraft}
|
||||
onkeydown={(e) => e.key === 'Enter' && bookmarkSave(i)}
|
||||
/>
|
||||
<button class="primary" title="Notiz speichern" onclick={() => bookmarkSave(i)}><Icon name="check" size={13} /></button>
|
||||
{:else}
|
||||
<span class="bmtext muted small">{b.note || '—'}</span>
|
||||
<button class="ghost" title="Notiz bearbeiten" onclick={() => { bmEditIdx = i; bmDraft = b.note }}><Icon name="pencil" size={12} /></button>
|
||||
{/if}
|
||||
<button class="ghost" title="Lesezeichen löschen" onclick={() => bookmarkDel(i)}><Icon name="trash" size={12} /></button>
|
||||
</div>
|
||||
{/each}
|
||||
<button class="ghost" title="Panel schließen" onclick={() => (bmOpen = false)}><Icon name="x" size={12} /></button>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
</main>
|
||||
{/if}
|
||||
|
||||
49
src/app.css
49
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;
|
||||
}
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
forward:
|
||||
'<path d="m4 6 6 6-6 6"/><path d="m13 6 6 6-6 6"/>',
|
||||
check: '<path d="M4 12l5 5L20 6"/>',
|
||||
flag: '<path d="M6 21V4h11l-2.5 4L17 12H6"/>',
|
||||
pencil:
|
||||
'<path d="M4 20h4L20 8l-4-4L4 16v4z"/><path d="M13.5 6.5l4 4"/>',
|
||||
x: '<path d="M5 5l14 14"/><path d="M19 5 5 19"/>',
|
||||
|
||||
@ -67,6 +67,15 @@ export const playerSnapshot = () => invoke<PlayerState>('player_snapshot')
|
||||
export const onPlayerState = (cb: (s: PlayerState) => void) =>
|
||||
listen<PlayerState>('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<Bm[]>('bookmark_list', { asin })
|
||||
export const bmAdd = (asin: string, time: number, note: string) =>
|
||||
invoke<Bm[]>('bookmark_add', { asin, time, note })
|
||||
export const bmRemove = (asin: string, index: number) =>
|
||||
invoke<Bm[]>('bookmark_remove', { asin, index })
|
||||
export const bmSetNote = (asin: string, index: number, note: string) =>
|
||||
invoke<Bm[]>('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)
|
||||
|
||||
Reference in New Issue
Block a user