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

149 lines
5.1 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';
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
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]);
const onMouseEvent = useCallback((event: MouseEvent) => {
if (socket) {
const coordinates = {
x: event.clientX,
y: event.clientY,
}
2024-06-14 21:56:50 +05:30
switch (event.type) {
case 'mousedown':
const clickCoordinates = getMappedCoordinates(event, canvasRef.current, width, height);
if (getTextRef.current === true || getListRef.current === true) {
console.log('get text or get list is true');
} 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':
const coordinates = getMappedCoordinates(event, canvasRef.current, width, height);
2024-06-14 21:56:50 +05:30
if (lastMousePosition.current.x !== coordinates.x ||
2024-06-15 21:09:22 +05:30
lastMousePosition.current.y !== coordinates.y) {
lastMousePosition.current = {
x: coordinates.x,
y: coordinates.y,
};
socket.emit('input:mousemove', {
x: coordinates.x,
y: coordinates.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 (
<canvas
tabIndex={0}
ref={canvasRef}
height={720}
width={1280}
/>
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;