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

180 lines
6.2 KiB
TypeScript
Raw Normal View History

import React, { useCallback, useEffect, useRef } 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-25 22:44:18 +05:30
import { useActionContext } from '../../context/browserActions';
import DatePicker from './DatePicker';
2024-06-14 21:56:50 +05:30
interface CreateRefCallback {
(ref: React.RefObject<HTMLCanvasElement>): void;
}
2024-06-14 21:56:50 +05:30
interface CanvasProps {
width: number;
height: number;
onCreateRef: CreateRefCallback;
}
/**
* Interface for mouse's x,y coordinates
*/
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
const Canvas = ({ width, height, onCreateRef }: 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-08-08 00:41:32 +05:30
const { getText, getList } = useActionContext();
const getTextRef = useRef(getText);
2024-08-08 00:41:32 +05:30
const getListRef = useRef(getList);
2024-06-14 21:56:50 +05:30
const [datePickerInfo, setDatePickerInfo] = React.useState<{
coordinates: Coordinates;
selector: string;
} | null>(null);
2024-07-23 20:08:40 +05:30
const notifyLastAction = (action: string) => {
if (lastAction !== action) {
setLastAction(action);
}
};
const lastMousePosition = useRef<Coordinates>({ x: 0, y: 0 });
2024-07-25 22:44:50 +05:30
useEffect(() => {
getTextRef.current = getText;
2024-08-08 00:41:32 +05:30
getListRef.current = getList;
}, [getText, getList]);
useEffect(() => {
if (socket) {
socket.on('showDatePicker', (info: {coordinates: Coordinates, selector: string}) => {
setDatePickerInfo(info);
});
return () => {
socket.off('showDatePicker');
};
}
}, [socket]);
const onMouseEvent = useCallback((event: MouseEvent) => {
2024-10-18 16:48:26 +05:30
if (socket && canvasRef.current) {
// Get the canvas bounding rectangle
const rect = canvasRef.current.getBoundingClientRect();
const clickCoordinates = {
x: event.clientX - rect.left, // Use relative x coordinate
y: event.clientY - rect.top, // Use relative y coordinate
};
2024-06-14 21:56:50 +05:30
switch (event.type) {
case 'mousedown':
2024-09-09 05:19:15 +05:30
if (getTextRef.current === true) {
console.log('Capturing Text...');
2024-10-18 16:48:26 +05:30
} else if (getListRef.current === true) {
2024-09-09 05:19:15 +05:30
console.log('Capturing List...');
2024-10-18 16:48:26 +05:30
} else {
2024-07-25 22:44:18 +05:30
socket.emit('input:mousedown', clickCoordinates);
}
2024-07-26 04:26:07 +05:30
notifyLastAction('click');
break;
2024-06-14 21:56:50 +05:30
case 'mousemove':
2024-10-18 16:48:26 +05:30
if (lastMousePosition.current.x !== clickCoordinates.x ||
lastMousePosition.current.y !== clickCoordinates.y) {
lastMousePosition.current = {
2024-10-18 16:48:26 +05:30
x: clickCoordinates.x,
y: clickCoordinates.y,
};
socket.emit('input:mousemove', {
2024-10-18 16:48:26 +05:30
x: clickCoordinates.x,
y: clickCoordinates.y,
});
2024-07-23 20:09:30 +05:30
notifyLastAction('move');
2024-07-23 20:06:14 +05:30
}
break;
2024-06-14 21:56:50 +05:30
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;
default:
console.log('Default mouseEvent registered');
return;
2024-06-14 21:56:50 +05:30
}
}
}, [socket]);
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;
default:
console.log('Default keyEvent registered');
return;
2024-06-14 21:56:50 +05:30
}
}
}, [socket]);
2024-07-23 10:19:11 +05:30
2024-06-14 21:56:50 +05:30
useEffect(() => {
if (canvasRef.current) {
onCreateRef(canvasRef);
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) {
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-06-14 21:56:50 +05:30
};
} else {
console.log('Canvas not initialized');
2024-06-14 21:56:50 +05:30
}
}, [onMouseEvent]);
2024-06-14 21:56:50 +05:30
return (
2024-10-23 08:01:38 +05:30
<div style={{ borderRadius: '0px 0px 5px 5px', overflow: 'hidden', backgroundColor: 'white' }}>
<canvas
tabIndex={0}
ref={canvasRef}
height={400}
width={900}
style={{ display: 'block' }}
/>
{datePickerInfo && (
<DatePicker
coordinates={datePickerInfo.coordinates}
selector={datePickerInfo.selector}
onClose={() => setDatePickerInfo(null)}
/>
)}
2024-10-23 08:01:38 +05:30
</div>
2024-06-14 21:56:50 +05:30
);
2024-06-14 21:56:50 +05:30
};
2024-07-13 22:21:42 +05:30
export default Canvas;