playground: reset on navigate (#11715)

pull/11724/head
Rich Harris 8 months ago committed by GitHub
parent 09addad9ae
commit 881040fb78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -3,24 +3,26 @@
import '@sveltejs/site-kit/styles/index.css'; import '@sveltejs/site-kit/styles/index.css';
import Repl from '$lib/Repl.svelte'; import Repl from '$lib/Repl.svelte';
import { onMount } from 'svelte';
import { default_files } from './defaults.js'; import { default_files } from './defaults.js';
import { compress_and_encode_text, decode_and_decompress_text } from './gzip.js'; import { compress_and_encode_text, decode_and_decompress_text } from './gzip.js';
import { afterNavigate } from '$app/navigation';
/** @type {Repl} */ /** @type {Repl} */
let repl; let repl;
let setting_hash = false; let setting_hash = false;
let started = false; let navigating = false;
onMount(change_from_hash); afterNavigate(change_from_hash);
async function change_from_hash() { async function change_from_hash() {
navigating = true;
const hash = location.hash.slice(1); const hash = location.hash.slice(1);
if (!hash) { if (!hash) {
repl.set({ repl.set({
files: default_files files: default_files()
}); });
return; return;
@ -56,8 +58,8 @@
/** @param {CustomEvent<any>} e*/ /** @param {CustomEvent<any>} e*/
async function change_from_editor(e) { async function change_from_editor(e) {
if (!started) { if (navigating) {
started = true; // ignore initial change caused by the repl.set in change_from_hash navigating = false;
return; return;
} }
@ -76,7 +78,7 @@
</script> </script>
<svelte:window <svelte:window
on:hashchange={(e) => { on:hashchange={() => {
if (!setting_hash) { if (!setting_hash) {
change_from_hash(); change_from_hash();
} }

@ -1,4 +1,4 @@
export const default_files = [ export const default_files = () => [
{ {
name: 'App', name: 'App',
type: 'svelte', type: 'svelte',

Loading…
Cancel
Save