# Diagram editor — library for developers

Diagram model and drawing: flowcharts, org charts, network and electrical diagrams. Shapes, connectors with automatic routing, groups, pages, templates, export to SVG, PNG and PDF. No DOM needed for the model.

## Installation

```sh
npm install @openlibsdev/diagram-editor
```

```js
import DiagramEditor from "https://cdn.jsdelivr.net/npm/@openlibsdev/diagram-editor@1.0.0/+esm";
```

## Usage

```js
import DiagramEditor from "@openlibsdev/diagram-editor";

const d = DiagramEditor.createDiagram();
const a = DiagramEditor.addNode(d, "terminator", 40, 40, { text: "Start" });
const b = DiagramEditor.addNode(d, "rect", 40, 160, { text: "Step" });
DiagramEditor.addEdge(d, a.id, b.id);
const svg = DiagramEditor.toSVG(d);               // a string
const json = DiagramEditor.serialize(d);
```

## API

### `DiagramEditor.createDiagram()`

Creates an empty diagram.

### `DiagramEditor.createProject()`

Creates a project with pages.

### `DiagramEditor.addNode(d, type, x, y, props)`

Adds a shape at a point.

### `DiagramEditor.addEdge(d, from, to, props)`

Connects two shapes.

### `DiagramEditor.remove(d, ids)`

Removes items.

### `DiagramEditor.move(d, ids, dx, dy)`

Moves items.

### `DiagramEditor.align(d, ids, how)`

Aligns items.

### `DiagramEditor.distribute(d, ids, axis)`

Spaces items evenly.

### `DiagramEditor.group(d, ids)`

Groups items.

### `DiagramEditor.layoutTree(d, opts)`

Arranges shapes as a tree.

### `DiagramEditor.route(d, e)`

Recomputes the path of a connector.

### `DiagramEditor.toSVG(d, opts)`

Diagram as SVG.

### `DiagramEditor.toCanvas(d, opts)`

Diagram as a <canvas>.

### `DiagramEditor.toBlob(d, format, opts)`

Diagram as a file: svg, png, jpeg, webp, pdf.

### `DiagramEditor.serialize(x)`

Diagram or project → JSON.

### `DiagramEditor.parse(data)`

JSON → diagram or project.

### `DiagramEditor.registerShape(type, shape)`

Adds a shape type.

### `DiagramEditor.SHAPES`

Shape types.

### `DiagramEditor.TEMPLATES`

Ready diagrams.

### `DiagramEditor.findNamedColor(text, lang)`

