> ## Documentation Index
> Fetch the complete documentation index at: https://iso-topology.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Edit source through the API

> Apply stateless source edits, handle errors, and persist accepted changes through Go or the local Studio server.

Use `ApplyOp` when your application edits YAML and needs a fresh render. Keep persistence explicit: applying an edit returns source; it does not save an application-owned file.

### Direct-manipulation editing (for embedders)

This is the contract a cloud service or a WASM client uses to turn a canvas
gesture into a new DSL document — the same logic that powers Studio's
drag/edit, exposed as **pure, stateless functions**:

* The transform is a pure function of `(format, src, op)` — **no server state**.
  (Studio works by POSTing the whole DSL each time; the text is the only source
  of truth.)
* The rewrite **preserves the user's comments and formatting** — it is line/regex
  text surgery, not a reformatting YAML round-trip.

```go theme={null}
src := []byte(`# my diagram
nodes:
  scene:
    shape: composite
    layout: {mode: row, gap: 1}
    parts:
      - {id: api, shape: rectangle, geom: {w: 120, d: 80, h: 30}, label: API}
      - {id: db,  shape: cylinder,  geom: {w: 100, d: 100, h: 36}, label: DB}
`)

// Drag "api" by (+50, 0) world units. ApplyOp returns the rewritten DSL,
// a fresh SVG and validation issues; ApplyOpText returns only the new text.
op := isotopo.EditOp{Kind: "move", Target: "node", ID: "api", DWX: 50, DWY: 0}
newSrc, svg, issues, err := isotopo.ApplyOp("yaml", src, op)
_ = svg; _ = issues; _ = err
// newSrc still starts with "# my diagram" — comments survive.
```

**`EditOp` kinds** (the field set read depends on `Kind`):

| Kind | Fields used | Effect |
| - | - | - |
| `move` | `Target` (`node`\|`edge`), `ID`/`CI`, `DWX`,`DWY`, `Snap`, `Waypoints` | nudge a node offset, or move an edge (waypoints / bend). First move on an auto-layout scene freezes it to explicit coordinates. |
| `move-segment` | `CI`, `Segment`, `DWX`, `DWY` | move one pre-clip orthogonal 3-D segment; horizontal legs move perpendicularly, vertical legs move in XY. Writes `waypoints3d` and keeps resolved endpoints fixed. YAML/JSON explicit scenes only. |
| `set-field` | `Target` (`node`\|`edge`\|`canvas`), `ID`/`CI`, `Fields` (dotted path → value) | write any DSL field, e.g. `{"style.palette.top": "#FFF"}` |
| `add` | — | append a default rectangle to the scene |
| `delete` | `Target`, `ID`/`CI` | remove a node (and its connectors) or an edge. Refused for a **container** (group/lane) — empty it first |
| `duplicate` | `Target: node`, `ID` | clone a node with a fresh id, offset down-right. Refused for a **container** (cloning would collide its nested ids) |
| `reparent` | `ID` (node), `Target` (group id, or `""` for the scene root) | move a node into another group / out to the root; its stale offset is dropped so the new parent lays it out. No-op when the parent is unchanged; refused into its own descendant |

```go theme={null}
// Apply only the text transform (e.g. you render separately, or with ELK):
newSrc, err := isotopo.ApplyOpText("yaml", src, op)
```

`move-segment` indexes `ConnectorRoute.World3D` from a render of the **same
source**. A UI should hit-test `Drawing.Editable` intervals, which map final
polyline segment parameters back to `RouteSegment` using the producer's exact
source record. Curves, ambiguous final projected overlaps and displaced
coincident paths have no edit mapping. These intervals are not proven pixel
visibility: intersect them with the active coverage result and native hit
testing. Studio also checks pointer ambiguity in client coordinates: a
different visible leg within one CSS pixel of the best hit distance prevents
selection, including a visible leg that has no edit mapping. Covered geometry
does not compete in that pointer-distance check.

The operation leaves input unchanged on rejection or a zero effective delta.
It rejects unsupported routes, invalid indices/coordinates, oversized results
and aliased connector mappings. YAML surgery targets the selected scene and
connector, preserving surrounding source and comments; the selected flow
mapping or waypoint field may be reformatted. JSON keeps fields and values,
with normalized indentation. Undo belongs to the caller: record history only
after accepting a valid source **and** SVG response.

For the lowest level — the individual comment-preserving text operations — import
the [`yamledit` sub-package](./api.md#sub-package-yamledit).

## HTTP and persistence

Studio wraps the same contracts with local HTTP endpoints. See the [endpoint table](https://iso-topology.mintlify.site/guides/studio.md#endpoints-for-tooling) for routes, parameters and result fields. `/api/save` is the separate write-back path; the Studio frontend invokes it automatically for parseable nonempty edits in serve mode.

Source editing is YAML-oriented. D2 and JSON parsing support does not imply every text-surgery operation supports those formats. [Choose a format](https://iso-topology.mintlify.site/guides/choose-format.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.