|
2 | 2 |
|
3 | 3 | This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
4 | 4 |
|
| 5 | +## Repository Overview |
| 6 | + |
| 7 | +This is the tldraw monorepo - an infinite canvas SDK for React applications. It's organized using yarn workspaces with packages for the core editor, UI components, shapes, tools, and supporting infrastructure. |
| 8 | + |
| 9 | +**Important**: There are CONTEXT.md files throughout this repository designed specifically for AI agents. Always read the relevant CONTEXT.md files to understand packages and their architecture. |
| 10 | + |
5 | 11 | ## Essential Commands |
6 | 12 |
|
7 | | -**Development:** |
8 | | -- `yarn dev` - Runs examples app with hot reload (localhost:5420) |
9 | | -- `yarn dev-app` - Runs dotcom client and workers |
10 | | -- `yarn dev-docs` - Runs documentation site |
11 | | -- `yarn dev-vscode` - Develops VS Code extension |
| 13 | +### Development |
| 14 | + |
| 15 | +- `yarn dev` - Start development server for examples app (main SDK showcase) |
| 16 | +- `yarn dev-app` - Start tldraw.com client app development |
| 17 | +- `yarn dev-docs` - Start documentation site development |
| 18 | +- `yarn dev-vscode` - Start VSCode extension development |
| 19 | +- `yarn context` - Find and display nearest CONTEXT.md file (supports -v, -r, -u flags) |
| 20 | +- `yarn refresh-context` - Update CONTEXT.md files using Claude Code CLI |
| 21 | + |
| 22 | +### Building |
12 | 23 |
|
13 | | -**Building & Testing:** |
14 | | -- `yarn build` - Builds all packages |
15 | | -- `yarn test run` - Runs all vitest tests (slow, use package-specific instead) |
16 | | -- `yarn test-ci` - Runs CI test suite |
17 | | -- `yarn typecheck` - Type checks entire codebase |
18 | | -- `yarn lint` - Lints all code |
19 | | -- `yarn format` - Formats code with Prettier |
| 24 | +- `yarn build` - Build all packages using lazy (incremental build system) |
| 25 | +- `yarn build-package` - Build all SDK packages only |
| 26 | +- `yarn build-app` - Build tldraw.com client app |
| 27 | +- `yarn build-docs` - Build documentation site |
20 | 28 |
|
21 | | -**Package-specific commands:** |
22 | | -- Run `yarn test run` within individual package directories for faster testing |
23 | | -- Run `yarn build` within package directories for specific builds |
| 29 | +### Testing |
| 30 | + |
| 31 | +- `yarn test` - Run all tests (slow, avoid unless necessary) |
| 32 | +- `yarn test run` - Run tests in specific workspace (cd to workspace first) |
| 33 | +- `yarn test-ci` - Run tests in CI mode |
| 34 | +- `yarn e2e` - Run end-to-end tests for examples |
| 35 | +- `yarn e2e-dotcom` - Run end-to-end tests for tldraw.com |
| 36 | + |
| 37 | +### Code Quality |
| 38 | + |
| 39 | +- `yarn lint` - Lint all packages |
| 40 | +- `yarn typecheck` - Type check all packages |
| 41 | +- `yarn format` - Format code with Prettier |
| 42 | +- `yarn api-check` - Validate public API consistency |
24 | 43 |
|
25 | 44 | ## Architecture Overview |
26 | 45 |
|
27 | | -This is a monorepo using Yarn workspaces with the following key components: |
28 | | - |
29 | | -**Core SDK Packages:** |
30 | | -- `packages/editor` - Core infinite canvas editor without shapes/tools |
31 | | -- `packages/tldraw` - Complete editor with default shapes, tools, and UI |
32 | | -- `packages/state` - Reactive signals library (like MobX/Solid) |
33 | | -- `packages/store` - Reactive in-memory database for tldraw documents |
34 | | -- `packages/tlschema` - Type definitions, validators, and migrations |
35 | | -- `packages/utils` - Internal utilities and helpers |
36 | | - |
37 | | -**Supporting Packages:** |
38 | | -- `packages/assets` - Fonts, icons, translations, watermarks |
39 | | -- `packages/sync` & `packages/sync-core` - Multiplayer SDK |
40 | | -- `packages/ai` - AI integration module |
41 | | -- `packages/validate` - Lightweight validation library |
42 | | - |
43 | | -**Applications:** |
44 | | -- `apps/examples` - SDK examples and development environment |
45 | | -- `apps/docs` - Documentation website (tldraw.dev) |
46 | | -- `apps/dotcom/*` - The tldraw.com application and workers |
47 | | -- `apps/vscode` - VS Code extension |
48 | | - |
49 | | -**Templates:** |
50 | | -- Various starting points for different frameworks and use cases |
51 | | - |
52 | | -## Key Development Patterns |
53 | | - |
54 | | -**Monorepo Management:** |
55 | | -- Uses `lazyrepo` for task orchestration and caching |
56 | | -- Workspace dependencies use `workspace:*` protocol |
57 | | -- Build system handles TypeScript compilation and API extraction |
58 | | - |
59 | | -**State Management:** |
60 | | -- Uses custom reactive signals (`@tldraw/state`) throughout |
61 | | -- Editor state is managed via reactive derivations and atoms |
62 | | -- Store manages document data with automatic persistence |
63 | | - |
64 | | -**Testing:** |
65 | | -- vitest for unit tests with package-specific test commands |
66 | | -- Playwright for E2E tests |
67 | | -- Test files should be co-located with source files (`.test.ts`) |
68 | | -- Integration tests go in `src/test/` directories |
69 | | -- Use tldraw workspace for testing editor with default shapes |
70 | | - |
71 | | -**Examples Development:** |
72 | | -- Examples live in `apps/examples/src/examples/` |
73 | | -- Each example needs `README.md` with frontmatter and component file |
74 | | -- Use footnote-style comments `// [1]` with explanations at bottom |
75 | | -- Follow writing guide in `apps/examples/writing-examples.md` |
76 | | - |
77 | | -## Important Notes |
78 | | - |
79 | | -- Node.js ^20.0.0 required |
80 | | -- Uses Yarn Berry (4.7.0+) - enable corepack |
81 | | -- TypeScript project with strict type checking |
82 | | -- CSS bundled separately (`tldraw.css`, `editor.css`) |
83 | | -- Watermark licensing applies to commercial use |
| 46 | +### Core Packages Structure |
| 47 | + |
| 48 | +**@tldraw/editor** - Foundational infinite canvas editor |
| 49 | + |
| 50 | +- No shapes, tools, or UI - just the core engine |
| 51 | +- State management using reactive signals (@tldraw/state) |
| 52 | +- Shape system via ShapeUtil, Tools via StateNode |
| 53 | +- Bindings system for shape relationships |
| 54 | + |
| 55 | +**@tldraw/tldraw** - Complete "batteries included" SDK |
| 56 | + |
| 57 | +- Builds on editor with full UI, shapes, and tools |
| 58 | +- Default shape utilities (text, draw, geo, arrow, etc.) |
| 59 | +- Complete tool set (select, hand, eraser, etc.) |
| 60 | +- Responsive UI system with customizable components |
| 61 | + |
| 62 | +**@tldraw/store** - Reactive client-side database |
| 63 | + |
| 64 | +- Document persistence with IndexedDB |
| 65 | +- Reactive updates using signals |
| 66 | +- Migration system for schema changes |
| 67 | + |
| 68 | +**@tldraw/tlschema** - Type definitions and validators |
| 69 | + |
| 70 | +- Shape, binding, and record type definitions |
| 71 | +- Validation schemas and migrations |
| 72 | +- Shared data structures |
| 73 | + |
| 74 | +### Key Architectural Patterns |
| 75 | + |
| 76 | +**Reactive State Management** |
| 77 | + |
| 78 | +- Uses @tldraw/state for reactive signals (Atom, Computed) |
| 79 | +- All editor state is reactive and observable |
| 80 | +- Automatic dependency tracking prevents unnecessary re-renders |
| 81 | + |
| 82 | +**Shape System** |
| 83 | + |
| 84 | +- Each shape type has a ShapeUtil class defining behavior |
| 85 | +- ShapeUtil handles geometry, rendering, interactions |
| 86 | +- Extensible - custom shapes via new ShapeUtil implementations |
| 87 | + |
| 88 | +**Tools as State Machines** |
| 89 | + |
| 90 | +- Tools implemented as StateNode hierarchies |
| 91 | +- Event-driven with pointer, keyboard, tick handlers |
| 92 | +- Complex tools have child states (e.g., SelectTool has Brushing, Translating, etc.) |
| 93 | + |
| 94 | +**Bindings System** |
| 95 | + |
| 96 | +- Relationships between shapes (arrows to shapes, etc.) |
| 97 | +- BindingUtil classes define binding behavior |
| 98 | +- Automatic updates when connected shapes change |
| 99 | + |
| 100 | +## Testing Patterns |
| 101 | + |
| 102 | +**Vitest Tests** |
| 103 | + |
| 104 | +- Unit tests: name test files after the file being tested (e.g., `LicenseManager.test.ts`) |
| 105 | +- Integration tests: use `src/test/feature-name.test.ts` format |
| 106 | +- Test in tldraw workspace if you need default shapes/tools |
| 107 | + |
| 108 | +**Running Tests** |
| 109 | + |
| 110 | +- Run from specific workspace directory: `cd packages/editor && yarn test run` |
| 111 | +- Filter with additional args: `yarn test run --grep "selection"` |
| 112 | +- Avoid `yarn test` from root (slow and hard to filter) |
| 113 | + |
| 114 | +**Playwright E2E Tests** |
| 115 | + |
| 116 | +- Located in `apps/examples/e2e/` and `apps/dotcom/client/e2e/` |
| 117 | +- Use `yarn e2e` and `yarn e2e-dotcom` commands |
| 118 | + |
| 119 | +## Development Workspace Structure |
| 120 | + |
| 121 | +``` |
| 122 | +apps/ |
| 123 | +├── examples/ # SDK examples and demos |
| 124 | +├── docs/ # Documentation site (tldraw.dev) |
| 125 | +├── dotcom/ # tldraw.com application |
| 126 | +│ ├── client/ # Frontend React app |
| 127 | +│ ├── sync-worker/ # Multiplayer backend |
| 128 | +│ └── asset-upload-worker/ |
| 129 | +└── vscode/ # VSCode extension |
| 130 | +
|
| 131 | +packages/ |
| 132 | +├── editor/ # Core editor engine |
| 133 | +├── tldraw/ # Complete SDK with UI |
| 134 | +├── store/ # Reactive database |
| 135 | +├── tlschema/ # Type definitions |
| 136 | +├── state/ # Reactive signals library |
| 137 | +├── sync/ # Multiplayer SDK |
| 138 | +├── utils/ # Shared utilities |
| 139 | +├── validate/ # Lightweight validation library |
| 140 | +├── assets/ # Icons, fonts, translations |
| 141 | +├── ai/ # AI module SDK addon |
| 142 | +└── create-tldraw/ # npm create tldraw CLI |
| 143 | +
|
| 144 | +templates/ # Starter templates for different frameworks |
| 145 | +``` |
| 146 | + |
| 147 | +## Build System (LazyRepo) |
| 148 | + |
| 149 | +Uses `lazyrepo` for incremental builds with caching: |
| 150 | + |
| 151 | +- `yarn build` builds only what changed |
| 152 | +- Workspace dependencies handled automatically |
| 153 | +- Caching based on file inputs/outputs |
| 154 | +- Parallel execution where possible |
| 155 | + |
| 156 | +## Key Development Notes |
| 157 | + |
| 158 | +**TypeScript** |
| 159 | + |
| 160 | +- Uses workspace references for fast incremental compilation |
| 161 | +- Run `yarn typecheck` before commits |
| 162 | +- API surface validated with Microsoft API Extractor |
| 163 | + |
| 164 | +**Monorepo Management** |
| 165 | + |
| 166 | +- Yarn workspaces with berry (yarn 4.x) |
| 167 | +- Use `yarn` not `npm` - packageManager field enforces this |
| 168 | +- Dependencies managed at workspace level where possible |
| 169 | + |
| 170 | +**Asset Management** |
| 171 | + |
| 172 | +- Icons, fonts, translations in `/assets` (managed centrally) |
| 173 | +- Run `yarn refresh-assets` after asset changes |
| 174 | +- Assets bundled into packages during build |
| 175 | +- Automatic optimization and deduplication |
| 176 | + |
| 177 | +**Example Development** |
| 178 | + |
| 179 | +- Main development happens in `apps/examples` |
| 180 | +- Examples showcase SDK capabilities |
| 181 | +- See `apps/examples/writing-examples.md` for guidelines |
| 182 | + |
| 183 | +## Creating New Components |
| 184 | + |
| 185 | +**Custom Shapes** |
| 186 | + |
| 187 | +1. Create ShapeUtil class extending base ShapeUtil |
| 188 | +2. Implement required methods (getGeometry, component, indicator) |
| 189 | +3. Register in editor via shapeUtils prop |
| 190 | + |
| 191 | +**Custom Tools** |
| 192 | + |
| 193 | +1. Create StateNode class with tool logic |
| 194 | +2. Define state machine with onEnter/onExit/event handlers |
| 195 | +3. Register in editor via tools prop |
| 196 | + |
| 197 | +**UI Customization** |
| 198 | + |
| 199 | +- Every tldraw UI component can be overridden |
| 200 | +- Pass custom components via `components` prop |
| 201 | +- See existing components for patterns |
| 202 | + |
| 203 | +## Integration Notes |
| 204 | + |
| 205 | +**With External Apps** |
| 206 | + |
| 207 | +- Import CSS: `import 'tldraw/tldraw.css'` (full) or `import '@tldraw/editor/editor.css'` (editor only) |
| 208 | +- Requires React 18+ and modern bundler |
| 209 | +- Support for Vite, Next.js, and other React frameworks |
| 210 | + |
| 211 | +**Collaboration** |
| 212 | + |
| 213 | +- Use @tldraw/sync for multiplayer |
| 214 | +- WebSocket-based real-time synchronization |
| 215 | +- See templates/sync-cloudflare for implementation example |
| 216 | + |
| 217 | +**Licensing** |
| 218 | + |
| 219 | +- SDK has "Made with tldraw" watermark by default |
| 220 | +- Business license removes watermark |
| 221 | +- See tldraw.dev for licensing details |
| 222 | + |
| 223 | +# important-instruction-reminders |
| 224 | + |
| 225 | +Do what has been asked; nothing more, nothing less. |
| 226 | +NEVER create files unless they're absolutely necessary for achieving your goal. |
| 227 | +ALWAYS prefer editing an existing file to creating a new one. |
| 228 | +NEVER proactively create documentation files (\*.md) or README files. Only create documentation files if explicitly requested by the User. |
| 229 | + |
| 230 | +# cursor-rules-integration |
| 231 | + |
| 232 | +When writing examples, be sure to read the `./apps/examples/writing-examples.md` file for proper example patterns and conventions. |
0 commit comments