-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathindex.ts
More file actions
168 lines (146 loc) · 6.05 KB
/
Copy pathindex.ts
File metadata and controls
168 lines (146 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { db } from '$lib/catalog/db';
import type { VolumeData, VolumeMetadata } from '$lib/types';
import { liveQuery } from 'dexie';
import { derived, readable, type Readable } from 'svelte/store';
import { deriveSeriesFromVolumes } from '$lib/catalog/catalog';
import { unifiedCloudManager } from '$lib/util/sync/unified-cloud-manager';
import { generatePlaceholders } from '$lib/catalog/placeholders';
import { routeParams } from '$lib/util/hash-router';
import { libraryFilesStore, libraryMokuroFilesStore, generateLibraryPlaceholders } from '$lib/util/libraries';
import { selectedLibraryId } from '$lib/settings/libraries';
// Single source of truth from the database
export const volumes = readable<Record<string, VolumeMetadata>>({}, (set) => {
const subscription = liveQuery(async () => {
const volumesArray = await db.volumes.toArray();
return volumesArray.reduce(
(acc, vol) => {
acc[vol.volume_uuid] = vol;
return acc;
},
{} as Record<string, VolumeMetadata>
);
}).subscribe({
next: (value) => set(value),
error: (err) => console.error(err)
});
return () => subscription.unsubscribe();
});
// Merge local volumes with cloud placeholders and library placeholders
export const volumesWithPlaceholders = derived(
[volumes, unifiedCloudManager.cloudFiles, libraryFilesStore, libraryMokuroFilesStore, selectedLibraryId],
([$volumes, $cloudFiles, $libraryFiles, $libraryMokuroFiles, $selectedLibraryId]) => {
const combined = { ...$volumes };
const localVolumes = Object.values($volumes);
// Generate cloud provider placeholders
if ($cloudFiles.size > 0) {
const cloudPlaceholders = generatePlaceholders($cloudFiles, localVolumes);
for (const placeholder of cloudPlaceholders) {
combined[placeholder.volume_uuid] = placeholder;
}
}
// Generate library placeholders
if ($libraryFiles.size > 0) {
// Pass all combined volumes so library placeholders don't duplicate cloud placeholders
const allVolumes = Object.values(combined);
const libraryPlaceholders = generateLibraryPlaceholders(
$libraryFiles,
$libraryMokuroFiles,
allVolumes,
$selectedLibraryId
);
for (const placeholder of libraryPlaceholders) {
combined[placeholder.volume_uuid] = placeholder;
}
}
return combined;
},
{} as Record<string, VolumeMetadata>
);
// Each derived store needs to be passed as an array if using multiple inputs
export const catalog = derived([volumesWithPlaceholders], ([$volumesWithPlaceholders]) => {
// Return null while loading (before first data emission)
if ($volumesWithPlaceholders === undefined) {
return null;
}
return deriveSeriesFromVolumes(Object.values($volumesWithPlaceholders));
});
export const currentSeries = derived([routeParams, catalog], ([$routeParams, $catalog]) => {
if (!$catalog || !$routeParams.manga) return [];
const routeKey = $routeParams.manga.trim().replace(/\s+/g, ' ').toLowerCase();
// Primary: match by title (folder name) - handles placeholder→local transition
let series = $catalog.find((s) => s.title.trim().replace(/\s+/g, ' ').toLowerCase() === routeKey);
// Fallback: match by UUID (for legacy URLs)
if (!series) {
series = $catalog.find((s) => s.series_uuid === $routeParams.manga);
}
return series?.volumes || [];
});
export const currentVolume = derived([routeParams, volumes], ([$routeParams, $volumes]) => {
if ($routeParams && $volumes && $routeParams.volume) {
return $volumes[$routeParams.volume]; // Direct lookup instead of find()
}
return undefined;
});
export const currentVolumeData: Readable<VolumeData | undefined> = derived(
[currentVolume],
([$currentVolume], set: (value: VolumeData | undefined) => void) => {
// Track the last volume UUID to avoid unnecessary clears
// This prevents flash when unrelated volumes are added to the database
const newUuid = $currentVolume?.volume_uuid;
// Only clear data when actually navigating to a different volume
// Don't clear if the store just emitted a new object reference for the same volume
if (newUuid !== currentVolumeDataLastUuid) {
currentVolumeDataLastUuid = newUuid;
// Clear old data synchronously to prevent state leaks between volumes
set(undefined);
}
if ($currentVolume) {
// Assemble VolumeData from volume_ocr and volume_files tables
Promise.all([
db.volume_ocr.get($currentVolume.volume_uuid),
db.volume_files.get($currentVolume.volume_uuid)
]).then(([ocr, files]) => {
if (ocr) {
set({
volume_uuid: $currentVolume.volume_uuid,
pages: ocr.pages,
files: files?.files
});
}
});
}
},
undefined // Initial value
);
// Track last volume UUID to prevent unnecessary data clears
let currentVolumeDataLastUuid: string | undefined;
/**
* Japanese character count for current volume.
* Uses page_char_counts from metadata for O(1) lookup when available.
*/
export const currentVolumeCharacterCount = derived(
[currentVolume, currentVolumeData],
([$currentVolume, $currentVolumeData]) => {
if (!$currentVolume) return 0;
// Use pre-calculated cumulative char counts from metadata (v3)
if ($currentVolume.page_char_counts && $currentVolume.page_char_counts.length > 0) {
// Last element of cumulative array is the total
return $currentVolume.page_char_counts[$currentVolume.page_char_counts.length - 1];
}
// Fallback: calculate from pages if page_char_counts not available
if ($currentVolumeData && $currentVolumeData.pages) {
const japaneseRegex =
/[○◯々-〇〻ぁ-ゖゝ-ゞァ-ヺー\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}]/u;
let totalChars = 0;
for (const page of $currentVolumeData.pages) {
for (const block of page.blocks) {
for (const line of block.lines) {
totalChars += Array.from(line).filter((char) => japaneseRegex.test(char)).length;
}
}
}
return totalChars;
}
return 0;
}
);