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
package/src/data/row-editor.ts
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
import type { SomaComponentDefinition } from 'soma-core';
|
|
16
16
|
import { bool, createKitController, FIELD_CLASS, formatValue, joinClass, num, str, type KitApi } from '../base/shared';
|
|
17
17
|
import { kitText, type KitTextKey } from '../i18n/kit-text';
|
|
18
|
+
import { setKitFace } from '../recipes';
|
|
18
19
|
import { KIT_BUSY_PROP_DEFS, kitBusyOf } from '../base/action-busy';
|
|
19
20
|
import { KIT_DATE_CLASS_CANDIDATES, mountKitDateField } from '../base/date';
|
|
20
21
|
import { KIT_LISTBOX_CLASS_CANDIDATES } from '../base/listbox-core';
|
|
@@ -25,8 +26,9 @@ import type { SomaDataPort } from 'soma-core';
|
|
|
25
26
|
import { askKitConfirm } from './confirm-dialog';
|
|
26
27
|
import { watchKitOutcome } from './outcome-watch';
|
|
27
28
|
import { stagedDiff, stagedHeadKeys, stagedMarks, stagedModel } from './staged-rows';
|
|
29
|
+
import { testRegexGuarded } from 'soma-foundation/predicates';
|
|
28
30
|
|
|
29
|
-
// 集合本地草稿(staged):未同步条与行小标的类串(条与小标的语气取 warning-soft
|
|
31
|
+
// 集合本地草稿(staged):未同步条与行小标的类串(条与小标的语气取 warning-soft)
|
|
30
32
|
const STAGE_BAR_CLASS = `flex flex-wrap items-center justify-between gap-2 rounded-md px-3 py-2 text-sm ${TONE_SOFT.warning}`;
|
|
31
33
|
const STAGE_BTNS_CLASS = 'flex items-center gap-2';
|
|
32
34
|
const STAGE_MARK_CLASS = `ms-2 rounded-full px-2 py-0.5 text-xs font-medium ${TONE_SOFT.warning}`;
|
|
@@ -62,12 +64,14 @@ const LIST_WRAP = 'flex flex-col';
|
|
|
62
64
|
const NUM_ALIGN = 'text-end tabular-nums';
|
|
63
65
|
const FOOTER_ROW_CLASS = 'grid grid-cols-12 items-center gap-2 border-t border-border-divider px-2 py-2 text-sm font-semibold text-fg';
|
|
64
66
|
const COUNT_CLASS = 'text-xs text-fg-subtle';
|
|
67
|
+
/** 出错单元格的外框(令牌色环,不撑行高;圆角不在此处手写,沿用单元格自身) */
|
|
68
|
+
const CELL_ERROR_CLASS = 'ring-1 ring-danger';
|
|
65
69
|
const DATE_FIELD_CLASS = 'block h-9 w-full rounded-md border border-border bg-surface ps-3 pe-8 text-sm text-fg transition-colors placeholder:text-fg-subtle enabled:hover:border-fg-subtle focus-visible:outline-none focus-visible:border-primary focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-not-allowed disabled:bg-surface-sunken disabled:text-fg-subtle disabled:opacity-60 read-only:bg-surface-sunken';
|
|
66
70
|
|
|
67
71
|
export const KIT_ROW_EDITOR_CLASS_CANDIDATES: readonly string[] = classTokens(
|
|
68
72
|
ROOT_CLASS, TITLE_CLASS, HINT_CLASS, HEAD_CLASS, ROW_CLASS, CELL_CLASS, CELL_MONO, CELL_MUTED, END_CLASS, FORM_CLASS, FIELD_WRAP,
|
|
69
73
|
LABEL_CLASS, REQUIRED_MARK, TEXTAREA_LIKE, ERROR_CLASS, LIST_WRAP, Object.values(SPAN_CLASS), Object.values(FORM_COLS),
|
|
70
|
-
FIELD_CLASS.md, BUTTON_CLASS_TOKENS, 'self-end', '@container', NUM_ALIGN, FOOTER_ROW_CLASS, COUNT_CLASS, DATE_FIELD_CLASS, KIT_DATE_CLASS_CANDIDATES, KIT_LISTBOX_CLASS_CANDIDATES,
|
|
74
|
+
FIELD_CLASS.md, BUTTON_CLASS_TOKENS, 'self-end', '@container', NUM_ALIGN, FOOTER_ROW_CLASS, COUNT_CLASS, CELL_ERROR_CLASS, DATE_FIELD_CLASS, KIT_DATE_CLASS_CANDIDATES, KIT_LISTBOX_CLASS_CANDIDATES,
|
|
71
75
|
STAGE_BAR_CLASS, STAGE_BTNS_CLASS, STAGE_MARK_CLASS, STAGE_NOTE_CLASS,
|
|
72
76
|
);
|
|
73
77
|
|
|
@@ -110,6 +114,11 @@ export interface KitRowColumn {
|
|
|
110
114
|
list: boolean;
|
|
111
115
|
/** GB5:select 列的远程选项(options 写成对象 { from, valueKey?, labelKey?, rowsPath?, params? });静态选项为 null */
|
|
112
116
|
optionsFrom: KitRowOptionsFrom | null;
|
|
117
|
+
/** 远程候选失败时附加到当前列视图的重试元数据 */
|
|
118
|
+
optionsError?: boolean;
|
|
119
|
+
optionsKey?: string;
|
|
120
|
+
optionsRetryState?: 'loading' | 'ok' | 'failed';
|
|
121
|
+
optionsRetryLabel?: string;
|
|
113
122
|
}
|
|
114
123
|
|
|
115
124
|
/** select 列的远程选项声明(规整后;键同 schema.<字段>.options 的对象写法,params 只许字面量) */
|
|
@@ -217,31 +226,61 @@ export function rowTotals(columns: readonly KitRowColumn[], rows: ReadonlyArray<
|
|
|
217
226
|
return out;
|
|
218
227
|
}
|
|
219
228
|
|
|
220
|
-
/**
|
|
221
|
-
|
|
229
|
+
/** 单列录入校验:通过返回 null,否则返回该列的第一条提示文字 */
|
|
230
|
+
function columnDraftError(c: KitRowColumn, draft: Readonly<Record<string, string>>, requiredText: string, at?: Element | string | null): string | null {
|
|
222
231
|
const T = (key: KitTextKey, params: Record<string, string | number>): string => kitText(key, params, at);
|
|
232
|
+
const v = (draft[c.key] ?? '').trim();
|
|
233
|
+
if (!c.editable) return null;
|
|
234
|
+
if (c.required && v === '') return c.requiredMessage !== '' ? c.requiredMessage : `${requiredText}${c.label}`;
|
|
235
|
+
if (isNumericRowKind(c.kind) && v !== '') {
|
|
236
|
+
const n = Number(v);
|
|
237
|
+
if (!Number.isFinite(n)) return T('soma.kit.field_number', { label: c.label });
|
|
238
|
+
if (c.min !== null && n < c.min) return T('soma.kit.field_min', { label: c.label, n: c.min });
|
|
239
|
+
if (c.max !== null && n > c.max) return T('soma.kit.field_max', { label: c.label, n: c.max });
|
|
240
|
+
const frac = v.includes('.') ? v.split('.')[1]!.length : 0;
|
|
241
|
+
if (c.decimals >= 0 && frac > c.decimals) return T('soma.kit.field_decimals', { label: c.label, n: c.decimals });
|
|
242
|
+
}
|
|
243
|
+
if (c.kind === 'date' && v !== '' && !isIsoDate(v)) return T('soma.kit.field_date', { label: c.label });
|
|
244
|
+
// 与字段规则同一份带防护的正则实现:模式非法 / 超长(null)放行,被测文本超长视为不通过且不执行匹配
|
|
245
|
+
if (c.pattern !== '' && v !== '' && testRegexGuarded(c.pattern, v) === false) {
|
|
246
|
+
return c.patternMessage !== '' ? c.patternMessage : T('soma.kit.field_format', { label: c.label });
|
|
247
|
+
}
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** 一行草稿的全部列错误(每列至多一条,按列序):col 为列 key */
|
|
252
|
+
export function rowDraftErrors(columns: readonly KitRowColumn[], draft: Readonly<Record<string, string>>, requiredText: string, at?: Element | string | null): Array<{ col: string; message: string }> {
|
|
253
|
+
const out: Array<{ col: string; message: string }> = [];
|
|
223
254
|
for (const c of columns) {
|
|
224
|
-
const
|
|
225
|
-
if (
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
if (c.kind === 'date' && v !== '' && !isIsoDate(v)) return T('soma.kit.field_date', { label: c.label });
|
|
236
|
-
if (c.pattern !== '' && v !== '') {
|
|
237
|
-
let re: RegExp | null = null;
|
|
238
|
-
try { re = new RegExp(c.pattern); } catch { re = null; }
|
|
239
|
-
if (re && !re.test(v)) return c.patternMessage !== '' ? c.patternMessage : T('soma.kit.field_format', { label: c.label });
|
|
240
|
-
}
|
|
255
|
+
const message = columnDraftError(c, draft, requiredText, at);
|
|
256
|
+
if (message !== null) out.push({ col: c.key, message });
|
|
257
|
+
}
|
|
258
|
+
return out;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** 录入校验(纯函数):通过返回 null,否则返回提示文字(第一条) */
|
|
262
|
+
export function validateRowDraft(columns: readonly KitRowColumn[], draft: Readonly<Record<string, string>>, requiredText: string, at?: Element | string | null): string | null {
|
|
263
|
+
for (const c of columns) {
|
|
264
|
+
const message = columnDraftError(c, draft, requiredText, at);
|
|
265
|
+
if (message !== null) return message;
|
|
241
266
|
}
|
|
242
267
|
return null;
|
|
243
268
|
}
|
|
244
269
|
|
|
270
|
+
/** 行是否有内容:任一列(含只读列:服务端回填的编号、名称)存在非空值(空串 / null / undefined / 空数组 / 等于列缺省值的都算空) */
|
|
271
|
+
function rowHasContent(columns: readonly KitRowColumn[], row: Readonly<Record<string, unknown>>): boolean {
|
|
272
|
+
for (const c of columns) {
|
|
273
|
+
const v = row[c.key];
|
|
274
|
+
if (v === undefined || v === null) continue;
|
|
275
|
+
if (Array.isArray(v)) { if (v.length > 0) return true; continue; }
|
|
276
|
+
const t = (typeof v === 'object' ? JSON.stringify(v) : String(v)).trim();
|
|
277
|
+
if (t === '' || t === c.dflt.trim()) continue;
|
|
278
|
+
if (c.kind === 'bool' && t === 'false' && c.dflt !== 'true') continue;
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
|
|
245
284
|
/** 草稿 -> 行:bool 转布尔,json 能解析就解析,其余原样字符串(text 去首尾空白) */
|
|
246
285
|
export function draftToRow(columns: readonly KitRowColumn[], draft: Readonly<Record<string, string>>): Record<string, unknown> {
|
|
247
286
|
const row: Record<string, unknown> = {};
|
|
@@ -304,9 +343,28 @@ export function inlineCellValue(col: KitRowColumn, text: string): unknown {
|
|
|
304
343
|
return text;
|
|
305
344
|
}
|
|
306
345
|
|
|
346
|
+
/** 子表单元格错误:row 为行下标,col 为列 key */
|
|
347
|
+
export interface KitRowCellError { row: number; col: string; message: string }
|
|
348
|
+
|
|
349
|
+
/** 整张子表逐行逐格校验(纯函数,不含行数上下限):返回全部单元格错误,行序、列序;locked_key 行跳过 */
|
|
350
|
+
export function validateInlineRowCells(
|
|
351
|
+
columns: readonly KitRowColumn[],
|
|
352
|
+
rows: ReadonlyArray<Record<string, unknown>>,
|
|
353
|
+
opts: { requiredText?: string; lockedKey?: string } = {},
|
|
354
|
+
at?: Element | string | null,
|
|
355
|
+
): KitRowCellError[] {
|
|
356
|
+
const requiredText = opts.requiredText ?? kitText('soma.kit.row_required', undefined, at);
|
|
357
|
+
const out: KitRowCellError[] = [];
|
|
358
|
+
for (let i = 0; i < rows.length; i += 1) {
|
|
359
|
+
if (opts.lockedKey !== undefined && opts.lockedKey !== '' && rows[i]![opts.lockedKey] === true) continue;
|
|
360
|
+
for (const e of rowDraftErrors(columns, rowToDraft(columns, rows[i]!), requiredText, at)) out.push({ row: i, col: e.col, message: e.message });
|
|
361
|
+
}
|
|
362
|
+
return out;
|
|
363
|
+
}
|
|
364
|
+
|
|
307
365
|
/**
|
|
308
366
|
* 整张子表的提交前校验(纯函数):行数上下限,再逐行逐列沿用录入校验(required / pattern / 数值范围),
|
|
309
|
-
* 错误带「第 N
|
|
367
|
+
* 错误带「第 N 行:」前缀,只返回第一条。通过返回 null。全部错误用 validateInlineRowCells。
|
|
310
368
|
*/
|
|
311
369
|
export function validateInlineRows(
|
|
312
370
|
columns: readonly KitRowColumn[],
|
|
@@ -318,13 +376,8 @@ export function validateInlineRows(
|
|
|
318
376
|
const maxRows = Math.max(0, Math.floor(opts.maxRows ?? 0));
|
|
319
377
|
if (minRows > 0 && rows.length < minRows) return kitText('soma.kit.rows_min', { n: minRows }, at);
|
|
320
378
|
if (maxRows > 0 && rows.length > maxRows) return kitText('soma.kit.rows_max', { n: maxRows }, at);
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
if (opts.lockedKey !== undefined && opts.lockedKey !== '' && rows[i]![opts.lockedKey] === true) continue;
|
|
324
|
-
const message = validateRowDraft(columns, rowToDraft(columns, rows[i]!), requiredText, at);
|
|
325
|
-
if (message !== null) return kitText('soma.kit.rows_row_error', { n: i + 1, message }, at);
|
|
326
|
-
}
|
|
327
|
-
return null;
|
|
379
|
+
const first = validateInlineRowCells(columns, rows, opts, at)[0];
|
|
380
|
+
return first !== undefined ? kitText('soma.kit.rows_row_error', { n: first.row + 1, message: first.message }, at) : null;
|
|
328
381
|
}
|
|
329
382
|
|
|
330
383
|
// ── 组件 ──
|
|
@@ -335,9 +388,14 @@ interface EditorState {
|
|
|
335
388
|
rows: Array<Record<string, unknown>>;
|
|
336
389
|
rowsSig: string;
|
|
337
390
|
localError: string;
|
|
391
|
+
/** 父表单传来的单元格错误(本地可被单格修改逐个清除)与最近一次收到的入参签名 */
|
|
392
|
+
cellErrors: KitRowCellError[];
|
|
393
|
+
cellErrSig: string;
|
|
338
394
|
pendingClear: boolean;
|
|
339
395
|
/** 行内编辑模式的行表签名(结构不变就不重建,输入框焦点与光标不丢) */
|
|
340
396
|
listSig: string;
|
|
397
|
+
/** 最近向外派发 change 的本地快照记录(防止父级滞后回灌冲掉最新输入并失焦) */
|
|
398
|
+
emittedSnapshots: string[];
|
|
341
399
|
/** 行内编辑的单元格控件(自绘下拉 / 日期框):行表重建前统一释放 */
|
|
342
400
|
cellEmbeds: KitEmbedBag;
|
|
343
401
|
nodes: { list: HTMLElement; form: HTMLElement; err: HTMLElement; title: HTMLElement; hint: HTMLElement; count: HTMLElement } | null;
|
|
@@ -349,9 +407,18 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
349
407
|
const states = new WeakMap<HTMLElement, EditorState>();
|
|
350
408
|
const stateOf = (root: HTMLElement): EditorState => {
|
|
351
409
|
let s = states.get(root);
|
|
352
|
-
if (!s) { s = { columnsSig: '', draft: {}, rows: [], rowsSig: '', localError: '', pendingClear: false, listSig: '', cellEmbeds: createKitEmbedBag(), nodes: null, embeds: createKitEmbedBag() }; states.set(root, s); }
|
|
410
|
+
if (!s) { s = { columnsSig: '', draft: {}, rows: [], rowsSig: '', localError: '', cellErrors: [], cellErrSig: '', pendingClear: false, listSig: '', emittedSnapshots: [], cellEmbeds: createKitEmbedBag(), nodes: null, embeds: createKitEmbedBag() }; states.set(root, s); }
|
|
353
411
|
return s;
|
|
354
412
|
};
|
|
413
|
+
const recordSnapshot = (st: EditorState): void => {
|
|
414
|
+
try {
|
|
415
|
+
const snap = JSON.stringify(st.rows);
|
|
416
|
+
st.emittedSnapshots.push(snap);
|
|
417
|
+
if (st.emittedSnapshots.length > 30) {
|
|
418
|
+
st.emittedSnapshots.splice(0, st.emittedSnapshots.length - 30);
|
|
419
|
+
}
|
|
420
|
+
} catch {}
|
|
421
|
+
};
|
|
355
422
|
const resetDraft = (st: EditorState, cols: readonly KitRowColumn[]): void => {
|
|
356
423
|
for (const c of cols) st.draft[c.key] = c.kind === 'bool' ? (c.dflt === 'true' ? 'true' : 'false') : c.dflt;
|
|
357
424
|
st.columnsSig = '';
|
|
@@ -374,11 +441,25 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
374
441
|
}
|
|
375
442
|
const stages = new WeakMap<HTMLElement, StageState>();
|
|
376
443
|
const apis = new WeakMap<HTMLElement, KitApi>();
|
|
444
|
+
/** 删行确认框打开期间的组件根(防重复点击叠弹窗) */
|
|
445
|
+
const removeAsking = new WeakSet<HTMLElement>();
|
|
377
446
|
|
|
378
447
|
// ── GB5:select 列的远程选项(options.from)──
|
|
379
448
|
// 按根登记已取回的候选;key = from + params + rowsPath。取数中显示『选项加载中…』、失败显示『选项加载失败』,都不假装有选项。
|
|
380
449
|
interface RemoteSlot { state: 'loading' | 'ok' | 'failed'; options: Array<{ value: string; label: string }> }
|
|
381
450
|
const remotes = new WeakMap<HTMLElement, Map<string, RemoteSlot>>();
|
|
451
|
+
const optionsKeyOf = (of: KitRowOptionsFrom): string => JSON.stringify([of.from, of.params, of.rowsPath, of.valueKey, of.labelKey]);
|
|
452
|
+
const withOptionsRetry = (holder: HTMLElement, column: KitRowColumn): HTMLElement => {
|
|
453
|
+
if ((column.optionsRetryState !== 'failed' && column.optionsRetryState !== 'loading') || column.optionsKey === undefined) return holder;
|
|
454
|
+
const box = mk('span', FIELD_WRAP);
|
|
455
|
+
box.appendChild(holder);
|
|
456
|
+
const loading = column.optionsRetryState === 'loading';
|
|
457
|
+
const retry = actionButton({ key: 'options_retry', label: column.optionsRetryLabel ?? kitText(loading ? 'soma.kit.rows_options_loading' : 'soma.kit.ct_retry'), tone: 'neutral', disabled: false, reason: '' });
|
|
458
|
+
retry.setAttribute('data-kit-option-key', column.optionsKey);
|
|
459
|
+
if (loading) retry.setAttribute('aria-disabled', 'true');
|
|
460
|
+
box.appendChild(retry);
|
|
461
|
+
return box;
|
|
462
|
+
};
|
|
382
463
|
/** 把远程选项叠到列声明上(纯叠加;取数在这里发起,回来后整体重绘) */
|
|
383
464
|
const overlayRemote = (root: HTMLElement, cols: KitRowColumn[]): KitRowColumn[] => {
|
|
384
465
|
if (!cols.some((c) => c.optionsFrom !== null)) return cols;
|
|
@@ -388,7 +469,7 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
388
469
|
return cols.map((c) => {
|
|
389
470
|
const of = c.optionsFrom;
|
|
390
471
|
if (of === null) return c;
|
|
391
|
-
const key =
|
|
472
|
+
const key = optionsKeyOf(of);
|
|
392
473
|
let slot = mine.get(key);
|
|
393
474
|
if (slot === undefined) {
|
|
394
475
|
const fresh: RemoteSlot = { state: 'loading', options: [] };
|
|
@@ -401,18 +482,43 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
401
482
|
const api = apis.get(root);
|
|
402
483
|
if (api) paint(root, api.props());
|
|
403
484
|
};
|
|
404
|
-
|
|
485
|
+
// 第 5 参 root:按组件所在页面宿主取信封(同一文档多宿主时不串)
|
|
486
|
+
fetchRemoteOptionRows(apis.get(root)?.services, of.from, of.params, of.rowsPath, root)
|
|
405
487
|
.then((rows) => done('ok', rows.map((r) => ({ value: textOf(r[of.valueKey]), label: textOf(r[of.labelKey]) || textOf(r[of.valueKey]) })).filter((o) => o.value !== '')))
|
|
406
488
|
.catch(() => done('failed', []));
|
|
407
489
|
}
|
|
408
490
|
return {
|
|
409
491
|
...c,
|
|
410
492
|
options: slot.options,
|
|
493
|
+
optionsError: slot.state === 'failed',
|
|
494
|
+
optionsKey: key,
|
|
495
|
+
optionsRetryState: slot.state,
|
|
496
|
+
optionsRetryLabel: kitText(slot.state === 'loading' ? 'soma.kit.rows_options_loading' : 'soma.kit.ct_retry', undefined, root),
|
|
411
497
|
placeholder: slot.state === 'loading' ? kitText('soma.kit.rows_options_loading', undefined, root)
|
|
412
498
|
: slot.state === 'failed' ? kitText('soma.kit.rows_options_failed', undefined, root) : c.placeholder,
|
|
413
499
|
};
|
|
414
500
|
});
|
|
415
501
|
};
|
|
502
|
+
const retryRemoteOptions = (root: HTMLElement, key: string): void => {
|
|
503
|
+
const mine = remotes.get(root);
|
|
504
|
+
const previous = mine?.get(key);
|
|
505
|
+
const api = apis.get(root);
|
|
506
|
+
if (!mine || previous?.state !== 'failed' || !api) return;
|
|
507
|
+
const column = rowColumnsOf(api.props().columns, root).find((c) => c.optionsFrom !== null && optionsKeyOf(c.optionsFrom) === key);
|
|
508
|
+
if (!column?.optionsFrom) return;
|
|
509
|
+
const fresh: RemoteSlot = { state: 'loading', options: [] };
|
|
510
|
+
mine.set(key, fresh);
|
|
511
|
+
const done = (state: 'ok' | 'failed', options: Array<{ value: string; label: string }>): void => {
|
|
512
|
+
if (remotes.get(root) !== mine || mine.get(key) !== fresh) return;
|
|
513
|
+
fresh.state = state;
|
|
514
|
+
fresh.options = options;
|
|
515
|
+
paint(root, api.props());
|
|
516
|
+
};
|
|
517
|
+
paint(root, api.props());
|
|
518
|
+
fetchRemoteOptionRows(api.services, column.optionsFrom.from, column.optionsFrom.params, column.optionsFrom.rowsPath, root)
|
|
519
|
+
.then((rows) => done('ok', rows.map((r) => ({ value: textOf(r[column.optionsFrom!.valueKey]), label: textOf(r[column.optionsFrom!.labelKey]) || textOf(r[column.optionsFrom!.valueKey]) })).filter((o) => o.value !== '')))
|
|
520
|
+
.catch(() => done('failed', []));
|
|
521
|
+
};
|
|
416
522
|
const stageOf = (root: HTMLElement): StageState => {
|
|
417
523
|
let s = stages.get(root);
|
|
418
524
|
if (!s) { s = { base: null, sent: null, stale: false, awaiting: false, syncing: false, failMsg: '', scope: '', off: null, bar: null, barSig: '', lastCount: 0 }; stages.set(root, s); }
|
|
@@ -559,6 +665,7 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
559
665
|
locked_key: { type: 'string', default: '', desc: '行级只读:该字段为真的行,所有列显示为纯文字、不可改(如敏感字段不允许勾选);提交校验也跳过这些行。只在 edit_mode: inline 生效' },
|
|
560
666
|
fixed_rows: { type: 'boolean', default: false, desc: '行由数据给定,不可增删(无添加钮与删除钮),只改 editable 的列;常用于「字段策略」这类逐行勾选' },
|
|
561
667
|
columns: { type: 'array', default: [], desc: '列 [{ key, label, kind?: text|select|bool|json|number|money|date, min?, max?, decimals?, currency?, sum?(number/money 参与合计,缺省真), options?, placeholder?, required?, pattern?, pattern_message?, mono?, span?, default?, true_label?, false_label?, required_message?, editable?(假=只读展示列), list?(假=只在录入区) }]' },
|
|
668
|
+
cell_errors: { type: 'array', default: [], desc: '单元格错误 [{ row, col, message }](row 行下标、col 列 key):由表单提交校验失败后传入,对应单元格控件标 aria-invalid、外框标红并以 title 给出该格消息;改某格即清除该格标记。只在 edit_mode: inline 生效' },
|
|
562
669
|
row_key: { type: 'string', default: '', desc: '主键列(同主键视为替换),缺省第一列' },
|
|
563
670
|
readonly: { type: 'boolean', default: false, desc: '只读:无录入区与删除钮' },
|
|
564
671
|
...KIT_BUSY_PROP_DEFS,
|
|
@@ -593,7 +700,7 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
593
700
|
},
|
|
594
701
|
services: ['data', 'http'],
|
|
595
702
|
templateHtml: `<section data-soma-id="root" class="@container ${ROOT_CLASS}"></section>`,
|
|
596
|
-
controllerFactory: createKitController({
|
|
703
|
+
controllerFactory: createKitController(() => ({
|
|
597
704
|
busyAware: true,
|
|
598
705
|
attach(root, api) {
|
|
599
706
|
apis.set(root, api);
|
|
@@ -618,6 +725,13 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
618
725
|
const next = inlineCellValue(col, el.value);
|
|
619
726
|
if (JSON.stringify(row[cell]) === JSON.stringify(next)) return;
|
|
620
727
|
st.rows = st.rows.map((r, i) => (i === index ? { ...r, [cell]: next } : r));
|
|
728
|
+
recordSnapshot(st);
|
|
729
|
+
// 改了这一格:该格的校验错误标记即时清除(其余格保留,等下次提交重算)
|
|
730
|
+
if (st.cellErrors.some((e) => e.row === index && e.col === cell)) {
|
|
731
|
+
st.cellErrors = st.cellErrors.filter((e) => !(e.row === index && e.col === cell));
|
|
732
|
+
st.cellErrSig = ''; // 下次下传的列表(哪怕与上次内容相同)也要重新采用
|
|
733
|
+
applyCellErrors(root, st);
|
|
734
|
+
}
|
|
621
735
|
// st.rowsSig 不动:它记的是「上次收到的外部行」,表单回灌与本地副本一致的值时 paint 不重建
|
|
622
736
|
const allCols = rowColumnsOf(p.columns, root);
|
|
623
737
|
api.emit('change', { value: st.rows, totals: rowTotals(allCols, st.rows) });
|
|
@@ -637,6 +751,11 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
637
751
|
const cols = rowColumnsOf(p.columns, root);
|
|
638
752
|
const key = keyOf();
|
|
639
753
|
const act = btn.getAttribute('data-kit-action');
|
|
754
|
+
if (act === 'options_retry') {
|
|
755
|
+
const optionKey = btn.getAttribute('data-kit-option-key');
|
|
756
|
+
if (optionKey !== null && btn.getAttribute('aria-disabled') !== 'true') retryRemoteOptions(root, optionKey);
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
640
759
|
if (act === 'stage_sync') { stageSync(root, p); return; }
|
|
641
760
|
if (act === 'stage_discard') { void stageDiscard(root, p); return; }
|
|
642
761
|
if (act === 'add_row') {
|
|
@@ -653,6 +772,7 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
653
772
|
for (const c of cols) blank[c.key] = c.kind === 'bool' ? (c.dflt === 'true' ? 'true' : 'false') : c.dflt;
|
|
654
773
|
st.rows = [...st.rows, draftToRow(cols, blank)];
|
|
655
774
|
st.localError = '';
|
|
775
|
+
recordSnapshot(st);
|
|
656
776
|
api.emit('change', { value: st.rows, totals: rowTotals(cols, st.rows) });
|
|
657
777
|
paint(root, api.props());
|
|
658
778
|
const last = root.querySelectorAll<HTMLElement>(`[data-kit-index="${st.rows.length - 1}"][data-kit-cell]`);
|
|
@@ -671,17 +791,46 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
671
791
|
api.emit('invalid', { message: msg });
|
|
672
792
|
return;
|
|
673
793
|
}
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
794
|
+
// 先按引用定位;确认期间外部换了行数据(引用失效)时,按主键值重新定位(主键值为空或不唯一则放弃)
|
|
795
|
+
const locate = (): number => {
|
|
796
|
+
const byRef = st.rows.indexOf(row);
|
|
797
|
+
if (byRef >= 0) return byRef;
|
|
798
|
+
const kv = key !== '' ? textOf(row[key]) : '';
|
|
799
|
+
if (kv === '') return -1;
|
|
800
|
+
const hits: number[] = [];
|
|
801
|
+
st.rows.forEach((r, i) => { if (textOf(r[key]) === kv) hits.push(i); });
|
|
802
|
+
return hits.length === 1 ? hits[0]! : -1;
|
|
803
|
+
};
|
|
804
|
+
const doRemove = (): void => {
|
|
805
|
+
const at = locate();
|
|
806
|
+
if (at < 0 || !root.isConnected) return;
|
|
807
|
+
const live = api.props();
|
|
808
|
+
api.emit('remove', { key: textOf(st.rows[at]![key]), row: st.rows[at]!, index: at });
|
|
809
|
+
if (bool(live.auto_apply) || bool(live.staged) || str(live.edit_mode) === 'inline') {
|
|
810
|
+
st.rows = st.rows.filter((_, i) => i !== at);
|
|
811
|
+
st.rowsSig = incomingSig(live);
|
|
812
|
+
st.localError = '';
|
|
813
|
+
st.cellErrors = []; // 行下标整体位移,旧的单元格错误失效
|
|
814
|
+
st.cellErrSig = '';
|
|
815
|
+
recordSnapshot(st);
|
|
816
|
+
api.emit('change', { value: st.rows, totals: rowTotals(cols, st.rows) });
|
|
817
|
+
paint(root, live);
|
|
818
|
+
// 行表重绘后焦点会丢:落到同位置(或最后一行)的删除钮,没有行了就落到录入区第一格
|
|
819
|
+
const removes = root.querySelectorAll<HTMLElement>('[data-kit-action="remove"]');
|
|
820
|
+
(removes[Math.min(at, removes.length - 1)] ?? root.querySelector<HTMLElement>('[data-kit-field]'))?.focus();
|
|
821
|
+
}
|
|
822
|
+
};
|
|
823
|
+
// 有内容的行先自绘确认(danger),空行直接删;确认弹出期间重复点击不叠加
|
|
824
|
+
if (rowHasContent(cols, row)) {
|
|
825
|
+
if (removeAsking.has(root)) return;
|
|
826
|
+
removeAsking.add(root);
|
|
827
|
+
void askKitConfirm({ message: kitText('soma.kit.confirm_remove', undefined, root), tone: 'danger', at: root })
|
|
828
|
+
.then((ok) => { if (ok) doRemove(); })
|
|
829
|
+
.catch((err) => { console.error('[soma-kit] 删除行失败', err); }) // 不成为未处理的拒绝;确认框已关闭,行保持原样
|
|
830
|
+
.finally(() => { removeAsking.delete(root); });
|
|
831
|
+
return;
|
|
684
832
|
}
|
|
833
|
+
doRemove();
|
|
685
834
|
return;
|
|
686
835
|
}
|
|
687
836
|
if (act === 'add') {
|
|
@@ -707,6 +856,7 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
707
856
|
if (bool(p.auto_apply) || bool(p.staged)) {
|
|
708
857
|
st.rows = key === '' ? [...st.rows, row] : [...st.rows.filter((r) => textOf(r[key]) !== textOf(row[key])), row];
|
|
709
858
|
st.rowsSig = incomingSig(p);
|
|
859
|
+
recordSnapshot(st);
|
|
710
860
|
api.emit('change', { value: st.rows, totals: rowTotals(cols, st.rows) });
|
|
711
861
|
resetDraft(st, cols);
|
|
712
862
|
paint(root, api.props());
|
|
@@ -725,11 +875,18 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
725
875
|
event.preventDefault();
|
|
726
876
|
add.click();
|
|
727
877
|
};
|
|
878
|
+
// 表单把 aria-invalid 也标在组件根上(文档顺序里先于单元格):根被聚焦时转交给首个出错单元格
|
|
879
|
+
const onRootFocus = (event: Event): void => {
|
|
880
|
+
if (event.target !== root) return;
|
|
881
|
+
root.querySelector<HTMLElement>('[data-kit-row] [aria-invalid="true"]')?.focus({ preventScroll: true });
|
|
882
|
+
};
|
|
883
|
+
root.addEventListener('focus', onRootFocus);
|
|
728
884
|
root.addEventListener('input', onInput);
|
|
729
885
|
root.addEventListener('change', onInput);
|
|
730
886
|
root.addEventListener('click', onClick);
|
|
731
887
|
root.addEventListener('keydown', onKeydown);
|
|
732
888
|
return () => {
|
|
889
|
+
root.removeEventListener('focus', onRootFocus);
|
|
733
890
|
root.removeEventListener('input', onInput);
|
|
734
891
|
root.removeEventListener('change', onInput);
|
|
735
892
|
root.removeEventListener('click', onClick);
|
|
@@ -746,9 +903,50 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
746
903
|
render(root, p) {
|
|
747
904
|
paint(root, p);
|
|
748
905
|
},
|
|
749
|
-
}),
|
|
906
|
+
})),
|
|
750
907
|
};
|
|
751
908
|
|
|
909
|
+
/** 入参 cell_errors -> 本地:入参签名变了就整体采用(含空列表);表单在字段出错期间每次都按当前草稿重算后下传,入参即真相 */
|
|
910
|
+
function adoptCellErrors(st: EditorState, raw: unknown): void {
|
|
911
|
+
const list: KitRowCellError[] = [];
|
|
912
|
+
if (Array.isArray(raw)) {
|
|
913
|
+
for (const e of raw) {
|
|
914
|
+
const r = recOf(e);
|
|
915
|
+
const row = Number(r.row);
|
|
916
|
+
if (Number.isInteger(row) && row >= 0 && typeof r.col === 'string') list.push({ row, col: r.col, message: textOf(r.message) });
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
const sig = JSON.stringify(list);
|
|
920
|
+
if (sig === st.cellErrSig) return;
|
|
921
|
+
st.cellErrSig = sig;
|
|
922
|
+
st.cellErrors = list;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/** 把 st.cellErrors 画到单元格:控件 aria-invalid、外框令牌色环、单元格 title 给出消息(不加行高);无错的格还原 */
|
|
926
|
+
function applyCellErrors(root: HTMLElement, st: EditorState): void {
|
|
927
|
+
const bad = new Map<string, string>();
|
|
928
|
+
for (const e of st.cellErrors) bad.set(`${e.row}\u0000${e.col}`, e.message);
|
|
929
|
+
const tokens = CELL_ERROR_CLASS.split(' ');
|
|
930
|
+
root.querySelectorAll<HTMLElement>('[data-kit-cell]').forEach((control) => {
|
|
931
|
+
const message = bad.get(`${control.getAttribute('data-kit-index')}\u0000${control.getAttribute('data-kit-cell')}`);
|
|
932
|
+
// 自绘下拉的载体是 hidden input,可见且可聚焦的是同壳的 combobox
|
|
933
|
+
const visible = control.closest<HTMLElement>('[data-kit-select-host]')?.querySelector<HTMLElement>('[role="combobox"]') ?? control;
|
|
934
|
+
const wrap = control.closest<HTMLElement>('[data-kit-row] > span');
|
|
935
|
+
if (message !== undefined) {
|
|
936
|
+
visible.setAttribute('aria-invalid', 'true');
|
|
937
|
+
wrap?.classList.add(...tokens);
|
|
938
|
+
wrap?.setAttribute('title', message);
|
|
939
|
+
} else {
|
|
940
|
+
visible.removeAttribute('aria-invalid');
|
|
941
|
+
wrap?.classList.remove(...tokens);
|
|
942
|
+
wrap?.removeAttribute('title');
|
|
943
|
+
}
|
|
944
|
+
});
|
|
945
|
+
// 根要能接住表单的 focus() 再转交给出错单元格
|
|
946
|
+
if (st.cellErrors.length > 0) root.setAttribute('tabindex', '-1');
|
|
947
|
+
else root.removeAttribute('tabindex');
|
|
948
|
+
}
|
|
949
|
+
|
|
752
950
|
function showError(root: HTMLElement, st: EditorState, external: string): void {
|
|
753
951
|
const text = st.localError !== '' ? st.localError : external;
|
|
754
952
|
const err = st.nodes?.err;
|
|
@@ -782,6 +980,7 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
782
980
|
holder = field.el;
|
|
783
981
|
} else {
|
|
784
982
|
const input = mk('input', c.mono || c.kind === 'json' ? `${FIELD_CLASS.md} ${TEXTAREA_LIKE}` : isNumericRowKind(c.kind) ? `${FIELD_CLASS.md} ${NUM_ALIGN}` : FIELD_CLASS.md);
|
|
983
|
+
setKitFace(input, 'field');
|
|
785
984
|
input.type = 'text';
|
|
786
985
|
if (isNumericRowKind(c.kind)) input.inputMode = 'decimal';
|
|
787
986
|
input.placeholder = c.placeholder;
|
|
@@ -793,7 +992,7 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
793
992
|
}
|
|
794
993
|
control.setAttribute('data-kit-cell', c.key);
|
|
795
994
|
control.setAttribute('data-kit-index', String(index));
|
|
796
|
-
return holder;
|
|
995
|
+
return withOptionsRetry(holder, c);
|
|
797
996
|
}
|
|
798
997
|
|
|
799
998
|
/** 行内编辑的合计行:随单格改动就地刷新数字(不重建行表,输入焦点不丢) */
|
|
@@ -808,6 +1007,15 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
808
1007
|
|
|
809
1008
|
/** 行内多行编辑的行表:每行每格就地可改;readonly / disabled 时退回纯文字;fixed_rows 时没有添加 / 删除 */
|
|
810
1009
|
function paintInlineList(root: HTMLElement, st: EditorState, p: Record<string, unknown>, list: HTMLElement, cols: readonly KitRowColumn[], readonly: boolean, fixedRows: boolean): void {
|
|
1010
|
+
let focusRestore: { index: string; cell: string; start: number | null; end: number | null } | null = null;
|
|
1011
|
+
const active = typeof document !== 'undefined' ? document.activeElement : null;
|
|
1012
|
+
if (active instanceof HTMLInputElement || active instanceof HTMLTextAreaElement) {
|
|
1013
|
+
const idx = active.getAttribute('data-kit-index');
|
|
1014
|
+
const cell = active.getAttribute('data-kit-cell');
|
|
1015
|
+
if (idx !== null && cell !== null && list.contains(active)) {
|
|
1016
|
+
focusRestore = { index: idx, cell, start: active.selectionStart, end: active.selectionEnd };
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
811
1019
|
st.cellEmbeds.release(list);
|
|
812
1020
|
list.textContent = '';
|
|
813
1021
|
const shown = cols.filter((c) => c.list);
|
|
@@ -868,6 +1076,17 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
868
1076
|
bar.appendChild(actionButton({ key: 'add_row', label: str(p.add_label, kitText('soma.kit.rows_add_row', undefined, root)), tone: 'neutral', disabled: false, reason: '' }));
|
|
869
1077
|
list.appendChild(bar);
|
|
870
1078
|
}
|
|
1079
|
+
if (focusRestore !== null) {
|
|
1080
|
+
const target = list.querySelector<HTMLInputElement | HTMLTextAreaElement>(
|
|
1081
|
+
`[data-kit-index="${focusRestore.index}"][data-kit-cell="${focusRestore.cell}"]`,
|
|
1082
|
+
);
|
|
1083
|
+
if (target) {
|
|
1084
|
+
target.focus();
|
|
1085
|
+
if (focusRestore.start !== null && focusRestore.end !== null) {
|
|
1086
|
+
try { target.setSelectionRange(focusRestore.start, focusRestore.end); } catch {}
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
871
1090
|
}
|
|
872
1091
|
|
|
873
1092
|
function paint(root: HTMLElement, p: Record<string, unknown>): void {
|
|
@@ -888,12 +1107,22 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
888
1107
|
// 本地有未同步变更:服务端数据换新时不覆盖工作副本(否则会悄悄丢掉用户的编辑),只换基线并提示「已在别处更新」
|
|
889
1108
|
stg.base = cloneRows(next);
|
|
890
1109
|
stg.stale = true;
|
|
891
|
-
} else
|
|
892
|
-
|
|
893
|
-
st.rows
|
|
894
|
-
|
|
895
|
-
if (
|
|
896
|
-
|
|
1110
|
+
} else {
|
|
1111
|
+
const nextJson = JSON.stringify(next);
|
|
1112
|
+
const currJson = JSON.stringify(st.rows);
|
|
1113
|
+
const isEcho = given !== null && (nextJson === currJson || st.emittedSnapshots.includes(nextJson));
|
|
1114
|
+
if (isEcho) {
|
|
1115
|
+
// 父级回灌的值等于本地当前行或任一刚发出的滞后快照(回声):视为滞后回灌,不冲掉本地编辑,不重建 DOM
|
|
1116
|
+
const idx = st.emittedSnapshots.indexOf(nextJson);
|
|
1117
|
+
if (idx >= 0) st.emittedSnapshots.splice(0, idx + 1);
|
|
1118
|
+
} else {
|
|
1119
|
+
// 真正外部换新才更新本地行与触发重建
|
|
1120
|
+
st.emittedSnapshots = [];
|
|
1121
|
+
st.rows = next;
|
|
1122
|
+
st.listSig = '';
|
|
1123
|
+
if (st.pendingClear) { st.pendingClear = false; resetDraft(st, cols); }
|
|
1124
|
+
if (stg !== null) { stg.base = cloneRows(next); stg.stale = false; }
|
|
1125
|
+
}
|
|
897
1126
|
}
|
|
898
1127
|
}
|
|
899
1128
|
if (bool(p.staged)) {
|
|
@@ -1025,6 +1254,7 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
1025
1254
|
holder = field.el;
|
|
1026
1255
|
} else {
|
|
1027
1256
|
const input = mk('input', c.mono || c.kind === 'json' ? `${FIELD_CLASS.md} ${TEXTAREA_LIKE}` : isNumericRowKind(c.kind) ? `${FIELD_CLASS.md} ${NUM_ALIGN}` : FIELD_CLASS.md);
|
|
1257
|
+
setKitFace(input, 'field');
|
|
1028
1258
|
input.type = 'text';
|
|
1029
1259
|
if (isNumericRowKind(c.kind)) input.inputMode = 'decimal';
|
|
1030
1260
|
input.placeholder = c.placeholder;
|
|
@@ -1033,7 +1263,7 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
1033
1263
|
control = input;
|
|
1034
1264
|
}
|
|
1035
1265
|
control.setAttribute('data-kit-field', c.key);
|
|
1036
|
-
wrap.appendChild(holder ?? control);
|
|
1266
|
+
wrap.appendChild(withOptionsRetry(holder ?? control, c));
|
|
1037
1267
|
grid.appendChild(wrap);
|
|
1038
1268
|
}
|
|
1039
1269
|
const last = mk('div', `${FIELD_WRAP} self-end`);
|
|
@@ -1044,6 +1274,9 @@ export function createRowEditor(): SomaComponentDefinition {
|
|
|
1044
1274
|
form.hidden = noEntry || !cols.some((c) => c.editable);
|
|
1045
1275
|
}
|
|
1046
1276
|
showError(root, st, textOf(data.error));
|
|
1277
|
+
adoptCellErrors(st, p.cell_errors);
|
|
1278
|
+
if (!inline) { st.cellErrors = []; st.cellErrSig = ''; }
|
|
1279
|
+
applyCellErrors(root, st);
|
|
1047
1280
|
refreshStage(root, p);
|
|
1048
1281
|
// 忙闲通道:所接动作(add / remove / change)在进行中时添加、删除钮禁用并挂 aria-busy,结束后恢复(只还原自己置的禁用)
|
|
1049
1282
|
const busy = kitBusyOf(p);
|