@dudko.dev/agent-web-ui 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -31,7 +31,8 @@ any framework on it - a web component, Vue, Angular, Svelte, plain DOM - or use
31
31
  the MCP Apps frame (its policy, its shell, the extension's JSON-RPC bridge) and the MCP Apps
32
32
  sandbox page a server serves on an origin of its own.
33
33
  - **The stylesheets** - `styles.css` (the chat) and `pipeline.css` (the pipeline editor): plain
34
- CSS, `awr-` classes and `--awr-*` custom properties, light and dark.
34
+ CSS, `awr-` classes and `--awr-*` custom properties, light and dark, with soft motion built in
35
+ (see [Motion](#motion)).
35
36
 
36
37
  It ships no agent logic (the core does that) and draws nothing: a front end draws, this package
37
38
  says what to draw. `@dudko.dev/agent-web-react` is built on it and re-exports everything it used to
@@ -77,7 +78,7 @@ not install this package itself: `@dudko.dev/agent-web-react` depends on it.
77
78
  | `@dudko.dev/agent-web-ui/mcp-app-sandbox` | the MCP Apps sandbox page for a SERVER (a Worker, Node, Deno, Bun): the page, its headers, a `Request` → `Response` handler | nothing |
78
79
  | `@dudko.dev/agent-web-ui/pipeline` | the pipeline editor's logic: the reducer of a pipeline run, the editing helpers, the code generator, the editor's strings | the core |
79
80
  | `@dudko.dev/agent-web-ui/pipeline-format` | the pipeline DEFINITION format an editor writes and an executor reads: its types, the validator (refuses secret values), references by name, the JSON Schema, the converters to and from the engine's definition (see [The pipeline definition format](#the-pipeline-definition-format)) | nothing |
80
- | `@dudko.dev/agent-web-ui/canvas` | the generic canvas's maths (viewport, curves, hit tests, layout order) and its strings | nothing |
81
+ | `@dudko.dev/agent-web-ui/canvas` | the generic canvas's maths (viewport, curves, edge routing, hit tests, layout order) and its strings | nothing |
81
82
  | `@dudko.dev/agent-web-ui/controller` | the agent as a store any front end wraps: `createAgentController`, and `createRemoteAgentController` for an agent on a server (see [Controller](#controller)) | the core, dynamically, when an agent is built in the page |
82
83
  | `@dudko.dev/agent-web-ui/markdown` | the answers' Markdown as a tree any front end draws: the parser, the parser of an answer still being written, `safeHref`, the link allowlist, the renderer's helpers (see [Markdown](#markdown)) | nothing |
83
84
  | `@dudko.dev/agent-web-ui/locales` | the chat's labels in your language, loaded on demand: `loadAgentLabels(tag)`, the list of tags, `labelsFromCatalog` | nothing |
@@ -216,7 +217,10 @@ allow.onclick = () => chat.approve(chat.getSnapshot().pendingApprovals[0].id)
216
217
  `createRemoteAgentController(options)` is the same store for an agent that runs on a SERVER - the
217
218
  swarm's gateway (`examples/swarm*` in the repository: Temporal + Postgres, Cloudflare, Google
218
219
  Cloud, AWS - one HTTP API). The same snapshot and the same methods, so any view of the controller
219
- draws it unchanged; nothing of the agent is in the page (no model, no key, no tool).
220
+ draws it unchanged; nothing of the agent is in the page (no model, no key, no tool). While a
221
+ session given (`sessionId`, `loadChat`) is replayed from its stream, the snapshot says
222
+ `restoring: true` (until the stream has been quiet `REPLAY_QUIET_MS`, 150 ms, at most
223
+ `REPLAY_MAX_MS`, 5 s): draw a placeholder then, not the empty state.
220
224
 
221
225
  ```js
222
226
  import { createRemoteAgentController } from '@dudko.dev/agent-web-ui/controller'
@@ -335,6 +339,39 @@ const doc = definitionToDocument(editedDefinition, { base: loadedDocument }) //
335
339
  it, not with `JSON.stringify` - a version stored by hand or by another tool would always look
336
340
  changed.
337
341
 
342
+ ## Motion
343
+
344
+ Soft motion is built into `styles.css` and on by default - the side panels slide in and out, menus,
345
+ dialogs, consent and question cards and notices ease in, a message of a turn just begun rises in,
346
+ the steps of the answer being written settle in, disclosures unfold by height (and close at once), and a saved
347
+ chat being read shows a placeholder (never the empty state) and fades in. Only `transform` and
348
+ `opacity` move (and a disclosure's height, by the browser: nothing is measured by script), and
349
+ nothing that is on the screen at the first paint animates.
350
+
351
+ | `data-awr-motion` on the chat's root | |
352
+ | --- | --- |
353
+ | absent / `auto` (default) | Soft motion; a short fade with no movement when the device asks for reduced motion (`prefers-reduced-motion: reduce`) |
354
+ | `reduced` | That fade, always: nothing travels |
355
+ | `none` | Nothing moves: every duration 0 |
356
+
357
+ A binding sets it from its option (`<AgentChat motion>`, `<dd-agent-chat motion>`); a front end of
358
+ your own puts it on its `.awr-chat` / `.awr-theme` element. The pure half is exported:
359
+ `AgentMotion`, `AGENT_MOTIONS`, `agentMotionOf` (an attribute's text, any case), `motionAttribute`
360
+ (the attribute for a motion - none for `auto`), `enteringMessageIds` (which messages arrive live
361
+ and may rise in: never the first drawing, a saved chat opened or a transcript replaced) and
362
+ `exitDurationMs` (how long a closing element's animation runs, from its computed style - 0 means
363
+ remove it now).
364
+
365
+ | Token | Default | |
366
+ | --- | --- | --- |
367
+ | `--awr-motion-duration-fast` / `-base` / `-slow` | 150 / 220 / 320 ms | Small parts (menus, rows), cards and panels, sheets |
368
+ | `--awr-motion-duration-exit` | 160 ms | What leaves goes faster than it came |
369
+ | `--awr-motion-ease-out` / `-ease-in-out` / `-ease-in` | `cubic-bezier(…)` | Entering, opening / closing, leaving |
370
+ | `--awr-motion-rise` / `-shift` / `-sheet` | 6px / 16px / 100% | How far a card rises, a wide side panel shifts, a narrow one slides |
371
+ | `--awr-motion-scale` | 0.97 | A menu or a dialog grows from |
372
+
373
+ Durations of 0 turn motion off the same way `none` does; distances of 0 make it `reduced`.
374
+
338
375
  ## Labels
339
376
 
340
377
  `defaultLabels` is every string the chat shows; counted phrases are functions so that languages
package/dist/canvas.cjs CHANGED
@@ -22,6 +22,7 @@ var canvas_exports = {};
22
22
  __export(canvas_exports, {
23
23
  DEFAULT_MAX_ZOOM: () => DEFAULT_MAX_ZOOM,
24
24
  DEFAULT_MIN_ZOOM: () => DEFAULT_MIN_ZOOM,
25
+ EDGE_HANDLE_SIZE: () => EDGE_HANDLE_SIZE,
25
26
  IDENTITY_VIEWPORT: () => IDENTITY_VIEWPORT,
26
27
  boundsOf: () => boundsOf,
27
28
  centerOn: () => centerOn,
@@ -30,7 +31,9 @@ __export(canvas_exports, {
30
31
  curvePoint: () => curvePoint,
31
32
  defaultCanvasLabels: () => defaultCanvasLabels,
32
33
  distanceToCurve: () => distanceToCurve,
34
+ distanceToRoute: () => distanceToRoute,
33
35
  edgeCurve: () => edgeCurve,
36
+ estimateLabelSize: () => estimateLabelSize,
34
37
  fitViewport: () => fitViewport,
35
38
  hitEdge: () => hitEdge,
36
39
  hitNode: () => hitNode,
@@ -41,7 +44,11 @@ __export(canvas_exports, {
41
44
  pinchViewport: () => pinchViewport,
42
45
  readingOrder: () => readingOrder,
43
46
  rectContains: () => rectContains,
47
+ roundedPath: () => roundedPath,
48
+ routeEdges: () => routeEdges,
49
+ routeHitsRect: () => routeHitsRect,
44
50
  screenToWorld: () => screenToWorld,
51
+ segmentHitsRect: () => segmentHitsRect,
45
52
  snapPoint: () => snapPoint,
46
53
  snapTo: () => snapTo,
47
54
  worldToScreen: () => worldToScreen,
@@ -221,7 +228,7 @@ var hitEdge = (edges, point, tolerance = 8) => {
221
228
  let best;
222
229
  let bestDistance = Infinity;
223
230
  for (const edge of edges) {
224
- const distance = distanceToCurve(edge.curve, point);
231
+ const distance = "route" in edge ? distanceToRoute(edge.route, point) : distanceToCurve(edge.curve, point);
225
232
  if (distance <= tolerance && distance < bestDistance) {
226
233
  best = edge.id;
227
234
  bestDistance = distance;
@@ -234,10 +241,281 @@ var readingOrder = (nodes, column = 40) => nodes.map((node, index) => ({ node, i
234
241
  const bx = Math.round(b.node.x / column);
235
242
  return ax - bx || a.node.y - b.node.y || a.index - b.index;
236
243
  }).map(({ node }) => node.id);
244
+ var EDGE_HANDLE_SIZE = { width: 14, height: 14 };
245
+ var estimateLabelSize = (text) => text ? { width: Math.min(text.length, 28) * 6.2 + 20, height: 22 } : EDGE_HANDLE_SIZE;
246
+ var inflate = (r, by) => ({
247
+ x: r.x - by,
248
+ y: r.y - by,
249
+ width: r.width + by * 2,
250
+ height: r.height + by * 2
251
+ });
252
+ var rectsOverlap = (a, b) => a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
253
+ var segmentHitsRect = (a, b, rect) => {
254
+ const dx = b.x - a.x;
255
+ const dy = b.y - a.y;
256
+ let t0 = 0;
257
+ let t1 = 1;
258
+ const sides = [
259
+ [-dx, a.x - rect.x],
260
+ [dx, rect.x + rect.width - a.x],
261
+ [-dy, a.y - rect.y],
262
+ [dy, rect.y + rect.height - a.y]
263
+ ];
264
+ for (const [p, q] of sides) {
265
+ if (p === 0) {
266
+ if (q <= 0) return false;
267
+ continue;
268
+ }
269
+ const t = q / p;
270
+ if (p < 0) {
271
+ if (t > t1) return false;
272
+ if (t > t0) t0 = t;
273
+ } else {
274
+ if (t < t0) return false;
275
+ if (t < t1) t1 = t;
276
+ }
277
+ }
278
+ return t1 - t0 > 1e-9;
279
+ };
280
+ var routePolyline = (route) => route.kind === "curve" && route.curve ? Array.from({ length: 33 }, (_, i) => curvePoint(route.curve, i / 32)) : route.points;
281
+ var routeHitsRect = (route, rect, margin = 0) => {
282
+ const box = inflate(rect, margin);
283
+ const line = routePolyline(route);
284
+ for (let i = 1; i < line.length; i++) if (segmentHitsRect(line[i - 1], line[i], box)) return true;
285
+ return false;
286
+ };
287
+ var distanceToRoute = (route, point) => {
288
+ if (route.kind === "curve" && route.curve) return distanceToCurve(route.curve, point);
289
+ let best = Infinity;
290
+ for (let i = 1; i < route.points.length; i++) {
291
+ best = Math.min(best, distanceToSegment(point, route.points[i - 1], route.points[i]));
292
+ }
293
+ return best;
294
+ };
295
+ var roundedPath = (points, radius = 10) => {
296
+ if (points.length === 0) return "";
297
+ const p = (pt) => `${round(pt.x)} ${round(pt.y)}`;
298
+ let d = `M ${p(points[0])}`;
299
+ for (let i = 1; i < points.length - 1; i++) {
300
+ const a = points[i - 1];
301
+ const c = points[i];
302
+ const b = points[i + 1];
303
+ const inLength = Math.hypot(c.x - a.x, c.y - a.y);
304
+ const outLength = Math.hypot(b.x - c.x, b.y - c.y);
305
+ const r = Math.min(radius, inLength / 2, outLength / 2);
306
+ if (r <= 0.01) {
307
+ d += ` L ${p(c)}`;
308
+ continue;
309
+ }
310
+ const before = { x: c.x - (c.x - a.x) / inLength * r, y: c.y - (c.y - a.y) / inLength * r };
311
+ const after = { x: c.x + (b.x - c.x) / outLength * r, y: c.y + (b.y - c.y) / outLength * r };
312
+ d += ` L ${p(before)} Q ${p(c)}, ${p(after)}`;
313
+ }
314
+ return `${d} L ${p(points[points.length - 1])}`;
315
+ };
316
+ var simplify = (points) => {
317
+ const out = [];
318
+ for (const pt of points) {
319
+ const last = out[out.length - 1];
320
+ if (last && Math.abs(last.x - pt.x) < 0.01 && Math.abs(last.y - pt.y) < 0.01) continue;
321
+ const before = out[out.length - 2];
322
+ if (before && last && (Math.abs(before.x - last.x) < 0.01 && Math.abs(last.x - pt.x) < 0.01 || Math.abs(before.y - last.y) < 0.01 && Math.abs(last.y - pt.y) < 0.01)) {
323
+ out[out.length - 1] = pt;
324
+ continue;
325
+ }
326
+ out.push(pt);
327
+ }
328
+ return out;
329
+ };
330
+ var routeEdges = (nodes, edges, options = {}) => {
331
+ const stub = options.stub ?? 16;
332
+ const spacing = options.spacing ?? 12;
333
+ const clearance = options.clearance ?? 14;
334
+ const radius = options.radius ?? 10;
335
+ const byId = new Map(nodes.map((n) => [n.id, n]));
336
+ const others = (edge) => nodes.filter((n) => n.id !== edge.from && n.id !== edge.to);
337
+ const curves = /* @__PURE__ */ new Map();
338
+ const orthogonal = [];
339
+ for (const edge of edges) {
340
+ if (edge.end.x - edge.start.x >= 40 && edge.from !== edge.to) {
341
+ const curve = edgeCurve(edge.start, edge.end);
342
+ const route = { kind: "curve", points: [], curve };
343
+ if (!others(edge).some((n) => routeHitsRect(route, n, 2))) {
344
+ curves.set(edge.id, curve);
345
+ continue;
346
+ }
347
+ }
348
+ orthogonal.push(edge);
349
+ }
350
+ const exitX = /* @__PURE__ */ new Map();
351
+ const entryX = /* @__PURE__ */ new Map();
352
+ const assignChannels = (key, at, side, into) => {
353
+ const groups = /* @__PURE__ */ new Map();
354
+ for (const e of orthogonal) groups.set(key(e), [...groups.get(key(e)) ?? [], e]);
355
+ for (const group of groups.values()) {
356
+ const sorted = [...group].sort(
357
+ (a, b) => (side === 1 ? a.start.y - b.start.y : a.end.y - b.end.y) || 0
358
+ );
359
+ sorted.forEach((e, i) => into.set(e.id, at(e) + side * (stub + i * spacing)));
360
+ }
361
+ };
362
+ assignChannels(
363
+ (e) => e.from,
364
+ (e) => Math.max(e.start.x, (byId.get(e.from)?.x ?? 0) + (byId.get(e.from)?.width ?? 0)),
365
+ 1,
366
+ exitX
367
+ );
368
+ assignChannels(
369
+ (e) => e.to,
370
+ (e) => Math.min(e.end.x, byId.get(e.to)?.x ?? e.end.x),
371
+ -1,
372
+ entryX
373
+ );
374
+ const runs = [];
375
+ const lanes = /* @__PURE__ */ new Map();
376
+ const order = [...orthogonal].sort(
377
+ (a, b) => Math.abs((exitX.get(a.id) ?? 0) - (entryX.get(a.id) ?? 0)) - Math.abs((exitX.get(b.id) ?? 0) - (entryX.get(b.id) ?? 0))
378
+ );
379
+ for (const edge of order) {
380
+ const xa = exitX.get(edge.id);
381
+ const xb = entryX.get(edge.id);
382
+ const left = Math.min(xa, xb);
383
+ const right = Math.max(xa, xb);
384
+ const solid = nodes.map((n) => inflate(n, clearance - 1));
385
+ const inSpan = nodes.filter((n) => n.x < right + clearance && n.x + n.width > left - clearance).map((n) => inflate(n, clearance)).sort((p, q) => p.y - q.y);
386
+ const corridors = [];
387
+ let reach = -Infinity;
388
+ for (const r of inSpan) {
389
+ if (r.y > reach) corridors.push({ from: reach, to: r.y });
390
+ reach = Math.max(reach, r.y + r.height);
391
+ }
392
+ corridors.push({ from: reach, to: Infinity });
393
+ const travelAt = (y2) => Math.abs(edge.start.y - y2) + Math.abs(edge.end.y - y2);
394
+ const ranked = corridors.map((c) => {
395
+ const ideal = clamp((edge.start.y + edge.end.y) / 2, c.from, c.to);
396
+ return { ...c, cost: travelAt(ideal) };
397
+ }).sort((p, q) => p.cost - q.cost);
398
+ let best;
399
+ for (const c of ranked) {
400
+ const slots = [];
401
+ if (Number.isFinite(c.from) && Number.isFinite(c.to)) {
402
+ const middle = (c.from + c.to) / 2;
403
+ for (let k = 0; k * spacing <= (c.to - c.from) / 2; k++) {
404
+ slots.push(middle + k * spacing);
405
+ if (k > 0) slots.push(middle - k * spacing);
406
+ }
407
+ } else if (Number.isFinite(c.from)) {
408
+ for (let k = 0; k < 24; k++) slots.push(c.from + 4 + k * spacing);
409
+ } else {
410
+ for (let k = 0; k < 24; k++) slots.push(c.to - 4 - k * spacing);
411
+ }
412
+ for (const y2 of slots) {
413
+ if (!(y2 > c.from && y2 < c.to)) continue;
414
+ const path = [
415
+ { x: xa, y: edge.start.y },
416
+ { x: xa, y: y2 },
417
+ { x: xb, y: y2 },
418
+ { x: xb, y: edge.end.y }
419
+ ];
420
+ let blocked = false;
421
+ for (let i = 1; i < path.length && !blocked; i++) {
422
+ blocked = solid.some((r) => segmentHitsRect(path[i - 1], path[i], r));
423
+ }
424
+ if (!blocked) {
425
+ const rest = others(edge);
426
+ blocked = rest.some((r) => segmentHitsRect(edge.start, path[0], r)) || rest.some((r) => segmentHitsRect(path[3], edge.end, r));
427
+ }
428
+ if (blocked) continue;
429
+ if (runs.some((r) => Math.abs(r.y - y2) < spacing - 0.5 && r.x1 < right && left < r.x2)) {
430
+ continue;
431
+ }
432
+ best = y2;
433
+ break;
434
+ }
435
+ if (best !== void 0) break;
436
+ }
437
+ const bottom = Math.max(edge.start.y, edge.end.y, ...inSpan.map((n) => n.y + n.height));
438
+ const y = best ?? bottom + spacing;
439
+ runs.push({ y, x1: left, x2: right });
440
+ lanes.set(edge.id, [
441
+ edge.start,
442
+ { x: xa, y: edge.start.y },
443
+ { x: xa, y },
444
+ { x: xb, y },
445
+ { x: xb, y: edge.end.y },
446
+ edge.end
447
+ ]);
448
+ }
449
+ const placed = [];
450
+ const placeLabel = (candidates, size) => {
451
+ const boxAt = (c) => ({
452
+ x: c.x - size.width / 2,
453
+ y: c.y - size.height / 2,
454
+ width: size.width,
455
+ height: size.height
456
+ });
457
+ for (const c of candidates) {
458
+ const box = boxAt(c);
459
+ if (nodes.some((n) => rectsOverlap(box, inflate(n, 2)))) continue;
460
+ if (placed.some((p) => rectsOverlap(box, p))) continue;
461
+ placed.push(box);
462
+ return c;
463
+ }
464
+ const fallback = candidates.find((c) => !nodes.some((n) => rectsOverlap(boxAt(c), n))) ?? candidates[0];
465
+ placed.push(boxAt(fallback));
466
+ return fallback;
467
+ };
468
+ const fromMiddle = (steps) => {
469
+ const ts = [0.5];
470
+ for (let i = 1; i < steps; i++) ts.push(0.5 - i / (steps * 2), 0.5 + i / (steps * 2));
471
+ return ts;
472
+ };
473
+ return edges.map((edge) => {
474
+ const size = edge.label ?? EDGE_HANDLE_SIZE;
475
+ const curve = curves.get(edge.id);
476
+ if (curve) {
477
+ const label2 = placeLabel(
478
+ fromMiddle(10).map((t) => curvePoint(curve, t)),
479
+ size
480
+ );
481
+ return {
482
+ id: edge.id,
483
+ kind: "curve",
484
+ points: [curve.p0, curve.p3],
485
+ curve,
486
+ path: curvePath(curve),
487
+ label: label2
488
+ };
489
+ }
490
+ const points = simplify(lanes.get(edge.id));
491
+ let lane = [points[0], points[points.length - 1]];
492
+ let longest = -1;
493
+ for (let i = 1; i < points.length; i++) {
494
+ const a = points[i - 1];
495
+ const b = points[i];
496
+ if (Math.abs(a.y - b.y) < 0.01 && Math.abs(a.x - b.x) > longest) {
497
+ longest = Math.abs(a.x - b.x);
498
+ lane = [a, b];
499
+ }
500
+ }
501
+ const label = placeLabel(
502
+ fromMiddle(12).map((t) => ({ x: lane[0].x + (lane[1].x - lane[0].x) * t, y: lane[0].y })),
503
+ size
504
+ );
505
+ return {
506
+ id: edge.id,
507
+ kind: "orthogonal",
508
+ points,
509
+ path: roundedPath(points, radius),
510
+ label
511
+ };
512
+ });
513
+ };
237
514
  // Annotate the CommonJS export names for ESM import in node:
238
515
  0 && (module.exports = {
239
516
  DEFAULT_MAX_ZOOM,
240
517
  DEFAULT_MIN_ZOOM,
518
+ EDGE_HANDLE_SIZE,
241
519
  IDENTITY_VIEWPORT,
242
520
  boundsOf,
243
521
  centerOn,
@@ -246,7 +524,9 @@ var readingOrder = (nodes, column = 40) => nodes.map((node, index) => ({ node, i
246
524
  curvePoint,
247
525
  defaultCanvasLabels,
248
526
  distanceToCurve,
527
+ distanceToRoute,
249
528
  edgeCurve,
529
+ estimateLabelSize,
250
530
  fitViewport,
251
531
  hitEdge,
252
532
  hitNode,
@@ -257,7 +537,11 @@ var readingOrder = (nodes, column = 40) => nodes.map((node, index) => ({ node, i
257
537
  pinchViewport,
258
538
  readingOrder,
259
539
  rectContains,
540
+ roundedPath,
541
+ routeEdges,
542
+ routeHitsRect,
260
543
  screenToWorld,
544
+ segmentHitsRect,
261
545
  snapPoint,
262
546
  snapTo,
263
547
  worldToScreen,
package/dist/canvas.d.cts CHANGED
@@ -87,11 +87,17 @@ declare const curvePath: ({ p0, p1, p2, p3 }: Curve) => string;
87
87
  declare const curvePoint: ({ p0, p1, p2, p3 }: Curve, t: number) => Point;
88
88
  /** How far `point` is from a curve (the curve is walked in `samples` straight pieces). */
89
89
  declare const distanceToCurve: (curve: Curve, point: Point, samples?: number) => number;
90
- /** The id of the edge within `tolerance` of `point`, the nearest one when several are. */
91
- declare const hitEdge: (edges: {
90
+ /**
91
+ * The id of the edge within `tolerance` of `point`, the nearest one when several are. An edge is
92
+ * given by its curve, or by its route (`routeEdges`).
93
+ */
94
+ declare const hitEdge: (edges: ({
92
95
  id: string;
93
96
  curve: Curve;
94
- }[], point: Point, tolerance?: number) => string | undefined;
97
+ } | {
98
+ id: string;
99
+ route: EdgeRoute;
100
+ })[], point: Point, tolerance?: number) => string | undefined;
95
101
  /**
96
102
  * The order a keyboard walks the nodes in: left to right, then top to bottom - the direction a
97
103
  * graph that flows from its entry to its end is read in. Nodes in the same column (within
@@ -100,6 +106,68 @@ declare const hitEdge: (edges: {
100
106
  declare const readingOrder: <T extends Point & {
101
107
  id: string;
102
108
  }>(nodes: T[], column?: number) => string[];
109
+ /** A card the router must route around: its id and where it is (world). */
110
+ interface RouteNode extends Rect {
111
+ id: string;
112
+ }
113
+ /** One edge to route: the ids of its ends and the points it leaves and enters at. */
114
+ interface RouteEdge {
115
+ id: string;
116
+ from: string;
117
+ to: string;
118
+ /** Where it leaves (an output port, on the right side of `from`). */
119
+ start: Point;
120
+ /** Where it arrives (the input, on the left side of `to`). */
121
+ end: Point;
122
+ /** The size of its label, to place it clear of the cards (default: a small handle). */
123
+ label?: Size;
124
+ }
125
+ /** How an edge is drawn. */
126
+ interface EdgeRoute {
127
+ id: string;
128
+ /** `curve`: the forward S. `orthogonal`: straight runs with rounded corners, through a lane. */
129
+ kind: 'curve' | 'orthogonal';
130
+ /** The corners of an orthogonal route, start to end; for a curve, points along it. */
131
+ points: Point[];
132
+ /** The curve, for `kind: 'curve'`. */
133
+ curve?: Curve;
134
+ /** The SVG path data. */
135
+ path: string;
136
+ /** Where its label (or handle) is centred. */
137
+ label: Point;
138
+ }
139
+ interface RouteOptions {
140
+ /** How far a route runs out of a port before it turns (default 16). */
141
+ stub?: number;
142
+ /** The distance between two parallel runs: lanes and channels (default 12). */
143
+ spacing?: number;
144
+ /** The room kept between a lane and a card (default 14). */
145
+ clearance?: number;
146
+ /** The radius of the rounded corners (default 10). */
147
+ radius?: number;
148
+ }
149
+ /** The default size of an edge's handle when it has no label. */
150
+ declare const EDGE_HANDLE_SIZE: Size;
151
+ /**
152
+ * A guess of the size of an edge's label pill (its text in a 22px-high pill), for placing it
153
+ * before it is measured. Generous: a little wider than the stylesheet draws it.
154
+ */
155
+ declare const estimateLabelSize: (text: string | undefined) => Size;
156
+ /** Does the straight segment from `a` to `b` pass through the inside of `rect`? */
157
+ declare const segmentHitsRect: (a: Point, b: Point, rect: Rect) => boolean;
158
+ /** Does a route pass through `rect` (grown by `margin`)? */
159
+ declare const routeHitsRect: (route: Pick<EdgeRoute, "kind" | "points" | "curve">, rect: Rect, margin?: number) => boolean;
160
+ /** How far `point` is from a route. */
161
+ declare const distanceToRoute: (route: Pick<EdgeRoute, "kind" | "points" | "curve">, point: Point) => number;
162
+ /** The SVG path data of a polyline whose corners are rounded with `radius`. */
163
+ declare const roundedPath: (points: Point[], radius?: number) => string;
164
+ /**
165
+ * Route every edge of a graph. Forward edges stay curves; loops back, row wraps and edges a curve
166
+ * would draw across a card become orthogonal routes through lanes that no card stands on and no
167
+ * other route already uses. Labels are placed on the route, clear of the cards and of each other
168
+ * where there is room: an orthogonal route's on its lane.
169
+ */
170
+ declare const routeEdges: (nodes: RouteNode[], edges: RouteEdge[], options?: RouteOptions) => EdgeRoute[];
103
171
 
104
- export { DEFAULT_MAX_ZOOM, DEFAULT_MIN_ZOOM, IDENTITY_VIEWPORT, boundsOf, centerOn, clamp, curvePath, curvePoint, distanceToCurve, edgeCurve, fitViewport, hitEdge, hitNode, inputAnchor, isRectVisible, outputAnchor, panBy, pinchViewport, readingOrder, rectContains, screenToWorld, snapPoint, snapTo, worldToScreen, zoomAt };
105
- export type { Curve, EdgeCurveOptions, FitOptions, Point, Rect, Size, Viewport };
172
+ export { DEFAULT_MAX_ZOOM, DEFAULT_MIN_ZOOM, EDGE_HANDLE_SIZE, IDENTITY_VIEWPORT, boundsOf, centerOn, clamp, curvePath, curvePoint, distanceToCurve, distanceToRoute, edgeCurve, estimateLabelSize, fitViewport, hitEdge, hitNode, inputAnchor, isRectVisible, outputAnchor, panBy, pinchViewport, readingOrder, rectContains, roundedPath, routeEdges, routeHitsRect, screenToWorld, segmentHitsRect, snapPoint, snapTo, worldToScreen, zoomAt };
173
+ export type { Curve, EdgeCurveOptions, EdgeRoute, FitOptions, Point, Rect, RouteEdge, RouteNode, RouteOptions, Size, Viewport };
package/dist/canvas.d.ts CHANGED
@@ -87,11 +87,17 @@ declare const curvePath: ({ p0, p1, p2, p3 }: Curve) => string;
87
87
  declare const curvePoint: ({ p0, p1, p2, p3 }: Curve, t: number) => Point;
88
88
  /** How far `point` is from a curve (the curve is walked in `samples` straight pieces). */
89
89
  declare const distanceToCurve: (curve: Curve, point: Point, samples?: number) => number;
90
- /** The id of the edge within `tolerance` of `point`, the nearest one when several are. */
91
- declare const hitEdge: (edges: {
90
+ /**
91
+ * The id of the edge within `tolerance` of `point`, the nearest one when several are. An edge is
92
+ * given by its curve, or by its route (`routeEdges`).
93
+ */
94
+ declare const hitEdge: (edges: ({
92
95
  id: string;
93
96
  curve: Curve;
94
- }[], point: Point, tolerance?: number) => string | undefined;
97
+ } | {
98
+ id: string;
99
+ route: EdgeRoute;
100
+ })[], point: Point, tolerance?: number) => string | undefined;
95
101
  /**
96
102
  * The order a keyboard walks the nodes in: left to right, then top to bottom - the direction a
97
103
  * graph that flows from its entry to its end is read in. Nodes in the same column (within
@@ -100,6 +106,68 @@ declare const hitEdge: (edges: {
100
106
  declare const readingOrder: <T extends Point & {
101
107
  id: string;
102
108
  }>(nodes: T[], column?: number) => string[];
109
+ /** A card the router must route around: its id and where it is (world). */
110
+ interface RouteNode extends Rect {
111
+ id: string;
112
+ }
113
+ /** One edge to route: the ids of its ends and the points it leaves and enters at. */
114
+ interface RouteEdge {
115
+ id: string;
116
+ from: string;
117
+ to: string;
118
+ /** Where it leaves (an output port, on the right side of `from`). */
119
+ start: Point;
120
+ /** Where it arrives (the input, on the left side of `to`). */
121
+ end: Point;
122
+ /** The size of its label, to place it clear of the cards (default: a small handle). */
123
+ label?: Size;
124
+ }
125
+ /** How an edge is drawn. */
126
+ interface EdgeRoute {
127
+ id: string;
128
+ /** `curve`: the forward S. `orthogonal`: straight runs with rounded corners, through a lane. */
129
+ kind: 'curve' | 'orthogonal';
130
+ /** The corners of an orthogonal route, start to end; for a curve, points along it. */
131
+ points: Point[];
132
+ /** The curve, for `kind: 'curve'`. */
133
+ curve?: Curve;
134
+ /** The SVG path data. */
135
+ path: string;
136
+ /** Where its label (or handle) is centred. */
137
+ label: Point;
138
+ }
139
+ interface RouteOptions {
140
+ /** How far a route runs out of a port before it turns (default 16). */
141
+ stub?: number;
142
+ /** The distance between two parallel runs: lanes and channels (default 12). */
143
+ spacing?: number;
144
+ /** The room kept between a lane and a card (default 14). */
145
+ clearance?: number;
146
+ /** The radius of the rounded corners (default 10). */
147
+ radius?: number;
148
+ }
149
+ /** The default size of an edge's handle when it has no label. */
150
+ declare const EDGE_HANDLE_SIZE: Size;
151
+ /**
152
+ * A guess of the size of an edge's label pill (its text in a 22px-high pill), for placing it
153
+ * before it is measured. Generous: a little wider than the stylesheet draws it.
154
+ */
155
+ declare const estimateLabelSize: (text: string | undefined) => Size;
156
+ /** Does the straight segment from `a` to `b` pass through the inside of `rect`? */
157
+ declare const segmentHitsRect: (a: Point, b: Point, rect: Rect) => boolean;
158
+ /** Does a route pass through `rect` (grown by `margin`)? */
159
+ declare const routeHitsRect: (route: Pick<EdgeRoute, "kind" | "points" | "curve">, rect: Rect, margin?: number) => boolean;
160
+ /** How far `point` is from a route. */
161
+ declare const distanceToRoute: (route: Pick<EdgeRoute, "kind" | "points" | "curve">, point: Point) => number;
162
+ /** The SVG path data of a polyline whose corners are rounded with `radius`. */
163
+ declare const roundedPath: (points: Point[], radius?: number) => string;
164
+ /**
165
+ * Route every edge of a graph. Forward edges stay curves; loops back, row wraps and edges a curve
166
+ * would draw across a card become orthogonal routes through lanes that no card stands on and no
167
+ * other route already uses. Labels are placed on the route, clear of the cards and of each other
168
+ * where there is room: an orthogonal route's on its lane.
169
+ */
170
+ declare const routeEdges: (nodes: RouteNode[], edges: RouteEdge[], options?: RouteOptions) => EdgeRoute[];
103
171
 
104
- export { DEFAULT_MAX_ZOOM, DEFAULT_MIN_ZOOM, IDENTITY_VIEWPORT, boundsOf, centerOn, clamp, curvePath, curvePoint, distanceToCurve, edgeCurve, fitViewport, hitEdge, hitNode, inputAnchor, isRectVisible, outputAnchor, panBy, pinchViewport, readingOrder, rectContains, screenToWorld, snapPoint, snapTo, worldToScreen, zoomAt };
105
- export type { Curve, EdgeCurveOptions, FitOptions, Point, Rect, Size, Viewport };
172
+ export { DEFAULT_MAX_ZOOM, DEFAULT_MIN_ZOOM, EDGE_HANDLE_SIZE, IDENTITY_VIEWPORT, boundsOf, centerOn, clamp, curvePath, curvePoint, distanceToCurve, distanceToRoute, edgeCurve, estimateLabelSize, fitViewport, hitEdge, hitNode, inputAnchor, isRectVisible, outputAnchor, panBy, pinchViewport, readingOrder, rectContains, roundedPath, routeEdges, routeHitsRect, screenToWorld, segmentHitsRect, snapPoint, snapTo, worldToScreen, zoomAt };
173
+ export type { Curve, EdgeCurveOptions, EdgeRoute, FitOptions, Point, Rect, RouteEdge, RouteNode, RouteOptions, Size, Viewport };