yuhwa 1.5.2 → 1.6.0

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.
Files changed (45) hide show
  1. package/dist/astro.d.ts +1 -1
  2. package/dist/astro.js +1 -1
  3. package/dist/browser.d.ts +3 -3
  4. package/dist/browser.js +5 -5
  5. package/dist/canvas.js +1 -1
  6. package/dist/cli.js +11 -3
  7. package/dist/editor/assets/{index-Dd3yJXpt.css → index-BtyZhez4.css} +1 -1
  8. package/dist/editor/assets/index-ChQpisN7.js +689 -0
  9. package/dist/editor/assets/{lsp.worker-BuZEbjK9.js → lsp.worker-bwlomb1r.js} +22 -22
  10. package/dist/editor/index.html +2 -2
  11. package/dist/{figure-BtRpJvZ9.d.ts → figure-CqzRbgF5.d.ts} +1 -1
  12. package/dist/{index-C6v0NhkF.d.ts → index-CT7AJAf8.d.ts} +3 -2
  13. package/dist/index.d.ts +3 -3
  14. package/dist/index.js +5 -5
  15. package/dist/ironmark.d.ts +1 -1
  16. package/dist/ironmark.js +1 -1
  17. package/dist/llms.txt +14 -6
  18. package/dist/lsp-browser.js +1 -1
  19. package/dist/lsp-node.js +15 -8
  20. package/dist/lsp-server.js +1 -1
  21. package/dist/lsp.js +1 -1
  22. package/dist/markdown-it.d.ts +2 -2
  23. package/dist/markdown-it.js +2 -2
  24. package/dist/{node-Cdzt2Mis.js → node-MjVxKTHg.js} +3 -3
  25. package/dist/{parse-fnqTkelX.js → parse-BT2Xuqty.js} +426 -58
  26. package/dist/{plugin-DX1Pv2Tq.js → plugin-Dckiw3N9.js} +1 -1
  27. package/dist/{plugin-DhSeDLo1.d.ts → plugin-y9uy8ayQ.d.ts} +26 -2
  28. package/dist/plugin.d.ts +1 -1
  29. package/dist/plugin.js +2 -2
  30. package/dist/{raster-B_SJsm-q.js → raster-Dz9_MPsh.js} +1 -1
  31. package/dist/remark.d.ts +1 -1
  32. package/dist/remark.js +1 -1
  33. package/dist/satteri.d.ts +1 -1
  34. package/dist/satteri.js +1 -1
  35. package/dist/{server-BW2tKjSu.js → server-Bgqav5i4.js} +2 -2
  36. package/dist/{service-CXRIavFP.js → service-BFj8Un47.js} +17 -10
  37. package/dist/{shared-DJCDEbFf.js → shared-BuNGLF8_.js} +655 -109
  38. package/dist/{src-CQ8ltV_K.js → src-WJP0qnZY.js} +253 -57
  39. package/dist/vite.d.ts +1 -1
  40. package/dist/vite.js +3 -3
  41. package/dist/vuepress.d.ts +1 -1
  42. package/dist/vuepress.js +1 -1
  43. package/dist/yuhwa.iife.js +64 -56
  44. package/package.json +1 -1
  45. package/dist/editor/assets/index-DyXXchTq.js +0 -689
@@ -1066,12 +1066,70 @@ const textFont = (p, size, weight, theme) => {
1066
1066
  };
1067
1067
  /** an edge label's font */
1068
1068
  const edgeFont = (p, theme) => textFont(p, theme.fontSize * .86, 500, theme);
1069
+ /** the group ids from the outermost down to `groupId` */
1070
+ function groupPath(d, groupId) {
1071
+ const path = [];
1072
+ const seen = /* @__PURE__ */ new Set();
1073
+ let g = groupId;
1074
+ while (g && d.groups.has(g) && !seen.has(g)) {
1075
+ seen.add(g);
1076
+ path.push(g);
1077
+ g = d.groups.get(g).parent;
1078
+ }
1079
+ return path.reverse();
1080
+ }
1081
+ /** a prop a node takes from itself (or its classes), else from its innermost group that sets it */
1082
+ function inheritedProp(d, n, key) {
1083
+ return effectiveProps(d, n)[key] ?? groupProp(d, n.group, key)?.value;
1084
+ }
1085
+ /** the innermost group from `gid` outward that sets `key`, with its value */
1086
+ function groupProp(d, gid, key) {
1087
+ for (const g of groupPath(d, gid).reverse()) {
1088
+ const value = effectiveProps(d, d.groups.get(g))[key];
1089
+ if (value !== void 0) return {
1090
+ group: g,
1091
+ value
1092
+ };
1093
+ }
1094
+ }
1069
1095
  /** Merge class props then own props (own wins). */
1070
1096
  function effectiveProps(d, decl) {
1071
1097
  const out = {};
1072
1098
  for (const c of decl.classes ?? []) Object.assign(out, d.classes[c]);
1073
1099
  return Object.assign(out, decl.props);
1074
1100
  }
1101
+ /**
1102
+ * `padding` as in CSS: one value for every side, two for top/bottom and
1103
+ * left/right, three for top, left/right and bottom, four clockwise from the
1104
+ * top. Null unless it is one to four numbers of 0 or more.
1105
+ */
1106
+ function parsePadding(v) {
1107
+ const parts = v?.split(/[\s,]+/).filter(Boolean) ?? [];
1108
+ const ns = parts.map((x) => Number(x.replace(/px$/, "")));
1109
+ if (!parts.length || parts.length > 4 || ns.some((n) => !(n >= 0) || !Number.isFinite(n))) return null;
1110
+ const [top, right = top, bottom = top, left = right] = ns;
1111
+ return {
1112
+ top,
1113
+ right,
1114
+ bottom,
1115
+ left
1116
+ };
1117
+ }
1118
+ /** a group's `padding` (its own or from a class), else `fallback` on every side */
1119
+ function groupPadding(d, gid, fallback) {
1120
+ const g = d.groups.get(gid);
1121
+ return (g && parsePadding(effectiveProps(d, g).padding)) ?? {
1122
+ top: fallback,
1123
+ right: fallback,
1124
+ bottom: fallback,
1125
+ left: fallback
1126
+ };
1127
+ }
1128
+ /** the `gap` between a group's members: its own, else the nearest enclosing group's, else `fallback` */
1129
+ function groupGap(d, gid, fallback) {
1130
+ const own = groupProp(d, gid, "gap");
1131
+ return own ? size(own.value, fallback) : fallback;
1132
+ }
1075
1133
  /** the `stroke-dasharray` of a line style at a stroke width; null for solid */
