Skip to content
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

fix: better iframe navigation #193

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
60 changes: 14 additions & 46 deletions content/tutorial/common/src/__client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,22 @@ window.alert = (message) => {
};

window.addEventListener('message', async (e) => {
if (e.data.type === 'fetch') {
const names = e.data.names;

const transformed = await Promise.all(
names.map(async (name) => {
const res = await fetch(name);
return {
name,
code: await res.text()
};
})
);

const css_files = [];

for (const { name, code } of transformed) {
if (
name.endsWith('.svelte') &&
code.includes('svelte&type=style&lang.css')
) {
css_files.push(name + '?svelte&type=style&lang.css');
}
}

if (css_files.length > 0) {
const css_transformed = await Promise.all(
css_files.map(async (name) => {
const res = await fetch(name);
return {
name,
code: await res.text()
};
})
);

transformed.push(...css_transformed);
}

parent.postMessage(
{
type: 'fetch-result',
data: transformed
},
'*'
);
// Belts and braces against malicious messages
if (e.data.type === 'goto' && e.data.path?.startsWith('/')) {
// SvelteKit's client.js will pick this up
const a = document.createElement('a');
a.href = e.data.path;
document.firstElementChild.append(a);
a.click();
a.remove();
}
});

history.pushState = function (state, title, url) {
// Don't create a new history entry for better back/forward navigation in the parent window
history.replaceState(state, title, url);
};

function ping() {
parent.postMessage(
{
Expand Down Expand Up @@ -90,7 +58,7 @@ if (import.meta.hot) {
});
}

/**
/**
* The iframe sometimes takes focus control in ways we can't prevent
* while the editor is focussed. Refocus the editor in these cases.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/routes/tutorial/[slug]/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
if (new_path) {
ignore_path_change = true;
[history_bwd, history_fwd] = [history_bwd.slice(0, -1), [path, ...history_fwd]];
route_to(new_path);
iframe.contentWindow?.postMessage({ type: 'goto', path: new_path }, '*');
}
}

Expand All @@ -222,7 +222,7 @@
if (new_path) {
ignore_path_change = true;
[history_bwd, history_fwd] = [[...history_bwd, path], history_fwd.slice(1)];
route_to(new_path);
iframe.contentWindow?.postMessage({ type: 'goto', path: new_path }, '*');
}
}
</script>
Expand Down