fix: revert to !conditional mousedown emit
This commit is contained in:
@@ -1,57 +1,31 @@
|
|||||||
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
import React, { useCallback, useEffect, useRef } from 'react';
|
||||||
import { useSocketStore } from '../../context/socket';
|
import { useSocketStore } from '../../context/socket';
|
||||||
import { getMappedCoordinates } from "../../helpers/inputHelpers";
|
import { getMappedCoordinates } from "../../helpers/inputHelpers";
|
||||||
import { useGlobalInfoStore } from "../../context/globalInfo";
|
import { useGlobalInfoStore } from "../../context/globalInfo";
|
||||||
import { GenericModal } from '../atoms/GenericModal';
|
|
||||||
import { Box, Button, Typography } from '@mui/material';
|
|
||||||
|
|
||||||
interface CreateRefCallback {
|
interface CreateRefCallback {
|
||||||
(ref: React.RefObject<HTMLCanvasElement>): void;
|
(ref: React.RefObject<HTMLCanvasElement>): void;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Interface for mouse's x,y coordinates
|
|
||||||
*/
|
|
||||||
interface CanvasProps {
|
interface CanvasProps {
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
onCreateRef: CreateRefCallback;
|
onCreateRef: CreateRefCallback;
|
||||||
highlighterData: { rect: DOMRect, selector: string } | null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for mouse's x,y coordinates
|
||||||
|
*/
|
||||||
export interface Coordinates {
|
export interface Coordinates {
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
}
|
|
||||||
|
|
||||||
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>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Canvas = ({ width, height, onCreateRef, highlighterData }: CanvasProps) => {
|
const Canvas = ({ width, height, onCreateRef }: CanvasProps) => {
|
||||||
|
|
||||||
const canvasRef = useRef<HTMLCanvasElement>(null);
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||||
const { socket } = useSocketStore();
|
const { socket } = useSocketStore();
|
||||||
const { setLastAction, lastAction } = useGlobalInfoStore();
|
const { setLastAction, lastAction } = useGlobalInfoStore();
|
||||||
const [showConfirmation, setShowConfirmation] = useState(false);
|
|
||||||
const [pendingClick, setPendingClick] = useState<Coordinates | null>(null);
|
|
||||||
|
|
||||||
const lastMousePosition = useRef<Coordinates>({ x: 0, y: 0 });
|
|
||||||
|
|
||||||
const notifyLastAction = (action: string) => {
|
const notifyLastAction = (action: string) => {
|
||||||
if (lastAction !== action) {
|
if (lastAction !== action) {
|
||||||
@@ -59,12 +33,23 @@ const Canvas = ({ width, height, onCreateRef, highlighterData }: CanvasProps) =>
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const onMouseEvent = useCallback((event: MouseEvent) => {
|
const lastMousePosition = useRef<Coordinates>({ x: 0, y: 0 });
|
||||||
if (socket && canvasRef.current) {
|
//const lastWheelPosition = useRef<ScrollDeltas>({ deltaX: 0, deltaY: 0 });
|
||||||
const coordinates = getMappedCoordinates(event, canvasRef.current, width, height);
|
|
||||||
|
|
||||||
|
const onMouseEvent = useCallback((event: MouseEvent) => {
|
||||||
|
if (socket) {
|
||||||
|
const coordinates = {
|
||||||
|
x: event.clientX,
|
||||||
|
y: event.clientY,
|
||||||
|
}
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
|
case 'mousedown':
|
||||||
|
const clickCoordinates = getMappedCoordinates(event, canvasRef.current, width, height);
|
||||||
|
socket.emit('input:mousedown', clickCoordinates);
|
||||||
|
notifyLastAction('click');
|
||||||
|
break;
|
||||||
case 'mousemove':
|
case 'mousemove':
|
||||||
|
const coordinates = getMappedCoordinates(event, canvasRef.current, width, height);
|
||||||
if (lastMousePosition.current.x !== coordinates.x ||
|
if (lastMousePosition.current.x !== coordinates.x ||
|
||||||
lastMousePosition.current.y !== coordinates.y) {
|
lastMousePosition.current.y !== coordinates.y) {
|
||||||
lastMousePosition.current = {
|
lastMousePosition.current = {
|
||||||
@@ -78,26 +63,6 @@ const Canvas = ({ width, height, onCreateRef, highlighterData }: CanvasProps) =>
|
|||||||
notifyLastAction('move');
|
notifyLastAction('move');
|
||||||
}
|
}
|
||||||
break;
|
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);
|
|
||||||
notifyLastAction('click');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
socket.emit('input:mousedown', coordinates);
|
|
||||||
notifyLastAction('click');
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'wheel':
|
case 'wheel':
|
||||||
const wheelEvent = event as WheelEvent;
|
const wheelEvent = event as WheelEvent;
|
||||||
const deltas = {
|
const deltas = {
|
||||||
@@ -107,9 +72,12 @@ const Canvas = ({ width, height, onCreateRef, highlighterData }: CanvasProps) =>
|
|||||||
socket.emit('input:wheel', deltas);
|
socket.emit('input:wheel', deltas);
|
||||||
notifyLastAction('scroll');
|
notifyLastAction('scroll');
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
console.log('Default mouseEvent registered');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [socket, width, height, setLastAction, highlighterData]);
|
}, [socket]);
|
||||||
|
|
||||||
const onKeyboardEvent = useCallback((event: KeyboardEvent) => {
|
const onKeyboardEvent = useCallback((event: KeyboardEvent) => {
|
||||||
if (socket) {
|
if (socket) {
|
||||||
@@ -121,18 +89,13 @@ const Canvas = ({ width, height, onCreateRef, highlighterData }: CanvasProps) =>
|
|||||||
case 'keyup':
|
case 'keyup':
|
||||||
socket.emit('input:keyup', event.key);
|
socket.emit('input:keyup', event.key);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
console.log('Default keyEvent registered');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [socket, setLastAction]);
|
}, [socket]);
|
||||||
|
|
||||||
const handleConfirmation = (confirmed: boolean) => {
|
|
||||||
if (confirmed && pendingClick && socket) {
|
|
||||||
socket.emit('input:mousedown', pendingClick);
|
|
||||||
notifyLastAction('click');
|
|
||||||
}
|
|
||||||
setShowConfirmation(false);
|
|
||||||
setPendingClick(null);
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (canvasRef.current) {
|
if (canvasRef.current) {
|
||||||
@@ -151,31 +114,24 @@ const Canvas = ({ width, height, onCreateRef, highlighterData }: CanvasProps) =>
|
|||||||
canvasRef.current.removeEventListener('keydown', onKeyboardEvent);
|
canvasRef.current.removeEventListener('keydown', onKeyboardEvent);
|
||||||
canvasRef.current.removeEventListener('keyup', onKeyboardEvent);
|
canvasRef.current.removeEventListener('keyup', onKeyboardEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
console.log('Canvas not initialized');
|
||||||
}
|
}
|
||||||
}, [onMouseEvent, onKeyboardEvent, onCreateRef]);
|
|
||||||
|
}, [onMouseEvent]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<canvas
|
||||||
<GenericModal
|
tabIndex={0}
|
||||||
isOpen={showConfirmation}
|
ref={canvasRef}
|
||||||
onClose={() => setShowConfirmation(false)}
|
height={720}
|
||||||
canBeClosed={false}
|
width={1280}
|
||||||
>
|
/>
|
||||||
<ConfirmationBox
|
|
||||||
selector={highlighterData?.selector || ''}
|
|
||||||
onYes={() => handleConfirmation(true)}
|
|
||||||
onNo={() => handleConfirmation(false)}
|
|
||||||
/>
|
|
||||||
</GenericModal>
|
|
||||||
<canvas
|
|
||||||
tabIndex={0}
|
|
||||||
ref={canvasRef}
|
|
||||||
height={720}
|
|
||||||
width={1280}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
);
|
);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export default Canvas;
|
export default Canvas;
|
||||||
@@ -3,18 +3,52 @@ import { useSocketStore } from '../../context/socket';
|
|||||||
import Canvas from "../atoms/canvas";
|
import Canvas from "../atoms/canvas";
|
||||||
import { useBrowserDimensionsStore } from "../../context/browserDimensions";
|
import { useBrowserDimensionsStore } from "../../context/browserDimensions";
|
||||||
import { Highlighter } from "../atoms/Highlighter";
|
import { Highlighter } from "../atoms/Highlighter";
|
||||||
|
import { GenericModal } from '../atoms/GenericModal';
|
||||||
|
import { Button, Typography, Box } from '@mui/material';
|
||||||
|
|
||||||
|
interface ConfirmationBoxProps {
|
||||||
|
selector: string;
|
||||||
|
onYes: () => void;
|
||||||
|
onNo: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// New component for the confirmation box inside the modal
|
||||||
|
const ConfirmationBox = ({ selector, onYes, onNo }: ConfirmationBoxProps) => {
|
||||||
|
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>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const BrowserWindow = () => {
|
export const BrowserWindow = () => {
|
||||||
const [canvasRef, setCanvasReference] = useState<React.RefObject<HTMLCanvasElement> | undefined>(undefined);
|
const [canvasRef, setCanvasReference] = useState<React.RefObject<HTMLCanvasElement> | undefined>(undefined);
|
||||||
const [screenShot, setScreenShot] = useState<string>("");
|
const [screenShot, setScreenShot] = useState<string>("");
|
||||||
const [highlighterData, setHighlighterData] = useState<{ rect: DOMRect, selector: string } | null>(null);
|
const [highlighterData, setHighlighterData] = useState<{ rect: DOMRect, selector: string } | null>(null);
|
||||||
|
const [showConfirmation, setShowConfirmation] = useState(false);
|
||||||
|
|
||||||
const { socket } = useSocketStore();
|
const { socket } = useSocketStore();
|
||||||
const { width, height } = useBrowserDimensionsStore();
|
const { width, height } = useBrowserDimensionsStore();
|
||||||
|
|
||||||
|
console.log('Use browser dimensions:', width, height)
|
||||||
|
|
||||||
const onMouseMove = (e: MouseEvent) => {
|
const onMouseMove = (e: MouseEvent) => {
|
||||||
if (canvasRef && canvasRef.current && highlighterData) {
|
if (canvasRef && canvasRef.current && highlighterData) {
|
||||||
const canvasRect = canvasRef.current.getBoundingClientRect();
|
const canvasRect = canvasRef.current.getBoundingClientRect();
|
||||||
|
// mousemove outside the browser window
|
||||||
if (
|
if (
|
||||||
e.pageX < canvasRect.left
|
e.pageX < canvasRect.left
|
||||||
|| e.pageX > canvasRect.right
|
|| e.pageX > canvasRect.right
|
||||||
@@ -28,7 +62,7 @@ export const BrowserWindow = () => {
|
|||||||
|
|
||||||
const screencastHandler = useCallback((data: string) => {
|
const screencastHandler = useCallback((data: string) => {
|
||||||
setScreenShot(data);
|
setScreenShot(data);
|
||||||
}, []);
|
}, [screenShot]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (socket) {
|
if (socket) {
|
||||||
@@ -46,6 +80,7 @@ export const BrowserWindow = () => {
|
|||||||
|
|
||||||
const highlighterHandler = useCallback((data: { rect: DOMRect, selector: string }) => {
|
const highlighterHandler = useCallback((data: { rect: DOMRect, selector: string }) => {
|
||||||
setHighlighterData(data);
|
setHighlighterData(data);
|
||||||
|
console.log('Highlighter Rect via socket:', data.rect)
|
||||||
}, [highlighterData])
|
}, [highlighterData])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -59,9 +94,48 @@ export const BrowserWindow = () => {
|
|||||||
};
|
};
|
||||||
}, [socket, onMouseMove]);
|
}, [socket, onMouseMove]);
|
||||||
|
|
||||||
|
const handleClick = (e: React.MouseEvent<HTMLDivElement>) => {
|
||||||
|
if (highlighterData && canvasRef?.current) {
|
||||||
|
const canvasRect = canvasRef.current.getBoundingClientRect();
|
||||||
|
const clickX = e.clientX - canvasRect.left;
|
||||||
|
const clickY = e.clientY - canvasRect.top;
|
||||||
|
|
||||||
|
const highlightRect = highlighterData.rect;
|
||||||
|
if (
|
||||||
|
clickX >= highlightRect.left &&
|
||||||
|
clickX <= highlightRect.right &&
|
||||||
|
clickY >= highlightRect.top &&
|
||||||
|
clickY <= highlightRect.bottom
|
||||||
|
) {
|
||||||
|
setShowConfirmation(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleConfirmation = (confirmed: boolean) => {
|
||||||
|
if (confirmed) {
|
||||||
|
console.log(`User confirmed interaction with: ${highlighterData?.selector}`);
|
||||||
|
// Here you can add logic to interact with the element
|
||||||
|
} else {
|
||||||
|
console.log('User declined interaction');
|
||||||
|
}
|
||||||
|
setShowConfirmation(false);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div onClick={handleClick}>
|
||||||
{(highlighterData?.rect != null && highlighterData?.rect.top != null) && canvasRef?.current ?
|
<GenericModal
|
||||||
|
isOpen={showConfirmation}
|
||||||
|
onClose={() => setShowConfirmation(false)}
|
||||||
|
canBeClosed={false}
|
||||||
|
>
|
||||||
|
<ConfirmationBox
|
||||||
|
selector={highlighterData?.selector || ''}
|
||||||
|
onYes={() => handleConfirmation(true)}
|
||||||
|
onNo={() => handleConfirmation(false)}
|
||||||
|
/>
|
||||||
|
</GenericModal>
|
||||||
|
{(!showConfirmation && highlighterData?.rect != null && highlighterData?.rect.top != null) && canvasRef?.current ?
|
||||||
<Highlighter
|
<Highlighter
|
||||||
unmodifiedRect={highlighterData?.rect}
|
unmodifiedRect={highlighterData?.rect}
|
||||||
displayedSelector={highlighterData?.selector}
|
displayedSelector={highlighterData?.selector}
|
||||||
@@ -74,18 +148,23 @@ export const BrowserWindow = () => {
|
|||||||
onCreateRef={setCanvasReference}
|
onCreateRef={setCanvasReference}
|
||||||
width={width}
|
width={width}
|
||||||
height={height}
|
height={height}
|
||||||
highlighterData={highlighterData}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const drawImage = (image: string, canvas: HTMLCanvasElement): void => {
|
const drawImage = (image: string, canvas: HTMLCanvasElement): void => {
|
||||||
|
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
const img = new Image();
|
const img = new Image();
|
||||||
|
|
||||||
img.src = image;
|
img.src = image;
|
||||||
img.onload = () => {
|
img.onload = () => {
|
||||||
URL.revokeObjectURL(img.src);
|
URL.revokeObjectURL(img.src);
|
||||||
ctx?.drawImage(img, 0, 0, 1280, 720);
|
ctx?.drawImage(img, 0, 0, 1280, 720);
|
||||||
|
console.log('Image drawn on canvas:', img.width, img.height);
|
||||||
|
console.log('Image drawn on canvas:', canvas.width, canvas.height);
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user