From 08bb0b6f0d8eefd49df796a1085e97498f591774 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:00:40 -0800 Subject: [PATCH 1/2] Add Ctrl+S shortcut to Playground v2 Users rely on the v1 shortcut to save the current playground state and copy a shareable link. Preserve that workflow in v2, including when automatic URL updates are disabled. --- packages/playground-v2/src/exporter.ts | 2 ++ packages/playground-v2/src/index.html | 1 + packages/playground-v2/src/main.ts | 36 +++++++++++++++++++++++--- packages/playground-v2/src/styles.css | 13 ++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/packages/playground-v2/src/exporter.ts b/packages/playground-v2/src/exporter.ts index 87a887f008d5..9b202b7bd0e4 100644 --- a/packages/playground-v2/src/exporter.ts +++ b/packages/playground-v2/src/exporter.ts @@ -166,6 +166,8 @@ export function configureExports(options: { project(): ExportProject; compile(): throw new Error(`Unknown export: ${action}`) } } + + return { showLink: () => perform("link") } } function element(id: string) { diff --git a/packages/playground-v2/src/index.html b/packages/playground-v2/src/index.html index 3c93de346730..c0b4a53f98d3 100644 --- a/packages/playground-v2/src/index.html +++ b/packages/playground-v2/src/index.html @@ -55,6 +55,7 @@

TypeScript Playground v2

+
diff --git a/packages/playground-v2/src/main.ts b/packages/playground-v2/src/main.ts index f7b31df12421..b2dc79dcc655 100644 --- a/packages/playground-v2/src/main.ts +++ b/packages/playground-v2/src/main.ts @@ -242,6 +242,7 @@ const clearRunOutput = getElement("clear-run-output") const runOutput = getElement("run-output") const runLog = getElement("run-log") const status = getElement("status") +const shortcutToast = getElement("shortcut-toast") const loader = getElement("loader") const loadingAnimation = getElement("loading-animation") const loadingCopy = getElement("loading-copy") @@ -459,6 +460,23 @@ inputEditor.addAction({ keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter], run: runProject, }) +document.addEventListener( + "keydown", + event => { + if (!(event.ctrlKey || event.metaKey) || event.altKey || event.key.toLowerCase() !== "s") return + event.preventDefault() + persistProjectState(true) + void (async () => { + try { + await navigator.clipboard.writeText(location.href) + showShortcutToast("Playground link copied to clipboard.") + } catch { + await exportActions.showLink() + } + })() + }, + { capture: true } +) const inlayEmitter = new monaco.Emitter() const typeQueries = new Map() // Monaco subscribes to refresh events only after its first hint request completes. @@ -820,7 +838,7 @@ monaco.languages.registerInlayHintsProvider("json", { }) let updateTimer = 0 -configureExports({ +const exportActions = configureExports({ compile: () => compileActiveProject?.(), project() { const state = currentProjectState() @@ -2593,6 +2611,16 @@ function restoreProjectSelection(state: ProjectState) { inputEditor.revealRangeInCenter(inputEditor.getSelection()!, monaco.editor.ScrollType.Immediate) } +let shortcutToastTimer = 0 +function showShortcutToast(message: string) { + shortcutToast.textContent = message + shortcutToast.hidden = false + window.clearTimeout(shortcutToastTimer) + shortcutToastTimer = window.setTimeout(() => { + shortcutToast.hidden = true + }, 2000) +} + function renderStatus() { const failure = compilerFailure ?? lspFailure const error = failure ?? projectFailure ?? (typeAcquisitionFailure ? `Package types: ${typeAcquisitionFailure}` : "") @@ -3137,7 +3165,7 @@ function currentProjectState(): ProjectState { } } -function persistProjectState() { +function persistProjectState(forceUrl = false) { window.clearTimeout(projectPersistTimer) window.removeEventListener("beforeunload", flushProjectBeforeUnload) if (!projectPersistenceEnabled) return @@ -3147,8 +3175,8 @@ function persistProjectState() { localStorage.setItem(storageKey, serialized) lastStoredProject = serialized } - if (!playgroundSettings.saveToUrl || legacyQuery !== undefined) return - if (serialized === lastUrlProject) return + if (!forceUrl && (!playgroundSettings.saveToUrl || legacyQuery !== undefined)) return + if (!forceUrl && serialized === lastUrlProject) return const url = new URL(location.href) url.hash = `${projectHashPrefix.slice(1)}${LZString.compressToEncodedURIComponent(serialized)}` history.replaceState({}, "", url) diff --git a/packages/playground-v2/src/styles.css b/packages/playground-v2/src/styles.css index 0fdd72c9eed0..19719fd9cb29 100644 --- a/packages/playground-v2/src/styles.css +++ b/packages/playground-v2/src/styles.css @@ -350,6 +350,19 @@ body { white-space: normal; } +.shortcut-toast { + position: fixed; + z-index: 20; + right: 1rem; + bottom: 1rem; + padding: 0.6rem 0.85rem; + border: 1px solid var(--border); + border-radius: 0.35rem; + color: inherit; + background: var(--panel); + box-shadow: 0 0.25rem 1rem rgb(0 0 0 / 25%); +} + .form-dialog { width: min(28rem, calc(100vw - 2rem)); padding: 0; From 9b2f32b677db8a26bdfbbbcec87b70b7c6d423c2 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:11:10 -0800 Subject: [PATCH 2/2] Build Ctrl+S link from live project state Users can save while edits are still pending or browser storage is unavailable. Build the share URL from the current project so Ctrl+S still provides a usable link in those cases. --- packages/playground-v2/src/main.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/playground-v2/src/main.ts b/packages/playground-v2/src/main.ts index b2dc79dcc655..fb9c9369400c 100644 --- a/packages/playground-v2/src/main.ts +++ b/packages/playground-v2/src/main.ts @@ -465,13 +465,23 @@ document.addEventListener( event => { if (!(event.ctrlKey || event.metaKey) || event.altKey || event.key.toLowerCase() !== "s") return event.preventDefault() - persistProjectState(true) + if (event.repeat) return void (async () => { try { - await navigator.clipboard.writeText(location.href) - showShortcutToast("Playground link copied to clipboard.") - } catch { - await exportActions.showLink() + const link = projectUrl(currentProjectState()) + history.replaceState({}, "", link) + persistProjectState(true) + try { + await navigator.clipboard.writeText(link) + showShortcutToast("Playground link copied to clipboard.") + } catch { + await exportActions.showLink() + } + } catch (error) { + projectFailure = `Could not save the Playground link: ${error instanceof Error ? error.message : String(error)}` + console.error("Could not save the TypeScript Playground link", error) + renderStatus() + showShortcutToast("Could not save the Playground link.") } })() }, @@ -842,8 +852,6 @@ const exportActions = configureExports({ compile: () => compileActiveProject?.(), project() { const state = currentProjectState() - const url = new URL(location.href) - url.hash = `${projectHashPrefix.slice(1)}${LZString.compressToEncodedURIComponent(serializeProjectState(state))}` return { activeFile: relativeProjectPath( state.activeFile && state.files[state.activeFile] !== undefined @@ -861,7 +869,7 @@ const exportActions = configureExports({ symlinks: Object.fromEntries( Object.entries(projectSymlinks).map(([path, target]) => [relativeProjectPath(path), target]) ), - url: url.href, + url: projectUrl(state), } }, }) @@ -3165,6 +3173,12 @@ function currentProjectState(): ProjectState { } } +function projectUrl(state: ProjectState) { + const url = new URL(location.href) + url.hash = `${projectHashPrefix.slice(1)}${LZString.compressToEncodedURIComponent(serializeProjectState(state))}` + return url.href +} + function persistProjectState(forceUrl = false) { window.clearTimeout(projectPersistTimer) window.removeEventListener("beforeunload", flushProjectBeforeUnload)