From 53027e618ab4b3d25f4f208d968c1f97dee9734b Mon Sep 17 00:00:00 2001 From: "Jiaxiao (mossaka) Zhou" Date: Thu, 19 Feb 2026 08:01:54 +0000 Subject: [PATCH] Add auto-save to localStorage for Playground editor Save editor content to localStorage on every change so users don't lose their work when closing the tab. On page load, restore saved content if available, falling back to the Hello World template. Co-Authored-By: Claude Opus 4.6 (1M context) --- docs/public/editor/editor.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/public/editor/editor.js b/docs/public/editor/editor.js index 11ff0ba76dc..49cf30787c5 100644 --- a/docs/public/editor/editor.js +++ b/docs/public/editor/editor.js @@ -130,6 +130,7 @@ const panels = $('panels'); // --------------------------------------------------------------- // State // --------------------------------------------------------------- +const STORAGE_KEY = 'gh-aw-playground-content'; let compiler = null; let isReady = false; let isCompiling = false; @@ -175,8 +176,11 @@ function setTheme(theme) { // --------------------------------------------------------------- // CodeMirror: Input Editor (Markdown with YAML frontmatter) // --------------------------------------------------------------- +const savedContent = localStorage.getItem(STORAGE_KEY); +const initialContent = savedContent || DEFAULT_CONTENT; + const editorView = new EditorView({ - doc: DEFAULT_CONTENT, + doc: initialContent, extensions: [ basicSetup, markdown(), @@ -189,6 +193,8 @@ const editorView = new EditorView({ }]), EditorView.updateListener.of(update => { if (update.docChanged) { + try { localStorage.setItem(STORAGE_KEY, update.state.doc.toString()); } + catch (_) { /* localStorage full or unavailable */ } if (isReady) { scheduleCompile(); } else { @@ -200,6 +206,11 @@ const editorView = new EditorView({ parent: editorMount, }); +// If restoring saved content, clear the dropdown since it may not match any sample +if (savedContent) { + sampleSelect.value = ''; +} + // --------------------------------------------------------------- // CodeMirror: Output View (YAML, read-only) // ---------------------------------------------------------------