fix(reader): resolve KOReader sync conflict against non-KOReader servers (#4205)

The sync-conflict dialog had two issues with servers other than KOReader
(e.g. Kavita's KOReader-compatible sync endpoint):

- "This device" preview rendered a bare "undefined" because reflowable
  books built the string from `sectionLabel`, which is empty for spine
  items with no matching TOC entry. It now falls back to the page count.
- Choosing "use remote" closed the dialog but never moved the reader:
  `applyRemoteProgress` only knew how to navigate via CREngine XPointers,
  so non-XPointer progress strings were silently ignored. It now falls
  back to `view.goToFraction` using the reported percentage.

Also fixes the section-title indentation in the dialog (SectionTitle
bakes in `ps-4`, which misaligned the labels against their values).

Closes #4200

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Huang Xin
2026-05-17 23:22:34 +08:00
committed by GitHub
parent 9f0aa2f55d
commit c8fabd331c
6 changed files with 220 additions and 37 deletions
@@ -10,6 +10,8 @@ import { BookDoc } from '@/libs/document';
import { debounce } from '@/utils/debounce';
import { eventDispatcher } from '@/utils/event';
import { getCFIFromXPointer, XCFI } from '@/utils/xcfi';
import { getLocalProgressPreview, getProgressPercentage } from './kosyncPreview';
import { getRemoteFraction, isXPointerProgress } from './kosyncProgress';
import { useWindowActiveChanged } from './useWindowActiveChanged';
type SyncState = 'idle' | 'checking' | 'conflict' | 'synced' | 'error';
@@ -101,22 +103,42 @@ export const useKOSync = (bookKey: string) => {
if (FIXED_LAYOUT_FORMATS.has(book.format)) {
const pageToGo = parseInt(remote.progress!, 10);
if (isNaN(pageToGo)) return;
view?.select(pageToGo - 1);
view.select(pageToGo - 1);
} else {
if (!remote.progress?.startsWith('/body')) return;
try {
const content = view?.renderer
.getContents()
.find((x) => x.index === view?.renderer.primaryIndex);
const koProgress = remote.progress;
const cfi = await getCFIFromXPointer(koProgress, content?.doc, content?.index, bookDoc);
view?.goTo(cfi);
} catch (error) {
console.error('Failed to convert XPointer to CFI', error);
return;
let navigated = false;
// KOReader stores positions as CREngine XPointers; convert and jump
// precisely when we have one.
if (isXPointerProgress(remote.progress)) {
try {
const content = view.renderer
.getContents()
.find((x) => x.index === view.renderer.primaryIndex);
const cfi = await getCFIFromXPointer(
remote.progress!,
content?.doc,
content?.index,
bookDoc,
);
view.goTo(cfi);
navigated = true;
} catch (error) {
console.error('Failed to convert XPointer to CFI', error);
}
}
// Other KOSync-compatible servers (e.g. Kavita) report progress in
// formats Readest can't resolve positionally — approximate with the
// reported percentage so "use remote" still moves the reader.
if (!navigated) {
const remoteFraction = getRemoteFraction(remote);
if (remoteFraction === undefined) return;
view.goToFraction(remoteFraction);
}
}
eventDispatcher.dispatch('toast', { message: _('Reading Progress Synced'), type: 'info' });
eventDispatcher.dispatch('toast', {
message: _('Reading Progress Synced'),
type: 'info',
timeout: 2000,
});
};
const promptedSync = async (
@@ -125,26 +147,17 @@ export const useKOSync = (bookKey: string) => {
local: BookProgress,
remote: KoSyncProgress,
) => {
let localPreview = '';
let remotePreview = '';
const remotePercentage = remote.percentage || 0;
const conflictProgressDiffThreshold = 0.0001;
let showConflictDetails = false;
const isFixedLayout = FIXED_LAYOUT_FORMATS.has(book.format);
if (FIXED_LAYOUT_FORMATS.has(book.format)) {
const localPreview = getLocalProgressPreview(local, isFixedLayout, _);
const localPercentage = getProgressPercentage(isFixedLayout ? local.section : local.pageinfo);
if (isFixedLayout) {
const localPageInfo = local.section;
const localPercentage =
localPageInfo && localPageInfo.total > 0
? (localPageInfo.current + 1) / localPageInfo.total
: 0;
localPreview = localPageInfo
? _('Page {{page}} of {{total}} ({{percentage}}%)', {
page: localPageInfo.current + 1,
total: localPageInfo.total,
percentage: Math.round(localPercentage * 100),
})
: _('Current position');
const remotePage = parseInt(remote.progress!, 10);
if (!isNaN(remotePage) && remotePercentage > 0) {
const localTotalPages = localPageInfo?.total ?? 0;
@@ -172,13 +185,6 @@ export const useKOSync = (bookKey: string) => {
});
}
} else {
const localPageInfo = local.pageinfo;
const localPercentage =
localPageInfo && localPageInfo.total > 0
? (localPageInfo.current + 1) / localPageInfo.total
: 0;
localPreview = `${local.sectionLabel} (${Math.round(localPercentage * 100)}%)`;
remotePreview = _('Approximately {{percentage}}%', {
percentage: Math.round(remotePercentage * 100),
});
@@ -343,7 +349,8 @@ export const useKOSync = (bookKey: string) => {
const book = conflictDetails?.book;
const bookDoc = conflictDetails?.bookDoc;
if (!book || !bookDoc || !remote || !remote.progress || !view) return;
if (!book || !bookDoc || !remote || !view) return;
if (!remote.progress && getRemoteFraction(remote) === undefined) return;
applyRemoteProgress(book, bookDoc, remote);
setSyncState('synced');