How synergycodes/ng-diagram works
1. Repository shape
This is a pnpm monorepo organized around one primary product: the Angular library in packages/ng-diagram, plus three supporting applications/tools:
- apps/docs: the documentation and examples site
- apps/angular-demo: a runnable demo app
- tools/mcp-server: an MCP server for doc and symbol search
2. Primary product boundary: the Angular library
packages/ng-diagram is the main deliverable published to npm. It exposes the public API from src/public-api.ts and is packaged as an Angular library. The library is split into:
- src/lib: public components, directives, services, styles, and types
- src/core: the internal engine for state, commands, routing, middleware, and interaction handling
This separation is the key architectural boundary: UI-facing Angular API on one side, diagram engine internals on the other.
3. Core runtime model
The diagram is centered on a reactive model built with Angular signals, as described in the README. Model initialization and adaptation live in src/lib/model, while the engine-side model logic lives in src/core. The main flow is:
- consumer provides a model via initializeModel(...) or a custom model adapter
- the diagram component consumes that model
- internal services and middleware mutate state through controlled update paths
4. Rendering layer
The visible diagram is composed from Angular components under src/lib/components:
- ng-diagram.component as the top-level host
- canvas, node, edge, minimap, palette, panel, zoom controls, background, watermark, and adornments
- template components for default/custom node and edge rendering
This layer is mostly declarative Angular template composition, with CSS tokens and shared styles under src/lib/styles.
5. Interaction and input boundaries
User interaction is handled through directives under src/lib/directives/input-events and the matching services in src/lib/services/input-events. These cover:
- panning and zooming
- box selection
- node move, resize, rotate
- linking/edge creation
- palette drag-and-drop
- keyboard shortcuts and pointer-based selection
Input events are normalized at the boundary and routed into the core engine rather than directly mutating view state.
6. Command and action-state engine
The core interaction logic is organized around:
- action-state-manager
- command-handler
- a large command set for move, resize, rotate, zoom, copy/paste, selection, grouping, viewport changes, and linking
This is the “behavioral core” of the library. It converts user intent into model changes and lifecycle events, which is important for diagram state consistency and undo/redo style interactions.
7. Middleware pipeline
src/core/src/middleware-manager is a major architectural subsystem. It applies cross-cutting processing to model actions, including:
- edge routing
- event emission
- internal ID assignment
- measured bounds assignment
- measurement tracking
- z-index assignment
- logging
This pipeline acts as a controlled transformation layer between raw actions and finalized model state. It is one of the main extension points of the library.
8. Geometry, routing, and spatial computation
Diagram-specific computation is isolated in dedicated engine modules:
- edge-routing-manager computes polyline, bezier, and orthogonal routes
- math, utils, and spatial-hash handle geometry, snapping, intersections, and collision-like calculations
- label-batch-processor, measurement-tracker, and port-batch-processor support layout-sensitive updates
These are core to accurate diagram rendering and interactive manipulation, especially when nodes move, resize, or rotate.
9. Direct vs virtualized rendering
The library supports two render strategies under src/core/src/render-strategy:
- direct
- virtualized
Virtualization is a meaningful subsystem because it changes how elements and ports are updated and measured for large diagrams. The supporting services and update strategies in src/lib/services/update-ports and src/core/src/updater coordinate this optimization.
10. Services as the public façade
src/lib/public-services exposes the main API surface: diagram, model, node, groups, selection, viewport, clipboard. These services wrap the core engine and provide a stable API for consumers. provideNgDiagram() and related providers assemble the dependency graph so Angular applications can use the library without wiring internal services manually.
11. Documentation site architecture
apps/docs is an Astro-based documentation site that embeds Angular example components. It has three distinct content layers:
- authored docs in src/content/docs
- example/demo components under src/components/angular
- generated API docs and changelog content
The site is both documentation and a live integration showcase, which means it exercises the library in realistic configurations rather than just showing static screenshots.
12. Demo app and usage examples
apps/angular-demo is a standalone Angular application that demonstrates the library in a compact runtime environment. It contains reusable demo data and templates for:
- default models
- generated models
- palette interactions
- virtualization tests
- custom node/edge templates
This app is a consumer boundary, useful for validating the public API and interaction behavior outside the docs site.
13. MCP server and search tooling
tools/mcp-server indexes documentation and API symbols and exposes tools such as get-doc, get-symbol, search-docs, and search-symbols. It does not participate in diagram rendering; it is an auxiliary knowledge-access layer for AI assistants and editor integrations. Its data flow is: indexed docs/API artifacts in, search and retrieval requests out.
14. Build, release, and governance
The repo uses GitHub Actions for PR checks, docs deployment, and npm publishing. Package scripts also generate API reports and bundle/minify the library artifacts. Architecturally, this means the repo has a clear separation between:
- source packages
- generated docs/API artifacts
- release automation and deployment targets
The docs site and package publish workflows are treated as first-class deliverables, not afterthoughts.
Main entry points: flow-core.ts Engine core · public-api.ts Public API · pnpm-workspace.yaml Monorepo · astro.config.mjs Docs site
Components 20
Repository
Top-level pnpm workspace and shared governance
- Monorepo · Workspace root coordinating packages, apps, tools, docs, and release automation
pnpm-workspace.yaml
Angular library
Primary npm package and its internal architecture
- ng-diagram · Primary library package published to npm
packages/ng-diagram/projects/ng-diagram/src/public-api.ts - Public API · Stable entrypoint for consumers and package surface
packages/ng-diagram/projects/ng-diagram/src/public-api.ts - UI layer · Diagram host, canvas, nodes, edges, minimap, palette, panels, controls, and adornments
packages/ng-diagram/projects/ng-diagram/src/lib/components/diagram/ng-diagram.component.ts - Input boundary · Pointer, keyboard, selection, linking, zooming, resize, rotate, and viewport directives
packages/ng-diagram/projects/ng-diagram/src/lib/directives/input-events/panning/panning.directive.ts - Public façade · Consumer-facing services and providers for diagram, model, selection, viewport, clipboard, and templates
packages/ng-diagram/projects/ng-diagram/src/lib/public-services/ng-diagram.service.ts - Model setup · Signal-based model initialization and adaptation boundary
packages/ng-diagram/projects/ng-diagram/src/lib/model/initialize-model.ts - Engine core · Internal state, commands, input routing, transactions, and model orchestration
packages/ng-diagram/projects/ng-diagram/src/core/src/flow-core.ts - Commands · Action-state manager and command handler that translate intent into model updates
packages/ng-diagram/projects/ng-diagram/src/core/src/command-handler/command-handler.ts - Input router · Normalized pointer, keyboard, and gesture events routed into command execution
packages/ng-diagram/projects/ng-diagram/src/core/src/input-events/input-events.router.ts - Middleware · Cross-cutting action pipeline for routing, IDs, bounds, measurements, z-index, events, and logging
packages/ng-diagram/projects/ng-diagram/src/core/src/middleware-manager/middleware-manager.ts - Geometry · Routing, math, spatial hashing, measurement, and batch processors
packages/ng-diagram/projects/ng-diagram/src/core/src/edge-routing-manager/edge-routing-manager.ts - Render strategy · Direct and virtualized render/update strategies for large diagrams
packages/ng-diagram/projects/ng-diagram/src/core/src/render-strategy/index.ts
Docs site
Astro documentation site with live Angular examples
- Docs site · Documentation and examples site that showcases the library live
apps/docs/astro.config.mjs - Docs content · Authored guides, API docs, policies, changelog, and intro material
apps/docs/src/content/docs/index.mdx - Examples · Embedded Angular example components used throughout the docs site
apps/docs/src/content/docs/examples/index.mdx
Demo app
Standalone Angular consumer application
- Angular demo · Standalone Angular application for interactive library validation
apps/angular-demo/src/main.ts
MCP server
Doc and symbol search tooling
- Search server · Auxiliary server exposing documentation and symbol search tools
tools/mcp-server/src/server.ts - MCP tools · get-doc, get-symbol, search-docs, and search-symbols request handlers
tools/mcp-server/src/tools/search-docs/handler.ts
Automation
Build, release, and deployment workflows
- Release flow · CI, docs deployment, npm publishing, and package build scripts
.github/workflows/publish-npm.yml
Connections 29
- Monorepo → ng-diagram: contains · Workspace includes the primary library package
- Monorepo → Docs site: contains · Workspace includes the docs site
- Monorepo → Angular demo: contains · Workspace includes the demo app
- Monorepo → Search server: contains · Workspace includes the MCP server
- Monorepo → Release flow: drives · Shared workflows and package scripts govern build and release
- ng-diagram → Public API: exports · Package entrypoint defines the public surface
- Public API → UI layer: exposes · Public API re-exports the Angular rendering layer
- Public API → Input boundary: exposes · Public API re-exports interaction directives
- Public API → Public façade: exposes · Public API re-exports façade services and providers
- Public API → Model setup: exposes · Public API includes model initialization utilities
- Public API → Engine core: wraps · Public layer delegates to the internal engine
- Public façade → Engine core: adapts · Façade services wrap and coordinate core runtime behavior
- Input boundary → Input router: routes input to · Normalized user input is forwarded into the engine
- Model setup → Engine core: feeds · Initialized models are consumed by the runtime core
- Engine core → Commands: coordinates · Core runtime uses commands for stateful interactions
- Engine core → Input router: consumes · Engine consumes normalized input events
- Engine core → Middleware: passes through · Actions are finalized through the middleware pipeline
- Engine core → Geometry: uses · Runtime relies on geometry and routing computations
- Engine core → Render strategy: selects · Core chooses direct or virtualized rendering behavior
- Middleware → Geometry: depends on · Routing and measurement middleware use geometry helpers
- Docs site → Docs content: serves · Docs site renders authored documentation and API pages
- Docs site → Examples: embeds · Docs site embeds live Angular examples
- Examples → ng-diagram: demonstrates · Examples exercise the published library
- Angular demo → ng-diagram: consumes · Demo app validates the public package in a standalone runtime
- Search server → MCP tools: hosts · Server exposes retrieval tools to clients
- MCP tools → Docs content: indexes · Tools search and retrieve documentation content
- MCP tools → ng-diagram: indexes · Tools search and retrieve API symbols from the library
- Release flow → Docs site: deploys · Workflows publish docs builds
- Release flow → ng-diagram: publishes · Workflows build and publish the npm package