Skip to content

Implement backward and forward navigation options to iframed window (2) #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 7 additions & 28 deletions src/routes/tutorial/[slug]/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,14 @@
}
});

function on_iframe_load() {
iframe.classList.add('loaded');
}
function destroy() {
iframe.removeEventListener('load', on_iframe_load);
unsub();
if (adapter) {
adapter.destroy();
}
}

document.addEventListener('pagehide', destroy);
iframe.addEventListener('load', on_iframe_load);
return destroy;
});

Expand Down Expand Up @@ -99,7 +94,7 @@
adapter = _adapter;
await _adapter.init;

set_iframe_src(adapter.base + path);
iframe.src = adapter.base + path;
}

await new Promise((fulfil, reject) => {
Expand All @@ -118,7 +113,7 @@
if (!called) {
// Updating the iframe too soon sometimes results in a blank screen,
// so we try again after a short delay if we haven't heard back
set_iframe_src(adapter.base + path);
iframe.src = adapter.base + path;
}
}, 5000);

Expand All @@ -131,7 +126,7 @@

if (reload_iframe) {
await new Promise((fulfil) => setTimeout(fulfil, 200));
set_iframe_src(adapter.base + path);
iframe.src = adapter.base + path;
}

return adapter;
Expand All @@ -142,7 +137,7 @@
function schedule_iframe_reload() {
clearTimeout(reload_timeout);
reload_timeout = setTimeout(() => {
set_iframe_src(adapter.base + path);
iframe.src = adapter.base + path;
}, 1000);
}

Expand Down Expand Up @@ -172,31 +167,19 @@

// we lost contact, refresh the page
loading = true;
set_iframe_src(adapter.base + path);
iframe.src = adapter.base + path;
loading = false;
}, 1000);
} else if (e.data.type === 'ping-pause') {
clearTimeout(timeout);
}
}

/** @param {string} src */
function set_iframe_src(src) {
// removing the iframe from the document allows us to
// change the src without adding a history entry, which
// would make back/forward traversal very annoying
const parentNode = /** @type {HTMLElement} */ (iframe.parentNode);
iframe.classList.remove('loaded');
parentNode?.removeChild(iframe);
iframe.src = src;
parentNode?.appendChild(iframe);
}

/** @param {string} path */
function route_to(path) {
const url = new URL(path, adapter.base);
path = url.pathname + url.search + url.hash;
set_iframe_src(adapter.base + path);
iframe.src = adapter.base + path;
}

/** @param {string | null} new_path */
Expand Down Expand Up @@ -234,7 +217,7 @@
{path}
{loading}
on:refresh={() => {
set_iframe_src(adapter.base + path);
iframe.src = adapter.base + path;
}}
on:change={(e) => nav_to(e.detail.value)}
on:back={go_bwd}
Expand Down Expand Up @@ -272,8 +255,4 @@
border: none;
background: var(--sk-back-2);
}

iframe:not(.loaded) {
display: none;
}
</style>