2025-09-16 19:36:36 +01:00
|
|
|
import { useEffect, useRef, useState } from 'react';
|
2025-09-11 19:08:44 +01:00
|
|
|
import { useZoom } from '@embedpdf/plugin-zoom/react';
|
2025-09-16 19:36:36 +01:00
|
|
|
import { useViewer } from '../../contexts/ViewerContext';
|
2025-09-11 19:08:44 +01:00
|
|
|
|
|
|
|
/**
|
2025-09-16 19:36:36 +01:00
|
|
|
* Component that runs inside EmbedPDF context and manages zoom state locally
|
2025-09-11 19:08:44 +01:00
|
|
|
*/
|
2025-09-12 16:26:05 +01:00
|
|
|
export function ZoomAPIBridge() {
|
2025-09-11 19:08:44 +01:00
|
|
|
const { provides: zoom, state: zoomState } = useZoom();
|
2025-09-16 19:36:36 +01:00
|
|
|
const { registerBridge } = useViewer();
|
2025-09-15 18:20:11 +01:00
|
|
|
const hasSetInitialZoom = useRef(false);
|
2025-09-16 19:36:36 +01:00
|
|
|
|
|
|
|
// Store state locally
|
|
|
|
const [_localState, setLocalState] = useState({
|
|
|
|
currentZoom: 1.4,
|
|
|
|
zoomPercent: 140
|
|
|
|
});
|
2025-09-15 18:20:11 +01:00
|
|
|
|
|
|
|
// Set initial zoom once when plugin is ready
|
|
|
|
useEffect(() => {
|
|
|
|
if (zoom && !hasSetInitialZoom.current) {
|
|
|
|
hasSetInitialZoom.current = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
console.log('Setting initial zoom to 140%');
|
|
|
|
zoom.requestZoom(1.4);
|
|
|
|
}, 50);
|
|
|
|
}
|
|
|
|
}, [zoom]);
|
2025-09-11 19:08:44 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2025-09-16 19:36:36 +01:00
|
|
|
if (zoom && zoomState) {
|
|
|
|
// Update local state
|
|
|
|
const newState = {
|
|
|
|
currentZoom: zoomState.currentZoomLevel || 1.4,
|
|
|
|
zoomPercent: Math.round((zoomState.currentZoomLevel || 1.4) * 100),
|
2025-09-11 19:08:44 +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('zoom', {
|
|
|
|
state: newState,
|
|
|
|
api: zoom
|
|
|
|
});
|
2025-09-11 19:08:44 +01:00
|
|
|
}
|
2025-09-16 19:36:36 +01:00
|
|
|
}, [zoom, zoomState, registerBridge]);
|
2025-09-11 19:08:44 +01:00
|
|
|
|
2025-09-12 16:26:05 +01:00
|
|
|
return null;
|
|
|
|
}
|