Skip to content

Commit 74b23f0

Browse files
Mossakaclaude
andauthored
Add auto-save to localStorage for Playground editor (#16782)
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) <noreply@anthropic.com>
1 parent 417eaf1 commit 74b23f0

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

docs/public/editor/editor.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ const panels = $('panels');
129129
// ---------------------------------------------------------------
130130
// State
131131
// ---------------------------------------------------------------
132+
const STORAGE_KEY = 'gh-aw-playground-content';
132133
let compiler = null;
133134
let isReady = false;
134135
let isCompiling = false;
@@ -162,8 +163,11 @@ function applyCmTheme() {
162163
// ---------------------------------------------------------------
163164
// CodeMirror: Input Editor (Markdown with YAML frontmatter)
164165
// ---------------------------------------------------------------
166+
const savedContent = localStorage.getItem(STORAGE_KEY);
167+
const initialContent = savedContent || DEFAULT_CONTENT;
168+
165169
const editorView = new EditorView({
166-
doc: DEFAULT_CONTENT,
170+
doc: initialContent,
167171
extensions: [
168172
basicSetup,
169173
markdown(),
@@ -176,6 +180,8 @@ const editorView = new EditorView({
176180
}]),
177181
EditorView.updateListener.of(update => {
178182
if (update.docChanged) {
183+
try { localStorage.setItem(STORAGE_KEY, update.state.doc.toString()); }
184+
catch (_) { /* localStorage full or unavailable */ }
179185
if (isReady) {
180186
scheduleCompile();
181187
} else {
@@ -187,6 +193,11 @@ const editorView = new EditorView({
187193
parent: editorMount,
188194
});
189195

196+
// If restoring saved content, clear the dropdown since it may not match any sample
197+
if (savedContent) {
198+
sampleSelect.value = '';
199+
}
200+
190201
// ---------------------------------------------------------------
191202
// CodeMirror: Output View (YAML, read-only)
192203
// ---------------------------------------------------------------

0 commit comments

Comments
 (0)