soma-kit 0.1.14 → 0.1.16

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 (505) hide show
  1. package/README.md +1 -1
  2. package/package.json +8 -5
  3. package/packs/ai/model-card.ts +3 -3
  4. package/packs/finance/reconcile-desk/compile.ts +1 -1
  5. package/packs/finance/reconcile-desk/shell.ts +1 -1
  6. package/packs/ops/error-budget-card.ts +3 -3
  7. package/packs/ops/release-readiness-card.ts +3 -3
  8. package/packs/security/control-evidence-card.ts +3 -3
  9. package/packs/security/finding-card.ts +3 -2
  10. package/packs/shared/card-util.ts +17 -0
  11. package/spec/generated/kit-app.schema.json +2148 -338
  12. package/spec/generated/kit-page.schema.json +1152 -87
  13. package/spec/kit-grammar.json +1084 -136
  14. package/spec/kit-grammar.schema.json +167 -26
  15. package/src/app/app-config.ts +220 -187
  16. package/src/app/component-gate.ts +2 -2
  17. package/src/app/compose-apps.ts +5 -2
  18. package/src/app/define-kit-actions.ts +200 -22
  19. package/src/app/define-kit-app.ts +54 -8
  20. package/src/app/define-kit-view.ts +1 -1
  21. package/src/app/index.ts +12 -7
  22. package/src/app/kit-view-context.ts +50 -3
  23. package/src/app/list-page-defaults.ts +14 -0
  24. package/src/app/server-pages.ts +239 -7
  25. package/src/app/server.ts +466 -25
  26. package/src/app/theme-endpoint.ts +68 -24
  27. package/src/app/theme-runtime.ts +421 -87
  28. package/src/app/theme-shards.ts +69 -0
  29. package/src/app/tier-compat.ts +107 -0
  30. package/src/base/action-busy.ts +215 -38
  31. package/src/base/affix.ts +137 -0
  32. package/src/base/alert.ts +107 -59
  33. package/src/base/anchored-layer.ts +8 -7
  34. package/src/base/avatar.ts +147 -61
  35. package/src/base/badge.ts +186 -0
  36. package/src/base/button-group.ts +227 -0
  37. package/src/base/button.ts +365 -53
  38. package/src/base/card.ts +233 -0
  39. package/src/base/cascader.ts +147 -32
  40. package/src/base/check-core.ts +196 -33
  41. package/src/base/checkbox.ts +67 -2
  42. package/src/base/choice.ts +132 -31
  43. package/src/base/collapse.ts +249 -0
  44. package/src/base/color-picker.ts +404 -28
  45. package/src/base/combobox.ts +6 -2
  46. package/src/base/command-palette.ts +155 -37
  47. package/src/base/context-menu.ts +26 -6
  48. package/src/base/countdown.ts +37 -15
  49. package/src/base/date-range.ts +115 -94
  50. package/src/base/date.ts +487 -76
  51. package/src/base/delta.ts +20 -11
  52. package/src/base/divider.ts +64 -97
  53. package/src/base/end-edge.ts +10 -4
  54. package/src/base/fab.ts +64 -23
  55. package/src/base/gap-value.ts +35 -16
  56. package/src/base/hint-tips.ts +100 -30
  57. package/src/base/identity-cells.ts +839 -0
  58. package/src/base/illustrations.ts +6 -4
  59. package/src/base/image.ts +40 -19
  60. package/src/base/index.ts +92 -24
  61. package/src/base/inline-error.ts +90 -0
  62. package/src/base/input.ts +174 -38
  63. package/src/base/kbd.ts +31 -17
  64. package/src/base/kit-contract.ts +400 -0
  65. package/src/base/link.ts +41 -20
  66. package/src/base/listbox-core.ts +324 -95
  67. package/src/base/loading-gate.ts +3 -0
  68. package/src/base/look.ts +197 -0
  69. package/src/base/mentions.ts +58 -22
  70. package/src/base/menu-button.ts +129 -42
  71. package/src/base/menu-core.ts +210 -26
  72. package/src/base/month-grid.ts +60 -3
  73. package/src/base/motion/aura.ts +52 -10
  74. package/src/base/motion/hover-3d.ts +10 -5
  75. package/src/base/motion/hover-gallery.ts +19 -12
  76. package/src/base/motion/limits.ts +10 -0
  77. package/src/base/motion/marquee.ts +18 -11
  78. package/src/base/motion/text-rotate.ts +34 -16
  79. package/src/base/notification.ts +292 -0
  80. package/src/base/number.ts +93 -12
  81. package/src/base/otp.ts +143 -27
  82. package/src/base/overlay-runtime.ts +32 -24
  83. package/src/base/overlay-shell.ts +186 -0
  84. package/src/base/period-picker.ts +868 -0
  85. package/src/base/popover.ts +625 -74
  86. package/src/base/popup.ts +219 -78
  87. package/src/base/portal-core.ts +99 -2
  88. package/src/base/price-text.ts +51 -17
  89. package/src/base/progress.ts +100 -18
  90. package/src/base/qr-code.ts +64 -17
  91. package/src/base/qty-stepper.ts +145 -21
  92. package/src/base/radio.ts +43 -2
  93. package/src/base/range.ts +194 -39
  94. package/src/base/rating.ts +117 -31
  95. package/src/base/reason-tip.ts +60 -18
  96. package/src/base/relation-cells.ts +541 -0
  97. package/src/{frames → base}/resize-handle.ts +6 -4
  98. package/src/base/row-fresh.ts +84 -0
  99. package/src/base/section-spy.ts +79 -0
  100. package/src/base/segmented.ts +99 -56
  101. package/src/base/select.ts +8 -2
  102. package/src/base/shared.ts +520 -73
  103. package/src/base/spinner.ts +107 -0
  104. package/src/base/state-panel.ts +77 -22
  105. package/src/base/status-tone.ts +1 -1
  106. package/src/base/sticky-edge.ts +96 -29
  107. package/src/base/swap.ts +39 -21
  108. package/src/base/switch.ts +158 -38
  109. package/src/base/tabs.ts +292 -0
  110. package/src/base/tag-input.ts +129 -55
  111. package/src/base/tag.ts +203 -91
  112. package/src/base/text.ts +491 -77
  113. package/src/base/textarea.ts +89 -40
  114. package/src/base/time-picker.ts +341 -57
  115. package/src/base/time-range.ts +846 -0
  116. package/src/base/tree-select.ts +346 -89
  117. package/src/base/upload.ts +612 -158
  118. package/src/base/view-transition.ts +7 -3
  119. package/src/base/virtual-list.ts +49 -14
  120. package/src/biz/approval-desk/compile.ts +98 -6
  121. package/src/biz/approval-desk/index.ts +2 -0
  122. package/src/biz/approval-desk/shell.ts +53 -13
  123. package/src/biz/auth-panel/compile.ts +14 -4
  124. package/src/biz/auth-panel/shell.ts +28 -8
  125. package/src/biz/category-crud/compile.ts +64 -13
  126. package/src/biz/category-crud/index.ts +2 -0
  127. package/src/biz/category-crud/shell.ts +30 -8
  128. package/src/biz/checkout-page/compile.ts +38 -4
  129. package/src/biz/checkout-page/index.ts +1 -0
  130. package/src/biz/checkout-page/shell.ts +28 -10
  131. package/src/biz/compose.ts +9 -4
  132. package/src/biz/data-components.ts +2 -0
  133. package/src/biz/define.ts +1 -1
  134. package/src/biz/import-wizard/compile.ts +5 -2
  135. package/src/biz/import-wizard/index.ts +1 -0
  136. package/src/biz/import-wizard/shell.ts +36 -20
  137. package/src/biz/index.ts +5 -4
  138. package/src/biz/landing-page/compile.ts +4 -3
  139. package/src/biz/landing-page/shell.ts +25 -5
  140. package/src/biz/layout-contracts.ts +918 -0
  141. package/src/biz/link/hub.ts +199 -8
  142. package/src/biz/link/shared.ts +62 -16
  143. package/src/biz/master-sub/compile.ts +318 -38
  144. package/src/biz/master-sub/detail-head.ts +145 -21
  145. package/src/biz/master-sub/index.ts +6 -3
  146. package/src/biz/master-sub/shell.ts +232 -66
  147. package/src/biz/multi-board/compile.ts +89 -38
  148. package/src/biz/multi-board/composition.ts +7 -20
  149. package/src/biz/multi-board/index.ts +25 -9
  150. package/src/biz/multi-board/prose-fold.ts +17 -13
  151. package/src/biz/multi-board/shell.ts +153 -38
  152. package/src/biz/names.ts +1 -0
  153. package/src/biz/notification-center/compile.ts +28 -7
  154. package/src/biz/notification-center/index.ts +2 -0
  155. package/src/biz/notification-center/shell.ts +51 -11
  156. package/src/biz/page-templates/domain-templates.ts +147 -20
  157. package/src/biz/page-templates/result-page/compile.ts +5 -2
  158. package/src/biz/page-templates/result-page/index.ts +1 -0
  159. package/src/biz/page-templates/result-page/shell.ts +20 -6
  160. package/src/biz/page-templates/shared.ts +1 -1
  161. package/src/biz/page-templates/sign-in-page/compile.ts +20 -9
  162. package/src/biz/page-templates/sign-in-page/index.ts +1 -0
  163. package/src/biz/page-templates/sign-in-page/shell.ts +30 -14
  164. package/src/biz/record-detail/compile.ts +43 -12
  165. package/src/biz/record-detail/index.ts +1 -0
  166. package/src/biz/record-detail/shell.ts +42 -11
  167. package/src/biz/settings-center/compile.ts +5 -2
  168. package/src/biz/settings-center/index.ts +1 -0
  169. package/src/biz/settings-center/shell.ts +33 -9
  170. package/src/biz/shared/action-host.ts +146 -48
  171. package/src/biz/shared/compile-helpers.ts +134 -32
  172. package/src/biz/shared/coordinator.ts +727 -163
  173. package/src/biz/shared/focus-return.ts +180 -0
  174. package/src/biz/shared/object-aliases.ts +7 -1
  175. package/src/biz/shared/overlay-compile.ts +102 -24
  176. package/src/biz/shared/overlay-shell.ts +97 -116
  177. package/src/biz/shared/page-form-actions.ts +151 -0
  178. package/src/biz/shared/page-form.ts +255 -0
  179. package/src/biz/shared/pipeline.ts +74 -0
  180. package/src/biz/shared/runtime-hub.ts +101 -8
  181. package/src/biz/shared/verbs.ts +129 -0
  182. package/src/biz/single-form/compile.ts +6 -4
  183. package/src/biz/single-form/index.ts +2 -0
  184. package/src/biz/single-form/shell.ts +22 -8
  185. package/src/biz/standard-crud/compile.ts +213 -38
  186. package/src/biz/standard-crud/index.ts +2 -1
  187. package/src/biz/standard-crud/shell.ts +90 -13
  188. package/src/biz/standard-crud/summary.ts +2 -35
  189. package/src/biz/step-form/compile.ts +13 -4
  190. package/src/biz/step-form/hub.ts +168 -49
  191. package/src/biz/step-form/index.ts +1 -0
  192. package/src/biz/step-form/shell.ts +35 -17
  193. package/src/biz/types.ts +49 -6
  194. package/src/cli/app-layer-check.ts +375 -0
  195. package/src/cli/kit-cli.ts +179 -33
  196. package/src/cli/kit-init.ts +152 -3
  197. package/src/cli/migrate/theme-map.ts +35 -28
  198. package/src/cli/migrate/ts-rules.ts +2 -2
  199. package/src/cli/migrate/types.ts +3 -3
  200. package/src/cli/migrate/yaml-rules.ts +11 -9
  201. package/src/cli/view-writes.ts +13 -0
  202. package/src/compile/access-gate-attach.ts +1 -1
  203. package/src/compile/action-host-attach.ts +1 -1
  204. package/src/compile/closed-set.ts +102 -6
  205. package/src/compile/compile-page.ts +461 -273
  206. package/src/compile/component-candidates.ts +58 -5
  207. package/src/compile/danger-tier.ts +65 -0
  208. package/src/compile/hints.ts +6 -59
  209. package/src/compile/index.ts +2 -2
  210. package/src/compile/info-structure.ts +155 -0
  211. package/src/compile/layout-lint/index.ts +4 -1
  212. package/src/compile/layout-lint/rules-composition.ts +44 -13
  213. package/src/compile/layout-lint/rules-table.ts +4 -19
  214. package/src/compile/look-page.ts +551 -0
  215. package/src/compile/look.ts +376 -0
  216. package/src/compile/page-composition.ts +405 -67
  217. package/src/compile/page-hierarchy.ts +131 -0
  218. package/src/compile/page-pipeline-look.ts +156 -0
  219. package/src/compile/page-pipeline.ts +200 -0
  220. package/src/compile/parse.ts +12 -11
  221. package/src/compile/redirect.ts +1 -1
  222. package/src/compile/relation-render.ts +120 -0
  223. package/src/compile/remove-guard.ts +115 -41
  224. package/src/compile/stages.ts +786 -0
  225. package/src/compile/styles.ts +286 -48
  226. package/src/compile/types.ts +63 -6
  227. package/src/compile/view-check.ts +190 -1
  228. package/src/data/access-gate.ts +17 -9
  229. package/src/data/amount-breakdown.ts +52 -23
  230. package/src/data/anchor-nav.ts +77 -16
  231. package/src/data/approval-flow.ts +217 -126
  232. package/src/data/assignment-picker.ts +182 -94
  233. package/src/data/async-result-panel.ts +114 -46
  234. package/src/data/audit-log.ts +461 -0
  235. package/src/data/breadcrumbs.ts +150 -24
  236. package/src/data/calendar.ts +436 -97
  237. package/src/data/card-grid.ts +319 -0
  238. package/src/data/carousel.ts +81 -41
  239. package/src/data/cell-value.ts +253 -0
  240. package/src/data/chart.ts +1053 -96
  241. package/src/data/checklist.ts +82 -38
  242. package/src/data/code-view.ts +296 -44
  243. package/src/data/comment-thread.ts +611 -86
  244. package/src/data/compare-view.ts +227 -0
  245. package/src/data/confirm-dialog.ts +250 -39
  246. package/src/data/context-menu.ts +95 -21
  247. package/src/data/d9-limits.ts +2 -0
  248. package/src/data/d9-util.ts +13 -5
  249. package/src/data/diff.ts +109 -49
  250. package/src/data/display-util.ts +9 -4
  251. package/src/data/dock.ts +38 -18
  252. package/src/data/embed.ts +143 -8
  253. package/src/data/engine-ports.ts +135 -0
  254. package/src/data/field-mapper.ts +116 -54
  255. package/src/data/file-list.ts +522 -0
  256. package/src/data/flow-editor.ts +695 -158
  257. package/src/data/flow-node-options.ts +24 -5
  258. package/src/data/flow-remote-options.ts +94 -19
  259. package/src/data/footer.ts +88 -26
  260. package/src/data/form-draft-registry.ts +183 -12
  261. package/src/data/gantt.ts +640 -117
  262. package/src/data/graph-view.ts +51 -0
  263. package/src/data/heat-calendar.ts +412 -0
  264. package/src/data/hero.ts +40 -13
  265. package/src/data/image-viewer.ts +265 -62
  266. package/src/data/index.ts +75 -6
  267. package/src/data/item-list.ts +219 -58
  268. package/src/data/json-view.ts +909 -0
  269. package/src/data/kanban.ts +346 -91
  270. package/src/data/kit-contract.ts +230 -0
  271. package/src/data/leaderboard.ts +343 -0
  272. package/src/data/level-class.ts +21 -0
  273. package/src/data/log-stream.ts +347 -0
  274. package/src/data/markdown-view.ts +597 -0
  275. package/src/data/matrix.ts +234 -120
  276. package/src/data/media-grid.ts +165 -38
  277. package/src/data/mega-menu.ts +43 -26
  278. package/src/data/menu-bar.ts +254 -0
  279. package/src/data/menu-dom.ts +55 -19
  280. package/src/data/menu.ts +138 -48
  281. package/src/data/mockup.ts +37 -25
  282. package/src/data/modal-dialog.ts +126 -98
  283. package/src/data/navbar.ts +152 -66
  284. package/src/data/notice-list.ts +101 -49
  285. package/src/data/notify-renderer.ts +80 -15
  286. package/src/data/object-form.ts +1504 -164
  287. package/src/data/org-chart.ts +544 -0
  288. package/src/data/outcome-watch.ts +90 -21
  289. package/src/data/pager.ts +129 -45
  290. package/src/data/panel-head.ts +238 -76
  291. package/src/data/permission-matrix.ts +379 -120
  292. package/src/data/permission-tree.ts +146 -78
  293. package/src/data/pricing-table.ts +112 -43
  294. package/src/data/prompt-dialog.ts +49 -21
  295. package/src/data/reaction-bar.ts +67 -32
  296. package/src/data/record-card.ts +245 -72
  297. package/src/data/record-picker.ts +61 -28
  298. package/src/data/redirect.ts +39 -15
  299. package/src/data/request-console.ts +141 -38
  300. package/src/data/resident.ts +11 -13
  301. package/src/data/result-panel.ts +65 -27
  302. package/src/data/rich-text.ts +260 -127
  303. package/src/data/row-confirm.ts +47 -0
  304. package/src/data/row-editor.ts +384 -105
  305. package/src/data/rule-builder.ts +136 -81
  306. package/src/data/schedule-grid.ts +238 -0
  307. package/src/data/segment-bar.ts +221 -0
  308. package/src/data/shape-util.ts +90 -36
  309. package/src/data/stage-flow.ts +244 -0
  310. package/src/data/staged-rows.ts +207 -24
  311. package/src/data/stat-group.ts +493 -100
  312. package/src/data/static-lazy.ts +21 -0
  313. package/src/data/static-registry.ts +46 -4
  314. package/src/data/status-strip.ts +171 -0
  315. package/src/data/steps.ts +69 -36
  316. package/src/data/summary-strip.ts +136 -0
  317. package/src/data/summary-util.ts +116 -0
  318. package/src/data/survey.ts +134 -47
  319. package/src/data/system-switcher.ts +53 -24
  320. package/src/data/target-progress.ts +208 -0
  321. package/src/data/text-action-button.ts +28 -7
  322. package/src/data/text-diff.ts +731 -0
  323. package/src/data/tile-grid.ts +343 -0
  324. package/src/data/timeline.ts +205 -36
  325. package/src/data/tour-guide.ts +113 -47
  326. package/src/data/transfer.ts +204 -47
  327. package/src/data/tree-table.ts +228 -81
  328. package/src/data/tree.ts +193 -69
  329. package/src/data/trend-line.ts +68 -12
  330. package/src/data/virtual-rows.ts +248 -98
  331. package/src/data/waterfall.ts +171 -0
  332. package/src/data/watermark.ts +35 -6
  333. package/src/data/wrap.ts +28 -6
  334. package/src/frames/blank-frame.ts +6 -4
  335. package/src/frames/index.ts +4 -4
  336. package/src/frames/inherited-shell.ts +219 -0
  337. package/src/frames/page-bar.ts +208 -28
  338. package/src/frames/page-status.ts +3 -2
  339. package/src/frames/resident-components.ts +18 -0
  340. package/src/frames/shared.ts +73 -26
  341. package/src/frames/single-frame.ts +20 -4
  342. package/src/frames/split-frame/shell.ts +27 -17
  343. package/src/frames/split-frame/wiring.ts +16 -12
  344. package/src/frames/tabs-frame/shell.ts +14 -7
  345. package/src/frames/tabs-frame/wiring.ts +2 -1
  346. package/src/frames/top-main-frame.ts +55 -3
  347. package/src/frames/types.ts +5 -2
  348. package/src/frames/workspace-frame/extras.ts +23 -7
  349. package/src/frames/workspace-frame/index.ts +1 -1
  350. package/src/frames/workspace-frame/pipeline-key.ts +6 -0
  351. package/src/frames/workspace-frame/shell.ts +24 -3
  352. package/src/frames/workspace-frame/state.ts +42 -8
  353. package/src/frames/workspace-frame/view.ts +773 -194
  354. package/src/generated/kit-grammar-runtime.ts +1061 -77
  355. package/src/generated/kit-page.ts +272 -0
  356. package/src/i18n/index.ts +2 -0
  357. package/src/i18n/kit-text.ts +210 -5
  358. package/src/i18n/resolve-text-refs.ts +1 -1
  359. package/src/object-renderer/action-group.ts +204 -22
  360. package/src/object-renderer/action-more.ts +66 -19
  361. package/src/object-renderer/arrange.ts +270 -28
  362. package/src/object-renderer/category-tone.ts +96 -0
  363. package/src/object-renderer/collapse-wiring.ts +19 -9
  364. package/src/object-renderer/collection-expand.ts +3 -3
  365. package/src/object-renderer/collection-parts.ts +459 -103
  366. package/src/object-renderer/collection-table.ts +406 -59
  367. package/src/object-renderer/collection.ts +333 -71
  368. package/src/object-renderer/compile-data.ts +13 -3
  369. package/src/object-renderer/defaults.ts +14 -5
  370. package/src/object-renderer/emit-events.ts +46 -20
  371. package/src/object-renderer/emit-object.ts +35 -6
  372. package/src/object-renderer/emit-static.ts +831 -153
  373. package/src/object-renderer/field-value.ts +12 -239
  374. package/src/object-renderer/filter-plan.ts +3 -3
  375. package/src/object-renderer/filter-toolbar.ts +190 -0
  376. package/src/object-renderer/form-detail.ts +87 -2
  377. package/src/{compile → object-renderer}/fragment.ts +2 -2
  378. package/src/object-renderer/index.ts +1 -1
  379. package/src/object-renderer/key-field.ts +21 -0
  380. package/src/object-renderer/layout-facts.ts +55 -7
  381. package/src/object-renderer/layout-plan.ts +6 -3
  382. package/src/object-renderer/overlay-decide.ts +124 -0
  383. package/src/object-renderer/pick-component.ts +8 -2
  384. package/src/object-renderer/pipeline-mode.ts +6 -0
  385. package/src/object-renderer/relation-field.ts +79 -0
  386. package/src/object-renderer/render-object.ts +72 -27
  387. package/src/object-renderer/request-actions.ts +119 -21
  388. package/src/object-renderer/table-actions.ts +479 -111
  389. package/src/object-renderer/table-gap.ts +2 -1
  390. package/src/object-renderer/table-reorder.ts +237 -0
  391. package/src/object-renderer/table-summary.ts +26 -12
  392. package/src/object-renderer/table-tools.ts +437 -130
  393. package/src/object-renderer/types.ts +100 -17
  394. package/src/object-renderer/value-present.ts +49 -0
  395. package/src/page-target/ai-surface.ts +15 -22
  396. package/src/page-target/compile.ts +5 -6
  397. package/src/page-target/drift.ts +0 -7
  398. package/src/page-target/intent-schema.ts +46 -43
  399. package/src/page-target/target.ts +4 -5
  400. package/src/page-target/writer-policy.ts +4 -5
  401. package/src/recipes/app-blocks.ts +66 -0
  402. package/src/recipes/chrome-surface.ts +20 -0
  403. package/src/recipes/code-view.ts +57 -0
  404. package/src/recipes/color-role.ts +442 -0
  405. package/src/recipes/compact-group.ts +32 -0
  406. package/src/recipes/composition-projector.ts +321 -0
  407. package/src/recipes/container.ts +93 -26
  408. package/src/recipes/control.ts +85 -8
  409. package/src/recipes/desc-list.ts +108 -0
  410. package/src/recipes/divider.ts +74 -0
  411. package/src/recipes/emphasis.ts +102 -13
  412. package/src/recipes/field-text.ts +72 -0
  413. package/src/recipes/form-section.ts +177 -0
  414. package/src/recipes/frame-bleed.ts +6 -5
  415. package/src/recipes/index.ts +39 -5
  416. package/src/recipes/inherited-shell.ts +43 -0
  417. package/src/recipes/json-view.ts +50 -0
  418. package/src/recipes/look.ts +56 -0
  419. package/src/recipes/mark.ts +138 -0
  420. package/src/recipes/markdown.ts +96 -0
  421. package/src/recipes/overlay.ts +151 -37
  422. package/src/recipes/page-width.ts +51 -0
  423. package/src/recipes/pipeline-runtime.ts +366 -0
  424. package/src/recipes/placeholder.ts +38 -20
  425. package/src/recipes/rail-grid.ts +6 -3
  426. package/src/recipes/self-owned.ts +59 -0
  427. package/src/recipes/slot-align.ts +5 -3
  428. package/src/recipes/stat-state.ts +6 -6
  429. package/src/recipes/state.ts +64 -11
  430. package/src/recipes/status.ts +21 -20
  431. package/src/recipes/strip.ts +114 -0
  432. package/src/recipes/surface-edge.ts +3 -1
  433. package/src/recipes/surface.ts +40 -7
  434. package/src/recipes/table-gap.ts +4 -3
  435. package/src/recipes/table-native-edit.ts +25 -12
  436. package/src/recipes/table-row.ts +56 -20
  437. package/src/recipes/tabs.ts +140 -31
  438. package/src/recipes/text-diff.ts +97 -0
  439. package/src/recipes/text-fit.ts +132 -0
  440. package/src/recipes/title-row.ts +72 -5
  441. package/src/recipes/typography.ts +2 -2
  442. package/src/shared/action-func.ts +106 -0
  443. package/src/shared/color-scheme.ts +127 -0
  444. package/src/shared/confirm-decl.ts +5 -2
  445. package/src/shared/danger-facts.ts +55 -0
  446. package/src/shared/diagnostics.ts +16 -0
  447. package/src/shared/dom-attrs-registry.ts +1009 -0
  448. package/src/shared/dom-attrs.ts +743 -0
  449. package/src/shared/envelope.ts +1 -1
  450. package/src/shared/field-kind.ts +49 -0
  451. package/src/shared/field-value-format.ts +292 -0
  452. package/src/shared/focus-ring.ts +8 -0
  453. package/src/shared/glyphs.ts +127 -0
  454. package/src/shared/hints.ts +56 -0
  455. package/src/{base → shared}/icons.ts +40 -1
  456. package/src/shared/identifier-field.ts +66 -0
  457. package/src/shared/kit-error.ts +1 -1
  458. package/src/shared/list-pager.ts +23 -0
  459. package/src/shared/list-title.ts +29 -0
  460. package/src/{object-renderer → shared}/merge-class.ts +3 -2
  461. package/src/shared/order-value.ts +15 -0
  462. package/src/shared/page-attrs-runtime.ts +327 -0
  463. package/src/shared/page-attrs.ts +262 -0
  464. package/src/shared/path-guard.ts +1 -1
  465. package/src/shared/pipeline-mode.ts +64 -0
  466. package/src/shared/primitives-types.ts +8 -0
  467. package/src/shared/row-key.ts +15 -0
  468. package/src/shared/theme-runtime-shared.ts +181 -21
  469. package/src/shared/theme-usage.ts +16 -0
  470. package/src/shared/value-table-contract.ts +68 -0
  471. package/src/spatial/action-func.ts +4 -0
  472. package/src/spatial/declared-actions.ts +116 -0
  473. package/src/spatial/emit.ts +660 -0
  474. package/src/spatial/expand.ts +1482 -0
  475. package/src/spatial/field-types.ts +48 -0
  476. package/src/spatial/index.ts +82 -0
  477. package/src/spatial/layout.ts +697 -0
  478. package/src/spatial/look-apply.ts +206 -0
  479. package/src/spatial/page-decl.ts +44 -0
  480. package/src/spatial/page-object.ts +349 -0
  481. package/src/spatial/recognize.ts +491 -0
  482. package/src/spatial/self-report.ts +571 -0
  483. package/src/spatial/shape.ts +205 -0
  484. package/src/spatial/spatial-map.ts +136 -0
  485. package/src/spatial/tone.ts +145 -0
  486. package/src/theme/index.ts +11 -2
  487. package/src/theme/kit-theme-text.ts +1 -1
  488. package/src/theme/kit-theme.css +563 -28
  489. package/src/theme/kit-theme.tpl +527 -28
  490. package/src/theme/look-carrier.ts +65 -0
  491. package/src/theme/nine-dim-structure.ts +538 -176
  492. package/src/theme/private-vars.ts +153 -0
  493. package/src/theme/surface-layers.ts +12 -3
  494. package/src/theme/theme-tokens.ts +1602 -90
  495. package/src/theme/theme-types.ts +38 -59
  496. package/src/theme/value-table.ts +2379 -0
  497. package/src/app/envelope.ts +0 -1
  498. package/src/app/notify-renderer.ts +0 -1
  499. package/src/app/paged-list-adapter.ts +0 -1
  500. package/src/app/theme-runtime-shared.ts +0 -1
  501. package/src/compile/kit-error.ts +0 -1
  502. package/src/frames/list-summary.ts +0 -100
  503. package/src/recipes/nowrap.ts +0 -16
  504. package/src/theme/block-theme.ts +0 -202
  505. /package/src/{base → shared}/class-candidates.ts +0 -0
