Add line count

This commit is contained in:
ZXY101
2024-03-10 03:32:24 +02:00
parent 5bc486660c
commit 282cad6558
3 changed files with 25 additions and 11 deletions

View File

@@ -66,7 +66,7 @@
return; return;
} }
const pageClamped = clamp(newPage, 1, pages?.length); const pageClamped = clamp(newPage, 1, pages?.length);
const charCount = getCharCount(pages, pageClamped) || 0; const { charCount } = getCharCount(pages, pageClamped);
updateProgress( updateProgress(
volume.mokuroData.volume_uuid, volume.mokuroData.volume_uuid,
@@ -144,8 +144,9 @@
} }
} }
$: charCount = $settings.charCount ? getCharCount(pages, page) : 0; $: charCount = $settings.charCount ? getCharCount(pages, page).charCount : 0;
$: maxCharCount = getCharCount(pages); $: maxCharCount = getCharCount(pages).charCount;
$: totalLineCount = getCharCount(pages).lineCount;
let startX = 0; let startX = 0;
let startY = 0; let startY = 0;
@@ -204,26 +205,34 @@
$: { $: {
if (volume) { if (volume) {
const { charCount, lineCount } = getCharCount(pages, page);
fireExstaticEvent('mokuro-reader:page.change', { fireExstaticEvent('mokuro-reader:page.change', {
title: volume.mokuroData.title, title: volume.mokuroData.title,
volumeName: volume.mokuroData.volume, volumeName: volume.mokuroData.volume,
currentCharCount: getCharCount(pages, page) || 0, currentCharCount: charCount,
currentPage: page, currentPage: page,
totalPages: pages.length, totalPages: pages.length,
totalCharCount: maxCharCount || 0 totalCharCount: maxCharCount || 0,
currentlineCount: lineCount,
totalLineCount
}); });
} }
} }
beforeNavigate(() => { beforeNavigate(() => {
if (volume) { if (volume) {
const { charCount, lineCount } = getCharCount(pages, page);
fireExstaticEvent('mokuro-reader:reader.closed', { fireExstaticEvent('mokuro-reader:reader.closed', {
title: volume.mokuroData.title, title: volume.mokuroData.title,
volumeName: volume.mokuroData.volume, volumeName: volume.mokuroData.volume,
currentCharCount: getCharCount(pages, page) || 0, currentCharCount: charCount,
currentPage: page, currentPage: page,
totalPages: pages.length, totalPages: pages.length,
totalCharCount: maxCharCount || 0 totalCharCount: maxCharCount || 0,
currentlineCount: lineCount,
totalLineCount
}); });
} }
}); });

View File

@@ -17,20 +17,23 @@ export function countChars(line: string) {
} }
export function getCharCount(pages: Page[], currentPage?: number) { export function getCharCount(pages: Page[], currentPage?: number) {
let charCount = 0;
let lineCount = 0;
if (pages && pages.length > 0) { if (pages && pages.length > 0) {
const max = currentPage || pages.length const max = currentPage || pages.length
let charCount = 0;
for (let i = 0; i < max; i++) { for (let i = 0; i < max; i++) {
const blocks = pages[i].blocks; const blocks = pages[i].blocks;
blocks.forEach((block) => { blocks.forEach((block) => {
lineCount += block.lines.length;
block.lines.forEach((line) => { block.lines.forEach((line) => {
charCount += countChars(line); charCount += countChars(line);
}); });
}); });
} }
}
return charCount; return { charCount, lineCount };
}
} }

View File

@@ -39,7 +39,9 @@ type ExtaticPayload = {
currentCharCount: number; currentCharCount: number;
totalCharCount: number; totalCharCount: number;
currentPage: number; currentPage: number;
totalPages: number totalPages: number;
currentlineCount: number;
totalLineCount: number;
} }
type ExtaticEvent = 'mokuro-reader:page.change' | 'mokuro-reader:reader.closed' type ExtaticEvent = 'mokuro-reader:page.change' | 'mokuro-reader:reader.closed'