Finds a named color by its name in the given language ("dark blue" → #00008b).

### `DiagramEditor.listNamedColors(lang)`

Lists the named colors with their names in the given language.

### `DiagramEditor.ALIGNS`

Text alignments: left, center, right.

### `DiagramEditor.APP`

Name, version and release date of the library: {name, version, created}.

### `DiagramEditor.ARROWS`

Line end markers: arrows, triangles, diamonds, circle and the ER crow's-foot ends.

### `DiagramEditor.BAND`

Height of a container's title band, in pixels.

### `DiagramEditor.DASHES`

Line styles: solid, dashed, dotted (as dash patterns).

### `DiagramEditor.DOTS`

Junction dot modes of a line: auto, on, off.

### `DiagramEditor.EDGE_DEFAULTS`

Default properties of a new line: ends, kind, arrows, color, width, style, text.

### `DiagramEditor.FORMATS`

Export formats and their MIME types.

### `DiagramEditor.IMAGE_LIMIT`

Largest picture kept in a diagram, in bytes of its data URL (3 MB).

### `DiagramEditor.LINE_KINDS`

Line kinds: straight, elbow (right angles), curved.

### `DiagramEditor.MIN_SIZE`

Smallest width and height of a shape, in pixels.

### `DiagramEditor.NODE_DEFAULTS`

Default properties of a new shape: text, fill, opacity, border, font, alignment.

### `DiagramEditor.PAGE_DEFAULTS`

Default page settings: background, transparency, grid, snapping, grid size, margin, junction dots.

### `DiagramEditor.SHAPE_SETS`

Shapes by set as the editor offers them: basic, flowchart, UML, ER, BPMN, network, electrical, logic.

### `DiagramEditor.SIDES`

Sides a line can attach to: auto, top, right, bottom, left.

### `DiagramEditor.addBend(d, e, x, y)`

Adds a bend where the line passes near a point; returns its index, or -1.

### `DiagramEditor.anchor(n, side)`

Point in the middle of a side of a shape's outline (taking rotation into account).

### `DiagramEditor.bounds(d, ids)`

Box around the given items (or all of them); null for an empty diagram.

### `DiagramEditor.clone(d)`

Deep copy of a diagram.

### `DiagramEditor.contents(d, c)`

Shapes lying inside a container.

### `DiagramEditor.cssFont(it)`

CSS font string of an item (style, weight, size, family).

### `DiagramEditor.decorationBox(d)`

Box of the page decoration (for example a drawing frame) set with setDecorations, or null.

### `DiagramEditor.distToSegment(p, a, b)`

Distance from a point to a line segment.

### `DiagramEditor.drawPrimitives(ctx, prims)`

Draws primitives (from primitives()) on a canvas 2D context.

### `DiagramEditor.duplicate(d, ids, dx, dy)`

Copies items and pastes them shifted by (dx, dy); returns the new ids.

### `DiagramEditor.edgeAt(d, x, y, tol)`

The topmost line passing near a point, or null.

### `DiagramEditor.edgeEnds(d, e)`

Where a line really ends: [start, end], each with point, side, pin and whether it is free.

### `DiagramEditor.electricShape(W, H, o)`

Makes an electrical symbol shape from its size and drawing (lines, pins, label place), for registerShape.

### `DiagramEditor.exportBox(d, ids)`

Area to export: the given items with their outside labels, or the whole diagram with its decoration.

### `DiagramEditor.extract(d, ids)`

Copies a part of a diagram (shapes and the lines between them) as a piece to insert elsewhere.

### `DiagramEditor.find(d, id)`

A shape or a line by its id.

### `DiagramEditor.findEdge(d, id)`

A line by its id, or null.

### `DiagramEditor.findNode(d, id)`

A shape by its id, or null.

### `DiagramEditor.insert(d, part, dx, dy)`

Inserts a piece made by extract, shifted by (dx, dy); groups get new names. Returns the new ids.

### `DiagramEditor.itemsInRect(d, box)`

Ids of the shapes and lines entirely inside a rectangle.

### `DiagramEditor.junctions(d)`

Points where junction dots are drawn (T joints and crossings of three or more wires).

### `DiagramEditor.labelPlace(n)`

Where the outside label of a symbol goes: below, above or to the right.

### `DiagramEditor.labelText(n)`

Text of a symbol's label: designator, value and text, one per line.

### `DiagramEditor.loadFonts(d)`

Loads the font files of every family the diagram uses, so drawing and measuring use the real fonts.

### `DiagramEditor.loadImages(d)`

Loads every picture of the diagram before drawing.

### `DiagramEditor.markup(d, opts)`

The diagram as SVG elements without the <svg> around them (to put into your own SVG).

### `DiagramEditor.moveSegment(d, e, k, dx, dy)`

Moves one segment of a line as it is drawn by (dx, dy).

### `DiagramEditor.namedColorTexts(lang)`

Labels of the color field in the given language (the texts of the color picker), English for a missing language.

### `DiagramEditor.nearestPin(n, x, y, toward)`

Index of the pin of a shape nearest to a point, or -1.

### `DiagramEditor.nodeAt(d, x, y, pad)`

The topmost shape under a point (a container only on its title band and border).

### `DiagramEditor.normalize(d)`

Any object → a valid diagram: unknown values replaced by defaults, ids made unique, broken lines removed.

### `DiagramEditor.normalizeProject(p)`

Any object → a valid project with pages.

### `DiagramEditor.order(d, ids, how)`

Changes the drawing order of shapes: front, back, forward, backward.

### `DiagramEditor.orthoThrough(a, sa, vias, b, sb)`

Right-angle polyline from one point through via points to another, leaving and entering along the given sides.

### `DiagramEditor.pins(n)`

Pins of a shape as seen (electrical and logic symbols): [{x, y, side}].

### `DiagramEditor.primitives(d, opts)`

The diagram as a list of drawing primitives (paths, texts, pictures), ready for canvas or SVG.

### `DiagramEditor.removeBend(e, i)`

Removes a bend of a line by its index.

### `DiagramEditor.resetBends(e)`

Removes all manual bends of a line (back to automatic routing).

### `DiagramEditor.rotate(d, ids, dir)`

Turns shapes by a quarter turn (1: clockwise, -1: counter-clockwise) around their centres.

### `DiagramEditor.scalePath(d, sx, sy)`

Scales SVG path data (M L H V Q C A Z) by sx and sy.

### `DiagramEditor.setCornerJoins(on)`

Turns junction dots at wire corners on or off.

### `DiagramEditor.setDecorations(fn)`

Sets a function that draws a page decoration (for example a drawing frame and title block).

### `DiagramEditor.setProjectHook(fn)`

Sets a function called on every project being opened (to migrate or complete its data).

### `DiagramEditor.setRefText(fn)`

Sets a function that formats designators in labels.

### `DiagramEditor.sheetPlan(box)`

How a diagram is cut into A4 sheets at real size: orientation, columns, rows, tile size.

### `DiagramEditor.snap(d, v)`

Rounds a coordinate to the grid when snapping is on.

### `DiagramEditor.splitWire(d, edgeId, x, y)`

Splits a wire at a point with a junction; returns the junction shape, or null.

### `DiagramEditor.template(name, texts)`

A ready diagram by template name, with your own texts.

### `DiagramEditor.textBox(n)`

Box where a shape's text is placed, with its lines and alignment.

### `DiagramEditor.textFontData`

Data for a text and font block: the font list, loading a font style, adding a font file, the block's labels in a language and the default values.

### `DiagramEditor.ungroup(d, ids)`

Ungroups the groups of the given items; returns how many shapes left a group.

### `DiagramEditor.visualBox(n)`

Everything a shape takes in the picture: its box and an outside label.

### `DiagramEditor.wirePoint(d, x, y, tol, except)`

Point on the nearest wire within a distance, or null (a dropped wire end lands there).

### `DiagramEditor.withContents(d, ids)`

The ids plus the contents of every container among them.

### `DiagramEditor.withGroups(d, ids)`

The ids plus the other members of their groups.

### `DiagramEditor.wrapText(text, it, maxWidth)`

Splits text into lines no wider than a width (words kept whole).

## Dependencies

- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @simonwep/pickr 1.10.2 (MIT)
- @simonwep/pickr 1.10.2 (MIT)
- dejavu-fonts-ttf 2.37.3 (Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain)
- imagetracerjs 1.2.6 (Unlicense)
- jspdf 2.5.2 (MIT)

- GitHub: https://github.com/OpenLibsDev/diagram-editor
- npm: https://www.npmjs.com/package/@openlibsdev/diagram-editor
- Open the app: https://openlibs.dev/en/app/diagram-editor/
