@eraserlabs/layout

中文文档
0.5.2发布于 2026/11/12MITTypeScriptES Module构建来源已验证近 30 天下载 1.3万

Eraser's layout geometry and corridor connection router.

原始使用文档
文档目录3 节
  1. Introduction
  2. Why
  3. Usage

@eraserlabs/layout

Introduction

@eraserlabs/layout is the layout package used by eraser-diagrams. It can also be used independently as a connection router for diagrams whose entities have already been sized and positioned, with @eraserlabs/utils as its only dependency.

Its main routine is routeConnections. Each connection has a style. elbow connections (the default) go to the corridor router: it produces orthogonal routes, understands nested containers, avoids obstacles, and can route some or all connections in a diagram, respecting the current coordinates of lines that have already been routed. straight and arc connections are one direct segment between the entities' drawn boundaries; an arc adds a bend, the signed distance from the chord's middle to its apex. When an entity moves, a straight line's bound end glides along the outline and an arc's bound end keeps its place on the entity.

Why

We could not find a standalone router for arbitrary nested diagrams that also supported partial routing. Existing options tend to be coupled to a complete graph-layout engine or editor, assume a flat diagram, or require every connection to be regenerated together.

@eraserlabs/layout treats entity placement as input. It routes connections around that existing geometry while allowing unaffected routes to remain fixed, which makes it suitable for both initial rendering, incremental edits, and fully interactive drag editing in a GUI editor.

Usage

import {
  COMMIT_REPAIR,
  LayoutManager,
  routeConnections,
  type LayoutEntity,
  type NewConnection,
} from '@eraserlabs/layout';

const entities: LayoutEntity[] = [
  { id: 'client', x: 0, y: 70, width: 120, height: 60 },
  {
    id: 'services',
    x: 220,
    y: 0,
    width: 320,
    height: 200,
    isContainer: true,
  },
  {
    id: 'api',
    containerId: 'services',
    x: 270,
    y: 70,
    width: 100,
    height: 60,
  },
];

const connections: NewConnection[] = [
  { id: 'client-api', from: 'client', to: 'api' },
  {
    id: 'client-services',
    from: 'client',
    to: 'services',
    connectorStyle: 'arc',
    bend: 40,
  },
];

const layoutManager = new LayoutManager({ entities, connections: [] });

const { updates, fallbacks } = routeConnections({
  layoutManager,
  connectionsToRoute: connections,
  repair: COMMIT_REPAIR,
});

const route = layoutManager.getConnectionById('client-api');

layoutManager contains the positioned entities and any existing connections. Entity coordinates are absolute; containerId identifies nesting and isContainer identifies containers. connectionsToRoute selects the new or existing connections to route; alternatively, updates selects the connections that touch entities you moved in the manager's staged changes. repair limits how long the router spends improving routes: false for none, { timeBudgetMs } for a time limit (COMMIT_REPAIR is 100 ms; Infinity makes the result independent of machine speed).

Connections that are not selected keep their points, which enables partial routing. A selected connection keeps its existing path when its attachments and clearance are still valid (allowShift, on by default). updates holds each routed connection's change, and fallbacks the ids of connections that got direct fallback geometry instead of a corridor route. Each polyline is an absolute x / y origin with relative [x, y] points.

版本与发布记录

@eraserlabs/layout 共发布 11 个版本,首次发布于 2026/08/19;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.2latest2026/11/122 天前
0.5.12026/11/059 天前
0.5.02026/11/0311 天前
0.4.32026/10/2816 天前
0.4.22026/10/2817 天前
0.4.12026/10/2817 天前

关于 @eraserlabs/layout 的常见问题

@eraserlabs/layout 是什么?

Eraser's layout geometry and corridor connection router.

如何安装 @eraserlabs/layout?

在项目目录运行 npm install @eraserlabs/layout,或使用 pnpm add / yarn add / bun add。

@eraserlabs/layout 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@eraserlabs/layout 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@eraserlabs/layout 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.23.4 || ^24.22.1 || >=26.12.1。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@eraserlabs/layout 可以商用吗?

@eraserlabs/layout 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@eraserlabs/layout 的下载量有多少?

根据 npm 官方下载统计,@eraserlabs/layout 在 2026/10/14 – 2026/11/12 这 30 天内被下载 12,860 次,日均约 429 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@eraserlabs/layout 还在维护吗?

最新版本 0.5.2 发布于 2026/11/12(2 天前);至今共发布 11 个版本,其中过去 12 个月发布了 11 个;首次发布于 2026/08/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。