soma-kit 0.0.0-stage → 0.1.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 (353) hide show
  1. package/README.md +539 -2
  2. package/bin/soma-kit.mjs +27 -0
  3. package/package.json +66 -4
  4. package/packs/ai/agent-run-card.ts +64 -0
  5. package/packs/ai/evaluation-card.ts +51 -0
  6. package/packs/ai/guardrail-policy-card.ts +48 -0
  7. package/packs/ai/index.ts +31 -0
  8. package/packs/ai/model-card.ts +55 -0
  9. package/packs/ai/model-routing-card.ts +49 -0
  10. package/packs/ai/prompt-template-card.ts +49 -0
  11. package/packs/ai/texts.ts +19 -0
  12. package/packs/commerce/address-card.ts +58 -0
  13. package/packs/commerce/coupon-card.ts +64 -0
  14. package/packs/commerce/index.ts +25 -0
  15. package/packs/commerce/order-card.ts +60 -0
  16. package/packs/commerce/purchase-order-card.ts +74 -0
  17. package/packs/commerce/texts.ts +14 -0
  18. package/packs/finance/index.ts +30 -0
  19. package/packs/finance/invoice-summary-card.ts +48 -0
  20. package/packs/finance/reconcile-board.ts +450 -0
  21. package/packs/finance/reconcile-case-card.ts +70 -0
  22. package/packs/finance/reconcile-desk/compile.ts +208 -0
  23. package/packs/finance/reconcile-desk/index.ts +20 -0
  24. package/packs/finance/reconcile-desk/shell.ts +27 -0
  25. package/packs/finance/texts.ts +21 -0
  26. package/packs/ops/change-window-card.ts +54 -0
  27. package/packs/ops/dependency-status-card.ts +50 -0
  28. package/packs/ops/error-budget-card.ts +60 -0
  29. package/packs/ops/execution-task-runner.ts +61 -0
  30. package/packs/ops/index.ts +36 -0
  31. package/packs/ops/integration-status-card.ts +45 -0
  32. package/packs/ops/release-readiness-card.ts +63 -0
  33. package/packs/ops/runbook-execution-card.ts +81 -0
  34. package/packs/ops/texts.ts +21 -0
  35. package/packs/security/control-evidence-card.ts +54 -0
  36. package/packs/security/finding-card.ts +96 -0
  37. package/packs/security/index.ts +29 -0
  38. package/packs/security/texts.ts +31 -0
  39. package/packs/shared/card-util.ts +60 -0
  40. package/packs/shared/wrap.ts +13 -0
  41. package/spec/generated/kit-app.schema.json +84 -0
  42. package/spec/generated/kit-page.schema.json +2766 -0
  43. package/spec/kit-grammar.json +1358 -0
  44. package/spec/kit-grammar.schema.json +716 -0
  45. package/src/app/app-config.ts +108 -0
  46. package/src/app/compose-apps.ts +139 -0
  47. package/src/app/define-kit-actions.ts +89 -0
  48. package/src/app/define-kit-app.ts +362 -0
  49. package/src/app/define-kit-pack.ts +114 -0
  50. package/src/app/envelope.ts +117 -0
  51. package/src/app/index.ts +50 -0
  52. package/src/app/notify-renderer.ts +114 -0
  53. package/src/app/paged-list-adapter.ts +246 -0
  54. package/src/app/server-pages.ts +303 -0
  55. package/src/app/server.ts +244 -0
  56. package/src/app/submit.types.ts +87 -0
  57. package/src/base/action-busy.ts +159 -0
  58. package/src/base/action-gate.ts +267 -0
  59. package/src/base/action-status.ts +31 -0
  60. package/src/base/alert.ts +136 -0
  61. package/src/base/avatar.ts +271 -0
  62. package/src/base/button.ts +139 -0
  63. package/src/base/cascader.ts +523 -0
  64. package/src/base/check-core.ts +214 -0
  65. package/src/base/checkbox.ts +35 -0
  66. package/src/base/choice.ts +183 -0
  67. package/src/base/color-picker.ts +316 -0
  68. package/src/base/combobox.ts +61 -0
  69. package/src/base/command-palette.ts +371 -0
  70. package/src/base/context-menu.ts +103 -0
  71. package/src/base/countdown.ts +168 -0
  72. package/src/base/date-range.ts +444 -0
  73. package/src/base/date.ts +899 -0
  74. package/src/base/divider.ts +156 -0
  75. package/src/base/fab.ts +192 -0
  76. package/src/base/icons.ts +106 -0
  77. package/src/base/image.ts +177 -0
  78. package/src/base/index.ts +214 -0
  79. package/src/base/input.ts +316 -0
  80. package/src/base/kbd.ts +99 -0
  81. package/src/base/link.ts +167 -0
  82. package/src/base/listbox-core.ts +965 -0
  83. package/src/base/mentions.ts +323 -0
  84. package/src/base/menu-button.ts +240 -0
  85. package/src/base/menu-core.ts +341 -0
  86. package/src/base/month-grid.ts +171 -0
  87. package/src/base/motion/aura.ts +94 -0
  88. package/src/base/motion/hover-3d.ts +154 -0
  89. package/src/base/motion/hover-gallery.ts +185 -0
  90. package/src/base/motion/index.ts +27 -0
  91. package/src/base/motion/limits.ts +75 -0
  92. package/src/base/motion/marquee.ts +157 -0
  93. package/src/base/motion/text-rotate.ts +195 -0
  94. package/src/base/number.ts +121 -0
  95. package/src/base/otp.ts +259 -0
  96. package/src/base/permission-gate.ts +99 -0
  97. package/src/base/popover.ts +352 -0
  98. package/src/base/popup.ts +542 -0
  99. package/src/base/portal-core.ts +297 -0
  100. package/src/base/price-text.ts +126 -0
  101. package/src/base/progress.ts +216 -0
  102. package/src/base/qr-code.ts +446 -0
  103. package/src/base/qty-stepper.ts +241 -0
  104. package/src/base/radio.ts +33 -0
  105. package/src/base/range.ts +444 -0
  106. package/src/base/rating.ts +249 -0
  107. package/src/base/segmented.ts +286 -0
  108. package/src/base/select.ts +57 -0
  109. package/src/base/shared.ts +708 -0
  110. package/src/base/swap.ts +86 -0
  111. package/src/base/switch.ts +111 -0
  112. package/src/base/tag-input.ts +464 -0
  113. package/src/base/tag.ts +250 -0
  114. package/src/base/text.ts +126 -0
  115. package/src/base/textarea.ts +129 -0
  116. package/src/base/time-picker.ts +606 -0
  117. package/src/base/tree-select.ts +629 -0
  118. package/src/base/upload.ts +548 -0
  119. package/src/base/virtual-list.ts +286 -0
  120. package/src/biz/approval-desk/compile.ts +179 -0
  121. package/src/biz/approval-desk/index.ts +21 -0
  122. package/src/biz/approval-desk/shell.ts +50 -0
  123. package/src/biz/auth-panel/compile.ts +196 -0
  124. package/src/biz/auth-panel/index.ts +24 -0
  125. package/src/biz/auth-panel/shell.ts +46 -0
  126. package/src/biz/category-crud/compile.ts +173 -0
  127. package/src/biz/category-crud/index.ts +19 -0
  128. package/src/biz/category-crud/shell.ts +29 -0
  129. package/src/biz/checkout-page/compile.ts +111 -0
  130. package/src/biz/checkout-page/index.ts +20 -0
  131. package/src/biz/checkout-page/shell.ts +50 -0
  132. package/src/biz/compose.ts +313 -0
  133. package/src/biz/data-components.ts +26 -0
  134. package/src/biz/define.ts +23 -0
  135. package/src/biz/import-wizard/compile.ts +204 -0
  136. package/src/biz/import-wizard/hub.ts +540 -0
  137. package/src/biz/import-wizard/index.ts +24 -0
  138. package/src/biz/import-wizard/shell.ts +161 -0
  139. package/src/biz/index.ts +80 -0
  140. package/src/biz/landing-page/compile.ts +67 -0
  141. package/src/biz/landing-page/index.ts +20 -0
  142. package/src/biz/landing-page/shell.ts +48 -0
  143. package/src/biz/link/hub.ts +255 -0
  144. package/src/biz/link/shared.ts +493 -0
  145. package/src/biz/master-sub/compile.ts +232 -0
  146. package/src/biz/master-sub/index.ts +20 -0
  147. package/src/biz/master-sub/shell.ts +56 -0
  148. package/src/biz/multi-board/compile.ts +234 -0
  149. package/src/biz/multi-board/index.ts +19 -0
  150. package/src/biz/multi-board/shell.ts +70 -0
  151. package/src/biz/names.ts +30 -0
  152. package/src/biz/notification-center/compile.ts +138 -0
  153. package/src/biz/notification-center/index.ts +29 -0
  154. package/src/biz/notification-center/shell.ts +42 -0
  155. package/src/biz/page-templates/result-page/compile.ts +46 -0
  156. package/src/biz/page-templates/result-page/index.ts +19 -0
  157. package/src/biz/page-templates/result-page/shell.ts +31 -0
  158. package/src/biz/page-templates/shared.ts +121 -0
  159. package/src/biz/page-templates/sign-in-page/compile.ts +176 -0
  160. package/src/biz/page-templates/sign-in-page/index.ts +18 -0
  161. package/src/biz/page-templates/sign-in-page/shell.ts +116 -0
  162. package/src/biz/record-detail/compile.ts +290 -0
  163. package/src/biz/record-detail/index.ts +22 -0
  164. package/src/biz/record-detail/shell.ts +64 -0
  165. package/src/biz/settings-center/compile.ts +100 -0
  166. package/src/biz/settings-center/index.ts +18 -0
  167. package/src/biz/settings-center/shell.ts +51 -0
  168. package/src/biz/shared/action-host.ts +469 -0
  169. package/src/biz/shared/coordinator.ts +516 -0
  170. package/src/biz/shared/index.ts +9 -0
  171. package/src/biz/shared/pipes.ts +58 -0
  172. package/src/biz/shared/runtime-hub.ts +112 -0
  173. package/src/biz/shared/step-values.ts +25 -0
  174. package/src/biz/single-form/compile.ts +146 -0
  175. package/src/biz/single-form/index.ts +18 -0
  176. package/src/biz/single-form/shell.ts +38 -0
  177. package/src/biz/standard-crud/bridge.ts +11 -0
  178. package/src/biz/standard-crud/compile.ts +546 -0
  179. package/src/biz/standard-crud/index.ts +18 -0
  180. package/src/biz/standard-crud/shell.ts +157 -0
  181. package/src/biz/step-form/compile.ts +400 -0
  182. package/src/biz/step-form/hub.ts +412 -0
  183. package/src/biz/step-form/index.ts +23 -0
  184. package/src/biz/step-form/shell.ts +96 -0
  185. package/src/biz/step-form/steps-ref.ts +112 -0
  186. package/src/biz/types.ts +96 -0
  187. package/src/cli/kit-cli.ts +102 -0
  188. package/src/compile/action-host-attach.ts +76 -0
  189. package/src/compile/compile-page.ts +472 -0
  190. package/src/compile/component-candidates.ts +147 -0
  191. package/src/compile/fragment.ts +104 -0
  192. package/src/compile/hints.ts +255 -0
  193. package/src/compile/index.ts +16 -0
  194. package/src/compile/locate.ts +166 -0
  195. package/src/compile/page-params.ts +80 -0
  196. package/src/compile/parse.ts +355 -0
  197. package/src/compile/redirect.ts +90 -0
  198. package/src/compile/registry.ts +30 -0
  199. package/src/compile/remove-guard.ts +99 -0
  200. package/src/compile/styles.ts +244 -0
  201. package/src/compile/types.ts +106 -0
  202. package/src/data/amount-breakdown.ts +132 -0
  203. package/src/data/anchor-nav.ts +225 -0
  204. package/src/data/approval-flow.ts +269 -0
  205. package/src/data/assignment-picker.ts +652 -0
  206. package/src/data/async-result-panel.ts +308 -0
  207. package/src/data/breadcrumbs.ts +197 -0
  208. package/src/data/calendar.ts +363 -0
  209. package/src/data/carousel.ts +408 -0
  210. package/src/data/chart.ts +726 -0
  211. package/src/data/checklist.ts +215 -0
  212. package/src/data/code-view.ts +415 -0
  213. package/src/data/comment-thread.ts +342 -0
  214. package/src/data/confirm-dialog.ts +144 -0
  215. package/src/data/context-menu.ts +216 -0
  216. package/src/data/d9-limits.ts +42 -0
  217. package/src/data/d9-util.ts +81 -0
  218. package/src/data/diff.ts +239 -0
  219. package/src/data/display-util.ts +135 -0
  220. package/src/data/dock.ts +152 -0
  221. package/src/data/embed.ts +200 -0
  222. package/src/data/field-mapper.ts +544 -0
  223. package/src/data/flow-editor-contract.ts +41 -0
  224. package/src/data/flow-editor.ts +2622 -0
  225. package/src/data/flow-remote-options.ts +257 -0
  226. package/src/data/footer.ts +157 -0
  227. package/src/data/form-draft-registry.ts +76 -0
  228. package/src/data/gantt.ts +430 -0
  229. package/src/data/hero.ts +149 -0
  230. package/src/data/image-viewer.ts +333 -0
  231. package/src/data/index.ts +268 -0
  232. package/src/data/item-list.ts +335 -0
  233. package/src/data/kanban.ts +667 -0
  234. package/src/data/media-grid.ts +246 -0
  235. package/src/data/mega-menu.ts +257 -0
  236. package/src/data/menu-dom.ts +169 -0
  237. package/src/data/menu.ts +464 -0
  238. package/src/data/mockup.ts +176 -0
  239. package/src/data/modal-dialog.ts +182 -0
  240. package/src/data/navbar.ts +292 -0
  241. package/src/data/object-form.ts +1981 -0
  242. package/src/data/options-bus.ts +24 -0
  243. package/src/data/outcome-watch.ts +40 -0
  244. package/src/data/overlay-portal.ts +6 -0
  245. package/src/data/pager.ts +473 -0
  246. package/src/data/permission-matrix.ts +432 -0
  247. package/src/data/permission-tree.ts +916 -0
  248. package/src/data/pricing-table.ts +269 -0
  249. package/src/data/prompt-dialog.ts +235 -0
  250. package/src/data/reaction-bar.ts +236 -0
  251. package/src/data/record-card.ts +413 -0
  252. package/src/data/record-picker.ts +489 -0
  253. package/src/data/redirect.ts +109 -0
  254. package/src/data/remote-options.ts +54 -0
  255. package/src/data/request-console.ts +399 -0
  256. package/src/data/resident.ts +58 -0
  257. package/src/data/result-panel.ts +160 -0
  258. package/src/data/rich-text.ts +626 -0
  259. package/src/data/row-editor.ts +1066 -0
  260. package/src/data/rule-builder.ts +1137 -0
  261. package/src/data/shape-util.ts +221 -0
  262. package/src/data/staged-rows.ts +69 -0
  263. package/src/data/stat-group.ts +231 -0
  264. package/src/data/static-lazy.ts +63 -0
  265. package/src/data/static-registry.ts +227 -0
  266. package/src/data/steps.ts +195 -0
  267. package/src/data/survey.ts +455 -0
  268. package/src/data/system-switcher.ts +243 -0
  269. package/src/data/timeline.ts +204 -0
  270. package/src/data/tour-guide.ts +392 -0
  271. package/src/data/transfer.ts +597 -0
  272. package/src/data/tree-check.ts +30 -0
  273. package/src/data/tree-table.ts +674 -0
  274. package/src/data/tree.ts +656 -0
  275. package/src/data/trend-line.ts +147 -0
  276. package/src/data/virtual-rows.ts +556 -0
  277. package/src/data/watermark.ts +140 -0
  278. package/src/data/wrap.ts +267 -0
  279. package/src/data/wrapped-candidates.ts +96 -0
  280. package/src/frames/blank-frame.ts +19 -0
  281. package/src/frames/index.ts +44 -0
  282. package/src/frames/page-bar.ts +185 -0
  283. package/src/frames/page-header.ts +185 -0
  284. package/src/frames/page-status.ts +54 -0
  285. package/src/frames/resize-handle.ts +147 -0
  286. package/src/frames/shared.ts +242 -0
  287. package/src/frames/single-frame.ts +16 -0
  288. package/src/frames/split-frame/index.ts +4 -0
  289. package/src/frames/split-frame/shell.ts +219 -0
  290. package/src/frames/split-frame/span.ts +64 -0
  291. package/src/frames/split-frame/wiring.ts +164 -0
  292. package/src/frames/tabs-frame/index.ts +3 -0
  293. package/src/frames/tabs-frame/shell.ts +112 -0
  294. package/src/frames/tabs-frame/wiring.ts +69 -0
  295. package/src/frames/top-main-frame.ts +20 -0
  296. package/src/frames/types.ts +72 -0
  297. package/src/frames/workspace-frame/extras.ts +197 -0
  298. package/src/frames/workspace-frame/index.ts +13 -0
  299. package/src/frames/workspace-frame/shell.ts +282 -0
  300. package/src/frames/workspace-frame/state.ts +152 -0
  301. package/src/frames/workspace-frame/view.ts +1013 -0
  302. package/src/generated/kit-grammar-runtime.ts +2822 -0
  303. package/src/generated/page-origin.ts +57 -0
  304. package/src/i18n/index.ts +22 -0
  305. package/src/i18n/kit-text.ts +692 -0
  306. package/src/i18n/resolve-text-refs.ts +128 -0
  307. package/src/index.ts +6 -0
  308. package/src/object-renderer/action-gate.ts +216 -0
  309. package/src/object-renderer/adopt-gate.ts +333 -0
  310. package/src/object-renderer/arrange.ts +411 -0
  311. package/src/object-renderer/candidates.ts +68 -0
  312. package/src/object-renderer/collapse-wiring.ts +99 -0
  313. package/src/object-renderer/collection-expand.ts +185 -0
  314. package/src/object-renderer/collection-parts.ts +653 -0
  315. package/src/object-renderer/collection-table.ts +503 -0
  316. package/src/object-renderer/collection.ts +628 -0
  317. package/src/object-renderer/compile-data.ts +549 -0
  318. package/src/object-renderer/defaults.ts +252 -0
  319. package/src/object-renderer/display-checks.ts +153 -0
  320. package/src/object-renderer/emit-events.ts +498 -0
  321. package/src/object-renderer/emit-object.ts +98 -0
  322. package/src/object-renderer/emit-static.ts +1036 -0
  323. package/src/object-renderer/field-policy.ts +89 -0
  324. package/src/object-renderer/field-value.ts +581 -0
  325. package/src/object-renderer/form-detail.ts +116 -0
  326. package/src/object-renderer/index.ts +13 -0
  327. package/src/object-renderer/linkage.ts +371 -0
  328. package/src/object-renderer/merge-class.ts +126 -0
  329. package/src/object-renderer/mode.ts +25 -0
  330. package/src/object-renderer/outcome-draft.ts +61 -0
  331. package/src/object-renderer/parts.ts +136 -0
  332. package/src/object-renderer/pick-component.ts +36 -0
  333. package/src/object-renderer/render-object.ts +724 -0
  334. package/src/object-renderer/request-actions.ts +581 -0
  335. package/src/object-renderer/skeleton.ts +101 -0
  336. package/src/object-renderer/table-actions.ts +438 -0
  337. package/src/object-renderer/table-summary.ts +105 -0
  338. package/src/object-renderer/table-tools.ts +1072 -0
  339. package/src/object-renderer/types.ts +936 -0
  340. package/src/page-target/ai-surface.ts +154 -0
  341. package/src/page-target/compile.ts +354 -0
  342. package/src/page-target/drift.ts +186 -0
  343. package/src/page-target/index.ts +25 -0
  344. package/src/page-target/intent-map.ts +137 -0
  345. package/src/page-target/intent-schema.ts +711 -0
  346. package/src/page-target/origin.ts +17 -0
  347. package/src/page-target/target.ts +450 -0
  348. package/src/page-target/writer-policy.ts +315 -0
  349. package/src/theme/block-theme.ts +202 -0
  350. package/src/theme/kit-theme-text.ts +2 -0
  351. package/src/theme/kit-theme.css +275 -0
  352. package/src/theme/kit-theme.tpl +275 -0
  353. package/src/theme/theme-tokens.ts +135 -0
