Skip to content

Commit 53748b7

Browse files
committed
[fix] reset focus after iframe reload
Fixes #150
1 parent deebd40 commit 53748b7

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/routes/tutorial/[slug]/+page.svelte

+22-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
5252
let width = browser ? window.innerWidth : 1000;
5353
let selected_view = 0;
54+
/** @type {import('monaco-editor').editor.IStandaloneCodeEditor | undefined} */
55+
let editor_instance;
5456
5557
$: mobile = writable(false);
5658
$: $mobile = width < 768;
@@ -293,13 +295,30 @@
293295
294296
/** @param {string} src */
295297
function set_iframe_src(src) {
296-
// removing the iframe from the document allows us to
298+
// Removing the iframe from the document allows us to
297299
// change the src without adding a history entry, which
298-
// would make back/forward traversal very annoying
300+
// would make back/forward traversal very annoying.
299301
const parentNode = /** @type {HTMLElement} */ (iframe.parentNode);
300302
parentNode?.removeChild(iframe);
301303
iframe.src = src;
302304
parentNode?.appendChild(iframe);
305+
// We need to reset focus because it's lost on reload when the
306+
// inner app has `export const ssr = false`, which results in
307+
// a initial navigation with a focus reset at the end.
308+
const editor_has_focus = editor_instance?.hasTextFocus();
309+
const active_element = document.activeElement;
310+
const on_blur = () => {
311+
// Timeout so that following focus event happens first
312+
setTimeout(() => {
313+
if (editor_has_focus) {
314+
editor_instance?.focus();
315+
} else {
316+
active_element?.focus();
317+
}
318+
});
319+
};
320+
document.addEventListener('focusout', on_blur, { once: true });
321+
setTimeout(() => document.removeEventListener('focusout', on_blur), 2000);
303322
}
304323
</script>
305324
@@ -391,6 +410,7 @@
391410
stubs={$files}
392411
selected={$selected}
393412
read_only={$mobile}
413+
bind:editor_instance
394414
on:change={update_stub}
395415
/>
396416
<ImageViewer selected={$selected} />

src/routes/tutorial/[slug]/Editor.svelte

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@
2424
/** @type {import('$lib/types').Stub | null} */
2525
export let selected = null;
2626
export let read_only = false;
27+
/** @type {import('monaco-editor').editor.IStandaloneCodeEditor | undefined} */
28+
export let editor_instance;
2729
2830
const dispatch = createEventDispatcher();
2931
3032
/** @type {HTMLDivElement} */
3133
let container;
3234
33-
/** @type {ReturnType<typeof init> | undefined}*/
34-
let instance;
35-
3635
let w = 0;
3736
let h = 0;
3837
38+
/** @type {ReturnType<typeof init> | undefined}*/
39+
let instance;
40+
3941
onMount(() => {
4042
let destroyed = false;
4143
@@ -50,6 +52,7 @@
5052
import('$lib/client/monaco/monaco.js').then(({ monaco }) => {
5153
if (destroyed) return;
5254
instance = init(monaco, dark_mode);
55+
editor_instance = instance.editor;
5356
});
5457
5558
/** @param {MediaQueryListEvent} event */

0 commit comments

Comments
 (0)