Skip to content

Commit 683dd86

Browse files
committed
add listener and cleanup function
1 parent 6fda53a commit 683dd86

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

client/utils/previewEntry.js

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ setInterval(() => {
3737
}
3838
}, LOGWAIT);
3939

40-
// handling capturing messages from the console
4140
function handleMessageEvent(e) {
4241
// maybe don't need this?? idk!
4342
if (window.origin !== e.origin) return;
@@ -65,40 +64,42 @@ function handleMessageEvent(e) {
6564
window.addEventListener('message', handleMessageEvent);
6665

6766
// setting up mouse x and y coordinates
68-
// similar to hooking into console?
69-
70-
// notes from cassie: may want to do sth with the dispatcher?
71-
// this file, previewEntry.js is just for catching console errors
72-
const canvasMouseBuffer = [];
7367

7468
function hookIntoCanvas() {
75-
const coordinatesDiv = document.createElement('div');
76-
77-
// ideally want this to be updated next to the Preview Header in IDEView eventually
78-
coordinatesDiv.style.position = 'absolute';
79-
coordinatesDiv.style.backgroundColor = 'rgba(255, 255, 255, 0.8)';
80-
coordinatesDiv.style.padding = '5px';
81-
coordinatesDiv.style.border = '1px solid #ccc';
82-
coordinatesDiv.style.fontSize = '12px';
83-
coordinatesDiv.style.zIndex = '1000';
84-
85-
document.body.appendChild(coordinatesDiv);
69+
let isListenerAttached = false;
8670

8771
const waitForCanvas = () => {
8872
const canvas = document.getElementById('defaultCanvas0');
8973

90-
if (canvas) {
74+
if (canvas && !isListenerAttached) {
9175
console.log('canvas found, adding mouseover listener');
76+
isListenerAttached = true;
9277

93-
canvas.addEventListener('mousemove', (event) => {
78+
const mouseMoveHandler = (event) => {
9479
const rect = canvas.getBoundingClientRect();
9580
const x = event.clientX - rect.left;
9681
const y = event.clientY - rect.top;
9782

98-
// console.log(`mouse coordinates: ${x} and ${y}`);
99-
coordinatesDiv.innerHTML = `Mouse X: ${x}, Mouse Y: ${y}`;
83+
const message = {
84+
payload: { xVal: x, yVal: y },
85+
type: 'COORDINATES'
86+
};
87+
window.parent.postMessage(message, window.parent.location.origin);
88+
};
89+
90+
canvas.addEventListener('mousemove', mouseMoveHandler);
91+
92+
const observer = new MutationObserver(() => {
93+
if (!document.body.contains(canvas)) {
94+
console.log('Canvas removed, cleaning up listener');
95+
canvas.removeEventListener('mousemove', mouseMoveHandler);
96+
observer.disconnect();
97+
isListenerAttached = false;
98+
}
10099
});
101-
} else {
100+
101+
observer.observe(document.body, { childList: true, subtree: true });
102+
} else if (!canvas) {
102103
console.log('canvas not found yet');
103104
setTimeout(waitForCanvas, LOGWAIT);
104105
}
@@ -107,17 +108,6 @@ function hookIntoCanvas() {
107108
waitForCanvas();
108109
}
109110

110-
setInterval(() => {
111-
if (canvasMouseBuffer.length > 0) {
112-
const message = {
113-
messages: canvasMouseBuffer,
114-
source: 'sketch'
115-
};
116-
editor.postMessage(message, editorOrigin);
117-
canvasMouseBuffer.length = 0;
118-
}
119-
}, LOGWAIT);
120-
121111
document.addEventListener('DOMContentLoaded', hookIntoCanvas);
122112

123113
// catch reference errors, via http://stackoverflow.com/a/12747364/2994108

0 commit comments

Comments
 (0)