@@ -37,7 +37,6 @@ setInterval(() => {
37
37
}
38
38
} , LOGWAIT ) ;
39
39
40
- // handling capturing messages from the console
41
40
function handleMessageEvent ( e ) {
42
41
// maybe don't need this?? idk!
43
42
if ( window . origin !== e . origin ) return ;
@@ -65,40 +64,42 @@ function handleMessageEvent(e) {
65
64
window . addEventListener ( 'message' , handleMessageEvent ) ;
66
65
67
66
// 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 = [ ] ;
73
67
74
68
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 ;
86
70
87
71
const waitForCanvas = ( ) => {
88
72
const canvas = document . getElementById ( 'defaultCanvas0' ) ;
89
73
90
- if ( canvas ) {
74
+ if ( canvas && ! isListenerAttached ) {
91
75
console . log ( 'canvas found, adding mouseover listener' ) ;
76
+ isListenerAttached = true ;
92
77
93
- canvas . addEventListener ( 'mousemove' , ( event ) => {
78
+ const mouseMoveHandler = ( event ) => {
94
79
const rect = canvas . getBoundingClientRect ( ) ;
95
80
const x = event . clientX - rect . left ;
96
81
const y = event . clientY - rect . top ;
97
82
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
+ }
100
99
} ) ;
101
- } else {
100
+
101
+ observer . observe ( document . body , { childList : true , subtree : true } ) ;
102
+ } else if ( ! canvas ) {
102
103
console . log ( 'canvas not found yet' ) ;
103
104
setTimeout ( waitForCanvas , LOGWAIT ) ;
104
105
}
@@ -107,17 +108,6 @@ function hookIntoCanvas() {
107
108
waitForCanvas ( ) ;
108
109
}
109
110
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
-
121
111
document . addEventListener ( 'DOMContentLoaded' , hookIntoCanvas ) ;
122
112
123
113
// catch reference errors, via http://stackoverflow.com/a/12747364/2994108
0 commit comments