Files
parcer/src/components/atoms/canvas.tsx

175 lines
6.7 KiB
TypeScript
Raw Normal View History

2024-07-23 10:19:11 +05:30
import React, { useCallback, useEffect, useRef, useState } from 'react';
2024-06-14 21:56:50 +05:30
import { useSocketStore } from '../../context/socket';
import { getMappedCoordinates } from "../../helpers/inputHelpers";
import { useGlobalInfoStore } from "../../context/globalInfo";
2024-07-23 10:19:11 +05:30
import { GenericModal } from '../atoms/GenericModal';
import { Box, Button, Typography } from '@mui/material';
2024-06-14 21:56:50 +05:30
interface CreateRefCallback {
(ref: React.RefObject<HTMLCanvasElement>): void;
}
2024-07-23 20:07:45 +05:30
/**
* Interface for mouse's x,y coordinates
*/
2024-06-14 21:56:50 +05:30
interface CanvasProps {
width: number;
height: number;
onCreateRef: CreateRefCallback;
2024-07-23 10:19:11 +05:30
highlighterData: { rect: DOMRect, selector: string } | null;
2024-06-14 21:56:50 +05:30
}
export interface Coordinates {
x: number;
y: number;
2024-07-23 10:19:11 +05:30
}
2024-06-14 21:56:50 +05:30
2024-07-23 10:19:11 +05:30
const ConfirmationBox = ({ selector, onYes, onNo }: { selector: string; onYes: () => void; onNo: () => void }) => {
return (
<Box sx={{ textAlign: 'center' }}>
<Typography variant="h6" component="h2" gutterBottom>
Confirmation
</Typography>
<Typography variant="body1" gutterBottom>
Do you want to interact with the element: {selector}?
</Typography>
<Box sx={{ mt: 2, display: 'flex', justifyContent: 'center', gap: 2 }}>
<Button variant="contained" color="primary" onClick={onYes}>
Yes
</Button>
<Button variant="contained" color="secondary" onClick={onNo}>
No
</Button>
</Box>
</Box>
);
};
2024-06-14 21:56:50 +05:30
2024-07-23 10:19:11 +05:30
const Canvas = ({ width, height, onCreateRef, highlighterData }: CanvasProps) => {
2024-06-14 21:56:50 +05:30
const canvasRef = useRef<HTMLCanvasElement>(null);
const { socket } = useSocketStore();
2024-07-23 20:08:40 +05:30
const { setLastAction, lastAction } = useGlobalInfoStore();
2024-07-23 10:19:11 +05:30
const [showConfirmation, setShowConfirmation] = useState(false);
const [pendingClick, setPendingClick] = useState<Coordinates | null>(null);
2024-06-14 21:56:50 +05:30
const lastMousePosition = useRef<Coordinates>({ x: 0, y: 0 });
2024-07-23 20:08:40 +05:30
const notifyLastAction = (action: string) => {
if (lastAction !== action) {
setLastAction(action);
}
};
2024-06-14 21:56:50 +05:30
const onMouseEvent = useCallback((event: MouseEvent) => {
2024-07-23 20:06:14 +05:30
if (socket && canvasRef.current) {
2024-07-23 10:19:11 +05:30
const coordinates = getMappedCoordinates(event, canvasRef.current, width, height);
2024-07-23 20:06:14 +05:30
2024-06-14 21:56:50 +05:30
switch (event.type) {
case 'mousemove':
if (lastMousePosition.current.x !== coordinates.x ||
2024-06-15 21:09:22 +05:30
lastMousePosition.current.y !== coordinates.y) {
2024-07-23 20:06:14 +05:30
lastMousePosition.current = coordinates;
socket.emit('input:mousemove', coordinates);
2024-07-23 20:09:30 +05:30
notifyLastAction('move');
2024-07-23 20:06:14 +05:30
}
break;
case 'mousedown':
if (highlighterData) {
const highlightRect = highlighterData.rect;
if (
coordinates.x >= highlightRect.left &&
coordinates.x <= highlightRect.right &&
coordinates.y >= highlightRect.top &&
coordinates.y <= highlightRect.bottom
) {
setPendingClick(coordinates);
setShowConfirmation(true);
} else {
socket.emit('input:mousedown', coordinates);
2024-07-23 20:09:30 +05:30
notifyLastAction('click');
2024-07-23 20:06:14 +05:30
}
} else {
socket.emit('input:mousedown', coordinates);
2024-07-23 20:09:30 +05:30
notifyLastAction('click');
2024-06-14 21:56:50 +05:30
}
break;
case 'wheel':
const wheelEvent = event as WheelEvent;
const deltas = {
deltaX: Math.round(wheelEvent.deltaX),
deltaY: Math.round(wheelEvent.deltaY),
};
socket.emit('input:wheel', deltas);
2024-07-23 20:09:30 +05:30
notifyLastAction('scroll');
2024-06-14 21:56:50 +05:30
break;
}
}
2024-07-23 20:06:14 +05:30
}, [socket, width, height, setLastAction, highlighterData]);
2024-06-14 21:56:50 +05:30
const onKeyboardEvent = useCallback((event: KeyboardEvent) => {
if (socket) {
switch (event.type) {
case 'keydown':
socket.emit('input:keydown', { key: event.key, coordinates: lastMousePosition.current });
2024-07-23 20:09:30 +05:30
notifyLastAction(`${event.key} pressed`);
2024-06-14 21:56:50 +05:30
break;
case 'keyup':
socket.emit('input:keyup', event.key);
break;
}
}
2024-07-23 10:19:11 +05:30
}, [socket, setLastAction]);
const handleConfirmation = (confirmed: boolean) => {
2024-07-23 20:06:14 +05:30
if (confirmed && pendingClick && socket) {
socket.emit('input:mousedown', pendingClick);
2024-07-23 20:09:30 +05:30
notifyLastAction('click');
2024-07-23 10:19:11 +05:30
}
setShowConfirmation(false);
setPendingClick(null);
};
2024-06-14 21:56:50 +05:30
useEffect(() => {
if (canvasRef.current) {
onCreateRef(canvasRef);
2024-07-23 20:06:14 +05:30
canvasRef.current.addEventListener('mousedown', onMouseEvent);
2024-06-14 21:56:50 +05:30
canvasRef.current.addEventListener('mousemove', onMouseEvent);
canvasRef.current.addEventListener('wheel', onMouseEvent, { passive: true });
canvasRef.current.addEventListener('keydown', onKeyboardEvent);
canvasRef.current.addEventListener('keyup', onKeyboardEvent);
return () => {
if (canvasRef.current) {
2024-07-23 20:06:14 +05:30
canvasRef.current.removeEventListener('mousedown', onMouseEvent);
2024-06-14 21:56:50 +05:30
canvasRef.current.removeEventListener('mousemove', onMouseEvent);
canvasRef.current.removeEventListener('wheel', onMouseEvent);
canvasRef.current.removeEventListener('keydown', onKeyboardEvent);
canvasRef.current.removeEventListener('keyup', onKeyboardEvent);
}
};
}
2024-07-23 20:06:14 +05:30
}, [onMouseEvent, onKeyboardEvent, onCreateRef]);
2024-06-14 21:56:50 +05:30
return (
2024-07-23 10:19:11 +05:30
<>
<GenericModal
isOpen={showConfirmation}
onClose={() => setShowConfirmation(false)}
canBeClosed={false}
>
<ConfirmationBox
selector={highlighterData?.selector || ''}
onYes={() => handleConfirmation(true)}
onNo={() => handleConfirmation(false)}
/>
</GenericModal>
<canvas
tabIndex={0}
ref={canvasRef}
height={720}
width={1280}
/>
</>
2024-06-14 21:56:50 +05:30
);
};
2024-07-13 22:21:42 +05:30
export default Canvas;