@@ -0,0 +1,909 @@
1
+ // JsonView:JSON / YAML 结构化查看(layer: data,写死形状,accept: single;内容展示批次 ③ 后半,规格 knowledge/Kit主题规则与算法.md §10.17)。
2
+ //
3
+ // 数据:`data` 是任意 JSON 值(对象 / 数组直接按树展示),或 JSON / YAML 原文字符串(经 core 端口 engines.structured 解析,
4
+ // 格式按 format → filename → 「整体能 JSON.parse」确定性推断)。传入即渲染:
5
+ // - 自动决定:初始展开深度(按节点数选档)、子项超过一页时分页「再显示 N 项」、长字符串截断后可展开、记号着色(与 CodeView 同一张记号表)、
6
+ // 节点多时出搜索框(命中路径自动展开、上一处 / 下一处)、「树 / 原文」两态(原文与 CodeView 同一画法)、可见行数上限。
7
+ // - 每个节点可「复制值 / 复制路径」(路径写法 `a.b[0].c`,非标识符键写 `["键 名"]`);抛 `copy { ok, what }`,不带文本。
8
+ // - 降级:空值 → 空态;语法错误 → 说明(第 L 行第 C 列 + 引擎原话)+ 原文(错误行标记);不是结构 / 过大 / 节点过多 / 引擎不可用 → 说明 + 原文;
9
+ // 循环引用 → 节点写「[循环引用]」并在底部注明。
10
+ // 键盘走 core 键盘接线(layout: tree,与 Tree 同一套):↑↓ 可见行,→ 展开 / 进入,← 折叠 / 回父项,Home / End,Enter / 空格激活;
11
+ // 行上按 c 复制值、p 复制路径。展开、分页、截断、搜索、视图都是组件内部状态。数据只经 textContent 写入。
12
+
13
+ import { kitApplyLevelClass } from './level-class';
14
+ import type { SomaComponentDefinition } from 'soma-core';
15
+ import { createKeyboardWiring, type CollectionItem } from 'soma-core/keyboard';
16
+ import { SOMA_SIZE_UNITS } from 'soma-foundation/theme';
17
+ import { KIT_CONTENT_LIMITS } from 'soma-foundation/kit-limits';
18
+ import { bool, createKitController, joinClass, preserveKitScroll, str } from '../base/shared';
19
+ import { kitIcon } from '../shared/icons';
20
+ import { kitText } from '../i18n/kit-text';
21
+ import { KIT_PART_ATTRIBUTE, KIT_PART_WELL } from '../recipes';
22
+ import {
23
+ KIT_JSON_ACTIONS_CLASS, KIT_JSON_CLIP_NOTE_CLASS, KIT_JSON_COLON_CLASS, KIT_JSON_COUNT_CLASS, KIT_JSON_ERROR_DETAIL_CLASS, KIT_JSON_FAIL_BOX_CLASS, KIT_JSON_HEAD_CLASS,
24
+ KIT_JSON_HEIGHT_CLASS, KIT_JSON_HIT_CLASS, KIT_JSON_HIT_CURRENT_CLASS, KIT_JSON_INDENT, KIT_JSON_INDEX_CLASS, KIT_JSON_KEY_CLASS, KIT_JSON_MORE_CLASS, KIT_JSON_ROOT_CLASS,
25
+ KIT_JSON_ROW_CLASS, KIT_JSON_SEARCH_CLASS, KIT_JSON_SEARCH_COUNT_CLASS, KIT_JSON_SPACER_CLASS, KIT_JSON_SUMMARY_CLASS, KIT_JSON_TITLE_CLASS,
26
+ KIT_JSON_TOGGLE_CLASS, KIT_JSON_VALUE_CLASS, KIT_JSON_VIEW_RECIPE_CLASS_CANDIDATES, KIT_JSON_WELL_CLASS,
27
+ } from '../recipes/json-view';
28
+ import { KIT_CODE_LANG_CLASS, KIT_CODE_NOTE_CLASS, KIT_CODE_VIEW_RECIPE_CLASS_CANDIDATES, kitCodeTokenClass } from '../recipes/code-view';
29
+ import { kitPlainActionRecipe } from '../recipes/emphasis';
30
+ import { classTokens, emptyNode, mk } from './shape-util';
31
+ import { kitMarkDataFacts, kitMarkLevel, kitMarkPart } from './kit-contract';
32
+ import { KIT_ATTR_ACTION, KIT_ATTR_CONTAINER, KIT_ATTR_JSON_LIVE, KIT_ATTR_JSON_SEARCH, KIT_ATTR_JSON_TREE, KIT_ATTR_KEY, kitAttrSel } from '../shared/dom-attrs';
33
+ import { codeWrapDefault, paintCodeLines } from './code-view';
34
+ import { kitEnginePort, type KitHighlightEnginePort, type KitStructuredEnginePort, type KitStructuredParseResult } from './engine-ports';
35
+ import { KIT_STATE_PANEL_CLASS_CANDIDATES } from '../base/state-panel';
36
+ import { TEXT_ACTION_CLASS_TOKENS, textActionButton } from './text-action-button';
37
+
38
+ const JL = KIT_CONTENT_LIMITS.jsonView;
39
+
40
+ // ── 纯函数(模型;便于单测) ───────────────────────────────────────────────
41
+
42
+ export type KitJsonPathPart = string | number;
43
+ export type KitJsonKind = 'object' | 'array' | 'string' | 'number' | 'boolean' | 'null';
44
+
45
+ const IDENT = /^[\p{L}_$][\p{L}\p{N}_$]*$/u;
46
+
47
+ /** 路径文字:`a.b[0].c`;非标识符键写 `["键 名"]`;根是 `$` */
48
+ export function jsonPathText(path: readonly KitJsonPathPart[]): string {
49
+ if (path.length === 0) return '$';
50
+ let out = '';
51
+ for (const part of path) {
52
+ if (typeof part === 'number') out += `[${part}]`;
53
+ else if (IDENT.test(part)) out += out === '' ? part : `.${part}`;
54
+ else out += `[${JSON.stringify(part)}]`;
55
+ }
56
+ return out;
57
+ }
58
+
59
+ /** 行键(唯一、可逆):路径的 JSON */
60
+ export const jsonKeyOf = (path: readonly KitJsonPathPart[]): string => JSON.stringify(path);
61
+
62
+ export function jsonKindOf(v: unknown): KitJsonKind {
63
+ if (v === null || v === undefined) return 'null';
64
+ if (Array.isArray(v)) return 'array';
65
+ if (typeof v === 'object') return 'object';
66
+ if (typeof v === 'string') return 'string';
67
+ if (typeof v === 'number' || typeof v === 'bigint') return 'number';
68
+ if (typeof v === 'boolean') return 'boolean';
69
+ return 'string';
70
+ }
71
+
72
+ const isContainer = (v: unknown): v is Record<string, unknown> | unknown[] => v !== null && typeof v === 'object';
73
+
74
+ function entriesOf(v: Record<string, unknown> | unknown[]): Array<[KitJsonPathPart, unknown]> {
75
+ return Array.isArray(v) ? v.map((x, i): [KitJsonPathPart, unknown] => [i, x]) : Object.entries(v);
76
+ }
77
+
78
+ /** 节点数(数到 cap 为止;祖先链上的循环不再深入) */
79
+ export function jsonCount(v: unknown, cap: number): number {
80
+ let n = 0;
81
+ const path = new Set<object>();
82
+ const walk = (x: unknown): void => {
83
+ if (n >= cap) return;
84
+ n += 1;
85
+ if (!isContainer(x) || path.has(x)) return;
86
+ path.add(x);
87
+ for (const [, c] of entriesOf(x)) { walk(c); if (n >= cap) break; }
88
+ path.delete(x);
89
+ };
90
+ walk(v);
91
+ return n;
92
+ }
93
+
94
+ /** 初始展开深度:节点少全展开(到 maxDepth),中等展开两层,多则只展开第一层 */
95
+ export function jsonOpenDepth(nodes: number): number {
96
+ if (nodes <= JL.openAllMax) return JL.maxDepth;
97
+ if (nodes <= JL.openTwoMax) return 2;
98
+ return 1;
99
+ }
100
+
101
+ /** 缺省展开集合:层级 < depth 的容器(层级 1 = 根的子项);遍历以 rowsMax 为预算 */
102
+ export function jsonDefaultOpen(value: unknown, depth: number): Set<string> {
103
+ const open = new Set<string>();
104
+ let budget = JL.rowsMax;
105
+ const walk = (v: unknown, path: KitJsonPathPart[], level: number): void => {
106
+ if (!isContainer(v) || level > depth || budget <= 0) return;
107
+ for (const [k, c] of entriesOf(v).slice(0, JL.pageSize)) {
108
+ budget -= 1;
109
+ if (!isContainer(c) || level >= depth) continue;
110
+ const p = [...path, k];
111
+ open.add(jsonKeyOf(p));
112
+ walk(c, p, level + 1);
113
+ }
114
+ };
115
+ walk(value, [], 1);
116
+ return open;
117
+ }
118
+
119
+ export interface KitJsonRow {
120
+ readonly key: string;
121
+ readonly path: readonly KitJsonPathPart[];
122
+ /** 1 起 */
123
+ readonly level: number;
124
+ readonly type: 'node' | 'more';
125
+ readonly kind: KitJsonKind;
126
+ readonly value: unknown;
127
+ /** 容器的子项数 */
128
+ readonly count: number;
129
+ readonly open: boolean;
130
+ /** 超过 maxDepth 的容器:不可展开 */
131
+ readonly deep: boolean;
132
+ /** 循环引用(祖先链上已出现) */
133
+ readonly cycle: boolean;
134
+ /** more 行:父键与剩余数 */
135
+ readonly parentKey?: string;
136
+ readonly left?: number;
137
+ readonly posinset: number;
138
+ readonly setsize: number;
139
+ }
140
+
141
+ export interface KitJsonViewState {
142
+ readonly open: ReadonlySet<string>;
143
+ /** 父键 → 已显示的子项数(缺省一页) */
144
+ readonly pages: ReadonlyMap<string, number>;
145
+ }
146
+
147
+ /** 可见行(深度优先;容器子项分页;总行数到 rowsMax 截断) */
148
+ export function jsonRows(value: unknown, st: KitJsonViewState): { rows: KitJsonRow[]; capped: boolean } {
149
+ const rows: KitJsonRow[] = [];
150
+ let capped = false;
151
+ const ancestors = new Set<object>();
152
+ const walk = (v: Record<string, unknown> | unknown[], path: KitJsonPathPart[], level: number): void => {
153
+ const entries = entriesOf(v);
154
+ const parentKey = jsonKeyOf(path);
155
+ const shown = Math.min(entries.length, Math.max(JL.pageSize, st.pages.get(parentKey) ?? JL.pageSize));
156
+ const setsize = shown + (shown < entries.length ? 1 : 0);
157
+ ancestors.add(v);
158
+ for (let i = 0; i < shown; i += 1) {
159
+ if (rows.length >= JL.rowsMax) { capped = true; break; }
160
+ const [k, c] = entries[i]!;
161
+ const p = [...path, k];
162
+ const key = jsonKeyOf(p);
163
+ const container = isContainer(c);
164
+ const cycle = container && ancestors.has(c);
165
+ const deep = container && level >= JL.maxDepth;
166
+ const open = container && !cycle && !deep && st.open.has(key);
167
+ rows.push({
168
+ key, path: p, level, type: 'node', kind: jsonKindOf(c), value: c, count: container ? entriesOf(c).length : 0,
169
+ open, deep, cycle, posinset: i + 1, setsize,
170
+ });
171
+ if (open) walk(c as Record<string, unknown> | unknown[], p, level + 1);
172
+ if (capped) break;
173
+ }
174
+ ancestors.delete(v);
175
+ if (!capped && shown < entries.length) {
176
+ if (rows.length >= JL.rowsMax) capped = true;
177
+ else rows.push({
178
+ key: `${parentKey}#more`, path, level, type: 'more', kind: 'null', value: null, count: 0, open: false, deep: false, cycle: false,
179
+ parentKey, left: entries.length - shown, posinset: setsize, setsize,
180
+ });
181
+ }
182
+ };
183
+ if (isContainer(value)) walk(value, [], 1);
184
+ return { rows, capped };
185
+ }
186
+
187
+ /** 标量的显示文字(字符串带引号由渲染层加) */
188
+ export function jsonScalarText(v: unknown): string {
189
+ if (v === null || v === undefined) return 'null';
190
+ if (typeof v === 'string') return v;
191
+ return String(v);
192
+ }
193
+
194
+ export interface KitJsonSearch {
195
+ readonly hits: readonly string[];
196
+ readonly open: ReadonlySet<string>;
197
+ readonly pages: ReadonlyMap<string, number>;
198
+ /** 命中数到上限截止 */
199
+ readonly capped: boolean;
200
+ }
201
+
202
+ /** 搜索:键名或标量文字包含关键字(不分大小写);命中的祖先自动展开、分页放到能看见命中项 */
203
+ export function jsonSearch(value: unknown, query: string): KitJsonSearch {
204
+ const q = query.trim().toLocaleLowerCase();
205
+ const hits: string[] = [];
206
+ const open = new Set<string>();
207
+ const pages = new Map<string, number>();
208
+ if (q === '' || !isContainer(value)) return { hits, open, pages, capped: false };
209
+ let capped = false;
210
+ const ancestors = new Set<object>();
211
+ /** 父链每一级:父容器键 → 必须能看见第 index 项(分页放大到含它的那一页) */
212
+ const showIndex = (parentKey: string, index: number): void => {
213
+ const need = Math.ceil((index + 1) / JL.pageSize) * JL.pageSize;
214
+ if (need > (pages.get(parentKey) ?? JL.pageSize)) pages.set(parentKey, need);
215
+ };
216
+ // idx[j] = path[j] 在其父容器里的序号(对象键按出现顺序)
217
+ const walk = (v: Record<string, unknown> | unknown[], path: KitJsonPathPart[], idx: number[], level: number): void => {
218
+ if (level > JL.maxDepth || ancestors.has(v)) return;
219
+ ancestors.add(v);
220
+ const entries = entriesOf(v);
221
+ for (let i = 0; i < entries.length && !capped; i += 1) {
222
+ const [k, c] = entries[i]!;
223
+ const p = [...path, k];
224
+ const keyHit = typeof k === 'string' && k.toLocaleLowerCase().includes(q);
225
+ const valHit = !isContainer(c) && jsonScalarText(c).toLocaleLowerCase().includes(q);
226
+ if (keyHit || valHit) {
227
+ hits.push(jsonKeyOf(p));
228
+ // 祖先全部展开、每一级的分页都放到能看见下一级
229
+ for (let j = 0; j < path.length; j += 1) {
230
+ open.add(jsonKeyOf(path.slice(0, j + 1)));
231
+ showIndex(jsonKeyOf(path.slice(0, j)), idx[j]!);
232
+ }
233
+ showIndex(jsonKeyOf(path), i);
234
+ if (hits.length >= JL.maxHits) { capped = true; break; }
235
+ }
236
+ if (isContainer(c)) walk(c, p, [...idx, i], level + 1);
237
+ }
238
+ ancestors.delete(v);
239
+ };
240
+ walk(value, [], [], 1);
241
+ return { hits, open, pages, capped };
242
+ }
243
+
244
+ /** 复制「值」的文字:字符串原样,其余 JSON(容器两空格缩进) */
245
+ export function jsonCopyText(v: unknown): string {
246
+ if (typeof v === 'string') return v;
247
+ try { return JSON.stringify(v, null, isContainer(v) ? 2 : undefined) ?? ''; } catch { return ''; }
248
+ }
249
+
250
+ /** 路径取值(行动作用) */
251
+ export function jsonAt(root: unknown, path: readonly KitJsonPathPart[]): unknown {
252
+ let cur: unknown = root;
253
+ for (const part of path) {
254
+ if (!isContainer(cur)) return undefined;
255
+ cur = (cur as Record<string, unknown>)[part as string];
256
+ }
257
+ return cur;
258
+ }
259
+
260
+ /** `path` prop:data 里的点路径(`result.body`、`items.0`);空串 = data 本身;路径断了 = undefined(空态) */
261
+ export function jsonAtDotPath(data: unknown, path: string): unknown {
262
+ const p = path.trim();
263
+ if (p === '') return data;
264
+ let cur: unknown = data;
265
+ for (const part of p.split('.')) {
266
+ if (!isContainer(cur)) return undefined;
267
+ cur = (cur as Record<string, unknown>)[part];
268
+ }
269
+ return cur;
270
+ }
271
+
272
+ // ── 类 ────────────────────────────────────────────────────────────────
273
+
274
+ // 类串全部来自配方 recipes/json-view.ts(G101:组件文件不手写圆角 / 字阶 / 焦点环)
275
+ const ROOT_CLASS = KIT_JSON_ROOT_CLASS;
276
+ const HEAD_CLASS = KIT_JSON_HEAD_CLASS;
277
+ const TITLE_CLASS = KIT_JSON_TITLE_CLASS;
278
+ const COUNT_CLASS = `${KIT_JSON_COUNT_CLASS} tabular-nums`;
279
+ const SPACER_CLASS = KIT_JSON_SPACER_CLASS;
280
+ const SEARCH_CLASS = KIT_JSON_SEARCH_CLASS;
281
+ const SEARCH_COUNT_CLASS = KIT_JSON_SEARCH_COUNT_CLASS;
282
+ const WELL_CLASS = KIT_JSON_WELL_CLASS;
283
+ const HEIGHT_CLASS = KIT_JSON_HEIGHT_CLASS;
284
+ const ROW_CLASS = KIT_JSON_ROW_CLASS;
285
+ const INDENT = KIT_JSON_INDENT;
286
+ const TOGGLE_CLASS = KIT_JSON_TOGGLE_CLASS;
287
+ const INDEX_CLASS = `${KIT_JSON_INDEX_CLASS} tabular-nums`;
288
+ const VALUE_CLASS = KIT_JSON_VALUE_CLASS;
289
+ const SUMMARY_CLASS = `${KIT_JSON_SUMMARY_CLASS} tabular-nums`;
290
+ const CLIP_NOTE_CLASS = `${KIT_JSON_CLIP_NOTE_CLASS} tabular-nums`;
291
+ const MORE_CLASS = `${KIT_JSON_MORE_CLASS} tabular-nums`;
292
+ const ACTIONS_CLASS = KIT_JSON_ACTIONS_CLASS;
293
+ const HIT_CLASS = KIT_JSON_HIT_CLASS;
294
+ const HIT_CURRENT_CLASS = KIT_JSON_HIT_CURRENT_CLASS;
295
+ const NOTE_CLASS = KIT_CODE_NOTE_CLASS;
296
+ const ERROR_DETAIL_CLASS = KIT_JSON_ERROR_DETAIL_CLASS;
297
+ const FAIL_BOX_CLASS = KIT_JSON_FAIL_BOX_CLASS;
298
+
299
+ export const KIT_JSON_VIEW_CLASS_CANDIDATES: readonly string[] = classTokens(
300
+ KIT_JSON_VIEW_RECIPE_CLASS_CANDIDATES, KIT_CODE_LANG_CLASS, NOTE_CLASS, KIT_CODE_VIEW_RECIPE_CLASS_CANDIDATES, KIT_STATE_PANEL_CLASS_CANDIDATES,
301
+ TEXT_ACTION_CLASS_TOKENS, kitPlainActionRecipe('toggle'), 'tabular-nums', 'sr-only',
302
+ );
303
+
304
+ // ── 组件 ──────────────────────────────────────────────────────────────
305
+
306
+ type Source =
307
+ | { kind: 'empty' }
308
+ | { kind: 'value'; value: unknown; nodes: number; cycles: number; text: string; format: 'json' | 'yaml' | 'value' }
309
+ | { kind: 'pending'; text: string; format: 'json' | 'yaml' }
310
+ | { kind: 'fail'; text: string; format: 'json' | 'yaml'; result: Extract<KitStructuredParseResult, { ok: false }> | { ok: false; reason: 'engine-missing'; format: 'json' | 'yaml' } };
311
+
312
+ interface ViewState {
313
+ initialized: boolean;
314
+ alive: boolean;
315
+ given: unknown;
316
+ givenFormat: string;
317
+ givenFilename: string;
318
+ inflightKey: string | null;
319
+ latestServices: unknown;
320
+ source: Source;
321
+ open: Set<string>;
322
+ pages: Map<string, number>;
323
+ unclipped: Set<string>;
324
+ query: string;
325
+ hits: readonly string[];
326
+ hitsCapped: boolean;
327
+ hitAt: number;
328
+ raw: boolean;
329
+ cursor: string | null;
330
+ parseSeq: number;
331
+ rows: KitJsonRow[];
332
+ }
333
+
334
+ const FORMAT_LABEL: Readonly<Record<string, string>> = Object.freeze({ json: 'JSON', yaml: 'YAML', value: 'JSON' });
335
+
336
+ export function createJsonView(): SomaComponentDefinition {
337
+ const states = new WeakMap<HTMLElement, ViewState>();
338
+ const timers = new WeakMap<HTMLElement, ReturnType<typeof setTimeout>>();
339
+
340
+ const stateOf = (root: HTMLElement): ViewState => {
341
+ let st = states.get(root);
342
+ if (st === undefined) {
343
+ st = {
344
+ initialized: false, alive: true, given: undefined, givenFormat: '', givenFilename: '', inflightKey: null, latestServices: undefined,
345
+ source: { kind: 'empty' }, open: new Set(), pages: new Map(), unclipped: new Set(),
346
+ query: '', hits: [], hitsCapped: false, hitAt: -1, raw: false, cursor: null, parseSeq: 0, rows: [],
347
+ };
348
+ states.set(root, st);
349
+ }
350
+ return st;
351
+ };
352
+
353
+ /** 数据变了(或 pending 态需重新发起解析)才重新识别;递增 parseSeq 守卫过期解析结果 */
354
+ const sync = (root: HTMLElement, p: Record<string, unknown>, services: unknown, repaint: () => void): void => {
355
+ const st = stateOf(root);
356
+ const data = jsonAtDotPath(p.data, str(p.path));
357
+ const fmt = str(p.format, 'auto');
358
+ const filename = str(p.filename);
359
+ const port = typeof data === 'string' ? kitEnginePort<KitStructuredEnginePort>(services, 'structured') : null;
360
+ const reqKey = typeof data === 'string' && port !== null ? `${fmt}\0${filename}\0${data}` : null;
361
+ if (st.initialized && data === st.given && fmt === st.givenFormat && filename === st.givenFilename) {
362
+ if (st.source.kind !== 'pending') return;
363
+ if (reqKey !== null && st.inflightKey === reqKey) return;
364
+ }
365
+ st.initialized = true;
366
+ st.given = data;
367
+ st.givenFormat = fmt;
368
+ st.givenFilename = filename;
369
+ st.open = new Set();
370
+ st.pages = new Map();
371
+ st.unclipped = new Set();
372
+ st.hits = [];
373
+ st.hitAt = -1;
374
+ st.cursor = null;
375
+ st.inflightKey = null;
376
+ const mine = (st.parseSeq += 1);
377
+ const ready = (value: unknown, text: string, format: 'json' | 'yaml' | 'value', nodes: number, cycles: number): void => {
378
+ st.source = { kind: 'value', value, text, format, nodes, cycles };
379
+ st.open = jsonDefaultOpen(value, jsonOpenDepth(nodes));
380
+ if (st.query !== '') applySearch(st);
381
+ };
382
+ if (data === undefined || data === null || (typeof data === 'string' && data.trim() === '')) {
383
+ st.source = { kind: 'empty' };
384
+ return;
385
+ }
386
+ if (typeof data !== 'string') {
387
+ ready(data, '', 'value', jsonCount(data, JL.rowsMax * 10), 0);
388
+ return;
389
+ }
390
+ if (port === null) {
391
+ // 宿主没装配结构化端口:JSON 由运行时解析(不涉第三方库),YAML 无法解析 → 原文
392
+ try {
393
+ const v: unknown = JSON.parse(data);
394
+ if (isContainer(v)) { ready(v, data, 'json', jsonCount(v, JL.rowsMax * 10), 0); return; }
395
+ } catch { /* 落到原文 */ }
396
+ st.source = { kind: 'fail', text: data, format: 'json', result: { ok: false, reason: 'engine-missing', format: 'json' } };
397
+ return;
398
+ }
399
+ const format = port.detect(data, { format: fmt === 'auto' ? '' : fmt, filename });
400
+ st.source = { kind: 'pending', text: data, format };
401
+ st.inflightKey = reqKey;
402
+ void port.parse(data, format).then((res) => {
403
+ if (!st.alive || st.parseSeq !== mine) return;
404
+ st.inflightKey = null;
405
+ if (res.ok) ready(res.value, data, res.format, res.nodes, res.cycles);
406
+ else st.source = { kind: 'fail', text: data, format: res.format, result: res };
407
+ repaint();
408
+ }, () => {
409
+ if (!st.alive || st.parseSeq !== mine) return;
410
+ st.inflightKey = null;
411
+ st.source = { kind: 'fail', text: data, format, result: { ok: false, reason: 'engine-load-failed', format } };
412
+ repaint();
413
+ });
414
+ };
415
+
416
+ const applySearch = (st: ViewState): void => {
417
+ if (st.source.kind !== 'value') { st.hits = []; st.hitAt = -1; return; }
418
+ const res = jsonSearch(st.source.value, st.query);
419
+ st.hits = res.hits;
420
+ st.hitsCapped = res.capped;
421
+ st.hitAt = res.hits.length > 0 ? 0 : -1;
422
+ for (const k of res.open) st.open.add(k);
423
+ for (const [k, n] of res.pages) st.pages.set(k, Math.max(n, st.pages.get(k) ?? 0));
424
+ };
425
+
426
+ return {
427
+ contract: {
428
+ name: 'JsonView',
429
+ layer: 'data',
430
+ props: {
431
+ data: { type: 'any', default: null, desc: '任意 JSON 值(对象 / 数组按树展示)或 JSON / YAML 原文字符串(经 core 端口 engines.structured 解析)' },
432
+ path: { type: 'string', default: '', desc: '只看 data 里的一个点路径(如 result.body);路径下是字符串时按原文解析' },
433
+ format: { type: 'enum', values: ['auto', 'json', 'yaml'], default: 'auto', desc: '原文格式;auto 按 filename 扩展名 → 整体能 JSON.parse → YAML 确定性推断' },
434
+ filename: { type: 'string', default: '', desc: '原文的文件名(只用于格式推断与头部显示)' },
435
+ title: { type: 'string', default: '', desc: '标题;缺省显示格式名' },
436
+ view: { type: 'enum', values: ['tree', 'raw'], default: 'tree', desc: '初始视图:tree 结构树 / raw 原文(用户可切换)' },
437
+ searchable: { type: 'enum', values: ['auto', 'on', 'off'], default: 'auto', desc: '搜索框:auto = 节点数达到 kit-limits#content.jsonView.searchMin 才出' },
438
+ copyable: { type: 'boolean', default: true, desc: '头部「复制全部」与每行「复制值 / 复制路径」' },
439
+ max_height: { type: 'enum', values: ['none', 'sm', 'md', 'lg'], default: 'md', desc: '内容区最大高度档,超出滚动' },
440
+ empty_text: { type: 'string', default: '', desc: '没有数据时的文案;缺省取内置文案' },
441
+ aria_label: { type: 'string', default: '', desc: '内容区的无障碍名称(缺省取标题)' },
442
+ class: { type: 'string', default: '', desc: '外部传入的类,追加在根节点类串末尾' },
443
+ },
444
+ emits: {
445
+ copy: { payload: { ok: { type: 'boolean' }, what: { type: 'string' } }, desc: '复制后抛:what = value / path / all;不带文本' },
446
+ view: { payload: { view: { type: 'string' } }, desc: '切换树 / 原文后抛' },
447
+ },
448
+ keyboard: { layout: 'tree', wrap: false, typeahead: false, focus_model: 'roving', space: 'activate' },
449
+ },
450
+ services: ['engines'],
451
+ templateHtml: `<section ${KIT_ATTR_CONTAINER}="card" data-soma-id="root" class="${ROOT_CLASS}"></section>`,
452
+ controllerFactory: createKitController((api) => {
453
+ let host: HTMLElement | null = null;
454
+ const P = (): Record<string, unknown> => api.props();
455
+ const repaint = (): void => { if (host) paint(host, P(), stateOf(host).latestServices ?? api.services); };
456
+
457
+ const items = (): CollectionItem[] => {
458
+ if (!host) return [];
459
+ return stateOf(host).rows.map((r) => ({
460
+ key: r.key, text: r.type === 'more' ? '' : String(r.path[r.path.length - 1] ?? ''), level: r.level - 1,
461
+ ...(r.type === 'node' && r.count > 0 && !r.deep && !r.cycle ? { expanded: r.open } : {}),
462
+ }));
463
+ };
464
+ const pageStep = SOMA_SIZE_UNITS.collection.keyboard.pageStep;
465
+ const wiring = createKeyboardWiring({
466
+ contract: { layout: 'tree', wrap: false, typeahead: false, focus_model: 'roving', space: 'activate' },
467
+ host: () => host?.querySelector<HTMLElement>(kitAttrSel(KIT_ATTR_JSON_TREE)) ?? host ?? document.body,
468
+ items,
469
+ typeaheadDebounceMs: 0,
470
+ paging: { listItems: pageStep.listItems, gridRows: pageStep.gridRows },
471
+ getCursor: () => (host ? stateOf(host).cursor : null),
472
+ setCursor: (key) => { if (host) moveCursor(host, key, true); },
473
+ onActivate: (key) => activate(key),
474
+ onExpand: (key) => setOpen(key, true),
475
+ onCollapse: (key) => setOpen(key, false),
476
+ });
477
+
478
+ const rowOf = (key: string): KitJsonRow | undefined => (host ? stateOf(host).rows.find((r) => r.key === key) : undefined);
479
+
480
+ const setOpen = (key: string, flag: boolean): void => {
481
+ if (!host) return;
482
+ const row = rowOf(key);
483
+ if (!row || row.type !== 'node' || row.count === 0 || row.deep || row.cycle) return;
484
+ const st = stateOf(host);
485
+ if (flag) st.open.add(key); else st.open.delete(key);
486
+ st.cursor = key;
487
+ repaint();
488
+ moveCursor(host, key, true);
489
+ };
490
+
491
+ const activate = (key: string): void => {
492
+ if (!host) return;
493
+ const st = stateOf(host);
494
+ const row = rowOf(key);
495
+ if (!row) return;
496
+ if (row.type === 'more' && row.parentKey !== undefined) {
497
+ st.pages.set(row.parentKey, (st.pages.get(row.parentKey) ?? JL.pageSize) + JL.pageSize);
498
+ repaint();
499
+ moveCursor(host, key, true);
500
+ return;
501
+ }
502
+ if (row.count > 0 && !row.deep && !row.cycle) { setOpen(key, !row.open); return; }
503
+ if (typeof row.value === 'string' && row.value.length > JL.stringClip) {
504
+ if (st.unclipped.has(key)) st.unclipped.delete(key); else st.unclipped.add(key);
505
+ repaint();
506
+ moveCursor(host, key, true);
507
+ }
508
+ };
509
+
510
+ const copy = (text: string, what: 'value' | 'path' | 'all'): void => {
511
+ const r = host;
512
+ void (async () => {
513
+ let ok = false;
514
+ try {
515
+ if (typeof navigator !== 'undefined' && navigator.clipboard) { await navigator.clipboard.writeText(text); ok = true; }
516
+ } catch { ok = false; }
517
+ if (ok && r) {
518
+ const live = r.querySelector<HTMLElement>(kitAttrSel(KIT_ATTR_JSON_LIVE));
519
+ if (live) live.textContent = kitText(what === 'path' ? 'soma.kit.json_copied_path' : 'soma.kit.copied', undefined, r);
520
+ const prev = timers.get(r);
521
+ if (prev !== undefined) clearTimeout(prev);
522
+ timers.set(r, setTimeout(() => { if (live) live.textContent = ''; timers.delete(r); }, KIT_CONTENT_LIMITS.codeView.copyFeedbackMs));
523
+ }
524
+ api.emit('copy', { ok, what });
525
+ })();
526
+ };
527
+
528
+ const copyRow = (key: string, what: 'value' | 'path'): void => {
529
+ if (!host) return;
530
+ const st = stateOf(host);
531
+ const row = rowOf(key);
532
+ if (!row || row.type !== 'node' || st.source.kind !== 'value') return;
533
+ copy(what === 'path' ? jsonPathText(row.path) : jsonCopyText(jsonAt(st.source.value, row.path)), what);
534
+ };
535
+
536
+ const jumpHit = (delta: number): void => {
537
+ if (!host) return;
538
+ const st = stateOf(host);
539
+ if (st.hits.length === 0) return;
540
+ st.hitAt = (st.hitAt + delta + st.hits.length) % st.hits.length;
541
+ st.cursor = st.hits[st.hitAt]!;
542
+ repaint();
543
+ moveCursor(host, st.cursor, false);
544
+ };
545
+
546
+ return {
547
+ attach(root) {
548
+ host = root;
549
+ stateOf(root).alive = true;
550
+ const onClick = (event: Event): void => {
551
+ const t = event.target as HTMLElement | null;
552
+ const act = t?.closest<HTMLElement>(kitAttrSel(KIT_ATTR_ACTION));
553
+ if (act && root.contains(act)) {
554
+ const name = act.getAttribute(KIT_ATTR_ACTION);
555
+ const st = stateOf(root);
556
+ if (name === 'json-view-toggle') {
557
+ st.raw = !st.raw;
558
+ repaint();
559
+ api.emit('view', { view: st.raw ? 'raw' : 'tree' });
560
+ root.querySelector<HTMLElement>(kitAttrSel(KIT_ATTR_ACTION, 'json-view-toggle'))?.focus();
561
+ return;
562
+ }
563
+ if (name === 'json-copy-all') {
564
+ const src = st.source;
565
+ const text = src.kind === 'value' ? (src.text !== '' ? src.text : jsonCopyText(src.value)) : src.kind === 'empty' ? '' : src.text;
566
+ copy(text, 'all');
567
+ return;
568
+ }
569
+ const rowEl = act.closest<HTMLElement>(kitAttrSel(KIT_ATTR_KEY));
570
+ const key = rowEl?.getAttribute(KIT_ATTR_KEY);
571
+ if (key && (name === 'json-copy-value' || name === 'json-copy-path')) {
572
+ event.stopPropagation();
573
+ copyRow(key, name === 'json-copy-path' ? 'path' : 'value');
574
+ return;
575
+ }
576
+ }
577
+ const rowEl = t?.closest<HTMLElement>(`${kitAttrSel(KIT_ATTR_JSON_TREE)} ${kitAttrSel(KIT_ATTR_KEY)}`);
578
+ const key = rowEl?.getAttribute(KIT_ATTR_KEY);
579
+ if (!key || !root.contains(rowEl!)) return;
580
+ stateOf(root).cursor = key;
581
+ activate(key);
582
+ };
583
+ const onKeydown = (event: KeyboardEvent): void => {
584
+ const t = event.target as HTMLElement | null;
585
+ if (t?.matches(kitAttrSel(KIT_ATTR_JSON_SEARCH))) {
586
+ if (event.key === 'Enter') { event.preventDefault(); jumpHit(event.shiftKey ? -1 : 1); }
587
+ else if (event.key === 'Escape' && (t as HTMLInputElement).value !== '') {
588
+ event.preventDefault();
589
+ (t as HTMLInputElement).value = '';
590
+ const st = stateOf(root);
591
+ st.query = '';
592
+ st.hits = [];
593
+ st.hitAt = -1;
594
+ repaint();
595
+ }
596
+ return;
597
+ }
598
+ if (!t?.closest(kitAttrSel(KIT_ATTR_JSON_TREE))) return;
599
+ const st = stateOf(root);
600
+ if (st.cursor === null) st.cursor = t.closest<HTMLElement>(kitAttrSel(KIT_ATTR_KEY))?.getAttribute(KIT_ATTR_KEY) ?? null;
601
+ if (!event.ctrlKey && !event.metaKey && !event.altKey && st.cursor !== null && bool(P().copyable ?? true)) {
602
+ if (event.key === 'c' || event.key === 'p') { event.preventDefault(); copyRow(st.cursor, event.key === 'p' ? 'path' : 'value'); return; }
603
+ }
604
+ if (wiring.onKeydown(event)) event.preventDefault();
605
+ };
606
+ const onInput = (event: Event): void => {
607
+ const t = event.target as HTMLInputElement | null;
608
+ if (!t?.matches(kitAttrSel(KIT_ATTR_JSON_SEARCH))) return;
609
+ const st = stateOf(root);
610
+ st.query = t.value;
611
+ applySearch(st);
612
+ if (st.hitAt >= 0) st.cursor = st.hits[0]!;
613
+ repaint();
614
+ if (st.cursor !== null && st.hitAt >= 0) moveCursor(root, st.cursor, false);
615
+ };
616
+ root.addEventListener('click', onClick);
617
+ root.addEventListener('keydown', onKeydown as EventListener);
618
+ root.addEventListener('input', onInput);
619
+ return () => {
620
+ root.removeEventListener('click', onClick);
621
+ root.removeEventListener('keydown', onKeydown as EventListener);
622
+ root.removeEventListener('input', onInput);
623
+ const tm = timers.get(root);
624
+ if (tm !== undefined) clearTimeout(tm);
625
+ timers.delete(root);
626
+ const st = states.get(root);
627
+ if (st) {
628
+ st.alive = false;
629
+ st.parseSeq += 1;
630
+ st.inflightKey = null;
631
+ }
632
+ states.delete(root);
633
+ host = null;
634
+ };
635
+ },
636
+ render(root, p, renderApi) {
637
+ host = root;
638
+ const st = stateOf(root);
639
+ st.alive = true;
640
+ st.latestServices = renderApi.services;
641
+ if (!st.initialized) st.raw = str(p.view, 'tree') === 'raw';
642
+ sync(root, p, renderApi.services, repaint);
643
+ paint(root, p, renderApi.services);
644
+ },
645
+ };
646
+ }),
647
+ };
648
+
649
+ /** 只把游标挪到某一行(不重画):改 tabindex,必要时无跳滚落焦;行在滚动区外时滚到可见 */
650
+ function moveCursor(root: HTMLElement, key: string, focus: boolean): void {
651
+ const st = stateOf(root);
652
+ st.cursor = key;
653
+ const rows = root.querySelectorAll<HTMLElement>(`${kitAttrSel(KIT_ATTR_JSON_TREE)} ${kitAttrSel(KIT_ATTR_KEY)}`);
654
+ let target: HTMLElement | null = null;
655
+ rows.forEach((el) => {
656
+ const on = el.getAttribute(KIT_ATTR_KEY) === key;
657
+ el.tabIndex = on ? 0 : -1;
658
+ if (on) target = el;
659
+ });
660
+ const el = target as HTMLElement | null;
661
+ if (!el) return;
662
+ if (focus) el.focus({ preventScroll: true });
663
+ if (typeof el.scrollIntoView === 'function') el.scrollIntoView({ block: 'nearest' });
664
+ }
665
+
666
+ function paint(root: HTMLElement, p: Record<string, unknown>, services: unknown): void {
667
+ const st = stateOf(root);
668
+ const src = st.source;
669
+ const doc = root.ownerDocument;
670
+ const hadFocus = doc.activeElement !== null && root.contains(doc.activeElement);
671
+ const focusedSearch = doc.activeElement?.matches?.(kitAttrSel(KIT_ATTR_JSON_SEARCH)) === true;
672
+ const selStart = focusedSearch ? (doc.activeElement as HTMLInputElement).selectionStart : null;
673
+ const copyable = p.copyable === undefined ? true : bool(p.copyable);
674
+ const prevWell = root.querySelector<HTMLElement>(`[${KIT_PART_ATTRIBUTE}="${KIT_PART_WELL}"]`);
675
+ root.className = joinClass(ROOT_CLASS, p.class);
676
+ // 数据状态:没有内容为空;解析失败(语法 / 非结构化 / 过大)为出错,原文仍按原样展示
677
+ kitMarkDataFacts(root, { empty: src.kind === 'empty', error: src.kind === 'fail' });
678
+ if (src.kind === 'empty') {
679
+ root.textContent = '';
680
+ root.appendChild(emptyNode(str(p.empty_text, kitText('soma.kit.empty_content', undefined, root))));
681
+ return;
682
+ }
683
+ for (const ch of Array.from(root.childNodes)) {
684
+ if (ch !== prevWell) ch.remove();
685
+ }
686
+ const format = src.format;
687
+ const title = str(p.title);
688
+ const filename = str(p.filename);
689
+ const label = filename !== '' ? filename : FORMAT_LABEL[format] ?? 'JSON';
690
+
691
+ // 头部:标题(或文件名 / 格式名)· 节点数 · 搜索 · 树 / 原文 · 复制全部
692
+ const head = mk('div', HEAD_CLASS);
693
+ // 头部工具行(标题 · 节点数 · 搜索 · 切换 · 复制)= 面头
694
+ kitMarkLevel(head, 'head'); kitApplyLevelClass(head);
695
+ if (title !== '') {
696
+ const t = mk('span', TITLE_CLASS, title);
697
+ kitMarkPart(t, 'block-title');
698
+ head.appendChild(t);
699
+ }
700
+ head.appendChild(mk('span', KIT_CODE_LANG_CLASS, label));
701
+ if (src.kind === 'value' && isContainer(src.value)) head.appendChild(mk('span', COUNT_CLASS, kitText('soma.kit.json_nodes', { n: src.nodes }, root)));
702
+ head.appendChild(mk('span', SPACER_CLASS));
703
+ const treeable = src.kind === 'value' && isContainer(src.value);
704
+ const searchMode = str(p.searchable, 'auto');
705
+ const searchOn = treeable && !st.raw && (searchMode === 'on' || (searchMode === 'auto' && src.nodes >= JL.searchMin));
706
+ if (searchOn) {
707
+ const input = mk('input', SEARCH_CLASS);
708
+ input.type = 'search';
709
+ input.value = st.query;
710
+ input.placeholder = kitText('soma.kit.json_search', undefined, root);
711
+ input.setAttribute('aria-label', kitText('soma.kit.json_search', undefined, root));
712
+ input.setAttribute(KIT_ATTR_JSON_SEARCH, '');
713
+ head.appendChild(input);
714
+ if (st.query.trim() !== '') {
715
+ const n = st.hits.length;
716
+ const countText = n === 0 ? kitText('soma.kit.json_no_hits', undefined, root)
717
+ : kitText(st.hitsCapped ? 'soma.kit.json_hits_capped' : 'soma.kit.json_hits', { at: st.hitAt + 1, n }, root);
718
+ const c = mk('span', SEARCH_COUNT_CLASS, countText);
719
+ c.setAttribute('aria-live', 'polite');
720
+ head.appendChild(c);
721
+ }
722
+ }
723
+ if (treeable) {
724
+ const toggle = textActionButton('json-view-toggle', kitText(st.raw ? 'soma.kit.json_view_tree' : 'soma.kit.json_view_raw', undefined, root), 'toggle');
725
+ toggle.setAttribute('aria-pressed', st.raw ? 'true' : 'false');
726
+ kitMarkPart(toggle, 'action');
727
+ head.appendChild(toggle);
728
+ }
729
+ if (copyable) {
730
+ const copyAll = textActionButton('json-copy-all', kitText('soma.kit.copy', undefined, root), 'copy-text');
731
+ kitMarkPart(copyAll, 'action');
732
+ head.appendChild(copyAll);
733
+ }
734
+ root.insertBefore(head, prevWell);
735
+
736
+ const well = prevWell ?? mk('div', '');
737
+ if (!prevWell) root.appendChild(well);
738
+ const name = str(p.aria_label) !== '' ? str(p.aria_label) : title !== '' ? title : label;
739
+ const hl = kitEnginePort<KitHighlightEnginePort>(services, 'highlight');
740
+
741
+ if (src.kind === 'fail') {
742
+ const r = src.result;
743
+ const fmtName = FORMAT_LABEL[r.format] ?? 'JSON';
744
+ const note = r.reason === 'syntax'
745
+ ? (r.line !== undefined ? kitText('soma.kit.json_syntax_at', { format: fmtName, line: r.line, column: r.column ?? 1 }, root) : kitText('soma.kit.json_syntax', { format: fmtName }, root))
746
+ : r.reason === 'not-structured' ? kitText('soma.kit.json_not_structured', undefined, root)
747
+ : r.reason === 'too-large' || r.reason === 'too-many-nodes' ? kitText('soma.kit.json_too_large', undefined, root)
748
+ : kitText('soma.kit.json_engine_failed', undefined, root);
749
+ const box = mk('div', FAIL_BOX_CLASS);
750
+ box.setAttribute('role', 'status');
751
+ box.appendChild(mk('p', NOTE_CLASS, note));
752
+ if (r.reason === 'syntax' && r.message) box.appendChild(mk('p', ERROR_DETAIL_CLASS, r.message));
753
+ root.insertBefore(box, well);
754
+ }
755
+
756
+ preserveKitScroll(well, () => {
757
+ well.className = `${WELL_CLASS} ${HEIGHT_CLASS[str(p.max_height, 'md')] ?? HEIGHT_CLASS.md}`;
758
+ well.setAttribute(KIT_PART_ATTRIBUTE, KIT_PART_WELL);
759
+ well.removeAttribute('role');
760
+ well.removeAttribute(KIT_ATTR_CONTAINER);
761
+ well.removeAttribute('aria-label');
762
+ well.removeAttribute(KIT_ATTR_JSON_TREE);
763
+ well.removeAttribute('aria-keyshortcuts');
764
+ well.removeAttribute('tabindex');
765
+ well.textContent = '';
766
+
767
+ const rawInto = (text: string, language: string, marked?: number): void => {
768
+ well.setAttribute('role', 'region');
769
+ well.setAttribute(KIT_ATTR_CONTAINER, 'plain');
770
+ well.setAttribute('aria-label', name);
771
+ well.tabIndex = 0;
772
+ const lines = text.split('\n');
773
+ paintCodeLines(well, text, {
774
+ language, wrap: codeWrapDefault(language, lines), port: hl, root,
775
+ ...(marked !== undefined ? { marked: new Set([marked]) } : {}),
776
+ current: () => st.alive && root.contains(well),
777
+ });
778
+ };
779
+
780
+ if (src.kind === 'pending') {
781
+ rawInto(src.text, 'text');
782
+ } else if (src.kind === 'fail') {
783
+ const r = src.result;
784
+ rawInto(src.text, r.reason === 'not-structured' ? 'text' : r.format, r.reason === 'syntax' ? r.line : undefined);
785
+ } else if (st.raw || !isContainer(src.value)) {
786
+ if (!isContainer(src.value)) {
787
+ // 单个标量:一行值
788
+ well.appendChild(scalarNode(src.value, false, root));
789
+ } else {
790
+ const text = src.text !== '' ? src.text : jsonCopyText(src.value);
791
+ rawInto(text, src.format === 'yaml' ? 'yaml' : 'json');
792
+ }
793
+ } else {
794
+ const { rows, capped } = jsonRows(src.value, st);
795
+ st.rows = rows;
796
+ well.setAttribute('role', 'tree');
797
+ well.setAttribute('aria-label', name);
798
+ well.setAttribute(KIT_ATTR_JSON_TREE, '');
799
+ if (copyable) well.setAttribute('aria-keyshortcuts', 'c p');
800
+ if (rows.length === 0) {
801
+ well.appendChild(mk('div', SUMMARY_CLASS, kitText(Array.isArray(src.value) ? 'soma.kit.json_empty_array' : 'soma.kit.json_empty_object', undefined, root)));
802
+ }
803
+ if (st.cursor === null || !rows.some((r) => r.key === st.cursor)) st.cursor = rows[0]?.key ?? null;
804
+ const q = st.query.trim().toLocaleLowerCase();
805
+ const hitSet = new Set(st.hits);
806
+ const current = st.hitAt >= 0 ? st.hits[st.hitAt] : undefined;
807
+ for (const r of rows) well.appendChild(rowNode(r, st, q, hitSet.has(r.key), r.key === current, copyable, root));
808
+ if (capped) well.appendChild(mk('p', NOTE_CLASS, kitText('soma.kit.json_rows_capped', { n: JL.rowsMax }, root)));
809
+ if (src.cycles > 0) well.appendChild(mk('p', NOTE_CLASS, kitText('soma.kit.json_cycles', { n: src.cycles }, root)));
810
+ }
811
+ });
812
+
813
+ const live = mk('span', 'sr-only');
814
+ live.setAttribute('aria-live', 'polite');
815
+ live.setAttribute(KIT_ATTR_JSON_LIVE, '');
816
+ root.appendChild(live);
817
+
818
+ // 重画后把焦点还回去:搜索框保持光标位置;树里无跳滚落到游标行
819
+ if (focusedSearch) {
820
+ const input = root.querySelector<HTMLInputElement>(kitAttrSel(KIT_ATTR_JSON_SEARCH));
821
+ if (input) { input.focus({ preventScroll: true }); if (selStart !== null) input.setSelectionRange(selStart, selStart); }
822
+ } else if (hadFocus && st.cursor !== null && !st.raw) {
823
+ root.querySelector<HTMLElement>(`[${KIT_ATTR_JSON_TREE}] [${KIT_ATTR_KEY}="${cssEscape(st.cursor)}"]`)?.focus({ preventScroll: true });
824
+ }
825
+ }
826
+
827
+ function textWithHit(text: string, q: string, hit: boolean, current: boolean): Node[] {
828
+ if (!hit || q === '') return [document.createTextNode(text)];
829
+ const at = text.toLocaleLowerCase().indexOf(q);
830
+ if (at < 0) return [document.createTextNode(text)];
831
+ const mark = mk('mark', current ? HIT_CURRENT_CLASS : HIT_CLASS, text.slice(at, at + q.length));
832
+ return [document.createTextNode(text.slice(0, at)), mark, document.createTextNode(text.slice(at + q.length))];
833
+ }
834
+
835
+ function scalarNode(v: unknown, unclipped: boolean, root: HTMLElement, q = '', hit = false, current = false): HTMLElement {
836
+ const kind = jsonKindOf(v);
837
+ const cls = kind === 'string' ? kitCodeTokenClass('string') : kind === 'number' ? kitCodeTokenClass('number') : kitCodeTokenClass('constant');
838
+ const box = mk('span', VALUE_CLASS);
839
+ kitMarkPart(box, 'value');
840
+ const text = jsonScalarText(v);
841
+ const clip = kind === 'string' && !unclipped && text.length > JL.stringClip;
842
+ const shown = clip ? `${text.slice(0, JL.stringClip)}…` : text;
843
+ const val = mk('span', cls);
844
+ for (const n of textWithHit(kind === 'string' ? `"${shown}"` : shown, q, hit, current)) val.appendChild(n);
845
+ box.appendChild(val);
846
+ if (clip) box.appendChild(mk('span', CLIP_NOTE_CLASS, kitText('soma.kit.json_clipped', { n: text.length }, root)));
847
+ return box;
848
+ }
849
+
850
+ function rowNode(r: KitJsonRow, st: ViewState, q: string, hit: boolean, current: boolean, copyable: boolean, root: HTMLElement): HTMLElement {
851
+ const row = mk('div', `${ROW_CLASS} ${INDENT[Math.min(r.level - 1, INDENT.length - 1)]}`);
852
+ row.setAttribute('role', 'treeitem');
853
+ row.setAttribute('aria-level', String(r.level));
854
+ row.setAttribute('aria-setsize', String(r.setsize));
855
+ row.setAttribute('aria-posinset', String(r.posinset));
856
+ row.setAttribute(KIT_ATTR_KEY, r.key);
857
+ // 每个节点行 = 集合条目
858
+ kitMarkLevel(row, 'item', 'line'); kitApplyLevelClass(row);
859
+ row.tabIndex = r.key === st.cursor ? 0 : -1;
860
+ const toggle = mk('span', TOGGLE_CLASS);
861
+ toggle.setAttribute('aria-hidden', 'true');
862
+ row.appendChild(toggle);
863
+ if (r.type === 'more') {
864
+ row.appendChild(mk('span', MORE_CLASS, kitText('soma.kit.json_more', { n: r.left ?? 0 }, root)));
865
+ return row;
866
+ }
867
+ const expandable = r.count > 0 && !r.deep && !r.cycle;
868
+ if (expandable) {
869
+ row.setAttribute('aria-expanded', r.open ? 'true' : 'false');
870
+ toggle.appendChild(kitIcon(r.open ? 'chevron-down' : 'chevron-right', 'sm', 'rtl:-scale-x-100'));
871
+ }
872
+ const last = r.path[r.path.length - 1]!;
873
+ if (typeof last === 'number') row.appendChild(mk('span', INDEX_CLASS, String(last)));
874
+ else {
875
+ const k = mk('span', `${KIT_JSON_KEY_CLASS} ${kitCodeTokenClass('property')}`);
876
+ kitMarkPart(k, 'field-name');
877
+ for (const n of textWithHit(last, q, hit, current)) k.appendChild(n);
878
+ row.appendChild(k);
879
+ }
880
+ row.appendChild(mk('span', `${KIT_JSON_COLON_CLASS} ${kitCodeTokenClass('punctuation')}`, ':'));
881
+ if (r.cycle) {
882
+ row.appendChild(mk('span', SUMMARY_CLASS, kitText('soma.kit.json_cycle', undefined, root)));
883
+ } else if (r.kind === 'object' || r.kind === 'array') {
884
+ const brace = r.kind === 'array' ? ['[', ']'] : ['{', '}'];
885
+ const s = mk('span', SUMMARY_CLASS);
886
+ s.appendChild(mk('span', kitCodeTokenClass('punctuation'), r.open ? brace[0]! : `${brace[0]}…${brace[1]}`));
887
+ s.appendChild(document.createTextNode(` ${kitText(r.kind === 'array' ? 'soma.kit.json_items' : 'soma.kit.json_keys', { n: r.count }, root)}`));
888
+ if (r.deep) s.appendChild(document.createTextNode(` ${kitText('soma.kit.json_too_deep', undefined, root)}`));
889
+ row.appendChild(s);
890
+ } else {
891
+ row.appendChild(scalarNode(r.value, st.unclipped.has(r.key), root, q, hit, current));
892
+ }
893
+ if (copyable) {
894
+ const acts = mk('span', ACTIONS_CLASS);
895
+ for (const [action, text] of [['json-copy-value', 'soma.kit.json_copy_value'], ['json-copy-path', 'soma.kit.json_copy_path']] as const) {
896
+ const b = textActionButton(action, kitText(text, undefined, root), 'copy-text');
897
+ b.tabIndex = -1;
898
+ kitMarkPart(b, 'action');
899
+ acts.appendChild(b);
900
+ }
901
+ row.appendChild(acts);
902
+ }
903
+ return row;
904
+ }
905
+ }
906
+
907
+ function cssEscape(value: string): string {
908
+ return value.replace(/["\\]/g, '\\$&');
909
+ }