|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +# Playground |
| 7 | + |
| 8 | +## Overview |
| 9 | +The LabelStudio Playground is a standalone, embeddable React application for editing and previewing LabelStudio XML labeling configurations. It is designed to be embedded via iframe in documentation or external web applications, providing a focused environment for working with XML-based labeling configs and live previewing the LabelStudio interface. |
| 10 | + |
| 11 | +## Main Features |
| 12 | + |
| 13 | +- **Live XML Config Editing:** Edit Label Studio XML configs in real time and instantly preview the result. |
| 14 | +- **Sample Data Generation:** Automatically generates sample data for all supported media types (image, audio, video, PDF, website, CSV, OCR, etc.) using public domain URLs (primarily from Wikimedia Commons). This ensures copyright-safe, always-accessible previews. |
| 15 | +- **Live Annotation Output:** The preview panel displays the current annotation as a live-updating JSON string, reflecting user interactions in real time. |
| 16 | +- **Sticky Bottom Panel:** The right preview panel ensures the bottom panel (annotation controls) is always visible and sticky, with the main preview area scrollable. |
| 17 | +- **Resizable Panels:** The left (editor) and right (preview) panels are resizable and fully responsive. |
| 18 | +- **Robust Error Handling:** Displays clear error messages for invalid configs, network issues, or MobX State Tree (MST) errors. All async and MST errors are handled gracefully to avoid UI crashes. |
| 19 | + |
| 20 | +## Main Components |
| 21 | + |
| 22 | +### 1. `PlaygroundApp` |
| 23 | +- **Location:** `src/components/PlaygroundApp.tsx` |
| 24 | +- **Role:** The main application component. Handles: |
| 25 | + - UI rendering and atom wiring only; all state is managed via Jotai atoms. |
| 26 | + - Reads and writes config, loading, error, interfaces state via atoms. |
| 27 | + - Uses utility functions for parsing URL parameters and interface options. |
| 28 | + - Renders the code editor and preview panels side by side using Tailwind CSS for layout. |
| 29 | + |
| 30 | +### 2. `PlaygroundPreview` |
| 31 | +- **Location:** `src/components/PlaygroundPreview.tsx` |
| 32 | +- **Role:** Renders the live LabelStudio preview panel. |
| 33 | + - Receives all state as props (from atoms in PlaygroundApp). |
| 34 | + - Dynamically loads the `@humansignal/editor` package and instantiates a LabelStudio instance with the current config and interface options. |
| 35 | + - Handles cleanup of the LabelStudio instance on config/interface changes or unmount. |
| 36 | + - Observes MST annotation changes and updates a Jotai atom or local state with the serialized annotation JSON, which is displayed below the preview. |
| 37 | + - Displays loading and error states using Tailwind classes. |
| 38 | + |
| 39 | +### 3. `main.tsx` |
| 40 | +- **Role:** Entry point. Mounts the `PlaygroundApp` to the DOM. |
| 41 | + |
| 42 | +## State Management |
| 43 | +- All application state (config, loading, error, interfaces, annotation, sample task) is managed using Jotai atoms, defined in `src/atoms/configAtoms.ts`. |
| 44 | +- Components use the `useAtom` hook to read and write state. |
| 45 | +- This ensures a strict separation of state logic from UI, and enables easy extension and testing. |
| 46 | + |
| 47 | +## Utility Functions |
| 48 | +- All logic for parsing URL parameters and interface options is placed in strict utility functions in `src/utils/query.ts`. |
| 49 | +- Sample data generation is handled in `src/utils/generateSampleTask.ts`. |
| 50 | +- Components import and use these utilities for all non-UI logic. |
| 51 | + |
| 52 | +## Data Flow |
| 53 | +- On load, `PlaygroundApp` uses utility functions to parse URL parameters: |
| 54 | + - `?config` (base64-encoded XML config) |
| 55 | + - `?configUrl` (URL to fetch XML config) |
| 56 | + - `?interfaces` (comma-separated list of LabelStudio interface options) |
| 57 | +- The config, loading, error, interfaces, annotation, and sample task state are set via Jotai atoms. |
| 58 | +- The code editor is a controlled component, updating the config atom on change. |
| 59 | +- The preview panel receives the current config and interface options as props and re-renders the LabelStudio instance accordingly. |
| 60 | +- Annotation changes in the preview are observed and the serialized annotation is displayed live below the preview. |
| 61 | + |
| 62 | +## MobX State Tree (MST) Integration |
| 63 | +- All MST model mutations (including cleanup) are performed via MST actions to avoid protection errors. |
| 64 | +- The annotation model includes a `cleanup` action for safe teardown, which is called from React cleanup. |
| 65 | +- Only plain objects (not MST models) are stored in React state or Jotai atoms. |
| 66 | + |
| 67 | +## Underlying Libraries |
| 68 | + |
| 69 | +### React |
| 70 | +- The app is built with React (function components, hooks, strict mode). |
| 71 | +- State and effects are managed with Jotai atoms and hooks. |
| 72 | + |
| 73 | +### Jotai |
| 74 | +- Used for all state management (config, loading, error, interfaces, annotation, sample task). |
| 75 | +- Atoms are defined in `src/atoms/configAtoms.ts`. |
| 76 | +- Components use `useAtom` for state access and updates. |
| 77 | + |
| 78 | +### Tailwind CSS |
| 79 | +- All layout, spacing, color, and typography is handled with Tailwind utility classes. |
| 80 | +- Only semantic and token-based Tailwind classes are used, following project rules. |
| 81 | + |
| 82 | +### @humansignal/ui |
| 83 | +- Provides the `CodeEditor` component for XML editing. |
| 84 | +- Ensures consistent UI and design token usage across the app. |
| 85 | + |
| 86 | +### @humansignal/editor |
| 87 | +- Provides the LabelStudio labeling interface for live preview. |
| 88 | +- Dynamically imported in the preview panel for performance and to avoid loading unnecessary code until needed. |
| 89 | +- The LabelStudio instance is created with the current config and interface options, and is destroyed/cleaned up on changes. |
| 90 | + |
| 91 | +## URL-based API |
| 92 | +- The app can be configured via URL parameters: |
| 93 | + - `?config` (base64-encoded XML config string) |
| 94 | + - `?configUrl` (URL to fetch XML config) |
| 95 | + - `?interfaces` (comma-separated list of LabelStudio interface options) |
| 96 | +- This allows external documentation or apps to embed the playground with preloaded configs and custom preview options. |
| 97 | + |
| 98 | +## Embeddability |
| 99 | +- The app is fully responsive and designed to be embedded via iframe. |
| 100 | +- All UI is self-contained and does not require authentication or external state. |
| 101 | + |
| 102 | +## Extensibility |
| 103 | +- Components are split into single-file-per-component for maintainability, and all live under `src/components/`. |
| 104 | +- All state is managed via Jotai atoms in `src/atoms/`. |
| 105 | +- All logic is placed in strict utility functions in `src/utils/`. |
| 106 | +- The code editor and preview logic are decoupled, allowing for future enhancements (e.g., validation, additional preview options, custom data, etc). |
| 107 | +- The app can be extended to support more URL parameters, additional LabelStudio features, or integration with other HumanSignal libraries. |
| 108 | +- To add new sample data types, update `generateSampleTask.ts` with new logic and public domain URLs. |
| 109 | +- To customize annotation output, update the preview logic to observe and display additional MST state as needed. |
| 110 | + |
| 111 | +## Directory Structure |
| 112 | + |
| 113 | +``` |
| 114 | +web/apps/playground/ |
| 115 | +├── src/ |
| 116 | +│ ├── atoms/ # Jotai atoms for state management |
| 117 | +│ ├── components/ # React components |
| 118 | +│ │ ├── BottomPanel/ # Data Input/Output Bottom panel component |
| 119 | +│ │ ├── EditorPanel/ # Labelling Config Editor panel component |
| 120 | +│ │ ├── PlaygroundApp/ # Main app component |
| 121 | +│ │ └── PreviewPanel/ # Labelling Preview panel component |
| 122 | +│ ├── utils/ # Utility functions |
| 123 | +│ ├── index.html # Entry HTML file |
| 124 | +│ └── main.tsx # Entry point |
| 125 | +├── .babelrc # Babel configuration |
| 126 | +├── jest.config.ts # Jest configuration |
| 127 | +├── project.json # Nx project configuration |
| 128 | +├── tsconfig.json # TypeScript configuration |
| 129 | +├── tsconfig.app.json # App-specific TypeScript configuration |
| 130 | +└── tsconfig.spec.json # Test-specific TypeScript configuration |
| 131 | +``` |
| 132 | + |
| 133 | +## Summary |
| 134 | +The Playground app is a modern, modular, and embeddable tool for experimenting with and sharing LabelStudio configs. It leverages the HumanSignal UI and editor libraries, is styled with Tailwind, uses Jotai for state, and is designed for easy integration into documentation and external web applications. It now features robust sample data generation, live annotation output, and safe MobX State Tree integration for a seamless developer experience. |
0 commit comments