From 881040fb78e6eafd772a4bbb9cca76d42b13c7af Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 21 May 2024 13:08:20 -0400 Subject: [PATCH] playground: reset on navigate (#11715) --- sites/svelte-5-preview/src/routes/+page.svelte | 16 +++++++++------- sites/svelte-5-preview/src/routes/defaults.js | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/sites/svelte-5-preview/src/routes/+page.svelte b/sites/svelte-5-preview/src/routes/+page.svelte index 3ce2f7062d..34157c6455 100644 --- a/sites/svelte-5-preview/src/routes/+page.svelte +++ b/sites/svelte-5-preview/src/routes/+page.svelte @@ -3,24 +3,26 @@ import '@sveltejs/site-kit/styles/index.css'; import Repl from '$lib/Repl.svelte'; - import { onMount } from 'svelte'; import { default_files } from './defaults.js'; import { compress_and_encode_text, decode_and_decompress_text } from './gzip.js'; + import { afterNavigate } from '$app/navigation'; /** @type {Repl} */ let repl; let setting_hash = false; - let started = false; + let navigating = false; - onMount(change_from_hash); + afterNavigate(change_from_hash); async function change_from_hash() { + navigating = true; + const hash = location.hash.slice(1); if (!hash) { repl.set({ - files: default_files + files: default_files() }); return; @@ -56,8 +58,8 @@ /** @param {CustomEvent} e*/ async function change_from_editor(e) { - if (!started) { - started = true; // ignore initial change caused by the repl.set in change_from_hash + if (navigating) { + navigating = false; return; } @@ -76,7 +78,7 @@ { + on:hashchange={() => { if (!setting_hash) { change_from_hash(); } diff --git a/sites/svelte-5-preview/src/routes/defaults.js b/sites/svelte-5-preview/src/routes/defaults.js index 7742cce594..f1bdbbbb35 100644 --- a/sites/svelte-5-preview/src/routes/defaults.js +++ b/sites/svelte-5-preview/src/routes/defaults.js @@ -1,4 +1,4 @@ -export const default_files = [ +export const default_files = () => [ { name: 'App', type: 'svelte',