|
51 | 51 |
|
52 | 52 | let width = browser ? window.innerWidth : 1000;
|
53 | 53 | let selected_view = 0;
|
| 54 | + /** @type {import('monaco-editor').editor.IStandaloneCodeEditor | undefined} */ |
| 55 | + let editor_instance; |
54 | 56 |
|
55 | 57 | $: mobile = writable(false);
|
56 | 58 | $: $mobile = width < 768;
|
|
293 | 295 |
|
294 | 296 | /** @param {string} src */
|
295 | 297 | 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 |
297 | 299 | // 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. |
299 | 301 | const parentNode = /** @type {HTMLElement} */ (iframe.parentNode);
|
300 | 302 | parentNode?.removeChild(iframe);
|
301 | 303 | iframe.src = src;
|
302 | 304 | 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); |
303 | 322 | }
|
304 | 323 | </script>
|
305 | 324 |
|
|
391 | 410 | stubs={$files}
|
392 | 411 | selected={$selected}
|
393 | 412 | read_only={$mobile}
|
| 413 | + bind:editor_instance |
394 | 414 | on:change={update_stub}
|
395 | 415 | />
|
396 | 416 | <ImageViewer selected={$selected} />
|
|
0 commit comments