Skip to content

Commit 94b22f9

Browse files
authored
Merge pull request #17 from githubnext/copilot/create-interactive-playground
Add interactive playground with live TypeScript execution
2 parents 045cdb3 + cb39efe commit 94b22f9

5 files changed

Lines changed: 714 additions & 85 deletions

File tree

biome.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"files": {
99
"ignoreUnknown": false,
10-
"ignore": ["dist/**", "node_modules/**", "*.d.ts"]
10+
"ignore": ["dist/**", "node_modules/**", "*.d.ts", "playground/**/*.js"]
1111
},
1212
"formatter": {
1313
"enabled": true,

docs/playground.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Playground Pages — Design & Requirements
2+
3+
Every feature in **tsb** ships with an interactive playground page hosted on
4+
GitHub Pages. This document describes the required properties that every
5+
playground page must satisfy.
6+
7+
## Required Properties
8+
9+
### 1. Executable Code Blocks
10+
11+
Each code example on a playground page **must** be executable in the browser.
12+
Users can edit the code, click **▶ Run** (or press **Ctrl+Enter**), and see
13+
real output rendered below the editor.
14+
15+
- Use `<textarea class="playground-editor">` for the editable code area.
16+
- Wrap each example in a `<div class="playground-block">` container.
17+
- Provide **▶ Run** and **↺ Reset** buttons in a `.playground-header`.
18+
- Display results in a `<div class="playground-output">`.
19+
20+
### 2. Full TypeScript Support
21+
22+
Code blocks accept TypeScript. The playground runtime loads the TypeScript
23+
compiler from CDN and transpiles user code to JavaScript before execution.
24+
Users can write type annotations, interfaces, and generics — the compiler
25+
strips them automatically.
26+
27+
- TypeScript compiler: loaded from `https://cdn.jsdelivr.net/npm/typescript@5/lib/typescript.js`
28+
- No WASM required — the compiler runs natively in JavaScript.
29+
30+
### 3. Live tsb Library Access
31+
32+
The full tsb API is available in every code block. Users import from `"tsb"`
33+
exactly as they would in a real project:
34+
35+
```typescript
36+
import { Index, Series, Dtype } from "tsb";
37+
```
38+
39+
The playground runtime transforms these imports to reference the browser
40+
bundle built by CI (`playground/dist/index.js`).
41+
42+
### 4. Output Capture
43+
44+
All `console.log()`, `console.error()`, and `console.warn()` output is
45+
intercepted and rendered in the output area. tsb objects (Index, Series, etc.)
46+
display their human-readable `toString()` representation.
47+
48+
### 5. Dark Theme
49+
50+
Pages use the project's standard dark GitHub-inspired theme (CSS variables
51+
`--bg`, `--surface`, `--border`, `--text`, `--accent`, `--green`). See
52+
`playground/index-playground.html` for the canonical style definitions.
53+
54+
### 6. Loading State
55+
56+
A loading overlay with a spinner is shown while the TypeScript compiler and
57+
tsb bundle are fetched. All Run buttons are disabled until initialization
58+
completes, preventing premature execution.
59+
60+
### 7. Keyboard Shortcuts
61+
62+
- **Ctrl+Enter** (or **Cmd+Enter** on macOS): Run the current code block.
63+
- **Tab**: Insert two spaces (does not move focus).
64+
65+
### 8. Reset Support
66+
67+
Every code block has a **↺ Reset** button that restores the original example
68+
code and clears the output.
69+
70+
### 9. Free-form Scratch Pad
71+
72+
Each playground page should include a final "Try It Yourself" section with an
73+
empty (or lightly seeded) code block where users can experiment freely.
74+
75+
## Architecture
76+
77+
```
78+
playground/
79+
index.html ← Landing page / feature roadmap
80+
index-playground.html ← Index & RangeIndex interactive tutorial
81+
playground-runtime.js ← Shared runtime (TS transpilation + execution)
82+
dist/ ← Built by CI (bun build, not committed)
83+
index.js ← tsb browser bundle (ESM)
84+
```
85+
86+
### Runtime Flow
87+
88+
1. The HTML page loads `playground-runtime.js` as an ES module.
89+
2. The runtime imports the tsb bundle from `./dist/index.js` and stores it
90+
on `window.__tsb`.
91+
3. The TypeScript compiler is loaded from CDN via a dynamic `<script>` tag.
92+
4. Each `.playground-block` is initialized with event listeners for Run,
93+
Reset, Tab, and Ctrl+Enter.
94+
5. On **Run**:
95+
- `import { … } from "tsb"` is rewritten to `const { … } = window.__tsb;`
96+
- The TypeScript compiler transpiles the code to JavaScript.
97+
- The JavaScript is executed via `new Function()` with console output
98+
captured and displayed.
99+
100+
### Adding a New Playground Page
101+
102+
1. Create `playground/{feature}.html` following the template in
103+
`index-playground.html`.
104+
2. Include the runtime: `<script type="module" src="playground-runtime.js"></script>`
105+
3. Add the loading overlay (`#playground-loading`).
106+
4. Structure examples as `.playground-block` containers with `.playground-editor`,
107+
`.playground-run`, `.playground-reset`, and `.playground-output` elements.
108+
5. End with a "Try It Yourself" scratch pad.
109+
6. Link the page from `playground/index.html`.
110+
111+
### Building the Bundle Locally
112+
113+
```bash
114+
bun build ./src/index.ts --outdir ./playground/dist --target browser --minify
115+
```
116+
117+
The CI pipeline (`pages.yml`) runs this automatically during deployment.
118+
119+
## Non-Goals (Current Scope)
120+
121+
- **Syntax highlighting** in the editor: the current implementation uses a
122+
plain `<textarea>`. A future enhancement could integrate CodeMirror or
123+
Monaco for richer editing.
124+
- **Infinite loop protection**: long-running or infinite loops will hang the
125+
browser tab. A Web Worker–based sandbox could be added later.
126+
- **Type checking**: the playground transpiles but does not type-check.
127+
Adding diagnostics display is a potential future enhancement.

0 commit comments

Comments
 (0)