# Editable DOT and Mermaid imports

Graffalo 0.36 adds DOT tables and named ports, Mermaid ER/class/sequence diagrams,
explicit endpoint symbols and group-boundary connections. These become native
editable objects, not embedded screenshots. Existing plan and scope checks apply.

## Browser and agent workflow

In the document menu, choose **Import diagram** and a `.dot`, `.gv`, `.mmd` or
`.mermaid` file. Review the preview and choose the empty open canvas, new canvases
in the current document, or a new document. Existing content is not overwritten.
Connected browser imports, REST `POST /api/import/text`, and MCP `create_document`
use the same Rust parser. Offline browser import remains a smaller flowchart/DOT
subset; connect to the backend for the features described here.

An agent should preview before saving:

```json
{
  "title": "Orders",
  "format": "mermaid",
  "source": "erDiagram\n CUSTOMER ||--o{ ORDER : places",
  "dry_run": true
}
```

Read `document`, `idMap`, `warnings`, `omissions` and `complete`. Save the reviewed
source with `dry_run:false` and a stable `idempotency_key`, then call `render` on
the saved document and inspect the actual image. Use stable node/row IDs in later
`apply_operations` calls, with `if_revision`; never guess IDs from array positions.
Import needs trial/paid access and `diagrams:read`, `diagrams:create`, and
`diagrams:import` scopes. Rendering has its own scope and entitlement checks.
Public HTTPS URL import follows the separate [URL import guide](url-import.md).

## DOT

- `graph`/`digraph`, `strict`, quoted/Unicode/numeric IDs, comments, scoped defaults,
  repeated attribute lists, chains and endpoint sets.
- Basic shapes, solid colors, labels, font size/weight, dimensions, dash states,
  `rankdir=TB|BT|LR|RL`, `dir=forward|back|both|none` and compass sides `n/e/s/w`.
- `record`/`Mrecord`: compartments become editable table rows. A first unported
  compartment is the table title. `<port>` supplies the row's stable field ID.
- HTML labels: a bounded `TABLE` → `TR` → `TD` structure, `TD PORT`, `<BR/>` and
  literal text formatting tags (`B`, `I`, `U`, `S`, `FONT`, `SUB`, `SUP`). A first
  single unported cell is the title. A row with one port combines cells into
  name/type/value; several ported cells become separate addressable rows.
- `node:port:e` and `node:port:w` attach to the row. `node:port` defaults to the
  outgoing/right or incoming/left side. Use the explicit side to disambiguate a
  row named `n`, `e`, `s` or `w` from a compass attachment.
- `arrowhead`/`arrowtail`: `normal`, `onormal`, `inv`, `oinv`, `vee`, `diamond`,
  `odiamond`, `dot`, `odot`, `tee`, `crow`, `box`, `obox`, `none`.
  `headlabel`/`taillabel` remain separate editable endpoint captions.
- `cluster*` subgraphs become nested groups. `lhead`/`ltail` connect to a cluster
  boundary; the original endpoint must belong to that cluster. No proxy node is
  added and membership is retained.

Example: [editable tables, row ports and a group boundary](examples/tables.dot).

Graffalo lays out the graph itself. Record orientation, individual HTML cell
colors/fonts/borders and exact Graphviz spacing are not reproduced; table imports
return a warning about this conversion. Nested HTML tables, row spans greater
than one, images/links/scripts, unknown tags/attributes, compass corners and
compound/half arrow shapes fail explicitly. Use JSON for exact Graffalo styling.

## Mermaid flowcharts and statecharts

Flowcharts retain TD/TB/BT/LR/RL direction, labels, chains, fan-in/fan-out, nested
subgraphs, classes/styles, solid/dashed/thick connections and supported basic
shapes. Connections may target a subgraph itself, including a forward reference
or an empty group. These attach to the group boundary and survive flowchart
export without an invented member node.

