Fix click to page flip not responsive occasionally

This commit is contained in:
chrox
2024-12-09 13:46:30 +01:00
parent 515a133731
commit d940fb8d06
3 changed files with 11 additions and 9 deletions
@@ -1,9 +1,9 @@
import React, { useEffect, useRef } from 'react';
import { useFoliateEvents } from '../hooks/useFoliateEvents';
import { BookDoc } from '@/libs/document';
import { BookConfig, BookNote, BookSearchConfig, BookSearchResult } from '@/types/book';
import { useReaderStore } from '@/store/readerStore';
import { useParallelViewStore } from '@/store/parallelViewStore';
import { useFoliateEvents } from '../hooks/useFoliateEvents';
import { getOSPlatform } from '@/utils/misc';
import { getStyles } from '@/utils/style';
import { useTheme } from '@/hooks/useTheme';
@@ -118,6 +118,7 @@ const FoliateViewer: React.FC<{
const docLoadHandler = (event: Event) => {
const detail = (event as CustomEvent).detail;
console.log('doc loaded:', detail);
if (detail.doc) {
const viewSettings = getViewSettings(bookKey)!;
if (viewSettings.scrolled && shouldAutoHideScrollbar) {
@@ -237,11 +238,12 @@ const FoliateViewer: React.FC<{
const view = wrappedFoliateView(document.createElement('foliate-view') as FoliateView);
document.body.append(view);
containerRef.current?.appendChild(view);
setFoliateView(bookKey, view);
await view.open(bookDoc);
// make sure we can listen renderer events after opening book
viewRef.current = view;
setFoliateView(bookKey, view);
const viewSettings = getViewSettings(bookKey)!;
view.renderer.setStyles?.(getStyles(viewSettings, themeCode));
@@ -105,7 +105,7 @@ const Annotator: React.FC<{ bookKey: string }> = ({ bookKey }) => {
setSelection(selection);
};
useFoliateEvents(view, { onLoad, onDrawAnnotation, onShowAnnotation }, [config]);
useFoliateEvents(view, { onLoad, onDrawAnnotation, onShowAnnotation });
const handleDismissPopup = () => {
setSelection(null);
@@ -4,18 +4,16 @@ import { FoliateView } from '@/app/reader/components/FoliateViewer';
type FoliateEventHandler = {
onLoad?: (event: Event) => void;
onRelocate?: (event: Event) => void;
onLinkClick?: (event: Event) => void;
onRendererRelocate?: (event: Event) => void;
onDrawAnnotation?: (event: Event) => void;
onShowAnnotation?: (event: Event) => void;
};
export const useFoliateEvents = (
view: FoliateView | null,
handlers?: FoliateEventHandler,
dependencies: React.DependencyList = [],
) => {
export const useFoliateEvents = (view: FoliateView | null, handlers?: FoliateEventHandler) => {
const onLoad = handlers?.onLoad;
const onRelocate = handlers?.onRelocate;
const onLinkClick = handlers?.onLinkClick;
const onRendererRelocate = handlers?.onRendererRelocate;
const onDrawAnnotation = handlers?.onDrawAnnotation;
const onShowAnnotation = handlers?.onShowAnnotation;
@@ -24,6 +22,7 @@ export const useFoliateEvents = (
if (!view) return;
if (onLoad) view.addEventListener('load', onLoad);
if (onRelocate) view.addEventListener('relocate', onRelocate);
if (onLinkClick) view.addEventListener('link', onLinkClick);
if (onRendererRelocate) view.renderer.addEventListener('relocate', onRendererRelocate);
if (onDrawAnnotation) view.addEventListener('draw-annotation', onDrawAnnotation);
if (onShowAnnotation) view.addEventListener('show-annotation', onShowAnnotation);
@@ -31,10 +30,11 @@ export const useFoliateEvents = (
return () => {
if (onLoad) view.removeEventListener('load', onLoad);
if (onRelocate) view.removeEventListener('relocate', onRelocate);
if (onLinkClick) view.removeEventListener('link', onLinkClick);
if (onRendererRelocate) view.renderer.removeEventListener('relocate', onRendererRelocate);
if (onDrawAnnotation) view.removeEventListener('draw-annotation', onDrawAnnotation);
if (onShowAnnotation) view.removeEventListener('show-annotation', onShowAnnotation);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [view, ...dependencies]);
}, [view]);
};