spatial-views 0.0.0-stage → 0.2.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 (220) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +245 -2
  3. package/dist/app/assets/abnfDiagram-S7VZAUFO-CFbb_Sz2.js +1 -0
  4. package/dist/app/assets/architecture-WOLXFQ4H-CsBD__5a.js +1 -0
  5. package/dist/app/assets/architectureDiagram-YGSQEFNW-C0ic4ib1.js +36 -0
  6. package/dist/app/assets/blockDiagram-APKKGZFW-BZDPbzAG.js +140 -0
  7. package/dist/app/assets/bpmnIcons-APHXDYXF-C0jbCElo.js +1 -0
  8. package/dist/app/assets/c4Diagram-KJ2TOQU5-BilBWuq5.js +38 -0
  9. package/dist/app/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
  10. package/dist/app/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
  11. package/dist/app/assets/chunk-6MUBBAK2-BBl-4tQr.js +2 -0
  12. package/dist/app/assets/chunk-AXFCVDMN-BhsttQ-k.js +62 -0
  13. package/dist/app/assets/chunk-BG4553SI-mWcPcFZr.js +127 -0
  14. package/dist/app/assets/chunk-DQASFODM-7FKe2gLt.js +10 -0
  15. package/dist/app/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
  16. package/dist/app/assets/chunk-HFQBQIA3-nXrQ08BL.js +1 -0
  17. package/dist/app/assets/chunk-J5ZVWO5B-oDCd-gWO.js +1 -0
  18. package/dist/app/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
  19. package/dist/app/assets/chunk-KQLB2F66-swukhFmZ.js +2 -0
  20. package/dist/app/assets/chunk-KS47KZWR-DkDkwO82.js +1 -0
  21. package/dist/app/assets/chunk-LEVMRMFU-DXH6iXv3.js +1 -0
  22. package/dist/app/assets/chunk-M57EVK23-B-kEyOJF.js +1 -0
  23. package/dist/app/assets/chunk-NGNAAXSQ-DI_UYJZ4.js +136 -0
  24. package/dist/app/assets/chunk-OY52VR5R-DweG7qYw.js +88 -0
  25. package/dist/app/assets/chunk-QB3YPZ4I-DyozK3lV.js +213 -0
  26. package/dist/app/assets/chunk-R3KSYZAL-BEXCUN6J.js +1 -0
  27. package/dist/app/assets/chunk-STOXU5M4-BaTlty4O.js +72 -0
  28. package/dist/app/assets/chunk-TU62E6Q5-_rcUP1Fh.js +1 -0
  29. package/dist/app/assets/chunk-X3CZISLH-NOpCnvgE.js +1 -0
  30. package/dist/app/assets/chunk-X4PVCEUW-DGxTL3kB.js +1 -0
  31. package/dist/app/assets/chunk-XXDRQBXY-C02Gpej2.js +1 -0
  32. package/dist/app/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
  33. package/dist/app/assets/classDiagram-v2-L25IHNXF-DLEBwpvk.js +217 -0
  34. package/dist/app/assets/cose-bilkent-JH36ORCC-kNCXOm8k.js +1 -0
  35. package/dist/app/assets/cynefin-EF2NZ3EQ-6MWNLbuF.js +1 -0
  36. package/dist/app/assets/cynefinDiagram-L36ESFBY-CLFstukD.js +62 -0
  37. package/dist/app/assets/cytoscape.esm-nTrd5EVq.js +321 -0
  38. package/dist/app/assets/d3-scale-CaJQQYkF.js +1 -0
  39. package/dist/app/assets/dagre-SPEB5RXH-BaSAny1T.js +4 -0
  40. package/dist/app/assets/diagram-3B7GL73J-DJNlt5Sg.js +200 -0
  41. package/dist/app/assets/diagram-DNEMPZML-DEHeuILB.js +24 -0
  42. package/dist/app/assets/diagram-L3MUGCDN-ob5Ka2Xc.js +41 -0
  43. package/dist/app/assets/diagram-LA4NIPWU-DNESzFAc.js +24 -0
  44. package/dist/app/assets/diagram-MHCOC4E2-CVi9zKjG.js +3 -0
  45. package/dist/app/assets/diagram-XPFHEONP-DInT4C7e.js +30 -0
  46. package/dist/app/assets/dist-CjvHN74u.js +1 -0
  47. package/dist/app/assets/ebnfDiagram-UZSYDL2S-CRCZNsji.js +1 -0
  48. package/dist/app/assets/elk-XV6XMXQE-D4fsWI2l.js +2 -0
  49. package/dist/app/assets/erDiagram-EYBAPDEH-YQNM5StV.js +99 -0
  50. package/dist/app/assets/eventmodeling-K75KTNOO-Bev08J2F.js +1 -0
  51. package/dist/app/assets/flowDiagram-MALGAFMO-BhJ554DH.js +1 -0
  52. package/dist/app/assets/ganttDiagram-23REXITY-CkaBMYM4.js +292 -0
  53. package/dist/app/assets/gitGraph-VSP46ZUC-CtbZ0sIS.js +1 -0
  54. package/dist/app/assets/gitGraphDiagram-STJD7YCW-D15ZdQ-Z.js +106 -0
  55. package/dist/app/assets/graphlib-DS17s2tU.js +1 -0
  56. package/dist/app/assets/index-Bn6_wHEp.js +98 -0
  57. package/dist/app/assets/index-H-Z7MFhZ.css +1 -0
  58. package/dist/app/assets/info-OHQRW6UA-CfUW1XFR.js +1 -0
  59. package/dist/app/assets/infoDiagram-KBXO4MBG-DT7LbBRQ.js +2 -0
  60. package/dist/app/assets/ishikawaDiagram-M35NCXHK-CF51lhjB.js +70 -0
  61. package/dist/app/assets/journeyDiagram-OPLOXDXO-_36FonKk.js +139 -0
  62. package/dist/app/assets/kanban-definition-BZKZHSQY-tLk_OlQ-.js +89 -0
  63. package/dist/app/assets/katex-MEuSR36T.js +257 -0
  64. package/dist/app/assets/mermaid-parser.core-_3Vo8x6b.js +7 -0
  65. package/dist/app/assets/mermaid.core-y-x1CYxC.js +44 -0
  66. package/dist/app/assets/mindmap-definition-I2QAS2VK-BCI4HRX3.js +98 -0
  67. package/dist/app/assets/packet-JDAUHWVQ-Cco3UoaI.js +1 -0
  68. package/dist/app/assets/pegDiagram-FD4XPPHP-BOzSoNji.js +1 -0
  69. package/dist/app/assets/pie-XZMESJXO-reOB2GXn.js +1 -0
  70. package/dist/app/assets/pieDiagram-J4AKIIQI-PlHpwT_c.js +39 -0
  71. package/dist/app/assets/quadrantDiagram-XTYBLID2-CdxZ0ael.js +7 -0
  72. package/dist/app/assets/radar-ABXABTNO-DfCl8uV2.js +1 -0
  73. package/dist/app/assets/railroad-I3PHUGI6-4s14UyW1.js +1 -0
  74. package/dist/app/assets/railroad-abnf-LNEFI6M7-C49jJE4Y.js +1 -0
  75. package/dist/app/assets/railroad-ebnf-DWYD2UWJ-Bx-pcPc7.js +1 -0
  76. package/dist/app/assets/railroad-peg-JR7BVNR7-DjBPSCKV.js +1 -0
  77. package/dist/app/assets/railroadDiagram-MRQ6NCDL-wGcmIMqq.js +1 -0
  78. package/dist/app/assets/requirementDiagram-Y7LWUY3S-DprFI03z.js +84 -0
  79. package/dist/app/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  80. package/dist/app/assets/rough.esm-Dy-Kn_BL.js +1 -0
  81. package/dist/app/assets/sankeyDiagram-WGJFYUXA-DygYWLTR.js +40 -0
  82. package/dist/app/assets/sequenceDiagram-5NDRBG5M-BzIr2DBO.js +169 -0
  83. package/dist/app/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
  84. package/dist/app/assets/stateDiagram-v2-FJ4FJNEA-MGCqKnm_.js +291 -0
  85. package/dist/app/assets/swimlanes-UMQ2AFYF-CIEH82Zt.js +1 -0
  86. package/dist/app/assets/swimlanesDiagram-OMPLPG4O-YWNJjl0g.js +8 -0
  87. package/dist/app/assets/timeline-definition-SEUHKTQE-BGYSqup2.js +120 -0
  88. package/dist/app/assets/treeView-D4JQ5SDB-B39o1sJo.js +1 -0
  89. package/dist/app/assets/treemap-SAJKECNS-BoGxrUH4.js +1 -0
  90. package/dist/app/assets/usecaseDiagram-AFA6YXN4-dy6oVOke.js +328 -0
  91. package/dist/app/assets/vennDiagram-NNMJR3RB-CUyuhcAk.js +34 -0
  92. package/dist/app/assets/wardley-7MLQ67FV-sYPAvQ06.js +1 -0
  93. package/dist/app/assets/wardleyDiagram-S7YFUSEH-DYtxYUHe.js +78 -0
  94. package/dist/app/assets/xychartDiagram-GGNKY7IX-ClR06AlB.js +7 -0
  95. package/dist/app/index.html +14 -0
  96. package/dist/cli/index.js +338 -0
  97. package/dist/core/api.js +420 -0
  98. package/dist/core/approvals.js +426 -0
  99. package/dist/core/context.js +259 -0
  100. package/dist/core/diff.js +156 -0
  101. package/dist/core/docs.js +140 -0
  102. package/dist/core/doctor.js +152 -0
  103. package/dist/core/doctypes.js +79 -0
  104. package/dist/core/edit.js +130 -0
  105. package/dist/core/errors.js +3 -0
  106. package/dist/core/flags.js +13 -0
  107. package/dist/core/gaps.js +119 -0
  108. package/dist/core/heading.js +4 -0
  109. package/dist/core/history.js +50 -0
  110. package/dist/core/home.js +345 -0
  111. package/dist/core/lanes.js +134 -0
  112. package/dist/core/launch.js +34 -0
  113. package/dist/core/layout.js +362 -0
  114. package/dist/core/links.js +68 -0
  115. package/dist/core/mermaid.js +175 -0
  116. package/dist/core/newdoc.js +66 -0
  117. package/dist/core/open-folder.js +30 -0
  118. package/dist/core/openspec.js +120 -0
  119. package/dist/core/paths.js +90 -0
  120. package/dist/core/project.js +392 -0
  121. package/dist/core/resolve.js +35 -0
  122. package/dist/core/route.js +420 -0
  123. package/dist/core/scan.js +514 -0
  124. package/dist/core/schema.js +270 -0
  125. package/dist/core/scope.js +126 -0
  126. package/dist/core/setup.js +134 -0
  127. package/dist/core/skills.js +181 -0
  128. package/dist/core/storage/config.js +74 -0
  129. package/dist/core/storage/events.js +54 -0
  130. package/dist/core/storage/folder-watch.js +147 -0
  131. package/dist/core/storage/folder.js +469 -0
  132. package/dist/core/storage/index.js +8 -0
  133. package/dist/core/storage/local.js +22 -0
  134. package/dist/core/storage/migrate.js +48 -0
  135. package/dist/core/storage/postgres.js +290 -0
  136. package/dist/core/storage/types.js +101 -0
  137. package/dist/core/storage.js +589 -0
  138. package/dist/core/suggest.js +283 -0
  139. package/dist/core/tree.js +254 -0
  140. package/dist/core/types.js +583 -0
  141. package/dist/core/version.js +1 -0
  142. package/dist/core/versions.js +245 -0
  143. package/dist/core/watch.js +2 -0
  144. package/dist/core/xml.js +68 -0
  145. package/dist/demo/args.js +58 -0
  146. package/dist/demo/browser.js +301 -0
  147. package/dist/demo/platform.js +147 -0
  148. package/dist/demo/run.js +198 -0
  149. package/dist/demo/steps.js +328 -0
  150. package/dist/mcp/project-tools.js +193 -0
  151. package/dist/mcp/server.js +785 -0
  152. package/examples/platform/README.md +33 -0
  153. package/examples/platform/api/package.json +8 -0
  154. package/examples/platform/api/src/auth-middleware.js +16 -0
  155. package/examples/platform/api/src/proxy.js +7 -0
  156. package/examples/platform/api/src/rate-limit.js +13 -0
  157. package/examples/platform/api/src/server.js +40 -0
  158. package/examples/platform/auth/package.json +9 -0
  159. package/examples/platform/auth/src/server.js +24 -0
  160. package/examples/platform/auth/src/tokens.js +19 -0
  161. package/examples/platform/auth/src/users.js +16 -0
  162. package/examples/platform/docs/api/PRD.md +10 -0
  163. package/examples/platform/docs/api/SDD.md +15 -0
  164. package/examples/platform/docs/auth/PRD.md +10 -0
  165. package/examples/platform/docs/auth/SDD.md +8 -0
  166. package/examples/platform/docs/frontend/PRD.md +18 -0
  167. package/examples/platform/docs/frontend/SDD.md +13 -0
  168. package/examples/platform/docs/pricing/PRD.md +9 -0
  169. package/examples/platform/docs/pricing/SDD.md +8 -0
  170. package/examples/platform/docs/products/PRD.md +9 -0
  171. package/examples/platform/docs/products/SDD.md +8 -0
  172. package/examples/platform/frontend/package.json +8 -0
  173. package/examples/platform/frontend/src/api-client.js +21 -0
  174. package/examples/platform/frontend/src/main.js +21 -0
  175. package/examples/platform/frontend/src/pages/products.js +12 -0
  176. package/examples/platform/frontend/src/pages/sign-in.js +14 -0
  177. package/examples/platform/frontend/src/session.js +11 -0
  178. package/examples/platform/pricing/package.json +9 -0
  179. package/examples/platform/pricing/src/cache.js +19 -0
  180. package/examples/platform/pricing/src/product-events.js +17 -0
  181. package/examples/platform/pricing/src/rules.js +19 -0
  182. package/examples/platform/pricing/src/server.js +32 -0
  183. package/examples/platform/products/package.json +9 -0
  184. package/examples/platform/products/src/catalog.js +20 -0
  185. package/examples/platform/products/src/events.js +11 -0
  186. package/examples/platform/products/src/search.js +9 -0
  187. package/examples/platform/products/src/server.js +31 -0
  188. package/migrations/postgres/001_init.sql +55 -0
  189. package/migrations/postgres/002_view_versions.sql +18 -0
  190. package/package.json +69 -4
  191. package/schema/approval.schema.json +173 -0
  192. package/schema/gap.schema.json +56 -0
  193. package/schema/pins.schema.json +122 -0
  194. package/schema/project.schema.json +93 -0
  195. package/schema/settings.schema.json +86 -0
  196. package/schema/toolbox.schema.json +206 -0
  197. package/schema/type.schema.json +121 -0
  198. package/schema/view.schema.json +240 -0
  199. package/skills/claude/build-gap/SKILL.md +28 -0
  200. package/skills/claude/check-ui/SKILL.md +40 -0
  201. package/skills/claude/draw-system/SKILL.md +38 -0
  202. package/skills/claude/gap-from-view/SKILL.md +29 -0
  203. package/skills/claude/post-audit/SKILL.md +58 -0
  204. package/skills/claude/pre-audit/SKILL.md +82 -0
  205. package/skills/claude/update-state/SKILL.md +56 -0
  206. package/skills/cursor/check-ui.mdc +40 -0
  207. package/skills/cursor/draw-system.mdc +38 -0
  208. package/skills/cursor/post-audit.mdc +44 -0
  209. package/skills/cursor/pre-audit.mdc +49 -0
  210. package/skills/cursor/update-state.mdc +45 -0
  211. package/skills/windsurf/spatial-views.md +212 -0
  212. package/toolboxes/aws.yaml +43 -0
  213. package/toolboxes/azure.yaml +41 -0
  214. package/toolboxes/cloud.yaml +95 -0
  215. package/toolboxes/docs.yaml +111 -0
  216. package/toolboxes/software.yaml +114 -0
  217. package/toolboxes/types/docs/gap.yaml +15 -0
  218. package/toolboxes/types/docs/mdd.yaml +19 -0
  219. package/toolboxes/types/docs/plain.yaml +7 -0
  220. package/toolboxes/types/docs/prd.yaml +17 -0
