Proper char count implementation
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
import Cropper from './Cropper.svelte';
|
||||
import { page as pageStore } from '$app/stores';
|
||||
import SettingsButton from './SettingsButton.svelte';
|
||||
import { getCharCount } from '$lib/util/count-chars';
|
||||
|
||||
// TODO: Refactor this whole mess
|
||||
|
||||
@@ -63,7 +64,6 @@
|
||||
}
|
||||
updateProgress(volume.mokuroData.volume_uuid, clamp(newPage, 1, pages?.length));
|
||||
zoomDefault();
|
||||
updateCharacterCount(clamp(newPage, 1, pages?.length));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,11 +111,6 @@
|
||||
|
||||
afterUpdate(() => {
|
||||
zoomDefault();
|
||||
updateCharacterCount(page);
|
||||
});
|
||||
|
||||
onMount(() => {
|
||||
updateCharacterCount(page);
|
||||
});
|
||||
|
||||
function handleShortcuts(event: KeyboardEvent & { currentTarget: EventTarget & Window }) {
|
||||
@@ -151,42 +146,8 @@
|
||||
}
|
||||
}
|
||||
|
||||
let charCount = 0;
|
||||
|
||||
function updateCharacterCount(currentPage: number) {
|
||||
charCount = 0;
|
||||
if (pages && pages.length > 0 && $settings.charCount) {
|
||||
for (let i = 0; i < currentPage; i++) {
|
||||
const blocks = pages[i].blocks;
|
||||
blocks.forEach((block) => {
|
||||
block.lines.forEach((line) => {
|
||||
charCount += line.length;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let maxCharCount = 0;
|
||||
|
||||
$: pages?.length, getMaxCharCount();
|
||||
|
||||
function getMaxCharCount() {
|
||||
maxCharCount = 0;
|
||||
|
||||
if (pages && pages.length > 0) {
|
||||
for (let i = 0; i < pages.length; i++) {
|
||||
const blocks = pages[i].blocks;
|
||||
blocks.forEach((block) => {
|
||||
block.lines.forEach((line) => {
|
||||
maxCharCount += line.length;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return maxCharCount;
|
||||
}
|
||||
$: charCount = $settings.charCount ? getCharCount(pages, page) : 0;
|
||||
$: maxCharCount = getCharCount(pages);
|
||||
|
||||
let startX = 0;
|
||||
let startY = 0;
|
||||
|
||||
35
src/lib/util/count-chars.ts
Normal file
35
src/lib/util/count-chars.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import type { Page } from "$lib/types";
|
||||
|
||||
/**
|
||||
* @license BSD-3-Clause
|
||||
* Copyright (c) 2023, ッツ Reader Authors
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
// isNotJapaneseRegex aquired from ttsu reader
|
||||
// https://github.com/ttu-ttu/ebook-reader/blob/main/apps/web/src/lib/functions/get-character-count.ts
|
||||
|
||||
export function countChars(line: string) {
|
||||
const isNotJapaneseRegex = /[^0-9A-Z○◯々-〇〻ぁ-ゖゝ-ゞァ-ヺー0-9A-Zヲ-ン\p{Radical}\p{Unified_Ideograph}]+/gimu
|
||||
const cleaned = line.replace(isNotJapaneseRegex, '')
|
||||
return cleaned.length;
|
||||
}
|
||||
|
||||
export function getCharCount(pages: Page[], currentPage?: number) {
|
||||
if (pages && pages.length > 0) {
|
||||
const max = currentPage || pages.length
|
||||
let charCount = 0;
|
||||
|
||||
for (let i = 0; i < max; i++) {
|
||||
const blocks = pages[i].blocks;
|
||||
|
||||
blocks.forEach((block) => {
|
||||
block.lines.forEach((line) => {
|
||||
charCount += countChars(line);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return charCount;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user