File tree 2 files changed +27
-17
lines changed
content/tutorial/common/src
src/routes/tutorial/[slug] 2 files changed +27
-17
lines changed Original file line number Diff line number Diff line change @@ -89,3 +89,18 @@ if (import.meta.hot) {
89
89
) ;
90
90
} ) ;
91
91
}
92
+
93
+ /**
94
+ * The iframe sometimes takes focus control in ways we can't prevent
95
+ * while the editor is focussed. Refocus the editor in these cases.
96
+ */
97
+ window . addEventListener ( 'focusin' , ( e ) => {
98
+ if ( e . target . tagName === 'BODY' ) {
99
+ parent . postMessage (
100
+ {
101
+ type : 'focus_on_editor'
102
+ } ,
103
+ '*'
104
+ ) ;
105
+ }
106
+ } ) ;
Original file line number Diff line number Diff line change 41
41
* This boolean tracks whether or not the editor should be refocused.
42
42
*/
43
43
let preserve_focus = true ;
44
+ /** @type {any} */
45
+ let remove_focus_timeout;
44
46
45
47
onMount (() => {
46
48
let destroyed = false ;
241
243
< / script>
242
244
243
245
< svelte: window
244
- on: pointerdown = {(e ) => {
245
- if (! container . contains ( /** @type {HTMLElement} */ ( e . target )) ) {
246
- preserve_focus = false ;
246
+ on: message = {(e ) => {
247
+ if (preserve_focus && e . data . type === ' focus_on_editor ' ) {
248
+ instance ? . editor . focus () ;
247
249
}
248
250
}}
249
251
/ >
250
252
251
253
< div bind: clientWidth= {w} bind: clientHeight= {h}>
252
254
< div
253
255
bind: this = {container}
254
- on: keydown= {(e ) => {
255
- if (e .key === ' Tab' ) {
256
- preserve_focus = false ;
257
-
258
- setTimeout (() => {
259
- preserve_focus = true ;
260
- }, 0 );
261
- }
262
- }}
263
256
on: focusin= {() => {
257
+ clearTimeout (remove_focus_timeout);
264
258
preserve_focus = true ;
265
259
}}
266
260
on: focusout= {() => {
267
- if (preserve_focus) {
268
- setTimeout (() => {
269
- instance? .editor .focus ();
270
- }, 0 );
271
- }
261
+ // Heuristic: user did refocus themmselves if focus_on_editor
262
+ // doesn't happen in the next few miliseconds. Needed
263
+ // because else navigations inside the iframe refocus the editor.
264
+ remove_focus_timeout = setTimeout (() => {
265
+ preserve_focus = false ;
266
+ }, 500 )
272
267
}}
273
268
/ >
274
269
< / div>
You can’t perform that action at this time.
0 commit comments