Skip to content

Revert "Implement backward and forward navigation options to iframed window" #187

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

Merged
merged 7 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
finish merge
  • Loading branch information
Rich-Harris committed Jan 24, 2023
commit 23784df42f9f2abfca85b04cb0b7951ea0ee9bae
29 changes: 0 additions & 29 deletions src/routes/tutorial/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -98,36 +98,7 @@
</section>

<section slot="b" class="preview">
<<<<<<< HEAD
<Chrome
{path}
{loading}
on:refresh={() => {
if (adapter) {
set_iframe_src(adapter.base + path);
}
}}
on:change={(e) => {
if (adapter) {
const url = new URL(e.detail.value, adapter.base);
path = url.pathname + url.search + url.hash;
set_iframe_src(adapter.base + path);
}
}}
/>

<div class="content">
{#if browser}
<iframe bind:this={iframe} title="Output" />
{/if}

{#if loading || error}
<Loading {initial} {error} {progress} {status} />
{/if}
</div>
=======
<Output path={data.exercise.path} />
>>>>>>> main
</section>
</SplitPane>
</section>
Expand Down
73 changes: 8 additions & 65 deletions src/routes/tutorial/[slug]/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@

/** @type {import('$lib/types').Adapter} Will be defined after first afterNavigate */
let adapter;
/** @type {string[]} */
let history_bwd = [];
/** @type {string[]} */
let history_fwd = [];
let ignore_path_change = false;

function reset_history() {
history_bwd = [];
history_fwd = [];
}

onMount(() => {
const unsub = state.subscribe(async (state) => {
Expand All @@ -57,25 +47,19 @@
}
});

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;
});

afterNavigate(() => {
clearTimeout(timeout);
reset_history();
});

/**
Expand Down Expand Up @@ -155,16 +139,7 @@
if (e.origin !== adapter.base) return;

if (e.data.type === 'ping') {
const new_path = e.data.data.path ?? path;
if (path !== new_path) {
// skip `nav_to` step if triggered by bwd/fwd action
if (ignore_path_change) {
ignore_path_change = false;
} else {
nav_to();
}
path = new_path;
}
path = e.data.data.path ?? path;

clearTimeout(timeout);
timeout = setTimeout(() => {
Expand All @@ -191,54 +166,22 @@
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);
}

/** @param {string | null} new_path */
function nav_to(new_path = null) {
if (path !== history_bwd[history_bwd.length - 1]) {
history_bwd = [...history_bwd, path];
}
history_fwd = [];
if (new_path) route_to(new_path);
}

function go_bwd() {
const new_path = history_bwd[history_bwd.length - 1];
if (new_path) {
ignore_path_change = true;
[history_bwd, history_fwd] = [history_bwd.slice(0, -1), [path, ...history_fwd]];
route_to(new_path);
}
}

function go_fwd() {
const new_path = history_fwd[0];
if (new_path) {
ignore_path_change = true;
[history_bwd, history_fwd] = [[...history_bwd, path], history_fwd.slice(1)];
route_to(new_path);
}
}
</script>

<svelte:window on:message={handle_message} />
<Chrome
{history_bwd}
{history_fwd}
{path}
{loading}
on:refresh={() => {
set_iframe_src(adapter.base + path);
}}
on:change={(e) => nav_to(e.detail.value)}
on:back={go_bwd}
on:forward={go_fwd}
on:change={(e) => {
if (adapter) {
const url = new URL(e.detail.value, adapter.base);
path = url.pathname + url.search + url.hash;
set_iframe_src(adapter.base + path);
}
}}
/>

<div class="content">
Expand Down