@yss123/cage-cli 0.2.0 → 0.3.1

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 (629) hide show
  1. package/README.md +47 -3
  2. package/bin/cage.js +5 -0
  3. package/bin/create-cage-app.js +5 -0
  4. package/dist/analyze.d.ts +16 -0
  5. package/dist/analyze.js +18 -2
  6. package/dist/analyze.js.map +1 -1
  7. package/dist/approval-tree.d.ts +88 -0
  8. package/dist/approval-tree.js +18 -0
  9. package/dist/approval-tree.js.map +1 -0
  10. package/dist/baseline.d.ts +8 -0
  11. package/dist/baseline.js +13 -0
  12. package/dist/baseline.js.map +1 -1
  13. package/dist/bootstrap.d.ts +20 -0
  14. package/dist/bootstrap.js +75 -0
  15. package/dist/bootstrap.js.map +1 -0
  16. package/dist/budget.d.ts +7 -0
  17. package/dist/budget.js +49 -5
  18. package/dist/budget.js.map +1 -1
  19. package/dist/capabilities.d.ts +38 -0
  20. package/dist/capabilities.js +148 -0
  21. package/dist/capabilities.js.map +1 -0
  22. package/dist/config.d.ts +31 -2
  23. package/dist/config.js +22 -0
  24. package/dist/config.js.map +1 -1
  25. package/dist/context.d.ts +14 -0
  26. package/dist/context.js +35 -2
  27. package/dist/context.js.map +1 -1
  28. package/dist/create-prompt.d.ts +7 -0
  29. package/dist/create-prompt.js +12 -0
  30. package/dist/create-prompt.js.map +1 -1
  31. package/dist/create.d.ts +21 -1
  32. package/dist/create.js +23 -1
  33. package/dist/create.js.map +1 -1
  34. package/dist/derive.d.ts +23 -0
  35. package/dist/derive.js +40 -0
  36. package/dist/derive.js.map +1 -1
  37. package/dist/dev.d.ts +6 -0
  38. package/dist/dev.js +11 -0
  39. package/dist/dev.js.map +1 -1
  40. package/dist/doctor.d.ts +98 -0
  41. package/dist/doctor.js +93 -0
  42. package/dist/doctor.js.map +1 -0
  43. package/dist/env.d.ts +6 -0
  44. package/dist/env.js +11 -0
  45. package/dist/env.js.map +1 -1
  46. package/dist/findings.d.ts +27 -0
  47. package/dist/findings.js +20 -0
  48. package/dist/findings.js.map +1 -1
  49. package/dist/gen/contracts.d.ts +28 -0
  50. package/dist/gen/contracts.js +52 -12
  51. package/dist/gen/contracts.js.map +1 -1
  52. package/dist/gen/index.d.ts +26 -0
  53. package/dist/gen/index.js +69 -1
  54. package/dist/gen/index.js.map +1 -1
  55. package/dist/gen/zod-code.d.ts +42 -0
  56. package/dist/gen/zod-code.js +178 -16
  57. package/dist/gen/zod-code.js.map +1 -1
  58. package/dist/graph-approvals.d.ts +70 -0
  59. package/dist/graph-approvals.js +93 -0
  60. package/dist/graph-approvals.js.map +1 -0
  61. package/dist/graph-business.d.ts +135 -0
  62. package/dist/graph-business.js +404 -0
  63. package/dist/graph-business.js.map +1 -0
  64. package/dist/graph-command.d.ts +17 -0
  65. package/dist/graph-command.js +21 -0
  66. package/dist/graph-command.js.map +1 -1
  67. package/dist/graph-model.d.ts +44 -0
  68. package/dist/graph-model.js +36 -3
  69. package/dist/graph-model.js.map +1 -1
  70. package/dist/graph-presentation.d.ts +16 -0
  71. package/dist/graph-presentation.js +10 -1
  72. package/dist/graph-presentation.js.map +1 -1
  73. package/dist/graph-query.d.ts +95 -0
  74. package/dist/graph-query.js +62 -0
  75. package/dist/graph-query.js.map +1 -1
  76. package/dist/graph-relations.d.ts +20 -0
  77. package/dist/graph-relations.js +42 -10
  78. package/dist/graph-relations.js.map +1 -1
  79. package/dist/graph-search.d.ts +35 -0
  80. package/dist/graph-search.js +36 -0
  81. package/dist/graph-search.js.map +1 -1
  82. package/dist/graph-snapshot.d.ts +26 -0
  83. package/dist/graph-snapshot.js +30 -1
  84. package/dist/graph-snapshot.js.map +1 -1
  85. package/dist/graph.d.ts +13 -2
  86. package/dist/graph.js +61 -4
  87. package/dist/graph.js.map +1 -1
  88. package/dist/impact.d.ts +50 -0
  89. package/dist/impact.js +62 -2
  90. package/dist/impact.js.map +1 -1
  91. package/dist/index.d.ts +10 -0
  92. package/dist/index.js +10 -0
  93. package/dist/index.js.map +1 -1
  94. package/dist/init.d.ts +12 -0
  95. package/dist/init.js +15 -0
  96. package/dist/init.js.map +1 -1
  97. package/dist/load.d.ts +10 -0
  98. package/dist/load.js +15 -0
  99. package/dist/load.js.map +1 -1
  100. package/dist/main.d.ts +6 -0
  101. package/dist/main.js +93 -5
  102. package/dist/main.js.map +1 -1
  103. package/dist/migrate.d.ts +12 -0
  104. package/dist/migrate.js +17 -0
  105. package/dist/migrate.js.map +1 -1
  106. package/dist/ontology.d.ts +67 -2
  107. package/dist/ontology.js +94 -4
  108. package/dist/ontology.js.map +1 -1
  109. package/dist/process-rules.d.ts +21 -0
  110. package/dist/process-rules.js +100 -0
  111. package/dist/process-rules.js.map +1 -0
  112. package/dist/process.d.ts +32 -0
  113. package/dist/process.js +32 -0
  114. package/dist/process.js.map +1 -1
  115. package/dist/project-structure.d.ts +5 -0
  116. package/dist/project-structure.js +10 -0
  117. package/dist/project-structure.js.map +1 -1
  118. package/dist/registration.d.ts +16 -0
  119. package/dist/registration.js +56 -0
  120. package/dist/registration.js.map +1 -0
  121. package/dist/rules.d.ts +52 -0
  122. package/dist/rules.js +58 -0
  123. package/dist/rules.js.map +1 -1
  124. package/dist/runtime-diagnostics.d.ts +27 -0
  125. package/dist/runtime-diagnostics.js +60 -0
  126. package/dist/runtime-diagnostics.js.map +1 -0
  127. package/dist/snapshots.d.ts +40 -0
  128. package/dist/snapshots.js +62 -0
  129. package/dist/snapshots.js.map +1 -0
  130. package/dist/static-check.d.ts +33 -0
  131. package/dist/static-check.js +37 -1
  132. package/dist/static-check.js.map +1 -1
  133. package/dist/sync.d.ts +11 -0
  134. package/dist/sync.js +66 -9
  135. package/dist/sync.js.map +1 -1
  136. package/dist/upgrade.d.ts +16 -0
  137. package/dist/upgrade.js +21 -0
  138. package/dist/upgrade.js.map +1 -1
  139. package/dist/verify.d.ts +14 -0
  140. package/dist/verify.js +19 -0
  141. package/dist/verify.js.map +1 -1
  142. package/dist/version.js +5 -0
  143. package/dist/version.js.map +1 -1
  144. package/graph-ui/app.js +321 -27
  145. package/graph-ui/approval-flow.js +185 -0
  146. package/graph-ui/approval-parallel.js +110 -0
  147. package/graph-ui/approval-tree.js +17 -0
  148. package/graph-ui/business.css +1011 -0
  149. package/graph-ui/business.js +566 -0
  150. package/graph-ui/flowchart-interaction.js +177 -0
  151. package/graph-ui/flowchart.js +1059 -0
  152. package/graph-ui/graph-approvals.js +92 -0
  153. package/graph-ui/graph-business.js +403 -0
  154. package/graph-ui/graph-model.js +36 -3
  155. package/graph-ui/graph-relations.js +42 -10
  156. package/graph-ui/graph-search.js +36 -0
  157. package/graph-ui/index.html +26 -12
  158. package/graph-ui/model.js +28 -0
  159. package/graph-ui/relations.js +4 -0
  160. package/graph-ui/structure.js +44 -0
  161. package/graph-ui/style.css +12 -0
  162. package/package.json +6 -4
  163. package/templates/app/.dockerignore +2 -0
  164. package/templates/app/.env.example +10 -0
  165. package/templates/app/AGENTS.md +50 -10
  166. package/templates/app/README.md +9 -5
  167. package/templates/app/_gitignore +1 -0
  168. package/templates/app/apps/api/package.json +2 -2
  169. package/templates/app/apps/api/src/main.ts +5 -1
  170. package/templates/app/apps/api/src/worker.ts +3 -0
  171. package/templates/app/apps/web/index.html +1 -0
  172. package/templates/app/apps/web/package.json +7 -3
  173. package/templates/app/apps/web/src/app/app.tsx +122 -12
  174. package/templates/app/apps/web/src/app/styles/global.css +88 -8
  175. package/templates/app/apps/web/src/app/use-session.ts +43 -0
  176. package/templates/app/apps/web/src/app/workspace-layout.tsx +58 -19
  177. package/templates/app/apps/web/src/main.tsx +5 -0
  178. package/templates/app/apps/web/src/modules/approval/components/task-operation-dialog.tsx +61 -0
  179. package/templates/app/apps/web/src/modules/approval/hooks/use-approval-tasks.ts +13 -5
  180. package/templates/app/apps/web/src/modules/approval/index.ts +4 -0
  181. package/templates/app/apps/web/src/modules/approval/pages/approval-tasks-page.tsx +89 -38
  182. package/templates/app/apps/web/src/modules/approval/pages/approval-tasks.css +18 -0
  183. package/templates/app/apps/web/src/modules/auth/index.ts +4 -0
  184. package/templates/app/apps/web/src/modules/auth/pages/login-page.tsx +12 -0
  185. package/templates/app/apps/web/src/modules/inventory/index.ts +4 -0
  186. package/templates/app/apps/web/src/modules/inventory/pages/inventory-page.tsx +29 -1
  187. package/templates/app/apps/web/src/modules/purchase/components/approval-history.tsx +67 -0
  188. package/templates/app/apps/web/src/modules/purchase/components/order-attachments.tsx +29 -0
  189. package/templates/app/apps/web/src/modules/purchase/components/order-reviewers.tsx +38 -0
  190. package/templates/app/apps/web/src/modules/purchase/components/purchase-assistant-context.tsx +17 -0
  191. package/templates/app/apps/web/src/modules/purchase/components/purchase-order-form.css +6 -0
  192. package/templates/app/apps/web/src/modules/purchase/components/purchase-order-form.tsx +32 -3
  193. package/templates/app/apps/web/src/modules/purchase/index.ts +5 -0
  194. package/templates/app/apps/web/src/modules/purchase/lib/presentation.ts +20 -0
  195. package/templates/app/apps/web/src/modules/purchase/pages/purchase-intelligence.css +17 -0
  196. package/templates/app/apps/web/src/modules/purchase/pages/purchase-order-detail-page.tsx +156 -0
  197. package/templates/app/apps/web/src/modules/purchase/pages/purchase-orders-page.tsx +54 -22
  198. package/templates/app/apps/web/src/modules/workspace/index.ts +5 -0
  199. package/templates/app/apps/web/src/modules/workspace/pages/home-page.tsx +44 -0
  200. package/templates/app/apps/web/src/shared/hooks/use-command.ts +13 -0
  201. package/templates/app/apps/web/src/shared/lib/error-message.ts +15 -1
  202. package/templates/app/apps/web/src/shared/lib/navigation.ts +21 -0
  203. package/templates/app/apps/web/src/shared/ui/approval-member-picker.css +10 -0
  204. package/templates/app/apps/web/src/shared/ui/approval-member-picker.tsx +51 -0
  205. package/templates/app/apps/web/src/shared/ui/assistant-markdown.tsx +19 -0
  206. package/templates/app/apps/web/src/shared/ui/assistant-panel.css +134 -0
  207. package/templates/app/apps/web/src/shared/ui/assistant-panel.tsx +200 -0
  208. package/templates/app/apps/web/src/shared/ui/assistant-scope.tsx +72 -0
  209. package/templates/app/apps/web/src/shared/ui/attachment-upload.css +28 -0
  210. package/templates/app/apps/web/src/shared/ui/attachment-upload.tsx +163 -0
  211. package/templates/app/apps/web/src/shared/ui/notice.tsx +7 -0
  212. package/templates/app/apps/web/src/shared/ui/page-heading.tsx +7 -0
  213. package/templates/app/apps/web/src/shared/ui/page-state.css +47 -0
  214. package/templates/app/apps/web/src/shared/ui/page-state.tsx +76 -0
  215. package/templates/app/apps/web/src/shared/ui/require-capability.tsx +34 -0
  216. package/templates/app/apps/web/vite.config.ts +5 -0
  217. package/templates/app/cage.config.json +23 -0
  218. package/templates/app/db/migrations/0002_draft.sql +12 -0
  219. package/templates/app/db/migrations/0003_draft.sql +8 -0
  220. package/templates/app/db/migrations/0004_draft.sql +27 -0
  221. package/templates/app/deploy/nginx.conf +52 -0
  222. package/templates/app/docs/ai/README.md +28 -2
  223. package/templates/app/docs/ai/approvals.md +260 -0
  224. package/templates/app/docs/ai/architecture.md +26 -0
  225. package/templates/app/docs/ai/commenting.md +203 -0
  226. package/templates/app/docs/ai/diagnostics.md +80 -0
  227. package/templates/app/docs/ai/files.md +93 -0
  228. package/templates/app/docs/ai/intelligence.md +348 -0
  229. package/templates/app/docs/ai/ontology-context.md +59 -4
  230. package/templates/app/docs/ai/primitives.md +34 -1
  231. package/templates/app/docs/ai/project-structure.md +8 -3
  232. package/templates/app/docs/ai/purchase-example.md +35 -7
  233. package/templates/app/docs/ai/recipes.md +10 -2
  234. package/templates/app/docs/ai/routing.md +86 -0
  235. package/templates/app/docs/ai/troubleshooting.md +5 -0
  236. package/templates/app/docs/ai/workflow.md +24 -11
  237. package/templates/app/docs/errors/CAGE-AI-001.md +61 -0
  238. package/templates/app/docs/errors/CAGE-APPR-001.md +13 -9
  239. package/templates/app/docs/errors/CAGE-APPR-002.md +2 -2
  240. package/templates/app/docs/errors/CAGE-APPR-003.md +1 -1
  241. package/templates/app/docs/errors/CAGE-APPR-004.md +4 -4
  242. package/templates/app/docs/errors/CAGE-APPR-005.md +4 -4
  243. package/templates/app/docs/errors/CAGE-APPR-006.md +29 -8
  244. package/templates/app/docs/errors/CAGE-APPR-007.md +9 -7
  245. package/templates/app/docs/errors/CAGE-APPR-010.md +5 -5
  246. package/templates/app/docs/errors/CAGE-AUDIT-001.md +53 -0
  247. package/templates/app/docs/errors/CAGE-AUTH-001.md +1 -1
  248. package/templates/app/docs/errors/CAGE-BIZ-001.md +51 -0
  249. package/templates/app/docs/errors/CAGE-BIZ-002.md +49 -0
  250. package/templates/app/docs/errors/CAGE-BUDGET-001.md +1 -1
  251. package/templates/app/docs/errors/CAGE-BUDGET-002.md +1 -1
  252. package/templates/app/docs/errors/CAGE-BUDGET-003.md +1 -1
  253. package/templates/app/docs/errors/CAGE-CMD-002.md +1 -1
  254. package/templates/app/docs/errors/CAGE-CMD-003.md +1 -1
  255. package/templates/app/docs/errors/CAGE-CONN-001.md +3 -3
  256. package/templates/app/docs/errors/CAGE-CONN-002.md +1 -1
  257. package/templates/app/docs/errors/CAGE-CONN-003.md +1 -1
  258. package/templates/app/docs/errors/CAGE-CONN-004.md +1 -1
  259. package/templates/app/docs/errors/CAGE-CONN-005.md +1 -1
  260. package/templates/app/docs/errors/CAGE-CONN-006.md +1 -1
  261. package/templates/app/docs/errors/CAGE-CONN-007.md +6 -6
  262. package/templates/app/docs/errors/CAGE-CONN-008.md +5 -5
  263. package/templates/app/docs/errors/CAGE-CONN-009.md +6 -6
  264. package/templates/app/docs/errors/CAGE-CONN-010.md +6 -6
  265. package/templates/app/docs/errors/CAGE-CONN-011.md +1 -1
  266. package/templates/app/docs/errors/CAGE-CONN-012.md +1 -1
  267. package/templates/app/docs/errors/CAGE-CONN-013.md +49 -0
  268. package/templates/app/docs/errors/CAGE-CONN-014.md +48 -0
  269. package/templates/app/docs/errors/CAGE-CONN-015.md +49 -0
  270. package/templates/app/docs/errors/CAGE-CREATE-001.md +10 -9
  271. package/templates/app/docs/errors/CAGE-DECL-001.md +2 -2
  272. package/templates/app/docs/errors/CAGE-DECL-002.md +7 -6
  273. package/templates/app/docs/errors/CAGE-DECL-003.md +1 -1
  274. package/templates/app/docs/errors/CAGE-DECL-004.md +1 -1
  275. package/templates/app/docs/errors/CAGE-DEP-001.md +2 -2
  276. package/templates/app/docs/errors/CAGE-DEP-002.md +1 -1
  277. package/templates/app/docs/errors/CAGE-DEP-003.md +2 -2
  278. package/templates/app/docs/errors/CAGE-DEP-004.md +1 -1
  279. package/templates/app/docs/errors/CAGE-DEP-005.md +1 -1
  280. package/templates/app/docs/errors/CAGE-DIAG-001.md +53 -0
  281. package/templates/app/docs/errors/CAGE-DLQ-001.md +2 -2
  282. package/templates/app/docs/errors/CAGE-ENV-001.md +4 -4
  283. package/templates/app/docs/errors/CAGE-ENV-002.md +51 -0
  284. package/templates/app/docs/errors/CAGE-ENV-003.md +48 -0
  285. package/templates/app/docs/errors/CAGE-EVENT-001.md +1 -1
  286. package/templates/app/docs/errors/CAGE-EVENT-002.md +7 -7
  287. package/templates/app/docs/errors/CAGE-FIELD-001.md +2 -2
  288. package/templates/app/docs/errors/CAGE-FIELD-002.md +1 -1
  289. package/templates/app/docs/errors/CAGE-FIELD-003.md +1 -1
  290. package/templates/app/docs/errors/CAGE-FIELD-004.md +1 -1
  291. package/templates/app/docs/errors/CAGE-FILE-001.md +56 -0
  292. package/templates/app/docs/errors/CAGE-GEN-001.md +2 -2
  293. package/templates/app/docs/errors/CAGE-GEN-002.md +1 -1
  294. package/templates/app/docs/errors/CAGE-GEN-003.md +53 -0
  295. package/templates/app/docs/errors/CAGE-GRANT-001.md +3 -3
  296. package/templates/app/docs/errors/CAGE-GRANT-002.md +2 -2
  297. package/templates/app/docs/errors/CAGE-GRANT-003.md +7 -6
  298. package/templates/app/docs/errors/CAGE-IMPACT-001.md +2 -2
  299. package/templates/app/docs/errors/CAGE-IMPACT-002.md +52 -0
  300. package/templates/app/docs/errors/CAGE-INIT-001.md +1 -1
  301. package/templates/app/docs/errors/CAGE-INIT-002.md +1 -1
  302. package/templates/app/docs/errors/CAGE-INIT-003.md +2 -2
  303. package/templates/app/docs/errors/CAGE-INIT-004.md +1 -1
  304. package/templates/app/docs/errors/CAGE-INIT-005.md +52 -0
  305. package/templates/app/docs/errors/CAGE-JOB-001.md +1 -1
  306. package/templates/app/docs/errors/CAGE-JOB-002.md +1 -1
  307. package/templates/app/docs/errors/CAGE-JOB-003.md +1 -1
  308. package/templates/app/docs/errors/CAGE-LINT-001.md +1 -1
  309. package/templates/app/docs/errors/CAGE-MIG-001.md +1 -1
  310. package/templates/app/docs/errors/CAGE-MIG-002.md +1 -1
  311. package/templates/app/docs/errors/CAGE-MIG-005.md +1 -1
  312. package/templates/app/docs/errors/CAGE-NAME-001.md +1 -1
  313. package/templates/app/docs/errors/CAGE-NAME-002.md +1 -1
  314. package/templates/app/docs/errors/CAGE-NAME-003.md +3 -3
  315. package/templates/app/docs/errors/CAGE-NAME-004.md +1 -1
  316. package/templates/app/docs/errors/CAGE-NAME-005.md +1 -1
  317. package/templates/app/docs/errors/CAGE-NAME-006.md +1 -1
  318. package/templates/app/docs/errors/CAGE-ORG-001.md +7 -6
  319. package/templates/app/docs/errors/CAGE-PAIR-002.md +6 -5
  320. package/templates/app/docs/errors/CAGE-QUERY-001.md +1 -1
  321. package/templates/app/docs/errors/CAGE-QUERY-002.md +2 -2
  322. package/templates/app/docs/errors/CAGE-QUERY-003.md +7 -6
  323. package/templates/app/docs/errors/CAGE-REACT-001.md +3 -3
  324. package/templates/app/docs/errors/CAGE-REACT-002.md +1 -1
  325. package/templates/app/docs/errors/CAGE-REACT-003.md +50 -0
  326. package/templates/app/docs/errors/CAGE-READS-001.md +9 -6
  327. package/templates/app/docs/errors/CAGE-REG-001.md +13 -12
  328. package/templates/app/docs/errors/CAGE-REG-002.md +2 -1
  329. package/templates/app/docs/errors/CAGE-REG-003.md +1 -1
  330. package/templates/app/docs/errors/CAGE-REL-001.md +14 -14
  331. package/templates/app/docs/errors/CAGE-ROUTE-001.md +7 -6
  332. package/templates/app/docs/errors/CAGE-SCOPE-001.md +1 -1
  333. package/templates/app/docs/errors/CAGE-SCOPE-002.md +2 -2
  334. package/templates/app/docs/errors/CAGE-SECRET-001.md +2 -2
  335. package/templates/app/docs/errors/CAGE-SECRET-002.md +1 -1
  336. package/templates/app/docs/errors/CAGE-SENS-001.md +5 -5
  337. package/templates/app/docs/errors/CAGE-SQL-001.md +1 -1
  338. package/templates/app/docs/errors/CAGE-SQL-002.md +1 -1
  339. package/templates/app/docs/errors/CAGE-STATE-001.md +3 -3
  340. package/templates/app/docs/errors/CAGE-STATE-002.md +1 -1
  341. package/templates/app/docs/errors/CAGE-STATE-003.md +1 -1
  342. package/templates/app/docs/errors/CAGE-STATE-004.md +1 -1
  343. package/templates/app/docs/errors/CAGE-STATE-005.md +3 -2
  344. package/templates/app/docs/errors/CAGE-STATE-010.md +5 -5
  345. package/templates/app/docs/errors/CAGE-STATE-011.md +5 -5
  346. package/templates/app/docs/errors/CAGE-STRUCT-001.md +1 -1
  347. package/templates/app/docs/errors/CAGE-STRUCT-002.md +1 -1
  348. package/templates/app/docs/errors/CAGE-SYNC-001.md +3 -2
  349. package/templates/app/docs/errors/CAGE-TEST-001.md +1 -1
  350. package/templates/app/docs/errors/CAGE-TEST-002.md +50 -0
  351. package/templates/app/docs/errors/CAGE-TEST-003.md +52 -0
  352. package/templates/app/docs/errors/CAGE-UNSAFE-001.md +3 -2
  353. package/templates/app/docs/errors/CAGE-UPGRADE-001.md +2 -2
  354. package/templates/app/docs/errors/CAGE-WORKER-001.md +48 -0
  355. package/templates/app/docs/errors/CAGE-WORKER-002.md +50 -0
  356. package/templates/app/docs/errors/CAGE-WORKER-003.md +48 -0
  357. package/templates/app/docs/errors/README.md +52 -2
  358. package/templates/app/package.json +3 -3
  359. package/templates/app/packages/contracts/package.json +1 -1
  360. package/templates/app/packages/contracts/src/aggregates/approval-member/index.ts +70 -0
  361. package/templates/app/packages/contracts/src/aggregates/dept/index.ts +10 -10
  362. package/templates/app/packages/contracts/src/aggregates/inventory/index.ts +5 -5
  363. package/templates/app/packages/contracts/src/aggregates/inventory-lock/index.ts +4 -4
  364. package/templates/app/packages/contracts/src/aggregates/purchase-order/index.ts +34 -12
  365. package/templates/app/packages/contracts/src/aggregates/role/index.ts +15 -15
  366. package/templates/app/packages/contracts/src/aggregates/user/index.ts +16 -16
  367. package/templates/app/packages/contracts/src/index.ts +16 -1
  368. package/templates/app/packages/contracts/src/queries/index.ts +2 -2
  369. package/templates/app/packages/domain/package.json +5 -4
  370. package/templates/app/packages/domain/src/index.ts +4 -0
  371. package/templates/app/packages/domain/src/integrations/erp.ts +15 -0
  372. package/templates/app/packages/domain/src/integrations/intelligence.ts +20 -0
  373. package/templates/app/packages/domain/src/modules/inventory/index.ts +1 -0
  374. package/templates/app/packages/domain/src/modules/inventory/inventory.ts +12 -0
  375. package/templates/app/packages/domain/src/modules/purchase/approval-members.ts +54 -0
  376. package/templates/app/packages/domain/src/modules/purchase/approvals.ts +38 -6
  377. package/templates/app/packages/domain/src/modules/purchase/index.ts +2 -0
  378. package/templates/app/packages/domain/src/modules/purchase/intelligence.test.ts +224 -0
  379. package/templates/app/packages/domain/src/modules/purchase/jobs.ts +5 -0
  380. package/templates/app/packages/domain/src/modules/purchase/purchase-order.test.ts +9 -0
  381. package/templates/app/packages/domain/src/modules/purchase/purchase-order.ts +125 -4
  382. package/templates/app/packages/domain/src/modules/purchase/queries.ts +5 -0
  383. package/templates/app/packages/domain/src/workflows/purchase-inventory.ts +6 -0
  384. package/templates/app/packages/domain/src/workflows/purchase-process.ts +42 -0
  385. package/templates/blank/.dockerignore +2 -0
  386. package/templates/blank/.env.example +10 -0
  387. package/templates/blank/AGENTS.md +50 -10
  388. package/templates/blank/README.md +4 -1
  389. package/templates/blank/_gitignore +1 -0
  390. package/templates/blank/apps/api/package.json +1 -1
  391. package/templates/blank/apps/api/src/main.ts +4 -1
  392. package/templates/blank/apps/api/src/worker.ts +2 -0
  393. package/templates/blank/apps/web/index.html +1 -0
  394. package/templates/blank/apps/web/package.json +7 -3
  395. package/templates/blank/apps/web/src/app/app.tsx +80 -18
  396. package/templates/blank/apps/web/src/app/styles/global.css +90 -0
  397. package/templates/blank/apps/web/src/app/use-session.ts +43 -0
  398. package/templates/blank/apps/web/src/app/workspace-layout.tsx +30 -5
  399. package/templates/blank/apps/web/src/main.tsx +5 -0
  400. package/templates/blank/apps/web/src/modules/auth/index.ts +4 -0
  401. package/templates/blank/apps/web/src/modules/auth/pages/login-page.tsx +18 -1
  402. package/templates/blank/apps/web/src/modules/directory/index.ts +7 -0
  403. package/templates/blank/apps/web/src/modules/directory/pages/user-detail-page.tsx +78 -0
  404. package/templates/blank/apps/web/src/modules/directory/pages/users-page.tsx +93 -0
  405. package/templates/blank/apps/web/src/modules/workspace/index.ts +4 -0
  406. package/templates/blank/apps/web/src/modules/workspace/pages/home-page.tsx +17 -0
  407. package/templates/blank/apps/web/src/shared/lib/error-message.ts +11 -0
  408. package/templates/blank/apps/web/src/shared/lib/navigation.ts +21 -0
  409. package/templates/blank/apps/web/src/shared/ui/brand.tsx +10 -2
  410. package/templates/blank/apps/web/src/shared/ui/page-state.css +47 -0
  411. package/templates/blank/apps/web/src/shared/ui/page-state.tsx +76 -0
  412. package/templates/blank/apps/web/src/shared/ui/require-capability.tsx +34 -0
  413. package/templates/blank/apps/web/vite.config.ts +5 -0
  414. package/templates/blank/deploy/nginx.conf +52 -0
  415. package/templates/blank/docs/ai/README.md +28 -2
  416. package/templates/blank/docs/ai/approvals.md +260 -0
  417. package/templates/blank/docs/ai/architecture.md +26 -0
  418. package/templates/blank/docs/ai/commenting.md +203 -0
  419. package/templates/blank/docs/ai/diagnostics.md +80 -0
  420. package/templates/blank/docs/ai/files.md +93 -0
  421. package/templates/blank/docs/ai/intelligence.md +348 -0
  422. package/templates/blank/docs/ai/ontology-context.md +59 -4
  423. package/templates/blank/docs/ai/primitives.md +34 -1
  424. package/templates/blank/docs/ai/project-structure.md +8 -3
  425. package/templates/blank/docs/ai/purchase-example.md +35 -7
  426. package/templates/blank/docs/ai/recipes.md +10 -2
  427. package/templates/blank/docs/ai/routing.md +86 -0
  428. package/templates/blank/docs/ai/troubleshooting.md +5 -0
  429. package/templates/blank/docs/ai/workflow.md +24 -11
  430. package/templates/blank/docs/errors/CAGE-AI-001.md +61 -0
  431. package/templates/blank/docs/errors/CAGE-APPR-001.md +13 -9
  432. package/templates/blank/docs/errors/CAGE-APPR-002.md +2 -2
  433. package/templates/blank/docs/errors/CAGE-APPR-003.md +1 -1
  434. package/templates/blank/docs/errors/CAGE-APPR-004.md +4 -4
  435. package/templates/blank/docs/errors/CAGE-APPR-005.md +4 -4
  436. package/templates/blank/docs/errors/CAGE-APPR-006.md +29 -8
  437. package/templates/blank/docs/errors/CAGE-APPR-007.md +9 -7
  438. package/templates/blank/docs/errors/CAGE-APPR-010.md +5 -5
  439. package/templates/blank/docs/errors/CAGE-AUDIT-001.md +53 -0
  440. package/templates/blank/docs/errors/CAGE-AUTH-001.md +1 -1
  441. package/templates/blank/docs/errors/CAGE-BIZ-001.md +51 -0
  442. package/templates/blank/docs/errors/CAGE-BIZ-002.md +49 -0
  443. package/templates/blank/docs/errors/CAGE-BUDGET-001.md +1 -1
  444. package/templates/blank/docs/errors/CAGE-BUDGET-002.md +1 -1
  445. package/templates/blank/docs/errors/CAGE-BUDGET-003.md +1 -1
  446. package/templates/blank/docs/errors/CAGE-CMD-002.md +1 -1
  447. package/templates/blank/docs/errors/CAGE-CMD-003.md +1 -1
  448. package/templates/blank/docs/errors/CAGE-CONN-001.md +3 -3
  449. package/templates/blank/docs/errors/CAGE-CONN-002.md +1 -1
  450. package/templates/blank/docs/errors/CAGE-CONN-003.md +1 -1
  451. package/templates/blank/docs/errors/CAGE-CONN-004.md +1 -1
  452. package/templates/blank/docs/errors/CAGE-CONN-005.md +1 -1
  453. package/templates/blank/docs/errors/CAGE-CONN-006.md +1 -1
  454. package/templates/blank/docs/errors/CAGE-CONN-007.md +6 -6
  455. package/templates/blank/docs/errors/CAGE-CONN-008.md +5 -5
  456. package/templates/blank/docs/errors/CAGE-CONN-009.md +6 -6
  457. package/templates/blank/docs/errors/CAGE-CONN-010.md +6 -6
  458. package/templates/blank/docs/errors/CAGE-CONN-011.md +1 -1
  459. package/templates/blank/docs/errors/CAGE-CONN-012.md +1 -1
  460. package/templates/blank/docs/errors/CAGE-CONN-013.md +49 -0
  461. package/templates/blank/docs/errors/CAGE-CONN-014.md +48 -0
  462. package/templates/blank/docs/errors/CAGE-CONN-015.md +49 -0
  463. package/templates/blank/docs/errors/CAGE-CREATE-001.md +10 -9
  464. package/templates/blank/docs/errors/CAGE-DECL-001.md +2 -2
  465. package/templates/blank/docs/errors/CAGE-DECL-002.md +7 -6
  466. package/templates/blank/docs/errors/CAGE-DECL-003.md +1 -1
  467. package/templates/blank/docs/errors/CAGE-DECL-004.md +1 -1
  468. package/templates/blank/docs/errors/CAGE-DEP-001.md +2 -2
  469. package/templates/blank/docs/errors/CAGE-DEP-002.md +1 -1
  470. package/templates/blank/docs/errors/CAGE-DEP-003.md +2 -2
  471. package/templates/blank/docs/errors/CAGE-DEP-004.md +1 -1
  472. package/templates/blank/docs/errors/CAGE-DEP-005.md +1 -1
  473. package/templates/blank/docs/errors/CAGE-DIAG-001.md +53 -0
  474. package/templates/blank/docs/errors/CAGE-DLQ-001.md +2 -2
  475. package/templates/blank/docs/errors/CAGE-ENV-001.md +4 -4
  476. package/templates/blank/docs/errors/CAGE-ENV-002.md +51 -0
  477. package/templates/blank/docs/errors/CAGE-ENV-003.md +48 -0
  478. package/templates/blank/docs/errors/CAGE-EVENT-001.md +1 -1
  479. package/templates/blank/docs/errors/CAGE-EVENT-002.md +7 -7
  480. package/templates/blank/docs/errors/CAGE-FIELD-001.md +2 -2
  481. package/templates/blank/docs/errors/CAGE-FIELD-002.md +1 -1
  482. package/templates/blank/docs/errors/CAGE-FIELD-003.md +1 -1
  483. package/templates/blank/docs/errors/CAGE-FIELD-004.md +1 -1
  484. package/templates/blank/docs/errors/CAGE-FILE-001.md +56 -0
  485. package/templates/blank/docs/errors/CAGE-GEN-001.md +2 -2
  486. package/templates/blank/docs/errors/CAGE-GEN-002.md +1 -1
  487. package/templates/blank/docs/errors/CAGE-GEN-003.md +53 -0
  488. package/templates/blank/docs/errors/CAGE-GRANT-001.md +3 -3
  489. package/templates/blank/docs/errors/CAGE-GRANT-002.md +2 -2
  490. package/templates/blank/docs/errors/CAGE-GRANT-003.md +7 -6
  491. package/templates/blank/docs/errors/CAGE-IMPACT-001.md +2 -2
  492. package/templates/blank/docs/errors/CAGE-IMPACT-002.md +52 -0
  493. package/templates/blank/docs/errors/CAGE-INIT-001.md +1 -1
  494. package/templates/blank/docs/errors/CAGE-INIT-002.md +1 -1
  495. package/templates/blank/docs/errors/CAGE-INIT-003.md +2 -2
  496. package/templates/blank/docs/errors/CAGE-INIT-004.md +1 -1
  497. package/templates/blank/docs/errors/CAGE-INIT-005.md +52 -0
  498. package/templates/blank/docs/errors/CAGE-JOB-001.md +1 -1
  499. package/templates/blank/docs/errors/CAGE-JOB-002.md +1 -1
  500. package/templates/blank/docs/errors/CAGE-JOB-003.md +1 -1
  501. package/templates/blank/docs/errors/CAGE-LINT-001.md +1 -1
  502. package/templates/blank/docs/errors/CAGE-MIG-001.md +1 -1
  503. package/templates/blank/docs/errors/CAGE-MIG-002.md +1 -1
  504. package/templates/blank/docs/errors/CAGE-MIG-005.md +1 -1
  505. package/templates/blank/docs/errors/CAGE-NAME-001.md +1 -1
  506. package/templates/blank/docs/errors/CAGE-NAME-002.md +1 -1
  507. package/templates/blank/docs/errors/CAGE-NAME-003.md +3 -3
  508. package/templates/blank/docs/errors/CAGE-NAME-004.md +1 -1
  509. package/templates/blank/docs/errors/CAGE-NAME-005.md +1 -1
  510. package/templates/blank/docs/errors/CAGE-NAME-006.md +1 -1
  511. package/templates/blank/docs/errors/CAGE-ORG-001.md +7 -6
  512. package/templates/blank/docs/errors/CAGE-PAIR-002.md +6 -5
  513. package/templates/blank/docs/errors/CAGE-QUERY-001.md +1 -1
  514. package/templates/blank/docs/errors/CAGE-QUERY-002.md +2 -2
  515. package/templates/blank/docs/errors/CAGE-QUERY-003.md +7 -6
  516. package/templates/blank/docs/errors/CAGE-REACT-001.md +3 -3
  517. package/templates/blank/docs/errors/CAGE-REACT-002.md +1 -1
  518. package/templates/blank/docs/errors/CAGE-REACT-003.md +50 -0
  519. package/templates/blank/docs/errors/CAGE-READS-001.md +9 -6
  520. package/templates/blank/docs/errors/CAGE-REG-001.md +13 -12
  521. package/templates/blank/docs/errors/CAGE-REG-002.md +2 -1
  522. package/templates/blank/docs/errors/CAGE-REG-003.md +1 -1
  523. package/templates/blank/docs/errors/CAGE-REL-001.md +14 -14
  524. package/templates/blank/docs/errors/CAGE-ROUTE-001.md +7 -6
  525. package/templates/blank/docs/errors/CAGE-SCOPE-001.md +1 -1
  526. package/templates/blank/docs/errors/CAGE-SCOPE-002.md +2 -2
  527. package/templates/blank/docs/errors/CAGE-SECRET-001.md +2 -2
  528. package/templates/blank/docs/errors/CAGE-SECRET-002.md +1 -1
  529. package/templates/blank/docs/errors/CAGE-SENS-001.md +5 -5
  530. package/templates/blank/docs/errors/CAGE-SQL-001.md +1 -1
  531. package/templates/blank/docs/errors/CAGE-SQL-002.md +1 -1
  532. package/templates/blank/docs/errors/CAGE-STATE-001.md +3 -3
  533. package/templates/blank/docs/errors/CAGE-STATE-002.md +1 -1
  534. package/templates/blank/docs/errors/CAGE-STATE-003.md +1 -1
  535. package/templates/blank/docs/errors/CAGE-STATE-004.md +1 -1
  536. package/templates/blank/docs/errors/CAGE-STATE-005.md +3 -2
  537. package/templates/blank/docs/errors/CAGE-STATE-010.md +5 -5
  538. package/templates/blank/docs/errors/CAGE-STATE-011.md +5 -5
  539. package/templates/blank/docs/errors/CAGE-STRUCT-001.md +1 -1
  540. package/templates/blank/docs/errors/CAGE-STRUCT-002.md +1 -1
  541. package/templates/blank/docs/errors/CAGE-SYNC-001.md +3 -2
  542. package/templates/blank/docs/errors/CAGE-TEST-001.md +1 -1
  543. package/templates/blank/docs/errors/CAGE-TEST-002.md +50 -0
  544. package/templates/blank/docs/errors/CAGE-TEST-003.md +52 -0
  545. package/templates/blank/docs/errors/CAGE-UNSAFE-001.md +3 -2
  546. package/templates/blank/docs/errors/CAGE-UPGRADE-001.md +2 -2
  547. package/templates/blank/docs/errors/CAGE-WORKER-001.md +48 -0
  548. package/templates/blank/docs/errors/CAGE-WORKER-002.md +50 -0
  549. package/templates/blank/docs/errors/CAGE-WORKER-003.md +48 -0
  550. package/templates/blank/docs/errors/README.md +52 -2
  551. package/templates/blank/docs/examples/purchase-admin/apps/api/src/main.ts +5 -1
  552. package/templates/blank/docs/examples/purchase-admin/apps/api/src/worker.ts +3 -0
  553. package/templates/blank/docs/examples/purchase-admin/apps/web/src/app/app.tsx +122 -12
  554. package/templates/blank/docs/examples/purchase-admin/apps/web/src/app/styles/global.css +88 -8
  555. package/templates/blank/docs/examples/purchase-admin/apps/web/src/app/use-session.ts +43 -0
  556. package/templates/blank/docs/examples/purchase-admin/apps/web/src/app/workspace-layout.tsx +58 -19
  557. package/templates/blank/docs/examples/purchase-admin/apps/web/src/main.tsx +5 -0
  558. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/approval/components/task-operation-dialog.tsx +61 -0
  559. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/approval/hooks/use-approval-tasks.ts +13 -5
  560. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/approval/index.ts +4 -0
  561. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/approval/pages/approval-tasks-page.tsx +89 -38
  562. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/approval/pages/approval-tasks.css +18 -0
  563. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/auth/index.ts +4 -0
  564. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/auth/pages/login-page.tsx +12 -0
  565. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/inventory/index.ts +4 -0
  566. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/inventory/pages/inventory-page.tsx +29 -1
  567. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/components/approval-history.tsx +67 -0
  568. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/components/order-attachments.tsx +29 -0
  569. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/components/order-reviewers.tsx +38 -0
  570. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/components/purchase-assistant-context.tsx +17 -0
  571. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/components/purchase-order-form.css +6 -0
  572. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/components/purchase-order-form.tsx +32 -3
  573. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/index.ts +5 -0
  574. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/lib/presentation.ts +20 -0
  575. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/pages/purchase-intelligence.css +17 -0
  576. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/pages/purchase-order-detail-page.tsx +156 -0
  577. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/purchase/pages/purchase-orders-page.tsx +54 -22
  578. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/workspace/index.ts +5 -0
  579. package/templates/blank/docs/examples/purchase-admin/apps/web/src/modules/workspace/pages/home-page.tsx +44 -0
  580. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/hooks/use-command.ts +13 -0
  581. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/lib/error-message.ts +15 -1
  582. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/lib/navigation.ts +21 -0
  583. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/approval-member-picker.css +10 -0
  584. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/approval-member-picker.tsx +51 -0
  585. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/assistant-markdown.tsx +19 -0
  586. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/assistant-panel.css +134 -0
  587. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/assistant-panel.tsx +200 -0
  588. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/assistant-scope.tsx +72 -0
  589. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/attachment-upload.css +28 -0
  590. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/attachment-upload.tsx +163 -0
  591. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/notice.tsx +7 -0
  592. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/page-heading.tsx +7 -0
  593. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/page-state.css +47 -0
  594. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/page-state.tsx +76 -0
  595. package/templates/blank/docs/examples/purchase-admin/apps/web/src/shared/ui/require-capability.tsx +34 -0
  596. package/templates/blank/docs/examples/purchase-admin/cage.config.json +23 -0
  597. package/templates/blank/docs/examples/purchase-admin/packages/contracts/src/aggregates/approval-member/index.ts +70 -0
  598. package/templates/blank/docs/examples/purchase-admin/packages/contracts/src/aggregates/dept/index.ts +10 -10
  599. package/templates/blank/docs/examples/purchase-admin/packages/contracts/src/aggregates/inventory/index.ts +5 -5
  600. package/templates/blank/docs/examples/purchase-admin/packages/contracts/src/aggregates/inventory-lock/index.ts +4 -4
  601. package/templates/blank/docs/examples/purchase-admin/packages/contracts/src/aggregates/purchase-order/index.ts +34 -12
  602. package/templates/blank/docs/examples/purchase-admin/packages/contracts/src/aggregates/role/index.ts +15 -15
  603. package/templates/blank/docs/examples/purchase-admin/packages/contracts/src/aggregates/user/index.ts +16 -16
  604. package/templates/blank/docs/examples/purchase-admin/packages/contracts/src/index.ts +16 -1
  605. package/templates/blank/docs/examples/purchase-admin/packages/contracts/src/queries/index.ts +2 -2
  606. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/index.ts +4 -0
  607. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/integrations/erp.ts +15 -0
  608. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/integrations/intelligence.ts +20 -0
  609. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/modules/inventory/index.ts +1 -0
  610. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/modules/inventory/inventory.ts +12 -0
  611. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/modules/purchase/approval-members.ts +54 -0
  612. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/modules/purchase/approvals.ts +38 -6
  613. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/modules/purchase/index.ts +2 -0
  614. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/modules/purchase/intelligence.test.ts +224 -0
  615. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/modules/purchase/jobs.ts +5 -0
  616. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/modules/purchase/purchase-order.test.ts +9 -0
  617. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/modules/purchase/purchase-order.ts +125 -4
  618. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/modules/purchase/queries.ts +5 -0
  619. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/workflows/purchase-inventory.ts +6 -0
  620. package/templates/blank/docs/examples/purchase-admin/packages/domain/src/workflows/purchase-process.ts +42 -0
  621. package/templates/blank/package.json +3 -3
  622. package/templates/blank/packages/contracts/package.json +1 -1
  623. package/templates/blank/packages/contracts/src/aggregates/dept/index.ts +10 -10
  624. package/templates/blank/packages/contracts/src/aggregates/role/index.ts +15 -15
  625. package/templates/blank/packages/contracts/src/aggregates/user/index.ts +16 -16
  626. package/templates/blank/packages/contracts/src/index.ts +1 -1
  627. package/templates/blank/packages/contracts/src/queries/index.ts +1 -1
  628. package/templates/blank/packages/domain/package.json +5 -4
  629. package/templates/blank/packages/domain/src/index.ts +2 -1
