feat(sync): add readest koplugin for progress sync, also implements #1729 (#1785)

This commit is contained in:
Huang Xin
2025-08-13 00:24:47 +08:00
committed by GitHub
parent 3698e6ca28
commit 0dfa8e96f5
11 changed files with 291 additions and 175 deletions
@@ -7,14 +7,12 @@ import { useReaderStore } from '@/store/readerStore';
import { useBookDataStore } from '@/store/bookDataStore';
import { useTranslation } from '@/hooks/useTranslation';
import { KOSyncClient, KoSyncProgress } from '@/services/sync/KOSyncClient';
import { Book, BookFormat } from '@/types/book';
import { Book, FIXED_LAYOUT_FORMATS } from '@/types/book';
import { BookDoc } from '@/libs/document';
import { debounce } from '@/utils/debounce';
import { eventDispatcher } from '@/utils/event';
import { getCFIFromXPointer, XCFI } from '@/utils/xcfi';
const PAGINATED_FORMATS: Set<BookFormat> = new Set(['PDF', 'CBZ']);
type SyncState = 'idle' | 'checking' | 'conflict' | 'synced' | 'error';
export interface SyncDetails {
@@ -62,7 +60,7 @@ export const useKOSync = (bookKey: string) => {
let progressStr: string;
let percentage: number;
if (PAGINATED_FORMATS.has(currentBook.format)) {
if (FIXED_LAYOUT_FORMATS.has(currentBook.format)) {
const page = (currentProgress.section?.current ?? 0) + 1;
const totalPages = currentProgress.section?.total ?? 0;
progressStr = page.toString();
@@ -223,12 +221,12 @@ export const useKOSync = (bookKey: string) => {
const localTimestamp = bookData?.config?.updatedAt || book.updatedAt;
const remoteIsNewer = remote.timestamp * 1000 > localTimestamp;
const localIdentifier = PAGINATED_FORMATS.has(book.format)
const localIdentifier = FIXED_LAYOUT_FORMATS.has(book.format)
? progress.section?.current.toString()
: progress.location;
const isLocalCFI = localIdentifier?.startsWith('epubcfi');
const remoteIdentifier = PAGINATED_FORMATS.has(book.format)
const remoteIdentifier = FIXED_LAYOUT_FORMATS.has(book.format)
? (parseInt(remote.progress, 10) - 1).toString()
: remote.progress.startsWith('epubcfi')
? remote.progress
@@ -265,7 +263,7 @@ export const useKOSync = (bookKey: string) => {
const applyRemoteProgress = async () => {
const view = getView(bookKey);
if (view && remote.progress) {
if (PAGINATED_FORMATS.has(book.format)) {
if (FIXED_LAYOUT_FORMATS.has(book.format)) {
const pageToGo = parseInt(remote.progress, 10);
if (!isNaN(pageToGo)) view.select(pageToGo - 1);
} else {
@@ -312,7 +310,7 @@ export const useKOSync = (bookKey: string) => {
let remotePreview = '';
const remotePercentage = remote.percentage || 0;
if (PAGINATED_FORMATS.has(book.format)) {
if (FIXED_LAYOUT_FORMATS.has(book.format)) {
const localPageInfo = progress.section;
const localPercentage =
localPageInfo && localPageInfo.total > 0
@@ -427,7 +425,7 @@ export const useKOSync = (bookKey: string) => {
const bookDoc = conflictDetails?.bookDoc;
if (view && remote?.progress && currentBook) {
if (PAGINATED_FORMATS.has(currentBook.format)) {
if (FIXED_LAYOUT_FORMATS.has(currentBook.format)) {
const localTotalPages = getProgress(bookKey)?.section?.total ?? 0;
const remotePage = parseInt(remote.progress, 10);
const remotePercentage = remote.percentage || 0;