Skip to content

Commit 2e0c9db

Browse files
committed
fix tracking coordinates on canvas reload
1 parent a026b2f commit 2e0c9db

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed
Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect, useState } from 'react';
2+
import PropTypes from 'prop-types';
23
import styled from 'styled-components';
34
import { remSize } from '../../theme';
45

@@ -23,59 +24,42 @@ const CoordContainer = styled.div`
2324
}
2425
`;
2526

26-
const CoordinateTracker = (isPlaying) => {
27+
const CoordinateTracker = ({ isPlaying, sketchReloaded }) => {
2728
const [coordinates, setCoordinates] = useState({ x: 0, y: 0 });
2829

2930
useEffect(() => {
30-
console.log('we here');
31-
let isListenerAttached = false;
32-
let mouseMoveHandler;
3331
let canvas;
32+
let mouseMoveHandler;
3433

35-
const waitForCanvas = () => {
34+
const timeout = setTimeout(() => {
3635
const iFrame = document.getElementById('previewIframe0');
37-
canvas = iFrame.contentWindow.document.getElementById('defaultCanvas0');
38-
console.log('iframe: ', iFrame);
39-
40-
if (canvas && !isListenerAttached) {
41-
isListenerAttached = true;
42-
console.log('Adding listener');
43-
44-
mouseMoveHandler = (event) => {
45-
console.log('hellooooo');
46-
const rect = canvas.getBoundingClientRect();
47-
const x = event.clientX - rect.left;
48-
const y = event.clientY - rect.top;
49-
50-
setCoordinates({ x, y });
51-
};
52-
53-
console.log('mouseMoveHandler', mouseMoveHandler);
54-
console.log('canvas', canvas);
36+
canvas = iFrame?.contentWindow?.document?.getElementById(
37+
'defaultCanvas0'
38+
);
5539

56-
document
57-
.querySelector('#defaultCanvas0')
58-
.addEventListener('mousemove', () => {
59-
console.log('Button clicked!');
60-
mouseMoveHandler();
61-
});
62-
// canvas.addEventListener('mousemove', mouseMoveHandler);
63-
} else if (!canvas) {
64-
setTimeout(waitForCanvas, 500);
40+
if (!canvas) {
41+
console.warn('Canvas not found.');
42+
return;
6543
}
66-
};
6744

68-
waitForCanvas();
45+
mouseMoveHandler = (event) => {
46+
const rect = canvas.getBoundingClientRect();
47+
const x = event.clientX - rect.left;
48+
const y = event.clientY - rect.top;
49+
setCoordinates({ x, y });
50+
};
51+
52+
canvas.addEventListener('mousemove', mouseMoveHandler);
53+
}, 500);
6954

7055
return () => {
71-
if (canvas && isListenerAttached) {
72-
console.log('Removing listener');
56+
clearTimeout(timeout);
57+
58+
if (canvas && mouseMoveHandler) {
7359
canvas.removeEventListener('mousemove', mouseMoveHandler);
74-
canvas = null;
75-
isListenerAttached = false;
7660
}
7761
};
78-
}, [isPlaying]);
62+
}, [isPlaying, sketchReloaded]);
7963

8064
return (
8165
<CoordContainer>
@@ -87,4 +71,9 @@ const CoordinateTracker = (isPlaying) => {
8771
);
8872
};
8973

74+
CoordinateTracker.propTypes = {
75+
isPlaying: PropTypes.bool.isRequired,
76+
sketchReloaded: PropTypes.bool.isRequired
77+
};
78+
9079
export default CoordinateTracker;

client/modules/Preview/previewIndex.jsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const App = () => {
2828
const [gridOutput, setGridOutput] = useState(false);
2929
const [userTheme, setUserTheme] = useState('light');
3030
const [coordinatesVisible, setCoordinatesVisible] = useState(false);
31+
const [sketchReloaded, setSketchReloaded] = useState(0);
3132

3233
registerFrame(window.parent, getConfig('EDITOR_URL'));
3334

@@ -42,6 +43,7 @@ const App = () => {
4243
setGridOutput(payload.gridOutput);
4344
setUserTheme(payload.userTheme);
4445
setCoordinatesVisible(payload.coordinates);
46+
setSketchReloaded((prev) => prev + 1);
4547
break;
4648
case MessageTypes.START:
4749
console.log('starting');
@@ -94,7 +96,12 @@ const App = () => {
9496
return (
9597
<ThemeProvider theme={theme[userTheme]}>
9698
<GlobalStyle />
97-
{coordinatesVisible && <CoordinateTracker isPlaying={isPlaying} />}
99+
{coordinatesVisible && (
100+
<CoordinateTracker
101+
isPlaying={isPlaying}
102+
sketchReloaded={sketchReloaded}
103+
/>
104+
)}
98105
<EmbedFrame
99106
files={memoizedFiles}
100107
isPlaying={isPlaying}

0 commit comments

Comments
 (0)