The existing `stateDiagram` / `stateDiagram-v2` importer retains nested composites,
initial/final/choice/fork/join states, notes and transitions. Concurrent/history
states remain unsupported. See [the agent guide](README.md#mermaid-statecharts-and-state-symbols).

## Mermaid ER diagrams

`erDiagram` entities become table nodes. Attributes retain type, name, PK/FK/UK
keys and quoted descriptions; every row gets a stable ID and sockets on both
sides. Relationships preserve the **minimum and maximum at each end**:

```mermaid
erDiagram
 CUSTOMER {
  uuid id PK
  string email UK
 }
 ORDER {
  uuid id PK
  uuid customer_id FK
 }
 CUSTOMER ||--o{ ORDER : places
```

This says every order has exactly one customer; a customer has zero or more
orders. `--` is identifying/solid; `..` is non-identifying/dashed. Direction and
node classes/styles are supported. ER syntax relates entities, so Graffalo does
not invent which FK row maps to which PK row. Attach rows explicitly afterward
when that information is known. [Full example](examples/entities.mmd).

## Mermaid class diagrams

`classDiagram` imports class bodies and `Class : member` declarations as table
rows, with member signatures, visibility and return/type text. Namespaces become
nested groups. Class aliases, annotations and common class/style assignments are
retained. Annotations are stored in the node's description. Connections preserve:

| Syntax | Meaning / symbol |
| --- | --- |
| `Parent <\|-- Child` | Inheritance, hollow triangle at Parent |
| `Interface <\|.. Implementation` | Realization, dashed hollow triangle |
| `Whole *-- Part` | Composition, filled diamond at Whole |
| `Container o-- Item` | Aggregation, hollow diamond at Container |
| `A --> B` / `A ..> B` | Association / dashed dependency |
| `A -- B` / `A .. B` | Link with no endpoint arrow |

Reversed and two-ended forms use their authored end symbols. Quoted multiplicities
such as `Whole "1" *-- "0..*" Part : contains` are stored separately from the
relationship caption and can be edited in the inspector. Complex generic class
identifiers and executable click/link directives remain unsupported.
[Full example](examples/classes.mmd).

## Mermaid sequence diagrams

`sequenceDiagram` uses a chronological layout. Participant/actor aliases, implicit
participants, messages, autonumber, activation/deactivation, self messages, notes
and `loop`, `alt`, `opt`, `par`, `critical`, `break` fragments are supported.
`else`, `and` and `option` captions remain editable within the fragment frame.

- `->>` / `-->>`: filled arrow, solid/dashed.
- `->` / `-->`: solid/dashed line without an arrowhead.
- `-)` / `--)`: asynchronous open arrow, solid/dashed.
- `-x` / `--x`: crossed ending, solid/dashed.
- `<<->>` / `<<-->>`: bidirectional filled arrows.

Each participant is one native object containing its header, lifeline and named
message event sockets. Moving it moves those endpoints with it. Resize or edit
socket `offset` through MCP/JSON (0–1 of its height) to adjust a message's time
position; self-loop routes may need manual adjustment after moving a participant. Activation bars,
notes and fragment frames are separate editable objects, not an executable model.
Generic flowchart auto-layout is rejected on sequence canvases to protect message
order. Native sequence reflow after arbitrary edits is not yet implemented.

Use one statement per line. `box`, `rect`, participant create/destroy, clickable
links, Mermaid configuration directives and unrecognized constructs fail with a
source position. [Full example](examples/sequence.mmd).

## Validation, partial import and limits

Strict import is the default. Unsupported semantics are not silently dropped.
Errors give a code, original source line/column and UTF-8 offset. Known cosmetic
fallbacks return warnings. Flowcharts/DOT/statecharts can preview selected
recoverable omissions with `partial:true,dry_run:true`; saving those requires
`accept_omissions:true`. ER/class/sequence currently fail atomically on unsupported
statements, even in partial mode. Broken structure always fails.

Title-only front matter, UTF-8 BOMs, CRLF and fenced snippets are accepted. This
is not a general YAML/HTML/script interpreter; no source code or linked assets are
executed or fetched. Default booleans are filled by canonical normalization.

Shared server limits: 1 MiB UTF-8 source, 2,000 nodes, 8,000 edges, 1,000 groups,
64 nesting levels and 128 rows per table. Sequence imports additionally limit
participants to 64, messages to 2,000 and event sockets to 256 per participant per
direction; other native-object and source limits may be reached first.

## Export and compatibility

JSON is the lossless editable interchange format. Native and browser SVG/PNG/PDF
retain tables, sequence graphics, relationship notation and endpoint captions.
3D/Blender preserve endpoint notation as simplified wire symbols and keep marker
metadata; filled symbols are hatched in the batched 3D line renderer.

Mermaid export remains a **flowchart projection**, not an ER/class/sequence source
round-trip. It reports loss of table rows, event timing, custom symbols and endpoint
captions. Keep the original source plus Graffalo JSON when those semantics matter.
Documents using `markers`, endpoint captions or `details.presentation=lifeline`
require Graffalo 0.36 or newer. Existing document and DB versions do not change;
older documents load unchanged, but do not round-trip new documents through an
older client that does not know these optional fields.