@@ -0,0 +1,1059 @@
1
+ import { isApprovalRejection } from './graph-approvals.js';
2
+ import { layoutApprovalFlow, renderApprovalFlow } from './approval-flow.js';
3
+ import { createFlowGestures, wheelFlowCamera, zoomFlowCamera, revealFlowPosition, MIN_FLOW_ZOOM, MAX_FLOW_ZOOM, } from './flowchart-interaction.js';
4
+ /**
5
+ * 创建新场景的独立视野和手动位置容器,未初始化相机由 mount 决定可读起点或适应全图。
6
+ */
7
+ export const createFlowChartState = () => ({
8
+ camera: { x: 0, y: 0, scale: 1, initialized: false },
9
+ positions: new Map(),
10
+ tool: 'select',
11
+ expanded: false,
12
+ selection: null,
13
+ });
14
+ /** 按中英文近似字宽换行,完整保留长业务标题;测量不是浏览器字体引擎的精确宽度。 */
15
+ /**
16
+ * 按中英文近似宽度换行以保留完整业务标题,不用省略号隐藏流程含义。
17
+ */
18
+ function wrap(text, length) {
19
+ const lines = [];
20
+ let line = '', used = 0;
21
+ for (const character of text) {
22
+ const width = /[\u0000-\u00ff]/.test(character) ? 0.55 : 1;
23
+ if (used + width > length && line) {
24
+ lines.push(line);
25
+ line = '';
26
+ used = 0;
27
+ }
28
+ line += character;
29
+ used += width;
30
+ }
31
+ if (line)
32
+ lines.push(line);
33
+ return lines;
34
+ }
35
+ /** 主流程分层,对象关联放在阶段侧面。循环和汇合回连已有阶段,
36
+ * 关联节点不参与流程深度,避免把“涉及设备”误读为“下一步执行设备”。 */
37
+ /**
38
+ * 测量步骤/审批,拓扑分层后压紧分支并为侧边关联与回路预留通道。
39
+ * @param root 已有界阅读树。
40
+ * @param name 步骤中文名称函数。
41
+ * @param state 步骤状态或参与人摘要函数。
42
+ * @param direction 主阅读方向,默认纵向。
43
+ * @param approvals 按步骤 ID 提供的审批投影。
44
+ * @returns 新布局,不修改声明;重复步骤回连原节点,关联不参与主流程层级。
45
+ */
46
+ export function layoutBusinessFlow(root, name, state, direction = 'tb', approvals = new Map()) {
47
+ const nodes = [];
48
+ const edges = [];
49
+ const sizes = new Map();
50
+ const related = new Map();
51
+ const relatedGaps = new Map();
52
+ /**
53
+ * 测量步骤并收集边;重复步骤指回祖先或已出现节点,关联对象单独测量且不加入主流程递归。
54
+ */
55
+ const walk = (step, depth, ancestors) => {
56
+ if (step.repeated) {
57
+ const ancestor = [...ancestors].reverse().find((n) => n.node.key === step.node.key);
58
+ const target = ancestor ?? nodes.find((n) => n.step.node.key === step.node.key)?.step;
59
+ if (target)
60
+ edges.push({
61
+ from: ancestors[ancestors.length - 1].id,
62
+ to: target.id,
63
+ step,
64
+ loop: Boolean(ancestor),
65
+ });
66
+ return;
67
+ }
68
+ const approval = approvals.has(step.id)
69
+ ? layoutApprovalFlow(approvals.get(step.id), name(step))
70
+ : undefined;
71
+ const decision = !approval &&
72
+ ((step.node.kind === 'approval' && step.label !== '取消审批') ||
73
+ step.node.attrs.stageKind === 'decision');
74
+ const title = wrap((step.label === '取消审批' ? '取消:' : '') + name(step), decision ? 9 : 12);
75
+ const summary = decision || approval ? [] : wrap(state(step), 21);
76
+ const width = approval?.width ?? (decision ? 198 : 208);
77
+ const height = approval?.height ??
78
+ (decision ? Math.max(116, title.length * 21 + 58) : 52 + title.length * 22 + summary.length * 16);
79
+ nodes.push({ step, title, state: summary, decision, approval, position: { x: 0, y: 0, width, height } });
80
+ const associations = step.children
81
+ .filter((child) => child.mode === 'association')
82
+ .map((child) => {
83
+ const title = wrap(name(child), 11);
84
+ const summary = wrap(state(child), 18);
85
+ return {
86
+ step: child,
87
+ title,
88
+ state: summary,
89
+ decision: false,
90
+ association: true,
91
+ position: { x: 0, y: 0, width: 190, height: 52 + title.length * 22 + summary.length * 16 },
92
+ };
93
+ });
94
+ related.set(step.id, associations);
95
+ // 关联说明放在线上方,横向空隙至少容纳完整文字,避免盖到两侧对象卡片。
96
+ const relatedGap = direction === 'tb'
97
+ ? Math.max(110, ...associations.map((item) => Math.max(...wrap(item.step.label, 20).map((line) => [...line].reduce((size, char) => size + (/[\u0000-\u00ff]/.test(char) ? 0.55 : 1), 0))) *
98
+ 11 +
99
+ 44))
100
+ : 110;
101
+ relatedGaps.set(step.id, relatedGap);
102
+ const relatedAlong = associations.reduce((total, n) => total + (direction === 'tb' ? n.position.height : n.position.width), 0) +
103
+ Math.max(0, associations.length - 1) * 36;
104
+ const along = Math.max(direction === 'tb' ? height : width, relatedAlong);
105
+ const cross = (direction === 'tb' ? width : height) +
106
+ (associations.length
107
+ ? relatedGap +
108
+ Math.max(...associations.map((n) => (direction === 'tb' ? n.position.width : n.position.height)))
109
+ : 0);
110
+ sizes.set(step.id, { along, cross, rank: 0 });
111
+ for (const child of step.children.filter((child) => child.mode !== 'association')) {
112
+ if (!child.repeated)
113
+ edges.push({ from: step.id, to: child.id, step: child, loop: false });
114
+ walk(child, depth + 1, [...ancestors, step]);
115
+ }
116
+ };
117
+ walk(root, 0, []);
118
+ // 汇合阶段排在所有前置步骤之后。DFS 深度会把较晚遇到的分支画成倒流。
119
+ const forward = edges.filter((edge) => !edge.loop);
120
+ const incoming = new Map(nodes.map((n) => [n.step.id, forward.filter((edge) => edge.to === n.step.id).length]));
121
+ const ready = nodes.filter((n) => incoming.get(n.step.id) === 0).map((n) => n.step.id);
122
+ for (let i = 0; i < ready.length; i++)
123
+ for (const edge of forward.filter((edge) => edge.from === ready[i])) {
124
+ const target = sizes.get(edge.to);
125
+ target.rank = Math.max(target.rank, sizes.get(edge.from).rank + 1);
126
+ incoming.set(edge.to, incoming.get(edge.to) - 1);
127
+ if (incoming.get(edge.to) === 0)
128
+ ready.push(edge.to);
129
+ }
130
+ const paths = edges.filter((edge) => edge.step.mode !== 'association');
131
+ const side = paths.filter((edge) => edge.loop ||
132
+ edge.step.repeated ||
133
+ edge.step.branch ||
134
+ paths.find((candidate) => candidate.from === edge.from &&
135
+ !candidate.step.branch &&
136
+ !candidate.loop &&
137
+ !candidate.step.repeated) !== edge);
138
+ const afterRank = [], beforeRank = [];
139
+ for (const edge of side) {
140
+ const outgoing = side.filter((candidate) => candidate.from === edge.from);
141
+ const incoming = side.filter((candidate) => candidate.to === edge.to);
142
+ const mainIncoming = paths.some((candidate) => candidate.to === edge.to && !side.includes(candidate));
143
+ const sourceOffset = outgoing.length > 1
144
+ ? outgoing.slice(0, outgoing.indexOf(edge) + 1).reduce((sum, item) => {
145
+ const target = nodes.find((node) => node.step.id === item.to).step;
146
+ const lines = [...wrap(item.step.label, 14), ...wrap(`到:${name(target)}`, 14)];
147
+ const extent = direction === 'tb'
148
+ ? lines.length * 14 + 6
149
+ : Math.max(...lines.map((line) => [...line].reduce((size, char) => size + (/[\u0000-\u00ff]/.test(char) ? 0.55 : 1), 0))) *
150
+ 11 +
151
+ 20;
152
+ return sum + extent + 32;
153
+ }, 0)
154
+ : undefined;
155
+ edge.routing = {
156
+ lane: 0,
157
+ sourcePort: (outgoing.indexOf(edge) + 1) / (outgoing.length + 1),
158
+ targetPort: (mainIncoming ? 0.12 : 0.16) +
159
+ ((mainIncoming ? 0.3 : 0.68) * (incoming.indexOf(edge) + 1)) / (incoming.length + 1),
160
+ targetLane: incoming.indexOf(edge),
161
+ sourceOffset,
162
+ sourceDecision: nodes.find((node) => node.step.id === edge.from).decision,
163
+ targetDecision: nodes.find((node) => node.step.id === edge.to).decision,
164
+ };
165
+ const sourceRank = sizes.get(edge.from).rank, targetRank = sizes.get(edge.to).rank;
166
+ afterRank[sourceRank] = Math.max(afterRank[sourceRank] ?? 0, sourceOffset ?? 0);
167
+ beforeRank[targetRank] = Math.max(beforeRank[targetRank] ?? 0, 24 + incoming.indexOf(edge) * 14);
168
+ }
169
+ const levels = [];
170
+ for (const size of sizes.values())
171
+ levels[size.rank] = Math.max(levels[size.rank] ?? 0, size.along);
172
+ const offsets = [];
173
+ levels.forEach((value, depth) => {
174
+ offsets[depth] = depth
175
+ ? offsets[depth - 1] +
176
+ levels[depth - 1] +
177
+ Math.max(direction === 'lr' ? 240 : 104, (afterRank[depth - 1] ?? 0) + (beforeRank[depth] ?? 0) + 32)
178
+ : 34;
179
+ });
180
+ /**
181
+ * 按层轮廓压紧子树,首条主线共轴,后续分支只在相交层横向让位;重复和关联步骤已在外层处理。
182
+ */
183
+ const pack = (step) => {
184
+ const size = sizes.get(step.id);
185
+ const item = nodes.find((n) => n.step.id === step.id);
186
+ const half = (direction === 'tb' ? item.position.width : item.position.height) / 2;
187
+ const result = {
188
+ centers: new Map([[step.id, 0]]),
189
+ contour: new Map([[size.rank, { left: -half, right: size.cross - half }]]),
190
+ };
191
+ const children = step.children.filter((child) => !child.repeated && child.mode !== 'association');
192
+ for (const [index, child] of children.entries()) {
193
+ const subtree = pack(child);
194
+ let shift = 0;
195
+ if (index > 0)
196
+ for (const [rank, bounds] of subtree.contour) {
197
+ const occupied = result.contour.get(rank);
198
+ if (occupied)
199
+ shift = Math.max(shift, occupied.right + 64 - bounds.left);
200
+ }
201
+ for (const [id, center] of subtree.centers)
202
+ result.centers.set(id, center + shift);
203
+ for (const [rank, bounds] of subtree.contour) {
204
+ const occupied = result.contour.get(rank);
205
+ result.contour.set(rank, {
206
+ left: Math.min(occupied?.left ?? Infinity, bounds.left + shift),
207
+ right: Math.max(occupied?.right ?? -Infinity, bounds.right + shift),
208
+ });
209
+ }
210
+ }
211
+ return result;
212
+ };
213
+ const packed = pack(root);
214
+ const origin = 34 - Math.min(...[...packed.contour.values()].map((bounds) => bounds.left));
215
+ for (const item of nodes) {
216
+ const size = sizes.get(item.step.id);
217
+ const depth = size.rank;
218
+ const cross = origin +
219
+ packed.centers.get(item.step.id) -
220
+ (direction === 'tb' ? item.position.width : item.position.height) / 2;
221
+ const nodeAlong = direction === 'tb' ? item.position.height : item.position.width;
222
+ const along = offsets[depth] + (levels[depth] - nodeAlong) / 2;
223
+ item.position.x = direction === 'tb' ? cross : along;
224
+ item.position.y = direction === 'tb' ? along : cross;
225
+ const associations = related.get(item.step.id);
226
+ const total = associations.reduce((n, a) => n + (direction === 'tb' ? a.position.height : a.position.width), 0) +
227
+ Math.max(0, associations.length - 1) * 36;
228
+ let position = offsets[depth] + (levels[depth] - total) / 2;
229
+ for (const association of associations) {
230
+ association.position.x =
231
+ direction === 'tb'
232
+ ? item.position.x + item.position.width + relatedGaps.get(item.step.id)
233
+ : position;
234
+ association.position.y =
235
+ direction === 'tb'
236
+ ? position
237
+ : item.position.y + item.position.height + relatedGaps.get(item.step.id);
238
+ position += (direction === 'tb' ? association.position.height : association.position.width) + 36;
239
+ edges.push({ from: item.step.id, to: association.step.id, step: association.step, loop: false });
240
+ }
241
+ }
242
+ nodes.push(...[...related.values()].flat());
243
+ // 后续路径从侧面独立引出,不能与主线共用出口或与其他取消路径共用长线。
244
+ // 不相交的区间可以复用外侧通道,避免展开分支后图谱无限变宽。
245
+ const byId = new Map(nodes.map((node) => [node.step.id, node.position]));
246
+ const lanes = [];
247
+ for (const edge of side) {
248
+ const a = byId.get(edge.from), b = byId.get(edge.to);
249
+ const start = direction === 'tb'
250
+ ? a.y + a.height + (edge.routing.sourceOffset ?? 0)
251
+ : a.x + a.width + (edge.routing.sourceOffset ?? 0);
252
+ const end = direction === 'tb'
253
+ ? b.y - 24 - edge.routing.targetLane * 14
254
+ : b.x - 24 - edge.routing.targetLane * 14;
255
+ const span = { start: Math.min(start, end) - 30, end: Math.max(start, end) + 30 };
256
+ let lane = lanes.findIndex((spans) => spans.every((s) => s.end < span.start || s.start > span.end));
257
+ if (lane < 0) {
258
+ lane = lanes.length;
259
+ lanes.push([]);
260
+ }
261
+ lanes[lane].push(span);
262
+ edge.routing.lane = lane;
263
+ }
264
+ return {
265
+ nodes,
266
+ edges,
267
+ width: Math.max(...nodes.map((n) => n.position.x + n.position.width)) + 50,
268
+ height: Math.max(...nodes.map((n) => n.position.y + n.position.height)) + 50,
269
+ };
270
+ }
271
+ /**
272
+ * 缩短已知触发链标签以适应连线,未知业务标题原样保留,不据文案推断执行类别。
273
+ */
274
+ const shortEdgeLabel = (label) => ({
275
+ '成功后 · 同一事务': '同一事务',
276
+ '成功后 · 异步处理': '异步处理',
277
+ '成功后 · 异步启动审批': '异步启动',
278
+ '成功后 · 异步外部同步': '异步同步',
279
+ 审批通过时: '通过',
280
+ 审批驳回时: '驳回',
281
+ 外部调用成功后回写: '成功回写',
282
+ 成功后产生的结果: '产生结果',
283
+ 任务声明可调用: '按任务条件调用',
284
+ })[label] ?? label;
285
+ /** 连线几何从实时节点位置生成;手动移动节点后,箭头与标签共同跟随。 */
286
+ /**
287
+ * 根据实时矩形计算正交折线、标签锚点和辅助方向箭头所需路径点。
288
+ * @param a 起点矩形。
289
+ * @param b 终点矩形。
290
+ * @param direction 连线主方向。
291
+ * @param loop 是否回路。
292
+ * @param obstacles 需避让的节点矩形。
293
+ * @param routing 可选预分配分支通道。
294
+ * @returns 路径与标签坐标;仅计算几何,不承诺任意拖动后全图最优布线。
295
+ */
296
+ export function flowEdgeGeometry(a, b, direction, loop = false, obstacles = [], routing) {
297
+ let path, labelX, labelY;
298
+ let points;
299
+ if (direction === 'lr') {
300
+ const forward = b.x + b.width / 2 >= a.x + a.width / 2;
301
+ const x1 = a.x + (forward ? a.width : 0), x2 = b.x + (forward ? 0 : b.width);
302
+ const y1 = a.y + a.height / 2, y2 = b.y + b.height / 2;
303
+ const middle = (x1 + x2) / 2;
304
+ path = `M${x1},${y1} H${middle} V${y2} H${x2}`;
305
+ points = [
306
+ { x: x1, y: y1 },
307
+ { x: middle, y: y1 },
308
+ { x: middle, y: y2 },
309
+ { x: x2, y: y2 },
310
+ ];
311
+ labelX = middle;
312
+ labelY = y2 - 12;
313
+ }
314
+ else {
315
+ const forward = b.y + b.height / 2 >= a.y + a.height / 2;
316
+ const x1 = a.x + a.width / 2, x2 = b.x + b.width / 2;
317
+ const y1 = a.y + (forward ? a.height : 0), y2 = b.y + (forward ? 0 : b.height);
318
+ const middle = (y1 + y2) / 2;
319
+ path = `M${x1},${y1} V${middle} H${x2} V${y2}`;
320
+ points = [
321
+ { x: x1, y: y1 },
322
+ { x: x1, y: middle },
323
+ { x: x2, y: middle },
324
+ { x: x2, y: y2 },
325
+ ];
326
+ labelX = x2;
327
+ labelY = middle - 24;
328
+ }
329
+ const crosses = (points, box) => points.some((point, i) => {
330
+ if (!i)
331
+ return false;
332
+ const previous = points[i - 1];
333
+ return point.x === previous.x
334
+ ? point.x > box.x &&
335
+ point.x < box.x + box.width &&
336
+ Math.max(point.y, previous.y) > box.y &&
337
+ Math.min(point.y, previous.y) < box.y + box.height
338
+ : point.y > box.y &&
339
+ point.y < box.y + box.height &&
340
+ Math.max(point.x, previous.x) > box.x &&
341
+ Math.min(point.x, previous.x) < box.x + box.width;
342
+ });
343
+ if (routing) {
344
+ const boxes = [...obstacles, a, b];
345
+ if (direction === 'tb') {
346
+ const channel = Math.min(...boxes.map((p) => p.x)) - 190 - routing.lane * 28;
347
+ const source = { x: a.x, y: a.y + a.height * routing.sourcePort };
348
+ const target = { x: b.x + b.width * routing.targetPort, y: b.y };
349
+ if (routing.sourceDecision)
350
+ source.x += (Math.abs(source.y - a.y - a.height / 2) * a.width) / a.height;
351
+ if (routing.targetDecision)
352
+ target.y += (Math.abs(target.x - b.x - b.width / 2) * b.height) / b.width;
353
+ const row = boxes.filter((p) => p.y < b.y + b.height && p.y + p.height > b.y);
354
+ const entry = Math.min(...row.map((p) => p.y)) - 24 - routing.targetLane * 14;
355
+ const sourceRow = boxes.filter((p) => p.y < a.y + a.height && p.y + p.height > a.y);
356
+ const exit = routing.sourceOffset === undefined
357
+ ? source.y
358
+ : Math.max(...sourceRow.map((p) => p.y + p.height)) + routing.sourceOffset;
359
+ const knee = a.x - 22 - routing.sourcePort * 26;
360
+ points =
361
+ routing.sourceOffset === undefined
362
+ ? [source, { x: channel, y: exit }]
363
+ : [source, { x: knee, y: source.y }, { x: knee, y: exit }, { x: channel, y: exit }];
364
+ if (boxes.some((p) => p !== a && p !== b && crosses(points, p))) {
365
+ const row = boxes.filter((p) => p.y < a.y + a.height && p.y + p.height > a.y);
366
+ const exit = Math.max(...row.map((p) => p.y + p.height)) + Math.max(72, routing.sourceOffset ?? 0);
367
+ points = [
368
+ { x: a.x + a.width / 2, y: a.y + a.height },
369
+ { x: a.x + a.width / 2, y: exit },
370
+ { x: channel, y: exit },
371
+ ];
372
+ }
373
+ const captionStart = points[points.length - 2], captionEnd = points[points.length - 1];
374
+ points.push({ x: channel, y: entry }, { x: target.x, y: entry }, target);
375
+ path = points.map((p, i) => `${i ? 'L' : 'M'}${p.x},${p.y}`).join(' ');
376
+ labelX = (captionStart.x + captionEnd.x) / 2;
377
+ labelY = captionEnd.y;
378
+ }
379
+ else {
380
+ const channel = Math.min(...boxes.map((p) => p.y)) - 150 - routing.lane * 28;
381
+ const source = { x: a.x + a.width * routing.sourcePort, y: a.y };
382
+ const target = { x: b.x, y: b.y + b.height * routing.targetPort };
383
+ if (routing.sourceDecision)
384
+ source.y += (Math.abs(source.x - a.x - a.width / 2) * a.height) / a.width;
385
+ if (routing.targetDecision)
386
+ target.x += (Math.abs(target.y - b.y - b.height / 2) * b.width) / b.height;
387
+ const row = boxes.filter((p) => p.x < b.x + b.width && p.x + p.width > b.x);
388
+ const entry = Math.min(...row.map((p) => p.x)) - 24 - routing.targetLane * 14;
389
+ const sourceRow = boxes.filter((p) => p.x < a.x + a.width && p.x + p.width > a.x);
390
+ const exit = routing.sourceOffset === undefined
391
+ ? source.x
392
+ : Math.max(...sourceRow.map((p) => p.x + p.width)) + routing.sourceOffset;
393
+ const knee = a.y - 22 - routing.sourcePort * 26;
394
+ points =
395
+ routing.sourceOffset === undefined
396
+ ? [source, { x: exit, y: channel }]
397
+ : [source, { x: source.x, y: knee }, { x: exit, y: knee }, { x: exit, y: channel }];
398
+ if (boxes.some((p) => p !== a && p !== b && crosses(points, p))) {
399
+ const row = boxes.filter((p) => p.x < a.x + a.width && p.x + p.width > a.x);
400
+ const exit = Math.max(...row.map((p) => p.x + p.width)) + 24;
401
+ points = [
402
+ { x: a.x + a.width, y: a.y + a.height / 2 },
403
+ { x: exit, y: a.y + a.height / 2 },
404
+ { x: exit, y: channel },
405
+ ];
406
+ }
407
+ const captionStart = points[points.length - 2], captionEnd = points[points.length - 1];
408
+ points.push({ x: entry, y: channel }, { x: entry, y: target.y }, target);
409
+ path = points.map((p, i) => `${i ? 'L' : 'M'}${p.x},${p.y}`).join(' ');
410
+ labelX = captionEnd.x;
411
+ labelY = (captionStart.y + captionEnd.y) / 2;
412
+ }
413
+ return { path, labelX, labelY, points, side: true };
414
+ }
415
+ const blocked = obstacles.some((box) => box !== a && box !== b && crosses(points, box));
416
+ if (loop || blocked) {
417
+ // 回路与跨层连线绕到图外,进出横线使用该层的空隙,避免穿过主线和侧面对象。
418
+ const boxes = [...obstacles, a, b];
419
+ if (direction === 'tb') {
420
+ const down = loop || b.y + b.height / 2 >= a.y + a.height / 2;
421
+ const sourceRow = boxes.filter((p) => p.y < a.y + a.height && p.y + p.height > a.y);
422
+ const targetRow = boxes.filter((p) => p.y < b.y + b.height && p.y + p.height > b.y);
423
+ const exit = down
424
+ ? Math.max(...sourceRow.map((p) => p.y + p.height)) + 26
425
+ : Math.min(...sourceRow.map((p) => p.y)) - 26;
426
+ const entry = down
427
+ ? Math.min(...targetRow.map((p) => p.y)) - 26
428
+ : Math.max(...targetRow.map((p) => p.y + p.height)) + 26;
429
+ const side = Math.min(...boxes.map((p) => p.x)) - 48;
430
+ const x1 = a.x + a.width / 2, x2 = b.x + b.width / 2;
431
+ const y1 = a.y + (down ? a.height : 0), y2 = b.y + (down ? 0 : b.height);
432
+ path = `M${x1},${y1} V${exit} H${side} V${entry} H${x2} V${y2}`;
433
+ points = [
434
+ { x: x1, y: y1 },
435
+ { x: x1, y: exit },
436
+ { x: side, y: exit },
437
+ { x: side, y: entry },
438
+ { x: x2, y: entry },
439
+ { x: x2, y: y2 },
440
+ ];
441
+ labelX = (x1 + side) / 2;
442
+ labelY = exit;
443
+ }
444
+ else {
445
+ const right = loop || b.x + b.width / 2 >= a.x + a.width / 2;
446
+ const sourceRow = boxes.filter((p) => p.x < a.x + a.width && p.x + p.width > a.x);
447
+ const targetRow = boxes.filter((p) => p.x < b.x + b.width && p.x + p.width > b.x);
448
+ const exit = right
449
+ ? Math.max(...sourceRow.map((p) => p.x + p.width)) + 26
450
+ : Math.min(...sourceRow.map((p) => p.x)) - 26;
451
+ const entry = right
452
+ ? Math.min(...targetRow.map((p) => p.x)) - 26
453
+ : Math.max(...targetRow.map((p) => p.x + p.width)) + 26;
454
+ const side = Math.min(...boxes.map((p) => p.y)) - 48;
455
+ const y1 = a.y + a.height / 2, y2 = b.y + b.height / 2;
456
+ const x1 = a.x + (right ? a.width : 0), x2 = b.x + (right ? 0 : b.width);
457
+ path = `M${x1},${y1} H${exit} V${side} H${entry} V${y2} H${x2}`;
458
+ points = [
459
+ { x: x1, y: y1 },
460
+ { x: exit, y: y1 },
461
+ { x: exit, y: side },
462
+ { x: entry, y: side },
463
+ { x: entry, y: y2 },
464
+ { x: x2, y: y2 },
465
+ ];
466
+ labelX = exit;
467
+ labelY = (y1 + side) / 2;
468
+ }
469
+ }
470
+ return { path, labelX, labelY, points, side: loop || blocked };
471
+ }
472
+ /** 箭头同时出现在终点与长线中段,目标在画布之外时也能看出走向。 */
473
+ /**
474
+ * 为首个足够长的线段补方向三角形,让目标在视口外也能看见走向。
475
+ * @param points 按路径顺序排列的画布点。
476
+ * @returns SVG polygon 点串,无长度至少 140 的线段则为空。
477
+ */
478
+ export function flowDirectionArrow(points) {
479
+ let length = 0, start, end;
480
+ for (let i = 1; i < points.length; i++) {
481
+ length = Math.hypot(points[i].x - points[i - 1].x, points[i].y - points[i - 1].y);
482
+ if (length >= 140) {
483
+ start = points[i - 1];
484
+ end = points[i];
485
+ break;
486
+ }
487
+ }
488
+ if (!start || !end)
489
+ return '';
490
+ const dx = (end.x - start.x) / length, dy = (end.y - start.y) / length;
491
+ // 箭头离源节点最多 90 个画布单位;终点和长通道在屏幕外时仍能看见方向。
492
+ const distance = Math.min(90, length / 2);
493
+ const x = start.x + dx * distance, y = start.y + dy * distance;
494
+ return `${x},${y} ${x - dx * 12 - dy * 6},${y - dy * 12 + dx * 6} ${x - dx * 12 + dy * 6},${y - dy * 12 - dx * 6}`;
495
+ }
496
+ /**
497
+ * 创建交互流程图,复用调用方保存的视野、位置和工具状态。
498
+ * @param root 阅读流程根,不代表运行时流程实例。
499
+ * @param options 名称/摘要、方向、选择回调、共享状态和审批子图。
500
+ * @returns element、mount、dispose;挂载后安装 resize 与全局键盘监听,移除前必须 dispose 清理。
501
+ * 拖动和展开仅改变阅读状态,点击回调由页面更新 URL,组件不执行任何领域命令。
502
+ */
503
+ export function createFlowChart(root, options) {
504
+ const ns = 'http://www.w3.org/2000/svg';
505
+ const vector = (tag, attrs = {}, text = '') => {
506
+ const node = document.createElementNS(ns, tag);
507
+ for (const [key, value] of Object.entries(attrs))
508
+ node.setAttribute(key, String(value));
509
+ node.textContent = text;
510
+ return node;
511
+ };
512
+ const host = document.createElement('section');
513
+ host.className = 'business-flowchart';
514
+ host.setAttribute('aria-label', '业务流程图');
515
+ const toolbar = document.createElement('div');
516
+ toolbar.className = 'flowchart-toolbar';
517
+ const title = document.createElement('strong');
518
+ title.textContent = '业务流程图';
519
+ const controls = document.createElement('div');
520
+ controls.className = 'flowchart-controls';
521
+ const button = (text, label, run, parent = controls) => {
522
+ const node = document.createElement('button');
523
+ node.textContent = text;
524
+ node.title = label;
525
+ node.setAttribute('aria-label', label);
526
+ node.onclick = run;
527
+ parent.append(node);
528
+ return node;
529
+ };
530
+ toolbar.append(title, controls);
531
+ const canvas = document.createElement('div');
532
+ canvas.className = 'flowchart-canvas';
533
+ const svg = vector('svg', { role: 'group', 'aria-label': '可点击的流程节点与方向箭头', tabindex: 0 });
534
+ const defs = vector('defs');
535
+ for (const [id, color] of [
536
+ ['flow-arrow', '#537f60'],
537
+ ['flow-arrow-selected', '#397a56'],
538
+ ['flow-arrow-branch', '#a57950'],
539
+ ['flow-arrow-related', '#8ba28e'],
540
+ ]) {
541
+ const marker = vector('marker', {
542
+ id,
543
+ viewBox: '0 0 10 10',
544
+ refX: 9,
545
+ refY: 5,
546
+ markerWidth: id === 'flow-arrow-related' ? 8 : 14,
547
+ markerHeight: id === 'flow-arrow-related' ? 8 : 14,
548
+ markerUnits: 'userSpaceOnUse',
549
+ orient: 'auto-start-reverse',
550
+ });
551
+ marker.append(vector('path', { d: 'M0 0L10 5L0 10z', fill: color }));
552
+ defs.append(marker);
553
+ }
554
+ const viewport = vector('g');
555
+ svg.append(defs, viewport);
556
+ canvas.append(svg);
557
+ const layout = layoutBusinessFlow(root, options.name, options.state, options.direction, options.approvals);
558
+ const obstacles = layout.nodes.map((n) => n.position);
559
+ const defaults = new Map(layout.nodes.map((n) => [n.step.id, { x: n.position.x, y: n.position.y }]));
560
+ for (const item of layout.nodes)
561
+ Object.assign(item.position, options.view.positions.get(item.step.id));
562
+ const byId = new Map(layout.nodes.map((n) => [n.step.id, n]));
563
+ const targets = new Map();
564
+ const nodeElements = new Map();
565
+ const selectedEdge = layout.edges.find((edge) => edge.step.id === options.selected);
566
+ const selectedItem = options.selected && (byId.get(options.selected) ?? (selectedEdge && byId.get(selectedEdge.to)));
567
+ const emphasize = (edge) => {
568
+ for (const [id, node] of nodeElements) {
569
+ node.classList.toggle('path-source', id === edge?.from);
570
+ node.classList.toggle('path-target', id === edge?.to);
571
+ }
572
+ };
573
+ const edgeViews = [];
574
+ for (const [index, edge] of layout.edges.entries()) {
575
+ const a = byId.get(edge.from).position, b = byId.get(edge.to).position;
576
+ const active = edge === selectedEdge;
577
+ const group = vector('g', {
578
+ class: `flow-edge${active ? ' selected' : ''}${!options.readable && edge.step.label.includes('异步') ? ' async' : ''}${edge.step.mode === 'association' ? ' association' : ''}${edge.step.branch ? ' branch' : ''}`,
579
+ role: 'button',
580
+ tabindex: 0,
581
+ 'aria-label': `${options.name(byId.get(edge.from).step)} → ${options.name(edge.step)}:${edge.step.label}`,
582
+ 'data-flow-target': `edge:${index}`,
583
+ });
584
+ const edgeDirection = edge.step.mode === 'association' ? (options.direction === 'tb' ? 'lr' : 'tb') : options.direction;
585
+ const geometry = flowEdgeGeometry(a, b, edgeDirection, edge.loop, obstacles, edge.routing);
586
+ const approval = byId.get(edge.from).approval;
587
+ const rejection = approval && isApprovalRejection(approval.flow, edge.step);
588
+ const actor = byId.get(edge.to).step.node.attrs.actor;
589
+ const caption = (rejection ? '任一步骤驳回' : shortEdgeLabel(edge.step.label)).replace(/^人工 · /, actor ? `${actor} · ` : '人工 · ');
590
+ const captionLines = [
591
+ ...wrap(caption, edge.routing ? 14 : 20),
592
+ ...(edge.routing
593
+ ? wrap(`${edge.loop ? '回到' : '到'}:${options.name(byId.get(edge.to).step)}`, 14)
594
+ : []),
595
+ ];
596
+ const width = Math.max(...captionLines.map((line) => [...line].reduce((length, char) => length + (/[\u0000-\u00ff]/.test(char) ? 0.55 : 1), 0))) *
597
+ 11 +
598
+ 20;
599
+ const height = captionLines.length * 14 + 6;
600
+ /**
601
+ * 根据侧绕或普通连线选择标签位置,使长分支说明留在相应出口附近;拖动后用新几何重新计算。
602
+ */
603
+ const captionPosition = (geometry) => {
604
+ if (geometry.side)
605
+ return edgeDirection === 'tb'
606
+ ? { x: geometry.labelX, y: geometry.labelY - height / 2 - 9 }
607
+ : {
608
+ x: geometry.labelX + (edge.routing?.sourceOffset === undefined ? 1 : -1) * (width / 2 + 12),
609
+ y: geometry.labelY,
610
+ };
611
+ const start = geometry.points[0], end = geometry.points[geometry.points.length - 1];
612
+ return edgeDirection === 'tb'
613
+ ? { x: Math.max(start.x, end.x) + width / 2 + 12, y: (start.y + end.y) / 2 }
614
+ : { x: (start.x + end.x) / 2, y: Math.min(start.y, end.y) - height / 2 - 9 };
615
+ };
616
+ const position = captionPosition(geometry);
617
+ const marker = active
618
+ ? 'flow-arrow-selected'
619
+ : edge.step.mode === 'association'
620
+ ? 'flow-arrow-related'
621
+ : edge.step.branch
622
+ ? 'flow-arrow-branch'
623
+ : 'flow-arrow';
624
+ const hit = vector('path', { d: geometry.path, class: 'flow-edge-hit' });
625
+ const line = vector('path', {
626
+ d: geometry.path,
627
+ class: 'flow-edge-line',
628
+ 'marker-end': `url(#${marker})`,
629
+ });
630
+ const arrow = vector('polygon', {
631
+ points: edge.step.mode === 'association' ? '' : flowDirectionArrow(geometry.points),
632
+ class: 'flow-edge-direction',
633
+ 'aria-hidden': 'true',
634
+ });
635
+ const captionGroup = vector('g', { transform: `translate(${position.x},${position.y})` });
636
+ const box = vector('rect', {
637
+ x: -width / 2,
638
+ y: -height / 2,
639
+ width,
640
+ height,
641
+ rx: 4,
642
+ class: 'flow-edge-label-box',
643
+ });
644
+ const label = vector('text', { 'text-anchor': 'middle', class: 'flow-edge-label' });
645
+ captionLines.forEach((text, index) => label.append(vector('tspan', { x: 0, y: 4 + (index - (captionLines.length - 1) / 2) * 14 }, text)));
646
+ captionGroup.append(box, label);
647
+ const arrival = edge.loop ? vector('g', { class: 'flow-edge-arrival' }) : undefined;
648
+ if (arrival) {
649
+ const text = `来自:${options.name(byId.get(edge.from).step)}`;
650
+ const lines = wrap(text, 18);
651
+ const width = Math.max(...lines.map((line) => [...line].length)) * 11 + 16;
652
+ const height = lines.length * 14 + 6;
653
+ arrival.append(vector('rect', { x: -width / 2, y: -height / 2, width, height, rx: 4, class: 'flow-edge-label-box' }));
654
+ const label = vector('text', { 'text-anchor': 'middle', class: 'flow-edge-label' });
655
+ lines.forEach((line, index) => label.append(vector('tspan', { x: 0, y: 4 + (index - (lines.length - 1) / 2) * 14 }, line)));
656
+ arrival.append(label);
657
+ }
658
+ /**
659
+ * 回路终点额外标注来源;根据最后折线路段方向偏移标签,避免与终点端口重叠。
660
+ */
661
+ const moveArrival = (geometry) => {
662
+ if (!arrival)
663
+ return;
664
+ const a = geometry.points[geometry.points.length - 3], b = geometry.points[geometry.points.length - 2];
665
+ arrival.setAttribute('transform', edgeDirection === 'tb'
666
+ ? `translate(${(a.x + b.x) / 2},${b.y - 20})`
667
+ : `translate(${b.x + 30},${(a.y + b.y) / 2})`);
668
+ };
669
+ moveArrival(geometry);
670
+ group.append(vector('title', {}, `从 ${options.name(byId.get(edge.from).step)} 到 ${options.name(byId.get(edge.to).step)}:${edge.step.label}${edge.step.note ? `。${edge.step.note}` : ''}`), hit, line, arrow, captionGroup);
671
+ if (arrival)
672
+ group.append(arrival);
673
+ edgeViews.push({
674
+ edge,
675
+ redraw: () => {
676
+ const geometry = flowEdgeGeometry(a, b, edgeDirection, edge.loop, obstacles, edge.routing);
677
+ const position = captionPosition(geometry);
678
+ hit.setAttribute('d', geometry.path);
679
+ line.setAttribute('d', geometry.path);
680
+ arrow.setAttribute('points', edge.step.mode === 'association' ? '' : flowDirectionArrow(geometry.points));
681
+ moveArrival(geometry);
682
+ captionGroup.setAttribute('transform', `translate(${position.x},${position.y})`);
683
+ },
684
+ });
685
+ const targetStep = edge.step;
686
+ group.onpointerenter = () => emphasize(edge);
687
+ group.onpointerleave = () => emphasize(selectedEdge);
688
+ group.onfocus = () => emphasize(edge);
689
+ group.onblur = () => emphasize(selectedEdge);
690
+ targets.set(`edge:${index}`, targetStep);
691
+ group.onclick = (event) => {
692
+ if (event.detail === 0)
693
+ options.select(targetStep);
694
+ };
695
+ group.onkeydown = (event) => {
696
+ if (event.key === 'Enter' || event.key === ' ') {
697
+ event.preventDefault();
698
+ event.stopPropagation();
699
+ options.select(targetStep);
700
+ }
701
+ };
702
+ viewport.append(group);
703
+ }
704
+ for (const item of layout.nodes) {
705
+ const { step, position: p, decision } = item;
706
+ const group = vector('g', {
707
+ class: `flow-node${step.id === options.selected ? ' selected' : ''}${decision ? ' decision' : ''}${item.association ? ' association' : ''}${item.approval ? ' approval-subflow' : ''}`,
708
+ transform: `translate(${p.x},${p.y})`,
709
+ role: 'button',
710
+ tabindex: 0,
711
+ 'aria-label': `${item.approval ? '审批子流程' : decision ? '审批分支' : '业务步骤'} ${options.name(step)},点击查看说明与代码`,
712
+ 'data-step': step.id,
713
+ 'data-flow-target': step.id,
714
+ 'aria-pressed': String(step.id === options.selected),
715
+ });
716
+ group.append(vector('title', {}, `${options.name(step)}\n${step.note}\n点击查看业务说明与实现代码`));
717
+ if (decision)
718
+ group.append(vector('path', {
719
+ d: `M${p.width / 2},0 L${p.width},${p.height / 2} L${p.width / 2},${p.height} L0,${p.height / 2}Z`,
720
+ class: 'flow-node-shape',
721
+ }));
722
+ else
723
+ group.append(vector('rect', {
724
+ width: p.width,
725
+ height: p.height,
726
+ rx: step.node.kind === 'event' || step.node.attrs.stageKind === 'end' || step.node.attrs.terminal
727
+ ? 32
728
+ : 9,
729
+ class: 'flow-node-shape',
730
+ }));
731
+ if (item.approval)
732
+ group.append(renderApprovalFlow(item.approval));
733
+ const textY = decision ? p.height / 2 - (item.title.length - 1) * 11 + 4 : 34;
734
+ if (!item.approval)
735
+ item.title.forEach((line, index) => group.append(vector('text', { x: p.width / 2, y: textY + index * 22, 'text-anchor': 'middle', class: 'flow-node-title' }, line)));
736
+ if (!decision && !item.approval)
737
+ item.state.forEach((line, index) => group.append(vector('text', {
738
+ x: p.width / 2,
739
+ y: 34 + item.title.length * 22 + index * 16,
740
+ 'text-anchor': 'middle',
741
+ class: 'flow-node-state',
742
+ }, line)));
743
+ if (step.boundary)
744
+ group.append(vector('text', { x: p.width / 2, y: p.height - 10, 'text-anchor': 'middle', class: 'flow-node-boundary' }, '后续已收起 · 点击继续查看'));
745
+ targets.set(step.id, step);
746
+ nodeElements.set(step.id, group);
747
+ group.onclick = (event) => {
748
+ if (event.detail === 0)
749
+ options.select(step);
750
+ };
751
+ group.onkeydown = (event) => {
752
+ if (event.key === 'Enter' || event.key === ' ') {
753
+ event.preventDefault();
754
+ event.stopPropagation();
755
+ options.select(step);
756
+ }
757
+ };
758
+ viewport.append(group);
759
+ }
760
+ emphasize(selectedEdge);
761
+ const view = options.view, camera = view.camera;
762
+ const zoomControls = document.createElement('div');
763
+ zoomControls.className = 'flowchart-zoom';
764
+ zoomControls.setAttribute('role', 'toolbar');
765
+ zoomControls.setAttribute('aria-label', '画布缩放与布局');
766
+ canvas.append(zoomControls);
767
+ const shrink = button('−', '缩小流程图', () => zoom(1 / 1.2), zoomControls);
768
+ const percentage = button('100%', '还原100%缩放', () => zoom(1 / camera.scale), zoomControls);
769
+ percentage.className = 'flowchart-percentage';
770
+ const grow = button('+', '放大流程图', () => zoom(1.2), zoomControls);
771
+ button('适应', '适应流程画布', () => fit(), zoomControls);
772
+ button('重排', '重置节点布局', () => {
773
+ view.positions.clear();
774
+ for (const item of layout.nodes) {
775
+ Object.assign(item.position, defaults.get(item.step.id));
776
+ nodeElements
777
+ .get(item.step.id)
778
+ .setAttribute('transform', `translate(${item.position.x},${item.position.y})`);
779
+ }
780
+ for (const edge of edgeViews)
781
+ edge.redraw();
782
+ fit();
783
+ }, zoomControls);
784
+ /**
785
+ * 将共享相机同步到 SVG 变换、缩放控件和网格背景;不更改节点业务坐标。
786
+ */
787
+ const apply = () => {
788
+ viewport.setAttribute('transform', `translate(${camera.x},${camera.y}) scale(${camera.scale})`);
789
+ percentage.textContent = `${Math.round(camera.scale * 100)}%`;
790
+ percentage.title = `当前 ${percentage.textContent} · 点击还原 100%`;
791
+ shrink.disabled = camera.scale <= MIN_FLOW_ZOOM;
792
+ grow.disabled = camera.scale >= MAX_FLOW_ZOOM;
793
+ const grid = Math.max(10, 24 * camera.scale);
794
+ canvas.style.backgroundSize = `${grid}px ${grid}px`;
795
+ canvas.style.backgroundPosition = `${camera.x}px ${camera.y}px`;
796
+ };
797
+ /**
798
+ * 读取已挂载 SVG 的实际内容边界并适应可用区域,保留外侧标签空间;调用会主动重置相机。
799
+ */
800
+ const fit = () => {
801
+ // 手动移动后的节点也必须进入“适应”范围,包括移到原点左侧的节点。
802
+ const bounds = viewport.getBBox();
803
+ const left = bounds.x - 36, top = bounds.y - 36;
804
+ const width = bounds.width + 106, height = bounds.height + 106;
805
+ camera.scale = Math.max(MIN_FLOW_ZOOM, Math.min(1, (canvas.clientWidth - 32) / width, (canvas.clientHeight - 72) / height));
806
+ camera.x = (canvas.clientWidth - width * camera.scale) / 2 - left * camera.scale;
807
+ camera.y = (canvas.clientHeight - height * camera.scale) / 2 - top * camera.scale - 16;
808
+ camera.initialized = true;
809
+ camera.width = canvas.clientWidth;
810
+ camera.height = canvas.clientHeight;
811
+ apply();
812
+ };
813
+ /**
814
+ * 以固定可读倍率定位流程起点,适合长业务流程;记录画布尺寸供后续 resize 保持相对视野。
815
+ */
816
+ const readStart = () => {
817
+ const start = byId.get(root.id).position;
818
+ camera.scale = 0.9;
819
+ camera.x =
820
+ options.direction === 'tb'
821
+ ? canvas.clientWidth / 2 - (start.x + start.width / 2) * camera.scale
822
+ : 36 - start.x * camera.scale;
823
+ camera.y =
824
+ options.direction === 'tb'
825
+ ? 30 - start.y * camera.scale
826
+ : canvas.clientHeight / 2 - (start.y + start.height / 2) * camera.scale;
827
+ camera.initialized = true;
828
+ camera.width = canvas.clientWidth;
829
+ camera.height = canvas.clientHeight;
830
+ apply();
831
+ };
832
+ if (options.readable)
833
+ button('起点', '以可读字号回到流程起点', readStart, zoomControls);
834
+ // 保存画布尺寸而非 DOM 实例:切到其他视图时仍可能调整窗口大小。
835
+ /**
836
+ * 容器尺寸变化时按中心差平移并露出选中项,保留倍率与手动位置,不重新执行完整布局。
837
+ */
838
+ const resize = () => {
839
+ const width = canvas.clientWidth, height = canvas.clientHeight;
840
+ if (camera.width === width && camera.height === height)
841
+ return;
842
+ camera.x += (width - (camera.width ?? width)) / 2;
843
+ camera.y += (height - (camera.height ?? height)) / 2;
844
+ camera.width = width;
845
+ camera.height = height;
846
+ if (selectedItem)
847
+ revealFlowPosition(camera, selectedItem.position);
848
+ apply();
849
+ };
850
+ /**
851
+ * 以指定屏幕点按相对倍率缩放,缺省锚点为画布中心;共用限幅函数并立即刷新显示。
852
+ */
853
+ const zoom = (factor, x = canvas.clientWidth / 2, y = canvas.clientHeight / 2) => {
854
+ zoomFlowCamera(camera, camera.scale * factor, { x, y });
855
+ apply();
856
+ };
857
+ let space = false;
858
+ const tools = new Map();
859
+ /**
860
+ * 把持久工具选择和临时空格抓手同步到光标及 aria 状态,临时抓手不覆盖保存的工具。
861
+ */
862
+ const updateTool = () => {
863
+ canvas.classList.toggle('mode-pan', view.tool === 'pan' || space);
864
+ for (const [tool, node] of tools)
865
+ node.setAttribute('aria-pressed', String(view.tool === tool));
866
+ };
867
+ for (const [tool, text] of [
868
+ ['select', '选择'],
869
+ ['pan', '抓手'],
870
+ ]) {
871
+ tools.set(tool, button(text, tool === 'select' ? '选择或拖动节点' : '抓手:拖动任意位置平移', () => {
872
+ view.tool = tool;
873
+ updateTool();
874
+ }));
875
+ }
876
+ for (const direction of ['tb', 'lr']) {
877
+ const node = button(direction === 'tb' ? '↓ 纵向' : '→ 横向', direction === 'tb' ? '从上到下排列流程' : '从左到右排列流程', () => options.directionChanged(direction));
878
+ node.setAttribute('aria-pressed', String(options.direction === direction));
879
+ }
880
+ const expanded = button('展开画布', '展开画布', () => expand(!view.expanded));
881
+ /**
882
+ * 同步工作区展开样式和按钮可访问名称,尺寸改变交给 ResizeObserver 处理。
883
+ */
884
+ const expand = (value) => {
885
+ view.expanded = value;
886
+ host.parentElement?.classList.toggle('expanded', value);
887
+ expanded.textContent = value ? '退出展开' : '展开画布';
888
+ expanded.setAttribute('aria-label', value ? '退出展开画布' : '展开画布');
889
+ expanded.title = value ? '退出展开画布(Esc)' : '展开画布';
890
+ expanded.setAttribute('aria-pressed', String(value));
891
+ };
892
+ /**
893
+ * 把浏览器视窗坐标转换为相对 SVG 的屏幕像素,后续手势层再换算业务坐标。
894
+ */
895
+ const localPoint = (event) => {
896
+ const bounds = svg.getBoundingClientRect();
897
+ return { x: event.clientX - bounds.left, y: event.clientY - bounds.top };
898
+ };
899
+ const gestures = createFlowGestures(camera, {
900
+ position: (id) => byId.get(id).position,
901
+ move: (id, point) => {
902
+ Object.assign(byId.get(id).position, point);
903
+ view.positions.set(id, { ...point });
904
+ nodeElements.get(id).setAttribute('transform', `translate(${point.x},${point.y})`);
905
+ // 移动的节点也可能是别的连线需要避让的对象。
906
+ for (const edge of edgeViews)
907
+ edge.redraw();
908
+ },
909
+ activate: (target) => options.select(targets.get(target)),
910
+ changed: apply,
911
+ });
912
+ svg.addEventListener('pointerdown', (event) => {
913
+ if (event.button !== 0 && event.button !== 1)
914
+ return;
915
+ event.preventDefault();
916
+ svg.focus({ preventScroll: true });
917
+ const target = event.target.closest('[data-flow-target]');
918
+ gestures.start({
919
+ id: event.pointerId,
920
+ point: localPoint(event),
921
+ touch: event.pointerType === 'touch',
922
+ pan: view.tool === 'pan' || space || event.button === 1,
923
+ node: target?.classList.contains('flow-node') ? target.getAttribute('data-step') : undefined,
924
+ target: target?.getAttribute('data-flow-target') ?? undefined,
925
+ });
926
+ svg.setPointerCapture(event.pointerId);
927
+ svg.classList.add('interacting');
928
+ });
929
+ svg.addEventListener('pointermove', (event) => {
930
+ gestures.move(event.pointerId, localPoint(event));
931
+ });
932
+ /**
933
+ * 结束或取消捕获的指针;只有 pointerup 可触发点击,取消与失去捕获不能误开详情。
934
+ */
935
+ const stop = (event) => {
936
+ gestures.end(event.pointerId, event.type !== 'pointerup');
937
+ if (svg.hasPointerCapture(event.pointerId))
938
+ svg.releasePointerCapture(event.pointerId);
939
+ svg.classList.remove('interacting');
940
+ };
941
+ svg.addEventListener('pointerup', stop);
942
+ svg.addEventListener('pointercancel', stop);
943
+ svg.addEventListener('lostpointercapture', stop);
944
+ // 指针点击由手势结束统一处理;辅助技术的无指针 click 仍由节点响应。
945
+ svg.addEventListener('click', (event) => {
946
+ if (event.detail > 0)
947
+ event.stopPropagation();
948
+ }, true);
949
+ svg.addEventListener('dragstart', (event) => event.preventDefault());
950
+ canvas.addEventListener('wheel', (event) => {
951
+ event.preventDefault();
952
+ wheelFlowCamera(camera, event, localPoint(event));
953
+ apply();
954
+ }, { passive: false });
955
+ svg.addEventListener('keydown', (event) => {
956
+ if (event.target.closest('.flow-node,.flow-edge'))
957
+ return;
958
+ const move = {
959
+ ArrowLeft: { x: 50, y: 0 },
960
+ ArrowRight: { x: -50, y: 0 },
961
+ ArrowUp: { x: 0, y: 50 },
962
+ ArrowDown: { x: 0, y: -50 },
963
+ };
964
+ if (move[event.key]) {
965
+ event.preventDefault();
966
+ camera.x += move[event.key].x;
967
+ camera.y += move[event.key].y;
968
+ apply();
969
+ }
970
+ else if (['+', '=', '-', '0', '1'].includes(event.key)) {
971
+ event.preventDefault();
972
+ if (event.key === '0')
973
+ fit();
974
+ else
975
+ zoom(event.key === '1' ? 1 / camera.scale : event.key === '-' ? 1 / 1.2 : 1.2);
976
+ }
977
+ });
978
+ /**
979
+ * Esc 退出展开,空格仅在画布获得关注且不编辑文本/按钮时临时启用抓手;监听在 dispose 移除。
980
+ */
981
+ const keydown = (event) => {
982
+ if (event.key === 'Escape' && view.expanded) {
983
+ event.preventDefault();
984
+ expand(false);
985
+ }
986
+ if (event.code !== 'Space' ||
987
+ event.defaultPrevented ||
988
+ event.target.closest('button,input,textarea,[contenteditable],.flow-node,.flow-edge'))
989
+ return;
990
+ if (canvas.matches(':hover') || canvas.contains(document.activeElement)) {
991
+ event.preventDefault();
992
+ space = true;
993
+ updateTool();
994
+ }
995
+ };
996
+ /**
997
+ * 松开空格或窗口失焦时清除临时抓手,避免漏收 keyup 后画布持续处于平移模式。
998
+ */
999
+ const releaseSpace = () => {
1000
+ space = false;
1001
+ updateTool();
1002
+ };
1003
+ /**
1004
+ * 只响应空格释放,其余快捷键由画布自身监听处理。
1005
+ */
1006
+ const keyup = (event) => {
1007
+ if (event.code === 'Space')
1008
+ releaseSpace();
1009
+ };
1010
+ const footer = document.createElement('div');
1011
+ footer.className = 'flowchart-footer';
1012
+ const hint = document.createElement('span');
1013
+ hint.textContent = '滚轮缩放 · 空白处拖动平移 · 节点可拖动 · 按住空格使用抓手';
1014
+ const legend = document.createElement('span');
1015
+ legend.textContent = options.readable
1016
+ ? '箭头:下一步 棕线:展开的其他分支 虚线:涉及对象'
1017
+ : '箭头:后续处理 虚线:异步';
1018
+ footer.append(hint, legend);
1019
+ host.append(toolbar, canvas, footer);
1020
+ let observer;
1021
+ return {
1022
+ element: host,
1023
+ /**
1024
+ * 在 element 加入 DOM 后初始化视野和 ResizeObserver,再安装全局键盘/失焦监听;调用方每个实例只挂载一次。
1025
+ */
1026
+ mount() {
1027
+ expand(view.expanded);
1028
+ updateTool();
1029
+ if (!camera.initialized) {
1030
+ if (options.readable)
1031
+ readStart();
1032
+ else
1033
+ fit();
1034
+ }
1035
+ else
1036
+ resize();
1037
+ if (selectedItem && options.selected !== view.selection)
1038
+ revealFlowPosition(camera, selectedItem.position);
1039
+ view.selection = options.selected;
1040
+ apply();
1041
+ // 查看详情、切换代码页签和调整窗口只改变可用空间,保留用户的缩放与布局。
1042
+ observer = new ResizeObserver(resize);
1043
+ observer.observe(canvas);
1044
+ document.addEventListener('keydown', keydown);
1045
+ document.addEventListener('keyup', keyup);
1046
+ window.addEventListener('blur', releaseSpace);
1047
+ },
1048
+ /**
1049
+ * 离开场景前断开观察器、取消手势并撤销全局监听;持久 view 由调用方保留以便恢复阅读。
1050
+ */
1051
+ dispose() {
1052
+ observer?.disconnect();
1053
+ gestures.cancel();
1054
+ document.removeEventListener('keydown', keydown);
1055
+ document.removeEventListener('keyup', keyup);
1056
+ window.removeEventListener('blur', releaseSpace);
1057
+ },
1058
+ };
1059
+ }