Files
librodrome/app/composables/useBookData.ts
Yvv 2f438d9d7a Refactoring complet : contenu livre, config unique, routes, admin et light mode
- Source unique : supprime app/data/librodrome.config.yml, renomme site/ en bookplayer.config.yml
- Morceaux : renommés avec slugs lisibles, fichiers audio renommés, inversion ch2↔ch3 corrigée
- Chapitres : 11 fichiers .md réécrits avec le vrai contenu du livre (synthèse fidèle du PDF)
- Routes : /lire → /modele-eco, /ecouter → /en-musique, redirections 301
- Admin chapitres : champs structurés (titre, description, temps lecture), compteur mots
- Éditeur markdown : mode split, plein écran, support Tab, meilleur rendu aperçu
- Admin morceaux : drag & drop, ajout/suppression, gestion playlist
- Light mode : palettes printemps/été plus saturées et contrastées, teintes primary
- Raccourcis clavier player : espace, flèches gauche/droite
- Paroles : toggle supprimé, toujours visibles et scrollables
- Nouvelles pages : autonomie, evenement

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 20:20:52 +01:00

103 lines
2.7 KiB
TypeScript

import type { Song } from '~/types/song'
import type { ChapterSongLink, BookConfig } from '~/types/book'
let _configCache: BookConfig | null = null
async function loadConfig(): Promise<BookConfig> {
if (_configCache) return _configCache
const parsed = await $fetch<any>('/api/content/config')
_configCache = {
title: parsed.book.title,
author: parsed.book.author,
description: parsed.book.description,
coverImage: parsed.book.coverImage,
chapters: [],
songs: parsed.songs as Song[],
chapterSongs: parsed.chapterSongs as ChapterSongLink[],
defaultPlaylistOrder: parsed.defaultPlaylistOrder as string[],
}
return _configCache
}
export function useBookData() {
const config = ref<BookConfig | null>(null)
const isLoaded = ref(false)
async function init() {
if (isLoaded.value) return
config.value = await loadConfig()
isLoaded.value = true
}
function getSongs(): Song[] {
return config.value?.songs ?? []
}
function getSongById(id: string): Song | undefined {
return config.value?.songs.find(s => s.id === id)
}
function getChapterSongs(chapterSlug: string): Song[] {
if (!config.value) return []
const links = config.value.chapterSongs.filter(cs => cs.chapterSlug === chapterSlug)
return links
.map(link => config.value!.songs.find(s => s.id === link.songId))
.filter((s): s is Song => !!s)
}
function getPrimarySong(chapterSlug: string): Song | undefined {
if (!config.value) return undefined
const link = config.value.chapterSongs.find(
cs => cs.chapterSlug === chapterSlug && cs.primary,
)
if (!link) return undefined
return config.value.songs.find(s => s.id === link.songId)
}
function getChapterSongLinks(chapterSlug: string): ChapterSongLink[] {
return config.value?.chapterSongs.filter(cs => cs.chapterSlug === chapterSlug) ?? []
}
function getPlaylistOrder(): Song[] {
if (!config.value) return []
return config.value.defaultPlaylistOrder
.map(id => config.value!.songs.find(s => s.id === id))
.filter((s): s is Song => !!s)
}
function getChapterForSong(songId: string): string | undefined {
if (!config.value) return undefined
const link = config.value.chapterSongs.find(
cs => cs.songId === songId && cs.primary,
)
return link?.chapterSlug
}
function getBookMeta() {
if (!config.value) return null
return {
title: config.value.title,
author: config.value.author,
description: config.value.description,
coverImage: config.value.coverImage,
}
}
return {
config,
isLoaded,
init,
getSongs,
getSongById,
getChapterSongs,
getPrimarySong,
getChapterSongLinks,
getChapterForSong,
getPlaylistOrder,
getBookMeta,
}
}