feat: select clicked elements

This commit is contained in:
karishmas6
2024-06-25 22:39:29 +05:30
parent d5b4dd368a
commit 6e927c3cb5

View File

@@ -58,7 +58,7 @@ export const BrowserWindow = () => {
const handleClick = useCallback(() => { const handleClick = useCallback(() => {
if (highlighterData) { if (highlighterData) {
setSelectedElement(prev => [...prev, highlighterData]); setSelectedElements(prev => [...prev, highlighterData]);
} }
}, [highlighterData]); }, [highlighterData]);
@@ -76,6 +76,27 @@ export const BrowserWindow = () => {
}; };
}, [socket, onMouseMove, handleClick]); }, [socket, onMouseMove, handleClick]);
// Adjust selected elements' positions after scroll
useEffect(() => {
const handleScroll = () => {
if (canvasRef && canvasRef.current) {
const canvasRect = canvasRef.current.getBoundingClientRect();
setSelectedElements(prev => prev.map(element => ({
...element,
rect: new DOMRect(
element.rect.x,
element.rect.y,
element.rect.width,
element.rect.height
)
})));
}
};
window.addEventListener('scroll', handleScroll);
return () => window.removeEventListener('scroll', handleScroll);
}, [canvasRef]);
return ( return (
<> <>
{(highlighterData?.rect != null && highlighterData?.rect.top != null) && canvasRef?.current ? {(highlighterData?.rect != null && highlighterData?.rect.top != null) && canvasRef?.current ?
@@ -88,16 +109,19 @@ export const BrowserWindow = () => {
isSelected={false} isSelected={false}
/> />
: null} : null}
{selectedElement && canvasRef?.current ? {selectedElements.map((element, index) => (
<Highlighter canvasRef?.current ?
unmodifiedRect={selectedElement?.rect} <Highlighter
displayedSelector={selectedElement?.selector} key={index}
width={width} unmodifiedRect={element?.rect}
height={height} displayedSelector={element?.selector}
canvasRect={canvasRef.current.getBoundingClientRect()} width={width}
isSelected={false} height={height}
/> canvasRect={canvasRef.current.getBoundingClientRect()}
: null} isSelected={false}
/>
: null
))}
<Canvas <Canvas
onCreateRef={setCanvasReference} onCreateRef={setCanvasReference}
width={width} width={width}