soma-kit 0.1.11 → 0.1.13
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 +106 -112
- package/bin/soma-kit.mjs +32 -2
- package/bin/soma.mjs +22 -0
- package/package.json +14 -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 +929 -37
- package/spec/generated/kit-page.schema.json +1684 -161
- package/spec/kit-grammar.json +1176 -584
- package/spec/kit-grammar.schema.json +1971 -393
- package/src/app/app-config.ts +449 -347
- package/src/app/component-gate.ts +283 -28
- package/src/app/compose-apps.ts +79 -8
- package/src/app/define-kit-actions.ts +245 -41
- package/src/app/define-kit-app.ts +176 -33
- package/src/app/define-kit-view.ts +104 -0
- package/src/app/dev-diagnostic.ts +5 -0
- package/src/app/envelope.ts +1 -161
- package/src/app/index.ts +29 -6
- package/src/app/kit-view-context.ts +439 -0
- package/src/app/notify-renderer.ts +1 -115
- package/src/app/paged-list-adapter.ts +1 -240
- package/src/app/render-ports.ts +18 -0
- package/src/app/server-pages.ts +11 -3
- package/src/app/server.ts +530 -103
- 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 +667 -0
- package/src/base/action-busy.ts +103 -6
- package/src/base/action-gate.ts +4 -255
- package/src/base/action-slots.ts +58 -0
- package/src/base/alert.ts +5 -3
- package/src/base/anchored-layer.ts +50 -0
- package/src/base/avatar.ts +2 -2
- package/src/base/button.ts +99 -37
- package/src/base/cascader.ts +11 -12
- package/src/base/check-core.ts +9 -1
- package/src/base/checkbox.ts +2 -1
- package/src/base/choice.ts +49 -28
- package/src/base/class-candidates.ts +17 -0
- package/src/base/color-picker.ts +16 -11
- package/src/base/combobox.ts +5 -3
- package/src/base/command-palette.ts +10 -6
- package/src/base/context-menu.ts +10 -7
- package/src/base/countdown.ts +1 -1
- package/src/base/date-range.ts +32 -18
- package/src/base/date.ts +135 -28
- package/src/base/delta.ts +72 -0
- package/src/base/divider.ts +2 -2
- package/src/base/end-edge.ts +49 -0
- package/src/base/fab.ts +12 -5
- package/src/base/gap-value.ts +148 -0
- package/src/base/hint-tips.ts +170 -0
- package/src/base/icons.ts +8 -0
- package/src/base/illustrations.ts +94 -0
- package/src/base/image.ts +1 -1
- package/src/base/index.ts +35 -11
- 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 +56 -35
- package/src/base/loading-gate.ts +186 -0
- package/src/base/mentions.ts +7 -6
- package/src/base/menu-button.ts +28 -12
- package/src/base/menu-core.ts +41 -8
- package/src/base/motion/aura.ts +2 -2
- package/src/base/motion/hover-3d.ts +3 -3
- package/src/base/motion/hover-gallery.ts +2 -2
- package/src/base/motion/marquee.ts +3 -3
- package/src/base/motion/text-rotate.ts +2 -2
- package/src/base/number.ts +93 -80
- package/src/base/otp.ts +9 -5
- package/src/base/overlay-runtime.ts +235 -0
- package/src/base/permission-gate.ts +6 -0
- package/src/base/popover.ts +48 -16
- package/src/base/popup.ts +143 -19
- package/src/base/portal-core.ts +4 -0
- 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 +14 -8
- package/src/base/radio.ts +2 -1
- package/src/base/range.ts +21 -4
- package/src/base/rating.ts +12 -7
- package/src/base/reason-tip.ts +150 -0
- package/src/base/segmented.ts +27 -21
- package/src/base/select.ts +4 -3
- package/src/base/shared.ts +387 -49
- package/src/base/state-panel.ts +346 -0
- package/src/base/status-tone.ts +27 -0
- package/src/base/sticky-edge.ts +116 -0
- package/src/base/swap.ts +10 -7
- package/src/base/switch.ts +11 -6
- package/src/base/tag-input.ts +20 -16
- package/src/base/tag.ts +68 -34
- package/src/base/text.ts +80 -18
- package/src/base/textarea.ts +37 -11
- package/src/base/time-picker.ts +18 -13
- package/src/base/tree-select.ts +21 -12
- package/src/base/trend-tone.ts +9 -0
- package/src/base/upload.ts +48 -14
- package/src/base/view-transition.ts +77 -0
- package/src/base/virtual-list.ts +107 -1
- package/src/biz/approval-desk/compile.ts +23 -41
- package/src/biz/approval-desk/shell.ts +11 -8
- package/src/biz/auth-panel/compile.ts +37 -52
- package/src/biz/auth-panel/index.ts +1 -1
- package/src/biz/auth-panel/shell.ts +5 -4
- package/src/biz/category-crud/compile.ts +23 -37
- package/src/biz/category-crud/shell.ts +2 -1
- package/src/biz/checkout-page/compile.ts +22 -5
- package/src/biz/checkout-page/shell.ts +4 -3
- 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 +5 -4
- package/src/biz/index.ts +3 -3
- package/src/biz/landing-page/compile.ts +19 -3
- package/src/biz/link/hub.ts +69 -22
- package/src/biz/link/shared.ts +127 -119
- package/src/biz/master-sub/compile.ts +127 -48
- package/src/biz/master-sub/index.ts +5 -2
- package/src/biz/master-sub/shell.ts +95 -18
- package/src/biz/multi-board/compile.ts +38 -50
- package/src/biz/multi-board/index.ts +18 -1
- package/src/biz/multi-board/shell.ts +32 -20
- package/src/biz/notification-center/compile.ts +21 -33
- package/src/biz/notification-center/shell.ts +6 -4
- 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/result-page/shell.ts +2 -1
- package/src/biz/page-templates/shared.ts +34 -36
- package/src/biz/page-templates/sign-in-page/shell.ts +2 -2
- package/src/biz/record-detail/compile.ts +48 -43
- package/src/biz/record-detail/index.ts +11 -1
- package/src/biz/record-detail/shell.ts +8 -6
- package/src/biz/settings-center/compile.ts +7 -5
- package/src/biz/settings-center/shell.ts +6 -4
- package/src/biz/shared/action-host.ts +97 -211
- package/src/biz/shared/compile-helpers.ts +98 -0
- package/src/biz/shared/coordinator.ts +472 -92
- package/src/biz/shared/index.ts +13 -2
- package/src/biz/shared/object-aliases.ts +154 -0
- package/src/biz/shared/overlay-compile.ts +239 -0
- package/src/biz/shared/overlay-shell.ts +209 -0
- package/src/biz/shared/pipes.ts +50 -31
- package/src/biz/shared/remove-wire.ts +14 -0
- package/src/biz/shared/runtime-hub.ts +226 -24
- package/src/biz/single-form/compile.ts +16 -37
- package/src/biz/single-form/shell.ts +4 -3
- package/src/biz/standard-crud/compile.ts +72 -255
- package/src/biz/standard-crud/shell.ts +21 -99
- package/src/biz/step-form/compile.ts +26 -97
- package/src/biz/step-form/hub.ts +34 -41
- package/src/biz/step-form/shell.ts +5 -5
- package/src/biz/types.ts +4 -3
- package/src/cli/core-imports.ts +54 -0
- package/src/cli/kit-cli.ts +254 -25
- package/src/cli/kit-init.ts +227 -0
- package/src/cli/layout-cli.ts +167 -0
- package/src/cli/migrate/index.ts +10 -0
- package/src/cli/migrate/migrate.ts +248 -0
- package/src/cli/migrate/text-utils.ts +306 -0
- package/src/cli/migrate/theme-map.ts +104 -0
- package/src/cli/migrate/ts-rules.ts +235 -0
- package/src/cli/migrate/types.ts +95 -0
- package/src/cli/migrate/view-skeleton.ts +159 -0
- package/src/cli/migrate/yaml-report.ts +376 -0
- package/src/cli/migrate/yaml-rules.ts +465 -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 +761 -0
- package/src/compile/action-host-attach.ts +21 -29
- package/src/compile/closed-set.ts +570 -0
- package/src/compile/compile-page.ts +267 -76
- package/src/compile/component-candidates.ts +13 -2
- 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/layout-lint/engine.ts +82 -0
- package/src/compile/layout-lint/index.ts +55 -0
- package/src/compile/layout-lint/rules-composition.ts +218 -0
- package/src/compile/layout-lint/rules-form.ts +192 -0
- package/src/compile/layout-lint/rules-table.ts +194 -0
- package/src/compile/layout-lint/score.ts +52 -0
- package/src/compile/layout-lint/types.ts +51 -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/remove-guard.ts +10 -7
- package/src/compile/styles.ts +63 -2
- package/src/compile/types.ts +31 -3
- package/src/compile/view-check.ts +284 -0
- package/src/data/amount-breakdown.ts +8 -3
- package/src/data/anchor-nav.ts +2 -2
- package/src/data/approval-flow.ts +3 -3
- package/src/data/assignment-picker.ts +92 -55
- package/src/data/async-result-panel.ts +111 -9
- package/src/data/breadcrumbs.ts +5 -4
- package/src/data/calendar.ts +24 -27
- package/src/data/carousel.ts +13 -8
- package/src/data/chart.ts +183 -44
- package/src/data/checklist.ts +20 -7
- package/src/data/code-view.ts +12 -6
- package/src/data/comment-thread.ts +7 -4
- package/src/data/confirm-dialog.ts +81 -18
- 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 +3 -3
- package/src/data/embed.ts +233 -30
- package/src/data/field-mapper.ts +35 -30
- package/src/data/flow-editor-contract.ts +8 -1
- package/src/data/flow-editor.ts +113 -108
- package/src/data/flow-node-options.ts +120 -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 +19 -19
- package/src/data/hero.ts +4 -3
- package/src/data/image-viewer.ts +27 -30
- package/src/data/index.ts +5 -2
- package/src/data/item-list.ts +48 -31
- package/src/data/kanban.ts +82 -52
- package/src/data/media-grid.ts +20 -20
- package/src/data/mega-menu.ts +6 -4
- package/src/data/menu-dom.ts +4 -2
- package/src/data/menu.ts +47 -15
- package/src/data/mockup.ts +10 -6
- package/src/data/modal-dialog.ts +49 -25
- package/src/data/navbar.ts +12 -7
- package/src/data/notice-list.ts +268 -0
- package/src/data/notify-renderer.ts +250 -0
- package/src/data/object-form.ts +1360 -666
- package/src/data/outcome-watch.ts +61 -2
- package/src/data/pager.ts +80 -34
- package/src/data/permission-matrix.ts +52 -31
- package/src/data/permission-tree.ts +71 -44
- package/src/data/pricing-table.ts +30 -14
- package/src/data/prompt-dialog.ts +124 -194
- package/src/data/reaction-bar.ts +83 -92
- package/src/data/read-rollback.ts +73 -0
- package/src/data/record-card.ts +26 -5
- package/src/data/record-picker.ts +104 -55
- 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 +8 -4
- package/src/data/rich-text.ts +61 -55
- package/src/data/row-editor.ts +362 -76
- package/src/data/rule-builder.ts +26 -20
- package/src/data/shape-util.ts +27 -9
- package/src/data/stat-group.ts +392 -109
- package/src/data/static-lazy.ts +1 -0
- package/src/data/static-registry.ts +24 -11
- package/src/data/steps.ts +10 -5
- package/src/data/survey.ts +27 -32
- package/src/data/system-switcher.ts +3 -3
- package/src/data/text-action-button.ts +17 -0
- package/src/data/timeline.ts +19 -8
- package/src/data/tour-guide.ts +23 -30
- package/src/data/transfer.ts +62 -38
- package/src/data/tree-table.ts +152 -78
- package/src/data/tree.ts +49 -36
- package/src/data/trend-line.ts +6 -24
- package/src/data/virtual-rows.ts +133 -82
- package/src/data/watermark.ts +2 -2
- package/src/data/wrap.ts +63 -38
- package/src/data/wrapped-candidates.ts +10 -0
- package/src/frames/blank-frame.ts +1 -0
- package/src/frames/index.ts +2 -2
- package/src/frames/page-bar.ts +26 -24
- package/src/frames/page-header.ts +14 -5
- package/src/frames/page-status.ts +9 -7
- package/src/frames/shared.ts +36 -25
- package/src/frames/single-frame.ts +1 -0
- package/src/frames/split-frame/shell.ts +6 -6
- package/src/frames/split-frame/wiring.ts +2 -2
- package/src/frames/tabs-frame/shell.ts +4 -14
- package/src/frames/tabs-frame/wiring.ts +5 -8
- package/src/frames/top-main-frame.ts +3 -2
- package/src/frames/types.ts +13 -2
- package/src/frames/workspace-frame/extras.ts +21 -5
- package/src/frames/workspace-frame/index.ts +1 -1
- package/src/frames/workspace-frame/shell.ts +3 -2
- package/src/frames/workspace-frame/state.ts +13 -6
- package/src/frames/workspace-frame/view.ts +250 -48
- package/src/generated/kit-grammar-runtime.ts +645 -397
- package/src/generated/page-origin.ts +0 -11
- package/src/i18n/kit-text.ts +84 -29
- package/src/i18n/resolve-text-refs.ts +22 -6
- package/src/index.ts +2 -1
- package/src/object-renderer/action-gate.ts +11 -147
- package/src/object-renderer/arrange.ts +112 -84
- 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 +401 -104
- package/src/object-renderer/collection-table.ts +287 -45
- package/src/object-renderer/collection.ts +258 -104
- package/src/object-renderer/compile-data.ts +112 -102
- package/src/object-renderer/crud-bridge.ts +32 -0
- package/src/object-renderer/defaults.ts +38 -16
- package/src/object-renderer/display-checks.ts +4 -25
- package/src/object-renderer/emit-events.ts +224 -176
- package/src/object-renderer/emit-object.ts +15 -2
- package/src/object-renderer/emit-static.ts +533 -151
- 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 +111 -2
- package/src/object-renderer/form-notify.ts +45 -0
- package/src/object-renderer/index.ts +26 -0
- package/src/object-renderer/key-field.ts +12 -0
- package/src/object-renderer/layout-facts.ts +386 -0
- package/src/object-renderer/layout-plan.ts +132 -0
- package/src/object-renderer/layout-plan.types.ts +22 -0
- package/src/object-renderer/linkage.ts +7 -270
- package/src/object-renderer/merge-class.ts +18 -1
- package/src/object-renderer/parts.ts +252 -1
- package/src/object-renderer/path-pick.ts +27 -0
- package/src/object-renderer/render-object.ts +456 -89
- package/src/object-renderer/request-actions.ts +232 -249
- package/src/object-renderer/skeleton.ts +58 -19
- package/src/object-renderer/table-actions.ts +319 -111
- package/src/object-renderer/table-summary.ts +2 -2
- package/src/object-renderer/table-tools.ts +429 -94
- package/src/object-renderer/types.ts +204 -127
- package/src/object-renderer/view-binding.ts +393 -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 +165 -28
- package/src/page-target/target.ts +262 -51
- package/src/page-target/writer-policy.ts +14 -3
- package/src/recipes/container.ts +210 -0
- package/src/recipes/control.ts +277 -0
- package/src/recipes/index.ts +312 -0
- package/src/recipes/nowrap.ts +16 -0
- package/src/recipes/overlay.ts +240 -0
- package/src/recipes/placeholder.ts +79 -0
- package/src/recipes/rail-grid.ts +45 -0
- package/src/recipes/slot-align.ts +56 -0
- package/src/recipes/state.ts +99 -0
- package/src/recipes/status.ts +139 -0
- package/src/recipes/surface.ts +40 -0
- package/src/recipes/table-row.ts +88 -0
- package/src/recipes/title-row.ts +37 -0
- package/src/recipes/typography.ts +99 -0
- package/src/shared/action-field-guard.ts +41 -0
- package/src/shared/confirm-decl.ts +118 -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 +15 -0
- package/src/shared/path-guard.ts +127 -0
- package/src/shared/theme-runtime-shared.ts +180 -0
- package/src/shared/view-info.ts +10 -0
- package/src/theme/index.ts +13 -0
- package/src/theme/kit-theme-text.ts +1 -1
- package/src/theme/kit-theme.css +407 -10
- package/src/theme/kit-theme.tpl +407 -10
- package/src/theme/nine-dim-structure.ts +247 -0
- package/src/theme/surface-layers.ts +101 -0
- package/src/theme/theme-tokens.ts +130 -84
- package/src/theme/theme-types.ts +103 -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/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 -1473
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// 动作 confirm 的声明形:`confirm: "文案"`(plain,逐字不变)或 `confirm: { level, text, phrase?, impact? }`。
|
|
2
|
+
//
|
|
3
|
+
// 判据只有 core 的 checkConfirmSpec(键闭集 level / text / phrase / impact / tone、level 取值、各级必写 / 禁写的键、长度上限);
|
|
4
|
+
// 占位文法与代入只有 core 的 confirm-spec(`{字段}` / `{n}`,触发时按载荷 row / rows 代入,见 soma-core 的 substituteConfirmText)。
|
|
5
|
+
// kit 不再抄一份闭集或限值,只在它前面加 kit 层的几条规矩(本文件是它们的唯一校验处):
|
|
6
|
+
// · 占位按动作所在处限定(J3):有行的动作(行动作、删除)可写 `{字段}`(取被操作行的字段,名字必须是标识符);
|
|
7
|
+
// 批量动作可写 `{n}`(已选个数);没有行的动作(工具栏 / 字段动作)与批量动作不许写 `{字段}`;`{n}` 只给批量动作;
|
|
8
|
+
// 字面花括号写 `{{` / `}}`;
|
|
9
|
+
// · 不许路径形(row. / view. / page. / $ / @ 开头):占位是字段替换,不是路径;
|
|
10
|
+
// · 管道尾段不支持转义:值里不许含 "|";
|
|
11
|
+
// · 文案以 `{` / `[` 开头时(如「{name} 将被删除」)core 会把整段当 JSON 读,所以编成 `confirm:{"level":"plain","text":…}`(confirmSegmentOfText)。
|
|
12
|
+
// 编成与字符串形同一个 core 尾段:`confirm:{"level":…,"text":…}`(core 的 parseConfirmSegment 解析回同一规格)。
|
|
13
|
+
// 对象形只给普通动作;builtin: remove 与 batchActions 仍只收字符串(批量走批量条自带的两步确认,见 table-tools)。
|
|
14
|
+
|
|
15
|
+
import { checkConfirmSpec, listConfirmPlaceholders, SOMA_CONFIRM_COUNT_NAME, type SomaConfirmSpec } from 'soma-core';
|
|
16
|
+
|
|
17
|
+
/** 对象形的三个文本键(其余键交给 core 判) */
|
|
18
|
+
const TEXT_KEYS = ['text', 'phrase', 'impact'] as const;
|
|
19
|
+
/** 路径形 / 取值形的字符串:不是字面量 */
|
|
20
|
+
const NON_LITERAL = /^(?:[$@])|^(?:row|view|page|ctrl|data|state)\.[A-Za-z_]/;
|
|
21
|
+
|
|
22
|
+
/** 动作所在处:有没有可取字段的行 / 是不是批量动作(缺省 = 不限定,只做文法与路径形检查,供尚不知所在处的闭集检查用) */
|
|
23
|
+
export interface KitConfirmScope {
|
|
24
|
+
readonly row: boolean;
|
|
25
|
+
readonly batch: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 行动作(含删除):可写 `{字段}` */
|
|
29
|
+
export const KIT_CONFIRM_ROW_SCOPE: KitConfirmScope = { row: true, batch: false };
|
|
30
|
+
/** 批量动作:只可写 `{n}` */
|
|
31
|
+
export const KIT_CONFIRM_BATCH_SCOPE: KitConfirmScope = { row: false, batch: true };
|
|
32
|
+
/** 没有行的动作(工具栏 / 字段动作):不许写占位 */
|
|
33
|
+
export const KIT_CONFIRM_BARE_SCOPE: KitConfirmScope = { row: false, batch: false };
|
|
34
|
+
|
|
35
|
+
/** 按动作的 on 取所在处(row = 行动作;其余 = 没有行) */
|
|
36
|
+
export function confirmScopeOfAction(on: unknown): KitConfirmScope {
|
|
37
|
+
return on === 'row' ? KIT_CONFIRM_ROW_SCOPE : KIT_CONFIRM_BARE_SCOPE;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isRec(v: unknown): v is Record<string, unknown> {
|
|
41
|
+
return v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 是不是对象形(含数组外的任何对象;数组 / null 不是) */
|
|
45
|
+
export function isConfirmObject(raw: unknown): raw is Record<string, unknown> {
|
|
46
|
+
return isRec(raw);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 一段文案里的占位在该所在处是否合法;合法返回 null,否则一句话问题(不含键路径) */
|
|
50
|
+
export function confirmPlaceholderProblem(text: string, scope: KitConfirmScope | undefined): string | null {
|
|
51
|
+
if (scope === undefined) return null;
|
|
52
|
+
const names = listConfirmPlaceholders(text);
|
|
53
|
+
if (names.length === 0) return null;
|
|
54
|
+
const isCount = (n: string): boolean => n === SOMA_CONFIRM_COUNT_NAME;
|
|
55
|
+
if (scope.batch) {
|
|
56
|
+
const field = names.find((n) => !isCount(n));
|
|
57
|
+
return field === undefined ? null : `批量动作的确认文案只能用 {${SOMA_CONFIRM_COUNT_NAME}}(已选个数),不能用 {${field}}(批量没有单一行)`;
|
|
58
|
+
}
|
|
59
|
+
if (scope.row) {
|
|
60
|
+
return names.some(isCount) ? `{${SOMA_CONFIRM_COUNT_NAME}} 是批量动作的已选个数占位,行动作要取字段请换一个字段名` : null;
|
|
61
|
+
}
|
|
62
|
+
return `这里没有行也不是批量动作,不能写占位 {${names[0]!}}(行动作可写 {字段},批量动作可写 {${SOMA_CONFIRM_COUNT_NAME}};字面花括号写 {{ }})`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 字符串形 confirm 的 kit 层检查:占位所在处、"|";合法返回 null */
|
|
66
|
+
export function checkKitConfirmText(text: string, scope?: KitConfirmScope): string | null {
|
|
67
|
+
if (text.includes('|')) return '文案含 "|",管道尾段不支持转义';
|
|
68
|
+
return confirmPlaceholderProblem(text, scope);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 字符串形 confirm 的管道尾段(含 `confirm:` 前缀):绝大多数文案逐字写;以 `{` / `[` 开头的文案
|
|
73
|
+
* 会被 core 当成结构化 JSON,所以改写成等价的 plain 结构,避免「{name} 将被删除」被误读。
|
|
74
|
+
*/
|
|
75
|
+
export function confirmSegmentOfText(text: string, tone?: 'danger'): string {
|
|
76
|
+
const head = text.trim().charAt(0);
|
|
77
|
+
// 破坏性动作带 core 确认规格的 tone: danger(确认钮警示色、初始焦点落取消);规格形必须是结构化段
|
|
78
|
+
if (tone === 'danger') return `confirm:${JSON.stringify({ level: 'plain', text, tone })}`;
|
|
79
|
+
if (head === '{' || head === '[') return `confirm:${JSON.stringify({ level: 'plain', text })}`;
|
|
80
|
+
return `confirm:${text}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 动作的确认语气(唯一判据):删除(builtin: remove)与自身 tone 为 danger 的动作 = 破坏性,确认编成 `tone: danger`;
|
|
85
|
+
* 其余不带语气。对象形里作者显式写的 tone 优先(见 checkKitConfirmObject)。
|
|
86
|
+
*/
|
|
87
|
+
export function confirmToneOfAction(action: { readonly builtin?: unknown; readonly tone?: unknown }): 'danger' | undefined {
|
|
88
|
+
return action.builtin === 'remove' || action.tone === 'danger' ? 'danger' : undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type KitConfirmCheck =
|
|
92
|
+
| { readonly ok: true; readonly spec: SomaConfirmSpec; /** 管道尾段(含 `confirm:` 前缀) */ readonly segment: string }
|
|
93
|
+
| { readonly ok: false; readonly error: string };
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 校验并编译对象形 confirm;非法返回 error(一句话,调用方挂到 `<动作>.confirm` 路径上记 KIT-ACT-010)。
|
|
97
|
+
* `scope` 缺省 = 不限定占位所在处(闭集检查阶段还不知道动作落在哪);编事件的地方必须传。
|
|
98
|
+
*/
|
|
99
|
+
export function checkKitConfirmObject(raw: unknown, scope?: KitConfirmScope, tone?: 'danger'): KitConfirmCheck {
|
|
100
|
+
if (!isRec(raw)) return { ok: false, error: 'confirm 对象形必须是 { level, text, phrase?, impact?, tone? }' };
|
|
101
|
+
for (const key of TEXT_KEYS) {
|
|
102
|
+
const v = raw[key];
|
|
103
|
+
if (typeof v !== 'string') continue;
|
|
104
|
+
if (v.includes('|')) return { ok: false, error: `confirm.${key} 含 "|",管道尾段不支持转义` };
|
|
105
|
+
if (NON_LITERAL.test(v.trim())) return { ok: false, error: `confirm.${key} 是路径 / 取值形写法(${JSON.stringify(v)}):占位只写 {字段名},不写路径` };
|
|
106
|
+
// phrase 是要用户键入的比对串,永远是字面量:core 不对它代入,所以不许写占位
|
|
107
|
+
if (key === 'phrase') {
|
|
108
|
+
if (listConfirmPlaceholders(v).length > 0) return { ok: false, error: 'confirm.phrase 是要用户键入的比对串,只能是字面量(不代入占位)' };
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const problem = confirmPlaceholderProblem(v, scope);
|
|
112
|
+
if (problem !== null) return { ok: false, error: `confirm.${key}:${problem}` };
|
|
113
|
+
}
|
|
114
|
+
// 动作本身是破坏性的(tone: danger / 删除)而作者没写 tone:补 danger;作者显式写的(含 default)优先
|
|
115
|
+
const checked = checkConfirmSpec(tone !== undefined && raw.tone === undefined ? { ...raw, tone } : raw);
|
|
116
|
+
if (!checked.ok) return { ok: false, error: checked.error };
|
|
117
|
+
return { ok: true, spec: checked.spec, segment: `confirm:${JSON.stringify(checked.spec)}` };
|
|
118
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// 业务信封约定:应用在 kit.app.yaml 声明一次(页面 page.envelope 可覆写),kit 里所有读响应的地方读这一份,
|
|
2
|
+
// 不再各写各的「success:false / ret≠0」。
|
|
3
|
+
//
|
|
4
|
+
// envelope:
|
|
5
|
+
// failWhen: { field: success, op: eq, value: false } # 闭集谓词(复用 core 的 {field, op, value} 与 all_of / any_of),命中 = 业务失败
|
|
6
|
+
// messagePath: message # 失败时业务文案在响应里的点路径
|
|
7
|
+
// dataPath: data # 成功时数据在响应里的点路径(缺省不拆)
|
|
8
|
+
//
|
|
9
|
+
// 编译时:失败判定编进 core 的 `failWhen / messagePath`(写通道与取数来源),业务失败 -> 写入结果 failed。
|
|
10
|
+
// 运行时:详情预取、分页适配等 kit 自己读响应的地方用本文件的纯函数。
|
|
11
|
+
|
|
12
|
+
import { evaluatePredicate, validatePredicate, collectPredicateFields } from 'soma-core/predicates';
|
|
13
|
+
import { load as loadYaml } from 'js-yaml';
|
|
14
|
+
import type { KitDiagnostic } from '../object-renderer/types';
|
|
15
|
+
import { kitFail } from './kit-error';
|
|
16
|
+
|
|
17
|
+
type Rec = Record<string, unknown>;
|
|
18
|
+
const isRec = (v: unknown): v is Rec => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
19
|
+
const PATH = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$/;
|
|
20
|
+
const ENVELOPE_KEYS = ['failWhen', 'messagePath', 'dataPath'] as const;
|
|
21
|
+
|
|
22
|
+
/** 规整后的信封约定(可序列化,作为 prop / 编译产物的一部分) */
|
|
23
|
+
export interface KitEnvelope {
|
|
24
|
+
failWhen?: Rec;
|
|
25
|
+
messagePath?: string;
|
|
26
|
+
dataPath?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 校验并规整;非法记 KIT-APP-001(应用级)或 KIT-PAGE-004(页面级) */
|
|
30
|
+
export function parseKitEnvelope(raw: unknown, at: string, code: 'KIT-APP-001' | 'KIT-PAGE-004', diagnostics: KitDiagnostic[]): KitEnvelope | undefined {
|
|
31
|
+
if (raw === undefined || raw === null) return undefined;
|
|
32
|
+
const bad = (message: string, path = at): undefined => {
|
|
33
|
+
diagnostics.push({ code, path, message });
|
|
34
|
+
return undefined;
|
|
35
|
+
};
|
|
36
|
+
if (!isRec(raw)) return bad('envelope 必须是 { failWhen?, messagePath?, dataPath? }');
|
|
37
|
+
const extra = Object.keys(raw).filter((k) => !(ENVELOPE_KEYS as readonly string[]).includes(k));
|
|
38
|
+
if (extra.length > 0) return bad(`envelope 不认键 ${extra.join(' / ')}(可用:${ENVELOPE_KEYS.join(' / ')})`);
|
|
39
|
+
const out: KitEnvelope = {};
|
|
40
|
+
if (raw.failWhen !== undefined) {
|
|
41
|
+
const problems = validatePredicate(raw.failWhen, collectPredicateFields(raw.failWhen as never), `${at}.failWhen`, {});
|
|
42
|
+
if (!isRec(raw.failWhen) || problems.length > 0) return bad(`envelope.failWhen 不是合法的闭集谓词:${problems.join(';') || '必须是对象'}`, `${at}.failWhen`);
|
|
43
|
+
out.failWhen = raw.failWhen;
|
|
44
|
+
}
|
|
45
|
+
for (const key of ['messagePath', 'dataPath'] as const) {
|
|
46
|
+
const v = raw[key];
|
|
47
|
+
if (v === undefined) continue;
|
|
48
|
+
if (typeof v !== 'string' || !PATH.test(v)) return bad(`envelope.${key} 必须是点路径(如 data.record)`, `${at}.${key}`);
|
|
49
|
+
out[key] = v;
|
|
50
|
+
}
|
|
51
|
+
if (out.messagePath !== undefined && out.failWhen === undefined) return bad('envelope.messagePath 必须与 failWhen 一起声明', `${at}.messagePath`);
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function pathGet(root: unknown, path: string): unknown {
|
|
56
|
+
let cur = root;
|
|
57
|
+
for (const key of path.split('.')) {
|
|
58
|
+
if (!isRec(cur)) return undefined;
|
|
59
|
+
cur = cur[key];
|
|
60
|
+
}
|
|
61
|
+
return cur;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 响应命中信封失败判定(没有声明 failWhen 一律不判失败) */
|
|
65
|
+
export function isEnvelopeFailure(raw: unknown, env?: KitEnvelope): raw is Rec {
|
|
66
|
+
if (env?.failWhen === undefined || !isRec(raw)) return false;
|
|
67
|
+
return evaluatePredicate(env.failWhen as never, raw);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 失败信封里的业务文案(messagePath 取到的非空字符串,取不到为空串) */
|
|
71
|
+
export function envelopeMessage(raw: unknown, env: KitEnvelope | undefined): string {
|
|
72
|
+
if (env?.messagePath === undefined) return '';
|
|
73
|
+
const v = pathGet(raw, env.messagePath);
|
|
74
|
+
return typeof v === 'string' ? v.trim() : '';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type UnwrappedEnvelope = { ok: true; data: unknown } | { ok: false; error: Rec };
|
|
78
|
+
|
|
79
|
+
/** 拆信封:命中失败 -> { ok:false, error: 原信封 };否则按 dataPath 取数据(没声明 dataPath 不拆) */
|
|
80
|
+
export function unwrapEnvelope(raw: unknown, env?: KitEnvelope): UnwrappedEnvelope {
|
|
81
|
+
if (isEnvelopeFailure(raw, env)) return { ok: false, error: raw };
|
|
82
|
+
if (env?.dataPath === undefined) return { ok: true, data: raw };
|
|
83
|
+
return { ok: true, data: pathGet(raw, env.dataPath) };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ---------------------------------------------------------------- 当前信封(编译期 / 服务端缺省)
|
|
87
|
+
|
|
88
|
+
let compileEnvelope: KitEnvelope | undefined;
|
|
89
|
+
/** 页面编译期间的有效信封(compile-page 在编译开始设置、结束清掉;编译第 2-3 步是同步的) */
|
|
90
|
+
export function setCompileEnvelope(env: KitEnvelope | undefined): void {
|
|
91
|
+
compileEnvelope = env;
|
|
92
|
+
}
|
|
93
|
+
export function getCompileEnvelope(): KitEnvelope | undefined {
|
|
94
|
+
return compileEnvelope;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 信封按作用域持有,不存模块级可变单例:
|
|
98
|
+
// · 服务端:属于应用实例(KitAppRegistry.appEnvelope,装配时写入该应用自己的持有者),读取点经装配对象 / 显式参数拿到;
|
|
99
|
+
// · 浏览器:属于页面宿主——页面级动作宿主 attach 时登记「宿主元素 -> 信封」,卸载即注销;读取点按触发元素向上找所属宿主。
|
|
100
|
+
// 同进程 / 同文档多个应用、多个页面宿主的信封互不覆盖;找不到唯一归属时返回 undefined,不猜「最后写入者」。
|
|
101
|
+
|
|
102
|
+
interface HostEnvelopeEntry { readonly host: Element; readonly env: KitEnvelope }
|
|
103
|
+
/** 页面级动作宿主登记表(登记顺序;卸载时精确移除自身那一条) */
|
|
104
|
+
const hostEnvelopes: HostEnvelopeEntry[] = [];
|
|
105
|
+
/** 应用级缺省信封登记表(浏览器引导按 kit.app.yaml 登记,供拿不到宿主的读取点兜底) */
|
|
106
|
+
const appDefaultEnvelopes: Array<{ readonly env: KitEnvelope }> = [];
|
|
107
|
+
|
|
108
|
+
/** 页面级动作宿主 attach 时登记本宿主的有效信封;返回注销函数(宿主卸载时调用) */
|
|
109
|
+
export function registerKitHostEnvelope(host: Element, env: KitEnvelope): () => void {
|
|
110
|
+
const entry: HostEnvelopeEntry = { host, env };
|
|
111
|
+
hostEnvelopes.push(entry);
|
|
112
|
+
return () => {
|
|
113
|
+
const i = hostEnvelopes.indexOf(entry);
|
|
114
|
+
if (i >= 0) hostEnvelopes.splice(i, 1);
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 浏览器引导登记应用级缺省信封(只给拿不到宿主的读取点兜底);返回注销函数 */
|
|
119
|
+
export function registerKitAppDefaultEnvelope(env: KitEnvelope): () => void {
|
|
120
|
+
const entry = { env };
|
|
121
|
+
appDefaultEnvelopes.push(entry);
|
|
122
|
+
return () => {
|
|
123
|
+
const i = appDefaultEnvelopes.indexOf(entry);
|
|
124
|
+
if (i >= 0) appDefaultEnvelopes.splice(i, 1);
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 按页面宿主取信封:从 `from`(触发元素)向上,最近一层祖先里包含的已登记宿主即所属页面(同层多个取最近登记的);
|
|
130
|
+
* 找不到时,只有恰好一个宿主登记才用它(单页常态);多个且无法归属返回 undefined,不猜。
|
|
131
|
+
*/
|
|
132
|
+
export function kitHostEnvelopeAt(from?: Element | null): KitEnvelope | undefined {
|
|
133
|
+
if (from !== undefined && from !== null) {
|
|
134
|
+
for (let cur: Element | null = from; cur !== null; cur = cur.parentElement) {
|
|
135
|
+
for (let i = hostEnvelopes.length - 1; i >= 0; i -= 1) {
|
|
136
|
+
if (cur.contains(hostEnvelopes[i]!.host)) return hostEnvelopes[i]!.env;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return hostEnvelopes.length === 1 ? hostEnvelopes[0]!.env : undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 浏览器读取点取信封:页面宿主(见 kitHostEnvelopeAt)优先,其次唯一一份应用级缺省;无归属为 undefined。
|
|
145
|
+
* 服务端没有宿主登记,恒为 undefined——服务端读取点必须显式传 envelope(装配对象 appContext.envelope / registry.appEnvelope)。
|
|
146
|
+
*/
|
|
147
|
+
export function getKitAppEnvelope(from?: Element | null): KitEnvelope | undefined {
|
|
148
|
+
return kitHostEnvelopeAt(from) ?? (appDefaultEnvelopes.length === 1 ? appDefaultEnvelopes[0]!.env : undefined);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 解析 kit.app.yaml 原文里的应用级信封(纯函数,不登记任何状态;构建期原样引入的同一个文件,不是镜像),只取 `envelope` 段。
|
|
153
|
+
* 没写 envelope 返回 undefined;yaml 或 envelope 不合法直接抛错,引导失败比静默读空信封好查。
|
|
154
|
+
*/
|
|
155
|
+
export function parseKitAppEnvelopeFromYaml(appYamlText: string): KitEnvelope | undefined {
|
|
156
|
+
let doc: unknown;
|
|
157
|
+
try {
|
|
158
|
+
doc = loadYaml(appYamlText);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
kitFail('KIT-APP-001', 'kit.app.yaml', `kit.app.yaml 解析失败:${(error as Error).message}`, '检查 YAML 缩进与引号;含冒号、逗号、# 的值要加引号');
|
|
161
|
+
}
|
|
162
|
+
if (!isRec(doc) || doc.envelope === undefined) return undefined;
|
|
163
|
+
const diagnostics: KitDiagnostic[] = [];
|
|
164
|
+
const env = parseKitEnvelope(doc.envelope, 'kit.app.yaml.envelope', 'KIT-APP-001', diagnostics);
|
|
165
|
+
if (diagnostics.length > 0 || env === undefined) {
|
|
166
|
+
kitFail('KIT-APP-001', 'kit.app.yaml.envelope', `kit.app.yaml 的 envelope 不合法:${diagnostics.map((d) => d.message).join(';')}`, '对照 `soma kit explain KIT-APP-001` 修正 envelope 段');
|
|
167
|
+
}
|
|
168
|
+
return env;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 拼装业务信封子路径(纯函数):
|
|
173
|
+
* - 若 dataPath === undefined || dataPath === '',返回 subPath;
|
|
174
|
+
* - 若 subPath === '',返回 dataPath;
|
|
175
|
+
* - 向后兼容守卫:若 subPath === dataPath || subPath.startsWith(`${dataPath}.`),直接返回 subPath(避免重复拼成 data.data.rows);
|
|
176
|
+
* - 否则返回 `${dataPath}.${subPath}`。
|
|
177
|
+
*/
|
|
178
|
+
export function resolveEnvelopeSubPath(subPath: string, dataPath?: string): string {
|
|
179
|
+
if (dataPath === undefined || dataPath === '') return subPath;
|
|
180
|
+
if (subPath === '') return dataPath;
|
|
181
|
+
if (subPath === dataPath || subPath.startsWith(`${dataPath}.`)) return subPath;
|
|
182
|
+
return `${dataPath}.${subPath}`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** 把有效信封编进一份 core 数据声明(`data:` 段):写通道与取数来源各加 failWhen / messagePath,单对象取数来源补 dataPath,已自带的不覆盖 */
|
|
186
|
+
export function applyEnvelopeToData(data: Record<string, unknown>, env: KitEnvelope | undefined): void {
|
|
187
|
+
if (env === undefined || (env.failWhen === undefined && env.dataPath === undefined)) return;
|
|
188
|
+
for (const decl of Object.values(data)) {
|
|
189
|
+
if (!isRec(decl)) continue;
|
|
190
|
+
const stamp = (target: Rec): void => {
|
|
191
|
+
if (env.failWhen !== undefined && target.failWhen === undefined) {
|
|
192
|
+
target.failWhen = env.failWhen;
|
|
193
|
+
if (env.messagePath !== undefined) target.messagePath = env.messagePath;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
if (isRec(decl.write)) stamp(decl.write);
|
|
197
|
+
if (Array.isArray(decl.from)) {
|
|
198
|
+
for (const source of decl.from) {
|
|
199
|
+
if (!isRec(source)) continue;
|
|
200
|
+
stamp(source);
|
|
201
|
+
if (env.dataPath !== undefined && source.into === undefined && source.dataPath === undefined) {
|
|
202
|
+
source.dataPath = env.dataPath;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// HTML 与 CSV 单元格转义轻量纯函数底座(单一实现;CSV 公式前缀读取 Kit 生成投影)
|
|
2
|
+
|
|
3
|
+
import { KIT_GRAMMAR_RUNTIME } from '../generated/kit-grammar-runtime';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* HTML 特殊字符转义(& < > " '):统一收拢,避免平行实现。
|
|
7
|
+
* 与 emit-static、frames、biz/link、biz/standard-crud、server、compile-page 保持一致。
|
|
8
|
+
*/
|
|
9
|
+
export function escapeHtml(text: string): string {
|
|
10
|
+
return text
|
|
11
|
+
.replace(/&/g, '&')
|
|
12
|
+
.replace(/</g, '<')
|
|
13
|
+
.replace(/>/g, '>')
|
|
14
|
+
.replace(/"/g, '"')
|
|
15
|
+
.replace(/'/g, ''');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { escapeHtml as esc };
|
|
19
|
+
|
|
20
|
+
const DEFAULT_FORMULA_GUARD = (KIT_GRAMMAR_RUNTIME as unknown as {
|
|
21
|
+
collectionKeys: { tableExt: { export: { formulaGuard: readonly string[] } } };
|
|
22
|
+
}).collectionKeys.tableExt.export.formulaGuard;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* CSV 单元格转义:
|
|
26
|
+
* - null / undefined -> ''
|
|
27
|
+
* - number / boolean -> String(value)(负数不误加单引号)
|
|
28
|
+
* - Array -> value.map(String).join(';')
|
|
29
|
+
* - object -> JSON.stringify(value)
|
|
30
|
+
* - 其余非数字/非布尔生成的非空文本,若首字符命中语法真源公式引导符,前置补 ' 防公式注入(含数组首项为公式文本)
|
|
31
|
+
* - 含 "、,、\r、\n 时双引号包裹并将内部 " 翻倍为 ""
|
|
32
|
+
*
|
|
33
|
+
* 支持直接传给 Array.prototype.map 作为回调函数(忽略多余的 index / array 参数)。
|
|
34
|
+
*/
|
|
35
|
+
export function escapeCsvCell(value: unknown, formulaGuardChars?: readonly string[] | unknown): string {
|
|
36
|
+
if (value === null || value === undefined) return '';
|
|
37
|
+
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
|
|
38
|
+
|
|
39
|
+
const guards = Array.isArray(formulaGuardChars) ? (formulaGuardChars as readonly string[]) : DEFAULT_FORMULA_GUARD;
|
|
40
|
+
|
|
41
|
+
let text: string;
|
|
42
|
+
if (Array.isArray(value)) {
|
|
43
|
+
text = value.map((v) => String(v)).join(';');
|
|
44
|
+
} else if (typeof value === 'object') {
|
|
45
|
+
text = JSON.stringify(value);
|
|
46
|
+
} else {
|
|
47
|
+
text = String(value);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (text !== '' && guards.includes(text.charAt(0))) {
|
|
51
|
+
text = `'${text}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return /[",\r\n]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { escapeCsvCell as csvCell };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// 装配期 / 登记期的结构化错误:所有面向使用者的 throw 都走这里,带 KIT-* 码、位置(path)、原因与修复建议。
|
|
2
|
+
// 码登记在 kit-grammar.json#diagnostics(`soma kit explain <码>` 读同一处);本文件只定形状,不放码表、不依赖其他编译模块(叶子,避免环)。
|
|
3
|
+
import type { KitDiagnostic } from '../object-renderer/types';
|
|
4
|
+
|
|
5
|
+
/** 带结构化诊断的错误;message 形如「[soma-kit] [码] 位置:原因(建议:…)」,diagnostics 供 `soma kit check --json` 等机器读取 */
|
|
6
|
+
export class KitDiagnosticError extends Error {
|
|
7
|
+
readonly diagnostics: KitDiagnostic[];
|
|
8
|
+
readonly code: string;
|
|
9
|
+
constructor(diagnostic: KitDiagnostic, tag = '[soma-kit]') {
|
|
10
|
+
const hint = diagnostic.hint !== undefined && diagnostic.hint !== '' ? `(建议:${diagnostic.hint})` : '';
|
|
11
|
+
super(`${tag} [${diagnostic.code}] ${diagnostic.path}:${diagnostic.message}${hint}`);
|
|
12
|
+
this.name = 'KitDiagnosticError';
|
|
13
|
+
this.code = diagnostic.code;
|
|
14
|
+
this.diagnostics = [diagnostic];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 抛一条结构化诊断:code 必须登记在 kit-grammar.json#diagnostics;hint 写「怎么改」 */
|
|
19
|
+
export function kitFail(code: string, path: string, message: string, hint: string): never {
|
|
20
|
+
throw new KitDiagnosticError({ code, path, message, hint });
|
|
21
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// 服务端分页整形助手已迁到 soma-core(J6 裁决:后端响应整形归 core):`soma-core/server/paged-list`。
|
|
2
|
+
// kit 应用不直接 import soma-core(AP-1 / KIT-APP-013),所以本文件是 kit 应用取用它的稳定转出口,
|
|
3
|
+
// 仅从服务端入口 `soma-kit/app/server` 暴露;浏览器入口 `soma-kit/app` 不导出这些名字。实现只有 core 一份,这里不加逻辑。
|
|
4
|
+
// 业务信封读取(isBizFailure / unwrapBizEnvelope)是浏览器也要用的信封约定,实现在 ./envelope,不在此处。
|
|
5
|
+
|
|
6
|
+
export { createPagedListAdapter, cursorPageWindow, withRowId } from 'soma-core/server/paged-list';
|
|
7
|
+
export type {
|
|
8
|
+
PagedListAdapter,
|
|
9
|
+
PagedListAdapterOptions,
|
|
10
|
+
PagedQuery,
|
|
11
|
+
PagedList,
|
|
12
|
+
CursorLoader,
|
|
13
|
+
CursorPageOptions,
|
|
14
|
+
CursorPageResult,
|
|
15
|
+
} from 'soma-core/server/paged-list';
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { KitDiagnostic } from '../object-renderer/types';
|
|
2
|
+
|
|
3
|
+
/** 纯路径校验:浏览器入口仅依赖该小模块,不导入 Node、扫描器或语法真源。 */
|
|
4
|
+
const CONTROL_CHAR_PATTERN = /[\u0000-\u001f\u007f-\u009f]/;
|
|
5
|
+
const EXTERNAL_HREF_PATTERN = /^(?:https?:\/\/|mailto:|tel:|#)/i;
|
|
6
|
+
const WINDOWS_DRIVE_PATTERN = /^[A-Za-z]:[\\/]/;
|
|
7
|
+
const FILE_SCHEME_PATTERN = /^file:/i;
|
|
8
|
+
const UNC_PATTERN = /^(?:\\\\|\/\/)/;
|
|
9
|
+
const PROTOCOL_RELATIVE_PATTERN = /^[/\\][\s\u0000-\u001f\u007f]*[/\\]/;
|
|
10
|
+
|
|
11
|
+
export interface PathCheckOptions {
|
|
12
|
+
/** 是否允许纯 query 传参形式(如 ?id=1) */
|
|
13
|
+
allowQueryOnly?: boolean;
|
|
14
|
+
/** 是否允许相对应用根的无前缀配置子路径(如 style: 'theme/custom.css') */
|
|
15
|
+
allowConfigSubpath?: boolean;
|
|
16
|
+
/** 是否允许外部超链接(如 https://, mailto:, tel:, #) */
|
|
17
|
+
allowExternalHref?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 兼容旧 helper:只识别 URL 语境下无歧义的非站内形态;单斜杠路由必须由语境校验。 */
|
|
21
|
+
export function isSystemAbsolutePath(val: string): boolean {
|
|
22
|
+
const trimmed = val.trim();
|
|
23
|
+
return /^[A-Za-z]:[\\/]/.test(trimmed) || /^\\\\|^\/\/|^file:/i.test(trimmed);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function pathnameOf(value: string): string {
|
|
27
|
+
const query = value.indexOf('?');
|
|
28
|
+
const hash = value.indexOf('#');
|
|
29
|
+
const end = query < 0 ? hash : hash < 0 ? query : Math.min(query, hash);
|
|
30
|
+
return end < 0 ? value : value.slice(0, end);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 编码约束只应用于 pathname/config path,不应用于 query 或 hash 的值。 */
|
|
34
|
+
function hasSafeEncodedSegments(pathname: string, allowEncodedSeparator = false): boolean {
|
|
35
|
+
const badEscape = allowEncodedSeparator ? /%(?:0[0-9a-f]|1[0-9a-f]|7f)/i : /%(?:2f|5c|0[0-9a-f]|1[0-9a-f]|7f)/i;
|
|
36
|
+
if (/%(?![0-9a-f]{2})/i.test(pathname) || badEscape.test(pathname)) return false;
|
|
37
|
+
for (const segment of pathname.split('/')) {
|
|
38
|
+
let decoded: string;
|
|
39
|
+
try {
|
|
40
|
+
decoded = decodeURIComponent(segment);
|
|
41
|
+
} catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
if (decoded === '.' || decoded === '..' || CONTROL_CHAR_PATTERN.test(decoded) || (!allowEncodedSeparator && (decoded.includes('/') || decoded.includes('\\')))) return false;
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function isSafeInternalPath(value: string): boolean {
|
|
50
|
+
const pathname = pathnameOf(value);
|
|
51
|
+
if (!value.startsWith('/') || value.startsWith('//') || pathname.includes('\\') || PROTOCOL_RELATIVE_PATTERN.test(pathname)) return false;
|
|
52
|
+
if (!pathname.startsWith('/') || pathname.startsWith('//') || /(?:^|\/)\.{1,2}(?:\/|$)/.test(pathname)) return false;
|
|
53
|
+
return hasSafeEncodedSegments(pathname);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function isSafeConfigSubpath(value: string): boolean {
|
|
57
|
+
if (value === '' || value.startsWith('/') || value.includes('\\') || WINDOWS_DRIVE_PATTERN.test(value) || UNC_PATTERN.test(value) || FILE_SCHEME_PATTERN.test(value)) return false;
|
|
58
|
+
if (value.includes('?') || value.includes('#') || /(?:^|\/)\.{1,2}(?:\/|$)/.test(value)) return false;
|
|
59
|
+
return hasSafeEncodedSegments(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isSafeExternalHref(value: string): boolean {
|
|
63
|
+
if (!EXTERNAL_HREF_PATTERN.test(value) || FILE_SCHEME_PATTERN.test(value) || WINDOWS_DRIVE_PATTERN.test(value) || UNC_PATTERN.test(value) || pathnameOf(value).includes('\\')) return false;
|
|
64
|
+
if (!/^https?:\/\//i.test(value)) return true;
|
|
65
|
+
const match = /^https?:\/\/[^/?#]+(\/[^?#]*)?(?:[?#]|$)/i.exec(value);
|
|
66
|
+
// 外部 http(s) 链接的路径段允许编码的 / \(如 SSO 回跳地址 https%3A%2F%2F…);点段与控制符仍拦
|
|
67
|
+
return match !== null && hasSafeEncodedSegments(match[1] ?? '', true);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 兼容原有公开 helper;true 表示值不符合当前 URL/config 路径语境。 */
|
|
71
|
+
export function isRelativePathValue(val: string, options?: PathCheckOptions): boolean {
|
|
72
|
+
if (CONTROL_CHAR_PATTERN.test(val)) return true;
|
|
73
|
+
const trimmed = val.trim();
|
|
74
|
+
if (trimmed === '') return false;
|
|
75
|
+
if (options?.allowQueryOnly === true && trimmed.startsWith('?')) return false;
|
|
76
|
+
if (options?.allowConfigSubpath === true) return !isSafeConfigSubpath(trimmed);
|
|
77
|
+
if (options?.allowExternalHref === true && EXTERNAL_HREF_PATTERN.test(trimmed)) return !isSafeExternalHref(trimmed);
|
|
78
|
+
return !isSafeInternalPath(trimmed);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function pushDiagOnce(out: KitDiagnostic[], diag: KitDiagnostic): void {
|
|
82
|
+
if (!out.some((d) => d.code === diag.code && d.path === diag.path)) out.push(diag);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 按 URL/config 语境校验;单个 / 开头的任意业务段都是合法站内路由。 */
|
|
86
|
+
export function checkNoRelativeOrSystemPath(
|
|
87
|
+
val: unknown,
|
|
88
|
+
fieldPath: string,
|
|
89
|
+
diagnostics: KitDiagnostic[],
|
|
90
|
+
code = 'KIT-PAGE-004',
|
|
91
|
+
options?: PathCheckOptions,
|
|
92
|
+
): boolean {
|
|
93
|
+
if (typeof val !== 'string') return true;
|
|
94
|
+
if (CONTROL_CHAR_PATTERN.test(val)) {
|
|
95
|
+
pushDiagOnce(diagnostics, { code, path: fieldPath, message: `${fieldPath} 含非法控制字符(收到 ${JSON.stringify(val)})` });
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const trimmed = val.trim();
|
|
100
|
+
if (trimmed === '') return true;
|
|
101
|
+
if (options?.allowQueryOnly === true && trimmed.startsWith('?')) return true;
|
|
102
|
+
|
|
103
|
+
if (options?.allowConfigSubpath === true) {
|
|
104
|
+
if (isSafeConfigSubpath(trimmed)) return true;
|
|
105
|
+
pushDiagOnce(diagnostics, {
|
|
106
|
+
code,
|
|
107
|
+
path: fieldPath,
|
|
108
|
+
message: `${fieldPath} 必须是位于应用根内的配置子路径(收到 ${JSON.stringify(val)});不允许绝对路径、点段、编码分隔符或 query/hash`,
|
|
109
|
+
});
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (options?.allowExternalHref === true && EXTERNAL_HREF_PATTERN.test(trimmed) && isSafeExternalHref(trimmed)) return true;
|
|
114
|
+
if (!isSafeInternalPath(trimmed)) {
|
|
115
|
+
const nonSite = FILE_SCHEME_PATTERN.test(trimmed) || WINDOWS_DRIVE_PATTERN.test(trimmed) || UNC_PATTERN.test(trimmed) || trimmed.startsWith('//') || pathnameOf(trimmed).includes('\\');
|
|
116
|
+
const message = PROTOCOL_RELATIVE_PATTERN.test(trimmed)
|
|
117
|
+
? `${fieldPath} 严禁使用协议相对或混淆地址(收到 ${JSON.stringify(val)});站内地址必须以单个 / 开头`
|
|
118
|
+
: nonSite
|
|
119
|
+
? `${fieldPath} 严禁使用 file:、盘符、UNC、协议相对或反斜杠路径(收到 ${JSON.stringify(val)});站内地址必须以单个 / 开头`
|
|
120
|
+
: !trimmed.startsWith('/')
|
|
121
|
+
? `${fieldPath} 严禁使用裸相对路径(收到 ${JSON.stringify(val)});必须是以 / 开头的站内路径`
|
|
122
|
+
: `${fieldPath} 的路径部分含非法点段、百分号编码、分隔符或控制符(收到 ${JSON.stringify(val)})`;
|
|
123
|
+
pushDiagOnce(diagnostics, { code, path: fieldPath, message });
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
return true;
|
|
127
|
+
}
|