forked from akai/readest
25 lines
749 B
TypeScript
25 lines
749 B
TypeScript
import { useEffect } from 'react';
|
|
import { FoliateView } from '@/app/reader/components/FoliateViewer';
|
|
|
|
type FoliateEventHandler = {
|
|
onLoad?: (event: Event) => void;
|
|
onRelocate?: (event: Event) => void;
|
|
};
|
|
|
|
export const useFoliateEvents = (view: FoliateView | null, handlers?: FoliateEventHandler) => {
|
|
const onLoad = handlers?.onLoad;
|
|
const onRelocate = handlers?.onRelocate;
|
|
|
|
useEffect(() => {
|
|
if (!view) return;
|
|
|
|
if (onLoad) view.addEventListener('load', onLoad);
|
|
if (onRelocate) view.addEventListener('relocate', onRelocate);
|
|
|
|
return () => {
|
|
if (onLoad) view.removeEventListener('load', onLoad);
|
|
if (onRelocate) view.removeEventListener('relocate', onRelocate);
|
|
};
|
|
}, [view, onLoad, onRelocate]);
|
|
};
|