4fb176faf6
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import { useApi } from '../composables/useApi.js'
|
|
|
|
// IR reader.js "viewport-center-in-third" rule, pure for unit tests.
|
|
// metrics: [{page_number, top, height}] in page order.
|
|
export function currentPageFromScroll(metrics, scrollTop, viewportH) {
|
|
if (!metrics.length) return 1
|
|
const target = scrollTop + viewportH / 3
|
|
let cur = metrics[0].page_number
|
|
for (const m of metrics) {
|
|
if (target >= m.top) cur = m.page_number
|
|
}
|
|
return cur
|
|
}
|
|
|
|
export function progressPct(scrollTop, scrollHeight, clientHeight) {
|
|
const denom = scrollHeight - clientHeight
|
|
if (denom <= 0) return 0
|
|
const pct = (scrollTop / denom) * 100
|
|
return Math.min(100, Math.max(0, pct))
|
|
}
|
|
|
|
export function clampPage(n, total) {
|
|
if (!total || total < 1) return 1
|
|
if (!Number.isFinite(n) || n < 1) return 1
|
|
if (n > total) return total
|
|
return Math.floor(n)
|
|
}
|
|
|
|
export const useSeriesReaderStore = defineStore('seriesReader', () => {
|
|
const api = useApi()
|
|
|
|
const series = ref(null)
|
|
const pages = ref([])
|
|
const loading = ref(false)
|
|
const error = ref(null)
|
|
|
|
async function load(tagId) {
|
|
loading.value = true
|
|
error.value = null
|
|
try {
|
|
const body = await api.get(`/api/series/${tagId}/pages`)
|
|
series.value = body.series
|
|
pages.value = body.pages
|
|
} catch (e) {
|
|
error.value = e.message
|
|
series.value = null
|
|
pages.value = []
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
return { series, pages, loading, error, load }
|
|
})
|