How 0xjoy07/xcalidraw works
Purpose and Entry Points
Xcalidraw is a hand-drawn, persistent canvas application with authenticated sharing and role-based access; it does not provide live multiplayer editing. The React/Vite client starts at client/src/main.tsx and routes through client/src/App.tsx. The Bun/Express API starts at server/src/index.js.
Client Application Shell and Access
The client separates public/auth screens, protected canvas/dashboard access, and an in-app documentation area. Authentication state is held in React context, while protected navigation gates canvas resources. Relevant paths: client/src/auth/AuthContext.tsx, client/src/routes/ProtectedRoute.tsx, client/src/pages/CanvasDashboardPage.tsx.
Canvas Interaction and Rendering
Canvas is the primary interactive surface: it owns pointer/touch-driven drawing, pan/zoom, selection, move/resize, and tool interaction. Rendering and hit testing are separated from the root interaction component, supporting RoughJS-style element drawing and spatial targeting. Core paths: client/src/canvas/Canvas.tsx, client/src/canvas/renderElement.ts, client/src/canvas/hitTest.ts.
Canvas State, Coordinate Model, and Persistence
Canvas elements and editor state are centralized in a Zustand store and represented by shared element types. The autosave boundary performs immediate localStorage persistence, then debounced API synchronization, allowing responsive offline-tolerant edits before MongoDB persistence. Core paths: client/src/store/elementsStore.ts, client/src/types/element.ts, client/src/hooks/useCanvasAutosave.ts.
API Boundary and Canvas Authorization
Client API modules isolate HTTP calls for authentication and canvas CRUD/sharing from UI and stores. Server canvas routes enforce authenticated access and resolve Owner, Editor, or Viewer permissions before operating on canvas documents and collaborator records. Core paths: client/src/lib/canvasApi.ts, server/src/routes/canvases.js, server/src/utils/resolveAccess.js.
Identity, Sessions, and Recovery
The server implements email/password and Google/GitHub OAuth through Passport. JWT access tokens are used client-side in memory, while refresh tokens use HTTP-only cookies and can be persisted/revoked. Password-reset tokens and transactional email complete account recovery. Core paths: server/src/routes/auth.js, server/src/config/passport.js, server/src/models/RefreshToken.js.
Data and External Infrastructure
MongoDB, accessed through Mongoose, is the durable system of record for users, canvases, collaboration grants, refresh tokens, and reset-token state. Nodemailer sends password recovery and collaborator-invite messages. Required external provisioning includes MongoDB, Google OAuth, GitHub OAuth, and an SMTP/Gmail App Password. Core paths: server/src/config/db.js, server/src/models/Canvas.js, server/src/lib/mailer.js.
Runtime and Deployment Boundaries
The frontend is a TypeScript React application built and served by Vite, with Vercel deployment configuration. The backend is JavaScript running on Bun with Express; its environment controls database, client-origin, OAuth, token, and mail configuration. Core paths: client/vite.config.ts, client/vercel.json, server/package.json.
Main entry points: auth.js Auth & recovery routes · canvases.js Canvas routes · App.tsx App routing shell · Canvas.tsx Interactive canvas
Components 24
React client
Vite single-page application.
- Client entry
client/src/main.tsx - App routing shell · Separates public, protected, and documentation areas.
client/src/App.tsx - Auth state · Holds client authentication state and tokens.
client/src/auth/AuthContext.tsx - Protected navigation · Gates dashboard and canvas resources.
client/src/routes/ProtectedRoute.tsx - Canvas dashboard
client/src/pages/CanvasDashboardPage.tsx - Interactive canvas · Handles drawing, gestures, selection, transforms, and tools.
client/src/canvas/Canvas.tsx - Rendering & targeting · Draws elements and performs hit testing.
client/src/canvas/renderElement.ts - Editor state · Centralizes elements and editor state.
client/src/store/elementsStore.ts - Element model
client/src/types/element.ts - Autosave boundary · Writes locally immediately and syncs API changes after debounce.
client/src/hooks/useCanvasAutosave.ts - Canvas API client · Isolates canvas CRUD and sharing requests.
client/src/lib/canvasApi.ts - Vite & Vercel delivery · Builds the client and configures Vercel hosting.
client/vercel.json
Bun / Express API
Authenticated canvas and identity services.
- API entry
server/src/index.js - Canvas routes · Handles canvas CRUD and collaborator sharing.
server/src/routes/canvases.js - Role resolution · Resolves Owner, Editor, or Viewer access.
server/src/utils/resolveAccess.js - Auth & recovery routes · Provides password login, OAuth, refresh, and recovery flows.
server/src/routes/auth.js - OAuth strategies · Supports Google and GitHub identity providers.
server/src/config/passport.js - Refresh token records · Supports persisted and revocable HTTP-only-cookie sessions.
server/src/models/RefreshToken.js - Bun runtime · Runs the Express API.
server/package.json
Data & external services
Durable storage and third-party providers.
- MongoDB connection · Connects the API to the durable system of record.
server/src/config/db.js - Canvas documents · Stores canvases and collaboration state.
server/src/models/Canvas.js - Transactional mail · Sends recovery and collaborator-invite email.
server/src/lib/mailer.js - Google & GitHub OAuth
- SMTP / Gmail
Connections 25
- Client entry → App routing shell: mounts
- App routing shell → Auth state: uses
- App routing shell → Protected navigation: routes through
- Protected navigation → Canvas dashboard: permits
- Canvas dashboard → Interactive canvas: hosts
- Interactive canvas → Rendering & targeting: draws and targets through
- Interactive canvas → Editor state: reads and updates
- Editor state → Element model: uses
- Autosave boundary → Editor state: observes changes
- Autosave boundary → Canvas API client: debounced sync
- Canvas API client → Canvas routes: HTTPS canvas requests
- Auth state → Auth & recovery routes: authentication requests
- API entry → Canvas routes: mounts
- API entry → Auth & recovery routes: mounts
- Bun runtime → API entry: runs
- Canvas routes → Role resolution: authorizes with
- Canvas routes → Canvas documents: reads and writes
- Canvas documents → MongoDB connection: persists in
- Auth & recovery routes → OAuth strategies: delegates OAuth to
- OAuth strategies → Google & GitHub OAuth: authenticates against
- Auth & recovery routes → Refresh token records: issues and revokes
- Refresh token records → MongoDB connection: persists in
- Auth & recovery routes → Transactional mail: sends recovery mail
- Transactional mail → SMTP / Gmail: delivers via
- Vite & Vercel delivery → Client entry: builds and serves