2025-09-11 22:51:10 +01:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import { usePan } from '@embedpdf/plugin-pan/react';
|
2025-09-16 19:36:36 +01:00
|
|
|
import { useViewer } from '../../contexts/ViewerContext';
|
2025-09-11 22:51:10 +01:00
|
|
|
|
|
|
|
/**
|
2025-09-16 19:36:36 +01:00
|
|
|
* Component that runs inside EmbedPDF context and updates pan state in ViewerContext
|
2025-09-11 22:51:10 +01:00
|
|
|
*/
|
2025-09-12 16:26:05 +01:00
|
|
|
export function PanAPIBridge() {
|
2025-09-11 22:51:10 +01:00
|
|
|
const { provides: pan, isPanning } = usePan();
|
2025-09-16 19:36:36 +01:00
|
|
|
const { registerBridge } = useViewer();
|
|
|
|
|
|
|
|
// Store state locally
|
|
|
|
const [_localState, setLocalState] = useState({
|
|
|
|
isPanning: false
|
|
|
|
});
|
2025-09-11 22:51:10 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (pan) {
|
2025-09-16 19:36:36 +01:00
|
|
|
// Update local state
|
|
|
|
const newState = {
|
|
|
|
isPanning
|
2025-09-11 22:51:10 +01:00
|
|
|
};
|
2025-09-16 19:36:36 +01:00
|
|
|
setLocalState(newState);
|
2025-09-12 16:26:05 +01:00
|
|
|
|
2025-09-16 19:36:36 +01:00
|
|
|
// Register this bridge with ViewerContext
|
|
|
|
registerBridge('pan', {
|
|
|
|
state: newState,
|
|
|
|
api: {
|
|
|
|
enable: () => {
|
|
|
|
pan.enablePan();
|
|
|
|
},
|
|
|
|
disable: () => {
|
|
|
|
pan.disablePan();
|
|
|
|
},
|
|
|
|
toggle: () => {
|
|
|
|
pan.togglePan();
|
|
|
|
},
|
|
|
|
makePanDefault: () => pan.makePanDefault(),
|
|
|
|
}
|
|
|
|
});
|
2025-09-11 22:51:10 +01:00
|
|
|
}
|
2025-09-17 12:00:20 +01:00
|
|
|
}, [pan, isPanning]);
|
2025-09-11 22:51:10 +01:00
|
|
|
|
2025-09-12 16:26:05 +01:00
|
|
|
return null;
|
|
|
|
}
|