@@ -0,0 +1,420 @@
1
+ /**
2
+ * Orthogonal edge routing: straight segments with right-angle bends.
3
+ *
4
+ * Pure (no React), so the canvas, the Approvals picture and the tests share it.
5
+ * Each connection leaves its module on the side that faces the other end (all
6
+ * four sides), and several connections on one side are spread along it. A
7
+ * connection's `bends` are corner points the route passes through: people add
8
+ * them (double-click), drag them and delete them; with none, the route is a
9
+ * clean step between the two facing sides.
10
+ */
11
+ /** How far a route runs straight out of a side before it may turn. */
12
+ export const STUB = 18;
13
+ /** The size assumed for a module that has not been measured yet. */
14
+ export const DEFAULT_SIZE = { w: 160, h: 44 };
15
+ const NORMAL = { top: { x: 0, y: -1 }, right: { x: 1, y: 0 }, bottom: { x: 0, y: 1 }, left: { x: -1, y: 0 } };
16
+ const OPPOSITE = { top: "bottom", bottom: "top", left: "right", right: "left" };
17
+ const axisOf = (s) => (s === "left" || s === "right" ? "h" : "v");
18
+ const centre = (r) => ({ x: r.x + r.w / 2, y: r.y + r.h / 2 });
19
+ const near = (a, b) => Math.abs(a - b) < 0.5;
20
+ const same = (a, b) => near(a.x, b.x) && near(a.y, b.y);
21
+ /**
22
+ * The side of `a` that faces `b` (a module or a point): the axis with the
23
+ * wider gap wins, except that two modules in different rows (room to turn
24
+ * between them, and not far off to one side) join bottom to top, as
25
+ * architects draw a top-down picture.
26
+ */
27
+ export function facingSide(a, b) {
28
+ const br = "w" in b ? b : { x: b.x, y: b.y, w: 0, h: 0 };
29
+ const ca = centre(a);
30
+ const cb = centre(br);
31
+ const dx = cb.x - ca.x;
32
+ const dy = cb.y - ca.y;
33
+ const gapX = Math.abs(dx) - (a.w + br.w) / 2;
34
+ const gapY = Math.abs(dy) - (a.h + br.h) / 2;
35
+ if ("w" in b && gapY >= 2 * STUB && gapY * 3 >= gapX)
36
+ return dy >= 0 ? "bottom" : "top";
37
+ if (gapX >= gapY)
38
+ return dx >= 0 ? "right" : "left";
39
+ return dy >= 0 ? "bottom" : "top";
40
+ }
41
+ /** The point on a side, at t (0..1) along it (left to right, top to bottom). */
42
+ function portAt(r, side, t) {
43
+ switch (side) {
44
+ case "top":
45
+ return { x: r.x + r.w * t, y: r.y };
46
+ case "bottom":
47
+ return { x: r.x + r.w * t, y: r.y + r.h };
48
+ case "left":
49
+ return { x: r.x, y: r.y + r.h * t };
50
+ case "right":
51
+ return { x: r.x + r.w, y: r.y + r.h * t };
52
+ }
53
+ }
54
+ /** Drop repeated points and the middle of any three in a line. */
55
+ export function simplify(points) {
56
+ const out = [];
57
+ for (const p of points) {
58
+ const q = { x: Math.round(p.x * 10) / 10, y: Math.round(p.y * 10) / 10 };
59
+ if (out.length && same(out[out.length - 1], q))
60
+ continue;
61
+ if (out.length >= 2) {
62
+ const a = out[out.length - 2];
63
+ const b = out[out.length - 1];
64
+ if ((near(a.x, b.x) && near(b.x, q.x)) || (near(a.y, b.y) && near(b.y, q.y)))
65
+ out.pop();
66
+ }
67
+ out.push(q);
68
+ }
69
+ return out;
70
+ }
71
+ /**
72
+ * An orthogonal path from `s` (leaving along `sAxis`) through each bend to `e`
73
+ * (arriving along `eAxis`). A bend is a corner the path passes through.
74
+ */
75
+ export function orthogonal(s, sAxis, bends, e, eAxis, mid = 0.5) {
76
+ const out = [s];
77
+ let axis = sAxis; // the axis the path leaves the current point along
78
+ const seq = [...bends, e];
79
+ for (let i = 0; i < seq.length; i++) {
80
+ const a = out[out.length - 1];
81
+ const b = seq[i];
82
+ const alongH = near(a.y, b.y);
83
+ const alongV = near(a.x, b.x);
84
+ if (i === seq.length - 1) {
85
+ if ((eAxis === "h" && alongH) || (eAxis === "v" && alongV))
86
+ out.push(b);
87
+ else if (axis !== eAxis)
88
+ out.push(axis === "h" ? { x: b.x, y: a.y } : { x: a.x, y: b.y }, b);
89
+ else if (axis === "h") {
90
+ const mx = a.x + (b.x - a.x) * mid;
91
+ out.push({ x: mx, y: a.y }, { x: mx, y: b.y }, b);
92
+ }
93
+ else {
94
+ const my = a.y + (b.y - a.y) * mid;
95
+ out.push({ x: a.x, y: my }, { x: b.x, y: my }, b);
96
+ }
97
+ break;
98
+ }
99
+ if (alongH || alongV) {
100
+ out.push(b);
101
+ axis = alongH ? "v" : "h";
102
+ continue;
103
+ }
104
+ out.push(axis === "h" ? { x: b.x, y: a.y } : { x: a.x, y: b.y }, b);
105
+ }
106
+ return out;
107
+ }
108
+ const segLen = (a, b) => Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
109
+ /** Does a box centred on p, w by h, touch the rect? */
110
+ const hits = (p, w, h, r) => p.x + w / 2 > r.x && p.x - w / 2 < r.x + r.w && p.y + h / 2 > r.y && p.y - h / 2 < r.y + r.h;
111
+ /** A label's box, roughly: 7px a character at 12px, plus padding. */
112
+ export const labelSize = (label) => ({ w: label.length * 7 + 14, h: 20 });
113
+ /** The longest label drawn in full; a longer one is cut with an ellipsis (the whole text is its tooltip). */
114
+ export const LABEL_MAX = 24;
115
+ /** A label cut to `max` characters, ending in an ellipsis. */
116
+ export function fitLabel(label, max = LABEL_MAX) {
117
+ return label.length <= max ? label : `${label.slice(0, Math.max(1, max - 1)).trimEnd()}…`;
118
+ }
119
+ /** Room kept clear round a label: from modules, and from other labels. */
120
+ const LABEL_GAP = 4;
121
+ const boxAt = (p, w, h) => ({ x: p.x - w / 2, y: p.y - h / 2, w, h });
122
+ const overlaps = (a, b, gap) => a.x < b.x + b.w + gap && b.x < a.x + a.w + gap && a.y < b.y + b.h + gap && b.y < a.y + a.h + gap;
123
+ /** Does the segment a–b (horizontal or vertical) run through the box? */
124
+ const crosses = (a, b, r) => Math.max(a.x, b.x) > r.x && Math.min(a.x, b.x) < r.x + r.w && Math.max(a.y, b.y) > r.y && Math.min(a.y, b.y) < r.y + r.h;
125
+ /**
126
+ * Every place a label could go on a path, best first by its own merits: on a
127
+ * segment (the longer the better, the nearer its middle the better, wholly on
128
+ * the segment rather than past its ends), then beside a segment (nudged off
129
+ * the line) when nothing on it is free.
130
+ */
131
+ function labelCandidates(points, w, h) {
132
+ const segs = points.slice(1).map((b, i) => ({ a: points[i], b, len: segLen(points[i], b) }));
133
+ const longest = Math.max(0, ...segs.map((s) => s.len));
134
+ const out = [];
135
+ for (const { a, b, len } of segs) {
136
+ if (len < 1)
137
+ continue;
138
+ const horizontal = near(a.y, b.y);
139
+ const along = horizontal ? w : h; // the label's extent along the segment
140
+ const dir = { x: Math.sign(b.x - a.x), y: Math.sign(b.y - a.y) };
141
+ const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
142
+ // Along the segment, out from its middle in 10px steps (both ways).
143
+ const offsets = [0];
144
+ for (let d = 10; d <= len / 2; d += 10)
145
+ offsets.push(d, -d);
146
+ for (const d of offsets) {
147
+ const p = { x: mid.x + dir.x * d, y: mid.y + dir.y * d };
148
+ const room = len / 2 - Math.abs(d); // from p to the nearer end
149
+ const onLine = room >= along / 2 + 2;
150
+ const base = (longest - len) * 0.05 + Math.abs(d) * 0.2 + (onLine ? 0 : 40);
151
+ out.push({ at: p, score: base });
152
+ // Beside the line: nudged off it, either side.
153
+ for (const side of [1, -1]) {
154
+ const q = horizontal ? { x: p.x, y: p.y + side * (h / 2 + 4) } : { x: p.x + side * (w / 2 + 4), y: p.y };
155
+ out.push({ at: q, score: base + 25 });
156
+ }
157
+ }
158
+ }
159
+ return out.sort((p, q) => p.score - q.score);
160
+ }
161
+ /** The middle of the longest segment whose label box clears every module (else the longest). */
162
+ export function labelPoint(points, label, rects) {
163
+ if (points.length < 2)
164
+ return undefined;
165
+ const { w, h } = labelSize(label);
166
+ const cands = labelCandidates(points, w, h);
167
+ const free = cands.find((c) => !rects.some((r) => overlaps(boxAt(c.at, w, h), r, LABEL_GAP)));
168
+ // No segment to sit on (both ends at one point): the first point.
169
+ return (free ?? cands[0])?.at ?? { ...points[0] };
170
+ }
171
+ /**
172
+ * Place every label on a View at once, so none overlaps a module or another
173
+ * label. Each takes the best free place on its own path (see labelCandidates),
174
+ * crossing as few other lines as it can; one with no free place in full is
175
+ * cut shorter (with an ellipsis) and tried again. Sets `label` (where) and
176
+ * `labelText` (what is drawn) on each route.
177
+ */
178
+ export function placeLabels(routes, edges, rects) {
179
+ const placed = [];
180
+ // The modules' extent: the canvas fits it to the screen, so a label past it may be cut off at the edge.
181
+ const ext = rects.length
182
+ ? {
183
+ x0: Math.min(...rects.map((r) => r.x)),
184
+ y0: Math.min(...rects.map((r) => r.y)),
185
+ x1: Math.max(...rects.map((r) => r.x + r.w)),
186
+ y1: Math.max(...rects.map((r) => r.y + r.h)),
187
+ }
188
+ : null;
189
+ const outside = (b) => (ext ? b.x < ext.x0 || b.y < ext.y0 || b.x + b.w > ext.x1 || b.y + b.h > ext.y1 : false);
190
+ const lines = [...routes.values()].map((r) => ({ id: r.id, pts: r.points }));
191
+ const lineHits = (id, b) => lines.reduce((n, l) => (l.id === id ? n : n + (l.pts.slice(1).some((q, i) => crosses(l.pts[i], q, b)) ? 1 : 0)), 0);
192
+ // Long labels first (they have the fewest places), then in View order.
193
+ const order = edges.filter((e) => e.label && routes.has(e.id)).sort((p, q) => q.label.length - p.label.length);
194
+ for (const e of order) {
195
+ const route = routes.get(e.id);
196
+ if (route.points.length < 2)
197
+ continue;
198
+ let best;
199
+ for (const max of [LABEL_MAX, 16, 10]) {
200
+ const text = fitLabel(e.label, max);
201
+ const { w, h } = labelSize(text);
202
+ for (const c of labelCandidates(route.points, w, h)) {
203
+ if (best && c.score >= best.score)
204
+ break; // sorted: nothing later beats it
205
+ const b = boxAt(c.at, w, h);
206
+ if (rects.some((r) => overlaps(b, r, LABEL_GAP)) || placed.some((p) => overlaps(b, p, LABEL_GAP)))
207
+ continue;
208
+ const score = c.score + lineHits(e.id, b) * 40 + (outside(b) ? 50 : 0) + (text === e.label ? 0 : 60);
209
+ if (!best || score < best.score)
210
+ best = { at: c.at, text, box: b, score };
211
+ }
212
+ if (best)
213
+ break;
214
+ }
215
+ if (!best) {
216
+ // Nowhere free even cut short: the middle of the longest segment, cut short.
217
+ const text = fitLabel(e.label, 10);
218
+ const { w, h } = labelSize(text);
219
+ const at = labelPoint(route.points, text, rects);
220
+ best = { at, text, box: boxAt(at, w, h), score: Infinity };
221
+ }
222
+ placed.push(best.box);
223
+ route.label = best.at;
224
+ if (best.text !== e.label)
225
+ route.labelText = best.text;
226
+ }
227
+ }
228
+ /** A connection from a module to itself: out of the right side, round the corner, into the top. */
229
+ function selfLoop(r) {
230
+ const out = { x: r.x + r.w, y: r.y + r.h / 2 };
231
+ const into = { x: r.x + r.w * 0.75, y: r.y };
232
+ return [out, { x: out.x + 2 * STUB, y: out.y }, { x: out.x + 2 * STUB, y: r.y - 2 * STUB }, { x: into.x, y: r.y - 2 * STUB }, into];
233
+ }
234
+ const stubOf = (p, side) => ({ x: p.x + NORMAL[side].x * STUB, y: p.y + NORMAL[side].y * STUB });
235
+ /** How many modules a path runs through (its own two only between their stubs). */
236
+ function crossCount(points, rects) {
237
+ let n = 0;
238
+ for (const r of rects.values()) {
239
+ const hit = points.slice(1).some((b, i) => {
240
+ const a = points[i];
241
+ return Math.max(a.x, b.x) > r.x + 1 && Math.min(a.x, b.x) < r.x + r.w - 1 && Math.max(a.y, b.y) > r.y + 1 && Math.min(a.y, b.y) < r.y + r.h - 1;
242
+ });
243
+ if (hit)
244
+ n++;
245
+ }
246
+ return n;
247
+ }
248
+ /**
249
+ * The sides (and where the middle run goes) for a connection with no bends:
250
+ * the facing sides unless that runs through a module, then an L, the other
251
+ * axis, or the middle run moved along: the first with the fewest modules crossed.
252
+ */
253
+ function bestShape(rects, from, to) {
254
+ const a = rects.get(from);
255
+ const b = rects.get(to);
256
+ const ca = centre(a);
257
+ const cb = centre(b);
258
+ const h = cb.x >= ca.x ? "right" : "left";
259
+ const v = cb.y >= ca.y ? "bottom" : "top";
260
+ const facing = { source: facingSide(a, b), target: facingSide(b, a) };
261
+ const shapes = [
262
+ { ...facing, mid: 0.5 },
263
+ { source: h, target: OPPOSITE[v], mid: 0.5 },
264
+ { source: v, target: OPPOSITE[h], mid: 0.5 },
265
+ { source: axisOf(facing.source) === "h" ? v : h, target: axisOf(facing.source) === "h" ? OPPOSITE[v] : OPPOSITE[h], mid: 0.5 },
266
+ ...[0.2, 0.35, 0.65, 0.8].map((mid) => ({ ...facing, mid })),
267
+ ];
268
+ let best = shapes[0];
269
+ let bestScore = Infinity;
270
+ for (const sh of shapes) {
271
+ const sp = portAt(a, sh.source, 0.5);
272
+ const tp = portAt(b, sh.target, 0.5);
273
+ const inner = orthogonal(stubOf(sp, sh.source), axisOf(sh.source), [], stubOf(tp, sh.target), axisOf(sh.target), sh.mid);
274
+ const pts = simplify(inner);
275
+ // A stub that has to turn back into its own module is no good.
276
+ const back = crossCount(pts, new Map([["a", a], ["b", b]]));
277
+ const others = new Map([...rects].filter(([id]) => id !== from && id !== to));
278
+ // Fewest modules crossed wins; on a tie, the earlier shape (the facing sides first).
279
+ const score = crossCount(pts, others) + back;
280
+ if (score < bestScore)
281
+ (best = sh), (bestScore = score);
282
+ if (score === 0)
283
+ break;
284
+ }
285
+ return best;
286
+ }
287
+ /**
288
+ * Route every connection on a View. `rects` holds each module's box (top-left,
289
+ * width, height). A connection whose module is missing is left out.
290
+ */
291
+ export function routeAll(rects, edges) {
292
+ const ends = [];
293
+ const sides = new Map();
294
+ for (const e of edges) {
295
+ const a = rects.get(e.from);
296
+ const b = rects.get(e.to);
297
+ if (!a || !b || e.from === e.to)
298
+ continue;
299
+ const bends = e.bends ?? [];
300
+ const first = bends[0];
301
+ const last = bends[bends.length - 1];
302
+ const pick = bends.length ? { source: facingSide(a, first), target: facingSide(b, last), mid: 0.5 } : bestShape(rects, e.from, e.to);
303
+ const { source, target } = pick;
304
+ sides.set(e.id, pick);
305
+ ends.push({ edge: e, node: e.from, side: source, toward: first ?? centre(b), end: "source" });
306
+ ends.push({ edge: e, node: e.to, side: target, toward: last ?? centre(a), end: "target" });
307
+ }
308
+ // Spread the connections on one side along it, in the order of where they go, so they don't cross.
309
+ const ports = new Map();
310
+ const groups = new Map();
311
+ for (const x of ends) {
312
+ const k = `${x.node}|${x.side}`;
313
+ groups.set(k, [...(groups.get(k) ?? []), x]);
314
+ }
315
+ for (const group of groups.values()) {
316
+ const { node, side } = group[0];
317
+ const r = rects.get(node);
318
+ const along = axisOf(side) === "h" ? "y" : "x";
319
+ group.sort((p, q) => p.toward[along] - q.toward[along] || (p.edge.id < q.edge.id ? -1 : 1));
320
+ group.forEach((x, i) => ports.set(`${x.edge.id}|${x.end}`, portAt(r, side, (i + 1) / (group.length + 1))));
321
+ }
322
+ const all = [...rects.values()];
323
+ const out = new Map();
324
+ for (const e of edges) {
325
+ const a = rects.get(e.from);
326
+ if (!a || !rects.has(e.to))
327
+ continue;
328
+ let points;
329
+ let sourceSide = "right";
330
+ let targetSide = "top";
331
+ if (e.from === e.to)
332
+ points = selfLoop(a);
333
+ else {
334
+ const s = sides.get(e.id);
335
+ sourceSide = s.source;
336
+ targetSide = s.target;
337
+ const sp = ports.get(`${e.id}|source`);
338
+ const tp = ports.get(`${e.id}|target`);
339
+ const sn = NORMAL[sourceSide];
340
+ const tn = NORMAL[targetSide];
341
+ const s1 = { x: sp.x + sn.x * STUB, y: sp.y + sn.y * STUB };
342
+ const t1 = { x: tp.x + tn.x * STUB, y: tp.y + tn.y * STUB };
343
+ const bends = e.bends ?? [];
344
+ // Facing sides almost level: one straight line, no wiggle.
345
+ if (!bends.length && OPPOSITE[sourceSide] === targetSide && near(axisOf(sourceSide) === "h" ? sp.y : sp.x, axisOf(sourceSide) === "h" ? tp.y : tp.x))
346
+ points = [sp, tp];
347
+ else
348
+ points = [sp, ...orthogonal(s1, axisOf(sourceSide), bends, t1, axisOf(targetSide), s.mid), tp];
349
+ }
350
+ points = simplify(points);
351
+ out.set(e.id, { id: e.id, points, sourceSide, targetSide });
352
+ }
353
+ placeLabels(out, edges, all);
354
+ return out;
355
+ }
356
+ // ---- Editing bends -------------------------------------------------------------
357
+ const onSegment = (p, a, b) => {
358
+ const dist = near(a.x, b.x)
359
+ ? Math.abs(p.x - a.x) + Math.max(0, Math.min(a.y, b.y) - p.y, p.y - Math.max(a.y, b.y))
360
+ : Math.abs(p.y - a.y) + Math.max(0, Math.min(a.x, b.x) - p.x, p.x - Math.max(a.x, b.x));
361
+ return dist;
362
+ };
363
+ /** The segment of a path nearest to p. */
364
+ export function nearestSegment(points, p) {
365
+ let best = 0;
366
+ let bestD = Infinity;
367
+ for (let i = 0; i < points.length - 1; i++) {
368
+ const d = onSegment(p, points[i], points[i + 1]);
369
+ if (d < bestD)
370
+ (best = i), (bestD = d);
371
+ }
372
+ return best;
373
+ }
374
+ const snap = (v, grid) => (grid > 0 ? Math.round(v / grid) * grid : Math.round(v));
375
+ /**
376
+ * Add a bend where a person double-clicked the path. The path's corners
377
+ * become bends first, so the line keeps its shape and the new bend sits on it.
378
+ */
379
+ export function addBend(route, at) {
380
+ const pts = route.points;
381
+ const i = nearestSegment(pts, at);
382
+ const a = pts[i];
383
+ const b = pts[i + 1];
384
+ // Put the new point on the segment itself.
385
+ const p = near(a.x, b.x) ? { x: a.x, y: at.y } : { x: at.x, y: a.y };
386
+ const corners = pts.slice(1, -1);
387
+ return [...corners.slice(0, i), { x: Math.round(p.x), y: Math.round(p.y) }, ...corners.slice(i)];
388
+ }
389
+ /** Remove one bend (double-click on it). */
390
+ export const removeBend = (bends, index) => bends.filter((_, i) => i !== index);
391
+ /** Move one bend (dragged), snapped to the grid. */
392
+ export const moveBend = (bends, index, to, grid = 0) => bends.map((b, i) => (i === index ? { x: snap(to.x, grid), y: snap(to.y, grid) } : b));
393
+ /** The segments of a path a person can drag: both ends are corners, not ports. */
394
+ export const draggableSegments = (points) => points.length < 4 ? [] : Array.from({ length: points.length - 3 }, (_, k) => k + 1);
395
+ /**
396
+ * Drag segment i of a path sideways (a horizontal segment moves up and down, a
397
+ * vertical one left and right): the corners become bends, and the two at its ends move.
398
+ */
399
+ export function moveSegment(route, i, to, grid = 0) {
400
+ const pts = route.points;
401
+ if (i < 1 || i > pts.length - 3)
402
+ return pts.slice(1, -1);
403
+ const a = pts[i];
404
+ const b = pts[i + 1];
405
+ const horizontal = near(a.y, b.y);
406
+ const corners = pts.slice(1, -1).map((p) => ({ ...p }));
407
+ // Corner k of the path is bend k - 1.
408
+ for (const k of [i - 1, i]) {
409
+ if (horizontal)
410
+ corners[k].y = snap(to.y, grid);
411
+ else
412
+ corners[k].x = snap(to.x, grid);
413
+ }
414
+ return corners;
415
+ }
416
+ /** Tidy bends before saving: whole numbers, no repeats. */
417
+ export function cleanBends(bends) {
418
+ const rounded = bends.map((b) => ({ x: Math.round(b.x), y: Math.round(b.y) }));
419
+ return rounded.filter((b, i) => i === 0 || !same(b, rounded[i - 1]));
420
+ }