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.
- package/README.md +539 -2
- package/bin/soma-kit.mjs +27 -0
- package/package.json +66 -4
- package/packs/ai/agent-run-card.ts +64 -0
- package/packs/ai/evaluation-card.ts +51 -0
- package/packs/ai/guardrail-policy-card.ts +48 -0
- package/packs/ai/index.ts +31 -0
- package/packs/ai/model-card.ts +55 -0
- package/packs/ai/model-routing-card.ts +49 -0
- package/packs/ai/prompt-template-card.ts +49 -0
- package/packs/ai/texts.ts +19 -0
- package/packs/commerce/address-card.ts +58 -0
- package/packs/commerce/coupon-card.ts +64 -0
- package/packs/commerce/index.ts +25 -0
- package/packs/commerce/order-card.ts +60 -0
- package/packs/commerce/purchase-order-card.ts +74 -0
- package/packs/commerce/texts.ts +14 -0
- package/packs/finance/index.ts +30 -0
- package/packs/finance/invoice-summary-card.ts +48 -0
- package/packs/finance/reconcile-board.ts +450 -0
- package/packs/finance/reconcile-case-card.ts +70 -0
- package/packs/finance/reconcile-desk/compile.ts +208 -0
- package/packs/finance/reconcile-desk/index.ts +20 -0
- package/packs/finance/reconcile-desk/shell.ts +27 -0
- package/packs/finance/texts.ts +21 -0
- package/packs/ops/change-window-card.ts +54 -0
- package/packs/ops/dependency-status-card.ts +50 -0
- package/packs/ops/error-budget-card.ts +60 -0
- package/packs/ops/execution-task-runner.ts +61 -0
- package/packs/ops/index.ts +36 -0
- package/packs/ops/integration-status-card.ts +45 -0
- package/packs/ops/release-readiness-card.ts +63 -0
- package/packs/ops/runbook-execution-card.ts +81 -0
- package/packs/ops/texts.ts +21 -0
- package/packs/security/control-evidence-card.ts +54 -0
- package/packs/security/finding-card.ts +96 -0
- package/packs/security/index.ts +29 -0
- package/packs/security/texts.ts +31 -0
- package/packs/shared/card-util.ts +60 -0
- package/packs/shared/wrap.ts +13 -0
- package/spec/generated/kit-app.schema.json +84 -0
- package/spec/generated/kit-page.schema.json +2766 -0
- package/spec/kit-grammar.json +1358 -0
- package/spec/kit-grammar.schema.json +716 -0
- package/src/app/app-config.ts +108 -0
- package/src/app/compose-apps.ts +139 -0
- package/src/app/define-kit-actions.ts +89 -0
- package/src/app/define-kit-app.ts +362 -0
- package/src/app/define-kit-pack.ts +114 -0
- package/src/app/envelope.ts +117 -0
- package/src/app/index.ts +50 -0
- package/src/app/notify-renderer.ts +114 -0
- package/src/app/paged-list-adapter.ts +246 -0
- package/src/app/server-pages.ts +303 -0
- package/src/app/server.ts +244 -0
- package/src/app/submit.types.ts +87 -0
- package/src/base/action-busy.ts +159 -0
- package/src/base/action-gate.ts +267 -0
- package/src/base/action-status.ts +31 -0
- package/src/base/alert.ts +136 -0
- package/src/base/avatar.ts +271 -0
- package/src/base/button.ts +139 -0
- package/src/base/cascader.ts +523 -0
- package/src/base/check-core.ts +214 -0
- package/src/base/checkbox.ts +35 -0
- package/src/base/choice.ts +183 -0
- package/src/base/color-picker.ts +316 -0
- package/src/base/combobox.ts +61 -0
- package/src/base/command-palette.ts +371 -0
- package/src/base/context-menu.ts +103 -0
- package/src/base/countdown.ts +168 -0
- package/src/base/date-range.ts +444 -0
- package/src/base/date.ts +899 -0
- package/src/base/divider.ts +156 -0
- package/src/base/fab.ts +192 -0
- package/src/base/icons.ts +106 -0
- package/src/base/image.ts +177 -0
- package/src/base/index.ts +214 -0
- package/src/base/input.ts +316 -0
- package/src/base/kbd.ts +99 -0
- package/src/base/link.ts +167 -0
- package/src/base/listbox-core.ts +965 -0
- package/src/base/mentions.ts +323 -0
- package/src/base/menu-button.ts +240 -0
- package/src/base/menu-core.ts +341 -0
- package/src/base/month-grid.ts +171 -0
- package/src/base/motion/aura.ts +94 -0
- package/src/base/motion/hover-3d.ts +154 -0
- package/src/base/motion/hover-gallery.ts +185 -0
- package/src/base/motion/index.ts +27 -0
- package/src/base/motion/limits.ts +75 -0
- package/src/base/motion/marquee.ts +157 -0
- package/src/base/motion/text-rotate.ts +195 -0
- package/src/base/number.ts +121 -0
- package/src/base/otp.ts +259 -0
- package/src/base/permission-gate.ts +99 -0
- package/src/base/popover.ts +352 -0
- package/src/base/popup.ts +542 -0
- package/src/base/portal-core.ts +297 -0
- package/src/base/price-text.ts +126 -0
- package/src/base/progress.ts +216 -0
- package/src/base/qr-code.ts +446 -0
- package/src/base/qty-stepper.ts +241 -0
- package/src/base/radio.ts +33 -0
- package/src/base/range.ts +444 -0
- package/src/base/rating.ts +249 -0
- package/src/base/segmented.ts +286 -0
- package/src/base/select.ts +57 -0
- package/src/base/shared.ts +708 -0
- package/src/base/swap.ts +86 -0
- package/src/base/switch.ts +111 -0
- package/src/base/tag-input.ts +464 -0
- package/src/base/tag.ts +250 -0
- package/src/base/text.ts +126 -0
- package/src/base/textarea.ts +129 -0
- package/src/base/time-picker.ts +606 -0
- package/src/base/tree-select.ts +629 -0
- package/src/base/upload.ts +548 -0
- package/src/base/virtual-list.ts +286 -0
- package/src/biz/approval-desk/compile.ts +179 -0
- package/src/biz/approval-desk/index.ts +21 -0
- package/src/biz/approval-desk/shell.ts +50 -0
- package/src/biz/auth-panel/compile.ts +196 -0
- package/src/biz/auth-panel/index.ts +24 -0
- package/src/biz/auth-panel/shell.ts +46 -0
- package/src/biz/category-crud/compile.ts +173 -0
- package/src/biz/category-crud/index.ts +19 -0
- package/src/biz/category-crud/shell.ts +29 -0
- package/src/biz/checkout-page/compile.ts +111 -0
- package/src/biz/checkout-page/index.ts +20 -0
- package/src/biz/checkout-page/shell.ts +50 -0
- package/src/biz/compose.ts +313 -0
- package/src/biz/data-components.ts +26 -0
- package/src/biz/define.ts +23 -0
- package/src/biz/import-wizard/compile.ts +204 -0
- package/src/biz/import-wizard/hub.ts +540 -0
- package/src/biz/import-wizard/index.ts +24 -0
- package/src/biz/import-wizard/shell.ts +161 -0
- package/src/biz/index.ts +80 -0
- package/src/biz/landing-page/compile.ts +67 -0
- package/src/biz/landing-page/index.ts +20 -0
- package/src/biz/landing-page/shell.ts +48 -0
- package/src/biz/link/hub.ts +255 -0
- package/src/biz/link/shared.ts +493 -0
- package/src/biz/master-sub/compile.ts +232 -0
- package/src/biz/master-sub/index.ts +20 -0
- package/src/biz/master-sub/shell.ts +56 -0
- package/src/biz/multi-board/compile.ts +234 -0
- package/src/biz/multi-board/index.ts +19 -0
- package/src/biz/multi-board/shell.ts +70 -0
- package/src/biz/names.ts +30 -0
- package/src/biz/notification-center/compile.ts +138 -0
- package/src/biz/notification-center/index.ts +29 -0
- package/src/biz/notification-center/shell.ts +42 -0
- package/src/biz/page-templates/result-page/compile.ts +46 -0
- package/src/biz/page-templates/result-page/index.ts +19 -0
- package/src/biz/page-templates/result-page/shell.ts +31 -0
- package/src/biz/page-templates/shared.ts +121 -0
- package/src/biz/page-templates/sign-in-page/compile.ts +176 -0
- package/src/biz/page-templates/sign-in-page/index.ts +18 -0
- package/src/biz/page-templates/sign-in-page/shell.ts +116 -0
- package/src/biz/record-detail/compile.ts +290 -0
- package/src/biz/record-detail/index.ts +22 -0
- package/src/biz/record-detail/shell.ts +64 -0
- package/src/biz/settings-center/compile.ts +100 -0
- package/src/biz/settings-center/index.ts +18 -0
- package/src/biz/settings-center/shell.ts +51 -0
- package/src/biz/shared/action-host.ts +469 -0
- package/src/biz/shared/coordinator.ts +516 -0
- package/src/biz/shared/index.ts +9 -0
- package/src/biz/shared/pipes.ts +58 -0
- package/src/biz/shared/runtime-hub.ts +112 -0
- package/src/biz/shared/step-values.ts +25 -0
- package/src/biz/single-form/compile.ts +146 -0
- package/src/biz/single-form/index.ts +18 -0
- package/src/biz/single-form/shell.ts +38 -0
- package/src/biz/standard-crud/bridge.ts +11 -0
- package/src/biz/standard-crud/compile.ts +546 -0
- package/src/biz/standard-crud/index.ts +18 -0
- package/src/biz/standard-crud/shell.ts +157 -0
- package/src/biz/step-form/compile.ts +400 -0
- package/src/biz/step-form/hub.ts +412 -0
- package/src/biz/step-form/index.ts +23 -0
- package/src/biz/step-form/shell.ts +96 -0
- package/src/biz/step-form/steps-ref.ts +112 -0
- package/src/biz/types.ts +96 -0
- package/src/cli/kit-cli.ts +102 -0
- package/src/compile/action-host-attach.ts +76 -0
- package/src/compile/compile-page.ts +472 -0
- package/src/compile/component-candidates.ts +147 -0
- package/src/compile/fragment.ts +104 -0
- package/src/compile/hints.ts +255 -0
- package/src/compile/index.ts +16 -0
- package/src/compile/locate.ts +166 -0
- package/src/compile/page-params.ts +80 -0
- package/src/compile/parse.ts +355 -0
- package/src/compile/redirect.ts +90 -0
- package/src/compile/registry.ts +30 -0
- package/src/compile/remove-guard.ts +99 -0
- package/src/compile/styles.ts +244 -0
- package/src/compile/types.ts +106 -0
- package/src/data/amount-breakdown.ts +132 -0
- package/src/data/anchor-nav.ts +225 -0
- package/src/data/approval-flow.ts +269 -0
- package/src/data/assignment-picker.ts +652 -0
- package/src/data/async-result-panel.ts +308 -0
- package/src/data/breadcrumbs.ts +197 -0
- package/src/data/calendar.ts +363 -0
- package/src/data/carousel.ts +408 -0
- package/src/data/chart.ts +726 -0
- package/src/data/checklist.ts +215 -0
- package/src/data/code-view.ts +415 -0
- package/src/data/comment-thread.ts +342 -0
- package/src/data/confirm-dialog.ts +144 -0
- package/src/data/context-menu.ts +216 -0
- package/src/data/d9-limits.ts +42 -0
- package/src/data/d9-util.ts +81 -0
- package/src/data/diff.ts +239 -0
- package/src/data/display-util.ts +135 -0
- package/src/data/dock.ts +152 -0
- package/src/data/embed.ts +200 -0
- package/src/data/field-mapper.ts +544 -0
- package/src/data/flow-editor-contract.ts +41 -0
- package/src/data/flow-editor.ts +2622 -0
- package/src/data/flow-remote-options.ts +257 -0
- package/src/data/footer.ts +157 -0
- package/src/data/form-draft-registry.ts +76 -0
- package/src/data/gantt.ts +430 -0
- package/src/data/hero.ts +149 -0
- package/src/data/image-viewer.ts +333 -0
- package/src/data/index.ts +268 -0
- package/src/data/item-list.ts +335 -0
- package/src/data/kanban.ts +667 -0
- package/src/data/media-grid.ts +246 -0
- package/src/data/mega-menu.ts +257 -0
- package/src/data/menu-dom.ts +169 -0
- package/src/data/menu.ts +464 -0
- package/src/data/mockup.ts +176 -0
- package/src/data/modal-dialog.ts +182 -0
- package/src/data/navbar.ts +292 -0
- package/src/data/object-form.ts +1981 -0
- package/src/data/options-bus.ts +24 -0
- package/src/data/outcome-watch.ts +40 -0
- package/src/data/overlay-portal.ts +6 -0
- package/src/data/pager.ts +473 -0
- package/src/data/permission-matrix.ts +432 -0
- package/src/data/permission-tree.ts +916 -0
- package/src/data/pricing-table.ts +269 -0
- package/src/data/prompt-dialog.ts +235 -0
- package/src/data/reaction-bar.ts +236 -0
- package/src/data/record-card.ts +413 -0
- package/src/data/record-picker.ts +489 -0
- package/src/data/redirect.ts +109 -0
- package/src/data/remote-options.ts +54 -0
- package/src/data/request-console.ts +399 -0
- package/src/data/resident.ts +58 -0
- package/src/data/result-panel.ts +160 -0
- package/src/data/rich-text.ts +626 -0
- package/src/data/row-editor.ts +1066 -0
- package/src/data/rule-builder.ts +1137 -0
- package/src/data/shape-util.ts +221 -0
- package/src/data/staged-rows.ts +69 -0
- package/src/data/stat-group.ts +231 -0
- package/src/data/static-lazy.ts +63 -0
- package/src/data/static-registry.ts +227 -0
- package/src/data/steps.ts +195 -0
- package/src/data/survey.ts +455 -0
- package/src/data/system-switcher.ts +243 -0
- package/src/data/timeline.ts +204 -0
- package/src/data/tour-guide.ts +392 -0
- package/src/data/transfer.ts +597 -0
- package/src/data/tree-check.ts +30 -0
- package/src/data/tree-table.ts +674 -0
- package/src/data/tree.ts +656 -0
- package/src/data/trend-line.ts +147 -0
- package/src/data/virtual-rows.ts +556 -0
- package/src/data/watermark.ts +140 -0
- package/src/data/wrap.ts +267 -0
- package/src/data/wrapped-candidates.ts +96 -0
- package/src/frames/blank-frame.ts +19 -0
- package/src/frames/index.ts +44 -0
- package/src/frames/page-bar.ts +185 -0
- package/src/frames/page-header.ts +185 -0
- package/src/frames/page-status.ts +54 -0
- package/src/frames/resize-handle.ts +147 -0
- package/src/frames/shared.ts +242 -0
- package/src/frames/single-frame.ts +16 -0
- package/src/frames/split-frame/index.ts +4 -0
- package/src/frames/split-frame/shell.ts +219 -0
- package/src/frames/split-frame/span.ts +64 -0
- package/src/frames/split-frame/wiring.ts +164 -0
- package/src/frames/tabs-frame/index.ts +3 -0
- package/src/frames/tabs-frame/shell.ts +112 -0
- package/src/frames/tabs-frame/wiring.ts +69 -0
- package/src/frames/top-main-frame.ts +20 -0
- package/src/frames/types.ts +72 -0
- package/src/frames/workspace-frame/extras.ts +197 -0
- package/src/frames/workspace-frame/index.ts +13 -0
- package/src/frames/workspace-frame/shell.ts +282 -0
- package/src/frames/workspace-frame/state.ts +152 -0
- package/src/frames/workspace-frame/view.ts +1013 -0
- package/src/generated/kit-grammar-runtime.ts +2822 -0
- package/src/generated/page-origin.ts +57 -0
- package/src/i18n/index.ts +22 -0
- package/src/i18n/kit-text.ts +692 -0
- package/src/i18n/resolve-text-refs.ts +128 -0
- package/src/index.ts +6 -0
- package/src/object-renderer/action-gate.ts +216 -0
- package/src/object-renderer/adopt-gate.ts +333 -0
- package/src/object-renderer/arrange.ts +411 -0
- package/src/object-renderer/candidates.ts +68 -0
- package/src/object-renderer/collapse-wiring.ts +99 -0
- package/src/object-renderer/collection-expand.ts +185 -0
- package/src/object-renderer/collection-parts.ts +653 -0
- package/src/object-renderer/collection-table.ts +503 -0
- package/src/object-renderer/collection.ts +628 -0
- package/src/object-renderer/compile-data.ts +549 -0
- package/src/object-renderer/defaults.ts +252 -0
- package/src/object-renderer/display-checks.ts +153 -0
- package/src/object-renderer/emit-events.ts +498 -0
- package/src/object-renderer/emit-object.ts +98 -0
- package/src/object-renderer/emit-static.ts +1036 -0
- package/src/object-renderer/field-policy.ts +89 -0
- package/src/object-renderer/field-value.ts +581 -0
- package/src/object-renderer/form-detail.ts +116 -0
- package/src/object-renderer/index.ts +13 -0
- package/src/object-renderer/linkage.ts +371 -0
- package/src/object-renderer/merge-class.ts +126 -0
- package/src/object-renderer/mode.ts +25 -0
- package/src/object-renderer/outcome-draft.ts +61 -0
- package/src/object-renderer/parts.ts +136 -0
- package/src/object-renderer/pick-component.ts +36 -0
- package/src/object-renderer/render-object.ts +724 -0
- package/src/object-renderer/request-actions.ts +581 -0
- package/src/object-renderer/skeleton.ts +101 -0
- package/src/object-renderer/table-actions.ts +438 -0
- package/src/object-renderer/table-summary.ts +105 -0
- package/src/object-renderer/table-tools.ts +1072 -0
- package/src/object-renderer/types.ts +936 -0
- package/src/page-target/ai-surface.ts +154 -0
- package/src/page-target/compile.ts +354 -0
- package/src/page-target/drift.ts +186 -0
- package/src/page-target/index.ts +25 -0
- package/src/page-target/intent-map.ts +137 -0
- package/src/page-target/intent-schema.ts +711 -0
- package/src/page-target/origin.ts +17 -0
- package/src/page-target/target.ts +450 -0
- package/src/page-target/writer-policy.ts +315 -0
- package/src/theme/block-theme.ts +202 -0
- package/src/theme/kit-theme-text.ts +2 -0
- package/src/theme/kit-theme.css +275 -0
- package/src/theme/kit-theme.tpl +275 -0
- package/src/theme/theme-tokens.ts +135 -0
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
// KitStepHub:StepForm 的页面级协作数据组件(layer: data,无可见界面)。
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要它:每一步是各自独立的 KitObjectForm(各持草稿、各校验本步字段),最后提交要合并所有步骤的 model;
|
|
4
|
+
// KitObjectForm 的 submit 只抛本步 model,页面表达不了「累加 + 末步合并提交」。所以:
|
|
5
|
+
// · 各步表单的 submit / cancel 用 `SCOPE | semantic` 把意图交给本组件(step_next / step_prev),restart 同理;
|
|
6
|
+
// · 本组件是 focus 切片的 owner,写「当前步、各步显隐真值、各步已确认的 model、是否完成」;切片镜像给页面做显隐与表单 initial;
|
|
7
|
+
// · 末步 step_next:合并各步 model,调 `services.data.submit(object, merged, 'create')`(core 按目标对象字典再校验一遍、构体、发请求);
|
|
8
|
+
// 成功 -> 完成态并抛 saved;失败 -> 回到第一个含出错字段的步骤并弹提示(出错字段落在哪一步按声明的步骤字段表判断)。
|
|
9
|
+
// · 各步表单一直挂着、只靠显隐门切换;「上一步」把本步当前草稿(KitObjectForm 的 cancel 载荷 model,未校验)写进累加器,
|
|
10
|
+
// 回来时表单 initial 绑 step_vals_i 镜像回填;「下一步」仍只在校验通过时写入。
|
|
11
|
+
//
|
|
12
|
+
// 已知边界:每步只校验本步字段(对象字典按步拆开),rules 里引用别步字段的条件规则校验不到,
|
|
13
|
+
// 末步提交时 core 对合并后的 model 按目标对象字典再校验一次,但条件规则的上下文仍只含本次 model。
|
|
14
|
+
//
|
|
15
|
+
// 每步保存(saveMode: perStep,Q 波 G3):
|
|
16
|
+
// · 「下一步」= 校验本步 -> 保存本步 -> 成功才前进;首步无 rid 走 create,取回 data[stepKey](或 data.data[stepKey])存进 focus.rid;
|
|
17
|
+
// 其余步(及回到首步改后再下一步)走 update,载荷 { ...本步 model, [stepKey]: rid },submit 第 4 参 { identityField: stepKey };
|
|
18
|
+
// core 的 update 端点占位符取「对象读态优先、其次提交的 model」,步骤对象无 from,所以占位符 {stepKey} 取自 model,core 无需改动。
|
|
19
|
+
// · 「上一步」不保存,只带草稿回上一步;再「下一步」会重新保存该步。
|
|
20
|
+
// · 失败停在本步(服务端 fieldErrors 指向更早的步则退回该步),rid 不清、已保存的步不回滚;「重新开始」清 rid 与草稿。
|
|
21
|
+
// · 放弃(config.abandon,缺省 keep 不发 delete):delete 时每步多一个「取消」,且离开页面有守卫。已保存过(有 rid)才二次确认并调首步对象的
|
|
22
|
+
// write.delete(占位 {stepKey},主键 = focus.rid);没保存过直接回到初始、不弹确认。删除失败 toast 照实报错:取消钮停在原地可重试,
|
|
23
|
+
// 站内链接离开则不拦(确认后删,成败都放行);刷新 / 关闭标签只有浏览器自带提示,来不及异步删除(已知限制)。
|
|
24
|
+
// · 取不到主键(KIT-STEP-002)视为首步失败,不前进。
|
|
25
|
+
//
|
|
26
|
+
// focus 切片值:{ step, total, done, busy, at: {s<i>}, past: {s<i>}, fut: {s<i>}, values: {s<i>: model}, seq, rid? }
|
|
27
|
+
|
|
28
|
+
import type { SomaComponentControllerContext, SomaComponentDefinition, SomaDataPort, SomaRegionPort } from 'soma-core';
|
|
29
|
+
import { kitText } from '../../i18n/kit-text';
|
|
30
|
+
import { askKitConfirm } from '../../data/confirm-dialog';
|
|
31
|
+
import { registerKitStepValues } from '../shared/step-values';
|
|
32
|
+
|
|
33
|
+
/** 语义事件名(SCOPE | semantic 要求小写 snake_case) */
|
|
34
|
+
export const KIT_STEP_EVENTS = {
|
|
35
|
+
next: 'step_next',
|
|
36
|
+
prev: 'step_prev',
|
|
37
|
+
restart: 'step_restart',
|
|
38
|
+
/** 中途「取消」(abandon: delete 才有按钮) */
|
|
39
|
+
abort: 'step_abort',
|
|
40
|
+
} as const;
|
|
41
|
+
|
|
42
|
+
export interface KitStepFocus {
|
|
43
|
+
step: number;
|
|
44
|
+
total: number;
|
|
45
|
+
done: boolean;
|
|
46
|
+
busy: boolean;
|
|
47
|
+
at: Record<string, boolean>;
|
|
48
|
+
past: Record<string, boolean>;
|
|
49
|
+
fut: Record<string, boolean>;
|
|
50
|
+
values: Record<string, Record<string, unknown>>;
|
|
51
|
+
seq: number;
|
|
52
|
+
/** perStep:首步保存后取回的主键(final 模式恒无) */
|
|
53
|
+
rid?: string | number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** 镜像键(compile 与测试共用) */
|
|
57
|
+
export const KIT_STEP_MIRRORS = {
|
|
58
|
+
at: (i: number): { key: string; path: string; as: 'truthy' } => ({ key: `step_at_${i}`, path: `at.s${i}`, as: 'truthy' }),
|
|
59
|
+
past: (i: number): { key: string; path: string; as: 'truthy' } => ({ key: `step_past_${i}`, path: `past.s${i}`, as: 'truthy' }),
|
|
60
|
+
fut: (i: number): { key: string; path: string; as: 'truthy' } => ({ key: `step_fut_${i}`, path: `fut.s${i}`, as: 'truthy' }),
|
|
61
|
+
values: (i: number): { key: string; path: string; as: 'value' } => ({ key: `step_vals_${i}`, path: `values.s${i}`, as: 'value' }),
|
|
62
|
+
done: { key: 'step_done', path: 'done', as: 'truthy' } as const,
|
|
63
|
+
/** 全部步骤已确认的 model(阶段闸门 steps.s<i>.字段 的取值来源;只有声明了 gate 才编入) */
|
|
64
|
+
allValues: { key: 'step_vals_all', path: 'values', as: 'value' } as const,
|
|
65
|
+
} as const;
|
|
66
|
+
|
|
67
|
+
type Rec = Record<string, unknown>;
|
|
68
|
+
|
|
69
|
+
function isRec(raw: unknown): raw is Rec {
|
|
70
|
+
return raw !== null && typeof raw === 'object' && !Array.isArray(raw);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 当前步(或完成态)对应的完整 focus;values 沿用传入的 */
|
|
74
|
+
export function stepFocus(
|
|
75
|
+
step: number,
|
|
76
|
+
total: number,
|
|
77
|
+
done: boolean,
|
|
78
|
+
values: Record<string, Record<string, unknown>>,
|
|
79
|
+
seq: number,
|
|
80
|
+
busy = false,
|
|
81
|
+
rid?: string | number,
|
|
82
|
+
): KitStepFocus {
|
|
83
|
+
const at: Record<string, boolean> = {};
|
|
84
|
+
const past: Record<string, boolean> = {};
|
|
85
|
+
const fut: Record<string, boolean> = {};
|
|
86
|
+
for (let i = 0; i < total; i += 1) {
|
|
87
|
+
at[`s${i}`] = !done && i === step;
|
|
88
|
+
past[`s${i}`] = done || i < step;
|
|
89
|
+
fut[`s${i}`] = !done && i > step;
|
|
90
|
+
}
|
|
91
|
+
return { step, total, done, busy, at, past, fut, values, seq, ...(rid !== undefined ? { rid } : {}) };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 取主键:data[stepKey],包裹一层 data.data[stepKey] 也认(口径同读侧 fromWrap);只收非空字符串 / 有限数 */
|
|
95
|
+
export function pickStepRid(data: unknown, stepKey: string): string | number | undefined {
|
|
96
|
+
const ok = (v: unknown): v is string | number => (typeof v === 'string' && v !== '') || (typeof v === 'number' && Number.isFinite(v));
|
|
97
|
+
if (!isRec(data) || stepKey === '') return undefined;
|
|
98
|
+
if (ok(data[stepKey])) return data[stepKey];
|
|
99
|
+
const inner = data.data;
|
|
100
|
+
if (isRec(inner) && ok(inner[stepKey])) return inner[stepKey];
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function initialStepFocus(total: number, seq = 0): KitStepFocus {
|
|
105
|
+
const values: Record<string, Record<string, unknown>> = {};
|
|
106
|
+
for (let i = 0; i < total; i += 1) values[`s${i}`] = {};
|
|
107
|
+
return stepFocus(0, total, false, values, seq);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 各步 model 按步骤顺序合并(字段在步骤间不重名,由编译期保证) */
|
|
111
|
+
export function mergeStepModels(values: Record<string, Record<string, unknown>>, total: number): Record<string, unknown> {
|
|
112
|
+
const out: Record<string, unknown> = {};
|
|
113
|
+
for (let i = 0; i < total; i += 1) Object.assign(out, values[`s${i}`] ?? {});
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface KitStepReduce {
|
|
118
|
+
focus: KitStepFocus;
|
|
119
|
+
/** 末步确认后要提交的合并 model;其余情况 null */
|
|
120
|
+
submit: Record<string, unknown> | null;
|
|
121
|
+
/** perStep:「下一步」要保存的这一步(focus 仍停在本步,保存成功才前进);其余情况 null */
|
|
122
|
+
save: { idx: number; model: Record<string, unknown>; last: boolean } | null;
|
|
123
|
+
/** perStep:「取消」时已经保存过(有 rid),要二次确认后删半成品;没保存过的取消直接回到初始(不带这个键) */
|
|
124
|
+
abort?: 'delete';
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 纯函数:当前 focus + 语义事件 + 载荷 -> 下一个 focus(及是否要提交)。
|
|
129
|
+
* @param steps 各步数据对象名(按步骤顺序),用来从 step_next 载荷的 object 反查步号
|
|
130
|
+
*/
|
|
131
|
+
export function reduceStepHub(
|
|
132
|
+
current: KitStepFocus,
|
|
133
|
+
name: string,
|
|
134
|
+
payload: unknown,
|
|
135
|
+
steps: readonly string[],
|
|
136
|
+
perStep = false,
|
|
137
|
+
): KitStepReduce {
|
|
138
|
+
const total = steps.length;
|
|
139
|
+
const seq = current.seq + 1;
|
|
140
|
+
const p = isRec(payload) ? payload : {};
|
|
141
|
+
if (name === KIT_STEP_EVENTS.restart) return { focus: initialStepFocus(total, seq), submit: null, save: null };
|
|
142
|
+
if (current.done || current.busy) return { focus: current, submit: null, save: null };
|
|
143
|
+
if (name === KIT_STEP_EVENTS.abort) {
|
|
144
|
+
if (!perStep) return { focus: current, submit: null, save: null };
|
|
145
|
+
// 还没保存过任何一步:没有半成品,不需要删除也不确认,直接回到初始
|
|
146
|
+
if (current.rid === undefined) return { focus: initialStepFocus(total, seq), submit: null, save: null };
|
|
147
|
+
return { focus: current, submit: null, save: null, abort: 'delete' };
|
|
148
|
+
}
|
|
149
|
+
if (name === KIT_STEP_EVENTS.prev) {
|
|
150
|
+
// 上一步:本步当前草稿(不校验)先写进累加器,回来时经 step_vals_i 镜像回填;载荷没有 model 则不动
|
|
151
|
+
const byObject = typeof p.object === 'string' ? steps.indexOf(p.object) : -1;
|
|
152
|
+
const idx = byObject >= 0 ? byObject : current.step;
|
|
153
|
+
const values = isRec(p.model) ? { ...current.values, [`s${idx}`]: { ...p.model } } : current.values;
|
|
154
|
+
const step = Math.max(0, idx - 1);
|
|
155
|
+
return { focus: stepFocus(step, total, false, values, seq, false, current.rid), submit: null, save: null };
|
|
156
|
+
}
|
|
157
|
+
if (name === KIT_STEP_EVENTS.next) {
|
|
158
|
+
const byObject = typeof p.object === 'string' ? steps.indexOf(p.object) : -1;
|
|
159
|
+
const idx = byObject >= 0 ? byObject : current.step;
|
|
160
|
+
const model = isRec(p.model) ? { ...p.model } : {};
|
|
161
|
+
const values = { ...current.values, [`s${idx}`]: model };
|
|
162
|
+
if (perStep) {
|
|
163
|
+
// 先留在本步,保存成功才前进(前进由控制器在保存回执里做)
|
|
164
|
+
return { focus: stepFocus(idx, total, false, values, seq, false, current.rid), submit: null, save: { idx, model, last: idx === total - 1 } };
|
|
165
|
+
}
|
|
166
|
+
if (idx < total - 1) return { focus: stepFocus(idx + 1, total, false, values, seq), submit: null, save: null };
|
|
167
|
+
return { focus: stepFocus(idx, total, false, values, seq), submit: mergeStepModels(values, total), save: null };
|
|
168
|
+
}
|
|
169
|
+
return { focus: current, submit: null, save: null };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 出错字段所在的第一个步骤号;都不在返回 -1(fields[i] = 第 i 步的字段名) */
|
|
173
|
+
export function stepOfFirstError(fieldErrors: Readonly<Record<string, string>>, fields: ReadonlyArray<readonly string[]>): number {
|
|
174
|
+
for (let i = 0; i < fields.length; i += 1) {
|
|
175
|
+
if ((fields[i] ?? []).some((f) => f in fieldErrors)) return i;
|
|
176
|
+
}
|
|
177
|
+
return -1;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function focusOf(raw: unknown, total: number): KitStepFocus {
|
|
181
|
+
if (!isRec(raw) || typeof raw.step !== 'number') return initialStepFocus(total);
|
|
182
|
+
return raw as unknown as KitStepFocus;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function strings(raw: unknown): string[] {
|
|
186
|
+
return Array.isArray(raw) ? raw.filter((v): v is string => typeof v === 'string') : [];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function createKitStepHub(): SomaComponentDefinition {
|
|
190
|
+
return {
|
|
191
|
+
contract: {
|
|
192
|
+
name: 'KitStepHub',
|
|
193
|
+
layer: 'data',
|
|
194
|
+
props: {
|
|
195
|
+
scope: { type: 'string', default: '', desc: '区域作用域 id;订阅它的语义事件,持有 focus 切片' },
|
|
196
|
+
object: { type: 'string', default: '', desc: '提交目标数据对象名(带 write;字段字典是各步字段的并集)' },
|
|
197
|
+
steps: { type: 'array', default: [], desc: '各步数据对象名,按步骤顺序' },
|
|
198
|
+
fields: { type: 'array', default: [], desc: '各步字段名,按步骤顺序(string[][];提交失败时定位出错步骤)' },
|
|
199
|
+
saveMode: { type: 'string', default: 'final', desc: 'final = 末步合并提交;perStep = 每步各自保存(steps 是各自带 write 的数据对象,object 不用)' },
|
|
200
|
+
stepKey: { type: 'string', default: '', desc: 'perStep:首步返回的主键字段名,后续步 update 的占位符与身份字段' },
|
|
201
|
+
abandon: { type: 'string', default: 'keep', desc: "perStep:中途放弃(点取消 / 离开页面)时 keep = 保留已保存的半成品,delete = 二次确认后调首步对象的 write.delete 删掉它" },
|
|
202
|
+
},
|
|
203
|
+
emits: {
|
|
204
|
+
step_change: { payload: { step: { type: 'number' }, total: { type: 'number' } }, desc: '当前步变化' },
|
|
205
|
+
saved: { payload: { result: { type: 'object' }, id: { type: 'string' } }, desc: '末步提交成功,进入完成态;perStep 另带 id(首步取回的主键)' },
|
|
206
|
+
},
|
|
207
|
+
scope: { slices: ['focus'] },
|
|
208
|
+
},
|
|
209
|
+
services: ['region', 'data', 'notify'],
|
|
210
|
+
templateHtml: '<div data-soma-id="root" class="hidden"></div>',
|
|
211
|
+
controllerFactory: (ctx: SomaComponentControllerContext) => {
|
|
212
|
+
let off: (() => void) | undefined;
|
|
213
|
+
let alive = false;
|
|
214
|
+
const props = (): Rec => (ctx.readProps ? ctx.readProps() : ctx.props);
|
|
215
|
+
const steps = (): string[] => strings(props().steps);
|
|
216
|
+
const dataPort = (): SomaDataPort | undefined => {
|
|
217
|
+
const port = (ctx.services as { data?: SomaDataPort } | undefined)?.data;
|
|
218
|
+
return port && typeof port === 'object' ? port : undefined;
|
|
219
|
+
};
|
|
220
|
+
const toast = (level: string, message: string): void => {
|
|
221
|
+
const svc = (ctx.services as { notify?: { toast?: (o: { level: string; message: string }) => void } } | undefined)?.notify;
|
|
222
|
+
if (svc && typeof svc.toast === 'function') svc.toast({ level, message });
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const submit = async (focus: KitStepFocus, port: SomaRegionPort, model: Record<string, unknown>): Promise<void> => {
|
|
226
|
+
const data = dataPort();
|
|
227
|
+
const slice = port.slice('focus');
|
|
228
|
+
if (!data || !slice) return;
|
|
229
|
+
slice.set({ ...focus, busy: true });
|
|
230
|
+
const object = typeof props().object === 'string' ? (props().object as string) : '';
|
|
231
|
+
const result = await data.submit(object, model, 'create');
|
|
232
|
+
if (!alive) return;
|
|
233
|
+
const cur = focusOf(slice.get(), steps().length);
|
|
234
|
+
// 提交期间用户点了「重新开始」(seq 已变):过期的结果不再写回,免得把新一轮表单置成完成态
|
|
235
|
+
if (cur.seq !== focus.seq) return;
|
|
236
|
+
if (result.ok) {
|
|
237
|
+
slice.set({ ...stepFocus(cur.step, cur.total, true, cur.values, cur.seq + 1) });
|
|
238
|
+
ctx.emit('saved', { result: result.data ?? {} });
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const errors = result.fieldErrors ?? {};
|
|
242
|
+
const rawFields = Array.isArray(props().fields) ? (props().fields as unknown[]) : [];
|
|
243
|
+
const fields = rawFields.map((f) => strings(f));
|
|
244
|
+
const at = stepOfFirstError(errors, fields);
|
|
245
|
+
const first = Object.values(errors).find((m) => typeof m === 'string' && m !== '');
|
|
246
|
+
const message = first ?? (result.message !== '' ? result.message : kitText('soma.kit.step_failed', undefined, ctx.host));
|
|
247
|
+
const back = at >= 0 ? at : cur.step;
|
|
248
|
+
slice.set(stepFocus(back, cur.total, false, cur.values, cur.seq + 1));
|
|
249
|
+
toast('danger', at >= 0 ? kitText('soma.kit.step_n_message', { n: at + 1, message }, ctx.host) : message);
|
|
250
|
+
if (back !== cur.step) ctx.emit('step_change', { step: back, total: cur.total });
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
/** perStep:保存一步;成功才前进(末步即完成态),失败停在本步(fieldErrors 指向更早的步则退回那一步),rid 不清 */
|
|
254
|
+
const saveStep = async (
|
|
255
|
+
focus: KitStepFocus,
|
|
256
|
+
port: SomaRegionPort,
|
|
257
|
+
job: { idx: number; model: Record<string, unknown>; last: boolean },
|
|
258
|
+
): Promise<void> => {
|
|
259
|
+
const data = dataPort();
|
|
260
|
+
const slice = port.slice('focus');
|
|
261
|
+
if (!data || !slice) return;
|
|
262
|
+
const stepKey = typeof props().stepKey === 'string' ? (props().stepKey as string) : '';
|
|
263
|
+
const object = steps()[job.idx] ?? '';
|
|
264
|
+
const rid = focus.rid;
|
|
265
|
+
slice.set({ ...focus, busy: true });
|
|
266
|
+
const fail = (message: string, fieldErrors: Readonly<Record<string, string>> = {}): void => {
|
|
267
|
+
const cur = focusOf(slice.get(), steps().length);
|
|
268
|
+
const rawFields = Array.isArray(props().fields) ? (props().fields as unknown[]) : [];
|
|
269
|
+
const at = stepOfFirstError(fieldErrors, rawFields.map((f) => strings(f)));
|
|
270
|
+
const first = Object.values(fieldErrors).find((m) => typeof m === 'string' && m !== '');
|
|
271
|
+
const text = first ?? (message !== '' ? message : kitText('soma.kit.step_failed', undefined, ctx.host));
|
|
272
|
+
const back = at >= 0 ? at : cur.step;
|
|
273
|
+
slice.set(stepFocus(back, cur.total, false, cur.values, cur.seq + 1, false, cur.rid));
|
|
274
|
+
toast('danger', at >= 0 ? kitText('soma.kit.step_n_message', { n: at + 1, message: text }, ctx.host) : text);
|
|
275
|
+
if (back !== cur.step) ctx.emit('step_change', { step: back, total: cur.total });
|
|
276
|
+
};
|
|
277
|
+
// 首步没有 rid 用 create,其余(含回到首步改后再下一步)用 update;update 必须带 rid
|
|
278
|
+
const creating = rid === undefined;
|
|
279
|
+
if (creating && job.idx !== 0) { fail(kitText('soma.kit.step_no_id', undefined, ctx.host)); return; }
|
|
280
|
+
const result = creating
|
|
281
|
+
? await data.submit(object, job.model, 'create')
|
|
282
|
+
: await data.submit(object, { ...job.model, [stepKey]: rid }, 'update', { identityField: stepKey });
|
|
283
|
+
if (!alive) return;
|
|
284
|
+
const cur = focusOf(slice.get(), steps().length);
|
|
285
|
+
if (cur.seq !== focus.seq) return; // 保存期间点了「重新开始」:过期回执不写回
|
|
286
|
+
if (!result.ok) { fail(result.message, result.fieldErrors ?? {}); return; }
|
|
287
|
+
const got = creating ? pickStepRid(result.data, stepKey) : rid;
|
|
288
|
+
if (got === undefined) { fail(kitText('soma.kit.step_no_id', undefined, ctx.host)); return; }
|
|
289
|
+
if (job.last) {
|
|
290
|
+
slice.set(stepFocus(cur.step, cur.total, true, cur.values, cur.seq + 1, false, got));
|
|
291
|
+
ctx.emit('saved', { result: result.data ?? {}, id: String(got) });
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
slice.set(stepFocus(job.idx + 1, cur.total, false, cur.values, cur.seq + 1, false, got));
|
|
295
|
+
ctx.emit('step_change', { step: job.idx + 1, total: cur.total });
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/** abandon: delete:调首步对象已声明的 write.delete,主键取 focus.rid;失败照实 toast,返回是否删成 */
|
|
299
|
+
const deleteDraft = async (slice: NonNullable<ReturnType<SomaRegionPort['slice']>>, focus: KitStepFocus): Promise<boolean> => {
|
|
300
|
+
const data = dataPort();
|
|
301
|
+
const stepKey = typeof props().stepKey === 'string' ? (props().stepKey as string) : '';
|
|
302
|
+
const first = steps()[0] ?? '';
|
|
303
|
+
if (!data || focus.rid === undefined || first === '' || stepKey === '') {
|
|
304
|
+
toast('danger', kitText('soma.kit.step_abandon_failed', { message: kitText('soma.kit.step_failed', undefined, ctx.host) }, ctx.host));
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
slice.set({ ...focus, busy: true });
|
|
308
|
+
const result = await data.remove(first, { [stepKey]: focus.rid });
|
|
309
|
+
if (result.ok) return true;
|
|
310
|
+
toast('danger', kitText('soma.kit.step_abandon_failed', { message: result.message !== '' ? result.message : kitText('soma.kit.step_failed', undefined, ctx.host) }, ctx.host));
|
|
311
|
+
return false;
|
|
312
|
+
};
|
|
313
|
+
// 放弃半成品的确认:自绘对话框(异步,danger 色调);无 DOM 时视为同意
|
|
314
|
+
const askAbandon = (): Promise<boolean> => askKitConfirm({
|
|
315
|
+
message: kitText('soma.kit.step_abandon_confirm', undefined, ctx.host), tone: 'danger', at: ctx.host ?? null, whenNoDom: true,
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
/** 中途点「取消」且已保存过:二次确认 -> 删半成品 -> 成功回到初始;失败停在原地(保留状态,可重试或继续填) */
|
|
319
|
+
const abandon = async (slice: NonNullable<ReturnType<SomaRegionPort['slice']>>, focus: KitStepFocus): Promise<void> => {
|
|
320
|
+
if (!(await askAbandon())) return;
|
|
321
|
+
if (!alive) return;
|
|
322
|
+
const ok = await deleteDraft(slice, focus);
|
|
323
|
+
if (!alive) return;
|
|
324
|
+
const cur = focusOf(slice.get(), steps().length);
|
|
325
|
+
if (cur.seq !== focus.seq) return; // 期间点了「重新开始」:过期回执不写回
|
|
326
|
+
if (!ok) { slice.set({ ...cur, busy: false }); return; }
|
|
327
|
+
slice.set(initialStepFocus(cur.total, cur.seq + 1));
|
|
328
|
+
toast('success', kitText('soma.kit.step_abandoned', undefined, ctx.host));
|
|
329
|
+
if (cur.step !== 0) ctx.emit('step_change', { step: 0, total: cur.total });
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
/** 离开页面守卫(abandon: delete 且已有半成品、未完成时生效):站内链接二次确认后删半成品再走(删除失败照实报错,不拦离开);刷新 / 关闭只能走浏览器自带提示,来不及异步删除 */
|
|
333
|
+
let offGuard: (() => void) | undefined;
|
|
334
|
+
let offStepValues: (() => void) | undefined;
|
|
335
|
+
const installLeaveGuard = (slice: NonNullable<ReturnType<SomaRegionPort['slice']>>): void => {
|
|
336
|
+
if (typeof window === 'undefined' || typeof document === 'undefined') return;
|
|
337
|
+
const active = (): KitStepFocus | undefined => {
|
|
338
|
+
const f = focusOf(slice.get(), steps().length);
|
|
339
|
+
return f.rid !== undefined && !f.done ? f : undefined;
|
|
340
|
+
};
|
|
341
|
+
const onBeforeUnload = (event: BeforeUnloadEvent): void => {
|
|
342
|
+
if (active() === undefined) return;
|
|
343
|
+
event.preventDefault();
|
|
344
|
+
event.returnValue = '';
|
|
345
|
+
};
|
|
346
|
+
const onLinkClick = (event: MouseEvent): void => {
|
|
347
|
+
const f = active();
|
|
348
|
+
if (f === undefined || f.busy) return;
|
|
349
|
+
if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
|
350
|
+
const a = (event.target as HTMLElement | null)?.closest?.('a[href]') as HTMLAnchorElement | null | undefined;
|
|
351
|
+
if (!a || a.target === '_blank' || (a.getAttribute('href') ?? '').startsWith('#')) return;
|
|
352
|
+
// 先拦下,确认后删完再用地址跳转(与表单自己的离开提醒不叠:这里 stopImmediatePropagation)
|
|
353
|
+
event.preventDefault();
|
|
354
|
+
event.stopImmediatePropagation();
|
|
355
|
+
const href = a.href;
|
|
356
|
+
void askAbandon().then((agreed) => {
|
|
357
|
+
if (!agreed || !alive) return;
|
|
358
|
+
return deleteDraft(slice, f).then(() => { if (alive) window.location.assign(href); });
|
|
359
|
+
});
|
|
360
|
+
};
|
|
361
|
+
window.addEventListener('beforeunload', onBeforeUnload);
|
|
362
|
+
document.addEventListener('click', onLinkClick, true);
|
|
363
|
+
offGuard = () => {
|
|
364
|
+
window.removeEventListener('beforeunload', onBeforeUnload);
|
|
365
|
+
document.removeEventListener('click', onLinkClick, true);
|
|
366
|
+
};
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
return {
|
|
370
|
+
onMount(): void {
|
|
371
|
+
alive = true;
|
|
372
|
+
const scope = typeof props().scope === 'string' ? (props().scope as string) : '';
|
|
373
|
+
const svc = (ctx.services as { region?: (id: string) => SomaRegionPort | null } | undefined)?.region;
|
|
374
|
+
const port = scope !== '' && typeof svc === 'function' ? svc(scope) : null;
|
|
375
|
+
const focus = port?.slice('focus');
|
|
376
|
+
if (!port || !focus) return;
|
|
377
|
+
focus.set(initialStepFocus(steps().length));
|
|
378
|
+
// GB1:登记各步已确认值的读口,request 动作的 $steps.s<i>.<字段> 经它取值
|
|
379
|
+
offStepValues?.();
|
|
380
|
+
offStepValues = registerKitStepValues(() => focusOf(focus.get(), steps().length).values);
|
|
381
|
+
offGuard?.();
|
|
382
|
+
offGuard = undefined;
|
|
383
|
+
if (props().saveMode === 'perStep' && props().abandon === 'delete') installLeaveGuard(focus);
|
|
384
|
+
off = port.onSemantic((name, payload) => {
|
|
385
|
+
const now = focusOf(focus.get(), steps().length);
|
|
386
|
+
const perStep = props().saveMode === 'perStep';
|
|
387
|
+
const r = reduceStepHub(now, name, payload, steps(), perStep);
|
|
388
|
+
if (r.focus !== now) {
|
|
389
|
+
focus.set(r.focus);
|
|
390
|
+
if (r.focus.step !== now.step) ctx.emit('step_change', { step: r.focus.step, total: r.focus.total });
|
|
391
|
+
}
|
|
392
|
+
if (r.submit !== null) void submit(r.focus, port, r.submit);
|
|
393
|
+
if (r.save !== null) void saveStep(r.focus, port, r.save);
|
|
394
|
+
if (r.abort === 'delete' && props().abandon === 'delete') void abandon(focus, r.focus);
|
|
395
|
+
});
|
|
396
|
+
},
|
|
397
|
+
onUnmount(): void {
|
|
398
|
+
alive = false;
|
|
399
|
+
offGuard?.();
|
|
400
|
+
offGuard = undefined;
|
|
401
|
+
offStepValues?.();
|
|
402
|
+
offStepValues = undefined;
|
|
403
|
+
if (off) off();
|
|
404
|
+
off = undefined;
|
|
405
|
+
},
|
|
406
|
+
};
|
|
407
|
+
},
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** 本组件模板里的类(候选类) */
|
|
412
|
+
export const KIT_STEP_HUB_CLASS_CANDIDATES: readonly string[] = Object.freeze(['hidden']);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// StepForm:分步表单(规格 5.3)。
|
|
2
|
+
// 对象角色槽:steps(必填,对象表,按书写顺序即步骤顺序:每步 title + schema + view.form)、
|
|
3
|
+
// target(必填:最终提交的写接口 behavior.write,title 作完成面板标题)。
|
|
4
|
+
|
|
5
|
+
import { defineKitBizComponent } from '../define';
|
|
6
|
+
import { compileStepForm } from './compile';
|
|
7
|
+
import { STEP_FORM_AREAS } from './shell';
|
|
8
|
+
|
|
9
|
+
export const StepForm = defineKitBizComponent({
|
|
10
|
+
kind: 'StepForm',
|
|
11
|
+
roles: {
|
|
12
|
+
steps: { required: true, views: ['form'], many: true },
|
|
13
|
+
// final 下必填(最终提交口),perStep 下可省(只留 title);必填性由 compile 按 saveMode 判定
|
|
14
|
+
target: { required: false, views: ['form'] },
|
|
15
|
+
},
|
|
16
|
+
areas: [...STEP_FORM_AREAS],
|
|
17
|
+
configKeys: ['saveMode', 'stepKey', 'abandon'],
|
|
18
|
+
compile: compileStepForm,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export { compileStepForm, stepFormObjectName } from './compile';
|
|
22
|
+
export { createKitStepHub, KIT_STEP_EVENTS, KIT_STEP_MIRRORS, KIT_STEP_HUB_CLASS_CANDIDATES, reduceStepHub } from './hub';
|
|
23
|
+
export { STEP_FORM_AREAS, STEP_FORM_CLASS, stepFormIds } from './shell';
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// StepForm 的 HTML 代码壳:居中定宽卡片 + 步骤条 + 每步一个面板(表单)+ 完成面板。
|
|
2
|
+
// 只有结构与 Tailwind 类(语义色名 + utilityScales 里有的尺度),不写原生 CSS。
|
|
3
|
+
// 显隐规则:面板 / 步骤条变体都是「外层只放 core 写的 hidden、内层放布局类」两层;各步表单一直挂着,只靠外层显隐切换。
|
|
4
|
+
|
|
5
|
+
import { mergeClass } from '../../object-renderer/merge-class';
|
|
6
|
+
import { cls, esc, host } from '../link/shared';
|
|
7
|
+
import { kitText } from '../../i18n/kit-text';
|
|
8
|
+
import { kitIconHtml } from '../../base/icons';
|
|
9
|
+
|
|
10
|
+
/** 壳的内部区域名(`areaClass` 的合法键) */
|
|
11
|
+
export const STEP_FORM_AREAS = ['root', 'card', 'steps', 'panel', 'done'] as const;
|
|
12
|
+
export type StepFormArea = (typeof STEP_FORM_AREAS)[number];
|
|
13
|
+
|
|
14
|
+
export const STEP_FORM_CLASS = {
|
|
15
|
+
root: '@container flex justify-center',
|
|
16
|
+
card: 'flex w-full max-w-2xl flex-col gap-6 rounded-lg border border-border-subtle bg-surface-raised p-6 shadow-xs',
|
|
17
|
+
title: 'text-lg font-semibold text-fg',
|
|
18
|
+
steps: 'flex flex-wrap items-center gap-2',
|
|
19
|
+
stepItem: 'flex items-center gap-2',
|
|
20
|
+
circle: 'flex h-6 w-6 items-center justify-center rounded-full text-sm font-medium',
|
|
21
|
+
circleAt: 'bg-primary text-primary-fg',
|
|
22
|
+
circlePast: 'bg-success text-success-fg',
|
|
23
|
+
circleFut: 'bg-surface-sunken text-fg-muted',
|
|
24
|
+
labelAt: 'text-sm font-semibold text-fg',
|
|
25
|
+
labelPast: 'text-sm text-fg',
|
|
26
|
+
labelFut: 'text-sm text-fg-muted',
|
|
27
|
+
joint: 'w-6 border-t border-border-subtle',
|
|
28
|
+
panel: 'flex flex-col gap-3',
|
|
29
|
+
progress: 'text-sm text-fg-muted',
|
|
30
|
+
panelTitle: 'text-base font-semibold text-fg',
|
|
31
|
+
done: 'flex flex-col items-start gap-3',
|
|
32
|
+
doneTitle: 'text-lg font-semibold text-fg',
|
|
33
|
+
} as const;
|
|
34
|
+
|
|
35
|
+
export interface StepFormIds {
|
|
36
|
+
root: string;
|
|
37
|
+
hub: string;
|
|
38
|
+
link: string;
|
|
39
|
+
done: string;
|
|
40
|
+
restart: string;
|
|
41
|
+
step(i: number, kind: 'past' | 'at' | 'fut'): string;
|
|
42
|
+
panel(i: number): string;
|
|
43
|
+
/** abandon: delete 时每个面板底下的「取消」钮 */
|
|
44
|
+
abort(i: number): string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function stepFormIds(pageId: string): StepFormIds {
|
|
48
|
+
return {
|
|
49
|
+
root: `${pageId}__stf_root`,
|
|
50
|
+
hub: `${pageId}__stephub`,
|
|
51
|
+
link: `${pageId}_link`,
|
|
52
|
+
done: `${pageId}__stf_done`,
|
|
53
|
+
restart: `${pageId}__stf_restart`,
|
|
54
|
+
step: (i, kind) => `${pageId}__stf_st${i}_${kind}`,
|
|
55
|
+
panel: (i) => `${pageId}__stf_panel${i}`,
|
|
56
|
+
abort: (i) => `${pageId}__stf_abort${i}`,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface StepBlock {
|
|
61
|
+
title: string;
|
|
62
|
+
form: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function stepFormHtml(
|
|
66
|
+
ids: StepFormIds,
|
|
67
|
+
parts: { title: string; steps: StepBlock[]; doneTitle: string; /** abandon: delete 才有:每个面板放一个「取消」钮 */ withAbort?: boolean },
|
|
68
|
+
areaClass: Record<string, string>,
|
|
69
|
+
extra: string | undefined,
|
|
70
|
+
locale?: string,
|
|
71
|
+
): string {
|
|
72
|
+
const total = parts.steps.length;
|
|
73
|
+
const C = STEP_FORM_CLASS;
|
|
74
|
+
const variant = (i: number, kind: 'past' | 'at' | 'fut', title: string): string => {
|
|
75
|
+
const circle = kind === 'at' ? C.circleAt : kind === 'past' ? C.circlePast : C.circleFut;
|
|
76
|
+
const label = kind === 'at' ? C.labelAt : kind === 'past' ? C.labelPast : C.labelFut;
|
|
77
|
+
const mark = kind === 'past' ? kitIconHtml('check', 'sm') : String(i + 1);
|
|
78
|
+
return `<div data-soma-id="${esc(ids.step(i, kind))}"><div${cls(C.stepItem)}>`
|
|
79
|
+
+ `<span${cls(mergeClass(C.circle, circle))}>${mark}</span><span${cls(label)}>${esc(title)}</span></div></div>`;
|
|
80
|
+
};
|
|
81
|
+
const items = parts.steps.map((s, i) => {
|
|
82
|
+
const joint = i < total - 1 ? `<span aria-hidden="true"${cls(C.joint)}></span>` : '';
|
|
83
|
+
return `<div${cls(C.stepItem)}>${variant(i, 'past', s.title)}${variant(i, 'at', s.title)}${variant(i, 'fut', s.title)}${joint}</div>`;
|
|
84
|
+
});
|
|
85
|
+
const bar = `<nav aria-label="steps"${cls(mergeClass(C.steps, areaClass.steps))}>${items.join('')}</nav>`;
|
|
86
|
+
const panels = parts.steps.map((s, i) => {
|
|
87
|
+
const head = `<p${cls(C.progress)}>${esc(kitText('soma.kit.step_progress', { n: i + 1, total }, locale))}</p><h3${cls(C.panelTitle)}>${esc(s.title)}</h3>`;
|
|
88
|
+
return `<div data-soma-id="${esc(ids.panel(i))}"><div${cls(mergeClass(C.panel, areaClass.panel))}>${head}${s.form}${parts.withAbort === true ? host('Button', ids.abort(i)) : ''}</div></div>`;
|
|
89
|
+
});
|
|
90
|
+
const done = `<div data-soma-id="${esc(ids.done)}"><div${cls(mergeClass(C.done, areaClass.done))}>`
|
|
91
|
+
+ `<h3${cls(C.doneTitle)}>${esc(parts.doneTitle)}</h3>${host('Button', ids.restart)}</div></div>`;
|
|
92
|
+
const title = parts.title === '' ? '' : `<h2${cls(C.title)}>${esc(parts.title)}</h2>`;
|
|
93
|
+
const card = `<section${cls(mergeClass(C.card, areaClass.card))}>${title}${bar}${panels.join('')}${done}</section>`;
|
|
94
|
+
const root = mergeClass(mergeClass(C.root, areaClass.root), extra);
|
|
95
|
+
return `<div data-soma-id="${esc(ids.root)}"${cls(root)}>\n${host('KitStepHub', ids.hub)}\n${card}\n</div>`;
|
|
96
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// StepForm 跨步取值(补能力 GB1):把步骤声明里的 `$steps.<步骤名>.<字段>` 校验后改写成规范序号形态 `s<序号>`。
|
|
2
|
+
//
|
|
3
|
+
// 能写 $steps 的四处(都只许指向「更早的步骤」及其 schema 里的字段,不含本步与之后的步骤):
|
|
4
|
+
// · schema.<字段>.default "$steps.<步骤名>.<字段>" -> "$steps.s<i>.<字段>"
|
|
5
|
+
// · schema.<字段>.options.params { steps: "<步骤名>.<字段>" } -> { steps: "s<i>.<字段>" }
|
|
6
|
+
// · schema.<字段>.derive.template {steps.<步骤名>.<字段>[|过滤器…]} -> {steps.s<i>.<字段>…}
|
|
7
|
+
// · behavior.actions[].request.body "$steps.<步骤名>.<字段>" -> "$steps.s<i>.<字段>"
|
|
8
|
+
// 阶段闸门(gate.when 的叶子 steps.<步骤名>.<字段>)早已支持,仍由 adopt-gate.ts 的 gateOf 改写,这里不动。
|
|
9
|
+
// 改写发生在 emitObject 之前;规范形态只在 runInKitStepScope 里才被下游校验认作合法(别处写 $steps 报 KIT-STEP-005)。
|
|
10
|
+
// 运行期取值:表单读 gateSteps 镜像、请求宿主读 step-values 登记的读口,都是 { s<i>: { 字段: 值 } }。
|
|
11
|
+
|
|
12
|
+
import type { KitDiagnostic } from '../../object-renderer/types';
|
|
13
|
+
|
|
14
|
+
type Rec = Record<string, unknown>;
|
|
15
|
+
|
|
16
|
+
const isRec = (v: unknown): v is Rec => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
17
|
+
|
|
18
|
+
export type EarlierSteps = ReadonlyMap<string, { index: number; fields: ReadonlySet<string> }>;
|
|
19
|
+
|
|
20
|
+
export interface StepRefResult {
|
|
21
|
+
step: Rec;
|
|
22
|
+
/** 本步有没有用到 $steps(用到才需要把各步已确认值的镜像交给表单) */
|
|
23
|
+
used: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const IDENT = '[A-Za-z_][A-Za-z0-9_]*';
|
|
27
|
+
const DEFAULT_RE = new RegExp(`^\\$steps\\.(${IDENT})\\.(${IDENT})$`);
|
|
28
|
+
const PLACEHOLDER_RE = new RegExp(`\\{steps\\.(${IDENT})\\.(${IDENT})(?=[|}])`, 'g');
|
|
29
|
+
|
|
30
|
+
export function rewriteStepRefs(raw: Rec, path: string, earlier: EarlierSteps, diags: KitDiagnostic[]): StepRefResult {
|
|
31
|
+
let used = false;
|
|
32
|
+
const bad = (message: string, at: string): void => { diags.push({ code: 'KIT-STEP-005', path: at, message }); };
|
|
33
|
+
/** 步骤名 + 字段 -> s<序号> + 字段;引用不到记 KIT-STEP-005 并返回 null */
|
|
34
|
+
const lookup = (name: string, field: string, at: string): string | null => {
|
|
35
|
+
const hit = earlier.get(name);
|
|
36
|
+
if (hit === undefined) { bad(`$steps 引用的步骤 "${name}" 不是更早的步骤(只能指向本步之前已有的步骤;可用:${[...earlier.keys()].join(' / ') || '无,这是第一步'})`, at); return null; }
|
|
37
|
+
if (!hit.fields.has(field)) { bad(`步骤 "${name}" 的 schema 里没有字段 "${field}"`, at); return null; }
|
|
38
|
+
used = true;
|
|
39
|
+
return `s${hit.index}`;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// schema
|
|
43
|
+
let schema = raw.schema;
|
|
44
|
+
if (isRec(raw.schema)) {
|
|
45
|
+
const next: Rec = {};
|
|
46
|
+
for (const [fname, decl] of Object.entries(raw.schema)) {
|
|
47
|
+
if (!isRec(decl)) { next[fname] = decl; continue; }
|
|
48
|
+
let out: Rec = decl;
|
|
49
|
+
const at = `${path}.schema.${fname}`;
|
|
50
|
+
// default
|
|
51
|
+
if (typeof decl.default === 'string' && decl.default.startsWith('$steps.')) {
|
|
52
|
+
const m = DEFAULT_RE.exec(decl.default);
|
|
53
|
+
if (m === null) bad(`default "${decl.default}" 应写成 $steps.<步骤名>.<字段>`, `${at}.default`);
|
|
54
|
+
else {
|
|
55
|
+
const id = lookup(m[1]!, m[2]!, `${at}.default`);
|
|
56
|
+
if (id !== null) out = { ...out, default: `$steps.${id}.${m[2]!}` };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// options.params
|
|
60
|
+
if (isRec(decl.options) && isRec(decl.options.params)) {
|
|
61
|
+
const params: Rec = {};
|
|
62
|
+
let changed = false;
|
|
63
|
+
for (const [pn, pv] of Object.entries(decl.options.params)) {
|
|
64
|
+
if (isRec(pv) && 'steps' in pv) {
|
|
65
|
+
const parts = typeof pv.steps === 'string' ? pv.steps.split('.') : [];
|
|
66
|
+
if (Object.keys(pv).length !== 1 || parts.length !== 2 || parts[0] === '' || parts[1] === '') {
|
|
67
|
+
bad(`params.${pn} 的 { steps } 应写成 { steps: '<步骤名>.<字段>' }`, `${at}.options.params.${pn}`);
|
|
68
|
+
params[pn] = pv;
|
|
69
|
+
} else {
|
|
70
|
+
const id = lookup(parts[0]!, parts[1]!, `${at}.options.params.${pn}`);
|
|
71
|
+
params[pn] = id === null ? pv : { steps: `${id}.${parts[1]!}` };
|
|
72
|
+
changed = true;
|
|
73
|
+
}
|
|
74
|
+
} else params[pn] = pv;
|
|
75
|
+
}
|
|
76
|
+
if (changed) out = { ...out, options: { ...decl.options, params } };
|
|
77
|
+
}
|
|
78
|
+
// derive.template
|
|
79
|
+
if (isRec(decl.derive) && typeof decl.derive.template === 'string' && decl.derive.template.includes('{steps.')) {
|
|
80
|
+
const tpl = decl.derive.template.replace(PLACEHOLDER_RE, (whole, name: string, field: string) => {
|
|
81
|
+
const id = lookup(name, field, `${at}.derive.template`);
|
|
82
|
+
return id === null ? whole : `{steps.${id}.${field}`;
|
|
83
|
+
});
|
|
84
|
+
out = { ...out, derive: { ...decl.derive, template: tpl } };
|
|
85
|
+
}
|
|
86
|
+
next[fname] = out;
|
|
87
|
+
}
|
|
88
|
+
schema = next;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 步骤动作的请求体
|
|
92
|
+
let behavior = raw.behavior;
|
|
93
|
+
if (isRec(raw.behavior) && Array.isArray(raw.behavior.actions)) {
|
|
94
|
+
const actions = raw.behavior.actions.map((a, i) => {
|
|
95
|
+
if (!isRec(a) || !isRec(a.request) || !isRec(a.request.body)) return a;
|
|
96
|
+
const body: Rec = {};
|
|
97
|
+
for (const [k, v] of Object.entries(a.request.body)) {
|
|
98
|
+
if (typeof v === 'string' && v.startsWith('$steps.')) {
|
|
99
|
+
const m = DEFAULT_RE.exec(v);
|
|
100
|
+
const at = `${path}.behavior.actions[${i}].request.body.${k}`;
|
|
101
|
+
if (m === null) { bad(`${v} 应写成 $steps.<步骤名>.<字段>`, at); body[k] = v; continue; }
|
|
102
|
+
const id = lookup(m[1]!, m[2]!, at);
|
|
103
|
+
body[k] = id === null ? v : `$steps.${id}.${m[2]!}`;
|
|
104
|
+
} else body[k] = v;
|
|
105
|
+
}
|
|
106
|
+
return { ...a, request: { ...a.request, body } };
|
|
107
|
+
});
|
|
108
|
+
behavior = { ...raw.behavior, actions };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return { step: { ...raw, ...(schema !== undefined ? { schema } : {}), ...(behavior !== undefined ? { behavior } : {}) }, used };
|
|
112
|
+
}
|