Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-09-11 22:51:10 +01:00
import { useEffect, useState } from 'react';
import { usePan } from '@embedpdf/plugin-pan/react';
/**
2025-09-12 16:26:05 +01:00
* Component that runs inside EmbedPDF context and bridges pan controls to global window
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();
const [panStateListeners, setPanStateListeners] = useState<Array<(isPanning: boolean) => void>>([]);
useEffect(() => {
if (pan) {
// Export pan controls to global window for right rail access
(window as any).embedPdfPan = {
enablePan: () => {
console.log('EmbedPDF: Enabling pan mode');
pan.enablePan();
},
disablePan: () => {
console.log('EmbedPDF: Disabling pan mode');
pan.disablePan();
},
togglePan: () => {
pan.togglePan();
},
makePanDefault: () => pan.makePanDefault(),
2025-09-12 15:06:06 +01:00
get isPanning() { return isPanning; }, // Use getter to always return current value
2025-09-11 22:51:10 +01:00
// Subscribe to pan state changes for reactive UI
onPanStateChange: (callback: (isPanning: boolean) => void) => {
setPanStateListeners(prev => [...prev, callback]);
// Return unsubscribe function
return () => {
setPanStateListeners(prev => prev.filter(cb => cb !== callback));
};
},
};
2025-09-12 16:26:05 +01:00
2025-09-11 22:51:10 +01:00
}
}, [pan, isPanning]);
// Notify all listeners when isPanning state changes
useEffect(() => {
panStateListeners.forEach(callback => callback(isPanning));
}, [isPanning, panStateListeners]);
2025-09-12 16:26:05 +01:00
return null;
}