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,540 @@
|
|
|
1
|
+
// KitImportHub:ImportWizard 的页面级协作数据组件(layer: data,无可见界面)。
|
|
2
|
+
//
|
|
3
|
+
// 四步:上传 -> 映射 -> 预览校验 -> 结果。每步是页面里直接声明的组件(Upload / FieldMapper / ItemList / ResultPanel),
|
|
4
|
+
// 它们的事件用 `SCOPE | semantic` 把意图交给本组件;本组件是 focus 切片的 owner,写「当前步、各步显隐真值、各步要显示的数据」,
|
|
5
|
+
// 切片镜像把这些值给到页面(与 KitStepHub 同一套做法)。
|
|
6
|
+
//
|
|
7
|
+
// 流程:
|
|
8
|
+
// import_files Upload 变化:取第一个文件,在浏览器内读成文本、按 CSV 解析(首行是表头);超过体积 / 行数上限给出错误提示
|
|
9
|
+
// import_next 0 -> 1:按表头与目标字段自动建议映射;1 -> 2:按映射逐行用目标对象字典校验(services.data.validate)
|
|
10
|
+
// import_commit 2 -> 3:把校验通过的行逐行 services.data.submit(目标, 行, 'create')(有限并发),收集失败行;写入全经 core 的 data 端口
|
|
11
|
+
// import_download 下载问题行报告(CSV,单元格防公式注入)
|
|
12
|
+
// import_prev 回上一步;import_restart 回第 1 步清空
|
|
13
|
+
// 错误行不会被导入,也不阻断其余行:预览里标出,结果里汇总,随时可下载报告。
|
|
14
|
+
//
|
|
15
|
+
// 本组件不读 / 不存任何凭据;文件内容只在浏览器内存里,不写本地存储。
|
|
16
|
+
|
|
17
|
+
import type { SomaComponentControllerContext, SomaComponentDefinition, SomaDataPort, SomaRegionPort } from 'soma-core';
|
|
18
|
+
import { KIT_BIZ_COMPONENT_LIMITS } from 'soma-foundation/kit-limits';
|
|
19
|
+
import { kitText } from '../../i18n/kit-text';
|
|
20
|
+
|
|
21
|
+
/** 语义事件名(SCOPE | semantic 要求小写 snake_case) */
|
|
22
|
+
export const KIT_IMPORT_EVENTS = {
|
|
23
|
+
files: 'import_files',
|
|
24
|
+
map: 'import_map',
|
|
25
|
+
next: 'import_next',
|
|
26
|
+
prev: 'import_prev',
|
|
27
|
+
commit: 'import_commit',
|
|
28
|
+
download: 'import_download',
|
|
29
|
+
restart: 'import_restart',
|
|
30
|
+
result: 'import_result',
|
|
31
|
+
} as const;
|
|
32
|
+
|
|
33
|
+
export const KIT_IMPORT_STEPS = 4;
|
|
34
|
+
|
|
35
|
+
interface ImportLimits { maxRows: number; maxFileKb: number; previewRows: number; concurrency: number; maxErrorText: number }
|
|
36
|
+
const LIMITS: ImportLimits = KIT_BIZ_COMPONENT_LIMITS.ImportWizard.limits;
|
|
37
|
+
|
|
38
|
+
/** 镜像键(compile 与测试共用) */
|
|
39
|
+
export const KIT_IMPORT_MIRRORS = {
|
|
40
|
+
at: (i: number): { key: string; path: string; as: 'truthy' } => ({ key: `imp_at_${i}`, path: `at.s${i}`, as: 'truthy' }),
|
|
41
|
+
past: (i: number): { key: string; path: string; as: 'truthy' } => ({ key: `imp_past_${i}`, path: `past.s${i}`, as: 'truthy' }),
|
|
42
|
+
fut: (i: number): { key: string; path: string; as: 'truthy' } => ({ key: `imp_fut_${i}`, path: `fut.s${i}`, as: 'truthy' }),
|
|
43
|
+
files: { key: 'imp_files', path: 'files', as: 'value' } as const,
|
|
44
|
+
parseError: { key: 'imp_parse_error', path: 'parse_error', as: 'value' } as const,
|
|
45
|
+
fileInfo: { key: 'imp_file_info', path: 'file_info', as: 'value' } as const,
|
|
46
|
+
upReady: { key: 'imp_up_ready', path: 'up_ready', as: 'truthy' } as const,
|
|
47
|
+
upBlocked: { key: 'imp_up_blocked', path: 'up_blocked', as: 'truthy' } as const,
|
|
48
|
+
mapData: { key: 'imp_map_data', path: 'map_data', as: 'value' } as const,
|
|
49
|
+
mapReady: { key: 'imp_map_ready', path: 'map_ready', as: 'truthy' } as const,
|
|
50
|
+
mapBlocked: { key: 'imp_map_blocked', path: 'map_blocked', as: 'truthy' } as const,
|
|
51
|
+
summary: { key: 'imp_summary', path: 'summary', as: 'value' } as const,
|
|
52
|
+
preview: { key: 'imp_preview', path: 'preview', as: 'value' } as const,
|
|
53
|
+
hasErrors: { key: 'imp_has_errors', path: 'has_errors', as: 'truthy' } as const,
|
|
54
|
+
commitReady: { key: 'imp_commit_ready', path: 'commit_ready', as: 'truthy' } as const,
|
|
55
|
+
commitBlocked: { key: 'imp_commit_blocked', path: 'commit_blocked', as: 'truthy' } as const,
|
|
56
|
+
busy: { key: 'imp_busy', path: 'busy', as: 'truthy' } as const,
|
|
57
|
+
notBusy: { key: 'imp_not_busy', path: 'not_busy', as: 'truthy' } as const,
|
|
58
|
+
result: { key: 'imp_result', path: 'result', as: 'value' } as const,
|
|
59
|
+
} as const;
|
|
60
|
+
|
|
61
|
+
type Rec = Record<string, unknown>;
|
|
62
|
+
|
|
63
|
+
function isRec(raw: unknown): raw is Rec {
|
|
64
|
+
return raw !== null && typeof raw === 'object' && !Array.isArray(raw);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ───────── CSV:解析、映射建议、行转换、报告(均为纯函数) ─────────
|
|
68
|
+
|
|
69
|
+
/** RFC 4180 的 CSV 解析:引号包裹、双引号转义、CRLF / LF;去掉 BOM 与全空行。返回行数组(每行是单元格数组) */
|
|
70
|
+
export function parseCsv(text: string): string[][] {
|
|
71
|
+
const src = text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
|
|
72
|
+
const rows: string[][] = [];
|
|
73
|
+
let row: string[] = [];
|
|
74
|
+
let cell = '';
|
|
75
|
+
let quoted = false;
|
|
76
|
+
let i = 0;
|
|
77
|
+
while (i < src.length) {
|
|
78
|
+
const ch = src[i]!;
|
|
79
|
+
if (quoted) {
|
|
80
|
+
if (ch === '"') {
|
|
81
|
+
if (src[i + 1] === '"') { cell += '"'; i += 2; continue; }
|
|
82
|
+
quoted = false;
|
|
83
|
+
i += 1;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
cell += ch;
|
|
87
|
+
i += 1;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (ch === '"' && cell === '') { quoted = true; i += 1; continue; }
|
|
91
|
+
if (ch === ',') { row.push(cell); cell = ''; i += 1; continue; }
|
|
92
|
+
if (ch === '\r' || ch === '\n') {
|
|
93
|
+
if (ch === '\r' && src[i + 1] === '\n') i += 1;
|
|
94
|
+
row.push(cell);
|
|
95
|
+
cell = '';
|
|
96
|
+
rows.push(row);
|
|
97
|
+
row = [];
|
|
98
|
+
i += 1;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
cell += ch;
|
|
102
|
+
i += 1;
|
|
103
|
+
}
|
|
104
|
+
if (cell !== '' || row.length > 0) { row.push(cell); rows.push(row); }
|
|
105
|
+
return rows.filter((r) => r.some((c) => c.trim() !== ''));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface KitImportTarget { key: string; label: string; required: boolean }
|
|
109
|
+
|
|
110
|
+
/** 自动建议映射:表头与目标 key 或 label 忽略大小写与首尾空白相等即配上;一个表头只配一个目标 */
|
|
111
|
+
export function suggestMapping(targets: readonly KitImportTarget[], columns: readonly string[]): Record<string, string> {
|
|
112
|
+
const out: Record<string, string> = {};
|
|
113
|
+
const used = new Set<string>();
|
|
114
|
+
for (const t of targets) {
|
|
115
|
+
const hit = columns.find((c) => !used.has(c) && [t.key, t.label].some((n) => n.trim().toLowerCase() === c.trim().toLowerCase()));
|
|
116
|
+
if (hit !== undefined) { out[t.key] = hit; used.add(hit); }
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** 必填目标里还没映射的 key */
|
|
122
|
+
export function missingRequired(targets: readonly KitImportTarget[], mapping: Readonly<Record<string, string>>): string[] {
|
|
123
|
+
return targets.filter((t) => t.required && (mapping[t.key] ?? '') === '').map((t) => t.key);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 一行原始值(表头 -> 单元格)按映射转成目标 model,并按字段类型转换(number / boolean),空串不写入 */
|
|
127
|
+
export function mapRow(
|
|
128
|
+
row: Readonly<Record<string, string>>,
|
|
129
|
+
mapping: Readonly<Record<string, string>>,
|
|
130
|
+
types: Readonly<Record<string, string | undefined>>,
|
|
131
|
+
): Record<string, unknown> {
|
|
132
|
+
const out: Record<string, unknown> = {};
|
|
133
|
+
for (const [target, column] of Object.entries(mapping)) {
|
|
134
|
+
const raw = (row[column] ?? '').trim();
|
|
135
|
+
if (raw === '') continue;
|
|
136
|
+
const type = types[target];
|
|
137
|
+
if (type === 'int' || type === 'decimal' || type === 'money' || type === 'percent') {
|
|
138
|
+
const n = Number(raw);
|
|
139
|
+
out[target] = Number.isFinite(n) ? n : raw;
|
|
140
|
+
} else if (type === 'bool') {
|
|
141
|
+
const t = raw.toLowerCase();
|
|
142
|
+
out[target] = t === 'true' || t === '1' || t === '是' || t === 'yes' ? true : t === 'false' || t === '0' || t === '否' || t === 'no' ? false : raw;
|
|
143
|
+
} else {
|
|
144
|
+
out[target] = raw;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** CSV 单元格转义;以 = + - @ 开头的文本前补单引号,防止在表格软件里被当公式执行 */
|
|
151
|
+
export function csvCell(value: unknown): string {
|
|
152
|
+
let s = value === undefined || value === null ? '' : String(value);
|
|
153
|
+
if (/^[=+\-@]/.test(s)) s = `'${s}`;
|
|
154
|
+
return /[",\r\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface KitImportIssue { row: number; field: string; message: string }
|
|
158
|
+
|
|
159
|
+
/** 问题行报告(CSV):行号、字段、原因,后接该行的原始列 */
|
|
160
|
+
export function buildIssueReport(
|
|
161
|
+
columns: readonly string[],
|
|
162
|
+
rows: ReadonlyArray<Readonly<Record<string, string>>>,
|
|
163
|
+
issues: readonly KitImportIssue[],
|
|
164
|
+
at?: Element | null,
|
|
165
|
+
): string {
|
|
166
|
+
const head = [...kitText('soma.kit.iw_csv_head', undefined, at).split(','), ...columns].map(csvCell).join(',');
|
|
167
|
+
const lines = issues.map((it) => {
|
|
168
|
+
const raw = rows[it.row - 1] ?? {};
|
|
169
|
+
return [it.row, it.field, it.message, ...columns.map((c) => raw[c] ?? '')].map(csvCell).join(',');
|
|
170
|
+
});
|
|
171
|
+
return [head, ...lines].join('\r\n');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ───────── 状态:mem(本组件内存)-> focus(给页面的镜像值) ─────────
|
|
175
|
+
|
|
176
|
+
export interface KitImportFile { name: string; size: number; status: 'done' | 'error' }
|
|
177
|
+
|
|
178
|
+
export interface KitImportMem {
|
|
179
|
+
step: number;
|
|
180
|
+
files: KitImportFile[];
|
|
181
|
+
parseError: string;
|
|
182
|
+
columns: string[];
|
|
183
|
+
rows: Array<Record<string, string>>;
|
|
184
|
+
mapping: Record<string, string>;
|
|
185
|
+
/** 预览校验结果;null = 还没校验 */
|
|
186
|
+
checked: { okRows: number[]; issues: KitImportIssue[] } | null;
|
|
187
|
+
busy: boolean;
|
|
188
|
+
progress: { done: number; total: number } | null;
|
|
189
|
+
result: { status: string; title: string; subtitle: string; extra: Array<{ key: string; label: string }> } | null;
|
|
190
|
+
seq: number;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function initialImportMem(seq = 0): KitImportMem {
|
|
194
|
+
return {
|
|
195
|
+
step: 0, files: [], parseError: '', columns: [], rows: [], mapping: {}, checked: null, busy: false, progress: null, result: null, seq,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export interface KitImportFocus {
|
|
200
|
+
step: number;
|
|
201
|
+
at: Record<string, boolean>;
|
|
202
|
+
past: Record<string, boolean>;
|
|
203
|
+
fut: Record<string, boolean>;
|
|
204
|
+
files: KitImportFile[];
|
|
205
|
+
parse_error: string;
|
|
206
|
+
file_info: string;
|
|
207
|
+
up_ready: boolean;
|
|
208
|
+
up_blocked: boolean;
|
|
209
|
+
map_data: { targets: KitImportTarget[]; sources: string[]; mapping: Record<string, string> };
|
|
210
|
+
map_ready: boolean;
|
|
211
|
+
map_blocked: boolean;
|
|
212
|
+
summary: string;
|
|
213
|
+
preview: Array<{ id: string; title: string; sub: string; tags: Array<{ label: string; tone: string }> }>;
|
|
214
|
+
has_errors: boolean;
|
|
215
|
+
commit_ready: boolean;
|
|
216
|
+
commit_blocked: boolean;
|
|
217
|
+
busy: boolean;
|
|
218
|
+
not_busy: boolean;
|
|
219
|
+
result: Rec;
|
|
220
|
+
seq: number;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function previewTitle(row: Readonly<Record<string, string>>, columns: readonly string[], at?: Element | null): string {
|
|
224
|
+
const cells = columns.map((c) => (row[c] ?? '').trim()).filter((c) => c !== '').slice(0, 3);
|
|
225
|
+
return cells.join(' / ') || kitText('soma.kit.iw_empty_row', undefined, at);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function clip(text: string): string {
|
|
229
|
+
return text.length > LIMITS.maxErrorText ? `${text.slice(0, LIMITS.maxErrorText)}…` : text;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** 纯函数:mem + 目标字段 -> 页面镜像值 */
|
|
233
|
+
export function viewOfImport(mem: KitImportMem, targets: readonly KitImportTarget[], lang?: Element | null): KitImportFocus {
|
|
234
|
+
const at: Record<string, boolean> = {};
|
|
235
|
+
const past: Record<string, boolean> = {};
|
|
236
|
+
const fut: Record<string, boolean> = {};
|
|
237
|
+
for (let i = 0; i < KIT_IMPORT_STEPS; i += 1) {
|
|
238
|
+
at[`s${i}`] = i === mem.step;
|
|
239
|
+
past[`s${i}`] = i < mem.step;
|
|
240
|
+
fut[`s${i}`] = i > mem.step;
|
|
241
|
+
}
|
|
242
|
+
const missing = missingRequired(targets, mem.mapping);
|
|
243
|
+
const mapReady = Object.keys(mem.mapping).length > 0 && missing.length === 0;
|
|
244
|
+
const upReady = mem.rows.length > 0 && mem.parseError === '';
|
|
245
|
+
const checked = mem.checked;
|
|
246
|
+
const errorRows = checked === null ? 0 : new Set(checked.issues.map((x) => x.row)).size;
|
|
247
|
+
const okCount = checked === null ? 0 : checked.okRows.length;
|
|
248
|
+
let summary = '';
|
|
249
|
+
if (mem.step === 2 && checked !== null) summary = kitText('soma.kit.iw_summary', { total: mem.rows.length, ok: okCount, bad: errorRows }, lang);
|
|
250
|
+
if (mem.busy && mem.progress !== null) summary = kitText('soma.kit.iw_progress', { done: mem.progress.done, total: mem.progress.total }, lang);
|
|
251
|
+
const preview: KitImportFocus['preview'] = [];
|
|
252
|
+
if (checked !== null) {
|
|
253
|
+
const byRow = new Map<number, string[]>();
|
|
254
|
+
for (const it of checked.issues) byRow.set(it.row, [...(byRow.get(it.row) ?? []), it.field !== '' ? `${it.field}${kitText('soma.kit.colon', undefined, lang)}${it.message}` : it.message]);
|
|
255
|
+
// 问题行排在前面,通过的行补满预览上限
|
|
256
|
+
const bad = [...byRow.keys()].sort((a, b) => a - b);
|
|
257
|
+
const good = checked.okRows.slice(0, Math.max(0, LIMITS.previewRows - bad.length));
|
|
258
|
+
for (const n of [...bad, ...good].slice(0, LIMITS.previewRows)) {
|
|
259
|
+
const row = mem.rows[n - 1] ?? {};
|
|
260
|
+
const errs = byRow.get(n);
|
|
261
|
+
preview.push({
|
|
262
|
+
id: kitText('soma.kit.iw_row_n', { n }, lang),
|
|
263
|
+
title: previewTitle(row, mem.columns, lang),
|
|
264
|
+
sub: errs === undefined ? '' : clip(errs.join(kitText('soma.kit.semicolon', undefined, lang))),
|
|
265
|
+
tags: errs === undefined ? [{ label: kitText('soma.kit.iw_pass', undefined, lang), tone: 'success' }] : [{ label: kitText('soma.kit.iw_problem', undefined, lang), tone: 'danger' }],
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const info = mem.files[0] === undefined || mem.parseError !== '' ? '' : kitText('soma.kit.iw_file_info', { name: mem.files[0].name, rows: mem.rows.length, cols: mem.columns.length }, lang);
|
|
270
|
+
return {
|
|
271
|
+
step: mem.step,
|
|
272
|
+
at, past, fut,
|
|
273
|
+
files: mem.files,
|
|
274
|
+
parse_error: mem.parseError,
|
|
275
|
+
file_info: info,
|
|
276
|
+
up_ready: upReady,
|
|
277
|
+
up_blocked: !upReady,
|
|
278
|
+
map_data: { targets: [...targets], sources: mem.columns, mapping: mem.mapping },
|
|
279
|
+
map_ready: mapReady,
|
|
280
|
+
map_blocked: !mapReady,
|
|
281
|
+
summary,
|
|
282
|
+
preview,
|
|
283
|
+
has_errors: errorRows > 0 || (mem.result !== null && (checked?.issues.length ?? 0) > 0),
|
|
284
|
+
commit_ready: checked !== null && okCount > 0 && !mem.busy,
|
|
285
|
+
commit_blocked: !(checked !== null && okCount > 0 && !mem.busy),
|
|
286
|
+
busy: mem.busy,
|
|
287
|
+
not_busy: !mem.busy,
|
|
288
|
+
result: mem.result ?? {},
|
|
289
|
+
seq: mem.seq,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// ───────── 组件 ─────────
|
|
294
|
+
|
|
295
|
+
function targetsOf(raw: unknown): KitImportTarget[] {
|
|
296
|
+
if (!Array.isArray(raw)) return [];
|
|
297
|
+
return raw.flatMap((t) => {
|
|
298
|
+
if (!isRec(t) || typeof t.key !== 'string' || t.key === '') return [];
|
|
299
|
+
return [{ key: t.key, label: typeof t.label === 'string' && t.label !== '' ? t.label : t.key, required: t.required === true }];
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function createKitImportHub(): SomaComponentDefinition {
|
|
304
|
+
return {
|
|
305
|
+
contract: {
|
|
306
|
+
name: 'KitImportHub',
|
|
307
|
+
layer: 'data',
|
|
308
|
+
props: {
|
|
309
|
+
scope: { type: 'string', default: '', desc: '区域作用域 id;订阅它的语义事件,持有 focus 切片' },
|
|
310
|
+
object: { type: 'string', default: '', desc: '导入目标数据对象名(带 write;字段字典是映射目标)' },
|
|
311
|
+
targets: { type: 'array', default: [], desc: '映射目标 [{ key, label, required }](来自目标对象字典)' },
|
|
312
|
+
types: { type: 'object', default: {}, desc: '目标字段 -> 字段类型(int / decimal / money / percent 转数字,bool 转布尔)' },
|
|
313
|
+
file_name: { type: 'string', default: 'import-issues.csv', desc: '问题行报告的文件名' },
|
|
314
|
+
},
|
|
315
|
+
emits: {
|
|
316
|
+
step_change: { payload: { step: { type: 'number' } }, desc: '当前步变化' },
|
|
317
|
+
imported: { payload: { ok: { type: 'number' }, failed: { type: 'number' } }, desc: '导入结束(成功 / 失败行数)' },
|
|
318
|
+
},
|
|
319
|
+
scope: { slices: ['focus'] },
|
|
320
|
+
},
|
|
321
|
+
services: ['region', 'data', 'notify'],
|
|
322
|
+
templateHtml: '<div data-soma-id="root" class="hidden"></div>',
|
|
323
|
+
controllerFactory: (ctx: SomaComponentControllerContext) => {
|
|
324
|
+
let off: (() => void) | undefined;
|
|
325
|
+
let alive = false;
|
|
326
|
+
let mem = initialImportMem();
|
|
327
|
+
/** 当前选中的文件对象(不进切片,只在浏览器内存里) */
|
|
328
|
+
let pickedFile: File | null = null;
|
|
329
|
+
const props = (): Rec => (ctx.readProps ? ctx.readProps() : ctx.props);
|
|
330
|
+
const targets = (): KitImportTarget[] => targetsOf(props().targets);
|
|
331
|
+
const types = (): Record<string, string | undefined> => {
|
|
332
|
+
const t = props().types;
|
|
333
|
+
return isRec(t) ? (t as Record<string, string | undefined>) : {};
|
|
334
|
+
};
|
|
335
|
+
const dataPort = (): SomaDataPort | undefined => {
|
|
336
|
+
const port = (ctx.services as { data?: SomaDataPort } | undefined)?.data;
|
|
337
|
+
return port && typeof port === 'object' ? port : undefined;
|
|
338
|
+
};
|
|
339
|
+
const toast = (level: string, message: string): void => {
|
|
340
|
+
const svc = (ctx.services as { notify?: { toast?: (o: { level: string; message: string }) => void } } | undefined)?.notify;
|
|
341
|
+
if (svc && typeof svc.toast === 'function') svc.toast({ level, message });
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const run = (port: SomaRegionPort, next: KitImportMem): void => {
|
|
345
|
+
const prevStep = mem.step;
|
|
346
|
+
mem = { ...next, seq: mem.seq + 1 };
|
|
347
|
+
port.slice('focus')?.set(viewOfImport(mem, targets(), ctx.host) as unknown as Rec);
|
|
348
|
+
if (mem.step !== prevStep) ctx.emit('step_change', { step: mem.step });
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const readFile = async (port: SomaRegionPort, file: File): Promise<void> => {
|
|
352
|
+
const files: KitImportFile[] = [{ name: file.name, size: file.size, status: 'done' }];
|
|
353
|
+
if (file.size > LIMITS.maxFileKb * 1024) {
|
|
354
|
+
run(port, { ...mem, files: [{ ...files[0]!, status: 'error' }], parseError: kitText('soma.kit.iw_too_big', { n: LIMITS.maxFileKb }, ctx.host), columns: [], rows: [], mapping: {}, checked: null });
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
let text = '';
|
|
358
|
+
try {
|
|
359
|
+
text = await file.text();
|
|
360
|
+
} catch {
|
|
361
|
+
if (alive) run(port, { ...mem, files: [{ ...files[0]!, status: 'error' }], parseError: kitText('soma.kit.iw_read_failed', undefined, ctx.host), columns: [], rows: [], mapping: {}, checked: null });
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (!alive || pickedFile !== file) return;
|
|
365
|
+
const table = parseCsv(text);
|
|
366
|
+
const header = (table[0] ?? []).map((h) => h.trim());
|
|
367
|
+
const body = table.slice(1);
|
|
368
|
+
let error = '';
|
|
369
|
+
if (header.length === 0 || header.every((h) => h === '')) error = kitText('soma.kit.iw_no_header', undefined, ctx.host);
|
|
370
|
+
else if (new Set(header).size !== header.length) error = kitText('soma.kit.iw_dup_header', undefined, ctx.host);
|
|
371
|
+
else if (body.length === 0) error = kitText('soma.kit.iw_no_rows', undefined, ctx.host);
|
|
372
|
+
else if (body.length > LIMITS.maxRows) error = kitText('soma.kit.iw_too_many', { n: LIMITS.maxRows }, ctx.host);
|
|
373
|
+
if (error !== '') {
|
|
374
|
+
run(port, { ...mem, files: [{ ...files[0]!, status: 'error' }], parseError: error, columns: [], rows: [], mapping: {}, checked: null });
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const rows = body.map((cells) => {
|
|
378
|
+
const r: Record<string, string> = {};
|
|
379
|
+
header.forEach((h, i) => { r[h] = cells[i] ?? ''; });
|
|
380
|
+
return r;
|
|
381
|
+
});
|
|
382
|
+
run(port, { ...mem, files, parseError: '', columns: header, rows, mapping: suggestMapping(targets(), header), checked: null });
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
const validateAll = (): { okRows: number[]; issues: KitImportIssue[] } => {
|
|
386
|
+
const data = dataPort();
|
|
387
|
+
const object = typeof props().object === 'string' ? (props().object as string) : '';
|
|
388
|
+
const okRows: number[] = [];
|
|
389
|
+
const issues: KitImportIssue[] = [];
|
|
390
|
+
mem.rows.forEach((raw, i) => {
|
|
391
|
+
const n = i + 1;
|
|
392
|
+
const model = mapRow(raw, mem.mapping, types());
|
|
393
|
+
const errors = data && object !== '' ? data.validate(object, model, 'submit') : {};
|
|
394
|
+
const entries = Object.entries(errors).filter(([, m]) => typeof m === 'string' && m !== '');
|
|
395
|
+
if (entries.length === 0) okRows.push(n);
|
|
396
|
+
else for (const [field, message] of entries) issues.push({ row: n, field, message });
|
|
397
|
+
});
|
|
398
|
+
return { okRows, issues };
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
const commit = async (port: SomaRegionPort): Promise<void> => {
|
|
402
|
+
const data = dataPort();
|
|
403
|
+
const object = typeof props().object === 'string' ? (props().object as string) : '';
|
|
404
|
+
const checked = mem.checked;
|
|
405
|
+
if (!data || object === '' || checked === null || checked.okRows.length === 0) return;
|
|
406
|
+
const seq0 = mem.seq + 1;
|
|
407
|
+
const queue = [...checked.okRows];
|
|
408
|
+
const total = queue.length;
|
|
409
|
+
const failed: KitImportIssue[] = [];
|
|
410
|
+
let done = 0;
|
|
411
|
+
run(port, { ...mem, step: 3, busy: true, progress: { done: 0, total }, result: null });
|
|
412
|
+
const worker = async (): Promise<void> => {
|
|
413
|
+
for (;;) {
|
|
414
|
+
const n = queue.shift();
|
|
415
|
+
if (n === undefined || !alive) return;
|
|
416
|
+
const r = await data.submit(object, mapRow(mem.rows[n - 1] ?? {}, mem.mapping, types()), 'create');
|
|
417
|
+
if (!r.ok) {
|
|
418
|
+
const fe = Object.entries(r.fieldErrors ?? {});
|
|
419
|
+
if (fe.length > 0) for (const [field, message] of fe) failed.push({ row: n, field, message });
|
|
420
|
+
else failed.push({ row: n, field: '', message: r.message !== '' ? r.message : '写入失败' });
|
|
421
|
+
}
|
|
422
|
+
done += 1;
|
|
423
|
+
// 重新开始(seq 变了)后,过期的进度不再写回
|
|
424
|
+
if (alive && mem.seq === seq0) run(port, { ...mem, progress: { done, total } });
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
await Promise.all(Array.from({ length: Math.min(LIMITS.concurrency, total) }, () => worker()));
|
|
428
|
+
if (!alive || mem.step !== 3) return;
|
|
429
|
+
const okCount = total - new Set(failed.map((f) => f.row)).size;
|
|
430
|
+
const failCount = total - okCount;
|
|
431
|
+
const issues = [...checked.issues, ...failed];
|
|
432
|
+
const skipped = new Set(checked.issues.map((x) => x.row)).size;
|
|
433
|
+
const status = failCount === 0 && skipped === 0 ? 'success' : okCount === 0 ? 'error' : 'warning';
|
|
434
|
+
const subtitle = kitText('soma.kit.iw_sub', { ok: okCount, fail: failCount, skipped }, ctx.host);
|
|
435
|
+
const extra: Array<{ key: string; label: string }> = [];
|
|
436
|
+
if (issues.length > 0) extra.push({ key: 'download', label: kitText('soma.kit.iw_download_issues', undefined, ctx.host) });
|
|
437
|
+
extra.push({ key: 'restart', label: kitText('soma.kit.iw_restart', undefined, ctx.host) });
|
|
438
|
+
run(port, {
|
|
439
|
+
...mem, busy: false, progress: null,
|
|
440
|
+
checked: { okRows: checked.okRows.filter((n) => !failed.some((f) => f.row === n)), issues },
|
|
441
|
+
result: { status, title: kitText(status === 'success' ? 'soma.kit.iw_done' : status === 'error' ? 'soma.kit.iw_failed' : 'soma.kit.iw_partial', undefined, ctx.host), subtitle, extra },
|
|
442
|
+
});
|
|
443
|
+
ctx.emit('imported', { ok: okCount, failed: failCount + skipped });
|
|
444
|
+
if (failCount === 0 && skipped === 0) toast('success', kitText('soma.kit.iw_imported', { n: okCount }, ctx.host));
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
const download = (): void => {
|
|
448
|
+
const checked = mem.checked;
|
|
449
|
+
if (!checked || checked.issues.length === 0 || typeof document === 'undefined' || typeof Blob === 'undefined') return;
|
|
450
|
+
const csv = buildIssueReport(mem.columns, mem.rows, checked.issues, ctx.host);
|
|
451
|
+
const url = URL.createObjectURL(new Blob([`${csv}`], { type: 'text/csv;charset=utf-8' }));
|
|
452
|
+
const a = document.createElement('a');
|
|
453
|
+
a.href = url;
|
|
454
|
+
const name = typeof props().file_name === 'string' && props().file_name !== '' ? (props().file_name as string) : 'import-issues.csv';
|
|
455
|
+
a.download = name;
|
|
456
|
+
a.rel = 'noopener';
|
|
457
|
+
a.click();
|
|
458
|
+
setTimeout(() => URL.revokeObjectURL(url), 0);
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
return {
|
|
462
|
+
onMount(): void {
|
|
463
|
+
alive = true;
|
|
464
|
+
const scope = typeof props().scope === 'string' ? (props().scope as string) : '';
|
|
465
|
+
const svc = (ctx.services as { region?: (id: string) => SomaRegionPort | null } | undefined)?.region;
|
|
466
|
+
const port = scope !== '' && typeof svc === 'function' ? svc(scope) : null;
|
|
467
|
+
const focus = port?.slice('focus');
|
|
468
|
+
if (!port || !focus) return;
|
|
469
|
+
mem = initialImportMem();
|
|
470
|
+
focus.set(viewOfImport(mem, targets(), ctx.host) as unknown as Rec);
|
|
471
|
+
off = port.onSemantic((name, payload) => {
|
|
472
|
+
const p = isRec(payload) ? payload : {};
|
|
473
|
+
switch (name) {
|
|
474
|
+
case KIT_IMPORT_EVENTS.files: {
|
|
475
|
+
if (mem.busy) return;
|
|
476
|
+
const list = Array.isArray(p.value) ? p.value.filter(isRec) : [];
|
|
477
|
+
const first = list.find((f) => typeof File !== 'undefined' && f.file instanceof File);
|
|
478
|
+
if (first === undefined) {
|
|
479
|
+
pickedFile = null;
|
|
480
|
+
run(port, { ...mem, files: [], parseError: '', columns: [], rows: [], mapping: {}, checked: null });
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
const file = first.file as File;
|
|
484
|
+
pickedFile = file;
|
|
485
|
+
run(port, { ...mem, files: [{ name: file.name, size: file.size, status: 'done' }], parseError: '', columns: [], rows: [], mapping: {}, checked: null });
|
|
486
|
+
void readFile(port, file);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
case KIT_IMPORT_EVENTS.map: {
|
|
490
|
+
if (mem.busy || !isRec(p.value)) return;
|
|
491
|
+
const mapping: Record<string, string> = {};
|
|
492
|
+
for (const [k, v] of Object.entries(p.value)) if (typeof v === 'string' && v !== '') mapping[k] = v;
|
|
493
|
+
run(port, { ...mem, mapping, checked: null });
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
case KIT_IMPORT_EVENTS.next: {
|
|
497
|
+
if (mem.busy) return;
|
|
498
|
+
if (mem.step === 0 && mem.rows.length > 0 && mem.parseError === '') {
|
|
499
|
+
run(port, { ...mem, step: 1 });
|
|
500
|
+
} else if (mem.step === 1 && missingRequired(targets(), mem.mapping).length === 0 && Object.keys(mem.mapping).length > 0) {
|
|
501
|
+
run(port, { ...mem, step: 2, checked: validateAll() });
|
|
502
|
+
}
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
case KIT_IMPORT_EVENTS.prev:
|
|
506
|
+
if (!mem.busy && (mem.step === 1 || mem.step === 2)) run(port, { ...mem, step: mem.step - 1 });
|
|
507
|
+
return;
|
|
508
|
+
case KIT_IMPORT_EVENTS.commit:
|
|
509
|
+
if (!mem.busy && mem.step === 2) void commit(port);
|
|
510
|
+
return;
|
|
511
|
+
case KIT_IMPORT_EVENTS.download:
|
|
512
|
+
download();
|
|
513
|
+
return;
|
|
514
|
+
case KIT_IMPORT_EVENTS.result: {
|
|
515
|
+
const key = typeof p.key === 'string' ? p.key : '';
|
|
516
|
+
if (key === 'download') download();
|
|
517
|
+
else if (key === 'restart' && !mem.busy) { pickedFile = null; run(port, initialImportMem(mem.seq)); }
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
case KIT_IMPORT_EVENTS.restart:
|
|
521
|
+
if (!mem.busy) { pickedFile = null; run(port, initialImportMem(mem.seq)); }
|
|
522
|
+
return;
|
|
523
|
+
default:
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
},
|
|
528
|
+
onUnmount(): void {
|
|
529
|
+
alive = false;
|
|
530
|
+
pickedFile = null;
|
|
531
|
+
if (off) off();
|
|
532
|
+
off = undefined;
|
|
533
|
+
},
|
|
534
|
+
};
|
|
535
|
+
},
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** 本组件模板里的类(候选类) */
|
|
540
|
+
export const KIT_IMPORT_HUB_CLASS_CANDIDATES: readonly string[] = Object.freeze(['hidden']);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// ImportWizard:导入向导(上传 -> 映射 -> 预览校验 -> 结果)。
|
|
2
|
+
// 对象角色槽:target(必填:schema 是映射目标字典,behavior.write 是逐行新增的写接口,title 作卡片标题);
|
|
3
|
+
// job(可选,只读:结果步里的后台任务面板,常用 AsyncResultPanel)。
|
|
4
|
+
|
|
5
|
+
import { defineKitBizComponent } from '../define';
|
|
6
|
+
import { compileImportWizard } from './compile';
|
|
7
|
+
import { IMPORT_WIZARD_AREAS } from './shell';
|
|
8
|
+
|
|
9
|
+
export const ImportWizard = defineKitBizComponent({
|
|
10
|
+
kind: 'ImportWizard',
|
|
11
|
+
roles: {
|
|
12
|
+
target: { required: true, views: ['form'] },
|
|
13
|
+
job: { required: false, views: ['summary', 'list'] },
|
|
14
|
+
},
|
|
15
|
+
areas: [...IMPORT_WIZARD_AREAS],
|
|
16
|
+
compile: compileImportWizard,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export { compileImportWizard, importWizardObjectName } from './compile';
|
|
20
|
+
export {
|
|
21
|
+
createKitImportHub, KIT_IMPORT_EVENTS, KIT_IMPORT_MIRRORS, KIT_IMPORT_HUB_CLASS_CANDIDATES,
|
|
22
|
+
parseCsv, suggestMapping, missingRequired, mapRow, csvCell, buildIssueReport, viewOfImport, initialImportMem,
|
|
23
|
+
} from './hub';
|
|
24
|
+
export { IMPORT_WIZARD_AREAS, IMPORT_WIZARD_CLASS, importWizardIds } from './shell';
|