1076
1134
  function dashFor(style, width) {
1077
1135
  switch (style) {
@@ -1191,6 +1249,82 @@ function resolveEdgeStyle(d, e, p, theme, defaultCurve) {
1191
1249
  opacity: num$1(p.opacity, 1)
1192
1250
  };
1193
1251
  }
1252
+ // src/core/align.ts
1253
+ const ALIGN_SIDES = [
1254
+ "center",
1255
+ "left",
1256
+ "right",
1257
+ "top",
1258
+ "bottom"
1259
+ ];
1260
+ /** `[center|left|right|top|bottom]`, for warnings and help */
1261
+ const ALIGN_SIDES_HINT = `[${ALIGN_SIDES.join("|")}]`;
1262
+ /** `[side] ids...` as written after `align`: the side (default `center`) and the rest */
1263
+ function alignSide(words) {
1264
+ const side = ALIGN_SIDES.find((x) => x === words[0]);
1265
+ return side ? [side, words.slice(1)] : ["center", words];
1266
+ }
1267
+ /**
1268
+ * Whether a layout line lists ids (`rank a b c`, `align left a b`) rather
1269
+ * than placing a node that happens to be called `rank` or `align`.
1270
+ */
1271
+ function listsIds(head, next) {
1272
+ return (head === "rank" || head === "align") && next !== "at" && next !== "size";
1273
+ }
1274
+ /** every node and group with an `align` prop, its own or from a class */
1275
+ function alignProps(d) {
1276
+ const out = [];
1277
+ const decls = [...[...d.nodes.values()].map((n) => [n, false]), ...[...d.groups.values()].map((g) => [g, true])];
1278
+ for (const [decl, group] of decls) {
1279
+ const value = effectiveProps(d, decl).align?.trim();
1280
+ if (value) {
1281
+ const [side, rest] = alignSide(value.split(/\s+/));
1282
+ out.push({
1283
+ decl,
1284
+ group,
1285
+ value,
1286
+ side,
1287
+ rest
1288
+ });
1289
+ }
1290
+ }
1291
+ return out;
1292
+ }
1293
+ /**
1294
+ * Every `align` set: the layout block's lines plus the props. `align: [side] id`
1295
+ * lines a node or group up with that id; a group's lone `align: side` lines up
1296
+ * its own members without moving them to another rank (`within`). Sets on the
1297
+ * same side that share an id become one set; member sets stay on their own.
1298
+ */
1299
+ function alignSets(d) {
1300
+ const sets = [...d.layout.aligns ?? []];
1301
+ const props = alignProps(d);
1302
+ const members = /* @__PURE__ */ new Map();
1303
+ if (props.some((p) => p.group && !p.rest.length)) {
1304
+ const add = (g, id) => g && (members.get(g)?.push(id) ?? members.set(g, [id]));
1305
+ d.nodes.forEach((n) => add(n.group, n.id));
1306
+ d.groups.forEach((g) => add(g.parent, g.id));
1307
+ }
1308
+ const out = [];
1309
+ for (const { decl, group, side, rest } of props) if (rest[0]) sets.push({
1310
+ side,
1311
+ ids: [decl.id, rest[0]]
1312
+ });
1313
+ else if (group && (members.get(decl.id)?.length ?? 0) > 1) out.push({
1314
+ side,
1315
+ ids: members.get(decl.id),
1316
+ within: decl.id
1317
+ });
1318
+ for (const s of sets) {
1319
+ const joined = out.filter((o) => !o.within && o.side === s.side && o.ids.some((x) => s.ids.includes(x)));
1320
+ const ids = /* @__PURE__ */ new Set([...joined.flatMap((o) => o.ids), ...s.ids]);
1321
+ out.splice(0, out.length, ...out.filter((o) => !joined.includes(o)), {
1322
+ side: s.side,
1323
+ ids: [...ids]
1324
+ });
1325
+ }
1326
+ return out;
1327
+ }
1194
1328
  // src/core/route.ts
1195
1329
  const SIDES$2 = [
1196
1330
  "top",
@@ -1288,6 +1422,28 @@ function avoidObstacles(a, b, s0, s1, free0, free1, obstacles) {
1288
1422
  }
1289
1423
  return best;
1290
1424
  }
1425
+ /** which ends a `merge` value lets join a trunk: [start, end] */
1426
+ const MERGE_ENDS = {
1427
+ both: [true, true],
1428
+ out: [true, false],
1429
+ in: [false, true],
1430
+ none: [false, false]
1431
+ };
1432
+ /** the side a node's edges leave by when they follow the layout direction */
1433
+ /** the side a node's edges leave by in each direction */
1434
+ const FORWARD = {
1435
+ right: "right",
1436
+ down: "bottom",
1437
+ left: "left",
1438
+ up: "top"
1439
+ };
1440
+ /** whether `b` lies wholly past `a` in the layout direction */
1441
+ function ahead(a, b, dir) {
1442
+ if (dir === "right") return b.x >= a.x + a.w;
1443
+ if (dir === "left") return b.x + b.w <= a.x;
1444
+ if (dir === "down") return b.y >= a.y + a.h;
1445
+ return b.y + b.h <= a.y;
1446
+ }
1291
1447
  const headKind = (v, dflt) => v && (v === "none" || MARKER_KINDS.has(v)) ? v : dflt;
1292
1448
  function routeEdges(d, nodes, ctx) {
1293
1449
  const items = [];
@@ -1302,6 +1458,17 @@ function routeEdges(d, nodes, ctx) {
1302
1458
  const fanGap = ctx.theme.fontSize * 2.1;
1303
1459
  const edgeByKey = new Map(d.edges.map((e) => [e.key, e]));
1304
1460
  const laneOffs = /* @__PURE__ */ new Map();
1461
+ const mergeOf = /* @__PURE__ */ new Map();
1462
+ const nodeMerge = (id) => {
1463
+ let ends = mergeOf.get(id);
1464
+ if (!ends) {
1465
+ const n = d.nodes.get(id);
1466
+ const v = (n && inheritedProp(d, n, "merge")) ?? d.settings.merge ?? "none";
1467
+ ends = MERGE_ENDS[v] ?? MERGE_ENDS.none;
1468
+ mergeOf.set(id, ends);
1469
+ }
1470
+ return ends;
1471
+ };
1305
1472
  for (const e of d.edges) {
1306
1473
  const a = nodes.get(e.from);
1307
1474
  const b = nodes.get(e.to);
@@ -1350,6 +1517,11 @@ function routeEdges(d, nodes, ctx) {
1350
1517
  let s0 = fixed0 ?? autoSide(a, via[0] ?? center(b), via.length ? null : b, ctx.direction);
1351
1518
  let s1 = fixed1 ?? autoSide(b, via[via.length - 1] ?? center(a), via.length ? null : a, ctx.direction);
1352
1519
  if (!via.length && a !== b && (!fixed0 || !fixed1)) [s0, s1] = avoidObstacles(a, b, s0, s1, !fixed0, !fixed1, ctx.obstacles);
1520
+ const mergeable = a === b || manualVia || (bundle?.length ?? 0) > 1 ? [false, false] : p.merge ? MERGE_ENDS[p.merge] ?? MERGE_ENDS.none : [nodeMerge(e.from)[0], nodeMerge(e.to)[1]];
1521
+ if (ahead(a, b, ctx.direction)) {
1522
+ s0 = mergeable[0] && !fixed0 ? FORWARD[ctx.direction] : s0;
1523
+ s1 = mergeable[1] && !fixed1 ? OPPOSITE[FORWARD[ctx.direction]] : s1;
1524
+ }
1353
1525
  items.push({
1354
1526
  e,
1355
1527
  a,
@@ -1361,34 +1533,140 @@ function routeEdges(d, nodes, ctx) {
1361
1533
  style,
1362
1534
  headKind: e.endHead || p.head ? headKind(p.head, "arrow") : "none",
1363
1535
  tailKind: e.startHead || p.tail ? headKind(p.tail, "arrow") : "none",
1364
- labelOff: o?.label
1536
+ labelOff: o?.label,
1537
+ mergeable,
1538
+ merged: [void 0, void 0]
1365
1539
  });
1366
1540
  }
1541
+ const trunks = sizeTrunks(groupTrunks(items));
1367
1542
  const slots = /* @__PURE__ */ new Map();
1543
+ const addSlot = (it, end, node, side, members) => {
1544
+ const others = members.map((m) => farEnd(m, end));
1545
+ const other = {
1546
+ x: others.reduce((x, q) => x + q.x, 0) / others.length,
1547
+ y: others.reduce((y, q) => y + q.y, 0) / others.length
1548
+ };
1549
+ const k = `${node.id}\u0000${side}`;
1550
+ const slot = slots.get(k) ?? {
1551
+ side,
1552
+ list: []
1553
+ };
1554
+ slots.set(k, slot);
1555
+ slot.list.push({
1556
+ it,
1557
+ end,
1558
+ other
1559
+ });
1560
+ };
1368
1561
  for (const it of items) {
1369
1562
  if (it.a === it.b) continue;
1370
- for (const end of [0, 1]) {
1371
- const node = end === 0 ? it.a : it.b;
1372
- const other = end === 0 ? it.via[0] ?? center(it.b) : it.via[it.via.length - 1] ?? center(it.a);
1373
- const k = `${node.id}\u0000${it.sides[end]}`;
1374
- if (!slots.has(k)) slots.set(k, []);
1375
- slots.get(k).push({
1376
- it,
1377
- end,
1378
- other
1379
- });
1380
- }
1563
+ for (const end of [0, 1]) if (!it.merged[end]) addSlot(it, end, end === 0 ? it.a : it.b, it.sides[end], [it]);
1381
1564
  }
1565
+ for (const t of trunks) addSlot(t.lead, t.end, t.node, t.side, t.members);
1382
1566
  const order = new Map(items.map((it, i) => [it, i]));
1383
- for (const [k, list] of slots) {
1384
- const side = k.split("\0")[1];
1567
+ for (const { side, list } of slots.values()) {
1385
1568
  const horizSide = side === "top" || side === "bottom";
1386
1569
  list.sort((p, q) => (horizSide ? p.other.x - q.other.x : p.other.y - q.other.y) || order.get(p.it) - order.get(q.it));
1387
1570
  const n = list.length;
1388
1571
  list.forEach((s, i) => s.it.t[s.end] = n === 1 ? 0 : (i + 1) / (n + 1) * 2 - 1);
1389
1572
  }
1573
+ placeJunctions(trunks);
1390
1574
  return items.map((it) => buildEdge(it, ctx));
1391
1575
  }
1576
+ /** the point an edge heads for after leaving `end`: its next waypoint or the far node */
1577
+ function farEnd(it, end) {
1578
+ return end === 0 ? it.via[0] ?? center(it.b) : it.via[it.via.length - 1] ?? center(it.a);
1579
+ }
1580
+ /** Join mergeable ends that share node, side and look into trunks. */
1581
+ function groupTrunks(items) {
1582
+ const groups = /* @__PURE__ */ new Map();
1583
+ for (const it of items) {
1584
+ const { stroke, width, dash, curve, flow, opacity } = it.style;
1585
+ const look = [
1586
+ stroke,
1587
+ width,
1588
+ dash,
1589
+ curve,
1590
+ flow,
1591
+ opacity
1592
+ ].join("\0");
1593
+ for (const end of [0, 1]) {
1594
+ if (!it.mergeable[end]) continue;
1595
+ const node = end === 0 ? it.a : it.b;
1596
+ const side = it.sides[end];
1597
+ const head = end === 0 ? it.tailKind : it.headKind;
1598
+ const k = [
1599
+ end,
1600
+ node.id,
1601
+ side,
1602
+ head,
1603
+ look
1604
+ ].join("\0");
1605
+ const trunk = groups.get(k);
1606
+ if (trunk) trunk.members.push(it);
1607
+ else groups.set(k, {
1608
+ members: [it],
1609
+ lead: it,
1610
+ end,
1611
+ node,
1612
+ side,
1613
+ reach: 0
1614
+ });
1615
+ }
1616
+ }
1617
+ const trunks = [...groups.values()].filter((t) => t.members.length > 1);
1618
+ for (const t of trunks) for (const m of t.members) m.merged[t.end] = t;
1619
+ return trunks;
1620
+ }
1621
+ /** shortest trunk worth drawing, and the share of the free run it takes */
1622
+ const TRUNK_MIN = 16;
1623
+ const TRUNK_SHARE = .4;
1624
+ /**
1625
+ * How far each trunk runs straight out from its node: part way towards the
1626
+ * nearest branch target, so every branch has room to turn. Trunks with no
1627
+ * room are dropped and their edges drawn on their own; the rest are returned.
1628
+ */
1629
+ function sizeTrunks(trunks) {
1630
+ return trunks.filter((trunk) => {
1631
+ const { end, node } = trunk;
1632
+ const port = portPoint(node.shape, node, trunk.side, 0);
1633
+ const n = SIDE_NORMAL[trunk.side];
1634
+ const along = (q) => (q.x - port.x) * n.x + (q.y - port.y) * n.y;
1635
+ trunk.reach = Math.min(...trunk.members.map((m) => {
1636
+ const via = end === 0 ? m.via[0] : m.via[m.via.length - 1];
1637
+ if (via) return along(via);
1638
+ const o = end === 0 ? m.b : m.a;
1639
+ const corners = [
1640
+ o,
1641
+ {
1642
+ x: o.x + o.w,
1643
+ y: o.y
1644
+ },
1645
+ {
1646
+ x: o.x,
1647
+ y: o.y + o.h
1648
+ },
1649
+ {
1650
+ x: o.x + o.w,
1651
+ y: o.y + o.h
1652
+ }
1653
+ ];
1654
+ return Math.min(...corners.map(along));
1655
+ })) * TRUNK_SHARE;
1656
+ if (trunk.reach >= TRUNK_MIN) return true;
1657
+ for (const m of trunk.members) m.merged[end] = void 0;
1658
+ return false;
1659
+ });
1660
+ }
1661
+ /** Once the ports are spread: every branch shares the lead's port, the junction sits `reach` out from it. */
1662
+ function placeJunctions(trunks) {
1663
+ for (const trunk of trunks) {
1664
+ const { end, lead } = trunk;
1665
+ const port = portPoint(trunk.node.shape, trunk.node, trunk.side, lead.t[end]);
1666
+ trunk.junction = add(port, SIDE_NORMAL[trunk.side], trunk.reach);
1667
+ for (const m of trunk.members) m.t[end] = lead.t[end];
1668
+ }
1669
+ }
1392
1670
  function buildEdge(it, ctx) {
1393
1671
  const { a, b, style } = it;
1394
1672
  let geo;
@@ -1421,40 +1699,18 @@ function buildEdge(it, ctx) {
1421
1699
  } else {
1422
1700
  S = portPoint(a.shape, a, it.sides[0], it.t[0]);
1423
1701
  E = portPoint(b.shape, b, it.sides[1], it.t[1]);
1424
- const n0 = SIDE_NORMAL[it.sides[0]];
1425
- const n1 = SIDE_NORMAL[it.sides[1]];
1426
- if (ctx.channels?.has(it.e.key) && it.via.length >= 2) {
1427
- const via = it.via.map((q) => ({ ...q }));
1428
- via[0].y = S.y;
1429
- via[via.length - 1].y = E.y;
1430
- geo = {
1431
- kind: "poly",
1432
- pts: orthoRoute(S, n0, E, n1, via, a, b, ctx.obstacles),
1433
- radius: 10
1434
- };
1435
- } else if (style.curve === "orthogonal") geo = {
1436
- kind: "poly",
1437
- pts: orthoRoute(S, n0, E, n1, it.via, a, b, ctx.obstacles),
1438
- radius: 10
1439
- };
1440
- else if (style.curve === "straight") geo = {
1441
- kind: "poly",
1442
- pts: [
1443
- S,
1444
- ...it.via,
1445
- E
1446
- ],
1447
- radius: it.via.length ? 6 : 0
1448
- };
1449
- else geo = {
1450
- kind: "cubic",
1451
- segs: smoothRoute(S, n0, E, n1, it.via)
1452
- };
1702
+ geo = routeBetween(it, ctx, it.merged[0]?.junction ?? S, it.merged[1]?.junction ?? E);
1453
1703
  }
1454
- const samples = sample(geo);
1455
- let label;
1456
1704
  const caption = it.e.props.caption;
1457
- if (it.e.label || caption) {
1705
+ const branch = it.e.label || caption ? sample(geo) : void 0;
1706
+ const [tail, head] = it.merged;
1707
+ const trunkStart = tail?.lead === it;
1708
+ const trunkEnd = head?.lead === it;
1709
+ if (trunkStart) attachTrunk(geo, "start", S);
1710
+ if (trunkEnd) attachTrunk(geo, "end", E);
1711
+ const samples = branch && !trunkStart && !trunkEnd ? branch : sample(geo);
1712
+ let label;
1713
+ if (branch) {
1458
1714
  const fs = style.fontSize;
1459
1715
  const { mono } = ctx.theme;
1460
1716
  const lines = it.e.label ? it.e.label.split("\n") : [];
@@ -1481,7 +1737,7 @@ function buildEdge(it, ctx) {
1481
1737
  w: dx * 2,
1482
1738
  h: dy1 - dy0
1483
1739
  };
1484
- const anchor = it.labelOff ? add(pointAtHalf(samples), it.labelOff) : labelSpot(samples, boxAt, it, ctx);
1740
+ const anchor = it.labelOff ? add(pointAtHalf(branch), it.labelOff) : labelSpot(branch, boxAt, it, ctx);
1485
1741
  const box = boxAt(anchor);
1486
1742
  label = {
1487
1743
  text: it.e.label ?? "",
@@ -1501,7 +1757,7 @@ function buildEdge(it, ctx) {
1501
1757
  const hl = headSize(style.width);
1502
1758
  const endDir = endDirection(geo, "end");
1503
1759
  const startDir = endDirection(geo, "start");
1504
- if (it.headKind !== "none") {
1760
+ if (it.headKind !== "none" && (!head || trunkEnd)) {
1505
1761
  heads.push({
1506
1762
  at: E,
1507
1763
  angle: Math.atan2(endDir.y, endDir.x),
@@ -1509,7 +1765,7 @@ function buildEdge(it, ctx) {
1509
1765
  });
1510
1766
  trim(geo, "end", trimFor(it.headKind, hl, style.width));
1511
1767
  }
1512
- if (it.tailKind !== "none") {
1768
+ if (it.tailKind !== "none" && (!tail || trunkStart)) {
1513
1769
  heads.push({
1514
1770
  at: S,
1515
1771
  angle: Math.atan2(startDir.y, startDir.x),
@@ -1532,6 +1788,58 @@ function buildEdge(it, ctx) {
1532
1788
  style
1533
1789
  };
1534
1790
  }
1791
+ /** the line between two points that stand in for the ports: the ports themselves or trunk junctions */
1792
+ function routeBetween(it, ctx, S, E) {
1793
+ const { a, b, style } = it;
1794
+ const n0 = SIDE_NORMAL[it.sides[0]];
1795
+ const n1 = SIDE_NORMAL[it.sides[1]];
1796
+ if (ctx.channels?.has(it.e.key) && it.via.length >= 2) {
1797
+ const via = it.via.map((q) => ({ ...q }));
1798
+ via[0].y = S.y;
1799
+ via[via.length - 1].y = E.y;
1800
+ return {
1801
+ kind: "poly",
1802
+ pts: orthoRoute(S, n0, E, n1, via, a, b, ctx.obstacles),
1803
+ radius: 10
1804
+ };
1805
+ }
1806
+ if (style.curve === "orthogonal") return {
1807
+ kind: "poly",
1808
+ pts: orthoRoute(S, n0, E, n1, it.via, a, b, ctx.obstacles),
1809
+ radius: 10
1810
+ };
1811
+ if (style.curve === "straight") return {
1812
+ kind: "poly",
1813
+ pts: [
1814
+ S,
1815
+ ...it.via,
1816
+ E
1817
+ ],
1818
+ radius: it.via.length ? 6 : 0
1819
+ };
1820
+ return {
1821
+ kind: "cubic",
1822
+ segs: smoothRoute(S, n0, E, n1, it.via)
1823
+ };
1824
+ }
1825
+ /** Extend `geo` with a straight trunk from its start back to the port, or from its end on to it. */
1826
+ function attachTrunk(geo, at, port) {
1827
+ if (geo.kind === "poly") {
1828
+ if (at === "start") geo.pts.unshift(port);
1829
+ else geo.pts.push(port);
1830
+ return;
1831
+ }
1832
+ const [p, q] = at === "start" ? [port, geo.segs[0][0]] : [geo.segs[geo.segs.length - 1][3], port];
1833
+ const d = sub(q, p);
1834
+ const seg = [
1835
+ p,
1836
+ add(p, d, 1 / 3),
1837
+ add(p, d, 2 / 3),
1838
+ q
1839
+ ];
1840
+ if (at === "start") geo.segs.unshift(seg);
1841
+ else geo.segs.push(seg);
1842
+ }
1535
1843
  const LABEL_TRIES = [
1536
1844
  .5,
1537
1845
  .42,
@@ -2026,7 +2334,9 @@ const NODE_PROPS = [
2026
2334
  "body-weight",
2027
2335
  "line-height",
2028
2336
  "max-width",
2029
- ...PLACEMENT_PROPS
2337
+ ...PLACEMENT_PROPS,
2338
+ "align",
2339
+ "merge"
2030
2340
  ];
2031
2341
  const EDGE_PROPS = [
2032
2342
  "style",
@@ -2052,7 +2362,8 @@ const EDGE_PROPS = [
2052
2362
  "constraint",
2053
2363
  "caption",
2054
2364
  "rotate",
2055
- "length"
2365
+ "length",
2366
+ "merge"
2056
2367
  ];
2057
2368
  const GROUP_PROPS = [
2058
2369
  "color",
@@ -2063,7 +2374,11 @@ const GROUP_PROPS = [
2063
2374
  "dashed",
2064
2375
  "dotted",
2065
2376
  "meta",
2066
- "caps"
2377
+ "caps",
2378
+ "align",
2379
+ "merge",
2380
+ "gap",
2381
+ "padding"
2067
2382
  ];
2068
2383
  /** frame props every panel takes, next to the plugin's own */
2069
2384
  const PANEL_PROPS = [
@@ -2103,7 +2418,13 @@ const ENUMS = table({
2103
2418
  exit: SIDES$1,
2104
2419
  "from-side": SIDES$1,
2105
2420
  enter: SIDES$1,
2106
- "to-side": SIDES$1
2421
+ "to-side": SIDES$1,
2422
+ merge: [
2423
+ "none",
2424
+ "in",
2425
+ "out",
2426
+ "both"
2427
+ ]
2107
2428
  });
2108
2429
  /** props whose value must be a colour (or `none`/`transparent`), on nodes, edges, groups and panels */
2109
2430
  const COLORS = [
@@ -2152,7 +2473,8 @@ const NUMBERS = [
2152
2473
  "length",
2153
2474
  "body-size",
2154
2475
  "body-weight",
2155
- "line-height"
2476
+ "line-height",
2477
+ "gap"
2156
2478
  ];
2157
2479
  function distance(a, b) {
2158
2480
  const dp = Array.from({ length: b.length + 1 }, (_, i) => i);
@@ -2199,6 +2521,7 @@ function checkProps$1(props, allowed, what, line0, warn, propLines) {
2199
2521
  warn(line, `Unknown ${k} "${v}"${didYouMean(v, opts) || ` (use one of: ${shown.join(", ")})`}`);
2200
2522
  } else if (BOOLS.includes(k) && !["true", "false"].includes(v)) warn(line, `${k} expects true or false, got "${v}"`);
2201
2523
  else if (NUMBERS.includes(k) && !(parseFloat(v) >= 0 && Number.isFinite(parseFloat(v)))) warn(line, `${k} expects a number of 0 or more, got "${v}"`);
2524
+ else if (k === "padding" && !parsePadding(v)) warn(line, `padding expects 1 to 4 numbers of 0 or more, like CSS (top right bottom left), got "${v}"`);
2202
2525
  else if (k === "link" && !isSafeLink(v)) warn(line, `link "${v}" is not rendered: use http(s), mailto, an #anchor or a relative path`);
2203
2526
  }
2204
2527
  }
@@ -2226,6 +2549,20 @@ const propLine = (n, key) => n.ref?.propLines?.[key] ?? n.propAt?.[key] ?? n.ref
2226
2549
  function placementOf(props) {
2227
2550
  for (const [k, v] of Object.entries(props)) if (PLACEMENT_PROPS.includes(k) && v) return [k, v.trim()];
2228
2551
  }
2552
+ /** `align` props: the id must name another node or group; a lone side only fits a group */
2553
+ function checkAlign(d, warn) {
2554
+ const known = (id) => d.nodes.has(id) || d.groups.has(id);
2555
+ for (const { decl, group, value, rest } of alignProps(d)) {
2556
+ const line = decl.ref && propLine(decl, "align");
2557
+ const [target] = rest;
2558
+ if (line === void 0) continue;
2559
+ else if (rest.length > 1) warn(line, `align takes one id: align: ${ALIGN_SIDES_HINT} id`);
2560
+ else if (!target) {
2561
+ if (!group) warn(line, `align: ${value} needs an id to line up with: align: ${value} other`);
2562
+ } else if (target === decl.id) warn(line, `align: ${target} lines ${decl.id} up with itself; use another id`);
2563
+ else if (!known(target)) warn(line, `align: unknown node or group "${target}"${didYouMean(target, [...d.nodes.keys(), ...d.groups.keys()])}`);
2564
+ }
2565
+ }
2229
2566
  /** placement props: the anchor must be another node, one prop per node, no loops */
2230
2567
  function checkPlacement(d, warn) {
2231
2568
  const anchor = /* @__PURE__ */ new Map();
@@ -2302,6 +2639,7 @@ function lint(d, panelOptions) {
2302
2639
  ...n.ref.propLines
2303
2640
  });
2304
2641
  checkPlacement(d, warn);
2642
+ checkAlign(d, warn);
2305
2643
  const seen = /* @__PURE__ */ new Set();
2306
2644
  for (const e of d.edges) {
2307
2645
  if (!e.ref) continue;
@@ -2360,7 +2698,8 @@ const KNOWN_SETTINGS = /* @__PURE__ */ new Set([
2360
2698
  "panel-gap",
2361
2699
  "wrap",
2362
2700
  "edge-color",
2363
- "edge-width"
2701
+ "edge-width",
2702
+ "merge"
2364
2703
  ]);
2365
2704
  /** other tools' diagram names → the yuhwa panel header to write instead (`ID` is the panel id) */
2366
2705
  const PANEL_ALIASES = table({
@@ -2402,7 +2741,25 @@ const SETTING_VALUES = table({
2402
2741
  "column",
2403
2742
  "grid",
2404
2743
  "wrap"
2405
- ]
2744
+ ],
2745
+ merge: ENUMS.merge
2746
+ });
2747
+ /**
2748
+ * What a setting does, for hover, completion and the editor. Settings hold for
2749
+ * the whole diagram; the note names the group prop that overrides one.
2750
+ * A rank is one step of the flow: a column in `right`/`left`, a row in `down`/`up`.
2751
+ */
2752
+ const SETTING_HELP = table({
2753
+ direction: "which way the flow runs: `right`, `down`, `left` or `up`",
2754
+ curve: "how edges bend: `smooth`, `orthogonal` or `straight`",
2755
+ gap: "space between nodes in the same rank, px. A group's `gap` overrides it for its members",
2756
+ "rank-gap": "space between ranks, px: between columns in `right`/`left`, between rows in `down`/`up`. Default 72, or 96 with groups. A group's `gap` overrides it where only the group's own edges cross",
2757
+ padding: "space around the whole diagram, px. A group's `padding` is the space inside its border",
2758
+ "max-width": "fit the diagram into this width, px: ranks wrap into rows",
2759
+ wrap: "wrap node labels wider than this, px",
2760
+ merge: "edges that share a node and side join into one line there. A node's, group's or edge's `merge` overrides it",
2761
+ panels: "how panels line up next to the graph: `row`, `column`, `grid` or `wrap`",
2762
+ "panel-gap": "space between the graph and the panels, px"
2406
2763
  });
2407
2764
  /** Mermaid-style direction names are accepted too */
2408
2765
  const DIRECTION_ALIASES = table({
@@ -3694,6 +4051,7 @@ function mergeDiagram(d, sub, edgeCount) {
3694
4051
  });
3695
4052
  for (const [k, v] of Object.entries(sub.data ?? {})) if (!(k in (d.data ??= {}))) d.data[k] = v;
3696
4053
  if (sub.layout.ranks?.length) (d.layout.ranks ??= []).push(...sub.layout.ranks);
4054
+ if (sub.layout.aligns?.length) (d.layout.aligns ??= []).push(...sub.layout.aligns);
3697
4055
  for (const [k, v] of Object.entries(sub.vars ?? {})) (d.vars ??= {})[k] ??= v;
3698
4056
  for (const [k, v] of Object.entries(sub.defs ?? {})) (d.defs ??= {})[k] ??= v;
3699
4057
  for (const [k, v] of Object.entries(sub.themes ?? {})) (d.themes ??= table({}))[k] ??= v;
@@ -3862,12 +4220,22 @@ function parseLayoutLine(code, li, d, warn) {
3862
4220
  return out;
3863
4221
  };
3864
4222
  let i = 1;
3865
- if (head === "rank" && words[1] !== "at" && words[1] !== "size") {
4223
+ const list = listsIds(head, words[1]);
4224
+ if (list && head === "rank") {
3866
4225
  const ids = words.slice(1).filter(Boolean);
3867
4226
  if (ids.length < 2) return warn(li, "rank needs two or more node ids: rank a b c");
3868
4227
  (d.layout.ranks ??= []).push(ids);
3869
4228
  return;
3870
4229
  }
4230
+ if (list && head === "align") {
4231
+ const [side, ids] = alignSide(words.slice(1).filter(Boolean));
4232
+ if (ids.length < 2) return warn(li, `align needs two or more ids: align ${ALIGN_SIDES_HINT} a b c`);
4233
+ (d.layout.aligns ??= []).push({
4234
+ side,
4235
+ ids
4236
+ });
4237
+ return;
4238
+ }
3871
4239
  if (head.includes("->")) {
3872
4240
  const m = head.match(/^(.+?)->(.+?)(?:#(\d+))?$/);
3873
4241
  if (!m) return warn(li, `Bad edge key "${head}"`);
@@ -5257,7 +5625,7 @@ function swatch(it, x, y, theme, d) {
5257
5625
  return `<rect x="${f$1(box.x)}" y="${f$1(box.y)}" width="${f$1(box.w)}" height="${f$1(box.h)}" rx="3" fill="${fill}" stroke="${stroke}" stroke-width="1.2"/>`;
5258
5626
  }
5259
5627
  /** split the items into rows no wider than `max` */
5260
- function rows(ws, max, space) {
5628
+ function rows$1(ws, max, space) {
5261
5629
  const out = [[]];
5262
5630
  let w = 0;
5263
5631
  ws.forEach((iw, i) => {
@@ -5284,7 +5652,7 @@ const legend = {
5284
5652
  const { fs, space } = metrics(theme);
5285
5653
  const ws = widths(list, theme, approxMeasure);
5286
5654
  const total = ws.reduce((a, b) => a + b, 0) + space * (ws.length - 1);
5287
- const n = rows(ws, MAX_W, space).length;
5655
+ const n = rows$1(ws, MAX_W, space).length;
5288
5656
  return {
5289
5657
  bottom: fs * 1.4 + n * fs * 1.9,
5290
5658
  w: Math.min(total, MAX_W)
@@ -5296,7 +5664,7 @@ const legend = {
5296
5664
  const ws = widths(list, theme, approxMeasure);
5297
5665
  const ink = nodeInk(theme);
5298
5666
  const out = [];
5299
- rows(ws, Math.min(MAX_W, area.w - pad * 2), space).forEach((row, r) => {
5667
+ rows$1(ws, Math.min(MAX_W, area.w - pad * 2), space).forEach((row, r) => {
5300
5668
  let x = area.x + pad;
5301
5669
  const y = area.y + fs * 1.4 + r * fs * 1.9;
5302
5670
  for (const i of row) {
@@ -9606,6 +9974,41 @@ function renderBody(b, left, right, top, col, theme) {
9606
9974
  });
9607
9975
  return `<g class="body"${b.mono && !theme.mono ? ` font-family="${esc(MONO)}"` : ""}>${out.join("")}</g>`;
9608
9976
  }
9977
+ /** the frames around a box in these groups: each group's padding, plus its header on top */
9978
+ function frameOf(pads, path) {
9979
+ const out = {
9980
+ top: 0,
9981
+ right: 0,
9982
+ bottom: 0,
9983
+ left: 0
9984
+ };
9985
+ for (const g of path) {
9986
+ const p = pads.get(g);
9987
+ out.top += p.top + 26;
9988
+ out.right += p.right;
9989
+ out.bottom += p.bottom;
9990
+ out.left += p.left;
9991
+ }
9992
+ return out;
9993
+ }
9994
+ /** how many outer groups two group paths share */
9995
+ function commonDepth(a, b) {
9996
+ let c = 0;
9997
+ while (c < a.length && c < b.length && a[c] === b[c]) c++;
9998
+ return c;
9999
+ }
10000
+ /** these nodes bucketed by rank */
10001
+ function nodesByRank(ids, nodes, rank) {
10002
+ const out = /* @__PURE__ */ new Map();
10003
+ for (const id of ids) {
10004
+ const n = nodes.get(id);
10005
+ if (n) {
10006
+ const r = rank.get(id);
10007
+ out.get(r)?.push(n) ?? out.set(r, [n]);
10008
+ }
10009
+ }
10010
+ return [...out.values()];
10011
+ }
9609
10012
  /** Break `text` into lines no wider than `max` px, at spaces; explicit `\n` breaks stay. */
9610
10013
  function wrapText(text, max, width) {
9611
10014
  const out = [];
@@ -9674,17 +10077,6 @@ function titleBlock(d, theme) {
9674
10077
  h: (title ? titleSize * 1.3 : 0) + (subtitle ? subSize * 1.5 : 0) + 10
9675
10078
  };
9676
10079
  }
9677
- function groupPath(d, groupId) {
9678
- const path = [];
9679
- const seen = /* @__PURE__ */ new Set();
9680
- let g = groupId;
9681
- while (g && d.groups.has(g) && !seen.has(g)) {
9682
- seen.add(g);
9683
- path.push(g);
9684
- g = d.groups.get(g).parent;
9685
- }
9686
- return path.reverse();
9687
- }
9688
10080
  /** mean relative position of each group across all layers: its place among its siblings */
9689
10081
  function groupRanks(layers) {
9690
10082
  const sum = /* @__PURE__ */ new Map();
@@ -9707,7 +10099,7 @@ function groupRanks(layers) {
9707
10099
  * in one go; pushing nodes layer by layer instead can make two bands shove
9708
10100
  * each other apart forever.
9709
10101
  */
9710
- function enforceGroupBands(layers, horiz, gap, sep) {
10102
+ function enforceGroupBands(layers, inset, gap, sep) {
9711
10103
  const all = layers.flat();
9712
10104
  const info = /* @__PURE__ */ new Map();
9713
10105
  for (const v of all) v.path.forEach((g, i) => {
@@ -9752,9 +10144,9 @@ function enforceGroupBands(layers, horiz, gap, sep) {
9752
10144
  let lo = 1e400;
9753
10145
  let hi = -1e400;
9754
10146
  for (const u of members) {
9755
- const k = u.path.length - u.path.indexOf(g);
9756
- const left = u.oExt / 2 + k * 18 + (horiz ? k * 26 : 0);
9757
- const right = u.oExt / 2 + k * 18;
10147
+ const within = u.path.slice(u.path.indexOf(g));
10148
+ const left = u.oExt / 2 + within.reduce((t, h) => t + inset.low(h), 0);
10149
+ const right = u.oExt / 2 + within.reduce((t, h) => t + inset.high(h), 0);
9758
10150
  add(L(g), u.id, left);
9759
10151
  add(u.id, R(g), right);
9760
10152
  lo = Math.min(lo, u.pos - left);
@@ -9930,7 +10322,23 @@ function place(d, opts) {
9930
10322
  rankEdges.unshift(...placed.edges);
9931
10323
  breakCycles(ids, rankEdges);
9932
10324
  const rank = assignRanks(ids, rankEdges);
9933
- const sameRank = [...d.layout.ranks ?? [], ...placed.same].map((set) => set.filter((id) => nodes.has(id))).filter((set) => set.length > 1);
10325
+ const orderSides = horiz ? [
10326
+ "center",
10327
+ "top",
10328
+ "bottom"
10329
+ ] : [
10330
+ "center",
10331
+ "left",
10332
+ "right"
10333
+ ];
10334
+ const orderAligns = [];
10335
+ const rankAligns = [];
10336
+ for (const a of alignSets(d)) (orderSides.includes(a.side) ? orderAligns : rankAligns).push(a);
10337
+ const sameRank = [
10338
+ ...d.layout.ranks ?? [],
10339
+ ...placed.same,
10340
+ ...rankAligns.filter((a) => !a.within).map((a) => a.ids)
10341
+ ].map((set) => set.filter((id) => nodes.has(id))).filter((set) => set.length > 1);
9934
10342
  if (sameRank.length) alignRanks(rank, rankEdges, sameRank);
9935
10343
  const ranked = new Set(rankEdges.flatMap((e) => [e.from, e.to]));
9936
10344
  const attach = /* @__PURE__ */ new Map();
@@ -10029,14 +10437,23 @@ function place(d, opts) {
10029
10437
  preds.get(v).push([u, w]);
10030
10438
  }
10031
10439
  orderLayers(layers, preds, succs, attach);
10440
+ const pads = new Map([...d.groups.keys()].map((g) => [g, groupPadding(d, g, 18)]));
10441
+ const gaps = new Map([...d.groups.keys()].map((g) => [g, groupGap(d, g, gap)]));
10442
+ const padOn = (g, side) => pads.get(g)[side] + (side === "top" ? 26 : 0);
10443
+ const inset = {
10444
+ low: (g) => padOn(g, horiz ? "top" : "left"),
10445
+ high: (g) => padOn(g, horiz ? "bottom" : "right"),
10446
+ start: (g) => padOn(g, OPPOSITE[FORWARD[dir]]),
10447
+ end: (g) => padOn(g, FORWARD[dir])
10448
+ };
10449
+ const sum = (gs, f) => gs.reduce((t, g) => t + f(g), 0);
10032
10450
  const sepOf = (a, b) => {
10033
- let c = 0;
10034
- while (c < a.path.length && c < b.path.length && a.path[c] === b.path[c]) c++;
10035
- const leave = a.path.length - c;
10036
- const enter = b.path.length - c;
10037
- let extra = (leave + enter) * 18 + (leave + enter > 0 ? 10 : 0);
10038
- if (horiz) extra += enter * 26;
10039
- const g = !a.real && !b.real ? 14 : !a.real || !b.real ? gap * .6 : gap;
10451
+ const c = commonDepth(a.path, b.path);
10452
+ const leave = a.path.slice(c);
10453
+ const enter = b.path.slice(c);
10454
+ const extra = sum(leave, inset.high) + sum(enter, inset.low) + (leave.length + enter.length > 0 ? 10 : 0);
10455
+ const base = c ? gaps.get(a.path[c - 1]) : gap;
10456
+ const g = !a.real && !b.real ? 14 : !a.real || !b.real ? base * .6 : base;
10040
10457
  return (a.oExt + b.oExt) / 2 + g + extra;
10041
10458
  };
10042
10459
  for (const layer of layers) {
@@ -10064,8 +10481,7 @@ function place(d, opts) {
10064
10481
  const layerSeps = layers.map((layer) => layer.slice(1).map((v, i) => sepOf(layer[i], v)));
10065
10482
  const reversed = layers.map((_, i) => layers.length - 1 - i);
10066
10483
  const forward = layers.map((_, i) => i);
10067
- for (let iter = 0; iter < 16; iter++) {
10068
- const mode = iter < 12 ? iter % 2 === 0 ? "down" : "up" : "both";
10484
+ const sweep = (mode, pinned) => {
10069
10485
  const nbs = mode === "down" ? predNb : mode === "up" ? succNb : bothNb;
10070
10486
  for (const r of mode === "up" ? reversed : forward) {
10071
10487
  const layer = layers[r];
@@ -10073,6 +10489,11 @@ function place(d, opts) {
10073
10489
  const desired = [];
10074
10490
  const weights = [];
10075
10491
  for (const v of layer) {
10492
+ if (pinned?.has(v)) {
10493
+ desired.push(v.pos);
10494
+ weights.push(1e3);
10495
+ continue;
10496
+ }
10076
10497
  let sw = 0;
10077
10498
  let s = 0;
10078
10499
  for (const [u, w] of nbs[v.u]) {
@@ -10085,8 +10506,11 @@ function place(d, opts) {
10085
10506
  const solved = isotonic(desired, weights, layerSeps[r]);
10086
10507
  layer.forEach((v, i) => v.pos = solved[i]);
10087
10508
  }
10088
- }
10089
- enforceGroupBands(layers, horiz, gap, sepOf);
10509
+ };
10510
+ for (let iter = 0; iter < 12; iter++) sweep(iter % 2 === 0 ? "down" : "up");
10511
+ const aligned = alignOrder(orderAligns, vnodes);
10512
+ for (let iter = 0; iter < 4; iter++) sweep("both", aligned);
10513
+ enforceGroupBands(layers, inset, gap, sepOf);
10090
10514
  const thick = layers.map((l) => Math.max(0, ...l.filter((v) => v.real).map((v) => v.rExt)));
10091
10515
  const labelRoom = new Array(layers.length).fill(0);
10092
10516
  d.edges.forEach((e) => {
@@ -10098,11 +10522,43 @@ function place(d, opts) {
10098
10522
  const ext = horiz ? Math.max(...lines.map((l) => richW(l, font.size, font.weight, font.mono, measure))) + 16 : lines.length * lineHeight(lines, font.size) + 10;
10099
10523
  labelRoom[r] = Math.max(labelRoom[r], ext);
10100
10524
  });
10525
+ const rankSpan = /* @__PURE__ */ new Map();
10526
+ for (const v of vnodes.values()) for (const g of v.path) {
10527
+ const sp = rankSpan.get(g);
10528
+ rankSpan.set(g, sp ? [Math.min(sp[0], v.rank), Math.max(sp[1], v.rank)] : [v.rank, v.rank]);
10529
+ }
10530
+ const closing = new Array(layers.length).fill(0);
10531
+ const opening = new Array(layers.length).fill(0);
10532
+ for (const v of vnodes.values()) {
10533
+ const ends = sum(v.path.filter((g) => rankSpan.get(g)[1] === v.rank), inset.end);
10534
+ const starts = sum(v.path.filter((g) => rankSpan.get(g)[0] === v.rank), inset.start);
10535
+ closing[v.rank] = Math.max(closing[v.rank], ends);
10536
+ opening[v.rank] = Math.max(opening[v.rank], starts);
10537
+ }
10538
+ const crossing = new Array(layers.length).fill(-1);
10539
+ const anyEdge = new Array(layers.length).fill(false);
10540
+ const ownGap = new Map([...d.groups.keys()].map((g) => [g, groupGap(d, g, NaN)]));
10541
+ for (const e of d.edges) {
10542
+ const [a, b] = [rank.get(e.from), rank.get(e.to)];
10543
+ if (a === void 0 || b === void 0 || a === b) continue;
10544
+ const pa = vnodes.get(e.from).path;
10545
+ const c = commonDepth(pa, vnodes.get(e.to).path);
10546
+ const own = c ? ownGap.get(pa[c - 1]) : NaN;
10547
+ const want = Number.isNaN(own) ? baseRankGap : Math.max(own, 20);
10548
+ for (let r = Math.min(a, b); r < Math.max(a, b); r++) {
10549
+ crossing[r] = Math.max(crossing[r], want);
10550
+ anyEdge[r] ||= Number.isNaN(own);
10551
+ }
10552
+ }
10101
10553
  const rankPos = [];
10102
10554
  let acc = 0;
10103
10555
  for (let r = 0; r < layers.length; r++) {
10104
10556
  rankPos.push(acc + thick[r] / 2);
10105
- acc += thick[r] + Math.max(baseRankGap, labelRoom[r] + 44);
10557
+ const walls = r + 1 < layers.length ? closing[r] + opening[r + 1] : 0;
10558
+ const frames = walls ? walls + 24 : 0;
10559
+ const own = crossing[r] >= 0 && !anyEdge[r];
10560
+ const base = crossing[r] < 0 ? baseRankGap : crossing[r];
10561
+ acc += thick[r] + Math.max(base, labelRoom[r] + (own ? 20 : 44), frames);
10106
10562
  }
10107
10563
  const toXY = (rankC, orderC) => {
10108
10564
  switch (dir) {
@@ -10131,21 +10587,22 @@ function place(d, opts) {
10131
10587
  n.x = c.x - n.w / 2;
10132
10588
  n.y = c.y - n.h / 2;
10133
10589
  }
10590
+ alignInRank(rankAligns, nodes, rank);
10134
10591
  const dummyPts = /* @__PURE__ */ new Map();
10135
10592
  for (const [idx, chain] of chains) dummyPts.set(idx, chain.map((id) => {
10136
10593
  const v = vnodes.get(id);
10137
10594
  return toXY(rankPos[v.rank], v.pos);
10138
10595
  }));
10139
- const maxDepth = Math.max(0, ...[...vnodes.values()].map((v) => v.path.length));
10596
+ const frames = new Map([...nodes.values()].map((n) => [n.id, frameOf(pads, vnodes.get(n.id).path)]));
10140
10597
  let minX = 1e400;
10141
10598
  let minY = 1e400;
10142
10599
  for (const n of nodes.values()) {
10143
- minX = Math.min(minX, n.x);
10144
- minY = Math.min(minY, n.y);
10600
+ minX = Math.min(minX, n.x - frames.get(n.id).left);
10601
+ minY = Math.min(minY, n.y - frames.get(n.id).top);
10145
10602
  }
10146
10603
  if (nodes.size) {
10147
- const ox = maxDepth * 18 - minX;
10148
- const oy = maxDepth * 44 - minY;
10604
+ const ox = -minX;
10605
+ const oy = -minY;
10149
10606
  for (const n of nodes.values()) {
10150
10607
  n.x = Math.round(n.x + ox);
10151
10608
  n.y = Math.round(n.y + oy);
@@ -10164,11 +10621,13 @@ function place(d, opts) {
10164
10621
  wrapRows(d, nodes, rank, dummyPts, dummyRank, channels, {
10165
10622
  maxW,
10166
10623
  dir,
10167
- rankGap: baseRankGap
10624
+ rankGap: baseRankGap,
10625
+ frames
10168
10626
  });
10169
10627
  }
10170
10628
  applyOverrides(d, nodes, horiz, gap);
10171
10629
  const anyPinned = [...nodes.values()].some((n) => n.pinned);
10630
+ if (anyPinned) keepGroupLayout(d, nodes, rank, horiz, orderAligns, rankAligns);
10172
10631
  const groups = layoutGroups(d, nodes, theme, measure);
10173
10632
  const dummyVia = /* @__PURE__ */ new Map();
10174
10633
  if (!anyPinned) d.edges.forEach((e, idx) => dummyPts.has(idx) && dummyVia.set(e.key, dummyPts.get(idx)));
@@ -10242,12 +10701,12 @@ function wrapRows(d, nodes, rank, dummyPts, dummyRank, channels, o) {
10242
10701
  const maxRank = Math.max(...[...nodes.keys()].map((id) => rank.get(id)));
10243
10702
  const outer = /* @__PURE__ */ new Map();
10244
10703
  for (const n of nodes.values()) {
10245
- const k = groupPath(d, n.decl.group).length;
10704
+ const fr = o.frames.get(n.id);
10246
10705
  outer.set(n.id, {
10247
- x: n.x - k * 18,
10248
- y: n.y - k * 44,
10249
- w: n.w + 2 * k * 18,
10250
- h: n.h + k * 62
10706
+ x: n.x - fr.left,
10707
+ y: n.y - fr.top,
10708
+ w: n.w + fr.left + fr.right,
10709
+ h: n.h + fr.top + fr.bottom
10251
10710
  });
10252
10711
  }
10253
10712
  const joined = new Array(maxRank + 1).fill(false);
@@ -10474,6 +10933,61 @@ function alignRanks(rank, edges, sets) {
10474
10933
  if (!changed) return;
10475
10934
  }
10476
10935
  }
10936
+ /**
10937
+ * `align` along the order axis: each aligned node or group moves as one block
10938
+ * so its edge (or centre) sits on the mean line of its set. A group's own
10939
+ * members (`within`) that share a rank move together as one row, so each row
10940
+ * lines up like a line of text. Returns the moved vnodes, which stay pinned
10941
+ * while the rest of the layout follows them.
10942
+ */
10943
+ function alignOrder(aligns, vnodes) {
10944
+ const moved = /* @__PURE__ */ new Set();
10945
+ const inGroup = /* @__PURE__ */ new Map();
10946
+ if (aligns.length) for (const v of vnodes.values()) for (const g of v.path) inGroup.get(g)?.push(v) ?? inGroup.set(g, [v]);
10947
+ for (const { side, ids, within } of aligns) {
10948
+ let units = ids.map((id) => vnodes.has(id) ? [vnodes.get(id)] : inGroup.get(id) ?? []).filter((u) => u.some((v) => v.real));
10949
+ if (within) units = rows(units);
10950
+ if (units.length < 2) continue;
10951
+ const anchor = (u) => {
10952
+ const real = u.filter((v) => v.real);
10953
+ const lo = Math.min(...real.map((v) => v.pos - v.oExt / 2));
10954
+ const hi = Math.max(...real.map((v) => v.pos + v.oExt / 2));
10955
+ return side === "left" || side === "top" ? lo : side === "right" || side === "bottom" ? hi : (lo + hi) / 2;
10956
+ };
10957
+ const anchors = units.map(anchor);
10958
+ const line = anchors.reduce((a, b) => a + b, 0) / anchors.length;
10959
+ units.forEach((u, i) => {
10960
+ for (const v of u) {
10961
+ v.pos += line - anchors[i];
10962
+ moved.add(v);
10963
+ }
10964
+ });
10965
+ }
10966
+ return moved;
10967
+ }
10968
+ /** units that share a rank, joined into one: the rows of a group's members */
10969
+ function rows(units) {
10970
+ const out = [];
10971
+ for (const u of units) {
10972
+ const ranks = new Set(u.filter((v) => v.real).map((v) => v.rank));
10973
+ const hit = out.filter((o) => o.some((v) => v.real && ranks.has(v.rank)));
10974
+ out.splice(0, out.length, ...out.filter((o) => !hit.includes(o)), [...hit.flat(), ...u]);
10975
+ }
10976
+ return out;
10977
+ }
10978
+ /** `align top a b` in a down diagram: the nodes of one rank share their top edge */
10979
+ function alignInRank(aligns, nodes, rank) {
10980
+ for (const { side, ids } of aligns) {
10981
+ const [key, len] = side === "top" || side === "bottom" ? ["y", "h"] : ["x", "w"];
10982
+ for (const ns of nodesByRank(ids, nodes, rank)) if (side === "top" || side === "left") {
10983
+ const lo = Math.min(...ns.map((n) => n[key]));
10984
+ ns.forEach((n) => n[key] = lo);
10985
+ } else {
10986
+ const hi = Math.max(...ns.map((n) => n[key] + n[len]));
10987
+ ns.forEach((n) => n[key] = hi - n[len]);
10988
+ }
10989
+ }
10990
+ }
10477
10991
  const even = (n, min = 0) => Math.ceil(Math.max(n, min) / 2) * 2;
10478
10992
  function breakCycles(ids, edges) {
10479
10993
  const out = /* @__PURE__ */ new Map();
@@ -10760,6 +11274,32 @@ function applyOverrides(d, nodes, horiz, gap) {
10760
11274
  }
10761
11275
  }
10762
11276
  /**
11277
+ * Pinned nodes (`layout { a at x y }`, written by editor drags) still follow
11278
+ * their group's `gap` and the `align` props: members of a rank restack along
11279
+ * the order axis with the group gap, then the align sets snap again. The
11280
+ * first member of each rank keeps its pinned place.
11281
+ */
11282
+ function keepGroupLayout(d, nodes, rank, horiz, orderAligns, rankAligns) {
11283
+ const [pos, len] = horiz ? ["y", "h"] : ["x", "w"];
11284
+ const byRank = (ids) => nodesByRank(ids, nodes, rank).map((ns) => ns.sort((a, b) => a[pos] - b[pos]));
11285
+ const members = /* @__PURE__ */ new Map();
11286
+ for (const n of nodes.values()) if (n.decl.group) members.get(n.decl.group)?.push(n.id) ?? members.set(n.decl.group, [n.id]);
11287
+ for (const [gid, ids] of members) {
11288
+ if (!ids.some((id) => nodes.get(id).pinned)) continue;
11289
+ const gap = groupGap(d, gid, NaN);
11290
+ if (Number.isNaN(gap)) continue;
11291
+ for (const ns of byRank(ids)) ns.forEach((n, i) => i && (n[pos] = ns[i - 1][pos] + ns[i - 1][len] + gap));
11292
+ }
11293
+ for (const { side, ids, within } of orderAligns) {
11294
+ const sets = within ? byRank(ids) : ids.flatMap((id) => nodes.has(id) ? [[nodes.get(id)]] : []);
11295
+ const at = (ns) => side === "center" ? ns[0][pos] + ns[0][len] / 2 : side === "top" || side === "left" ? ns[0][pos] : ns[0][pos] + ns[0][len];
11296
+ const ats = sets.map(at);
11297
+ const target = side === "center" ? ats.reduce((a, b) => a + b, 0) / ats.length : side === "top" || side === "left" ? Math.min(...ats) : Math.max(...ats);
11298
+ sets.forEach((ns, i) => ns.forEach((n) => n[pos] += target - ats[i]));
11299
+ }
11300
+ alignInRank(rankAligns, nodes, rank);
11301
+ }
11302
+ /**
10763
11303
  * The header text of a group: its label (bold, left) and `meta` (right) on
10764
11304
  * the first line. Font sizes match `renderGroup`.
10765
11305
  */
@@ -10805,9 +11345,11 @@ function fitGroupHead(d, gid, box, nodes, laid, theme, measure) {
10805
11345
  const others = [];
10806
11346
  const own = groupPath(d, gid);
10807
11347
  const padded = (b, path, self) => {
10808
- let c = 0;
10809
- while (c < path.length && path[c] === own[c]) c++;
10810
- const pad = (path.length - self - c + own.length - 1 - c) * 18;
11348
+ const c = commonDepth(path, own);
11349
+ const pad = [...path.slice(c, path.length - self), ...own.slice(c, own.length - 1)].reduce((t, l) => {
11350
+ const p = groupPadding(d, l, 18);
11351
+ return t + Math.max(p.top, p.right, p.bottom, p.left);
11352
+ }, 0);
10811
11353
  others.push({
10812
11354
  x: b.x - pad,
10813
11355
  y: b.y - pad,
@@ -10815,7 +11357,10 @@ function fitGroupHead(d, gid, box, nodes, laid, theme, measure) {
10815
11357
  h: b.h + 2 * pad
10816
11358
  });
10817
11359
  };
10818
- for (const n of nodes.values()) if (!inside(n.decl.group)) padded(n, groupPath(d, n.decl.group), 0);
11360
+ for (const n of nodes.values()) {
11361
+ const path = groupPath(d, n.decl.group);
11362
+ if (!path.includes(gid)) padded(n, path, 0);
11363
+ }
10819
11364
  for (const [cid, b] of laid) if (!inside(cid) && !own.includes(cid)) padded(b, groupPath(d, cid), 1);
10820
11365
  const m = 9;
10821
11366
  let lo = -1e400;
@@ -10853,10 +11398,11 @@ function layoutGroups(d, nodes, theme, measure) {
10853
11398
  for (const n of nodes.values()) if (n.decl.group === gid) members.push(n);
10854
11399
  for (const [cid, b] of boxes) if (d.groups.get(cid).parent === gid) members.push(b);
10855
11400
  if (!members.length) continue;
10856
- const x0 = Math.min(...members.map((b) => b.x)) - 18;
10857
- const y0 = Math.min(...members.map((b) => b.y)) - 18 - 26;
10858
- const x1 = Math.max(...members.map((b) => b.x + b.w)) + 18;
10859
- const y1 = Math.max(...members.map((b) => b.y + b.h)) + 18;
11401
+ const pad = groupPadding(d, gid, 18);
11402
+ const x0 = Math.min(...members.map((b) => b.x)) - pad.left;
11403
+ const y0 = Math.min(...members.map((b) => b.y)) - pad.top - 26;
11404
+ const x1 = Math.max(...members.map((b) => b.x + b.w)) + pad.right;
11405
+ const y1 = Math.max(...members.map((b) => b.y + b.h)) + pad.bottom;
10860
11406
  const box = {
10861
11407
  x: x0,
10862
11408
  y: y0,
@@ -14135,5 +14681,5 @@ function onTypeEdits(text, pos, ch) {
14135
14681
  }
14136
14682
  // src/version.ts
14137
14683
  /** the package version, inlined by the build */
14138
- const VERSION = "1.5.2";
14139
- export { BOOLS as A, SHAPES$2 as B, blockBraces as C, parse as D, lex as E, NODE_PROPS as F, THEME_KEYS as G, esc as H, NUMBERS as I, isSafeCss as J, THEME_NAMES as K, PANEL_PROPS as L, EDGE_PROPS as M, ENUMS as N, serializeProps as O, GROUP_PROPS as P, PANEL_PROP_HELP as R, SETTING_VALUES as S, joinPath$1 as T, parseSpanStyle as U, SPAN_KEYS as V, spanKey as W, table as X, themeNames as Y, canvasMeasure as _, formatPage as a, KNOWN_SETTINGS as b, diskResolve as c, MD_LANGUAGES as d, findBlocks as f, replaceBlockSource as g, processMarkdown as h, format as i, COLORS as j, unquote as k, render as l, pageBlocks as m, applyChange as n, onTypeEdits as o, isMarkdown as p, TONES as q, diffEdits as r, tidy as s, VERSION as t, toSource as u, optionKind as v, isYuhwaPath as w, PANELS as x, registry as y, PLACEMENT_PROPS as z };
14684
+ const VERSION = "1.6.0";
14685
+ export { themeNames as $, unquote as A, PLACEMENT_PROPS as B, SETTING_VALUES as C, lex as D, joinPath$1 as E, GROUP_PROPS as F, SPAN_KEYS as G, ALIGN_SIDES as H, NODE_PROPS as I, spanKey as J, esc as K, NUMBERS as L, COLORS as M, EDGE_PROPS as N, parse as O, ENUMS as P, isSafeCss as Q, PANEL_PROPS as R, SETTING_HELP as S, isYuhwaPath as T, ALIGN_SIDES_HINT as U, SHAPES$2 as V, listsIds as W, THEME_NAMES as X, THEME_KEYS as Y, TONES as Z, canvasMeasure as _, formatPage as a, KNOWN_SETTINGS as b, diskResolve as c, MD_LANGUAGES as d, table as et, findBlocks as f, replaceBlockSource as g, processMarkdown as h, format as i, BOOLS as j, serializeProps as k, render as l, pageBlocks as m, applyChange as n, onTypeEdits as o, isMarkdown as p, parseSpanStyle as q, diffEdits as r, tidy as s, VERSION as t, toSource as u, optionKind as v, blockBraces as w, PANELS as x, registry as y, PANEL_PROP_HELP as z };