@@ -0,0 +1,430 @@
1
+ // Gantt:只读甘特图(layer: data,写死形状,accept: list)。
2
+ //
3
+ // 数据:`data` 是任务行数组,键名由 `map` 改写(组件期望的键 -> 数据里的键);默认键:
4
+ // id、title、start、end(日期 YYYY-MM-DD,带时间的取前 10 位)、progress(0–100,可选)、group(可选)、deps(前置任务 id 数组,可选)、
5
+ // tone(success / warning / danger / info,缺省主色)、owner(可选,显示在行尾)。
6
+ //
7
+ // 时间轴换算写死(同输入同输出,不读时钟、不依赖时区):
8
+ // - 日期 -> 天序号:Date.UTC(y, m, d) / 86400000 取整;起止非法的任务整行跳过;结束早于开始按「当天」处理;
9
+ // - 轴范围:`start_date` / `end_date` 显式给就用,否则取任务最早开始到最晚结束(含首尾两端,单日任务占 1 天);
10
+ // span = 末日 - 首日 + 1,一天宽 100 / span(单位是时间轴宽度的百分点,viewBox 为 0..100);
11
+ // - 任务条 x = (start - 首日) / span * 100,宽 = (end - start + 1) / span * 100;
12
+ // - 表头刻度把轴等分成 tick_count 段(2–10,默认 5),第 i 段标注 首日 + round(span * i / tick_count);
13
+ // - 今日线由 `today`(YYYY-MM-DD)显式传入,落在当天格子的正中;today 不在轴范围内不画;
14
+ // - 依赖线:从前置任务条末端出发,横向外探固定 0.8,竖向到后继任务行中线,再横向接到后继任务条起点并带箭头;
15
+ // 前置 id 不存在或被跳过的依赖不画。
16
+ // 不做拖拽改期、不算关键路径与工期(改期是表单的事,依赖顺延属后端规则);左栏每行是按钮,点击抛 `pick { id, item }`
17
+ //(item 是源数据里的原对象),键盘用 Tab 走行、Enter / Space 触发。
18
+ // 画面用一张 SVG(任务条、进度、刻度线、今日线、依赖线),左栏与表头是 HTML;SVG 只用语义色名类与属性,无内联样式。
19
+
20
+ import type { SomaComponentControllerContext, SomaComponentDefinition } from 'soma-core';
21
+ import { findRoot, joinClass, num, str } from '../base/shared';
22
+ import { kitText } from '../i18n/kit-text';
23
+
24
+ // ── 数据规整纯函数:日期换算、任务解析、时间轴、任务条位置、刻度、行序 ──
25
+
26
+ export interface KitGanttMap {
27
+ id: string;
28
+ title: string;
29
+ start: string;
30
+ end: string;
31
+ progress: string;
32
+ group: string;
33
+ deps: string;
34
+ tone: string;
35
+ owner: string;
36
+ }
37
+
38
+ export const KIT_GANTT_DEFAULT_MAP: Readonly<KitGanttMap> = Object.freeze({
39
+ id: 'id', title: 'title', start: 'start', end: 'end', progress: 'progress',
40
+ group: 'group', deps: 'deps', tone: 'tone', owner: 'owner',
41
+ });
42
+
43
+ export type KitGanttTone = 'primary' | 'success' | 'warning' | 'danger' | 'info';
44
+
45
+ export interface KitGanttTask {
46
+ id: string;
47
+ title: string;
48
+ from: number;
49
+ to: number;
50
+ progress: number;
51
+ group: string;
52
+ deps: string[];
53
+ tone: KitGanttTone;
54
+ owner: string;
55
+ startText: string;
56
+ endText: string;
57
+ raw: unknown;
58
+ }
59
+
60
+ export interface KitGanttAxis {
61
+ from: number;
62
+ to: number;
63
+ span: number;
64
+ }
65
+
66
+ const DAY_MS = 86_400_000;
67
+ const DEFAULT_TICKS = 5;
68
+ /** 依赖线从前置条末端向外探出的距离(轴宽百分点) */
69
+ const DEP_STUB = 0.8;
70
+ /** 箭头:长(轴宽百分点)与半高(行高单位) */
71
+ const ARROW_LEN = 0.8;
72
+ const ARROW_HALF = 0.2;
73
+ /** 任务条在行内的上沿与高度(行高单位) */
74
+ const BAR_TOP = 0.2;
75
+ const BAR_HEIGHT = 0.6;
76
+
77
+ /** YYYY-MM-DD(或以它开头的日期时间)-> 从 1970-01-01 起的天序号;非法返回 NaN。不依赖本地时区 */
78
+ export function ganttDayIndex(value: unknown): number {
79
+ const m = /^(\d{4})-(\d{2})-(\d{2})/.exec(str(value).trim());
80
+ if (!m) return Number.NaN;
81
+ const y = Number(m[1]);
82
+ const mo = Number(m[2]);
83
+ const d = Number(m[3]);
84
+ if (mo < 1 || mo > 12 || d < 1 || d > 31) return Number.NaN;
85
+ const stamp = Date.UTC(y, mo - 1, d);
86
+ // Date.UTC 会把 2-31 滚成 3-3;滚过月的视为非法日期
87
+ if (new Date(stamp).getUTCMonth() !== mo - 1) return Number.NaN;
88
+ return Math.floor(stamp / DAY_MS);
89
+ }
90
+
91
+ /** 天序号 -> YYYY-MM-DD */
92
+ export function ganttDayText(index: number): string {
93
+ const date = new Date(index * DAY_MS);
94
+ const pad = (n: number): string => String(n).padStart(2, '0');
95
+ return `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`;
96
+ }
97
+
98
+ function mapOf(raw: unknown): KitGanttMap {
99
+ const given = raw !== null && typeof raw === 'object' && !Array.isArray(raw) ? raw as Record<string, unknown> : {};
100
+ const out: Record<string, string> = { ...KIT_GANTT_DEFAULT_MAP };
101
+ for (const key of Object.keys(KIT_GANTT_DEFAULT_MAP)) {
102
+ const v = given[key];
103
+ if (typeof v === 'string' && v !== '') out[key] = v;
104
+ }
105
+ return out as unknown as KitGanttMap;
106
+ }
107
+
108
+ function toneOf(raw: unknown): KitGanttTone {
109
+ return raw === 'success' || raw === 'warning' || raw === 'danger' || raw === 'info' ? raw : 'primary';
110
+ }
111
+
112
+ /** 解析任务(纯函数):起止非法整行跳过,id 缺省取行号,重复 id 后者加行号后缀;结束早于开始按当天 */
113
+ export function parseGanttTasks(data: unknown, map: KitGanttMap): KitGanttTask[] {
114
+ const list = Array.isArray(data) ? data : [];
115
+ const seen = new Set<string>();
116
+ const out: KitGanttTask[] = [];
117
+ list.forEach((raw, index) => {
118
+ if (raw === null || typeof raw !== 'object') return;
119
+ const r = raw as Record<string, unknown>;
120
+ const from = ganttDayIndex(r[map.start]);
121
+ const end = ganttDayIndex(r[map.end]);
122
+ if (!Number.isFinite(from) || !Number.isFinite(end)) return;
123
+ let id = str(r[map.id]).trim();
124
+ if (id === '') id = `t${index}`;
125
+ if (seen.has(id)) id = `${id}#${index}`;
126
+ seen.add(id);
127
+ const pr = num(r[map.progress]);
128
+ out.push({
129
+ id,
130
+ title: str(r[map.title]).trim() || id,
131
+ from,
132
+ to: Math.max(from, end),
133
+ progress: pr === null ? 0 : Math.min(100, Math.max(0, pr)),
134
+ group: str(r[map.group]).trim(),
135
+ deps: Array.isArray(r[map.deps]) ? (r[map.deps] as unknown[]).map((d) => str(d).trim()).filter((d) => d !== '') : [],
136
+ tone: toneOf(r[map.tone]),
137
+ owner: str(r[map.owner]).trim(),
138
+ startText: ganttDayText(from),
139
+ endText: ganttDayText(Math.max(from, end)),
140
+ raw,
141
+ });
142
+ });
143
+ return out;
144
+ }
145
+
146
+ /** 时间轴范围:显式起止优先,否则取任务的最早开始与最晚结束;无任务无显式值时为 null */
147
+ export function ganttAxis(tasks: readonly KitGanttTask[], startDate: unknown, endDate: unknown): KitGanttAxis | null {
148
+ const ef = ganttDayIndex(startDate);
149
+ const et = ganttDayIndex(endDate);
150
+ if (tasks.length === 0 && !(Number.isFinite(ef) && Number.isFinite(et))) return null;
151
+ const from = Number.isFinite(ef) ? ef : Math.min(...tasks.map((t) => t.from));
152
+ const to = Number.isFinite(et) ? et : Math.max(...tasks.map((t) => t.to));
153
+ const hi = Math.max(from, to);
154
+ return { from, to: hi, span: hi - from + 1 };
155
+ }
156
+
157
+ /** 任务条的 x 与宽(轴宽百分点,viewBox 0..100)。不裁剪:轴范围显式给小了,超出的部分画在 viewBox 外 */
158
+ export function ganttBar(task: Pick<KitGanttTask, 'from' | 'to'>, axis: KitGanttAxis): { x: number; w: number } {
159
+ return {
160
+ x: ((task.from - axis.from) / axis.span) * 100,
161
+ w: ((task.to - task.from + 1) / axis.span) * 100,
162
+ };
163
+ }
164
+
165
+ /** 表头刻度:等分 count 段,第 i 段标注 首日 + round(span * i / count) */
166
+ export function ganttTicks(axis: KitGanttAxis, count: number): string[] {
167
+ const n = Math.min(10, Math.max(2, Math.floor(count)));
168
+ return Array.from({ length: n }, (_, i) => ganttDayText(axis.from + Math.round((axis.span * i) / n)));
169
+ }
170
+
171
+ /** 数值转短文本,避免浮点尾巴把属性写得很长 */
172
+ function fx(n: number): string {
173
+ return String(Math.round(n * 1000) / 1000);
174
+ }
175
+
176
+ interface Row {
177
+ kind: 'group' | 'task';
178
+ group: string;
179
+ task?: KitGanttTask;
180
+ }
181
+
182
+ /** 行序:保持数据顺序;组名变化(非空)时在前面插一条分组行 */
183
+ export function ganttRows(tasks: readonly KitGanttTask[]): Row[] {
184
+ const rows: Row[] = [];
185
+ let last = '';
186
+ for (const task of tasks) {
187
+ if (task.group !== '' && task.group !== last) rows.push({ kind: 'group', group: task.group });
188
+ if (task.group !== '') last = task.group;
189
+ rows.push({ kind: 'task', group: task.group, task });
190
+ }
191
+ return rows;
192
+ }
193
+
194
+ // ── 类串常量与 Tailwind 候选清单 ──
195
+
196
+ const ROOT_CLASS = 'flex flex-col overflow-x-auto text-sm text-fg';
197
+ const INNER_CLASS = 'flex min-w-160 flex-col';
198
+ const HEAD_CLASS = 'flex border-b border-border-subtle';
199
+ const CORNER_CLASS = 'w-40 shrink-0 px-2 py-1 text-xs font-medium text-fg-muted';
200
+ const TICKS_CLASS = 'flex flex-1';
201
+ const TICK_CLASS = 'flex-1 border-l border-border-subtle px-1 py-1 text-xs text-fg-muted';
202
+ const BODY_CLASS = 'flex';
203
+ const LABELS_CLASS = 'flex w-40 shrink-0 flex-col';
204
+ const ROW_BTN_CLASS = 'flex h-8 w-full items-center gap-2 px-2 text-start text-sm text-fg transition-colors hover:bg-surface-sunken focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-ring';
205
+ const ROW_TITLE_CLASS = 'min-w-0 flex-1 truncate';
206
+ const ROW_OWNER_CLASS = 'shrink-0 text-xs text-fg-muted';
207
+ const GROUP_ROW_CLASS = 'flex h-8 items-center bg-surface-sunken px-2 text-xs font-medium text-fg-muted';
208
+ const CHART_CLASS = 'relative flex-1';
209
+ const SVG_CLASS = 'absolute inset-0 h-full w-full';
210
+ const EMPTY_CLASS = 'px-3 py-6 text-center text-fg-muted';
211
+
212
+ const TRACK_CLASS: Record<KitGanttTone, string> = {
213
+ primary: 'fill-primary-soft stroke-primary',
214
+ success: 'fill-success-soft stroke-success',
215
+ warning: 'fill-warning-soft stroke-warning',
216
+ danger: 'fill-danger-soft stroke-danger',
217
+ info: 'fill-info-soft stroke-info',
218
+ };
219
+ const DONE_CLASS: Record<KitGanttTone, string> = {
220
+ primary: 'fill-primary',
221
+ success: 'fill-success',
222
+ warning: 'fill-warning',
223
+ danger: 'fill-danger',
224
+ info: 'fill-info',
225
+ };
226
+ const GROUP_BG_CLASS = 'fill-surface-sunken';
227
+ const GRID_LINE_CLASS = 'stroke-border-subtle';
228
+ const DEP_LINE_CLASS = 'fill-none stroke-fg-subtle';
229
+ const DEP_ARROW_CLASS = 'fill-fg-subtle';
230
+ const TODAY_CLASS = 'stroke-danger';
231
+
232
+ /** 本组件自带的全部类(供编译器并入候选类) */
233
+ export const KIT_GANTT_CLASS_CANDIDATES: readonly string[] = Object.freeze(
234
+ [...new Set([
235
+ ROOT_CLASS, INNER_CLASS, HEAD_CLASS, CORNER_CLASS, TICKS_CLASS, TICK_CLASS, BODY_CLASS, LABELS_CLASS, ROW_BTN_CLASS,
236
+ ROW_TITLE_CLASS, ROW_OWNER_CLASS, GROUP_ROW_CLASS, CHART_CLASS, SVG_CLASS, EMPTY_CLASS,
237
+ ...Object.values(TRACK_CLASS), ...Object.values(DONE_CLASS),
238
+ GROUP_BG_CLASS, GRID_LINE_CLASS, DEP_LINE_CLASS, DEP_ARROW_CLASS, TODAY_CLASS,
239
+ ].flatMap((s) => s.split(/\s+/)).filter((c) => c !== ''))].sort(),
240
+ );
241
+
242
+ // ── 组件 ──
243
+
244
+ const SVG_NS = 'http://www.w3.org/2000/svg';
245
+
246
+ function svgEl(tag: string, attrs: Record<string, string>, className = ''): SVGElement {
247
+ const node = document.createElementNS(SVG_NS, tag);
248
+ for (const [k, v] of Object.entries(attrs)) node.setAttribute(k, v);
249
+ if (className !== '') node.setAttribute('class', className);
250
+ return node as SVGElement;
251
+ }
252
+
253
+ export function createGantt(): SomaComponentDefinition {
254
+ return {
255
+ contract: {
256
+ name: 'Gantt',
257
+ layer: 'data',
258
+ props: {
259
+ data: { type: 'array', default: [], desc: '任务行 [{ id, title, start, end, progress?, group?, deps?, tone?, owner? }];日期 YYYY-MM-DD' },
260
+ map: { type: 'object', default: {}, desc: '键名对照 { id, title, start, end, progress, group, deps, tone, owner }:组件期望的键 -> 数据里的键' },
261
+ start_date: { type: 'string', default: '', desc: '轴起点(YYYY-MM-DD);空则取任务最早开始' },
262
+ end_date: { type: 'string', default: '', desc: '轴终点(YYYY-MM-DD);空则取任务最晚结束' },
263
+ today: { type: 'string', default: '', desc: '今日线日期(YYYY-MM-DD),由页面显式传入;空或不在轴内则不画' },
264
+ tick_count: { type: 'number', default: DEFAULT_TICKS, desc: '表头刻度段数,2–10' },
265
+ show_progress: { type: 'boolean', default: true, desc: '是否画进度' },
266
+ empty_text: { type: 'string', default: '', desc: '无任务时的空态文案;缺省取内置文案(按页面语言)' },
267
+ class: { type: 'string', default: '', desc: '外部传入的类,追加在根节点类串末尾' },
268
+ },
269
+ emits: {
270
+ pick: { payload: { id: { type: 'string' }, item: { type: 'object' } }, desc: '点某一行任务;item 是源数据里的原对象' },
271
+ },
272
+ },
273
+ templateHtml: `<div data-soma-id="root" class="${ROOT_CLASS}"></div>`,
274
+ controllerFactory: (ctx: SomaComponentControllerContext) => {
275
+ const P = (): Record<string, unknown> => (ctx.readProps ? ctx.readProps() : ctx.props);
276
+ let root: HTMLElement | null = null;
277
+ let tasks: KitGanttTask[] = [];
278
+ let cleanup: (() => void) | undefined;
279
+
280
+ const el = <K extends keyof HTMLElementTagNameMap>(tag: K, className: string, text?: string): HTMLElementTagNameMap[K] => {
281
+ const node = document.createElement(tag);
282
+ node.className = className;
283
+ if (text !== undefined) node.textContent = text;
284
+ return node;
285
+ };
286
+
287
+ const buildChart = (rows: readonly Row[], axis: KitGanttAxis, p: Record<string, unknown>): SVGElement => {
288
+ const n = rows.length;
289
+ const svg = svgEl('svg', { viewBox: `0 0 100 ${n}`, preserveAspectRatio: 'none', 'aria-hidden': 'true' }, SVG_CLASS);
290
+ const line = (x1: number, y1: number, x2: number, y2: number, cls: string): SVGElement =>
291
+ svgEl('line', { x1: fx(x1), y1: fx(y1), x2: fx(x2), y2: fx(y2), 'vector-effect': 'non-scaling-stroke', 'stroke-width': '1' }, cls);
292
+ // 分组底色与行分隔线
293
+ rows.forEach((row, i) => {
294
+ if (row.kind === 'group') svg.appendChild(svgEl('rect', { x: '0', y: fx(i), width: '100', height: '1' }, GROUP_BG_CLASS));
295
+ });
296
+ for (let i = 1; i < n; i += 1) svg.appendChild(line(0, i, 100, i, GRID_LINE_CLASS));
297
+ // 刻度竖线(与表头的等分段对齐)
298
+ const tickCount = Math.min(10, Math.max(2, Math.floor(num(p.tick_count) ?? DEFAULT_TICKS)));
299
+ for (let i = 1; i < tickCount; i += 1) svg.appendChild(line((100 * i) / tickCount, 0, (100 * i) / tickCount, n, GRID_LINE_CLASS));
300
+
301
+ const index = new Map<string, { row: number; x: number; w: number }>();
302
+ const showProgress = p.show_progress !== false;
303
+ rows.forEach((row, i) => {
304
+ if (row.kind !== 'task' || !row.task) return;
305
+ const t = row.task;
306
+ const { x, w } = ganttBar(t, axis);
307
+ index.set(t.id, { row: i, x, w });
308
+ const g = svgEl('g', { 'data-kit-bar': t.id });
309
+ g.appendChild(svgEl('rect', { x: fx(x), y: fx(i + BAR_TOP), width: fx(w), height: fx(BAR_HEIGHT), 'vector-effect': 'non-scaling-stroke', 'stroke-width': '1' }, TRACK_CLASS[t.tone]));
310
+ if (showProgress && t.progress > 0) {
311
+ g.appendChild(svgEl('rect', { x: fx(x), y: fx(i + BAR_TOP), width: fx((w * t.progress) / 100), height: fx(BAR_HEIGHT) }, DONE_CLASS[t.tone]));
312
+ }
313
+ svg.appendChild(g);
314
+ });
315
+
316
+ // 依赖线:前置条末端 -> 外探 -> 竖到后继行中线 -> 接后继起点(带箭头)
317
+ for (const row of rows) {
318
+ if (row.kind !== 'task' || !row.task) continue;
319
+ const to = index.get(row.task.id);
320
+ if (!to) continue;
321
+ for (const dep of row.task.deps) {
322
+ const from = index.get(dep);
323
+ if (!from) continue;
324
+ const x1 = from.x + from.w;
325
+ const y1 = from.row + 0.5;
326
+ const y2 = to.row + 0.5;
327
+ const stub = x1 + DEP_STUB;
328
+ svg.appendChild(svgEl('path', {
329
+ d: `M ${fx(x1)} ${fx(y1)} H ${fx(stub)} V ${fx(y2)} H ${fx(to.x)}`,
330
+ 'vector-effect': 'non-scaling-stroke',
331
+ 'stroke-width': '1',
332
+ }, DEP_LINE_CLASS));
333
+ svg.appendChild(svgEl('path', {
334
+ d: `M ${fx(to.x)} ${fx(y2)} L ${fx(to.x - ARROW_LEN)} ${fx(y2 - ARROW_HALF)} L ${fx(to.x - ARROW_LEN)} ${fx(y2 + ARROW_HALF)} Z`,
335
+ }, DEP_ARROW_CLASS));
336
+ }
337
+ }
338
+
339
+ // 今日线:落在当天格子正中
340
+ const today = ganttDayIndex(p.today);
341
+ if (Number.isFinite(today) && today >= axis.from && today <= axis.to) {
342
+ const x = ((today - axis.from + 0.5) / axis.span) * 100;
343
+ svg.appendChild(line(x, 0, x, n, TODAY_CLASS));
344
+ }
345
+ return svg;
346
+ };
347
+
348
+ const paint = (): void => {
349
+ if (!root) return;
350
+ const p = P();
351
+ root.className = joinClass(ROOT_CLASS, p.class);
352
+ // 整块重建会丢焦点:记下聚焦的任务行,重建后还回去
353
+ const focused = document.activeElement instanceof HTMLElement && root.contains(document.activeElement)
354
+ ? document.activeElement.closest('[data-kit-task]')?.getAttribute('data-kit-task') ?? null : null;
355
+ root.textContent = '';
356
+ tasks = parseGanttTasks(p.data, mapOf(p.map));
357
+ const axis = ganttAxis(tasks, p.start_date, p.end_date);
358
+ if (tasks.length === 0 || axis === null) {
359
+ const empty = el('div', EMPTY_CLASS, str(p.empty_text, kitText('soma.kit.empty_schedule', undefined, root)));
360
+ empty.setAttribute('role', 'status');
361
+ root.appendChild(empty);
362
+ return;
363
+ }
364
+ const rows = ganttRows(tasks);
365
+ const showProgress = p.show_progress !== false;
366
+ const inner = el('div', INNER_CLASS);
367
+
368
+ const head = el('div', HEAD_CLASS);
369
+ head.appendChild(el('div', CORNER_CLASS));
370
+ const ticks = el('div', TICKS_CLASS);
371
+ const tickCount = num(p.tick_count) ?? DEFAULT_TICKS;
372
+ for (const text of ganttTicks(axis, tickCount)) ticks.appendChild(el('div', TICK_CLASS, text));
373
+ head.appendChild(ticks);
374
+ inner.appendChild(head);
375
+
376
+ const body = el('div', BODY_CLASS);
377
+ const labels = el('div', LABELS_CLASS);
378
+ for (const row of rows) {
379
+ if (row.kind === 'group') {
380
+ labels.appendChild(el('div', GROUP_ROW_CLASS, row.group));
381
+ continue;
382
+ }
383
+ const t = row.task!;
384
+ const btn = el('button', ROW_BTN_CLASS);
385
+ btn.type = 'button';
386
+ btn.setAttribute('data-kit-task', t.id);
387
+ const pct = showProgress && t.progress > 0 ? kitText('soma.kit.gantt_done', { n: Math.round(t.progress) }, root) : '';
388
+ const ownerPart = t.owner !== '' ? ` ${t.owner}` : '';
389
+ btn.setAttribute('aria-label', kitText('soma.kit.gantt_range', { title: t.title, owner: ownerPart, start: t.startText, end: t.endText, pct }, root));
390
+ btn.appendChild(el('span', ROW_TITLE_CLASS, t.title));
391
+ if (t.owner !== '') btn.appendChild(el('span', ROW_OWNER_CLASS, t.owner));
392
+ labels.appendChild(btn);
393
+ }
394
+ body.appendChild(labels);
395
+ const chart = el('div', CHART_CLASS);
396
+ chart.appendChild(buildChart(rows, axis, p) as unknown as Node);
397
+ body.appendChild(chart);
398
+ inner.appendChild(body);
399
+ root.appendChild(inner);
400
+ if (focused !== null) {
401
+ [...root.querySelectorAll<HTMLElement>('[data-kit-task]')].find((b) => b.getAttribute('data-kit-task') === focused)?.focus();
402
+ }
403
+ };
404
+
405
+ return {
406
+ onMount(): void {
407
+ root = findRoot(ctx.host);
408
+ const r = root;
409
+ const onClick = (event: Event): void => {
410
+ const id = (event.target as HTMLElement | null)?.closest<HTMLElement>('[data-kit-task]')?.getAttribute('data-kit-task');
411
+ if (!id) return;
412
+ const task = tasks.find((t) => t.id === id);
413
+ if (task) ctx.emit('pick', { id: task.id, item: task.raw });
414
+ };
415
+ r.addEventListener('click', onClick);
416
+ cleanup = () => r.removeEventListener('click', onClick);
417
+ paint();
418
+ },
419
+ onPropsChange(): void {
420
+ paint();
421
+ },
422
+ onUnmount(): void {
423
+ if (cleanup) cleanup();
424
+ cleanup = undefined;
425
+ root = null;
426
+ },
427
+ };
428
+ },
429
+ };
430
+ }
@@ -0,0 +1,149 @@
1
+ // Hero:主视觉(layer: data,写死形状,accept: single)。
2
+ //
3
+ // 数据:`data` 是 { title, subtitle?, description?, image?, image_alt?, actions? }(也可直接写在 `view.props`,data 优先)。
4
+ // actions 是 [{ key, label, tone?, disabled? }]:第一个默认主色、其余默认描边;点击抛 `action { key, item }`。
5
+ // layout 闭集:center(居中堆叠)/ split(容器够宽时左文右图,窄时上下堆叠)。图片地址走 safeHref,被拒则不出图。
6
+ // 动作只经事件抛出,组件不改数据。
7
+
8
+ import type { SomaComponentDefinition } from 'soma-core';
9
+ import { createKitController, joinClass, str } from '../base/shared';
10
+ import { actionButton, BUTTON_CLASS_TOKENS, classTokens, mk, rowsOf, type ShapeAction } from './shape-util';
11
+ import { singleOf, urlOf } from './display-util';
12
+
13
+ export const KIT_HERO_LAYOUTS = ['center', 'split'] as const;
14
+ export type KitHeroLayout = (typeof KIT_HERO_LAYOUTS)[number];
15
+
16
+ const KEYS = ['title', 'subtitle', 'description', 'image', 'image_alt', 'actions'] as const;
17
+
18
+ const ROOT_CLASS = '@container rounded-lg border border-border-subtle bg-surface-sunken px-6 py-10 text-fg';
19
+ const CENTER_CLASS = 'mx-auto flex max-w-2xl flex-col items-center gap-4 text-center';
20
+ const SPLIT_CLASS = 'flex flex-col gap-8 @md:flex-row @md:items-center';
21
+ const TEXT_CLASS = 'flex flex-1 flex-col gap-3';
22
+ const TEXT_CENTER_CLASS = 'flex flex-col items-center gap-3';
23
+ const TITLE_CLASS = 'text-2xl font-semibold text-fg';
24
+ const SUBTITLE_CLASS = 'text-lg font-medium text-fg-muted';
25
+ const DESC_CLASS = 'text-base text-fg-muted';
26
+ const ACTIONS_CLASS = 'flex flex-wrap items-center gap-3 pt-2';
27
+ const ACTIONS_CENTER_CLASS = 'flex flex-wrap items-center justify-center gap-3 pt-2';
28
+ const IMAGE_BOX_CLASS = 'flex-1';
29
+ const IMAGE_CLASS = 'block aspect-16/9 w-full rounded-lg object-cover shadow-md';
30
+ const IMAGE_CENTER_CLASS = 'block aspect-16/9 w-full max-w-xl rounded-lg object-cover shadow-md';
31
+
32
+ export const KIT_HERO_CLASS_CANDIDATES: readonly string[] = classTokens(
33
+ ROOT_CLASS, CENTER_CLASS, SPLIT_CLASS, TEXT_CLASS, TEXT_CENTER_CLASS, TITLE_CLASS, SUBTITLE_CLASS, DESC_CLASS,
34
+ ACTIONS_CLASS, ACTIONS_CENTER_CLASS, IMAGE_BOX_CLASS, IMAGE_CLASS, IMAGE_CENTER_CLASS, BUTTON_CLASS_TOKENS,
35
+ );
36
+
37
+ export function heroLayoutOf(raw: unknown): KitHeroLayout {
38
+ return raw === 'split' ? 'split' : 'center';
39
+ }
40
+
41
+ export interface KitHeroModel {
42
+ title: string;
43
+ subtitle: string;
44
+ description: string;
45
+ image: string;
46
+ imageAlt: string;
47
+ actions: Array<ShapeAction & { raw: Record<string, unknown> }>;
48
+ }
49
+
50
+ /** 数据 + props -> 视图模型(纯函数,便于单测);第一个未写 tone 的动作默认主色 */
51
+ export function heroModelOf(data: unknown, props: Record<string, unknown>): KitHeroModel {
52
+ const src = singleOf(data, props, KEYS);
53
+ const actions: KitHeroModel['actions'] = [];
54
+ rowsOf(src.actions).forEach((row) => {
55
+ const key = row.key === undefined || row.key === null ? '' : String(row.key);
56
+ const label = row.label === undefined || row.label === null ? '' : String(row.label);
57
+ if (key === '' || label === '') return;
58
+ const tone = row.tone === 'primary' || row.tone === 'danger' || row.tone === 'neutral'
59
+ ? row.tone
60
+ : actions.length === 0 ? 'primary' : 'neutral';
61
+ actions.push({
62
+ key,
63
+ label,
64
+ tone,
65
+ disabled: row.disabled === true,
66
+ reason: row.disabled_reason === undefined || row.disabled_reason === null ? '' : String(row.disabled_reason),
67
+ raw: row,
68
+ });
69
+ });
70
+ return {
71
+ title: str(src.title),
72
+ subtitle: str(src.subtitle),
73
+ description: str(src.description),
74
+ image: urlOf(src.image),
75
+ imageAlt: str(src.image_alt),
76
+ actions,
77
+ };
78
+ }
79
+
80
+ export function createHero(): SomaComponentDefinition {
81
+ return {
82
+ contract: {
83
+ name: 'Hero',
84
+ layer: 'data',
85
+ props: {
86
+ data: { type: 'object', default: {}, desc: '{ title, subtitle?, description?, image?, image_alt?, actions?[{ key, label, tone?, disabled? }] }' },
87
+ layout: { type: 'enum', values: [...KIT_HERO_LAYOUTS], default: 'center', desc: 'center 居中堆叠;split 左文右图(窄容器上下堆叠)' },
88
+ title: { type: 'string', default: '', desc: '标题(data 里有则以 data 为准)' },
89
+ subtitle: { type: 'string', default: '', desc: '副标题' },
90
+ description: { type: 'string', default: '', desc: '描述' },
91
+ image: { type: 'string', default: '', desc: '图片地址(safeHref 放行的相对 / http(s) 地址)' },
92
+ image_alt: { type: 'string', default: '', desc: '图片替代文字;空表示装饰图' },
93
+ actions: { type: 'array', default: [], desc: '动作 [{ key, label, tone?, disabled? }],第一个默认主色' },
94
+ class: { type: 'string', default: '', desc: '外部传入的类,追加在根节点类串末尾' },
95
+ },
96
+ emits: {
97
+ action: { payload: { key: { type: 'string' }, item: { type: 'object' } }, desc: '点某个动作按钮;item 是该动作的源对象' },
98
+ },
99
+ },
100
+ templateHtml: `<section data-soma-id="root" class="${ROOT_CLASS}"></section>`,
101
+ controllerFactory: createKitController({
102
+ attach(root, api) {
103
+ const onClick = (event: Event): void => {
104
+ const hit = (event.target as HTMLElement | null)?.closest<HTMLElement>('[data-kit-action]');
105
+ if (!hit || !root.contains(hit) || hit.hasAttribute('disabled')) return;
106
+ const p = api.props();
107
+ const key = hit.getAttribute('data-kit-action') ?? '';
108
+ const action = heroModelOf(p.data, p).actions.find((a) => a.key === key);
109
+ if (action) api.emit('action', { key, item: action.raw });
110
+ };
111
+ root.addEventListener('click', onClick);
112
+ return () => root.removeEventListener('click', onClick);
113
+ },
114
+ render(root, p) {
115
+ root.className = joinClass(ROOT_CLASS, p.class);
116
+ root.textContent = '';
117
+ const model = heroModelOf(p.data, p);
118
+ const split = heroLayoutOf(p.layout) === 'split';
119
+ const wrap = mk('div', split ? SPLIT_CLASS : CENTER_CLASS);
120
+ const text = mk('div', split ? TEXT_CLASS : TEXT_CENTER_CLASS);
121
+ if (model.title !== '') text.appendChild(mk('h2', TITLE_CLASS, model.title));
122
+ if (model.subtitle !== '') text.appendChild(mk('p', SUBTITLE_CLASS, model.subtitle));
123
+ if (model.description !== '') text.appendChild(mk('p', DESC_CLASS, model.description));
124
+ if (model.actions.length > 0) {
125
+ const bar = mk('div', split ? ACTIONS_CLASS : ACTIONS_CENTER_CLASS);
126
+ for (const a of model.actions) bar.appendChild(actionButton(a));
127
+ text.appendChild(bar);
128
+ }
129
+ const img = model.image === '' ? null : mk('img', split ? IMAGE_CLASS : IMAGE_CENTER_CLASS);
130
+ if (img) {
131
+ img.setAttribute('src', model.image);
132
+ img.setAttribute('alt', model.imageAlt);
133
+ img.setAttribute('loading', 'lazy');
134
+ }
135
+ wrap.appendChild(text);
136
+ if (img) {
137
+ if (split) {
138
+ const box = mk('div', IMAGE_BOX_CLASS);
139
+ box.appendChild(img);
140
+ wrap.appendChild(box);
141
+ } else {
142
+ wrap.appendChild(img);
143
+ }
144
+ }
145
+ root.appendChild(wrap);
146
+ },
147
+ }),
148
+ };
149
+ }