fix(kobo-sync): use KoboSpan for progress sync (#253)

This commit is contained in:
Alex
2026-04-23 13:36:15 +01:00
committed by GitHub
parent bfca35b03c
commit f21cf1aee9
60 changed files with 2770 additions and 161 deletions
@@ -60,7 +60,9 @@ export interface Book extends FileInfo {
}
export interface EpubProgress {
cfi: string;
cfi?: string | null;
href?: string;
contentSourceProgressPercent?: number | null;
percentage: number;
}
@@ -74,6 +74,7 @@ import {ViewEvent} from './core/view-manager.service';
})
export class EbookReaderComponent implements OnInit {
private readonly destroyRef = inject(DestroyRef);
private static readonly MAX_CHAPTER_PROGRESS_PERCENT = 99.9;
private loaderService = inject(ReaderLoaderService);
private styleService = inject(ReaderStyleService);
private bookService = inject(BookService);
@@ -101,6 +102,7 @@ export class EbookReaderComponent implements OnInit {
private visibilityManager!: ReaderHeaderFooterVisibilityManager;
private relocateTimeout?: ReturnType<typeof setTimeout>;
private sectionFractionsTimeout?: ReturnType<typeof setTimeout>;
private pendingInitialChapterRestore: { href: string; contentSourceProgressPercent: number } | null = null;
isLoading = signal(true);
showQuickSettings = signal(false);
@@ -251,6 +253,8 @@ export class EbookReaderComponent implements OnInit {
this.hasLoadedOnce = true;
if (book.epubProgress?.cfi) {
return this.viewManager.goTo(book.epubProgress.cfi);
} else if (book.epubProgress?.href) {
return this.viewManager.goTo(book.epubProgress.href);
} else if (book.epubProgress?.percentage && book.epubProgress.percentage > 0) {
return this.viewManager.goToFraction(book.epubProgress.percentage / 100);
} else {
@@ -287,7 +291,6 @@ export class EbookReaderComponent implements OnInit {
this.viewManager.createView(container);
return of(undefined);
}
private loadBookBlob(): Observable<void> {
return this.bookFileService.getFileContent(this.bookId, this.altBookType).pipe(
switchMap(fileBlob => {
@@ -309,6 +312,14 @@ export class EbookReaderComponent implements OnInit {
this.updateSectionFractions();
break;
case 'relocate':
if (this.handlePendingInitialChapterRestore(event.detail)) {
if (this.sectionFractionsTimeout) clearTimeout(this.sectionFractionsTimeout);
this.sectionFractionsTimeout = setTimeout(() => {
this.updateSectionFractions();
}, 500);
break;
}
if (this.relocateTimeout) clearTimeout(this.relocateTimeout);
this.relocateTimeout = setTimeout(() => {
this.progressService.handleRelocateEvent(event.detail);
@@ -382,6 +393,98 @@ export class EbookReaderComponent implements OnInit {
this.sectionFractions.set(this.viewManager.getSectionFractions());
}
private restoreSavedPosition(book: Book): Observable<void> {
const progress = book.epubProgress;
if (progress?.cfi) {
return this.viewManager.goTo(progress.cfi);
}
if (progress?.href) {
const chapterProgress = progress.contentSourceProgressPercent;
if (typeof chapterProgress === 'number' && Number.isFinite(chapterProgress) && chapterProgress > 0) {
this.pendingInitialChapterRestore = {
href: this.normalizeHref(progress.href),
contentSourceProgressPercent: chapterProgress
};
} else {
this.pendingInitialChapterRestore = null;
}
return this.viewManager.goTo(progress.href);
}
this.pendingInitialChapterRestore = null;
if (progress?.percentage && progress.percentage > 0) {
return this.viewManager.goToFraction(progress.percentage / 100);
}
return this.viewManager.goTo(0);
}
private handlePendingInitialChapterRestore(detail: RelocateProgressData): boolean {
if (!this.pendingInitialChapterRestore) {
return false;
}
const currentHref = this.normalizeHref(detail.pageItem?.href ?? detail.tocItem?.href ?? null);
if (!currentHref || !this.hrefsMatch(currentHref, this.pendingInitialChapterRestore.href)) {
return false;
}
const targetFraction = this.resolveChapterFraction(
detail.section?.current,
this.pendingInitialChapterRestore.contentSourceProgressPercent
);
if (targetFraction === null) {
return false;
}
if (typeof detail.fraction === 'number' && Math.abs(detail.fraction - targetFraction) < 0.0001) {
this.pendingInitialChapterRestore = null;
return false;
}
this.pendingInitialChapterRestore = null;
this.viewManager.goToFraction(targetFraction)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe();
return true;
}
private resolveChapterFraction(sectionIndex: number | undefined, chapterProgressPercent: number): number | null {
if (sectionIndex === undefined) {
return null;
}
const sectionFractions = this.viewManager.getSectionFractions();
const start = sectionFractions[sectionIndex];
const end = sectionFractions[sectionIndex + 1];
if (start === undefined || end === undefined || end <= start) {
return null;
}
const normalizedProgress = Math.min(
Math.max(chapterProgressPercent, 0),
EbookReaderComponent.MAX_CHAPTER_PROGRESS_PERCENT
) / 100;
return start + ((end - start) * normalizedProgress);
}
private normalizeHref(href: string | null | undefined): string {
return (href ?? '').split('#')[0].replace(/^(\.\/|\/)+/, '');
}
private hrefsMatch(leftHref: string, rightHref: string): boolean {
return leftHref === rightHref
|| leftHref.endsWith(`/${rightHref}`)
|| rightHref.endsWith(`/${leftHref}`);
}
private updateBookmarkIndicator(): void {
const currentCfi = this.progressService.currentCfi;
const isBookmarked = currentCfi
@@ -60,6 +60,16 @@
<br>
<strong>{{ t('kobo.twoWaySyncNote') }}</strong>
</p>
@if (syncForm.controls.twoWayProgressSync.value && !koboSettings.convertToKepub) {
<p class="setting-warning" role="status" aria-live="polite" aria-atomic="true">
<i class="pi pi-exclamation-triangle" aria-hidden="true"></i>
@if (isAdmin) {
{{ t('kobo.twoWaySyncKepubWarningAdmin') }}
} @else {
{{ t('kobo.twoWaySyncKepubWarning') }}
}
</p>
}
</div>
</div>
}
@@ -118,6 +118,12 @@
line-height: 1.5;
margin: 0;
}
.setting-warning {
@include settings.settings-warning-notice;
margin-top: 0.75rem;
}
}
.setting-control {
@@ -91,10 +91,10 @@ describe('KoboSyncSettingsComponent', () => {
const component = fixture.componentInstance;
TestBed.flushEffects();
expect(component.koboSettings.convertToKepub).toBe(false);
expect(component.koboSettings.convertToKepub).toBe(true);
appSettingsState.set(buildAppSettings({
convertToKepub: true,
convertToKepub: false,
conversionLimitInMb: 42,
convertCbxToEpub: true,
conversionImageCompressionPercentage: 73,
@@ -106,7 +106,7 @@ describe('KoboSyncSettingsComponent', () => {
TestBed.flushEffects();
expect(component.koboSettings).toEqual({
convertToKepub: true,
convertToKepub: false,
conversionLimitInMb: 42,
convertCbxToEpub: true,
conversionImageCompressionPercentage: 73,
@@ -58,7 +58,7 @@ export class KoboSyncSettingsComponent implements OnInit {
});
koboSettings: KoboSettings = {
convertToKepub: false,
convertToKepub: true,
conversionLimitInMb: 100,
convertCbxToEpub: false,
conversionImageCompressionPercentage: 85,
@@ -110,6 +110,16 @@ export class KoboSyncSettingsComponent implements OnInit {
protected hasHydratedKoboAdmin = false;
private readonly syncKepubSettingEffect = effect(() => {
const user = this.userService.currentUser();
const settings = this.appSettingsService.appSettings();
if (!user?.permissions.canSyncKobo || !settings) {
return;
}
this.koboSettings.convertToKepub = settings.koboSettings?.convertToKepub ?? true;
});
private readonly syncAdminSettingsEffect = effect(() => {
const user = this.userService.currentUser();
const settings = this.appSettingsService.appSettings();