Files
python-editor/src/editor_app/routers/frontend.py
Jimmy ca0ca6fe7e Add local-mode workspace, ZIP import/export, and richer pin/ADC/serial sims
Boot:
- Editor now picks local vs server mode based on URL flag, sign-in
  state, and a stale local-mode flag. Signed-in users are no longer
  bounced to IndexedDB if they had previously clicked "Use locally".

Local mode:
- New LocalWorkspaceClient (src/static/local-workspace.js) with
  pluggable IndexedDB and File System Access backends. Picked folder
  handles persist across reloads with a Reconnect button when the
  permission lapses.
- Static-only host: scripts/serve_static_editor.py serves src/static/
  with COOP/COEP so SharedArrayBuffer-backed sims keep working.
- Bundled MicroPython stubs ship under src/static/bundled-lib/ for
  static hosting; FastAPI also exposes them at /api/public/lib-bundle.

Workspace import / export:
- Zero-dep ZIP encoder + reader (STORE + DEFLATE via
  DecompressionStream). Export/Import buttons in the workspace badge
  work in both local and server modes; imports are confined to code/.

Pin / ADC / Serial simulation:
- machine.py grows ADC, UART, expanded Pin, and PWM mocks, all driven
  by SharedArrayBuffer when cross-origin isolated and falling back to
  postMessage + [pin-out] stdout markers otherwise — pins, ADC slider,
  and serial input now keep working over plain HTTP / LAN-IP origins.
- NeoPixel pins are claimed via a [pin-claim] marker and dropped from
  the Pins panel so the data line doesn't flicker per write().
- New demos: adc_slider_demo.py, pin_demo.py, serial_demo.py.

Lib layout:
- Single source of truth at repo lib/; workspace/lib/ caching layer
  removed and the directory deleted. Filesystem service reads stubs
  directly from PROJECT_ROOT/lib.

UI:
- Home page slimmed to "Sign in" + "Use locally" with optional editor
  / manage-users links. Admin user/invite UI moved to /users.
- Workspace badge gains storage indicator, Folder…/Reconnect, Export,
  Import, and Exit controls.
- Mobile-friendly tweaks: safer-area padding, larger touch targets,
  iOS-zoom-proof serial input, file-tree highlight fix.

Tests:
- test_auth.py patches PROJECT_ROOT for the lib-shared test so the
  repo-root lib refactor stays green. test_api.py asserts the new
  "LED Editor" branding.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-10 06:16:02 +12:00

66 lines
1.9 KiB
Python

from fastapi import APIRouter
from fastapi.responses import FileResponse
from editor_app.config import PROJECT_ROOT, STATIC_DIR
router = APIRouter()
@router.get("/api/public/lib-bundle")
async def serve_lib_bundle():
"""Public, unauthenticated dump of the read-only `lib/` stubs.
Local-mode browsers (no login, files in IndexedDB) need access to the
MicroPython mocks so completion, diagnostics, and `runpy` can resolve
`from machine import …`. Reading these is read-only and contains no
user data, so it is safe to expose without auth."""
bundle_root = (PROJECT_ROOT / "lib").resolve()
files: dict[str, str] = {}
if bundle_root.is_dir():
for path in sorted(bundle_root.rglob("*.py")):
if not path.is_file():
continue
try:
rel = path.relative_to(bundle_root)
except ValueError:
continue
if any(part.startswith(".") for part in rel.parts):
continue
try:
files[str(rel).replace("\\", "/")] = path.read_text(encoding="utf-8")
except (UnicodeDecodeError, OSError):
continue
return {"files": files}
@router.get("/")
async def serve_home():
return FileResponse(STATIC_DIR / "home.html")
@router.get("/editor")
async def serve_frontend():
return FileResponse(STATIC_DIR / "index.html")
@router.get("/tutorial")
async def serve_tutorial():
return FileResponse(STATIC_DIR / "tutorial.html")
@router.get("/login")
async def serve_login():
return FileResponse(STATIC_DIR / "login.html")
@router.get("/register")
async def serve_register():
return FileResponse(STATIC_DIR / "register.html")
@router.get("/users")
async def serve_users():
"""Admin panel for managing accounts and invites. Auth is enforced by
the underlying `/api/users*` endpoints, not the static page itself."""
return FileResponse(STATIC_DIR / "users.html")