soma-kit 0.1.10 → 0.1.12
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 +103 -110
- package/bin/soma-kit.mjs +1 -1
- package/bin/soma.mjs +22 -0
- package/package.json +13 -7
- package/packs/finance/reconcile-board.ts +5 -2
- package/packs/finance/reconcile-desk/compile.ts +3 -2
- package/spec/generated/kit-app.schema.json +776 -38
- package/spec/generated/kit-page.schema.json +1613 -224
- package/spec/kit-grammar.json +770 -529
- package/spec/kit-grammar.schema.json +1962 -392
- package/src/app/app-config.ts +371 -347
- package/src/app/component-gate.ts +12 -15
- package/src/app/compose-apps.ts +79 -8
- package/src/app/define-kit-actions.ts +245 -41
- package/src/app/define-kit-app.ts +174 -31
- package/src/app/define-kit-view.ts +100 -0
- package/src/app/envelope.ts +1 -161
- package/src/app/index.ts +13 -2
- package/src/app/kit-view-context.ts +290 -0
- package/src/app/notify-renderer.ts +1 -115
- package/src/app/paged-list-adapter.ts +1 -240
- package/src/app/server-pages.ts +8 -2
- package/src/app/server.ts +491 -101
- package/src/app/submit.types.ts +1 -1
- package/src/app/theme-endpoint.ts +63 -0
- package/src/app/theme-runtime-shared.ts +1 -0
- package/src/app/theme-runtime.ts +663 -0
- package/src/base/action-busy.ts +2 -2
- package/src/base/action-gate.ts +4 -255
- package/src/base/action-slots.ts +47 -0
- package/src/base/alert.ts +5 -3
- package/src/base/avatar.ts +2 -2
- package/src/base/button.ts +46 -29
- package/src/base/cascader.ts +11 -12
- package/src/base/check-core.ts +1 -1
- package/src/base/choice.ts +28 -26
- package/src/base/class-candidates.ts +17 -0
- package/src/base/color-picker.ts +6 -6
- package/src/base/combobox.ts +5 -3
- package/src/base/command-palette.ts +9 -5
- package/src/base/countdown.ts +1 -1
- package/src/base/date-range.ts +25 -11
- package/src/base/date.ts +85 -17
- package/src/base/divider.ts +2 -2
- package/src/base/fab.ts +11 -4
- package/src/base/image.ts +1 -1
- package/src/base/index.ts +9 -3
- package/src/base/input.ts +42 -13
- package/src/base/kbd.ts +3 -1
- package/src/base/link.ts +4 -4
- package/src/base/listbox-core.ts +50 -30
- package/src/base/loading-gate.ts +133 -0
- package/src/base/mentions.ts +5 -5
- package/src/base/menu-button.ts +6 -1
- package/src/base/menu-core.ts +4 -0
- package/src/base/motion/aura.ts +2 -2
- package/src/base/motion/hover-3d.ts +2 -2
- package/src/base/motion/hover-gallery.ts +2 -2
- package/src/base/motion/marquee.ts +2 -2
- package/src/base/motion/text-rotate.ts +2 -2
- package/src/base/number.ts +93 -80
- package/src/base/otp.ts +4 -1
- package/src/base/permission-gate.ts +6 -0
- package/src/base/popover.ts +40 -8
- package/src/base/popup.ts +79 -14
- package/src/base/price-text.ts +2 -2
- package/src/base/progress.ts +2 -2
- package/src/base/qr-code.ts +4 -4
- package/src/base/qty-stepper.ts +9 -3
- package/src/base/range.ts +2 -2
- package/src/base/rating.ts +1 -1
- package/src/base/segmented.ts +4 -0
- package/src/base/select.ts +4 -3
- package/src/base/shared.ts +221 -29
- package/src/base/swap.ts +4 -1
- package/src/base/switch.ts +1 -1
- package/src/base/tag-input.ts +14 -10
- package/src/base/tag.ts +5 -1
- package/src/base/text.ts +39 -6
- package/src/base/textarea.ts +37 -11
- package/src/base/time-picker.ts +12 -9
- package/src/base/tree-select.ts +15 -8
- package/src/base/upload.ts +34 -5
- package/src/base/virtual-list.ts +107 -1
- package/src/biz/approval-desk/compile.ts +23 -41
- package/src/biz/auth-panel/compile.ts +37 -52
- package/src/biz/auth-panel/index.ts +1 -1
- package/src/biz/category-crud/compile.ts +22 -36
- package/src/biz/checkout-page/compile.ts +21 -5
- package/src/biz/compose.ts +13 -27
- package/src/biz/define.ts +5 -4
- package/src/biz/import-wizard/compile.ts +22 -26
- package/src/biz/import-wizard/hub.ts +126 -61
- package/src/biz/import-wizard/shell.ts +3 -3
- package/src/biz/index.ts +3 -3
- package/src/biz/landing-page/compile.ts +19 -3
- package/src/biz/link/hub.ts +49 -20
- package/src/biz/link/shared.ts +115 -118
- package/src/biz/master-sub/compile.ts +31 -44
- package/src/biz/multi-board/compile.ts +34 -49
- package/src/biz/multi-board/index.ts +18 -1
- package/src/biz/multi-board/shell.ts +24 -15
- package/src/biz/notification-center/compile.ts +21 -33
- package/src/biz/page-templates/domain-templates.ts +80 -13
- package/src/biz/page-templates/result-page/compile.ts +11 -2
- package/src/biz/page-templates/shared.ts +34 -36
- package/src/biz/record-detail/compile.ts +47 -42
- package/src/biz/record-detail/index.ts +11 -1
- package/src/biz/settings-center/compile.ts +6 -5
- package/src/biz/shared/action-host.ts +91 -211
- package/src/biz/shared/compile-helpers.ts +80 -0
- package/src/biz/shared/coordinator.ts +486 -83
- package/src/biz/shared/index.ts +8 -2
- package/src/biz/shared/overlay-shell.ts +80 -0
- package/src/biz/shared/pipes.ts +43 -30
- package/src/biz/shared/remove-key.ts +6 -0
- package/src/biz/shared/remove-wire.ts +29 -0
- package/src/biz/shared/runtime-hub.ts +216 -24
- package/src/biz/single-form/compile.ts +15 -37
- package/src/biz/standard-crud/compile.ts +62 -107
- package/src/biz/standard-crud/shell.ts +16 -66
- package/src/biz/step-form/compile.ts +26 -97
- package/src/biz/step-form/hub.ts +34 -41
- package/src/biz/types.ts +4 -3
- package/src/cli/core-imports.ts +54 -0
- package/src/cli/kit-cli.ts +130 -26
- package/src/cli/kit-init.ts +222 -0
- package/src/cli/service-endpoint-check.ts +425 -0
- package/src/cli/source-path-check.d.mts +17 -0
- package/src/cli/source-path-check.mjs +334 -0
- package/src/cli/view-writes.ts +752 -0
- package/src/compile/action-host-attach.ts +21 -29
- package/src/compile/closed-set.ts +509 -0
- package/src/compile/compile-page.ts +219 -72
- package/src/compile/component-candidates.ts +1 -1
- package/src/compile/hints.ts +30 -1
- package/src/compile/index.ts +1 -0
- package/src/compile/kit-error.ts +1 -0
- package/src/compile/locate.ts +21 -1
- package/src/compile/parse.ts +370 -41
- package/src/compile/redirect.ts +6 -2
- package/src/compile/styles.ts +35 -2
- package/src/compile/types.ts +18 -2
- package/src/compile/view-check.ts +151 -0
- package/src/data/amount-breakdown.ts +2 -2
- package/src/data/anchor-nav.ts +2 -2
- package/src/data/approval-flow.ts +2 -2
- package/src/data/assignment-picker.ts +40 -29
- package/src/data/async-result-panel.ts +62 -6
- package/src/data/breadcrumbs.ts +2 -2
- package/src/data/calendar.ts +22 -25
- package/src/data/carousel.ts +5 -2
- package/src/data/chart.ts +42 -34
- package/src/data/checklist.ts +2 -2
- package/src/data/code-view.ts +4 -2
- package/src/data/comment-thread.ts +5 -2
- package/src/data/confirm-dialog.ts +10 -0
- package/src/data/context-menu.ts +29 -37
- package/src/data/d9-util.ts +2 -18
- package/src/data/diff.ts +4 -2
- package/src/data/dock.ts +2 -2
- package/src/data/embed.ts +2 -1
- package/src/data/field-mapper.ts +31 -26
- package/src/data/flow-editor-contract.ts +1 -1
- package/src/data/flow-editor.ts +89 -100
- package/src/data/flow-node-options.ts +90 -0
- package/src/data/flow-remote-options.ts +0 -103
- package/src/data/footer.ts +2 -2
- package/src/data/form-draft-registry.ts +2 -4
- package/src/data/gantt.ts +14 -17
- package/src/data/hero.ts +2 -2
- package/src/data/image-viewer.ts +20 -26
- package/src/data/index.ts +0 -2
- package/src/data/item-list.ts +31 -22
- package/src/data/kanban.ts +43 -42
- package/src/data/media-grid.ts +14 -19
- package/src/data/menu.ts +2 -2
- package/src/data/mockup.ts +2 -2
- package/src/data/modal-dialog.ts +22 -18
- package/src/data/navbar.ts +7 -2
- package/src/data/notify-renderer.ts +235 -0
- package/src/data/object-form.ts +1036 -648
- package/src/data/pager.ts +65 -29
- package/src/data/permission-matrix.ts +20 -21
- package/src/data/permission-tree.ts +28 -26
- package/src/data/pricing-table.ts +9 -3
- package/src/data/reaction-bar.ts +77 -88
- package/src/data/read-rollback.ts +73 -0
- package/src/data/record-card.ts +3 -3
- package/src/data/record-picker.ts +93 -50
- package/src/data/redirect.ts +32 -32
- package/src/data/remote-options.ts +13 -6
- package/src/data/request-console.ts +5 -3
- package/src/data/resident.ts +2 -1
- package/src/data/result-panel.ts +4 -2
- package/src/data/rich-text.ts +45 -55
- package/src/data/row-editor.ts +285 -52
- package/src/data/rule-builder.ts +18 -12
- package/src/data/shape-util.ts +4 -0
- package/src/data/stat-group.ts +243 -101
- package/src/data/static-registry.ts +22 -11
- package/src/data/steps.ts +2 -2
- package/src/data/survey.ts +24 -29
- package/src/data/system-switcher.ts +2 -2
- package/src/data/timeline.ts +2 -2
- package/src/data/tour-guide.ts +22 -29
- package/src/data/transfer.ts +27 -23
- package/src/data/tree-table.ts +96 -48
- package/src/data/tree.ts +27 -34
- package/src/data/trend-line.ts +6 -24
- package/src/data/virtual-rows.ts +118 -76
- package/src/data/watermark.ts +2 -2
- package/src/data/wrap.ts +63 -38
- package/src/frames/blank-frame.ts +1 -0
- package/src/frames/index.ts +2 -2
- package/src/frames/page-bar.ts +12 -11
- package/src/frames/page-header.ts +14 -5
- package/src/frames/page-status.ts +2 -2
- package/src/frames/shared.ts +23 -17
- package/src/frames/single-frame.ts +1 -0
- package/src/frames/split-frame/shell.ts +5 -5
- package/src/frames/split-frame/wiring.ts +2 -2
- package/src/frames/tabs-frame/shell.ts +2 -13
- package/src/frames/tabs-frame/wiring.ts +4 -8
- package/src/frames/top-main-frame.ts +1 -0
- package/src/frames/types.ts +13 -2
- package/src/frames/workspace-frame/extras.ts +20 -4
- package/src/frames/workspace-frame/index.ts +1 -1
- package/src/frames/workspace-frame/shell.ts +1 -0
- package/src/frames/workspace-frame/state.ts +13 -6
- package/src/frames/workspace-frame/view.ts +119 -25
- package/src/generated/kit-grammar-runtime.ts +430 -366
- package/src/generated/page-origin.ts +0 -11
- package/src/i18n/kit-text.ts +23 -27
- package/src/index.ts +2 -1
- package/src/object-renderer/action-gate.ts +11 -147
- package/src/object-renderer/arrange.ts +71 -77
- package/src/object-renderer/collapse-wiring.ts +2 -2
- package/src/object-renderer/collection-expand.ts +2 -2
- package/src/object-renderer/collection-parts.ts +216 -100
- package/src/object-renderer/collection-table.ts +180 -42
- package/src/object-renderer/collection.ts +95 -79
- package/src/object-renderer/compile-data.ts +85 -100
- package/src/object-renderer/crud-bridge.ts +32 -0
- package/src/object-renderer/defaults.ts +8 -14
- package/src/object-renderer/display-checks.ts +3 -24
- package/src/object-renderer/emit-events.ts +186 -172
- package/src/object-renderer/emit-object.ts +15 -2
- package/src/object-renderer/emit-static.ts +174 -132
- package/src/object-renderer/field-value.ts +593 -396
- package/src/object-renderer/filter-reload.ts +70 -0
- package/src/object-renderer/form-detail.ts +112 -2
- package/src/object-renderer/index.ts +26 -0
- package/src/object-renderer/key-field.ts +12 -0
- package/src/object-renderer/linkage.ts +7 -270
- package/src/object-renderer/merge-class.ts +18 -1
- package/src/object-renderer/path-pick.ts +27 -0
- package/src/object-renderer/render-object.ts +405 -86
- package/src/object-renderer/request-actions.ts +230 -249
- package/src/object-renderer/table-actions.ts +195 -95
- package/src/object-renderer/table-summary.ts +2 -2
- package/src/object-renderer/table-tools.ts +418 -84
- package/src/object-renderer/types.ts +128 -122
- package/src/object-renderer/view-binding.ts +196 -0
- package/src/page-target/ai-surface.ts +37 -0
- package/src/page-target/compile.ts +48 -8
- package/src/page-target/index.ts +8 -1
- package/src/page-target/intent-schema.ts +154 -28
- package/src/page-target/target.ts +257 -50
- package/src/page-target/writer-policy.ts +14 -3
- package/src/recipes/container.ts +165 -0
- package/src/recipes/control.ts +219 -0
- package/src/recipes/index.ts +263 -0
- package/src/recipes/overlay.ts +182 -0
- package/src/recipes/state.ts +70 -0
- package/src/recipes/table-row.ts +51 -0
- package/src/recipes/typography.ts +87 -0
- package/src/shared/action-field-guard.ts +41 -0
- package/src/shared/envelope.ts +207 -0
- package/src/shared/escape.ts +57 -0
- package/src/shared/kit-error.ts +21 -0
- package/src/shared/paged-list-adapter.ts +240 -0
- package/src/shared/path-guard.ts +127 -0
- package/src/shared/theme-runtime-shared.ts +175 -0
- package/src/shared/view-info.ts +10 -0
- package/src/theme/index.ts +12 -0
- package/src/theme/kit-theme-text.ts +1 -1
- package/src/theme/kit-theme.css +38 -1
- package/src/theme/kit-theme.tpl +38 -1
- package/src/theme/nine-dim-structure.ts +77 -0
- package/src/theme/surface-layers.ts +97 -0
- package/src/theme/theme-tokens.ts +85 -79
- package/src/theme/theme-types.ts +71 -0
- package/src/theme/typography.ts +17 -17
- package/src/biz/shared/step-values.ts +0 -25
- package/src/biz/step-form/steps-ref.ts +0 -112
- package/src/data/options-bus.ts +0 -24
- package/src/data/prompt-dialog.ts +0 -235
- package/src/object-renderer/adopt-gate.ts +0 -333
- package/src/object-renderer/field-policy.ts +0 -89
- package/src/object-renderer/outcome-draft.ts +0 -61
- package/src/theme/nine-dim-palettes.ts +0 -1278
|
@@ -1,320 +1,46 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// 纯函数,不碰 DOM:编译期(校验 / 规整)与浏览器(KitObjectForm
|
|
1
|
+
// 字段取值二件套:字段初值 default(GB2,字面量)、隐藏草稿字段 hidden(GB3)。
|
|
2
|
+
// 数字读 kit-limits#formLinkage.fieldValue,本文件不手抄。
|
|
3
|
+
// 纯函数,不碰 DOM:编译期(校验 / 规整)与浏览器(KitObjectForm 取值)共用。
|
|
4
4
|
//
|
|
5
5
|
// 铁律(与规格 4.6.4 一致):
|
|
6
|
-
// · default
|
|
7
|
-
// · hidden 不是安全边界:字段必须可提交,rules 照常校验,权限语义不变,AI
|
|
8
|
-
// · derive 是闭集模板(占位符 + 过滤器),没有表达式引擎:不做运算、比较、函数调用与嵌套。
|
|
6
|
+
// · default 只许字面量,只在新建时填「空且用户没改过」的字段;
|
|
7
|
+
// · hidden 不是安全边界:字段必须可提交,rules 照常校验,权限语义不变,AI 写面不可写。
|
|
9
8
|
|
|
10
9
|
import { KIT_FORM_LINKAGE_LIMITS } from 'soma-foundation/kit-limits';
|
|
11
10
|
import { KIT_FORM_LINKAGE } from './defaults';
|
|
12
|
-
import type { KitDiagnostic,
|
|
11
|
+
import type { KitDiagnostic, KitSchema } from './types';
|
|
13
12
|
|
|
14
13
|
type Json = Record<string, any>;
|
|
15
14
|
type Rec = Record<string, unknown>;
|
|
16
15
|
|
|
17
|
-
const FV = (KIT_FORM_LINKAGE as Json).fieldValue as {
|
|
18
|
-
schemaKeys: readonly string[];
|
|
19
|
-
deriveKeys: readonly string[];
|
|
20
|
-
defaultRefRoots: readonly string[];
|
|
21
|
-
refPattern: string;
|
|
22
|
-
stepsRefPattern: string;
|
|
23
|
-
stepsCanonicalPattern: string;
|
|
24
|
-
placeholderPattern: string;
|
|
25
|
-
stepsPlaceholderPrefix: string;
|
|
26
|
-
filters: readonly string[];
|
|
27
|
-
filterArgs: Readonly<Record<string, string>>;
|
|
28
|
-
eachPattern: string;
|
|
29
|
-
rowPlaceholderPrefix: string;
|
|
30
|
-
rowIndexToken: string;
|
|
31
|
-
};
|
|
32
16
|
const LIM = (KIT_FORM_LINKAGE_LIMITS as unknown as Json).fieldValue as {
|
|
33
|
-
templateMax: number;
|
|
34
|
-
placeholderMax: number;
|
|
35
|
-
filterMax: number;
|
|
36
|
-
orMax: number;
|
|
37
17
|
hiddenMax: number;
|
|
38
18
|
defaultMax: number;
|
|
39
|
-
eachMax: number;
|
|
40
|
-
eachRowsMax: number;
|
|
41
19
|
};
|
|
42
20
|
|
|
43
|
-
const EACH_RE = new RegExp(FV.eachPattern, 'g');
|
|
44
|
-
const REF_RE = new RegExp(FV.refPattern);
|
|
45
|
-
const STEPS_REF_RE = new RegExp(FV.stepsRefPattern);
|
|
46
|
-
const STEPS_CANON_RE = new RegExp(FV.stepsCanonicalPattern);
|
|
47
|
-
const FIELD_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
48
|
-
const TOKEN_RE = new RegExp(`\\{\\{|\\}\\}|${FV.placeholderPattern}`, 'g');
|
|
49
|
-
|
|
50
21
|
function isRec(v: unknown): v is Rec {
|
|
51
22
|
return v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
52
23
|
}
|
|
53
24
|
|
|
54
|
-
// ----------------------------------------------------------------
|
|
55
|
-
|
|
56
|
-
let stepScopeDepth = 0;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* StepForm 编译某一步时包住 emitObject:只有在这个作用域里,`$steps.s<i>.<字段>`(编译器已把步骤名改写成序号)才被认作合法写法。
|
|
60
|
-
* 同步调用,begin / end 之间不会交错(编译第 2-3 步是同步的)。
|
|
61
|
-
*/
|
|
62
|
-
export function runInKitStepScope<T>(fn: () => T): T {
|
|
63
|
-
stepScopeDepth += 1;
|
|
64
|
-
try { return fn(); } finally { stepScopeDepth -= 1; }
|
|
65
|
-
}
|
|
66
|
-
export function inKitStepScope(): boolean {
|
|
67
|
-
return stepScopeDepth > 0;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** 规范步骤引用:`s<序号>`(StepForm 编译器把步骤名改写成它;各步已确认 model 的键) */
|
|
71
|
-
export function isCanonicalStepId(id: string): boolean {
|
|
72
|
-
return STEPS_CANON_RE.test(id);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// ---------------------------------------------------------------- 派生模板(闭集,非表达式引擎)
|
|
76
|
-
|
|
77
|
-
export interface KitDeriveIR {
|
|
78
|
-
template: string;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export interface KitTemplateFilter {
|
|
82
|
-
name: string;
|
|
83
|
-
arg?: string;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export interface KitTemplatePlaceholder {
|
|
87
|
-
/** 'field' = 同对象草稿字段;'steps' = 更早步骤已确认的值;'row' = {#each} 块内当前行的键(field 为 '@n' 时是行序号) */
|
|
88
|
-
kind: 'field' | 'steps' | 'row';
|
|
89
|
-
/** steps 的步骤标识(编译前是步骤名,编译后是 s<序号>) */
|
|
90
|
-
step: string;
|
|
91
|
-
field: string;
|
|
92
|
-
filters: KitTemplateFilter[];
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** {#each 字段}…{/each} 块(H2b):field = 草稿里的行表字段,body 是块体(只含文字与 row 占位符) */
|
|
96
|
-
export interface KitTemplateEach {
|
|
97
|
-
kind: 'each';
|
|
98
|
-
field: string;
|
|
99
|
-
body: Array<string | KitTemplatePlaceholder>;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export type KitTemplatePart = string | KitTemplatePlaceholder | KitTemplateEach;
|
|
103
|
-
|
|
104
|
-
export type KitTemplateScan = { ok: true; parts: KitTemplatePart[] } | { ok: false; message: string };
|
|
105
|
-
|
|
106
|
-
function parsePlaceholder(body: string, inRow = false): KitTemplatePlaceholder | string {
|
|
107
|
-
const segs = body.split('|');
|
|
108
|
-
const head = (segs[0] ?? '').trim();
|
|
109
|
-
if (head.startsWith('#each') || head === '/each') return '{#each 字段名}…{/each} 必须成对出现,且不能嵌套';
|
|
110
|
-
const filters: KitTemplateFilter[] = [];
|
|
111
|
-
for (const raw of segs.slice(1)) {
|
|
112
|
-
const colon = raw.indexOf(':');
|
|
113
|
-
const name = (colon < 0 ? raw : raw.slice(0, colon)).trim();
|
|
114
|
-
const arg = colon < 0 ? undefined : raw.slice(colon + 1);
|
|
115
|
-
if (!FV.filters.includes(name)) return `过滤器 "${name}" 不在闭集 ${FV.filters.join(' / ')} 内`;
|
|
116
|
-
const wantsArg = FV.filterArgs[name] !== undefined;
|
|
117
|
-
if (wantsArg && (arg === undefined || arg === '')) return `过滤器 ${name} 需要参数(写成 ${name}:文本)`;
|
|
118
|
-
if (!wantsArg && arg !== undefined) return `过滤器 ${name} 不带参数`;
|
|
119
|
-
if (name === 'or' && arg !== undefined && arg.length > LIM.orMax) return `or 的兜底文本最多 ${LIM.orMax} 个字`;
|
|
120
|
-
filters.push(arg === undefined ? { name } : { name, arg });
|
|
121
|
-
}
|
|
122
|
-
if (filters.length > LIM.filterMax) return `一个占位符最多 ${LIM.filterMax} 个过滤器`;
|
|
123
|
-
if (inRow) {
|
|
124
|
-
if (head === FV.rowIndexToken) return { kind: 'row', step: '', field: FV.rowIndexToken, filters };
|
|
125
|
-
const key = head.startsWith(FV.rowPlaceholderPrefix) ? head.slice(FV.rowPlaceholderPrefix.length) : '';
|
|
126
|
-
if (!FIELD_RE.test(key)) return `{#each} 块内只许 {${FV.rowPlaceholderPrefix}<键>} 与 {${FV.rowIndexToken}},不是 {${head}}`;
|
|
127
|
-
return { kind: 'row', step: '', field: key, filters };
|
|
128
|
-
}
|
|
129
|
-
if (head.startsWith(FV.rowPlaceholderPrefix) || head === FV.rowIndexToken) return `{${head}} 只能写在 {#each 字段名}…{/each} 块里`;
|
|
130
|
-
if (head.startsWith(FV.stepsPlaceholderPrefix)) {
|
|
131
|
-
const rest = head.slice(FV.stepsPlaceholderPrefix.length).split('.');
|
|
132
|
-
if (rest.length !== 2 || !FIELD_RE.test(rest[0]!) || !FIELD_RE.test(rest[1]!)) return `占位符 {${head}} 应写成 {steps.<步骤名>.<字段>}`;
|
|
133
|
-
return { kind: 'steps', step: rest[0]!, field: rest[1]!, filters };
|
|
134
|
-
}
|
|
135
|
-
if (!FIELD_RE.test(head)) return `占位符 {${head}} 不是字段名(只许字母、数字、下划线,且不以数字开头)`;
|
|
136
|
-
return { kind: 'field', step: '', field: head, filters };
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* 扫描模板:{{ 与 }} 是字面花括号;单个 { 或 } 不成对 / 占位符写法不对返回 ok:false。
|
|
141
|
-
* {#each 字段}…{/each}(H2b)块先切出来,块体按 row 占位符规则扫,块外按普通规则扫;块数不超限。
|
|
142
|
-
*/
|
|
143
|
-
export function scanTemplate(template: string): KitTemplateScan {
|
|
144
|
-
const parts: KitTemplatePart[] = [];
|
|
145
|
-
let last = 0;
|
|
146
|
-
let blocks = 0;
|
|
147
|
-
for (const m of template.matchAll(EACH_RE)) {
|
|
148
|
-
const idx = m.index ?? 0;
|
|
149
|
-
const head = scanPlain(template.slice(last, idx), false);
|
|
150
|
-
if (!head.ok) return head;
|
|
151
|
-
const body = scanPlain(m[2] ?? '', true);
|
|
152
|
-
if (!body.ok) return body;
|
|
153
|
-
parts.push(...head.parts, { kind: 'each', field: m[1] ?? '', body: body.parts as KitTemplateEach['body'] });
|
|
154
|
-
last = idx + m[0].length;
|
|
155
|
-
blocks += 1;
|
|
156
|
-
}
|
|
157
|
-
if (blocks > LIM.eachMax) return { ok: false, message: `一个模板最多 ${LIM.eachMax} 个 {#each} 块` };
|
|
158
|
-
const tail = scanPlain(template.slice(last), false);
|
|
159
|
-
if (!tail.ok) return tail;
|
|
160
|
-
parts.push(...tail.parts);
|
|
161
|
-
return { ok: true, parts };
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function scanPlain(template: string, inRow: boolean): KitTemplateScan {
|
|
165
|
-
const parts: KitTemplatePart[] = [];
|
|
166
|
-
let last = 0;
|
|
167
|
-
const stray = (s: string): boolean => /[{}]/.test(s);
|
|
168
|
-
for (const m of template.matchAll(TOKEN_RE)) {
|
|
169
|
-
const idx = m.index ?? 0;
|
|
170
|
-
const lit = template.slice(last, idx);
|
|
171
|
-
if (stray(lit)) return { ok: false, message: '花括号不成对(字面花括号请写 {{ 或 }})' };
|
|
172
|
-
if (lit !== '') parts.push(lit);
|
|
173
|
-
if (m[0] === '{{') parts.push('{');
|
|
174
|
-
else if (m[0] === '}}') parts.push('}');
|
|
175
|
-
else {
|
|
176
|
-
const ph = parsePlaceholder(m[1] ?? '', inRow);
|
|
177
|
-
if (typeof ph === 'string') return { ok: false, message: ph };
|
|
178
|
-
parts.push(ph);
|
|
179
|
-
}
|
|
180
|
-
last = idx + m[0].length;
|
|
181
|
-
}
|
|
182
|
-
const tail = template.slice(last);
|
|
183
|
-
if (stray(tail)) return { ok: false, message: '花括号不成对(字面花括号请写 {{ 或 }})' };
|
|
184
|
-
if (tail !== '') parts.push(tail);
|
|
185
|
-
return { ok: true, parts };
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const scanCache = new Map<string, KitTemplateScan>();
|
|
189
|
-
function scanCached(template: string): KitTemplateScan {
|
|
190
|
-
let hit = scanCache.get(template);
|
|
191
|
-
if (hit === undefined) {
|
|
192
|
-
hit = scanTemplate(template);
|
|
193
|
-
if (scanCache.size > 200) scanCache.clear();
|
|
194
|
-
scanCache.set(template, hit);
|
|
195
|
-
}
|
|
196
|
-
return hit;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/** 一个值的显示文字:空 -> '',数组逗号连接,对象转 JSON */
|
|
200
|
-
export function plainText(v: unknown): string {
|
|
201
|
-
if (v === undefined || v === null) return '';
|
|
202
|
-
if (typeof v === 'string') return v;
|
|
203
|
-
if (typeof v === 'number' || typeof v === 'boolean' || typeof v === 'bigint') return String(v);
|
|
204
|
-
if (Array.isArray(v)) return v.map(plainText).join(', ');
|
|
205
|
-
try { return JSON.stringify(v) ?? ''; } catch { return ''; }
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function applyFilter(cur: unknown, f: KitTemplateFilter): unknown {
|
|
209
|
-
switch (f.name) {
|
|
210
|
-
case 'json': { try { return JSON.stringify(cur) ?? ''; } catch { return ''; } }
|
|
211
|
-
case 'upper': return plainText(cur).toUpperCase();
|
|
212
|
-
case 'lower': return plainText(cur).toLowerCase();
|
|
213
|
-
case 'trim': return plainText(cur).trim();
|
|
214
|
-
case 'count':
|
|
215
|
-
if (Array.isArray(cur)) return String(cur.length);
|
|
216
|
-
if (isRec(cur)) return String(Object.keys(cur).length);
|
|
217
|
-
return String(plainText(cur).length);
|
|
218
|
-
case 'or': return plainText(cur) === '' ? (f.arg ?? '') : cur;
|
|
219
|
-
default: return cur;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
export interface KitDeriveSources {
|
|
224
|
-
/** 本表单当前草稿 */
|
|
225
|
-
draft: Readonly<Rec>;
|
|
226
|
-
/** 各步已确认的 model { s<i>: { 字段: 值 } }(StepForm 以外为空) */
|
|
227
|
-
steps?: Readonly<Rec>;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/** 渲染派生文字(模板不合法时返回 '',编译期已报 KIT-FORM-011) */
|
|
231
|
-
export function renderDerive(ir: KitDeriveIR | undefined, src: KitDeriveSources): string {
|
|
232
|
-
if (ir === undefined) return '';
|
|
233
|
-
const scan = scanCached(ir.template);
|
|
234
|
-
if (!scan.ok) return '';
|
|
235
|
-
return renderParts(scan.parts, src, null);
|
|
236
|
-
}
|
|
25
|
+
// ---------------------------------------------------------------- 字段初值
|
|
237
26
|
|
|
238
|
-
/**
|
|
239
|
-
function
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
const rows = src.draft[part.field];
|
|
246
|
-
if (!Array.isArray(rows)) continue;
|
|
247
|
-
for (let i = 0; i < Math.min(rows.length, LIM.eachRowsMax); i += 1) out += renderParts(part.body, src, { data: rows[i], n: i + 1 });
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
let v: unknown = part.kind === 'row'
|
|
251
|
-
? (part.field === FV.rowIndexToken ? row?.n : isRec(row?.data) ? (row!.data as Rec)[part.field] : undefined)
|
|
252
|
-
: part.kind === 'steps'
|
|
253
|
-
? (isRec(src.steps?.[part.step]) ? (src.steps![part.step] as Rec)[part.field] : undefined)
|
|
254
|
-
: src.draft[part.field];
|
|
255
|
-
for (const f of part.filters) v = applyFilter(v, f);
|
|
256
|
-
out += plainText(v);
|
|
27
|
+
/** 取值路径形态:字符串以 $ 开头($filter / $row / $steps / $draft …)。深入数组与对象找,返回第一处;没有返回 undefined */
|
|
28
|
+
export function dollarPathOf(v: unknown): string | undefined {
|
|
29
|
+
if (typeof v === 'string') return v.startsWith('$') ? v : undefined;
|
|
30
|
+
const items = Array.isArray(v) ? v : isRec(v) ? Object.values(v) : [];
|
|
31
|
+
for (const x of items) {
|
|
32
|
+
const hit = dollarPathOf(x);
|
|
33
|
+
if (hit !== undefined) return hit;
|
|
257
34
|
}
|
|
258
|
-
return
|
|
35
|
+
return undefined;
|
|
259
36
|
}
|
|
260
37
|
|
|
261
|
-
/**
|
|
262
|
-
export function deriveOf(raw: unknown, fieldName: string, field: KitFieldSchema, schema: KitSchema, path: string, diags: KitDiagnostic[]): KitDeriveIR | null {
|
|
263
|
-
const bad = (message: string, at = path): null => { diags.push({ code: 'KIT-FORM-011', path: at, message }); return null; };
|
|
264
|
-
if (!isRec(raw)) return bad('derive 须是对象 { template }');
|
|
265
|
-
const extra = Object.keys(raw).filter((k) => !FV.deriveKeys.includes(k));
|
|
266
|
-
if (extra.length > 0) return bad(`derive 的键 ${extra.join(' / ')} 不在闭集 ${FV.deriveKeys.join(' / ')} 内`);
|
|
267
|
-
if (typeof raw.template !== 'string' || raw.template.trim() === '') return bad('derive.template 必须是非空文本', `${path}.template`);
|
|
268
|
-
if (raw.template.length > LIM.templateMax) return bad(`derive.template 最长 ${LIM.templateMax} 个字`, `${path}.template`);
|
|
269
|
-
if (field.permission !== undefined && field.permission !== 'r') return bad(`字段 ${fieldName} 写了 derive,permission 必须是 r(缺省即 r;派生值只读展示、不进 model)`, `${path}`);
|
|
270
|
-
if (field.hidden === true) return bad(`字段 ${fieldName} 不能同时写 hidden 与 derive(派生字段的用途就是显示)`, `${path}`);
|
|
271
|
-
const scan = scanTemplate(raw.template);
|
|
272
|
-
if (!scan.ok) return bad(scan.message, `${path}.template`);
|
|
273
|
-
const placeholders = scan.parts.filter((p): p is KitTemplatePlaceholder | KitTemplateEach => typeof p !== 'string');
|
|
274
|
-
if (placeholders.length === 0) return bad('derive.template 至少要有一个 {字段名} 占位符(没有占位符就不需要派生)', `${path}.template`);
|
|
275
|
-
const innerCount = placeholders.reduce((n, p) => n + (p.kind === 'each' ? p.body.filter((b) => typeof b !== 'string').length : 0), 0);
|
|
276
|
-
if (placeholders.length + innerCount > LIM.placeholderMax) return bad(`derive.template 最多 ${LIM.placeholderMax} 个占位符(含 {#each} 块与块内的占位符)`, `${path}.template`);
|
|
277
|
-
for (const ph of placeholders) {
|
|
278
|
-
if (ph.kind === 'each') {
|
|
279
|
-
// H2b:遍历的行表须是本对象另一个已声明、未写 derive 的字段
|
|
280
|
-
const rows = schema[ph.field];
|
|
281
|
-
if (rows === undefined) return bad(`{#each ${ph.field}} 引用了 schema 中不存在的字段`, `${path}.template`);
|
|
282
|
-
if (ph.field === fieldName) return bad('{#each} 不能遍历字段自己', `${path}.template`);
|
|
283
|
-
if (rows.derive !== undefined) return bad(`{#each ${ph.field}} 遍历的字段本身也是 derive(派生不能嵌套)`, `${path}.template`);
|
|
284
|
-
continue;
|
|
285
|
-
}
|
|
286
|
-
if (ph.kind === 'steps') {
|
|
287
|
-
if (!inKitStepScope() || !isCanonicalStepId(ph.step)) {
|
|
288
|
-
diags.push({ code: 'KIT-STEP-005', path: `${path}.template`, message: `占位符 {steps.${ph.step}.${ph.field}} 只能用在 StepForm 的步骤里,且指向更早的步骤及其字段` });
|
|
289
|
-
return null;
|
|
290
|
-
}
|
|
291
|
-
continue;
|
|
292
|
-
}
|
|
293
|
-
const target = schema[ph.field];
|
|
294
|
-
if (target === undefined) return bad(`占位符 {${ph.field}} 引用了 schema 中不存在的字段`, `${path}.template`);
|
|
295
|
-
if (ph.field === fieldName) return bad('占位符不能引用字段自己', `${path}.template`);
|
|
296
|
-
if (target.derive !== undefined) return bad(`占位符 {${ph.field}} 引用的字段本身也是 derive(派生不能嵌套)`, `${path}.template`);
|
|
297
|
-
}
|
|
298
|
-
return { template: raw.template };
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// ---------------------------------------------------------------- 字段初值
|
|
302
|
-
|
|
303
|
-
/** default 声明规整:字面量原样返回(深拷贝成 JSON 值),$ 开头按取值路径校验;写法不对记 KIT-FORM-010 / KIT-STEP-005 并返回 undefined */
|
|
38
|
+
/** default 声明规整:只许字面量(深拷贝成 JSON 值);写法不对记 KIT-FORM-010 并返回 undefined */
|
|
304
39
|
export function defaultOf(raw: unknown, fieldName: string, path: string, diags: KitDiagnostic[]): { value: unknown } | undefined {
|
|
305
|
-
const bad = (message: string
|
|
306
|
-
if (typeof raw === 'string' && raw.startsWith('$')) {
|
|
307
|
-
if (STEPS_REF_RE.test(raw)) {
|
|
308
|
-
const step = raw.split('.')[1] ?? '';
|
|
309
|
-
if (!inKitStepScope() || !isCanonicalStepId(step)) return bad(`字段 ${fieldName} 的 default ${raw} 只能用在 StepForm 的步骤里,且指向更早的步骤及其字段`, 'KIT-STEP-005');
|
|
310
|
-
return { value: raw };
|
|
311
|
-
}
|
|
312
|
-
if (!REF_RE.test(raw)) {
|
|
313
|
-
return bad(`字段 ${fieldName} 的 default "${raw}" 不是合法取值路径(可用:${FV.defaultRefRoots.map((r) => `$${r}.<字段>`).join(' / ')};$steps 写成 $steps.<步骤名>.<字段>)`);
|
|
314
|
-
}
|
|
315
|
-
return { value: raw };
|
|
316
|
-
}
|
|
40
|
+
const bad = (message: string): undefined => { diags.push({ code: 'KIT-FORM-010', path, message }); return undefined; };
|
|
317
41
|
if (raw === undefined) return bad(`字段 ${fieldName} 的 default 不能是 undefined`);
|
|
42
|
+
const dollar = dollarPathOf(raw);
|
|
43
|
+
if (dollar !== undefined) return bad(`字段 ${fieldName} 的 default 必须是字面量,不接受以 $ 开头的取值路径(${dollar})`);
|
|
318
44
|
let text: string;
|
|
319
45
|
try { text = JSON.stringify(raw) ?? ''; } catch { return bad(`字段 ${fieldName} 的 default 不是可序列化的字面量`); }
|
|
320
46
|
if (text === '') return bad(`字段 ${fieldName} 的 default 不是可序列化的字面量`);
|
|
@@ -322,83 +48,28 @@ export function defaultOf(raw: unknown, fieldName: string, path: string, diags:
|
|
|
322
48
|
return { value: JSON.parse(text) as unknown };
|
|
323
49
|
}
|
|
324
50
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function walkPath(root: unknown, path: readonly string[]): unknown {
|
|
332
|
-
let cur = root;
|
|
333
|
-
for (const k of path) {
|
|
334
|
-
if (!isRec(cur)) return undefined;
|
|
335
|
-
cur = cur[k];
|
|
336
|
-
}
|
|
337
|
-
return cur;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
/** 解析一个 default:字面量返回其拷贝;取值路径取不到返回 undefined(该字段不填) */
|
|
341
|
-
export function resolveDefaultValue(raw: unknown, src: KitDefaultSources): unknown {
|
|
342
|
-
if (typeof raw !== 'string' || !raw.startsWith('$')) {
|
|
343
|
-
if (raw === undefined) return undefined;
|
|
344
|
-
try { return JSON.parse(JSON.stringify(raw)) as unknown; } catch { return undefined; }
|
|
345
|
-
}
|
|
346
|
-
const [root = '', ...rest] = raw.slice(1).split('.');
|
|
347
|
-
const v = root === 'filter' ? walkPath(src.filter, rest)
|
|
348
|
-
: root === 'row' ? walkPath(src.row, rest)
|
|
349
|
-
: root === 'steps' ? walkPath(src.steps, rest)
|
|
350
|
-
: undefined;
|
|
351
|
-
if (v === undefined || v === null) return undefined;
|
|
352
|
-
try { return JSON.parse(JSON.stringify(v)) as unknown; } catch { return undefined; }
|
|
51
|
+
/** 解析一个 default:字面量返回其拷贝(不可序列化返回 undefined,该字段不填) */
|
|
52
|
+
export function resolveDefaultValue(raw: unknown): unknown {
|
|
53
|
+
if (raw === undefined) return undefined;
|
|
54
|
+
try { return JSON.parse(JSON.stringify(raw)) as unknown; } catch { return undefined; }
|
|
353
55
|
}
|
|
354
56
|
|
|
355
57
|
/** 解析整张 defaults 表(取不到值的字段不在结果里) */
|
|
356
|
-
export function resolveDefaults(defaults: Readonly<Rec
|
|
58
|
+
export function resolveDefaults(defaults: Readonly<Rec>): Rec {
|
|
357
59
|
const out: Rec = {};
|
|
358
60
|
for (const [field, raw] of Object.entries(defaults)) {
|
|
359
|
-
const v = resolveDefaultValue(raw
|
|
61
|
+
const v = resolveDefaultValue(raw);
|
|
360
62
|
if (v !== undefined) out[field] = v;
|
|
361
63
|
}
|
|
362
64
|
return out;
|
|
363
65
|
}
|
|
364
66
|
|
|
365
|
-
// ---------------------------------------------------------------- 远程选项参数里的 { steps }
|
|
366
|
-
|
|
367
|
-
/** options.from.params 里的 { steps: 's0.字段' } 换成该步已确认的值;任一值为空 missing 为真(此时不发请求,同依赖字段为空) */
|
|
368
|
-
export function bindStepParams(params: Readonly<Rec>, steps: Readonly<Rec>): { params: Rec; missing: boolean } {
|
|
369
|
-
let missing = false;
|
|
370
|
-
const bind = (node: unknown): unknown => {
|
|
371
|
-
if (isRec(node)) {
|
|
372
|
-
const keys = Object.keys(node);
|
|
373
|
-
if (keys.length === 1 && keys[0] === 'steps' && typeof node.steps === 'string') {
|
|
374
|
-
const [id = '', f = ''] = node.steps.split('.');
|
|
375
|
-
const got = isRec(steps[id]) ? (steps[id] as Rec)[f] : undefined;
|
|
376
|
-
if (got === undefined || got === null || got === '') missing = true;
|
|
377
|
-
return got;
|
|
378
|
-
}
|
|
379
|
-
return Object.fromEntries(keys.map((k) => [k, bind(node[k])]));
|
|
380
|
-
}
|
|
381
|
-
return Array.isArray(node) ? node.map(bind) : node;
|
|
382
|
-
};
|
|
383
|
-
return { params: bind(params) as Rec, missing };
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
/** params 里有没有 { steps } 引用 */
|
|
387
|
-
export function hasStepParams(params: Readonly<Rec> | undefined): boolean {
|
|
388
|
-
if (params === undefined) return false;
|
|
389
|
-
const walk = (node: unknown): boolean => {
|
|
390
|
-
if (isRec(node)) return (Object.keys(node).length === 1 && typeof node.steps === 'string') || Object.values(node).some(walk);
|
|
391
|
-
return Array.isArray(node) ? node.some(walk) : false;
|
|
392
|
-
};
|
|
393
|
-
return walk(params);
|
|
394
|
-
}
|
|
395
|
-
|
|
396
67
|
// ---------------------------------------------------------------- RowEditor 列的远程选项(GB5)
|
|
397
68
|
|
|
398
69
|
const ROW_OPTIONS_KEYS = ['from', 'valueKey', 'labelKey', 'rowsPath', 'params'] as const;
|
|
399
70
|
|
|
400
71
|
/** 校验 RowEditor 的 columns:columns[].options 写成对象 { from, ... } 时的写法(KIT-FORM-012);静态选项数组不管 */
|
|
401
|
-
export function checkRowColumnOptions(columns: unknown, path: string, diags: KitDiagnostic[]
|
|
72
|
+
export function checkRowColumnOptions(columns: unknown, path: string, diags: KitDiagnostic[]): void {
|
|
402
73
|
if (!Array.isArray(columns)) return;
|
|
403
74
|
columns.forEach((col, i) => {
|
|
404
75
|
if (!isRec(col) || !isRec(col.options)) return;
|
|
@@ -415,27 +86,52 @@ export function checkRowColumnOptions(columns: unknown, path: string, diags: Kit
|
|
|
415
86
|
if (col.options.params !== undefined) {
|
|
416
87
|
if (!isRec(col.options.params)) { bad('options.params 必须是 { 参数名: 字面量 }'); return; }
|
|
417
88
|
for (const [pn, pv] of Object.entries(col.options.params)) {
|
|
418
|
-
if (
|
|
419
|
-
// { field: 表单字段 }(取当前表单草稿值,与画布节点字段同一套解析):只在表单字段里的子表有意义,须引用本对象另一个已声明字段
|
|
420
|
-
const dep = pv.field;
|
|
421
|
-
if (schema === undefined || Object.keys(pv).length !== 1 || typeof dep !== 'string' || schema[dep] === undefined || dep === self) {
|
|
422
|
-
bad(`options.params.${pn} 的 { field } 必须引用本对象另一个已声明的字段(不能是子表字段自己)`);
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
425
|
-
} else if (!(typeof pv === 'string' || typeof pv === 'number' || typeof pv === 'boolean')) { bad(`options.params.${pn} 必须是字面量或 { field }`); return; }
|
|
89
|
+
if (!(typeof pv === 'string' || typeof pv === 'number' || typeof pv === 'boolean')) { bad(`options.params.${pn} 必须是字面量`); return; }
|
|
426
90
|
}
|
|
427
91
|
}
|
|
428
92
|
});
|
|
429
93
|
}
|
|
430
94
|
|
|
431
|
-
/**
|
|
432
|
-
|
|
95
|
+
/** 规整单个字段项(字符串 / { name, props } / 单键 { 字段: { props } });分隔线与非法项返回 undefined */
|
|
96
|
+
function unwrapFieldItem(it: unknown): { name: string; props?: Rec } | undefined {
|
|
97
|
+
if (typeof it === 'string') return { name: it };
|
|
98
|
+
if (!isRec(it)) return undefined;
|
|
99
|
+
if ('divider' in it && it.name === undefined) return undefined;
|
|
100
|
+
if (typeof it.name === 'string') return { name: it.name, ...(isRec(it.props) ? { props: it.props } : {}) };
|
|
101
|
+
const keys = Object.keys(it);
|
|
102
|
+
if (keys.length === 1) {
|
|
103
|
+
const k = keys[0]!;
|
|
104
|
+
const v = it[k];
|
|
105
|
+
return { name: k, ...(isRec(v) && isRec(v.props) ? { props: v.props } : {}) };
|
|
106
|
+
}
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 收集视图 fields 与 groups[].fields 里的全部字段项及其诊断路径 */
|
|
111
|
+
function formFieldEntries(form: Rec, slot: string): Array<{ name: string; props?: Rec; at: string }> {
|
|
112
|
+
const out: Array<{ name: string; props?: Rec; at: string }> = [];
|
|
113
|
+
const push = (items: unknown, base: string): void => {
|
|
114
|
+
if (!Array.isArray(items)) return;
|
|
115
|
+
items.forEach((it, i) => {
|
|
116
|
+
const u = unwrapFieldItem(it);
|
|
117
|
+
if (u !== undefined) out.push({ ...u, at: `${base}[${i}]` });
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
push(form.fields, `view.${slot}.fields`);
|
|
121
|
+
if (Array.isArray(form.groups)) {
|
|
122
|
+
form.groups.forEach((g, gi) => {
|
|
123
|
+
if (isRec(g)) push(g.fields, `view.${slot}.groups[${gi}].fields`);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 对象所有视图里的 RowEditor 列(视图字段项 props.columns、groups[].fields[].props.columns 与写死形状件的 props.columns) */
|
|
130
|
+
export function checkViewRowColumns(view: Readonly<Record<string, unknown>> | undefined, diags: KitDiagnostic[]): void {
|
|
433
131
|
for (const [slot, form] of Object.entries(view ?? {})) {
|
|
434
132
|
if (!isRec(form)) continue;
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
if (isRec(it) && isRec(it.props)) checkRowColumnOptions(it.props.columns, `view.${slot}.fields[${i}].props.columns`, diags, schema, typeof it.name === 'string' ? it.name : '');
|
|
438
|
-
});
|
|
133
|
+
for (const entry of formFieldEntries(form, slot)) {
|
|
134
|
+
if (entry.props !== undefined) checkRowColumnOptions(entry.props.columns, `${entry.at}.props.columns`, diags);
|
|
439
135
|
}
|
|
440
136
|
if (isRec(form.props)) checkRowColumnOptions(form.props.columns, `view.${slot}.props.columns`, diags);
|
|
441
137
|
}
|
|
@@ -462,23 +158,21 @@ const FLOW_OPT_LIM = (KIT_FORM_LINKAGE_LIMITS as unknown as Json).flowOptions as
|
|
|
462
158
|
|
|
463
159
|
/**
|
|
464
160
|
* 校验表单字段(画布字段)props.nodeTypes.<类型>.fields[].options 的对象写法(KIT-FORM-012);静态选项数组不管。
|
|
465
|
-
* params 的 { field } 必须引用本对象另一个已声明的字段(不能是画布字段自己,否则每次改画布都会重取)。
|
|
466
161
|
*/
|
|
467
|
-
export function checkViewFlowNodeOptions(view: Readonly<Record<string, unknown>> | undefined,
|
|
162
|
+
export function checkViewFlowNodeOptions(view: Readonly<Record<string, unknown>> | undefined, diags: KitDiagnostic[]): void {
|
|
468
163
|
for (const [slot, form] of Object.entries(view ?? {})) {
|
|
469
|
-
if (!isRec(form)
|
|
470
|
-
|
|
471
|
-
if (!
|
|
472
|
-
const self = typeof item.name === 'string' ? item.name : '';
|
|
164
|
+
if (!isRec(form)) continue;
|
|
165
|
+
for (const entry of formFieldEntries(form, slot)) {
|
|
166
|
+
if (!entry.props || !isRec(entry.props.nodeTypes)) continue;
|
|
473
167
|
let count = 0;
|
|
474
|
-
for (const [typeName, type] of Object.entries(
|
|
168
|
+
for (const [typeName, type] of Object.entries(entry.props.nodeTypes)) {
|
|
475
169
|
if (!isRec(type) || !Array.isArray(type.fields)) continue;
|
|
476
170
|
type.fields.forEach((f, j) => {
|
|
477
171
|
// select 的远程选项在 options,mapping 的远程候选在 targets(同形对象)
|
|
478
172
|
const remote = !isRec(f) ? undefined : f.kind === 'mapping' ? f.targets : f.options;
|
|
479
173
|
if (!isRec(f) || !isRec(remote)) return;
|
|
480
174
|
count += 1;
|
|
481
|
-
const at =
|
|
175
|
+
const at = `${entry.at}.props.nodeTypes.${typeName}.fields[${j}].${f.kind === 'mapping' ? 'targets' : 'options'}`;
|
|
482
176
|
const bad = (message: string): void => { diags.push({ code: 'KIT-FORM-012', path: at, message }); };
|
|
483
177
|
const opts = remote;
|
|
484
178
|
if (f.kind === 'mapping' && opts.meta !== undefined) { bad('mapping 的远程候选 targets 不支持 meta'); return; }
|
|
@@ -497,16 +191,10 @@ export function checkViewFlowNodeOptions(view: Readonly<Record<string, unknown>>
|
|
|
497
191
|
if (v !== undefined && (typeof v !== 'string' || v === '')) { bad(`options.${k} 必须是非空字符串`); return; }
|
|
498
192
|
}
|
|
499
193
|
if (opts.params !== undefined) {
|
|
500
|
-
if (!isRec(opts.params)) { bad('options.params 必须是 { 参数名: 字面量
|
|
194
|
+
if (!isRec(opts.params)) { bad('options.params 必须是 { 参数名: 字面量 }'); return; }
|
|
501
195
|
for (const [pn, pv] of Object.entries(opts.params)) {
|
|
502
|
-
if (
|
|
503
|
-
|
|
504
|
-
if (Object.keys(pv).length !== 1 || typeof dep !== 'string' || schema[dep] === undefined || dep === self) {
|
|
505
|
-
bad(`options.params.${pn} 的 { field } 必须引用本对象另一个已声明的字段(不能是画布字段自己)`);
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
} else if (!(pv === null || typeof pv === 'string' || typeof pv === 'number' || typeof pv === 'boolean')) {
|
|
509
|
-
bad(`options.params.${pn} 必须是字面量或 { field }`);
|
|
196
|
+
if (!(pv === null || typeof pv === 'string' || typeof pv === 'number' || typeof pv === 'boolean')) {
|
|
197
|
+
bad(`options.params.${pn} 必须是字面量`);
|
|
510
198
|
return;
|
|
511
199
|
}
|
|
512
200
|
}
|
|
@@ -519,28 +207,31 @@ export function checkViewFlowNodeOptions(view: Readonly<Record<string, unknown>>
|
|
|
519
207
|
});
|
|
520
208
|
}
|
|
521
209
|
if (count > FLOW_OPT_LIM.slotsMax) {
|
|
522
|
-
diags.push({ code: 'KIT-FORM-012', path:
|
|
210
|
+
diags.push({ code: 'KIT-FORM-012', path: `${entry.at}.props.nodeTypes`, message: `一个画布字段的远程选项最多 ${FLOW_OPT_LIM.slotsMax} 个,现有 ${count} 个` });
|
|
523
211
|
}
|
|
524
|
-
}
|
|
212
|
+
}
|
|
525
213
|
}
|
|
526
214
|
}
|
|
527
215
|
|
|
528
|
-
// ----------------------------------------------------------------
|
|
216
|
+
// ---------------------------------------------------------------- 一次校验一个对象的两类声明
|
|
529
217
|
|
|
530
218
|
export interface KitFieldValueIR {
|
|
531
|
-
/** 字段 -> 字面量
|
|
219
|
+
/** 字段 -> 字面量 */
|
|
532
220
|
defaults: Record<string, unknown>;
|
|
533
221
|
/** 隐藏草稿字段(schema 书写序) */
|
|
534
222
|
hidden: string[];
|
|
535
223
|
}
|
|
536
224
|
|
|
537
|
-
function fieldNamesOf(items: unknown): string[] {
|
|
225
|
+
export function fieldNamesOf(items: unknown): string[] {
|
|
538
226
|
if (!Array.isArray(items)) return [];
|
|
539
|
-
return items.flatMap((it) =>
|
|
227
|
+
return items.flatMap((it) => {
|
|
228
|
+
const u = unwrapFieldItem(it);
|
|
229
|
+
return u !== undefined ? [u.name] : [];
|
|
230
|
+
});
|
|
540
231
|
}
|
|
541
232
|
|
|
542
233
|
/**
|
|
543
|
-
* 校验并收集 schema 里的 default / hidden
|
|
234
|
+
* 校验并收集 schema 里的 default / hidden(每个对象调一次)。
|
|
544
235
|
* @param formFields view.form.fields 的原始写法(hidden 字段不得出现);groups 里的字段名一并传入 groupFields
|
|
545
236
|
*/
|
|
546
237
|
export function collectFieldValues(
|
|
@@ -563,8 +254,6 @@ export function collectFieldValues(
|
|
|
563
254
|
} else if (field.hidden) {
|
|
564
255
|
if (field.permission !== 'w' && field.permission !== 'create') {
|
|
565
256
|
diags.push({ code: 'KIT-FORM-010', path: `${at}.hidden`, message: `隐藏字段 ${name} 必须是可提交字段(permission 为 w 或 create);只读字段隐藏没有意义,hidden 也不能用来绕过权限` });
|
|
566
|
-
} else if (field.derive !== undefined) {
|
|
567
|
-
diags.push({ code: 'KIT-FORM-010', path: `${at}.hidden`, message: `字段 ${name} 不能同时写 hidden 与 derive` });
|
|
568
257
|
} else if (listed.has(name)) {
|
|
569
258
|
diags.push({ code: 'KIT-FORM-010', path: `${at}.hidden`, message: `隐藏字段 ${name} 不能出现在 view.form.fields / groups 里(隐藏字段不画)` });
|
|
570
259
|
} else {
|
|
@@ -572,10 +261,518 @@ export function collectFieldValues(
|
|
|
572
261
|
}
|
|
573
262
|
}
|
|
574
263
|
}
|
|
575
|
-
if (field.derive !== undefined) deriveOf(field.derive, name, field, schema, `${at}.derive`, diags);
|
|
576
264
|
}
|
|
577
265
|
if (out.hidden.length > LIM.hiddenMax) {
|
|
578
266
|
diags.push({ code: 'KIT-FORM-010', path: 'schema', message: `隐藏字段最多 ${LIM.hiddenMax} 个,现有 ${out.hidden.length} 个` });
|
|
579
267
|
}
|
|
580
268
|
return out;
|
|
581
269
|
}
|
|
270
|
+
|
|
271
|
+
// ---------------------------------------------------------------- 字段 rules 形状与参数校验 + enum 选项与编辑控件兼容矩阵
|
|
272
|
+
|
|
273
|
+
const ALLOWED_RULE_KEYS: ReadonlySet<string> = new Set([
|
|
274
|
+
"required", "min", "max", "min_len", "max_len", "minLength", "maxLength", "pattern", "step", "message",
|
|
275
|
+
]);
|
|
276
|
+
const CONDITION_RULE_KEYS: readonly string[] = [
|
|
277
|
+
"required", "min", "max", "min_len", "max_len", "minLength", "maxLength", "pattern", "step",
|
|
278
|
+
];
|
|
279
|
+
|
|
280
|
+
/** 编译期严格校验 schema.<field>.rules 数组的每一项(避免注入时才被 core 拒收) */
|
|
281
|
+
export function checkFieldRules(
|
|
282
|
+
fieldName: string,
|
|
283
|
+
rulesRaw: unknown,
|
|
284
|
+
path: string,
|
|
285
|
+
diags: KitDiagnostic[],
|
|
286
|
+
code = "KIT-FORM-006",
|
|
287
|
+
): void {
|
|
288
|
+
if (rulesRaw === undefined) return;
|
|
289
|
+
if (!Array.isArray(rulesRaw)) {
|
|
290
|
+
diags.push({ code, path, message: `字段 ${fieldName} 的 rules 必须是数组` });
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
let overallMin: number | undefined;
|
|
294
|
+
let overallMax: number | undefined;
|
|
295
|
+
let overallMinLen: number | undefined;
|
|
296
|
+
let overallMaxLen: number | undefined;
|
|
297
|
+
|
|
298
|
+
rulesRaw.forEach((rule, i) => {
|
|
299
|
+
const at = `${path}[${i}]`;
|
|
300
|
+
if (!isRec(rule)) {
|
|
301
|
+
diags.push({ code, path: at, message: "校验规则必须是对象" });
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (typeof rule.message !== "string" || rule.message.trim() === "") {
|
|
305
|
+
diags.push({ code, path: at, message: `字段 ${fieldName} 的校验规则必须包含非空字符串 message` });
|
|
306
|
+
}
|
|
307
|
+
for (const k of Object.keys(rule)) {
|
|
308
|
+
if (!ALLOWED_RULE_KEYS.has(k)) {
|
|
309
|
+
diags.push({ code: "KIT-PAGE-001", path: `${at}.${k}`, message: `未知键 ${k}(${at} 允许:${[...ALLOWED_RULE_KEYS].join(" / ")})` });
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const hasCond = CONDITION_RULE_KEYS.some((k) => rule[k] !== undefined);
|
|
313
|
+
if (!hasCond) {
|
|
314
|
+
diags.push({ code, path: at, message: `字段 ${fieldName} 的校验规则至少要声明一种约束条件(required / min / max / min_len / max_len / minLength / maxLength / pattern / step)` });
|
|
315
|
+
}
|
|
316
|
+
if (rule.required !== undefined && typeof rule.required !== "boolean") {
|
|
317
|
+
diags.push({ code, path: at, message: "规则 required 必须是布尔值" });
|
|
318
|
+
}
|
|
319
|
+
let ruleMin: number | undefined;
|
|
320
|
+
let ruleMax: number | undefined;
|
|
321
|
+
if (rule.min !== undefined) {
|
|
322
|
+
if (typeof rule.min !== "number" || !Number.isFinite(rule.min)) {
|
|
323
|
+
diags.push({ code, path: at, message: "规则 min 必须是有限数字" });
|
|
324
|
+
} else {
|
|
325
|
+
ruleMin = rule.min;
|
|
326
|
+
overallMin = overallMin === undefined ? rule.min : Math.max(overallMin, rule.min);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
if (rule.max !== undefined) {
|
|
330
|
+
if (typeof rule.max !== "number" || !Number.isFinite(rule.max)) {
|
|
331
|
+
diags.push({ code, path: at, message: "规则 max 必须是有限数字" });
|
|
332
|
+
} else {
|
|
333
|
+
ruleMax = rule.max;
|
|
334
|
+
overallMax = overallMax === undefined ? rule.max : Math.min(overallMax, rule.max);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
if (ruleMin !== undefined && ruleMax !== undefined && ruleMin > ruleMax) {
|
|
338
|
+
diags.push({ code, path: at, message: `规则 min (${ruleMin}) 不能大于 max (${ruleMax})` });
|
|
339
|
+
} else if (overallMin !== undefined && overallMax !== undefined && overallMin > overallMax) {
|
|
340
|
+
diags.push({ code, path: at, message: `字段 ${fieldName} 的规则 min (${overallMin}) 不能大于 max (${overallMax})` });
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
let ruleMinLen: number | undefined;
|
|
344
|
+
let ruleMaxLen: number | undefined;
|
|
345
|
+
for (const k of ["min_len", "minLength"] as const) {
|
|
346
|
+
if (rule[k] !== undefined) {
|
|
347
|
+
const v = rule[k];
|
|
348
|
+
if (typeof v !== "number" || !Number.isInteger(v) || v < 0) {
|
|
349
|
+
diags.push({ code, path: at, message: `规则 ${k} 必须是非负整数` });
|
|
350
|
+
} else {
|
|
351
|
+
ruleMinLen = v;
|
|
352
|
+
overallMinLen = overallMinLen === undefined ? v : Math.max(overallMinLen, v);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
for (const k of ["max_len", "maxLength"] as const) {
|
|
357
|
+
if (rule[k] !== undefined) {
|
|
358
|
+
const v = rule[k];
|
|
359
|
+
if (typeof v !== "number" || !Number.isInteger(v) || v < 0) {
|
|
360
|
+
diags.push({ code, path: at, message: `规则 ${k} 必须是非负整数` });
|
|
361
|
+
} else {
|
|
362
|
+
ruleMaxLen = v;
|
|
363
|
+
overallMaxLen = overallMaxLen === undefined ? v : Math.min(overallMaxLen, v);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (ruleMinLen !== undefined && ruleMaxLen !== undefined && ruleMinLen > ruleMaxLen) {
|
|
368
|
+
diags.push({ code, path: at, message: `规则最小长度 (${ruleMinLen}) 不能大于最大长度 (${ruleMaxLen})` });
|
|
369
|
+
} else if (overallMinLen !== undefined && overallMaxLen !== undefined && overallMinLen > overallMaxLen) {
|
|
370
|
+
diags.push({ code, path: at, message: `字段 ${fieldName} 的规则最小长度 (${overallMinLen}) 不能大于最大长度 (${overallMaxLen})` });
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (rule.pattern !== undefined) {
|
|
374
|
+
if (typeof rule.pattern !== "string" || rule.pattern === "") {
|
|
375
|
+
diags.push({ code, path: at, message: "规则 pattern 必须是非空正则表达式字符串" });
|
|
376
|
+
} else {
|
|
377
|
+
try {
|
|
378
|
+
new RegExp(rule.pattern);
|
|
379
|
+
} catch (err) {
|
|
380
|
+
diags.push({ code, path: at, message: `规则 pattern 不是合法的正则表达式:${err instanceof Error ? err.message : String(err)}` });
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (rule.step !== undefined) {
|
|
385
|
+
if (typeof rule.step !== "number" || !Number.isFinite(rule.step) || rule.step <= 0) {
|
|
386
|
+
diags.push({ code, path: at, message: "规则 step 必须是大于 0 的有限数字" });
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** 字段类型 × 编辑控件兼容矩阵(不含 App* 自定义组件) */
|
|
393
|
+
export const KIT_FIELD_EDIT_COMPATIBILITY: Readonly<Record<string, readonly string[]>> = {
|
|
394
|
+
bool: ["Switch", "Checkbox", "Radio", "Segmented", "Swap"],
|
|
395
|
+
boolean: ["Switch", "Checkbox", "Radio", "Segmented", "Swap"],
|
|
396
|
+
int: ["NumberInput", "Number", "Input", "Range", "Rating", "QtyStepper", "Select", "Radio", "Segmented"],
|
|
397
|
+
decimal: ["NumberInput", "Number", "Input", "Range", "Rating", "QtyStepper"],
|
|
398
|
+
money: ["NumberInput", "Number", "Input", "Range"],
|
|
399
|
+
percent: ["NumberInput", "Number", "Input", "Range"],
|
|
400
|
+
date: ["DatePicker", "Date", "Input"],
|
|
401
|
+
datetime: ["DatePicker", "Date", "TimePicker", "Input"],
|
|
402
|
+
daterange: ["DatePicker", "Date", "DateRange", "Range", "Input"],
|
|
403
|
+
enum: ["Select", "Radio", "Segmented", "Combobox", "Cascader", "TreeSelect"],
|
|
404
|
+
select: ["Select", "Radio", "Segmented", "Combobox", "Cascader", "TreeSelect"],
|
|
405
|
+
enums: ["Select", "Checkbox", "Combobox", "Cascader", "TreeSelect", "TagInput", "Upload"],
|
|
406
|
+
text: [
|
|
407
|
+
"Input", "Textarea", "Select", "Radio", "Combobox", "Cascader", "TreeSelect",
|
|
408
|
+
"ColorPicker", "Mentions", "Otp", "TimePicker", "TagInput", "Upload",
|
|
409
|
+
"FlowEditor", "RowEditor", "Segmented",
|
|
410
|
+
],
|
|
411
|
+
longtext: ["Textarea", "Input", "Mentions", "FlowEditor", "RowEditor"],
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
function unwrapViewFieldRef(it: unknown): { name: string; component?: string; options?: unknown; props?: Rec } | undefined {
|
|
415
|
+
if (typeof it === "string") return { name: it };
|
|
416
|
+
if (!isRec(it)) return undefined;
|
|
417
|
+
if ("divider" in it && it.name === undefined) return undefined;
|
|
418
|
+
if (typeof it.name === "string") {
|
|
419
|
+
return {
|
|
420
|
+
name: it.name,
|
|
421
|
+
...(typeof it.component === "string" ? { component: it.component } : {}),
|
|
422
|
+
...(it.options !== undefined ? { options: it.options } : {}),
|
|
423
|
+
...(isRec(it.props) ? { props: it.props } : {}),
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
const keys = Object.keys(it);
|
|
427
|
+
if (keys.length === 1) {
|
|
428
|
+
const k = keys[0]!;
|
|
429
|
+
const v = it[k];
|
|
430
|
+
if (isRec(v)) {
|
|
431
|
+
return {
|
|
432
|
+
name: k,
|
|
433
|
+
...(typeof v.component === "string" ? { component: v.component } : {}),
|
|
434
|
+
...(v.options !== undefined ? { options: v.options } : {}),
|
|
435
|
+
...(isRec(v.props) ? { props: v.props } : {}),
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
return { name: k };
|
|
439
|
+
}
|
|
440
|
+
return undefined;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* 校验:
|
|
445
|
+
* 1) enum / select 字段必须声明非空静态 options、远程 options.from,或在视图字段项绑定动态 options: view.xxx;
|
|
446
|
+
* 2) 字段 type 与 edit(以及可编辑视图字段项 component)的兼容性矩阵。
|
|
447
|
+
*/
|
|
448
|
+
export function checkEnumOptionsAndEditCompatibility(
|
|
449
|
+
schema: Readonly<Record<string, unknown>>,
|
|
450
|
+
view: Readonly<Record<string, unknown>> | undefined,
|
|
451
|
+
basePath: string,
|
|
452
|
+
diags: KitDiagnostic[],
|
|
453
|
+
): void {
|
|
454
|
+
const dynamicOptionFields = new Set<string>();
|
|
455
|
+
const viewEditComponents: Array<{ name: string; component: string; slot: string; at: string }> = [];
|
|
456
|
+
|
|
457
|
+
if (isRec(view)) {
|
|
458
|
+
for (const slot of ["form", "filter", "list", "summary"]) {
|
|
459
|
+
const vf = view[slot];
|
|
460
|
+
if (!isRec(vf)) continue;
|
|
461
|
+
const scanItems = (items: unknown, prefix: string): void => {
|
|
462
|
+
if (!Array.isArray(items)) return;
|
|
463
|
+
items.forEach((it, idx) => {
|
|
464
|
+
const ref = unwrapViewFieldRef(it);
|
|
465
|
+
if (!ref) return;
|
|
466
|
+
if (typeof ref.options === "string" && ref.options.trim() !== "") {
|
|
467
|
+
dynamicOptionFields.add(ref.name);
|
|
468
|
+
} else if (ref.props && Array.isArray(ref.props.options) && ref.props.options.length > 0) {
|
|
469
|
+
dynamicOptionFields.add(ref.name);
|
|
470
|
+
}
|
|
471
|
+
if ((slot === "form" || slot === "filter") && ref.component !== undefined && ref.component !== "") {
|
|
472
|
+
viewEditComponents.push({ name: ref.name, component: ref.component, slot, at: `${prefix}[${idx}].component` });
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
};
|
|
476
|
+
const slotPrefix = basePath ? `${basePath}.view.${slot}` : `view.${slot}`;
|
|
477
|
+
scanItems(vf.fields, `${slotPrefix}.fields`);
|
|
478
|
+
if (Array.isArray(vf.groups)) {
|
|
479
|
+
vf.groups.forEach((g, gi) => {
|
|
480
|
+
if (isRec(g)) scanItems(g.fields, `${slotPrefix}.groups[${gi}].fields`);
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
for (const [name, rawField] of Object.entries(schema)) {
|
|
487
|
+
if (!isRec(rawField)) continue;
|
|
488
|
+
const fPath = basePath ? `${basePath}.schema.${name}` : `schema.${name}`;
|
|
489
|
+
const type = typeof rawField.type === "string" ? rawField.type : "";
|
|
490
|
+
|
|
491
|
+
// 1. enum / select 漏写选项检查
|
|
492
|
+
if (type === "enum" || type === "select") {
|
|
493
|
+
const hasStatic = Array.isArray(rawField.options) && rawField.options.length > 0;
|
|
494
|
+
const hasRemote = isRec(rawField.options) && typeof rawField.options.from === "string" && rawField.options.from.trim() !== "";
|
|
495
|
+
const hasDynamic = dynamicOptionFields.has(name);
|
|
496
|
+
if (!hasStatic && !hasRemote && !hasDynamic && (rawField.options === undefined || (Array.isArray(rawField.options) && rawField.options.length === 0))) {
|
|
497
|
+
diags.push({
|
|
498
|
+
code: "KIT-FORM-003",
|
|
499
|
+
path: `${fPath}.options`,
|
|
500
|
+
message: `枚举字段 ${name}(type: ${type})必须声明非空静态 options、远程 options.from,或在视图字段项绑定动态 options: view.<成员>`,
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// 2. schema.<field>.edit 与字段 type 兼容矩阵
|
|
506
|
+
const edit = typeof rawField.edit === "string" ? rawField.edit : undefined;
|
|
507
|
+
if (edit !== undefined && edit !== "" && !/^App[A-Z]/.test(edit) && edit !== "FlowEditor") {
|
|
508
|
+
const allowed = KIT_FIELD_EDIT_COMPATIBILITY[type];
|
|
509
|
+
if (allowed !== undefined && !allowed.includes(edit)) {
|
|
510
|
+
diags.push({
|
|
511
|
+
code: "KIT-FORM-012",
|
|
512
|
+
path: `${fPath}.edit`,
|
|
513
|
+
message: `字段 ${name} 的类型 ${type} 与编辑控件 ${edit} 不兼容(允许:${allowed.join(" / ")})`,
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// 3. 可编辑视图(form / filter)字段项显式指定 component 与字段 type 的兼容检查
|
|
520
|
+
for (const item of viewEditComponents) {
|
|
521
|
+
const rawField = schema[item.name];
|
|
522
|
+
if (!isRec(rawField)) continue;
|
|
523
|
+
const type = typeof rawField.type === "string" ? rawField.type : "";
|
|
524
|
+
const perm = rawField.permission;
|
|
525
|
+
const isEditableInSlot = item.slot === "filter" || perm === "w" || perm === "create";
|
|
526
|
+
if (!isEditableInSlot) continue;
|
|
527
|
+
if (/^App[A-Z]/.test(item.component) || item.component === "FlowEditor") continue;
|
|
528
|
+
const allowed = KIT_FIELD_EDIT_COMPATIBILITY[type];
|
|
529
|
+
if (allowed !== undefined && !allowed.includes(item.component)) {
|
|
530
|
+
diags.push({
|
|
531
|
+
code: "KIT-FORM-012",
|
|
532
|
+
path: item.at,
|
|
533
|
+
message: `字段 ${item.name}(type: ${type})在可编辑视图中与控件 ${item.component} 不兼容(允许:${allowed.join(" / ")})`,
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// ---------------------------------------------------------------- 字段值格式化与解析(money / percent / daterange)
|
|
540
|
+
|
|
541
|
+
export const CURRENCY_SYMBOLS: Readonly<Record<string, string>> = Object.freeze({
|
|
542
|
+
CNY: '¥',
|
|
543
|
+
RMB: '¥',
|
|
544
|
+
JPY: '¥',
|
|
545
|
+
USD: '$',
|
|
546
|
+
EUR: '€',
|
|
547
|
+
GBP: '£',
|
|
548
|
+
HKD: 'HK$',
|
|
549
|
+
KRW: '₩',
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
export interface KitFieldValueFormatOptions {
|
|
553
|
+
type?: string;
|
|
554
|
+
kind?: string;
|
|
555
|
+
unit?: string;
|
|
556
|
+
currency?: string;
|
|
557
|
+
format?: string;
|
|
558
|
+
decimals?: number;
|
|
559
|
+
options?: ReadonlyArray<{ value?: unknown; label?: unknown; tone?: string }>;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* 解析 money 字段/列的币种符号:
|
|
564
|
+
* 优先取显式配置的 `unit`(如 `'$'`、`'€'`)或 `currency`(如 `'USD' -> '$'`、`'EUR' -> '€'`、`'GBP' -> '£'`、`'JPY' -> '¥'`、`'CNY' -> '¥'`)
|
|
565
|
+
* 或 `format: 'currency:USD'`;未指定时默认回退 `'¥'`。
|
|
566
|
+
*/
|
|
567
|
+
export function resolveCurrencySymbol(
|
|
568
|
+
opts?: { unit?: string; currency?: string; format?: string } | string,
|
|
569
|
+
): string {
|
|
570
|
+
if (typeof opts === 'string') {
|
|
571
|
+
const raw = opts.trim();
|
|
572
|
+
if (raw === '') return '¥';
|
|
573
|
+
const code = raw.startsWith('currency:') ? raw.slice('currency:'.length).trim() : raw;
|
|
574
|
+
if (code === '' || code === 'currency') return '¥';
|
|
575
|
+
return CURRENCY_SYMBOLS[code.toUpperCase()] ?? code;
|
|
576
|
+
}
|
|
577
|
+
if (isRec(opts)) {
|
|
578
|
+
if (typeof opts.unit === 'string' && opts.unit.trim() !== '') {
|
|
579
|
+
const u = opts.unit.trim();
|
|
580
|
+
return CURRENCY_SYMBOLS[u.toUpperCase()] ?? u;
|
|
581
|
+
}
|
|
582
|
+
if (typeof opts.currency === 'string' && opts.currency.trim() !== '') {
|
|
583
|
+
const c = opts.currency.trim();
|
|
584
|
+
return CURRENCY_SYMBOLS[c.toUpperCase()] ?? c;
|
|
585
|
+
}
|
|
586
|
+
if (typeof opts.format === 'string' && opts.format.trim() !== '') {
|
|
587
|
+
const f = opts.format.trim();
|
|
588
|
+
if (f.startsWith('currency:')) {
|
|
589
|
+
const fc = f.slice('currency:'.length).trim();
|
|
590
|
+
if (fc !== '') return CURRENCY_SYMBOLS[fc.toUpperCase()] ?? fc;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return '¥';
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* 格式化 money 值:使用字段/列配置的 `unit` 或 `currency` 对应币种符号,未指定时默认回退 `'¥'`。
|
|
599
|
+
*/
|
|
600
|
+
export function formatMoneyValue(
|
|
601
|
+
value: unknown,
|
|
602
|
+
opts?: { unit?: string; currency?: string; format?: string; decimals?: number } | string,
|
|
603
|
+
): string {
|
|
604
|
+
if (value === null || value === undefined || value === '') return '';
|
|
605
|
+
const symbol = resolveCurrencySymbol(opts);
|
|
606
|
+
const decimals = isRec(opts) && typeof opts.decimals === 'number' && Number.isInteger(opts.decimals) && opts.decimals >= 0
|
|
607
|
+
? opts.decimals
|
|
608
|
+
: undefined;
|
|
609
|
+
if (typeof value === 'number') {
|
|
610
|
+
if (!Number.isFinite(value)) return '';
|
|
611
|
+
const numStr = decimals !== undefined ? value.toFixed(decimals) : String(Number(value.toFixed(4)));
|
|
612
|
+
return value < 0 ? `-${symbol}${numStr.slice(1)}` : `${symbol}${numStr}`;
|
|
613
|
+
}
|
|
614
|
+
const strVal = String(value).trim();
|
|
615
|
+
if (strVal === '') return '';
|
|
616
|
+
const cleaned = strVal.replace(/,/g, '');
|
|
617
|
+
const n = Number(cleaned);
|
|
618
|
+
if (Number.isFinite(n)) {
|
|
619
|
+
const numStr = decimals !== undefined ? n.toFixed(decimals) : String(Number(n.toFixed(4)));
|
|
620
|
+
return n < 0 ? `-${symbol}${numStr.slice(1)}` : `${symbol}${numStr}`;
|
|
621
|
+
}
|
|
622
|
+
return strVal.startsWith(symbol) ? strVal : `${symbol}${strVal}`;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* 格式化 percent 值:统一 `soma-kit` 内 `percent` 口径为 `0..100` 原值加 `'%'`(绝不把 0.5 或 1 自动乘 100)。
|
|
627
|
+
*/
|
|
628
|
+
export function formatPercentValue(
|
|
629
|
+
value: unknown,
|
|
630
|
+
opts?: { decimals?: number },
|
|
631
|
+
): string {
|
|
632
|
+
if (value === null || value === undefined || value === '') return '';
|
|
633
|
+
const decimals = isRec(opts) && typeof opts.decimals === 'number' && Number.isInteger(opts.decimals) && opts.decimals >= 0
|
|
634
|
+
? opts.decimals
|
|
635
|
+
: undefined;
|
|
636
|
+
if (typeof value === 'number') {
|
|
637
|
+
if (!Number.isFinite(value)) return '';
|
|
638
|
+
const numStr = decimals !== undefined ? value.toFixed(decimals) : String(Number(value.toFixed(4)));
|
|
639
|
+
return `${numStr}%`;
|
|
640
|
+
}
|
|
641
|
+
const strVal = String(value).trim();
|
|
642
|
+
if (strVal === '') return '';
|
|
643
|
+
if (strVal.endsWith('%')) return strVal;
|
|
644
|
+
const n = Number(strVal);
|
|
645
|
+
if (Number.isFinite(n)) {
|
|
646
|
+
const numStr = decimals !== undefined ? n.toFixed(decimals) : String(Number(n.toFixed(4)));
|
|
647
|
+
return `${numStr}%`;
|
|
648
|
+
}
|
|
649
|
+
return `${strVal}%`;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function normalizeDateToken(raw: unknown): string {
|
|
653
|
+
if (raw === null || raw === undefined) return '';
|
|
654
|
+
if (raw instanceof Date) {
|
|
655
|
+
return Number.isNaN(raw.getTime()) ? '' : raw.toISOString().slice(0, 10);
|
|
656
|
+
}
|
|
657
|
+
if (typeof raw === 'number') {
|
|
658
|
+
if (!Number.isFinite(raw)) return '';
|
|
659
|
+
const d = new Date(raw);
|
|
660
|
+
return Number.isNaN(d.getTime()) ? String(raw) : d.toISOString().slice(0, 10);
|
|
661
|
+
}
|
|
662
|
+
if (typeof raw === 'string') {
|
|
663
|
+
const s = raw.trim();
|
|
664
|
+
if (/^\d{4}-\d{2}-\d{2}T/.test(s)) return s.slice(0, 10);
|
|
665
|
+
return s;
|
|
666
|
+
}
|
|
667
|
+
return '';
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* 解析 daterange 值:兼容 `['2026-01-01', '2026-01-31']` 数组、`{ start: '2026-01-01', end: '2026-01-31' }` 对象与
|
|
672
|
+
* `'2026-01-01 ~ 2026-01-31'` 字符串,返回 `[start, end]` 二元组;空值返回 `null`。
|
|
673
|
+
*/
|
|
674
|
+
export function parseDateRangeValue(value: unknown): [string, string] | null {
|
|
675
|
+
if (value === null || value === undefined || value === '') return null;
|
|
676
|
+
if (Array.isArray(value)) {
|
|
677
|
+
const start = normalizeDateToken(value[0]);
|
|
678
|
+
const end = normalizeDateToken(value[1]);
|
|
679
|
+
if (start === '' && end === '') return null;
|
|
680
|
+
return [start, end];
|
|
681
|
+
}
|
|
682
|
+
if (isRec(value)) {
|
|
683
|
+
const start = normalizeDateToken(value.start ?? value.from ?? value.begin ?? value.startDate);
|
|
684
|
+
const end = normalizeDateToken(value.end ?? value.to ?? value.endDate);
|
|
685
|
+
if (start === '' && end === '') return null;
|
|
686
|
+
return [start, end];
|
|
687
|
+
}
|
|
688
|
+
if (typeof value === 'string') {
|
|
689
|
+
const s = value.trim();
|
|
690
|
+
if (s === '') return null;
|
|
691
|
+
if (s.includes('~')) {
|
|
692
|
+
const [a, b] = s.split(/\s*~\s*/, 2);
|
|
693
|
+
return [normalizeDateToken(a), normalizeDateToken(b)];
|
|
694
|
+
}
|
|
695
|
+
const rangeMatch = /^(\d{4}-\d{2}-\d{2})\s*(?:至|to|,|\s+-\s+)\s*(\d{4}-\d{2}-\d{2})$/i.exec(s);
|
|
696
|
+
if (rangeMatch) {
|
|
697
|
+
return [rangeMatch[1]!, rangeMatch[2]!];
|
|
698
|
+
}
|
|
699
|
+
return [normalizeDateToken(s), ''];
|
|
700
|
+
}
|
|
701
|
+
return null;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* 格式化 daterange 值:兼容数组、对象与字符串输入,统一格式化为 `'YYYY-MM-DD ~ YYYY-MM-DD'`。
|
|
706
|
+
*/
|
|
707
|
+
export function formatDateRangeValue(value: unknown): string {
|
|
708
|
+
const parsed = parseDateRangeValue(value);
|
|
709
|
+
if (parsed === null) return '';
|
|
710
|
+
const [start, end] = parsed;
|
|
711
|
+
if (start !== '' && end !== '') return `${start} ~ ${end}`;
|
|
712
|
+
return start !== '' ? start : end;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/**
|
|
716
|
+
* 统一格式化字段/单元格展示值:处理 `options` 枚举映射、`money`、`percent`、`daterange`。
|
|
717
|
+
*/
|
|
718
|
+
export function formatFieldValue(
|
|
719
|
+
value: unknown,
|
|
720
|
+
field?: KitFieldValueFormatOptions | string,
|
|
721
|
+
): string {
|
|
722
|
+
if (value === null || value === undefined || value === '') return '';
|
|
723
|
+
const opts: KitFieldValueFormatOptions = typeof field === 'string' ? { type: field, format: field } : (field ?? {});
|
|
724
|
+
if (Array.isArray(opts.options) && opts.options.length > 0) {
|
|
725
|
+
const hit = opts.options.find((o) => isRec(o) && (o.value === value || String(o.value) === String(value)));
|
|
726
|
+
if (hit !== undefined) {
|
|
727
|
+
return typeof hit.label === 'string' && hit.label !== '' ? hit.label : String(hit.value ?? value);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
const kind = (opts.type ?? opts.kind ?? '').trim();
|
|
731
|
+
const fmt = (opts.format ?? '').trim();
|
|
732
|
+
if (kind === 'money' || kind === 'currency' || fmt === 'currency' || fmt.startsWith('currency:')) {
|
|
733
|
+
return formatMoneyValue(value, opts);
|
|
734
|
+
}
|
|
735
|
+
if (kind === 'percent' || fmt === 'percent') {
|
|
736
|
+
return formatPercentValue(value, opts);
|
|
737
|
+
}
|
|
738
|
+
if (kind === 'daterange' || fmt === 'daterange') {
|
|
739
|
+
return formatDateRangeValue(value);
|
|
740
|
+
}
|
|
741
|
+
if (Array.isArray(value) && value.length === 2 && typeof value[0] === 'string' && /^\d{4}-\d{2}-\d{2}/.test(value[0])) {
|
|
742
|
+
return formatDateRangeValue(value);
|
|
743
|
+
}
|
|
744
|
+
if (isRec(value) && ('start' in value || 'end' in value || 'from' in value || 'to' in value)) {
|
|
745
|
+
return formatDateRangeValue(value);
|
|
746
|
+
}
|
|
747
|
+
return String(value);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* 字段类型 / 显式 format -> 展示格式化标识:
|
|
752
|
+
* daterange -> "daterange",money -> "currency",percent -> "percent",date -> "date",datetime -> "datetime"。
|
|
753
|
+
*/
|
|
754
|
+
export function displayFormatOf(type?: string, explicitFormat?: string): string {
|
|
755
|
+
if (typeof explicitFormat === 'string' && explicitFormat.trim() !== '') {
|
|
756
|
+
return explicitFormat.trim();
|
|
757
|
+
}
|
|
758
|
+
const t = typeof type === 'string' ? type.trim() : '';
|
|
759
|
+
switch (t) {
|
|
760
|
+
case 'daterange':
|
|
761
|
+
return 'daterange';
|
|
762
|
+
case 'money':
|
|
763
|
+
case 'currency':
|
|
764
|
+
return 'currency';
|
|
765
|
+
case 'percent':
|
|
766
|
+
return 'percent';
|
|
767
|
+
case 'date':
|
|
768
|
+
return 'date';
|
|
769
|
+
case 'datetime':
|
|
770
|
+
return 'datetime';
|
|
771
|
+
case 'int':
|
|
772
|
+
case 'decimal':
|
|
773
|
+
case 'number':
|
|
774
|
+
return 'number';
|
|
775
|
+
default:
|
|
776
|
+
return 'text';
|
|
777
|
+
}
|
|
778
|
+
}
|