File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
packages/repl/src/lib/Output/srcdoc Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 43
43
}
44
44
45
45
( 0 , eval ) ( data . args . script ) ;
46
+
47
+ // hand focus back to the editor if it's taken in an effect
48
+ // that runs immediately (for focuses in effects, this is
49
+ // more effective than listening for the focusin event)
50
+ Promise . resolve ( ) . then ( ( ) => {
51
+ if ( document . activeElement !== null && document . activeElement !== document . body ) {
52
+ send ( { type : 'iframe_took_focus' } ) ;
53
+ }
54
+ } ) ;
46
55
}
47
56
48
57
if ( action === 'catch_clicks' ) {
244
253
original ( ...args ) ;
245
254
} ;
246
255
}
256
+
257
+ // Focus management
258
+ let can_focus = false ;
259
+
260
+ window . addEventListener ( 'pointerdown' , ( e ) => ( can_focus = true ) ) ;
261
+ window . addEventListener ( 'pointerup' , ( e ) => ( can_focus = false ) ) ;
262
+ window . addEventListener ( 'keydown' , ( e ) => ( can_focus = true ) ) ;
263
+ window . addEventListener ( 'keyup' , ( e ) => ( can_focus = false ) ) ;
264
+
265
+ window . addEventListener ( 'focusin' , ( e ) => {
266
+ // if focusin happened as a result of a mouse/keyboard event, allow it
267
+ if ( can_focus ) return ;
268
+
269
+ // if `e.target` is the `<body>` and there's a `relatedTarget`,
270
+ // assume the focusin was the result of a user navigation — allow it
271
+ if ( e . target . tagName === 'BODY' && e . relatedTarget ) return ;
272
+
273
+ // otherwise, broadcast an event that causes the editor to reclaim focus
274
+ send ( { type : 'iframe_took_focus' } ) ;
275
+ } ) ;
247
276
} ) ( ) ;
248
277
</ script >
249
278
</ head >
You can’t perform that action at this time.
0 commit comments