soma-kit 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +539 -2
- package/bin/soma-kit.mjs +27 -0
- package/package.json +66 -4
- package/packs/ai/agent-run-card.ts +64 -0
- package/packs/ai/evaluation-card.ts +51 -0
- package/packs/ai/guardrail-policy-card.ts +48 -0
- package/packs/ai/index.ts +31 -0
- package/packs/ai/model-card.ts +55 -0
- package/packs/ai/model-routing-card.ts +49 -0
- package/packs/ai/prompt-template-card.ts +49 -0
- package/packs/ai/texts.ts +19 -0
- package/packs/commerce/address-card.ts +58 -0
- package/packs/commerce/coupon-card.ts +64 -0
- package/packs/commerce/index.ts +25 -0
- package/packs/commerce/order-card.ts +60 -0
- package/packs/commerce/purchase-order-card.ts +74 -0
- package/packs/commerce/texts.ts +14 -0
- package/packs/finance/index.ts +30 -0
- package/packs/finance/invoice-summary-card.ts +48 -0
- package/packs/finance/reconcile-board.ts +450 -0
- package/packs/finance/reconcile-case-card.ts +70 -0
- package/packs/finance/reconcile-desk/compile.ts +208 -0
- package/packs/finance/reconcile-desk/index.ts +20 -0
- package/packs/finance/reconcile-desk/shell.ts +27 -0
- package/packs/finance/texts.ts +21 -0
- package/packs/ops/change-window-card.ts +54 -0
- package/packs/ops/dependency-status-card.ts +50 -0
- package/packs/ops/error-budget-card.ts +60 -0
- package/packs/ops/execution-task-runner.ts +61 -0
- package/packs/ops/index.ts +36 -0
- package/packs/ops/integration-status-card.ts +45 -0
- package/packs/ops/release-readiness-card.ts +63 -0
- package/packs/ops/runbook-execution-card.ts +81 -0
- package/packs/ops/texts.ts +21 -0
- package/packs/security/control-evidence-card.ts +54 -0
- package/packs/security/finding-card.ts +96 -0
- package/packs/security/index.ts +29 -0
- package/packs/security/texts.ts +31 -0
- package/packs/shared/card-util.ts +60 -0
- package/packs/shared/wrap.ts +13 -0
- package/spec/generated/kit-app.schema.json +84 -0
- package/spec/generated/kit-page.schema.json +2766 -0
- package/spec/kit-grammar.json +1358 -0
- package/spec/kit-grammar.schema.json +716 -0
- package/src/app/app-config.ts +108 -0
- package/src/app/compose-apps.ts +139 -0
- package/src/app/define-kit-actions.ts +89 -0
- package/src/app/define-kit-app.ts +362 -0
- package/src/app/define-kit-pack.ts +114 -0
- package/src/app/envelope.ts +117 -0
- package/src/app/index.ts +50 -0
- package/src/app/notify-renderer.ts +114 -0
- package/src/app/paged-list-adapter.ts +246 -0
- package/src/app/server-pages.ts +303 -0
- package/src/app/server.ts +244 -0
- package/src/app/submit.types.ts +87 -0
- package/src/base/action-busy.ts +159 -0
- package/src/base/action-gate.ts +267 -0
- package/src/base/action-status.ts +31 -0
- package/src/base/alert.ts +136 -0
- package/src/base/avatar.ts +271 -0
- package/src/base/button.ts +139 -0
- package/src/base/cascader.ts +523 -0
- package/src/base/check-core.ts +214 -0
- package/src/base/checkbox.ts +35 -0
- package/src/base/choice.ts +183 -0
- package/src/base/color-picker.ts +316 -0
- package/src/base/combobox.ts +61 -0
- package/src/base/command-palette.ts +371 -0
- package/src/base/context-menu.ts +103 -0
- package/src/base/countdown.ts +168 -0
- package/src/base/date-range.ts +444 -0
- package/src/base/date.ts +899 -0
- package/src/base/divider.ts +156 -0
- package/src/base/fab.ts +192 -0
- package/src/base/icons.ts +106 -0
- package/src/base/image.ts +177 -0
- package/src/base/index.ts +214 -0
- package/src/base/input.ts +316 -0
- package/src/base/kbd.ts +99 -0
- package/src/base/link.ts +167 -0
- package/src/base/listbox-core.ts +965 -0
- package/src/base/mentions.ts +323 -0
- package/src/base/menu-button.ts +240 -0
- package/src/base/menu-core.ts +341 -0
- package/src/base/month-grid.ts +171 -0
- package/src/base/motion/aura.ts +94 -0
- package/src/base/motion/hover-3d.ts +154 -0
- package/src/base/motion/hover-gallery.ts +185 -0
- package/src/base/motion/index.ts +27 -0
- package/src/base/motion/limits.ts +75 -0
- package/src/base/motion/marquee.ts +157 -0
- package/src/base/motion/text-rotate.ts +195 -0
- package/src/base/number.ts +121 -0
- package/src/base/otp.ts +259 -0
- package/src/base/permission-gate.ts +99 -0
- package/src/base/popover.ts +352 -0
- package/src/base/popup.ts +542 -0
- package/src/base/portal-core.ts +297 -0
- package/src/base/price-text.ts +126 -0
- package/src/base/progress.ts +216 -0
- package/src/base/qr-code.ts +446 -0
- package/src/base/qty-stepper.ts +241 -0
- package/src/base/radio.ts +33 -0
- package/src/base/range.ts +444 -0
- package/src/base/rating.ts +249 -0
- package/src/base/segmented.ts +286 -0
- package/src/base/select.ts +57 -0
- package/src/base/shared.ts +708 -0
- package/src/base/swap.ts +86 -0
- package/src/base/switch.ts +111 -0
- package/src/base/tag-input.ts +464 -0
- package/src/base/tag.ts +250 -0
- package/src/base/text.ts +126 -0
- package/src/base/textarea.ts +129 -0
- package/src/base/time-picker.ts +606 -0
- package/src/base/tree-select.ts +629 -0
- package/src/base/upload.ts +548 -0
- package/src/base/virtual-list.ts +286 -0
- package/src/biz/approval-desk/compile.ts +179 -0
- package/src/biz/approval-desk/index.ts +21 -0
- package/src/biz/approval-desk/shell.ts +50 -0
- package/src/biz/auth-panel/compile.ts +196 -0
- package/src/biz/auth-panel/index.ts +24 -0
- package/src/biz/auth-panel/shell.ts +46 -0
- package/src/biz/category-crud/compile.ts +173 -0
- package/src/biz/category-crud/index.ts +19 -0
- package/src/biz/category-crud/shell.ts +29 -0
- package/src/biz/checkout-page/compile.ts +111 -0
- package/src/biz/checkout-page/index.ts +20 -0
- package/src/biz/checkout-page/shell.ts +50 -0
- package/src/biz/compose.ts +313 -0
- package/src/biz/data-components.ts +26 -0
- package/src/biz/define.ts +23 -0
- package/src/biz/import-wizard/compile.ts +204 -0
- package/src/biz/import-wizard/hub.ts +540 -0
- package/src/biz/import-wizard/index.ts +24 -0
- package/src/biz/import-wizard/shell.ts +161 -0
- package/src/biz/index.ts +80 -0
- package/src/biz/landing-page/compile.ts +67 -0
- package/src/biz/landing-page/index.ts +20 -0
- package/src/biz/landing-page/shell.ts +48 -0
- package/src/biz/link/hub.ts +255 -0
- package/src/biz/link/shared.ts +493 -0
- package/src/biz/master-sub/compile.ts +232 -0
- package/src/biz/master-sub/index.ts +20 -0
- package/src/biz/master-sub/shell.ts +56 -0
- package/src/biz/multi-board/compile.ts +234 -0
- package/src/biz/multi-board/index.ts +19 -0
- package/src/biz/multi-board/shell.ts +70 -0
- package/src/biz/names.ts +30 -0
- package/src/biz/notification-center/compile.ts +138 -0
- package/src/biz/notification-center/index.ts +29 -0
- package/src/biz/notification-center/shell.ts +42 -0
- package/src/biz/page-templates/result-page/compile.ts +46 -0
- package/src/biz/page-templates/result-page/index.ts +19 -0
- package/src/biz/page-templates/result-page/shell.ts +31 -0
- package/src/biz/page-templates/shared.ts +121 -0
- package/src/biz/page-templates/sign-in-page/compile.ts +176 -0
- package/src/biz/page-templates/sign-in-page/index.ts +18 -0
- package/src/biz/page-templates/sign-in-page/shell.ts +116 -0
- package/src/biz/record-detail/compile.ts +290 -0
- package/src/biz/record-detail/index.ts +22 -0
- package/src/biz/record-detail/shell.ts +64 -0
- package/src/biz/settings-center/compile.ts +100 -0
- package/src/biz/settings-center/index.ts +18 -0
- package/src/biz/settings-center/shell.ts +51 -0
- package/src/biz/shared/action-host.ts +469 -0
- package/src/biz/shared/coordinator.ts +516 -0
- package/src/biz/shared/index.ts +9 -0
- package/src/biz/shared/pipes.ts +58 -0
- package/src/biz/shared/runtime-hub.ts +112 -0
- package/src/biz/shared/step-values.ts +25 -0
- package/src/biz/single-form/compile.ts +146 -0
- package/src/biz/single-form/index.ts +18 -0
- package/src/biz/single-form/shell.ts +38 -0
- package/src/biz/standard-crud/bridge.ts +11 -0
- package/src/biz/standard-crud/compile.ts +546 -0
- package/src/biz/standard-crud/index.ts +18 -0
- package/src/biz/standard-crud/shell.ts +157 -0
- package/src/biz/step-form/compile.ts +400 -0
- package/src/biz/step-form/hub.ts +412 -0
- package/src/biz/step-form/index.ts +23 -0
- package/src/biz/step-form/shell.ts +96 -0
- package/src/biz/step-form/steps-ref.ts +112 -0
- package/src/biz/types.ts +96 -0
- package/src/cli/kit-cli.ts +102 -0
- package/src/compile/action-host-attach.ts +76 -0
- package/src/compile/compile-page.ts +472 -0
- package/src/compile/component-candidates.ts +147 -0
- package/src/compile/fragment.ts +104 -0
- package/src/compile/hints.ts +255 -0
- package/src/compile/index.ts +16 -0
- package/src/compile/locate.ts +166 -0
- package/src/compile/page-params.ts +80 -0
- package/src/compile/parse.ts +355 -0
- package/src/compile/redirect.ts +90 -0
- package/src/compile/registry.ts +30 -0
- package/src/compile/remove-guard.ts +99 -0
- package/src/compile/styles.ts +244 -0
- package/src/compile/types.ts +106 -0
- package/src/data/amount-breakdown.ts +132 -0
- package/src/data/anchor-nav.ts +225 -0
- package/src/data/approval-flow.ts +269 -0
- package/src/data/assignment-picker.ts +652 -0
- package/src/data/async-result-panel.ts +308 -0
- package/src/data/breadcrumbs.ts +197 -0
- package/src/data/calendar.ts +363 -0
- package/src/data/carousel.ts +408 -0
- package/src/data/chart.ts +726 -0
- package/src/data/checklist.ts +215 -0
- package/src/data/code-view.ts +415 -0
- package/src/data/comment-thread.ts +342 -0
- package/src/data/confirm-dialog.ts +144 -0
- package/src/data/context-menu.ts +216 -0
- package/src/data/d9-limits.ts +42 -0
- package/src/data/d9-util.ts +81 -0
- package/src/data/diff.ts +239 -0
- package/src/data/display-util.ts +135 -0
- package/src/data/dock.ts +152 -0
- package/src/data/embed.ts +200 -0
- package/src/data/field-mapper.ts +544 -0
- package/src/data/flow-editor-contract.ts +41 -0
- package/src/data/flow-editor.ts +2622 -0
- package/src/data/flow-remote-options.ts +257 -0
- package/src/data/footer.ts +157 -0
- package/src/data/form-draft-registry.ts +76 -0
- package/src/data/gantt.ts +430 -0
- package/src/data/hero.ts +149 -0
- package/src/data/image-viewer.ts +333 -0
- package/src/data/index.ts +268 -0
- package/src/data/item-list.ts +335 -0
- package/src/data/kanban.ts +667 -0
- package/src/data/media-grid.ts +246 -0
- package/src/data/mega-menu.ts +257 -0
- package/src/data/menu-dom.ts +169 -0
- package/src/data/menu.ts +464 -0
- package/src/data/mockup.ts +176 -0
- package/src/data/modal-dialog.ts +182 -0
- package/src/data/navbar.ts +292 -0
- package/src/data/object-form.ts +1981 -0
- package/src/data/options-bus.ts +24 -0
- package/src/data/outcome-watch.ts +40 -0
- package/src/data/overlay-portal.ts +6 -0
- package/src/data/pager.ts +473 -0
- package/src/data/permission-matrix.ts +432 -0
- package/src/data/permission-tree.ts +916 -0
- package/src/data/pricing-table.ts +269 -0
- package/src/data/prompt-dialog.ts +235 -0
- package/src/data/reaction-bar.ts +236 -0
- package/src/data/record-card.ts +413 -0
- package/src/data/record-picker.ts +489 -0
- package/src/data/redirect.ts +109 -0
- package/src/data/remote-options.ts +54 -0
- package/src/data/request-console.ts +399 -0
- package/src/data/resident.ts +58 -0
- package/src/data/result-panel.ts +160 -0
- package/src/data/rich-text.ts +626 -0
- package/src/data/row-editor.ts +1066 -0
- package/src/data/rule-builder.ts +1137 -0
- package/src/data/shape-util.ts +221 -0
- package/src/data/staged-rows.ts +69 -0
- package/src/data/stat-group.ts +231 -0
- package/src/data/static-lazy.ts +63 -0
- package/src/data/static-registry.ts +227 -0
- package/src/data/steps.ts +195 -0
- package/src/data/survey.ts +455 -0
- package/src/data/system-switcher.ts +243 -0
- package/src/data/timeline.ts +204 -0
- package/src/data/tour-guide.ts +392 -0
- package/src/data/transfer.ts +597 -0
- package/src/data/tree-check.ts +30 -0
- package/src/data/tree-table.ts +674 -0
- package/src/data/tree.ts +656 -0
- package/src/data/trend-line.ts +147 -0
- package/src/data/virtual-rows.ts +556 -0
- package/src/data/watermark.ts +140 -0
- package/src/data/wrap.ts +267 -0
- package/src/data/wrapped-candidates.ts +96 -0
- package/src/frames/blank-frame.ts +19 -0
- package/src/frames/index.ts +44 -0
- package/src/frames/page-bar.ts +185 -0
- package/src/frames/page-header.ts +185 -0
- package/src/frames/page-status.ts +54 -0
- package/src/frames/resize-handle.ts +147 -0
- package/src/frames/shared.ts +242 -0
- package/src/frames/single-frame.ts +16 -0
- package/src/frames/split-frame/index.ts +4 -0
- package/src/frames/split-frame/shell.ts +219 -0
- package/src/frames/split-frame/span.ts +64 -0
- package/src/frames/split-frame/wiring.ts +164 -0
- package/src/frames/tabs-frame/index.ts +3 -0
- package/src/frames/tabs-frame/shell.ts +112 -0
- package/src/frames/tabs-frame/wiring.ts +69 -0
- package/src/frames/top-main-frame.ts +20 -0
- package/src/frames/types.ts +72 -0
- package/src/frames/workspace-frame/extras.ts +197 -0
- package/src/frames/workspace-frame/index.ts +13 -0
- package/src/frames/workspace-frame/shell.ts +282 -0
- package/src/frames/workspace-frame/state.ts +152 -0
- package/src/frames/workspace-frame/view.ts +1013 -0
- package/src/generated/kit-grammar-runtime.ts +2822 -0
- package/src/generated/page-origin.ts +57 -0
- package/src/i18n/index.ts +22 -0
- package/src/i18n/kit-text.ts +692 -0
- package/src/i18n/resolve-text-refs.ts +128 -0
- package/src/index.ts +6 -0
- package/src/object-renderer/action-gate.ts +216 -0
- package/src/object-renderer/adopt-gate.ts +333 -0
- package/src/object-renderer/arrange.ts +411 -0
- package/src/object-renderer/candidates.ts +68 -0
- package/src/object-renderer/collapse-wiring.ts +99 -0
- package/src/object-renderer/collection-expand.ts +185 -0
- package/src/object-renderer/collection-parts.ts +653 -0
- package/src/object-renderer/collection-table.ts +503 -0
- package/src/object-renderer/collection.ts +628 -0
- package/src/object-renderer/compile-data.ts +549 -0
- package/src/object-renderer/defaults.ts +252 -0
- package/src/object-renderer/display-checks.ts +153 -0
- package/src/object-renderer/emit-events.ts +498 -0
- package/src/object-renderer/emit-object.ts +98 -0
- package/src/object-renderer/emit-static.ts +1036 -0
- package/src/object-renderer/field-policy.ts +89 -0
- package/src/object-renderer/field-value.ts +581 -0
- package/src/object-renderer/form-detail.ts +116 -0
- package/src/object-renderer/index.ts +13 -0
- package/src/object-renderer/linkage.ts +371 -0
- package/src/object-renderer/merge-class.ts +126 -0
- package/src/object-renderer/mode.ts +25 -0
- package/src/object-renderer/outcome-draft.ts +61 -0
- package/src/object-renderer/parts.ts +136 -0
- package/src/object-renderer/pick-component.ts +36 -0
- package/src/object-renderer/render-object.ts +724 -0
- package/src/object-renderer/request-actions.ts +581 -0
- package/src/object-renderer/skeleton.ts +101 -0
- package/src/object-renderer/table-actions.ts +438 -0
- package/src/object-renderer/table-summary.ts +105 -0
- package/src/object-renderer/table-tools.ts +1072 -0
- package/src/object-renderer/types.ts +936 -0
- package/src/page-target/ai-surface.ts +154 -0
- package/src/page-target/compile.ts +354 -0
- package/src/page-target/drift.ts +186 -0
- package/src/page-target/index.ts +25 -0
- package/src/page-target/intent-map.ts +137 -0
- package/src/page-target/intent-schema.ts +711 -0
- package/src/page-target/origin.ts +17 -0
- package/src/page-target/target.ts +450 -0
- package/src/page-target/writer-policy.ts +315 -0
- package/src/theme/block-theme.ts +202 -0
- package/src/theme/kit-theme-text.ts +2 -0
- package/src/theme/kit-theme.css +275 -0
- package/src/theme/kit-theme.tpl +275 -0
- package/src/theme/theme-tokens.ts +135 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// EmojiReactionBar:表情回应条(layer: data,写死形状,accept: list)。
|
|
2
|
+
//
|
|
3
|
+
// 数据:`data` 是 [{ emoji, count, mine?, label? }]:每个表情一枚药丸(表情 + 计数),`mine` 为真是「我已回应」态(主色底,aria-pressed)。
|
|
4
|
+
// 点药丸抛 `toggle { emoji, on, count }`(on = 点完之后我的回应态);组件内先乐观翻转(mine 与计数 ±1),数据再次下行(内容变)即以数据为准。
|
|
5
|
+
// `addable` 为真时末尾出「+」钮,点开表情选择层(候选读 kit-grammar#dataLimits.reactions.palette,可由 `palette` prop 换);
|
|
6
|
+
// 选一个抛 `add { emoji }`(已存在的表情等同点它的药丸)。选择层 Esc 关闭并回焦「+」,点外面关闭,方向键在候选间移动。
|
|
7
|
+
// 表情种类上限同真源 maxKinds。
|
|
8
|
+
|
|
9
|
+
import type { SomaComponentControllerContext, SomaComponentDefinition } from 'soma-core';
|
|
10
|
+
import { bool, joinClass, num, str } from '../base/shared';
|
|
11
|
+
import { kitKey, kitText } from '../i18n/kit-text';
|
|
12
|
+
import { classTokens, mk, rowsOf, textOf } from './shape-util';
|
|
13
|
+
import { REACTION_MAX_KINDS, REACTION_PALETTE } from './d9-limits';
|
|
14
|
+
import { attrEsc } from './d9-util';
|
|
15
|
+
import { KIT_DROPDOWN_CLASS_CANDIDATES, dropdownOwnerOf, queryWithDropdowns, releaseDropdownsIn, syncDropdown } from '../base/popup';
|
|
16
|
+
|
|
17
|
+
const ROOT_CLASS = 'relative flex flex-wrap items-center gap-2 text-sm text-fg';
|
|
18
|
+
const PILL_CLASS = 'inline-flex h-7 items-center gap-1 rounded-full border border-border bg-surface px-2.5 text-xs text-fg transition-colors hover:bg-surface-sunken active:opacity-80 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
19
|
+
const PILL_MINE = 'inline-flex h-7 items-center gap-1 rounded-full border border-primary bg-primary-soft px-2.5 text-xs font-medium text-primary-on-soft transition-colors hover:opacity-80 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
20
|
+
const ADD_CLASS = 'inline-flex size-7 items-center justify-center rounded-full border border-dashed border-border bg-surface text-sm text-fg-muted transition-colors hover:bg-surface-sunken focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
21
|
+
const PANEL_BASE_CLASS = 'absolute z-20 flex flex-wrap gap-1 rounded-lg border border-border-subtle bg-surface-raised p-2 shadow-lg';
|
|
22
|
+
const PANEL_CLASS = `${PANEL_BASE_CLASS} start-0 top-full mt-1`;
|
|
23
|
+
const CHOICE_CLASS = 'inline-flex size-8 items-center justify-center rounded-md text-base transition-colors hover:bg-surface-sunken focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
24
|
+
|
|
25
|
+
export const KIT_REACTION_BAR_CLASS_CANDIDATES: readonly string[] = classTokens(
|
|
26
|
+
ROOT_CLASS, PILL_CLASS, PILL_MINE, ADD_CLASS, PANEL_BASE_CLASS, PANEL_CLASS, CHOICE_CLASS, KIT_DROPDOWN_CLASS_CANDIDATES,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
export interface KitReaction {
|
|
30
|
+
emoji: string;
|
|
31
|
+
count: number;
|
|
32
|
+
mine: boolean;
|
|
33
|
+
label: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 源数据 -> 回应(纯函数):emoji 为空的丢弃,同表情合并计数;种类超上限截断 */
|
|
37
|
+
export function reactionsOf(data: unknown): KitReaction[] {
|
|
38
|
+
const out = new Map<string, KitReaction>();
|
|
39
|
+
for (const raw of rowsOf(data)) {
|
|
40
|
+
const emoji = textOf(raw.emoji);
|
|
41
|
+
if (emoji === '') continue;
|
|
42
|
+
const count = Math.max(0, Math.floor(num(raw.count) ?? 0));
|
|
43
|
+
const mine = raw.mine === true || raw.mine === 'true';
|
|
44
|
+
const hit = out.get(emoji);
|
|
45
|
+
if (hit) { hit.count += count; hit.mine = hit.mine || mine; } else out.set(emoji, { emoji, count, mine, label: textOf(raw.label) });
|
|
46
|
+
}
|
|
47
|
+
return [...out.values()].slice(0, REACTION_MAX_KINDS);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function createEmojiReactionBar(): SomaComponentDefinition {
|
|
51
|
+
return {
|
|
52
|
+
contract: {
|
|
53
|
+
name: 'EmojiReactionBar',
|
|
54
|
+
layer: 'data',
|
|
55
|
+
props: {
|
|
56
|
+
data: { type: 'array', default: [], desc: '回应 [{ emoji, count, mine?, label? }]' },
|
|
57
|
+
addable: { type: 'boolean', default: false, desc: '末尾出「+」钮,可添加回应' },
|
|
58
|
+
palette: { type: 'array', default: [], desc: '添加层的候选表情;空则取真源默认候选' },
|
|
59
|
+
add_text: { type: 'string', default: '', desc: '「+」钮的无障碍名称;缺省取内置文案(按页面语言)' },
|
|
60
|
+
disabled: { type: 'boolean', default: false, desc: '只读:不抛事件' },
|
|
61
|
+
class: { type: 'string', default: '', desc: '外部传入的类,追加在根节点类串末尾' },
|
|
62
|
+
},
|
|
63
|
+
emits: {
|
|
64
|
+
toggle: { payload: { emoji: { type: 'string' }, on: { type: 'boolean' }, count: { type: 'number' } }, desc: '点已有表情;on = 我的回应态,count = 点后的计数' },
|
|
65
|
+
add: { payload: { emoji: { type: 'string' } }, desc: '从添加层选了一个新表情' },
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
templateHtml: `<div data-soma-id="root" class="${ROOT_CLASS}"></div>`,
|
|
69
|
+
controllerFactory: (ctx: SomaComponentControllerContext) => {
|
|
70
|
+
const P = (): Record<string, unknown> => (ctx.readProps ? ctx.readProps() : ctx.props);
|
|
71
|
+
let root: HTMLElement | null = null;
|
|
72
|
+
let panelOpen = false;
|
|
73
|
+
let sig = '';
|
|
74
|
+
const flipped = new Map<string, boolean>();
|
|
75
|
+
let removeListeners: Array<() => void> = [];
|
|
76
|
+
// 添加层挂到浮层端口后不再是 root 的后代,root 上的委托收不到它的事件:重画出的面板元素直接绑这两个处理函数
|
|
77
|
+
let panelClick: EventListener | null = null;
|
|
78
|
+
let panelKeydown: EventListener | null = null;
|
|
79
|
+
|
|
80
|
+
const view = (): KitReaction[] => reactionsOf(P().data).map((r) => {
|
|
81
|
+
const f = flipped.get(r.emoji);
|
|
82
|
+
if (f === undefined || f === r.mine) return r;
|
|
83
|
+
return { ...r, mine: f, count: Math.max(0, r.count + (f ? 1 : -1)) };
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const palette = (): string[] => {
|
|
87
|
+
const given = Array.isArray(P().palette) ? (P().palette as unknown[]).map(textOf).filter((s) => s !== '') : [];
|
|
88
|
+
return given.length > 0 ? given : [...REACTION_PALETTE];
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const toggle = (emoji: string): void => {
|
|
92
|
+
if (bool(P().disabled)) return;
|
|
93
|
+
const cur = view().find((r) => r.emoji === emoji);
|
|
94
|
+
const base = reactionsOf(P().data).find((r) => r.emoji === emoji);
|
|
95
|
+
const on = !(cur?.mine ?? false);
|
|
96
|
+
if (base) flipped.set(emoji, on);
|
|
97
|
+
const next = view().find((r) => r.emoji === emoji);
|
|
98
|
+
ctx.emit('toggle', { emoji, on, count: next?.count ?? (on ? 1 : 0) });
|
|
99
|
+
paint();
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const closePanel = (focusAdd: boolean): void => {
|
|
103
|
+
if (!panelOpen) return;
|
|
104
|
+
panelOpen = false;
|
|
105
|
+
paint();
|
|
106
|
+
if (focusAdd) root?.querySelector<HTMLElement>('[data-kit-add]')?.focus();
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
function paint(): void {
|
|
110
|
+
if (!root) return;
|
|
111
|
+
const p = P();
|
|
112
|
+
const dataSig = JSON.stringify(reactionsOf(p.data));
|
|
113
|
+
if (dataSig !== sig) { sig = dataSig; flipped.clear(); }
|
|
114
|
+
root.className = joinClass(ROOT_CLASS, p.class);
|
|
115
|
+
// 整条重建会丢焦点(键盘点药丸 / 「+」后):记下焦点所在,重绘后落回同一个
|
|
116
|
+
const focused = root.contains(document.activeElement) ? document.activeElement as HTMLElement : null;
|
|
117
|
+
const focusEmoji = focused?.closest('[data-kit-reaction]')?.getAttribute('data-kit-reaction') ?? null;
|
|
118
|
+
const focusAdd = focused?.closest('[data-kit-add]') != null;
|
|
119
|
+
releaseDropdownsIn(root);
|
|
120
|
+
root.textContent = '';
|
|
121
|
+
const disabled = bool(p.disabled);
|
|
122
|
+
for (const r of view()) {
|
|
123
|
+
const b = mk('button', r.mine ? PILL_MINE : PILL_CLASS);
|
|
124
|
+
b.type = 'button';
|
|
125
|
+
b.setAttribute('data-kit-reaction', r.emoji);
|
|
126
|
+
b.setAttribute('aria-pressed', r.mine ? 'true' : 'false');
|
|
127
|
+
b.setAttribute('aria-label', `${r.label !== '' ? r.label : r.emoji} ${r.count}`);
|
|
128
|
+
if (disabled) { b.disabled = true; b.setAttribute('aria-disabled', 'true'); }
|
|
129
|
+
b.appendChild(mk('span', '', r.emoji));
|
|
130
|
+
b.appendChild(mk('span', '', String(r.count)));
|
|
131
|
+
root.appendChild(b);
|
|
132
|
+
}
|
|
133
|
+
if (bool(p.addable) && !disabled && view().length < REACTION_MAX_KINDS) {
|
|
134
|
+
const add = mk('button', ADD_CLASS, '+');
|
|
135
|
+
add.type = 'button';
|
|
136
|
+
add.setAttribute('data-kit-add', '');
|
|
137
|
+
add.setAttribute('aria-label', str(p.add_text, kitText('soma.kit.add_reaction', undefined, root)));
|
|
138
|
+
add.setAttribute('aria-haspopup', 'true');
|
|
139
|
+
add.setAttribute('aria-expanded', panelOpen ? 'true' : 'false');
|
|
140
|
+
root.appendChild(add);
|
|
141
|
+
if (panelOpen) {
|
|
142
|
+
const panel = mk('div', PANEL_CLASS);
|
|
143
|
+
panel.setAttribute('role', 'group');
|
|
144
|
+
panel.setAttribute('aria-label', str(p.add_text, kitText('soma.kit.add_reaction', undefined, root)));
|
|
145
|
+
panel.setAttribute('data-kit-panel', '');
|
|
146
|
+
palette().forEach((e, i) => {
|
|
147
|
+
const c = mk('button', CHOICE_CLASS, e);
|
|
148
|
+
c.type = 'button';
|
|
149
|
+
c.setAttribute('data-kit-choice', e);
|
|
150
|
+
c.tabIndex = i === 0 ? 0 : -1;
|
|
151
|
+
panel.appendChild(c);
|
|
152
|
+
});
|
|
153
|
+
root.appendChild(panel);
|
|
154
|
+
syncDropdown(panel, true, PANEL_BASE_CLASS, root);
|
|
155
|
+
if (!root.contains(panel)) {
|
|
156
|
+
if (panelClick) panel.addEventListener('click', panelClick);
|
|
157
|
+
if (panelKeydown) panel.addEventListener('keydown', panelKeydown);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (focusEmoji !== null) root.querySelector<HTMLElement>(`[data-kit-reaction="${attrEsc(focusEmoji)}"]`)?.focus();
|
|
162
|
+
else if (focusAdd) root.querySelector<HTMLElement>('[data-kit-add]')?.focus();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
onMount(): void {
|
|
167
|
+
root = ctx.host.querySelector<HTMLElement>('[data-soma-scoped-id="root"]')
|
|
168
|
+
?? ctx.host.querySelector<HTMLElement>('[data-soma-id="root"]')
|
|
169
|
+
?? ctx.host;
|
|
170
|
+
const r = root;
|
|
171
|
+
const onClick = (event: Event): void => {
|
|
172
|
+
const t = event.target as HTMLElement | null;
|
|
173
|
+
const pill = t?.closest<HTMLElement>('[data-kit-reaction]');
|
|
174
|
+
if (pill) { toggle(pill.getAttribute('data-kit-reaction') ?? ''); return; }
|
|
175
|
+
if (t?.closest('[data-kit-add]')) {
|
|
176
|
+
panelOpen = !panelOpen;
|
|
177
|
+
paint();
|
|
178
|
+
if (panelOpen) queryWithDropdowns(r, '[data-kit-choice]')?.focus();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const choice = t?.closest<HTMLElement>('[data-kit-choice]');
|
|
182
|
+
if (choice) {
|
|
183
|
+
const emoji = choice.getAttribute('data-kit-choice') ?? '';
|
|
184
|
+
panelOpen = false;
|
|
185
|
+
if (reactionsOf(P().data).some((x) => x.emoji === emoji)) toggle(emoji); else { paint(); ctx.emit('add', { emoji }); }
|
|
186
|
+
r.querySelector<HTMLElement>('[data-kit-add]')?.focus();
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const onKeydown = (event: KeyboardEvent): void => {
|
|
190
|
+
if (!panelOpen) return;
|
|
191
|
+
if (event.key === 'Escape') { event.preventDefault(); closePanel(true); return; }
|
|
192
|
+
const choices = [...(queryWithDropdowns(r, '[data-kit-panel]') ?? r).querySelectorAll<HTMLElement>('[data-kit-choice]')];
|
|
193
|
+
const at = choices.indexOf(document.activeElement as HTMLElement);
|
|
194
|
+
if (at < 0) return;
|
|
195
|
+
const arrow = kitKey(event.key, r);
|
|
196
|
+
const step = arrow === 'ArrowRight' || arrow === 'ArrowDown' ? 1 : arrow === 'ArrowLeft' || arrow === 'ArrowUp' ? -1 : 0;
|
|
197
|
+
const to = event.key === 'Home' ? 0 : event.key === 'End' ? choices.length - 1 : at + step;
|
|
198
|
+
if (step === 0 && event.key !== 'Home' && event.key !== 'End') return;
|
|
199
|
+
event.preventDefault();
|
|
200
|
+
const hit = choices[Math.min(choices.length - 1, Math.max(0, to))];
|
|
201
|
+
choices.forEach((c) => { c.tabIndex = c === hit ? 0 : -1; });
|
|
202
|
+
hit?.focus();
|
|
203
|
+
};
|
|
204
|
+
const onDocDown = (event: Event): void => {
|
|
205
|
+
const target = event.target as Node;
|
|
206
|
+
const owner = dropdownOwnerOf(target);
|
|
207
|
+
if (panelOpen && !r.contains(target) && !(owner !== null && r.contains(owner))) closePanel(false);
|
|
208
|
+
};
|
|
209
|
+
panelClick = onClick;
|
|
210
|
+
panelKeydown = onKeydown as EventListener;
|
|
211
|
+
r.addEventListener('click', onClick);
|
|
212
|
+
r.addEventListener('keydown', onKeydown as EventListener);
|
|
213
|
+
document.addEventListener('pointerdown', onDocDown);
|
|
214
|
+
removeListeners = [
|
|
215
|
+
() => r.removeEventListener('click', onClick),
|
|
216
|
+
() => r.removeEventListener('keydown', onKeydown as EventListener),
|
|
217
|
+
() => document.removeEventListener('pointerdown', onDocDown),
|
|
218
|
+
];
|
|
219
|
+
paint();
|
|
220
|
+
},
|
|
221
|
+
onPropsChange(): void {
|
|
222
|
+
paint();
|
|
223
|
+
},
|
|
224
|
+
onUnmount(): void {
|
|
225
|
+
for (const off of removeListeners) off();
|
|
226
|
+
removeListeners = [];
|
|
227
|
+
flipped.clear();
|
|
228
|
+
if (root) releaseDropdownsIn(root);
|
|
229
|
+
panelClick = null;
|
|
230
|
+
panelKeydown = null;
|
|
231
|
+
root = null;
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
// RecordCard:记录卡片壳(layer: data,写死形状,accept: single)。
|
|
2
|
+
//
|
|
3
|
+
// 头部:标题 + 副标题 + 状态位(status 文字 + tone 语气);主体:事实对(label / value)、标签、进度;底部:动作位。
|
|
4
|
+
// 数据:`data` 是一个对象 { title, subtitle?, status?, tone?, facts?, tags?, progress?, actions? };键名可由 `map` 改写。
|
|
5
|
+
// 事实对来源固定两选一(先 `fields` 后 `facts`,不做推断):
|
|
6
|
+
// - `fields` prop:[{ key, label, format? }],按 key 从 data 里取值(format 取 soma-foundation/values 写法);
|
|
7
|
+
// - 否则读 data[map.facts]:[{ label, value, format? }]。
|
|
8
|
+
// `rail` 为真时左侧一条语气色细条(语气取 tone)。`selectable` 为真时整卡可聚焦,点击 / Enter / Space 抛 `select { item }`。
|
|
9
|
+
// 底部动作点击抛 `action { key }`(只上抛意图)。进度用自绘 SVG 条(与 KitProgress 条形同形,role=progressbar,0–100)。
|
|
10
|
+
|
|
11
|
+
import type { SomaComponentDefinition } from 'soma-core';
|
|
12
|
+
import { bool, createKitController, formatValue, joinClass, num, str } from '../base/shared';
|
|
13
|
+
import {
|
|
14
|
+
actionButton, actionsOf, BADGE_BASE, BUTTON_CLASS_TOKENS, classTokens, keyMap, mk, recOf, rowsOf, shapeToneOf, textOf,
|
|
15
|
+
TONE_DOT, TONE_SOFT,
|
|
16
|
+
} from './shape-util';
|
|
17
|
+
import { safeImgSrc } from './d9-util';
|
|
18
|
+
import { kitText } from '../i18n/kit-text';
|
|
19
|
+
import type { SomaDataPort } from 'soma-core';
|
|
20
|
+
import { watchKitOutcome, type KitOutcome } from './outcome-watch';
|
|
21
|
+
import { pickPath } from '../object-renderer/adopt-gate';
|
|
22
|
+
import type { ShapeTone } from './shape-util';
|
|
23
|
+
|
|
24
|
+
export const KIT_RECORD_CARD_DEFAULT_MAP = Object.freeze({
|
|
25
|
+
title: 'title', subtitle: 'subtitle', status: 'status', tone: 'tone', facts: 'facts', tags: 'tags', progress: 'progress', actions: 'actions',
|
|
26
|
+
media: 'media', media_alt: 'media_alt', secret: 'secret',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// 外观三档(variant):outlined 描边(缺省)/ elevated 抬升(阴影,可点时悬停加深)/ filled 填充(无边无影)。
|
|
30
|
+
// 版式两种:无 media 时与原来一致(根即内容行,p-5);有 media 时根改竖排、顶部整幅图,内容行下沉到内层 div 自带 p-5。
|
|
31
|
+
export type KitRecordVariant = 'outlined' | 'elevated' | 'filled';
|
|
32
|
+
const VARIANT_CLASS: Readonly<Record<KitRecordVariant, string>> = Object.freeze({
|
|
33
|
+
outlined: 'border border-border-subtle bg-surface',
|
|
34
|
+
elevated: 'border border-transparent bg-surface-raised shadow-lg',
|
|
35
|
+
filled: 'border border-transparent bg-surface-sunken',
|
|
36
|
+
});
|
|
37
|
+
const VARIANT_INTERACT: Readonly<Record<KitRecordVariant, string>> = Object.freeze({
|
|
38
|
+
outlined: 'cursor-pointer transition-colors hover:bg-surface-sunken active:opacity-80 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring',
|
|
39
|
+
elevated: 'cursor-pointer transition-shadow hover:shadow-xl active:opacity-80 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring',
|
|
40
|
+
filled: 'cursor-pointer transition-colors hover:bg-surface-raised active:opacity-80 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring',
|
|
41
|
+
});
|
|
42
|
+
const LAYOUT_PLAIN = 'flex gap-3 rounded-lg p-5 text-sm text-fg';
|
|
43
|
+
const LAYOUT_MEDIA = 'flex flex-col overflow-hidden rounded-lg text-sm text-fg';
|
|
44
|
+
const BODY_ROW_CLASS = 'flex gap-3 p-5';
|
|
45
|
+
const MEDIA_CLASS = 'aspect-video w-full object-cover';
|
|
46
|
+
|
|
47
|
+
/** 根节点类串(纯函数):版式 + 外观档 + 可点时的交互态 */
|
|
48
|
+
export function recordRootClass(variant: KitRecordVariant, interactive: boolean, hasMedia: boolean): string {
|
|
49
|
+
return `${hasMedia ? LAYOUT_MEDIA : LAYOUT_PLAIN} ${VARIANT_CLASS[variant]}${interactive ? ` ${VARIANT_INTERACT[variant]}` : ''}`;
|
|
50
|
+
}
|
|
51
|
+
const VARIANTS: readonly KitRecordVariant[] = Object.freeze(['outlined', 'elevated', 'filled']);
|
|
52
|
+
const RAIL_CLASS = 'w-1 shrink-0 self-stretch rounded-full';
|
|
53
|
+
const MAIN_CLASS = 'flex flex-1 flex-col gap-3';
|
|
54
|
+
const HEAD_CLASS = 'flex items-start justify-between gap-2';
|
|
55
|
+
const HEAD_TEXT = 'flex flex-col gap-0.5';
|
|
56
|
+
const TITLE_CLASS = 'text-base font-semibold text-fg';
|
|
57
|
+
const SUBTITLE_CLASS = 'text-fg-muted';
|
|
58
|
+
const FACTS_CLASS = 'grid grid-cols-2 gap-x-4 gap-y-2';
|
|
59
|
+
const FACT_CLASS = 'flex flex-col gap-0.5';
|
|
60
|
+
const FACT_LABEL = 'text-xs font-medium text-fg-muted';
|
|
61
|
+
const FACT_VALUE = 'text-sm text-fg';
|
|
62
|
+
const TAGS_CLASS = 'flex flex-wrap gap-1';
|
|
63
|
+
const TAG_CLASS = 'rounded-full bg-surface-sunken px-2 py-0.5 text-xs font-medium text-fg-muted';
|
|
64
|
+
const PROGRESS_CLASS = 'block h-2 w-full overflow-hidden rounded-full';
|
|
65
|
+
const PROGRESS_TRACK_CLASS = 'fill-border-subtle';
|
|
66
|
+
const PROGRESS_FILL_CLASS = 'fill-primary';
|
|
67
|
+
const FOOT_CLASS = 'flex flex-wrap items-center justify-end gap-2 border-t border-border-subtle pt-3';
|
|
68
|
+
const FACT_ROW_CLASS = 'flex items-center gap-2';
|
|
69
|
+
const FACT_MONO_CLASS = 'min-w-0 break-all font-mono text-xs text-fg';
|
|
70
|
+
const SECRET_CLASS = 'flex flex-col gap-2 rounded-md border border-border-subtle bg-surface-sunken p-3';
|
|
71
|
+
const SECRET_VALUE_CLASS = 'm-0 whitespace-pre-wrap break-all font-mono text-xs text-fg';
|
|
72
|
+
const SECRET_NOTE_CLASS = 'text-xs text-fg-subtle';
|
|
73
|
+
const SECRET_BAR_CLASS = 'flex items-center justify-between gap-2';
|
|
74
|
+
|
|
75
|
+
export const KIT_RECORD_CARD_CLASS_CANDIDATES: readonly string[] = classTokens(
|
|
76
|
+
Object.values(VARIANT_CLASS), Object.values(VARIANT_INTERACT), LAYOUT_PLAIN, LAYOUT_MEDIA, BODY_ROW_CLASS, MEDIA_CLASS, RAIL_CLASS, MAIN_CLASS, HEAD_CLASS, HEAD_TEXT, TITLE_CLASS, SUBTITLE_CLASS, FACTS_CLASS, FACT_CLASS,
|
|
77
|
+
FACT_LABEL, FACT_VALUE, TAGS_CLASS, TAG_CLASS, PROGRESS_CLASS, PROGRESS_TRACK_CLASS, PROGRESS_FILL_CLASS, FOOT_CLASS, BADGE_BASE, BUTTON_CLASS_TOKENS,
|
|
78
|
+
FACT_ROW_CLASS, FACT_MONO_CLASS, SECRET_CLASS, SECRET_VALUE_CLASS, SECRET_NOTE_CLASS, SECRET_BAR_CLASS,
|
|
79
|
+
Object.values(TONE_SOFT), Object.values(TONE_DOT),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
export interface KitFact {
|
|
83
|
+
label: string;
|
|
84
|
+
value: string;
|
|
85
|
+
/** 等宽展示(地址、路径、命令) */
|
|
86
|
+
mono?: boolean;
|
|
87
|
+
/** 值旁出「复制」钮,抛 copy { key, ok } */
|
|
88
|
+
copy?: boolean;
|
|
89
|
+
/** 非空时值以语气徽标展示(状态类事实) */
|
|
90
|
+
tone?: ShapeTone;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** 事实对的展示修饰(只在写了才带键,不改既有形状) */
|
|
94
|
+
function factExtras(f: Record<string, unknown>): Pick<KitFact, 'mono' | 'copy' | 'tone'> {
|
|
95
|
+
const out: Pick<KitFact, 'mono' | 'copy' | 'tone'> = {};
|
|
96
|
+
if (f.mono === true) out.mono = true;
|
|
97
|
+
if (f.copy === true) out.copy = true;
|
|
98
|
+
if (f.tone !== undefined && f.tone !== null && f.tone !== '') out.tone = shapeToneOf(f.tone);
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** 事实对(纯函数,便于单测) */
|
|
103
|
+
export function recordFactsOf(data: Record<string, unknown>, factsKey: string, fieldsRaw: unknown, locale = ''): KitFact[] {
|
|
104
|
+
const out: KitFact[] = [];
|
|
105
|
+
const fields = rowsOf(fieldsRaw);
|
|
106
|
+
if (fields.length > 0) {
|
|
107
|
+
for (const f of fields) {
|
|
108
|
+
const key = textOf(f.key);
|
|
109
|
+
if (key === '') continue;
|
|
110
|
+
const v = data[key];
|
|
111
|
+
out.push({ label: textOf(f.label) || key, value: v === undefined || v === null || v === '' ? '' : formatValue(v, textOf(f.format), locale), ...factExtras(f) });
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
for (const f of rowsOf(data[factsKey])) {
|
|
116
|
+
const v = f.value;
|
|
117
|
+
out.push({ label: textOf(f.label), value: v === undefined || v === null || v === '' ? '' : formatValue(v, textOf(f.format), locale), ...factExtras(f) });
|
|
118
|
+
}
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function writeClipboard(text: string): Promise<boolean> {
|
|
123
|
+
try {
|
|
124
|
+
if (typeof navigator === 'undefined' || !navigator.clipboard) return false;
|
|
125
|
+
await navigator.clipboard.writeText(text);
|
|
126
|
+
return true;
|
|
127
|
+
} catch { return false; }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function createRecordCard(): SomaComponentDefinition {
|
|
131
|
+
// 已复制并清除的 secret 明文(按根):同一明文再下行也不再显示
|
|
132
|
+
const consumed = new WeakMap<HTMLElement, string>();
|
|
133
|
+
// outcome 来源的 secret(GC2):订阅某个 request 动作(into)的写入结果,成功响应里取明文;复制后组件侧清空持有的响应体
|
|
134
|
+
const outs = new WeakMap<HTMLElement, { name: string; off: (() => void) | null; outcome: KitOutcome | null; cleared: boolean; port?: SomaDataPort }>();
|
|
135
|
+
const ensureOutcome = (root: HTMLElement, p: Record<string, unknown>, services: Record<string, unknown> | undefined): void => {
|
|
136
|
+
const name = str(p.outcome);
|
|
137
|
+
let st = outs.get(root);
|
|
138
|
+
if (!st) { st = { name: '', off: null, outcome: null, cleared: false }; outs.set(root, st); }
|
|
139
|
+
st.port = services?.data as SomaDataPort | undefined;
|
|
140
|
+
if (name === st.name) return;
|
|
141
|
+
st.off?.();
|
|
142
|
+
st.off = null;
|
|
143
|
+
st.name = name;
|
|
144
|
+
st.outcome = null;
|
|
145
|
+
st.cleared = false;
|
|
146
|
+
if (name === '') return;
|
|
147
|
+
const cur = st;
|
|
148
|
+
let subscribing = true;
|
|
149
|
+
cur.off = watchKitOutcome(services?.data as SomaDataPort | undefined, name, (o) => {
|
|
150
|
+
cur.outcome = o;
|
|
151
|
+
// 用完即清后 core 回 idle:保持「已复制」态;有新一轮请求(非 idle)才复位
|
|
152
|
+
if (!(cur.cleared && o.status === 'idle')) cur.cleared = false;
|
|
153
|
+
if (!subscribing) renderCard(root, currentProps.get(root) ?? p);
|
|
154
|
+
});
|
|
155
|
+
subscribing = false;
|
|
156
|
+
};
|
|
157
|
+
const currentProps = new WeakMap<HTMLElement, Record<string, unknown>>();
|
|
158
|
+
/** 有效的 secret:配了 outcome 就只认请求结果(成功才有,进行中 / 失败 / 空闲都不显示,旧明文随新一轮请求消失);否则读 data.secret */
|
|
159
|
+
const secretOf = (root: HTMLElement, p: Record<string, unknown>, data: Record<string, unknown>, key: string): Record<string, unknown> => {
|
|
160
|
+
const st = outs.get(root);
|
|
161
|
+
if (st === undefined || st.name === '') return recOf(data[key]);
|
|
162
|
+
const o = st.outcome;
|
|
163
|
+
if (st.cleared && o !== null && (o.status === 'saved' || o.status === 'idle')) return { cleared: true, label: str(p.secret_label) };
|
|
164
|
+
if (o === null || o.status !== 'saved') return {};
|
|
165
|
+
const v = pickPath(o.data, str(p.secret_path));
|
|
166
|
+
return typeof v === 'string' && v !== '' ? { value: v, label: str(p.secret_label), hint: str(p.secret_hint) } : {};
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
contract: {
|
|
170
|
+
name: 'RecordCard',
|
|
171
|
+
layer: 'data',
|
|
172
|
+
props: {
|
|
173
|
+
data: { type: 'object', default: {}, desc: '一条记录 { title, subtitle?, status?, tone?, facts?, tags?, progress?, actions?, media?(顶部整幅图地址,经安全校验), media_alt? }' },
|
|
174
|
+
map: { type: 'object', default: {}, desc: '键名对照:title / subtitle / status / tone / facts / tags / progress / actions / media / media_alt' },
|
|
175
|
+
fields: { type: 'array', default: [], desc: '事实对来源 [{ key, label, format? }](按 key 从 data 取值);空则读 data 里的 facts 数组' },
|
|
176
|
+
rail: { type: 'boolean', default: false, desc: '左侧语气色细条(语气取 tone)' },
|
|
177
|
+
selectable: { type: 'boolean', default: false, desc: '整卡可聚焦可点,抛 select' },
|
|
178
|
+
clickable: { type: 'boolean', default: false, desc: '整卡点击(与 selectable 同一行为,语义是「点进去」而非「选中」):可聚焦、Enter / Space、抛 select { item };卡内按钮的点击不触发整卡' },
|
|
179
|
+
variant: { type: 'enum', values: ['outlined', 'elevated', 'filled'], default: 'outlined', desc: '外观档:outlined 描边 / elevated 阴影 / filled 填充' },
|
|
180
|
+
outcome: { type: 'string', default: '', desc: 'secret 取自某个 request 动作的结果(request.into 的名字):成功响应里按 secret_path 取明文;配了它 data.secret 不再生效;进行中 / 失败时不显示;点「复制并清除」后组件不再持有明文' },
|
|
181
|
+
secret_path: { type: 'string', default: '', desc: 'outcome 成功响应(接口返回体)里明文的点路径,如 data.token;空则整个响应体必须是字符串' },
|
|
182
|
+
secret_label: { type: 'string', default: '', desc: 'outcome 来源的 secret 小标题' },
|
|
183
|
+
secret_hint: { type: 'string', default: '', desc: 'outcome 来源的 secret 说明(如「只显示一次」)' },
|
|
184
|
+
secret_copy_label: { type: 'string', default: '', desc: 'data.secret 的「复制并清除」钮文字;缺省取内置文案(按页面语言)' },
|
|
185
|
+
secret_cleared_text: { type: 'string', default: '', desc: 'data.secret 复制后显示的文字;缺省取内置「已复制」' },
|
|
186
|
+
locale: { type: 'string', default: '', desc: '格式化语言;空则 zh-CN' },
|
|
187
|
+
class: { type: 'string', default: '', desc: '外部传入的类,追加在根节点类串末尾' },
|
|
188
|
+
},
|
|
189
|
+
emits: {
|
|
190
|
+
select: { payload: { item: { type: 'object' } }, desc: '点整卡(selectable 时);item 是源数据对象' },
|
|
191
|
+
action: { payload: { key: { type: 'string' } }, desc: '点底部动作' },
|
|
192
|
+
copy: { payload: { key: { type: 'string' }, ok: { type: 'boolean' } }, desc: '点事实对旁的复制钮;key = 该事实的 label(不带文本,避免凭据再次上行)' },
|
|
193
|
+
secret_copied: { payload: { ok: { type: 'boolean' } }, desc: 'data.secret 点「复制并清除」后抛;ok 为真时组件已从页面抹掉明文(同一明文不再显示),宿主应同时把数据里的 secret 清空' },
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
templateHtml: `<article data-soma-id="root" class="${recordRootClass('outlined', false, false)}"></article>`,
|
|
197
|
+
controllerFactory: createKitController({
|
|
198
|
+
attach(root, api) {
|
|
199
|
+
const pick = (): void => {
|
|
200
|
+
const p = api.props();
|
|
201
|
+
if (bool(p.selectable) || bool(p.clickable)) api.emit('select', { item: recOf(p.data) });
|
|
202
|
+
};
|
|
203
|
+
const onClick = (event: Event): void => {
|
|
204
|
+
const target = event.target as HTMLElement | null;
|
|
205
|
+
const copyBtn = target?.closest<HTMLButtonElement>('[data-kit-copy]');
|
|
206
|
+
if (copyBtn) {
|
|
207
|
+
const p = api.props();
|
|
208
|
+
const m = keyMap(KIT_RECORD_CARD_DEFAULT_MAP, p.map);
|
|
209
|
+
const data = recOf(p.data);
|
|
210
|
+
if (copyBtn.getAttribute('data-kit-copy') === 'secret') {
|
|
211
|
+
const value = textOf(secretOf(root, p, data, m.secret ?? 'secret').value);
|
|
212
|
+
if (value === '') return;
|
|
213
|
+
void writeClipboard(value).then((ok) => {
|
|
214
|
+
if (ok) {
|
|
215
|
+
consumed.set(root, value);
|
|
216
|
+
// outcome 来源:组件侧丢掉响应体里的明文,只留「已复制」状态(并调 services.data.clearOutcome 把 core 写入结果存储里的响应体也清掉)
|
|
217
|
+
const os = outs.get(root);
|
|
218
|
+
if (os !== undefined && os.name !== '' && os.outcome !== null) {
|
|
219
|
+
os.outcome = { ...os.outcome, data: undefined };
|
|
220
|
+
os.cleared = true;
|
|
221
|
+
// 同时把 core 写入结果存储里的响应体清掉(用完即清);清除会通知订阅者,订阅回调会把 cleared 复位,所以先清再重置标记
|
|
222
|
+
const port = os.port;
|
|
223
|
+
if (typeof port?.clearOutcome === 'function') {
|
|
224
|
+
port.clearOutcome(os.name);
|
|
225
|
+
os.outcome = { status: 'idle', message: '', data: undefined, seq: os.outcome.seq + 1 };
|
|
226
|
+
os.cleared = true;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
renderCard(root, api.props());
|
|
230
|
+
}
|
|
231
|
+
api.emit('secret_copied', { ok });
|
|
232
|
+
});
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const fact = recordFactsOf(data, m.facts, p.fields, str(p.locale))[Number(copyBtn.getAttribute('data-kit-copy'))];
|
|
236
|
+
if (!fact || fact.value === '') return;
|
|
237
|
+
void writeClipboard(fact.value).then((ok) => api.emit('copy', { key: fact.label, ok }));
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const btn = target?.closest<HTMLButtonElement>('[data-kit-action]');
|
|
241
|
+
if (btn) {
|
|
242
|
+
if (!btn.disabled) api.emit('action', { key: btn.getAttribute('data-kit-action') ?? '' });
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
pick();
|
|
246
|
+
};
|
|
247
|
+
const onKey = (event: KeyboardEvent): void => {
|
|
248
|
+
if (event.target !== root) return;
|
|
249
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
250
|
+
event.preventDefault();
|
|
251
|
+
pick();
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
root.addEventListener('click', onClick);
|
|
255
|
+
root.addEventListener('keydown', onKey as EventListener);
|
|
256
|
+
return () => {
|
|
257
|
+
root.removeEventListener('click', onClick);
|
|
258
|
+
root.removeEventListener('keydown', onKey as EventListener);
|
|
259
|
+
outs.get(root)?.off?.();
|
|
260
|
+
outs.delete(root);
|
|
261
|
+
currentProps.delete(root);
|
|
262
|
+
};
|
|
263
|
+
},
|
|
264
|
+
render(root, p, api) {
|
|
265
|
+
currentProps.set(root, p);
|
|
266
|
+
ensureOutcome(root, p, api.services);
|
|
267
|
+
renderCard(root, p);
|
|
268
|
+
},
|
|
269
|
+
}),
|
|
270
|
+
services: ['data'],
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
function renderCard(root: HTMLElement, p: Record<string, unknown>): void {
|
|
274
|
+
const m = keyMap(KIT_RECORD_CARD_DEFAULT_MAP, p.map);
|
|
275
|
+
const data = recOf(p.data);
|
|
276
|
+
const selectable = bool(p.selectable) || bool(p.clickable);
|
|
277
|
+
const tone = shapeToneOf(data[m.tone]);
|
|
278
|
+
const variant = VARIANTS.includes(p.variant as KitRecordVariant) ? p.variant as KitRecordVariant : 'outlined';
|
|
279
|
+
const mediaSrc = safeImgSrc(data[m.media]);
|
|
280
|
+
root.className = joinClass(recordRootClass(variant, selectable, mediaSrc !== ''), p.class);
|
|
281
|
+
if (selectable) {
|
|
282
|
+
root.setAttribute('role', 'button');
|
|
283
|
+
root.tabIndex = 0;
|
|
284
|
+
} else {
|
|
285
|
+
root.removeAttribute('role');
|
|
286
|
+
root.removeAttribute('tabindex');
|
|
287
|
+
}
|
|
288
|
+
root.textContent = '';
|
|
289
|
+
|
|
290
|
+
// 有 media:根竖排,顶部整幅图 + 下面内容行;无 media 时内容行就是根本身
|
|
291
|
+
let body: HTMLElement = root;
|
|
292
|
+
if (mediaSrc !== '') {
|
|
293
|
+
const img = mk('img', MEDIA_CLASS);
|
|
294
|
+
img.src = mediaSrc;
|
|
295
|
+
img.alt = textOf(data[m.media_alt]);
|
|
296
|
+
img.loading = 'lazy';
|
|
297
|
+
root.appendChild(img);
|
|
298
|
+
body = mk('div', BODY_ROW_CLASS);
|
|
299
|
+
root.appendChild(body);
|
|
300
|
+
}
|
|
301
|
+
if (bool(p.rail)) {
|
|
302
|
+
const rail = mk('span', `${RAIL_CLASS} ${TONE_DOT[tone]}`);
|
|
303
|
+
rail.setAttribute('aria-hidden', 'true');
|
|
304
|
+
body.appendChild(rail);
|
|
305
|
+
}
|
|
306
|
+
const main = mk('div', MAIN_CLASS);
|
|
307
|
+
|
|
308
|
+
const head = mk('div', HEAD_CLASS);
|
|
309
|
+
const text = mk('div', HEAD_TEXT);
|
|
310
|
+
text.appendChild(mk('h3', TITLE_CLASS, textOf(data[m.title])));
|
|
311
|
+
const sub = textOf(data[m.subtitle]);
|
|
312
|
+
if (sub !== '') text.appendChild(mk('p', SUBTITLE_CLASS, sub));
|
|
313
|
+
head.appendChild(text);
|
|
314
|
+
const status = textOf(data[m.status]);
|
|
315
|
+
if (status !== '') head.appendChild(mk('span', `${BADGE_BASE} ${TONE_SOFT[tone]}`, status));
|
|
316
|
+
main.appendChild(head);
|
|
317
|
+
|
|
318
|
+
const facts = recordFactsOf(data, m.facts, p.fields, str(p.locale));
|
|
319
|
+
if (facts.length > 0) {
|
|
320
|
+
const dl = mk('dl', FACTS_CLASS);
|
|
321
|
+
facts.forEach((f, i) => {
|
|
322
|
+
const cell = mk('div', FACT_CLASS);
|
|
323
|
+
cell.appendChild(mk('dt', FACT_LABEL, f.label));
|
|
324
|
+
const wrap = f.copy === true || f.tone !== undefined;
|
|
325
|
+
const dd = mk('dd', wrap ? FACT_ROW_CLASS : f.mono === true ? FACT_MONO_CLASS : FACT_VALUE);
|
|
326
|
+
if (wrap) {
|
|
327
|
+
dd.appendChild(f.tone !== undefined && f.value !== ''
|
|
328
|
+
? mk('span', `${BADGE_BASE} ${TONE_SOFT[f.tone]}`, f.value)
|
|
329
|
+
: mk('span', f.mono === true ? FACT_MONO_CLASS : FACT_VALUE, f.value));
|
|
330
|
+
} else dd.textContent = f.value;
|
|
331
|
+
if (f.copy === true && f.value !== '') {
|
|
332
|
+
const btn = actionButton({ key: 'copy', label: kitText('soma.kit.copy', undefined, root), tone: 'neutral', disabled: false, reason: '' });
|
|
333
|
+
btn.removeAttribute('data-kit-action');
|
|
334
|
+
btn.setAttribute('data-kit-copy', String(i));
|
|
335
|
+
btn.setAttribute('aria-label', `${kitText('soma.kit.copy', undefined, root)} ${f.label}`);
|
|
336
|
+
dd.appendChild(btn);
|
|
337
|
+
}
|
|
338
|
+
cell.appendChild(dd);
|
|
339
|
+
dl.appendChild(cell);
|
|
340
|
+
});
|
|
341
|
+
main.appendChild(dl);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// secret:一次性明文块(等宽 + 「复制并清除」;复制成功后组件自己抹掉,同一明文不再显示)
|
|
345
|
+
const secret = secretOf(root, p, data, m.secret ?? 'secret');
|
|
346
|
+
const secretValue = textOf(secret.value);
|
|
347
|
+
// outcome 来源复制后:组件已丢掉明文,只剩「已复制」状态块
|
|
348
|
+
const secretCleared = secret.cleared === true;
|
|
349
|
+
if (secretValue !== '' || secretCleared || textOf(secret.hint) !== '') {
|
|
350
|
+
const box = mk('div', SECRET_CLASS);
|
|
351
|
+
box.setAttribute('data-kit-secret', '');
|
|
352
|
+
const secretLabel = textOf(secret.label);
|
|
353
|
+
const gone = secretCleared || (secretValue !== '' && consumed.get(root) === secretValue);
|
|
354
|
+
if (secretLabel !== '' || secretValue !== '') {
|
|
355
|
+
const bar = mk('div', SECRET_BAR_CLASS);
|
|
356
|
+
bar.appendChild(mk('span', FACT_LABEL, secretLabel));
|
|
357
|
+
if (secretValue !== '' && !gone) {
|
|
358
|
+
const btn = actionButton({ key: 'copy', label: str(p.secret_copy_label, kitText('soma.kit.copy', undefined, root)), tone: 'neutral', disabled: false, reason: '' });
|
|
359
|
+
btn.removeAttribute('data-kit-action');
|
|
360
|
+
btn.setAttribute('data-kit-copy', 'secret');
|
|
361
|
+
bar.appendChild(btn);
|
|
362
|
+
}
|
|
363
|
+
box.appendChild(bar);
|
|
364
|
+
}
|
|
365
|
+
if (secretValue !== '' || secretCleared) {
|
|
366
|
+
box.appendChild(mk('pre', SECRET_VALUE_CLASS, gone ? str(p.secret_cleared_text, kitText('soma.kit.copied', undefined, root)) : secretValue));
|
|
367
|
+
}
|
|
368
|
+
const hint = textOf(secret.hint);
|
|
369
|
+
if (hint !== '') box.appendChild(mk('p', SECRET_NOTE_CLASS, hint));
|
|
370
|
+
main.appendChild(box);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const tagsRaw = data[m.tags];
|
|
374
|
+
if (Array.isArray(tagsRaw) && tagsRaw.length > 0) {
|
|
375
|
+
const tags = mk('div', TAGS_CLASS);
|
|
376
|
+
for (const t of tagsRaw) {
|
|
377
|
+
const label = t !== null && typeof t === 'object' ? textOf((t as Record<string, unknown>).label) : textOf(t);
|
|
378
|
+
if (label !== '') tags.appendChild(mk('span', TAG_CLASS, label));
|
|
379
|
+
}
|
|
380
|
+
main.appendChild(tags);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const progress = num(data[m.progress]);
|
|
384
|
+
if (progress !== null) {
|
|
385
|
+
const pct = Math.min(100, Math.max(0, progress));
|
|
386
|
+
const ns = 'http://www.w3.org/2000/svg';
|
|
387
|
+
const bar = document.createElementNS(ns, 'svg');
|
|
388
|
+
bar.setAttribute('class', PROGRESS_CLASS);
|
|
389
|
+
bar.setAttribute('role', 'progressbar');
|
|
390
|
+
bar.setAttribute('aria-valuemin', '0');
|
|
391
|
+
bar.setAttribute('aria-valuemax', '100');
|
|
392
|
+
bar.setAttribute('aria-valuenow', String(Math.round(pct)));
|
|
393
|
+
bar.setAttribute('aria-label', `${Math.round(pct)}%`);
|
|
394
|
+
const mkRect = (width: number, cls: string): SVGElement => {
|
|
395
|
+
const r = document.createElementNS(ns, 'rect');
|
|
396
|
+
r.setAttribute('x', '0'); r.setAttribute('y', '0'); r.setAttribute('height', '100%'); r.setAttribute('ry', '50%');
|
|
397
|
+
r.setAttribute('width', `${width}%`); r.setAttribute('class', cls);
|
|
398
|
+
return r;
|
|
399
|
+
};
|
|
400
|
+
bar.appendChild(mkRect(100, PROGRESS_TRACK_CLASS));
|
|
401
|
+
if (pct > 0) bar.appendChild(mkRect(pct, PROGRESS_FILL_CLASS));
|
|
402
|
+
main.appendChild(bar);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const actions = actionsOf(data[m.actions]);
|
|
406
|
+
if (actions.length > 0) {
|
|
407
|
+
const foot = mk('div', FOOT_CLASS);
|
|
408
|
+
for (const a of actions) foot.appendChild(actionButton(a));
|
|
409
|
+
main.appendChild(foot);
|
|
410
|
+
}
|
|
411
|
+
body.appendChild(main);
|
|
412
|
+
}
|
|
413
|
+
}
|