# ダイアグラムエディター — 開発者向けライブラリ

ダイアグラムのモデルと描画：フローチャート、組織図、ネットワーク図、電気回路図。図形、自動ルーティングのコネクター、グループ、ページ、テンプレート、SVG・PNG・PDF への書き出し。モデルに DOM は不要です。

## インストール

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

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

## 使い方

```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()`

空のダイアグラムを作成します。

### `DiagramEditor.createProject()`

ページを持つプロジェクトを作成します。

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

指定した点に図形を追加します。

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

2 つの図形をつなぎます。

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

項目を削除します。

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

項目を移動します。

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

項目を整列します。

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

項目を等間隔に配置します。

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

項目をグループ化します。

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

図形をツリー状に配置します。

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

コネクターの経路を再計算します。

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

SVG としてのダイアグラム。

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

<canvas> としてのダイアグラム。

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

ファイルとしてのダイアグラム：svg、png、jpeg、webp、pdf。

### `DiagramEditor.serialize(x)`

ダイアグラムまたはプロジェクト → JSON。

### `DiagramEditor.parse(data)`

JSON → ダイアグラムまたはプロジェクト。

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

図形の種類を追加します。

### `DiagramEditor.SHAPES`

図形の種類。

### `DiagramEditor.TEMPLATES`

既製のダイアグラム。

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

指定した言語の色名から名前付きの色を探します（「紺色」→ #00008b）。

### `DiagramEditor.listNamedColors(lang)`

名前付きの色を、指定した言語の名前とともに一覧します。

### `DiagramEditor.ALIGNS`

テキストの配置：左、中央、右。

### `DiagramEditor.APP`

ライブラリの名前、バージョン、リリース日：{name, version, created}。

### `DiagramEditor.ARROWS`

線の端：矢印、三角、ひし形、円、ER のカラスの足。

### `DiagramEditor.BAND`

コンテナーのタイトル帯の高さ（ピクセル）。

### `DiagramEditor.DASHES`

線のスタイル：実線、破線、点線（ダッシュパターンとして）。

### `DiagramEditor.DOTS`

線の接続点のモード：自動、オン、オフ。

### `DiagramEditor.EDGE_DEFAULTS`

新しい線の既定のプロパティ：端、種類、矢印、色、太さ、スタイル、テキスト。

### `DiagramEditor.FORMATS`

書き出し形式とその MIME タイプ。

### `DiagramEditor.IMAGE_LIMIT`

ダイアグラムに保持する画像の最大サイズ（data URL のバイト数、3 MB）。

### `DiagramEditor.LINE_KINDS`

線の種類：直線、カギ線（直角）、曲線。

### `DiagramEditor.MIN_SIZE`

図形の最小の幅と高さ（ピクセル）。

### `DiagramEditor.NODE_DEFAULTS`

新しい図形の既定のプロパティ：テキスト、塗り、不透明度、枠線、フォント、配置。

### `DiagramEditor.PAGE_DEFAULTS`

ページの既定の設定：背景、透明度、グリッド、スナップ、グリッドサイズ、余白、接続点。

### `DiagramEditor.SHAPE_SETS`

エディターが提供するセットごとの図形：基本、フローチャート、UML、ER、BPMN、ネットワーク、電気、論理。

### `DiagramEditor.SIDES`

線が接続できる辺：自動、上、右、下、左。

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

線が点の近くを通る位置に折れ点を追加します。その番号、または -1 を返します。

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

図形の輪郭の辺の中点（回転を考慮）。

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

指定した項目（またはすべて）を囲む枠。空のダイアグラムでは null。

### `DiagramEditor.clone(d)`

ダイアグラムのディープコピー。

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

コンテナーの内側にある図形。

### `DiagramEditor.cssFont(it)`

項目の CSS フォント文字列（スタイル、太さ、サイズ、ファミリー）。

### `DiagramEditor.decorationBox(d)`

setDecorations で設定したページ装飾（図枠など）の枠、または null。

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

点から線分までの距離。

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

プリミティブ（primitives() の結果）を canvas の 2D コンテキストに描きます。

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

項目をコピーし、(dx, dy) ずらして貼り付けます。新しい id を返します。

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

点の近くを通る最前面の線、または null。

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

線が実際に終わる位置：[始点, 終点]。それぞれ点、辺、ピン、空いているかどうかを持ちます。

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

サイズと描画（線、ピン、ラベル位置）から電気記号の図形を作ります（registerShape 用）。

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

