# 圖表編輯器 — 開發人員程式庫

圖表模型與繪製：流程圖、組織結構圖、網路圖和電路圖。形狀、自動佈線的連線線、組、頁面、模板，可匯出為 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)`

連線兩個形狀。

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

在 canvas 2D 上下文中繪製圖元（來自 primitives()）。

### `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 形連線以及三根或更多導線的交叉處）。

### `DiagramEditor.labelPlace(n)`

符號外部標籤的位置：下方、上方或右側。

### `DiagramEditor.labelText(n)`

符號標籤的文本：位號、值和文本，每項一行。

### `DiagramEditor.loadFonts(d)`

載入圖中使用的所有字型族的字型檔案，使繪製和測量使用真實字型。

### `DiagramEditor.loadImages(d)`

在繪製前載入圖中的所有圖片。

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

不含外層 <svg> 的 SVG 元素形式的圖（用於放入你自己的 SVG）。

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

將線條按所繪製的一段移動 (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)`

將形狀繞各自中心旋轉四分之一圈（1：順時針，-1：逆時針）。

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

按 sx 和 sy 縮放 SVG 路徑資料（M L H V Q C A Z）。

### `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/zh-Hant/app/diagram-editor/
