# synergycodes/ng-diagram architecture

> Architecture diagram of the GitHub repository synergycodes/ng-diagram, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/synergycodes/ng-diagram
- Repository: https://github.com/synergycodes/ng-diagram

## Overview

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: [`packages/ng-diagram/projects/ng-diagram/src/core/src/flow-core.ts`](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/flow-core.ts) (Engine core), [`packages/ng-diagram/projects/ng-diagram/src/public-api.ts`](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/public-api.ts) (Public API), [`pnpm-workspace.yaml`](https://github.com/synergycodes/ng-diagram/blob/main/pnpm-workspace.yaml) (Monorepo), [`apps/docs/astro.config.mjs`](https://github.com/synergycodes/ng-diagram/blob/main/apps/docs/astro.config.mjs) (Docs site)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_repo["Repository"]
  node_root["Monorepo<br/>pnpm workspace"]
end

subgraph group_library["Angular library"]
  node_pkg["ng-diagram<br/>Angular library<br/>[public-api.ts]"]
  node_lib_api["Public API<br/>exports<br/>[public-api.ts]"]
  node_lib_components["UI layer<br/>Angular components"]
  node_lib_directives["Input boundary<br/>directives"]
  node_lib_services["Public façade<br/>services"]
  node_lib_model["Model setup<br/>model adapters"]
  node_core_engine["Engine core<br/>runtime core<br/>[flow-core.ts]"]
  node_core_commands["Commands<br/>behavioral core<br/>[command-handler.ts]"]
  node_core_input["Input router<br/>event normalization"]
  node_core_middleware["Middleware<br/>pipeline"]
  node_core_geometry["Geometry<br/>spatial computation"]
  node_core_rendering["Render strategy<br/>virtualization<br/>[index.ts]"]
end

subgraph group_docs["Docs site"]
  node_docs_site["Docs site<br/>Astro app<br/>[astro.config.mjs]"]
  node_docs_content["Docs content<br/>content layer<br/>[index.mdx]"]
  node_docs_examples["Examples<br/>demo content<br/>[index.mdx]"]
end

subgraph group_demo["Demo app"]
  node_angular_demo["Angular demo<br/>consumer app<br/>[main.ts]"]
end

subgraph group_mcp["MCP server"]
  node_mcp_server["Search server<br/>MCP server<br/>[server.ts]"]
  node_mcp_tools["MCP tools<br/>tooling<br/>[handler.ts]"]
end

subgraph group_automation["Automation"]
  node_automation_ci["Release flow<br/>automation<br/>[publish-npm.yml]"]
end

node_root -->|"contains"| node_pkg
node_root -->|"contains"| node_docs_site
node_root -->|"contains"| node_angular_demo
node_root -->|"contains"| node_mcp_server
node_root -->|"drives"| node_automation_ci
node_pkg -->|"exports"| node_lib_api
node_lib_api -->|"exposes"| node_lib_components
node_lib_api -->|"exposes"| node_lib_directives
node_lib_api -->|"exposes"| node_lib_services
node_lib_api -->|"exposes"| node_lib_model
node_lib_api -->|"wraps"| node_core_engine
node_lib_services -->|"adapts"| node_core_engine
node_lib_directives -->|"routes input to"| node_core_input
node_lib_model -->|"feeds"| node_core_engine
node_core_engine -->|"coordinates"| node_core_commands
node_core_engine -->|"consumes"| node_core_input
node_core_engine -->|"passes through"| node_core_middleware
node_core_engine -->|"uses"| node_core_geometry
node_core_engine -->|"selects"| node_core_rendering
node_core_middleware -->|"depends on"| node_core_geometry
node_docs_site -->|"serves"| node_docs_content
node_docs_site -->|"embeds"| node_docs_examples
node_docs_examples -->|"demonstrates"| node_pkg
node_angular_demo -->|"consumes"| node_pkg
node_mcp_server -->|"hosts"| node_mcp_tools
node_mcp_tools -->|"indexes"| node_docs_content
node_mcp_tools -->|"indexes"| node_pkg
node_automation_ci -->|"deploys"| node_docs_site
node_automation_ci -->|"publishes"| node_pkg

click node_root "https://github.com/synergycodes/ng-diagram/blob/main/pnpm-workspace.yaml"
click node_pkg "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/public-api.ts"
click node_lib_api "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/public-api.ts"
click node_lib_components "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/lib/components/diagram/ng-diagram.component.ts"
click node_lib_directives "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/lib/directives/input-events/panning/panning.directive.ts"
click node_lib_services "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/lib/public-services/ng-diagram.service.ts"
click node_lib_model "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/lib/model/initialize-model.ts"
click node_core_engine "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/flow-core.ts"
click node_core_commands "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/command-handler/command-handler.ts"
click node_core_input "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/input-events/input-events.router.ts"
click node_core_middleware "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/middleware-manager/middleware-manager.ts"
click node_core_geometry "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/edge-routing-manager/edge-routing-manager.ts"
click node_core_rendering "https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/render-strategy/index.ts"
click node_docs_site "https://github.com/synergycodes/ng-diagram/blob/main/apps/docs/astro.config.mjs"
click node_docs_content "https://github.com/synergycodes/ng-diagram/blob/main/apps/docs/src/content/docs/index.mdx"
click node_docs_examples "https://github.com/synergycodes/ng-diagram/blob/main/apps/docs/src/content/docs/examples/index.mdx"
click node_angular_demo "https://github.com/synergycodes/ng-diagram/blob/main/apps/angular-demo/src/main.ts"
click node_mcp_server "https://github.com/synergycodes/ng-diagram/blob/main/tools/mcp-server/src/server.ts"
click node_mcp_tools "https://github.com/synergycodes/ng-diagram/blob/main/tools/mcp-server/src/tools/search-docs/handler.ts"
click node_automation_ci "https://github.com/synergycodes/ng-diagram/blob/main/.github/workflows/publish-npm.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_root toneBlue
class node_pkg,node_lib_api,node_lib_components,node_lib_directives,node_lib_services,node_lib_model,node_core_engine,node_core_commands,node_core_input,node_core_middleware,node_core_geometry,node_core_rendering toneAmber
class node_docs_site,node_docs_content,node_docs_examples toneMint
class node_angular_demo toneRose
class node_mcp_server,node_mcp_tools toneIndigo
class node_automation_ci toneTeal
```

## Components

### Repository

Top-level pnpm workspace and shared governance

- [Monorepo](https://github.com/synergycodes/ng-diagram/blob/main/pnpm-workspace.yaml): pnpm workspace · Workspace root coordinating packages, apps, tools, docs, and release automation · `pnpm-workspace.yaml`

### Angular library

Primary npm package and its internal architecture

- [ng-diagram](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/public-api.ts): Angular library · Primary library package published to npm · `packages/ng-diagram/projects/ng-diagram/src/public-api.ts`
- [Public API](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/public-api.ts): exports · Stable entrypoint for consumers and package surface · `packages/ng-diagram/projects/ng-diagram/src/public-api.ts`
- [UI layer](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/lib/components/diagram/ng-diagram.component.ts): Angular components · 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](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/lib/directives/input-events/panning/panning.directive.ts): directives · 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](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/lib/public-services/ng-diagram.service.ts): services · 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](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/lib/model/initialize-model.ts): model adapters · Signal-based model initialization and adaptation boundary · `packages/ng-diagram/projects/ng-diagram/src/lib/model/initialize-model.ts`
- [Engine core](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/flow-core.ts): runtime core · Internal state, commands, input routing, transactions, and model orchestration · `packages/ng-diagram/projects/ng-diagram/src/core/src/flow-core.ts`
- [Commands](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/command-handler/command-handler.ts): behavioral core · 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](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/input-events/input-events.router.ts): event normalization · 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](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/middleware-manager/middleware-manager.ts): pipeline · 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](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/edge-routing-manager/edge-routing-manager.ts): spatial computation · 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](https://github.com/synergycodes/ng-diagram/blob/main/packages/ng-diagram/projects/ng-diagram/src/core/src/render-strategy/index.ts): virtualization · 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](https://github.com/synergycodes/ng-diagram/blob/main/apps/docs/astro.config.mjs): Astro app · Documentation and examples site that showcases the library live · `apps/docs/astro.config.mjs`
- [Docs content](https://github.com/synergycodes/ng-diagram/blob/main/apps/docs/src/content/docs/index.mdx): content layer · Authored guides, API docs, policies, changelog, and intro material · `apps/docs/src/content/docs/index.mdx`
- [Examples](https://github.com/synergycodes/ng-diagram/blob/main/apps/docs/src/content/docs/examples/index.mdx): demo content · 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](https://github.com/synergycodes/ng-diagram/blob/main/apps/angular-demo/src/main.ts): consumer app · Standalone Angular application for interactive library validation · `apps/angular-demo/src/main.ts`

### MCP server

Doc and symbol search tooling

- [Search server](https://github.com/synergycodes/ng-diagram/blob/main/tools/mcp-server/src/server.ts): MCP server · Auxiliary server exposing documentation and symbol search tools · `tools/mcp-server/src/server.ts`
- [MCP tools](https://github.com/synergycodes/ng-diagram/blob/main/tools/mcp-server/src/tools/search-docs/handler.ts): tooling · 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](https://github.com/synergycodes/ng-diagram/blob/main/.github/workflows/publish-npm.yml): automation · CI, docs deployment, npm publishing, and package build scripts · `.github/workflows/publish-npm.yml`

## Connections

- 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