書き出す範囲：指定した項目とその外側のラベル、またはダイアグラム全体と装飾。

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

ダイアグラムの一部（図形とその間の線）を、別の場所に挿入するための断片としてコピーします。

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

id で指定した図形または線。

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

id で指定した線、または null。

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

id で指定した図形、または null。

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

extract で作った断片を (dx, dy) ずらして挿入します。グループには新しい名前が付きます。新しい id を返します。

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

長方形の中に完全に収まる図形と線の id。

### `DiagramEditor.junctions(d)`

接続点を描く位置（T 字の接続と 3 本以上の配線の交差）。

### `DiagramEditor.labelPlace(n)`

記号の外側のラベルの位置：下、上、または右。

### `DiagramEditor.labelText(n)`

記号のラベルのテキスト：記号番号、値、テキストを 1 行ずつ。

### `DiagramEditor.loadFonts(d)`

ダイアグラムで使うすべてのファミリーのフォントファイルを読み込み、描画と計測に実際のフォントを使えるようにします。

### `DiagramEditor.loadImages(d)`

描画の前にダイアグラムのすべての画像を読み込みます。

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

周りの <svg> を含まない SVG 要素としてのダイアグラム（自分の SVG に入れる用）。

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

描かれている線の 1 区間を (dx, dy) 動かします。

### `DiagramEditor.namedColorTexts(lang)`

指定した言語での色フィールドのラベル（カラーピッカーのテキスト）。その言語がなければ英語。

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

点に最も近い図形のピンの番号、または -1。

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

点の下にある最前面の図形（コンテナーはタイトル帯と枠線の上だけ）。

### `DiagramEditor.normalize(d)`

任意のオブジェクト → 有効なダイアグラム：不明な値は既定値に、id は一意に、切れた線は削除。

### `DiagramEditor.normalizeProject(p)`

任意のオブジェクト → ページを持つ有効なプロジェクト。

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

図形の描画順を変えます：最前面へ、最背面へ、前面へ、背面へ。

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

ある点から中継点を経て別の点へ向かう直角の折れ線。指定した辺に沿って出入りします。

### `DiagramEditor.pins(n)`

見た目どおりの図形のピン（電気記号と論理記号）：[{x, y, side}]。

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

描画プリミティブ（パス、テキスト、画像）の一覧としてのダイアグラム。canvas や SVG にそのまま使えます。

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

番号で指定した線の折れ点を削除します。

### `DiagramEditor.resetBends(e)`

線の手動の折れ点をすべて削除します（自動配線に戻ります）。

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

図形をそれぞれの中心で 90 度回転します（1：時計回り、-1：反時計回り）。

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

SVG パスデータ（M L H V Q C A Z）を sx と sy で拡大縮小します。

### `DiagramEditor.setCornerJoins(on)`

配線の角の接続点をオン／オフします。

### `DiagramEditor.setDecorations(fn)`

ページ装飾（図枠と表題欄など）を描く関数を設定します。

### `DiagramEditor.setProjectHook(fn)`

開くすべてのプロジェクトに対して呼ばれる関数を設定します（データの移行や補完用）。

### `DiagramEditor.setRefText(fn)`

ラベル内の記号番号を整形する関数を設定します。

### `DiagramEditor.sheetPlan(box)`

ダイアグラムを実寸の A4 用紙に分割する方法：向き、列、行、タイルのサイズ。

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

スナップがオンのとき座標をグリッドに丸めます。

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

点で配線を接続点により分割します。接続点の図形、または null を返します。

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

テンプレート名で指定する既製のダイアグラム（自分のテキスト入り）。

### `DiagramEditor.textBox(n)`

図形のテキストを置く枠と、その行と配置。

### `DiagramEditor.textFontData`

テキストとフォントのブロック用データ：フォント一覧、スタイルの読み込み、フォントファイルの追加、言語ごとのブロックのラベル、既定値。

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

指定した項目のグループを解除します。グループから出た図形の数を返します。

### `DiagramEditor.visualBox(n)`

図形が画像上で占めるすべて：その枠と外側のラベル。

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

指定距離内で最も近い配線上の点、または null（離した配線の端がここに付きます）。

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

id と、その中のすべてのコンテナーの中身。

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

id と、そのグループの他のメンバー。

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

テキストを指定の幅を超えない行に分けます（単語は分割しません）。

## 依存関係

- @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
- アプリを開く: https://openlibs.dev/ja/app/diagram-editor/
