|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +`bpmn-visualization` is a TypeScript library for visualizing process execution data on BPMN diagrams. It parses BPMN 2.0 XML, builds an internal model, and renders interactive diagrams using mxGraph with extensive customization capabilities. |
| 8 | + |
| 9 | +## Common Development Commands |
| 10 | + |
| 11 | +### Setup and Development |
| 12 | +```bash |
| 13 | +npm install # Install dependencies |
| 14 | +npm run dev # Start dev server at http://localhost:10001/dev/public/index.html |
| 15 | +npm run all # Full check: clean, lint, build, test (run before PRs) |
| 16 | +``` |
| 17 | + |
| 18 | +### Building |
| 19 | +```bash |
| 20 | +npm run build # TypeScript compilation check (no output) |
| 21 | +npm run build-bundles # Create distribution bundles (ESM, IIFE) |
| 22 | +npm run prepack # Full build: generate types + bundles (runs before npm pack) |
| 23 | +``` |
| 24 | + |
| 25 | +### Testing |
| 26 | +```bash |
| 27 | +npm test # Run all tests (unit + integration + e2e) |
| 28 | +npm run test:unit # Jest unit tests (parsers, converters, utilities) |
| 29 | +npm run test:unit:coverage # Unit tests with coverage |
| 30 | +npm run test:unit:watch # Watch mode for unit tests |
| 31 | +npm run test:integration # Integration tests (parser → renderer) |
| 32 | +npm run test:e2e # Playwright E2E with visual regression |
| 33 | +npm run test:e2e:verbose # E2E tests with debug output |
| 34 | +npm run test:perf # Performance benchmarks |
| 35 | +npm run test:bundles # Validate distribution bundles |
| 36 | +``` |
| 37 | + |
| 38 | +To run a single test file: |
| 39 | +```bash |
| 40 | +# Unit test |
| 41 | +npx jest test/unit/path/to/test-file.test.ts --config=./test/unit/jest.config.cjs |
| 42 | + |
| 43 | +# Integration test |
| 44 | +npx jest test/integration/path/to/test-file.test.ts --config=./test/integration/jest.config.cjs |
| 45 | + |
| 46 | +# E2E test |
| 47 | +npx jest test/e2e/path/to/test-file.test.ts --config=./test/e2e/jest.config.cjs |
| 48 | +``` |
| 49 | + |
| 50 | +### Linting and Code Quality |
| 51 | +```bash |
| 52 | +npm run lint # Auto-fix linting issues |
| 53 | +npm run lint-check # Check linting without fixing |
| 54 | +``` |
| 55 | + |
| 56 | +### Documentation |
| 57 | +```bash |
| 58 | +npm run docs # Generate all documentation |
| 59 | +npm run docs:user # Generate user documentation |
| 60 | +npm run docs:api # Generate API documentation with typedoc |
| 61 | +``` |
| 62 | + |
| 63 | +## Architecture Overview |
| 64 | + |
| 65 | +The library follows a **3-layer pipeline architecture** with clear separation of concerns: |
| 66 | + |
| 67 | +``` |
| 68 | +BpmnVisualization.load(xml) |
| 69 | + ├─ 1. Parse: XML → BpmnModel (internal representation) |
| 70 | + ├─ 2. Register: BpmnModel → RenderedModel (prepare for rendering) |
| 71 | + └─ 3. Render: RenderedModel → mxGraph cells (visual display) |
| 72 | +``` |
| 73 | + |
| 74 | +### 1. Parsing Layer (`src/component/parser/`) |
| 75 | + |
| 76 | +**Two-stage parsing:** |
| 77 | +- **Stage 1:** `BpmnXmlParser` converts BPMN XML to JSON using `fast-xml-parser` |
| 78 | +- **Stage 2:** `BpmnJsonParser` orchestrates converters to build `BpmnModel` |
| 79 | + |
| 80 | +**Key Converters** (in `src/component/parser/json/converter/`): |
| 81 | +- `ProcessConverter` - Activities, events, gateways, sequence flows |
| 82 | +- `CollaborationConverter` - Pools, lanes, message flows |
| 83 | +- `DiagramConverter` - Visual information (shapes, edges, bounds, labels) |
| 84 | +- `EventDefinitionConverter` - Event definitions (timer, message, error, etc.) |
| 85 | + |
| 86 | +**Critical Pattern:** Converters populate a shared `ConvertedElements` registry during semantic parsing, then `DiagramConverter` links visual bounds/waypoints to semantic elements. |
| 87 | + |
| 88 | +### 2. Model Layer (`src/model/bpmn/internal/`) |
| 89 | + |
| 90 | +**Core types:** |
| 91 | +- `BpmnModel` - Top-level container (pools, lanes, flowNodes, edges) |
| 92 | +- `Shape` - Combines `ShapeBpmnElement` (semantic) + `Bounds` (visual) + `Label` |
| 93 | +- `Edge` - Combines `EdgeBpmnElement` (semantic) + waypoints (visual) + `Label` |
| 94 | +- `ShapeBpmnElement` hierarchy - Rich type system with markers, event definitions, subprocess kinds |
| 95 | + |
| 96 | +**Important:** The model is **mxGraph-independent** - pure domain model of BPMN. |
| 97 | + |
| 98 | +### 3. Rendering Layer (`src/component/mxgraph/`) |
| 99 | + |
| 100 | +**Initialization chain:** |
| 101 | +``` |
| 102 | +GraphConfigurator |
| 103 | + ├─ Creates BpmnGraph (extends mxGraph) |
| 104 | + ├─ StyleConfigurator.configureStyles() |
| 105 | + ├─ registerShapes() - Custom mxShape implementations |
| 106 | + └─ registerEdgeMarkers() - Custom edge markers |
| 107 | +``` |
| 108 | + |
| 109 | +**BpmnRenderer:** |
| 110 | +- Converts `RenderedModel` to mxGraph cells via `insertVertex()`/`insertEdge()` |
| 111 | +- Uses `StyleComputer` to generate mxGraph style strings |
| 112 | +- Uses `CoordinatesTranslator` for coordinate transformations (BPMN uses absolute coords, mxGraph uses relative-to-parent) |
| 113 | +- Inserts in order: pools → lanes → subprocesses → flow nodes → boundary events → edges |
| 114 | + |
| 115 | +**Custom Shapes** (`src/component/mxgraph/shape/`): |
| 116 | +- Each BPMN element has custom `mxShape` subclass (EventShape, TaskShape, GatewayShape) |
| 117 | +- `IconPainter` renders BPMN-specific icons (event definitions, task markers) |
| 118 | +- `BpmnCanvas` extends mxGraph canvas with BPMN primitives |
| 119 | + |
| 120 | +### 4. Registry System (`src/component/registry/`) |
| 121 | + |
| 122 | +`BpmnElementsRegistry` provides the public API, aggregating: |
| 123 | +- `BpmnModelRegistry` - Semantic model access via `getBpmnSemantic(id)` |
| 124 | +- `HtmlElementRegistry` - Query DOM elements by ID or kind |
| 125 | +- `StyleRegistry` - Update mxGraph cell styles dynamically |
| 126 | +- `CssClassesRegistry` - Manage CSS classes on DOM elements |
| 127 | +- `OverlaysRegistry` - Add/remove overlays on elements |
| 128 | + |
| 129 | +**Key insight:** Bridges semantic model with rendered DOM/mxGraph, enabling runtime manipulation without re-parsing. |
| 130 | + |
| 131 | +## Testing Architecture |
| 132 | + |
| 133 | +**Multi-layer approach:** |
| 134 | +- **Unit** (`test/unit/`) - Jest with jsdom, tests parsers/converters in isolation |
| 135 | +- **Integration** (`test/integration/`) - Tests parser → renderer with real BPMN files |
| 136 | +- **E2E** (`test/e2e/`) - Playwright visual regression with image snapshots |
| 137 | +- **Performance** (`test/performance/`) - Load/render benchmarks |
| 138 | +- **Bundles** (`test/bundles/`) - Validates ESM/IIFE distributions |
| 139 | + |
| 140 | +**E2E Pattern:** Heavy use of image snapshots (in `__image_snapshots__/`) to verify visual rendering correctness across features. |
| 141 | + |
| 142 | +## Key Architectural Patterns |
| 143 | + |
| 144 | +### Converter Pattern |
| 145 | +Multiple specialized converters (`ProcessConverter`, `DiagramConverter`) each handle BPMN subdomains, all populating a shared `ConvertedElements` registry. This allows semantic and visual information to be processed separately then linked. |
| 146 | + |
| 147 | +### Coordinate Translation |
| 148 | +`CoordinatesTranslator` is critical because: |
| 149 | +- BPMN XML uses absolute coordinates |
| 150 | +- mxGraph uses relative-to-parent coordinates |
| 151 | +- Parent-child relationships (pools → lanes → flow nodes) require careful coordinate conversion |
| 152 | +- Labels have special offset calculations |
| 153 | + |
| 154 | +### Parent-Child Relationships |
| 155 | +Rendering order matters: |
| 156 | +1. Parents must be rendered before children |
| 157 | +2. Order: pools → lanes → subprocesses → flow nodes → boundary events → edges |
| 158 | +3. Managed through `parentId` references in the model |
| 159 | + |
| 160 | +### mxGraph Encapsulation |
| 161 | +mxGraph is an **implementation detail** - fully wrapped by `BpmnElementsRegistry`. Public API never exposes mxGraph types (except experimental `graph` property). This enables potential future replacement. |
| 162 | + |
| 163 | +## Adding a New BPMN Element Type |
| 164 | + |
| 165 | +1. **Add enum value** to `ShapeBpmnElementKind` in `src/model/bpmn/internal/shape/kinds.ts` |
| 166 | +2. **Extend model** - Add class in `ShapeBpmnElement.ts` if new semantic properties needed |
| 167 | +3. **Update converter** - Add parsing logic in `ProcessConverter` or relevant converter |
| 168 | +4. **Create shape class** - Extend `mxShape` in `src/component/mxgraph/shape/` |
| 169 | +5. **Register shape** - Add to `registerShapes()` in `register-style-definitions.ts` |
| 170 | +6. **Add styles** - Configure in `StyleConfigurator.ts` |
| 171 | +7. **Add tests** - Unit (parser), integration (parse→render), E2E (visual) |
| 172 | + |
| 173 | +## Understanding Data Flow |
| 174 | + |
| 175 | +Trace how a BPMN task flows through the system: |
| 176 | +1. BPMN XML `<task>` → `BpmnXmlParser` → JSON object |
| 177 | +2. JSON → `ProcessConverter.parseTask()` → creates `ShapeBpmnElement` |
| 178 | +3. Stored in `ConvertedElements` registry |
| 179 | +4. `DiagramConverter` finds matching `<BPMNShape>` → creates `Shape` (semantic + visual) |
| 180 | +5. `BpmnRenderer.insertShape()` → `graph.insertVertex()` with computed style |
| 181 | +6. `BpmnElementsRegistry` enables runtime access via ID or kind lookup |
| 182 | + |
| 183 | +## Requirements |
| 184 | + |
| 185 | +- **Node.js**: Version in `.nvmrc` file (run `nvm use` if using nvm) |
| 186 | +- **npm**: Version associated with Node.js version |
| 187 | +- **Supported OS**: Windows, Linux, macOS |
| 188 | + |
| 189 | +## Pre-commit Hooks |
| 190 | + |
| 191 | +The project uses Husky for pre-commit linting. If using a Node Version Manager and getting "Command not found" errors, create `~/.config/husky/init.sh` with: |
| 192 | +```bash |
| 193 | +export NVM_DIR="$HOME/.nvm" |
| 194 | +[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" |
| 195 | +``` |
| 196 | + |
| 197 | +## Pull Request Guidelines |
| 198 | + |
| 199 | +- External contributions only accepted for issues marked "PR Accepted" |
| 200 | +- Run `npm run all` before opening PR (builds, checks, tests everything) |
| 201 | +- PR title becomes the commit message (commits are squashed) |
| 202 | +- First PR requires signing the Contributor License Agreement |
0 commit comments