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,308 @@
|
|
|
1
|
+
// AsyncResultPanel:后台生成 / 分析的结果面板(layer: data,写死形状,accept: single)。
|
|
2
|
+
//
|
|
3
|
+
// 一件事:「给一句指令 -> 后台跑(可取消)-> 出一份待确认的结果 -> 采纳或忽略」。AI 整理、报告生成、批量预检等都是这个形状。
|
|
4
|
+
// 数据:`data` 是 { title?, subtitle?, status, message?, result? }:
|
|
5
|
+
// status:idle / busy / ready / failed。busy 时「运行」钮禁用并显示 busy_label,出现「取消」钮;message 是状态行(读屏 polite 播报)。
|
|
6
|
+
// result:{ title?, summary?, tone?, lines[{ label?, text }] },非空才出结果框;tone 取 neutral / info / success / warning / danger(默认 info)。
|
|
7
|
+
// adopt_label 非空出「采纳」钮,dismiss_label 非空出「忽略」钮。
|
|
8
|
+
// 指令输入(show_prompt,默认真)是组件内草稿,重画不丢焦点;change 时抛 `prompt_change { value }`,点运行抛 `run { prompt }`。
|
|
9
|
+
// 不改数据:运行 / 取消 / 采纳 / 忽略都只抛事件,状态由页面经 data 下行。
|
|
10
|
+
|
|
11
|
+
import type { SomaComponentDefinition, SomaDataPort } from 'soma-core';
|
|
12
|
+
import { bool, createKitController, joinClass, str } from '../base/shared';
|
|
13
|
+
import { watchKitOutcome, type KitOutcome } from './outcome-watch';
|
|
14
|
+
import { kitText } from '../i18n/kit-text';
|
|
15
|
+
import { KIT_BUSY_PROP_DEFS, cancelKitAction, kitBusyOf } from '../base/action-busy';
|
|
16
|
+
import {
|
|
17
|
+
actionButton, BADGE_BASE, BUTTON_CLASS_TOKENS, classTokens, mk, recOf, rowsOf, shapeToneOf, textOf, TONE_SOFT,
|
|
18
|
+
} from './shape-util';
|
|
19
|
+
|
|
20
|
+
const ROOT_CLASS = 'flex flex-col gap-3';
|
|
21
|
+
const HEAD_CLASS = 'flex flex-col gap-1';
|
|
22
|
+
const TITLE_CLASS = 'text-sm font-semibold text-fg';
|
|
23
|
+
const SUBTITLE_CLASS = 'text-xs text-fg-subtle';
|
|
24
|
+
const LABEL_WRAP = 'flex flex-col gap-1';
|
|
25
|
+
const LABEL_CLASS = 'text-xs font-medium text-fg-muted';
|
|
26
|
+
const AREA_CLASS = 'block w-full appearance-none autofill-clear resize-none rounded-md border border-border bg-surface px-3 py-2 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:opacity-60';
|
|
27
|
+
const BUTTONS_CLASS = 'flex flex-wrap items-center gap-2';
|
|
28
|
+
const MESSAGE_CLASS = 'text-xs text-fg-subtle';
|
|
29
|
+
const MESSAGE_FAILED = 'text-xs text-danger-on-soft';
|
|
30
|
+
const RESULT_BASE = 'flex flex-col gap-2 rounded-md px-3 py-2 text-sm';
|
|
31
|
+
const RESULT_TITLE = 'font-medium';
|
|
32
|
+
const LINE_CLASS = 'flex flex-wrap gap-1';
|
|
33
|
+
const LINE_LABEL = 'font-medium';
|
|
34
|
+
|
|
35
|
+
export const KIT_ASYNC_RESULT_CLASS_CANDIDATES: readonly string[] = classTokens(
|
|
36
|
+
ROOT_CLASS, HEAD_CLASS, TITLE_CLASS, SUBTITLE_CLASS, LABEL_WRAP, LABEL_CLASS, AREA_CLASS, BUTTONS_CLASS, MESSAGE_CLASS, MESSAGE_FAILED,
|
|
37
|
+
RESULT_BASE, RESULT_TITLE, LINE_CLASS, LINE_LABEL, BADGE_BASE, Object.values(TONE_SOFT), BUTTON_CLASS_TOKENS,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
export type KitAsyncStatus = 'idle' | 'busy' | 'ready' | 'failed';
|
|
41
|
+
|
|
42
|
+
export function asyncStatusOf(raw: unknown): KitAsyncStatus {
|
|
43
|
+
return raw === 'busy' || raw === 'ready' || raw === 'failed' ? raw : 'idle';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface KitAsyncResult {
|
|
47
|
+
title: string;
|
|
48
|
+
summary: string;
|
|
49
|
+
tone: ReturnType<typeof shapeToneOf>;
|
|
50
|
+
lines: Array<{ label: string; text: string }>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 结果(纯函数):没有任何可展示内容回 null */
|
|
54
|
+
export function asyncResultOf(raw: unknown): KitAsyncResult | null {
|
|
55
|
+
const r = recOf(raw);
|
|
56
|
+
const lines = rowsOf(r.lines).map((l) => ({ label: textOf(l.label), text: textOf(l.text) })).filter((l) => l.text !== '' || l.label !== '');
|
|
57
|
+
const title = textOf(r.title);
|
|
58
|
+
const summary = textOf(r.summary);
|
|
59
|
+
if (title === '' && summary === '' && lines.length === 0) return null;
|
|
60
|
+
return { title, summary, tone: shapeToneOf(r.tone, 'info'), lines };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 写入结果(声明式请求动作的 into)-> 面板的 data:进行中 = busy,成功 = ready(结果取自响应体),失败 / 冲突 / 被拦 = failed。
|
|
65
|
+
* 响应体里有 result / title / summary / lines 就按面板结果形状展示;都没有时把顶层标量键逐行列出(不猜业务含义)。
|
|
66
|
+
*/
|
|
67
|
+
export function outcomePanelData(o: KitOutcome): Record<string, unknown> {
|
|
68
|
+
switch (o.status) {
|
|
69
|
+
case 'pending': return { status: 'busy' };
|
|
70
|
+
case 'failed': case 'conflict': case 'held': return { status: 'failed', message: o.message };
|
|
71
|
+
case 'saved': return { status: 'ready', result: outcomeResultOf(o.data) };
|
|
72
|
+
default: return { status: 'idle' };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function outcomeResultOf(body: unknown): Record<string, unknown> {
|
|
77
|
+
const top = recOf(body);
|
|
78
|
+
const payload = recOf(top.data !== undefined && top.data !== null && typeof top.data === 'object' ? top.data : top);
|
|
79
|
+
const nested = recOf(payload.result);
|
|
80
|
+
const shaped = (r: Record<string, unknown>): boolean => r.lines !== undefined || r.title !== undefined || r.summary !== undefined;
|
|
81
|
+
if (shaped(nested)) return nested;
|
|
82
|
+
if (shaped(payload)) return payload;
|
|
83
|
+
const lines = Object.entries(payload)
|
|
84
|
+
.filter(([, v]) => v !== null && v !== undefined && typeof v !== 'object')
|
|
85
|
+
.map(([k, v]) => ({ label: k, text: String(v) }));
|
|
86
|
+
return { lines };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
interface PanelState {
|
|
90
|
+
/** outcome 订阅:范围名、退订、最近一次结果 */
|
|
91
|
+
outcomeName: string;
|
|
92
|
+
offOutcome: (() => void) | null;
|
|
93
|
+
outcome: KitOutcome | null;
|
|
94
|
+
area: HTMLTextAreaElement | null;
|
|
95
|
+
top: HTMLElement | null;
|
|
96
|
+
mid: HTMLElement | null;
|
|
97
|
+
bottom: HTMLElement | null;
|
|
98
|
+
promptVisible: boolean;
|
|
99
|
+
/** 常驻节点:按钮槽 / 播报区 / 结果槽(播报区不随重绘重建) */
|
|
100
|
+
body: HTMLElement | null;
|
|
101
|
+
btnSlot: HTMLElement | null;
|
|
102
|
+
msg: HTMLElement | null;
|
|
103
|
+
resSlot: HTMLElement | null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function createAsyncResultPanel(): SomaComponentDefinition {
|
|
107
|
+
const states = new WeakMap<HTMLElement, PanelState>();
|
|
108
|
+
const stateOf = (root: HTMLElement): PanelState => {
|
|
109
|
+
let s = states.get(root);
|
|
110
|
+
if (!s) { s = { outcomeName: '', offOutcome: null, outcome: null, area: null, top: null, mid: null, bottom: null, promptVisible: false, body: null, btnSlot: null, msg: null, resSlot: null }; states.set(root, s); }
|
|
111
|
+
return s;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
contract: {
|
|
116
|
+
name: 'AsyncResultPanel',
|
|
117
|
+
layer: 'data',
|
|
118
|
+
props: {
|
|
119
|
+
data: { type: 'object', default: {}, desc: '{ title?, subtitle?, status: idle|busy|ready|failed, message?, result?: { title?, summary?, tone?, lines[{ label?, text }] } }' },
|
|
120
|
+
outcome: { type: 'string', default: '', desc: '订阅声明式请求动作的 into 范围名(request.into):进行中 -> busy,成功 -> ready(结果取自响应体:有 result / title / summary / lines 按其展示,否则逐行列出顶层标量键),失败 -> failed;写了它就以动作结果为准(data 的 title / subtitle 仍生效)' },
|
|
121
|
+
show_prompt: { type: 'boolean', default: true, desc: '显示指令输入框' },
|
|
122
|
+
prompt_label: { type: 'string', default: '', desc: '指令输入框标签' },
|
|
123
|
+
prompt_placeholder: { type: 'string', default: '', desc: '指令输入框占位' },
|
|
124
|
+
prompt_rows: { type: 'number', default: 2, desc: '指令输入框行数' },
|
|
125
|
+
prompt_value: { type: 'string', default: '', desc: '指令初值(只在它自己变化时覆盖草稿)' },
|
|
126
|
+
run_label: { type: 'string', default: '', desc: '运行钮文字;缺省取内置文案(按页面语言)' },
|
|
127
|
+
busy_label: { type: 'string', default: '', desc: 'busy 时运行钮文字;缺省取内置文案(按页面语言)' },
|
|
128
|
+
cancel_label: { type: 'string', default: '', desc: 'busy 时取消钮文字;缺省取内置文案(按页面语言)' },
|
|
129
|
+
adopt_label: { type: 'string', default: '', desc: '采纳钮文字,空则不出' },
|
|
130
|
+
dismiss_label: { type: 'string', default: '', desc: '忽略钮文字,空则不出' },
|
|
131
|
+
run_disabled: { type: 'boolean', default: false, desc: '运行钮禁用(前置条件未满足)' },
|
|
132
|
+
show_run: { type: 'boolean', default: true, desc: '显示运行钮;纯展示动作结果(outcome)时关掉,busy 时仍有取消钮' },
|
|
133
|
+
...KIT_BUSY_PROP_DEFS,
|
|
134
|
+
class: { type: 'string', default: '', desc: '外部传入的类,追加在根节点类串末尾' },
|
|
135
|
+
},
|
|
136
|
+
emits: {
|
|
137
|
+
run: { payload: { prompt: { type: 'string' } }, desc: '点运行' },
|
|
138
|
+
cancel: { payload: {}, desc: 'busy 时点取消' },
|
|
139
|
+
adopt: { payload: {}, desc: '点采纳' },
|
|
140
|
+
dismiss: { payload: {}, desc: '点忽略' },
|
|
141
|
+
prompt_change: { payload: { value: { type: 'string' } }, desc: '指令输入框 change' },
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
services: ['data'],
|
|
145
|
+
templateHtml: `<section data-soma-id="root" class="${ROOT_CLASS}"></section>`,
|
|
146
|
+
controllerFactory: createKitController({
|
|
147
|
+
busyAware: true,
|
|
148
|
+
attach(root, api) {
|
|
149
|
+
const onClick = (event: Event): void => {
|
|
150
|
+
const btn = (event.target as HTMLElement | null)?.closest<HTMLButtonElement>('[data-kit-action]');
|
|
151
|
+
if (!btn || btn.disabled) return;
|
|
152
|
+
const key = btn.getAttribute('data-kit-action');
|
|
153
|
+
if (key === 'run') api.emit('run', { prompt: stateOf(root).area?.value ?? '' });
|
|
154
|
+
else if (key === 'cancel' || key === 'adopt' || key === 'dismiss') {
|
|
155
|
+
// 所接 request 动作声明了 cancelable:真正中止在途请求(没声明则只发 cancel 事件,行为同前)
|
|
156
|
+
if (key === 'cancel') cancelKitAction(str(api.props().busy_action, ''));
|
|
157
|
+
api.emit(key, {});
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
const onChange = (event: Event): void => {
|
|
161
|
+
const el = event.target as HTMLTextAreaElement | null;
|
|
162
|
+
if (el && el.hasAttribute('data-kit-prompt')) api.emit('prompt_change', { value: el.value });
|
|
163
|
+
};
|
|
164
|
+
root.addEventListener('click', onClick);
|
|
165
|
+
root.addEventListener('change', onChange);
|
|
166
|
+
return () => {
|
|
167
|
+
root.removeEventListener('click', onClick);
|
|
168
|
+
root.removeEventListener('change', onChange);
|
|
169
|
+
states.get(root)?.offOutcome?.();
|
|
170
|
+
states.delete(root);
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
render: function renderPanel(root, p, api) {
|
|
174
|
+
const st = stateOf(root);
|
|
175
|
+
// outcome:订阅写入结果(名字变了才重订);回调只在订阅之后的变化里重绘,订阅当下的那次回调在本次绘制里直接用
|
|
176
|
+
const outName = str(p.outcome);
|
|
177
|
+
if (outName !== st.outcomeName) {
|
|
178
|
+
st.offOutcome?.();
|
|
179
|
+
st.offOutcome = null;
|
|
180
|
+
st.outcomeName = outName;
|
|
181
|
+
st.outcome = null;
|
|
182
|
+
if (outName !== '') {
|
|
183
|
+
let subscribing = true;
|
|
184
|
+
st.offOutcome = watchKitOutcome(api.services?.data as SomaDataPort | undefined, outName, (o) => {
|
|
185
|
+
st.outcome = o;
|
|
186
|
+
if (!subscribing) renderPanel(root, api.props(), api);
|
|
187
|
+
});
|
|
188
|
+
subscribing = false;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const data = outName !== '' && st.outcome !== null ? { ...recOf(p.data), ...outcomePanelData(st.outcome) } : recOf(p.data);
|
|
192
|
+
// 已取消:回到空闲并如实写一行状态(不是失败,不留结果)
|
|
193
|
+
if (outName !== '' && st.outcome?.status === 'cancelled') data.message = kitText('soma.kit.request_cancelled', undefined, root);
|
|
194
|
+
const status = asyncStatusOf(data.status);
|
|
195
|
+
const busy = status === 'busy';
|
|
196
|
+
const showPrompt = bool(p.show_prompt);
|
|
197
|
+
root.className = joinClass(ROOT_CLASS, p.class);
|
|
198
|
+
|
|
199
|
+
if (!st.top || !st.mid || !st.bottom) {
|
|
200
|
+
root.textContent = '';
|
|
201
|
+
st.top = mk('div', '');
|
|
202
|
+
st.mid = mk('div', '');
|
|
203
|
+
st.bottom = mk('div', '');
|
|
204
|
+
root.append(st.top, st.mid, st.bottom);
|
|
205
|
+
}
|
|
206
|
+
const top = st.top;
|
|
207
|
+
const mid = st.mid;
|
|
208
|
+
const bottom = st.bottom;
|
|
209
|
+
|
|
210
|
+
// 标题
|
|
211
|
+
top.textContent = '';
|
|
212
|
+
const head = mk('div', HEAD_CLASS);
|
|
213
|
+
if (textOf(data.title) !== '') head.appendChild(mk('h3', TITLE_CLASS, textOf(data.title)));
|
|
214
|
+
if (textOf(data.subtitle) !== '') head.appendChild(mk('p', SUBTITLE_CLASS, textOf(data.subtitle)));
|
|
215
|
+
top.appendChild(head);
|
|
216
|
+
top.hidden = head.childNodes.length === 0;
|
|
217
|
+
|
|
218
|
+
// 指令输入:只建一次(保住焦点与草稿)
|
|
219
|
+
if (showPrompt !== st.promptVisible || (showPrompt && !st.area)) {
|
|
220
|
+
mid.textContent = '';
|
|
221
|
+
st.area = null;
|
|
222
|
+
st.promptVisible = showPrompt;
|
|
223
|
+
if (showPrompt) {
|
|
224
|
+
const wrap = mk('label', LABEL_WRAP);
|
|
225
|
+
if (str(p.prompt_label) !== '') wrap.appendChild(mk('span', LABEL_CLASS, str(p.prompt_label)));
|
|
226
|
+
const area = mk('textarea', AREA_CLASS);
|
|
227
|
+
area.setAttribute('data-kit-prompt', '');
|
|
228
|
+
area.value = str(p.prompt_value);
|
|
229
|
+
area.setAttribute('data-kit-given', str(p.prompt_value));
|
|
230
|
+
wrap.appendChild(area);
|
|
231
|
+
mid.appendChild(wrap);
|
|
232
|
+
st.area = area;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
mid.hidden = !showPrompt;
|
|
236
|
+
if (st.area) {
|
|
237
|
+
st.area.rows = Math.max(1, Math.floor(Number(p.prompt_rows) || 2));
|
|
238
|
+
st.area.placeholder = str(p.prompt_placeholder);
|
|
239
|
+
st.area.disabled = busy;
|
|
240
|
+
const given = str(p.prompt_value);
|
|
241
|
+
if (st.area.getAttribute('data-kit-given') !== given) {
|
|
242
|
+
st.area.setAttribute('data-kit-given', given);
|
|
243
|
+
st.area.value = given;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// 按钮 + 状态 + 结果(每次重画,不含需要保焦点的控件)
|
|
248
|
+
// 播报区(role=status)建一次、常驻:每次重绘只改它的文字与类,不新建节点,读屏才稳定播报
|
|
249
|
+
if (!st.body || !st.btnSlot || !st.msg || !st.resSlot) {
|
|
250
|
+
bottom.textContent = '';
|
|
251
|
+
st.body = mk('div', LABEL_WRAP);
|
|
252
|
+
st.btnSlot = mk('div', '');
|
|
253
|
+
st.msg = mk('p', MESSAGE_CLASS);
|
|
254
|
+
st.msg.setAttribute('role', 'status');
|
|
255
|
+
st.msg.setAttribute('aria-live', 'polite');
|
|
256
|
+
st.msg.hidden = true;
|
|
257
|
+
st.resSlot = mk('div', '');
|
|
258
|
+
st.body.append(st.btnSlot, st.msg, st.resSlot);
|
|
259
|
+
bottom.appendChild(st.body);
|
|
260
|
+
}
|
|
261
|
+
const body = st.resSlot;
|
|
262
|
+
const actionBusy = kitBusyOf(p);
|
|
263
|
+
const buttons = mk('div', BUTTONS_CLASS);
|
|
264
|
+
if (p.show_run !== false) {
|
|
265
|
+
buttons.appendChild(actionButton({
|
|
266
|
+
key: 'run',
|
|
267
|
+
label: busy || actionBusy ? str(p.busy_label, kitText('soma.kit.running', undefined, root)) : str(p.run_label, kitText('soma.kit.run', undefined, root)),
|
|
268
|
+
tone: 'primary',
|
|
269
|
+
disabled: busy || actionBusy || bool(p.run_disabled),
|
|
270
|
+
reason: '',
|
|
271
|
+
}));
|
|
272
|
+
if (actionBusy) (buttons.firstElementChild as HTMLElement).setAttribute('aria-busy', 'true');
|
|
273
|
+
}
|
|
274
|
+
if (busy) buttons.appendChild(actionButton({ key: 'cancel', label: str(p.cancel_label, kitText('soma.kit.cancel', undefined, root)), tone: 'neutral', disabled: false, reason: '' }));
|
|
275
|
+
st.btnSlot.replaceChildren(buttons);
|
|
276
|
+
const message = textOf(data.message);
|
|
277
|
+
const msgEl = st.msg;
|
|
278
|
+
const msgClass = status === 'failed' ? MESSAGE_FAILED : MESSAGE_CLASS;
|
|
279
|
+
if (msgEl.className !== msgClass) msgEl.className = msgClass;
|
|
280
|
+
if (msgEl.textContent !== message) msgEl.textContent = message;
|
|
281
|
+
msgEl.hidden = message === '';
|
|
282
|
+
body.textContent = '';
|
|
283
|
+
const result = asyncResultOf(data.result);
|
|
284
|
+
if (result && !busy) {
|
|
285
|
+
const box = mk('div', `${RESULT_BASE} ${TONE_SOFT[result.tone]}`);
|
|
286
|
+
box.setAttribute('data-kit-result', '');
|
|
287
|
+
if (result.title !== '') box.appendChild(mk('span', RESULT_TITLE, result.title));
|
|
288
|
+
if (result.summary !== '') box.appendChild(mk('span', '', result.summary));
|
|
289
|
+
for (const l of result.lines) {
|
|
290
|
+
const line = mk('span', LINE_CLASS);
|
|
291
|
+
if (l.label !== '') line.appendChild(mk('span', LINE_LABEL, `${l.label}:`));
|
|
292
|
+
line.appendChild(mk('span', '', l.text));
|
|
293
|
+
box.appendChild(line);
|
|
294
|
+
}
|
|
295
|
+
const adopt = str(p.adopt_label);
|
|
296
|
+
const dismiss = str(p.dismiss_label);
|
|
297
|
+
if (adopt !== '' || dismiss !== '') {
|
|
298
|
+
const row = mk('div', BUTTONS_CLASS);
|
|
299
|
+
if (adopt !== '') row.appendChild(actionButton({ key: 'adopt', label: adopt, tone: 'primary', disabled: false, reason: '' }));
|
|
300
|
+
if (dismiss !== '') row.appendChild(actionButton({ key: 'dismiss', label: dismiss, tone: 'neutral', disabled: false, reason: '' }));
|
|
301
|
+
box.appendChild(row);
|
|
302
|
+
}
|
|
303
|
+
body.appendChild(box);
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
}),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// Breadcrumbs:面包屑(layer: data,写死形状,accept: list)。
|
|
2
|
+
//
|
|
3
|
+
// 数据:`data` 是路径 [{ id?, label, href?, icon? }],末项是当前页(aria-current="page",不可点)。
|
|
4
|
+
// 超长折叠:层级多于 max_items(缺省 5,小于 3 视为不折叠)时,中段折成一个「…」钮(可访问名列出被折的层级),点它展开全部;
|
|
5
|
+
// overflow = scroll 则不折叠、整行横向滚动。分隔符 marker = text 用 separator(缺省 `/`),chevron 用方向符。
|
|
6
|
+
// 项上写 inert: true 则只画文字、不可点(菜单分组名等)。点前序层级抛 `select { item }`;有 href 的画成 <a>(地址过 safeHref)并同样抛 select。
|
|
7
|
+
// 纯函数 `breadcrumbParts` / `breadcrumbsHtml` 也给页面框的静态页头用(编译期直出,不挂组件)。
|
|
8
|
+
|
|
9
|
+
import type { SomaComponentDefinition } from 'soma-core';
|
|
10
|
+
import { KIT_STATIC_COMPONENT_LIMITS } from 'soma-foundation/kit-limits';
|
|
11
|
+
import { createKitController, joinClass, num, str } from '../base/shared';
|
|
12
|
+
import { safeHref } from '../base/link';
|
|
13
|
+
import { kitText } from '../i18n/kit-text';
|
|
14
|
+
import { kitIcon, kitIconHtml } from '../base/icons';
|
|
15
|
+
import { classSet, FOCUS, h, isRec, recList, textOf, type Child, type Rec } from './menu-dom';
|
|
16
|
+
|
|
17
|
+
const ROOT_CLASS = 'flex min-w-0 items-center text-sm text-fg-muted';
|
|
18
|
+
const ROOT_SCROLL_CLASS = 'flex min-w-0 items-center overflow-x-auto text-sm text-fg-muted';
|
|
19
|
+
const LIST_CLASS = 'flex min-w-0 flex-wrap items-center gap-1';
|
|
20
|
+
const LIST_SCROLL_CLASS = 'flex flex-nowrap items-center gap-1 whitespace-nowrap';
|
|
21
|
+
const ITEM_CLASS = 'flex min-w-0 items-center gap-1';
|
|
22
|
+
const LINK_CLASS = `min-w-0 cursor-pointer truncate rounded-sm text-fg-muted transition-colors hover:text-fg hover:underline ${FOCUS}`;
|
|
23
|
+
const CURRENT_CLASS = 'min-w-0 truncate font-medium text-fg';
|
|
24
|
+
const SEP_CLASS = 'shrink-0 text-fg-subtle';
|
|
25
|
+
const ELLIPSIS_CLASS = `inline-flex h-6 min-w-6 cursor-pointer items-center justify-center rounded-sm px-1 text-fg-muted transition-colors hover:bg-surface-sunken hover:text-fg ${FOCUS}`;
|
|
26
|
+
const ELLIPSIS_STATIC_CLASS = 'shrink-0 px-1 text-fg-muted';
|
|
27
|
+
|
|
28
|
+
/** 本组件自带的全部类(供编译器并入候选类,页面框静态页头同用) */
|
|
29
|
+
export const KIT_BREADCRUMBS_CLASS_CANDIDATES: readonly string[] = classSet(
|
|
30
|
+
ROOT_CLASS, ROOT_SCROLL_CLASS, LIST_CLASS, LIST_SCROLL_CLASS, ITEM_CLASS, LINK_CLASS, CURRENT_CLASS, SEP_CLASS, ELLIPSIS_CLASS, ELLIPSIS_STATIC_CLASS,
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
export const KIT_BREADCRUMBS_DEFAULT_MAX: number = KIT_STATIC_COMPONENT_LIMITS.navigation.breadcrumbs.defaultMaxItems;
|
|
34
|
+
|
|
35
|
+
export interface KitCrumb {
|
|
36
|
+
id: string;
|
|
37
|
+
label: string;
|
|
38
|
+
href: string;
|
|
39
|
+
icon: string;
|
|
40
|
+
raw: unknown;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type KitCrumbPart =
|
|
44
|
+
| { kind: 'item'; crumb: KitCrumb; current: boolean }
|
|
45
|
+
| { kind: 'ellipsis'; hidden: KitCrumb[] };
|
|
46
|
+
|
|
47
|
+
export function crumbsOf(data: unknown): KitCrumb[] {
|
|
48
|
+
return recList(data).map((r, i) => ({
|
|
49
|
+
id: textOf(r.id, textOf(r.key, `c${i}`)),
|
|
50
|
+
label: textOf(r.label, textOf(r.id, `#${i + 1}`)),
|
|
51
|
+
href: textOf(r.href),
|
|
52
|
+
icon: textOf(r.icon),
|
|
53
|
+
raw: r,
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 折叠计算:max < 3 或层级不超过 max -> 全显;否则 首项 + 「…」+ 末 (max-2) 项。
|
|
59
|
+
*/
|
|
60
|
+
export function breadcrumbParts(crumbs: readonly KitCrumb[], max: number, expanded = false): KitCrumbPart[] {
|
|
61
|
+
const last = crumbs.length - 1;
|
|
62
|
+
const asItem = (c: KitCrumb, i: number): KitCrumbPart => ({ kind: 'item', crumb: c, current: i === last });
|
|
63
|
+
if (expanded || max < 3 || crumbs.length <= max) return crumbs.map(asItem);
|
|
64
|
+
const tail = max - 2;
|
|
65
|
+
const hidden = crumbs.slice(1, crumbs.length - tail);
|
|
66
|
+
return [
|
|
67
|
+
asItem(crumbs[0]!, 0),
|
|
68
|
+
{ kind: 'ellipsis', hidden },
|
|
69
|
+
...crumbs.slice(crumbs.length - tail).map((c, i) => asItem(c, crumbs.length - tail + i)),
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const esc = (t: string): string => t.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
74
|
+
|
|
75
|
+
export function breadcrumbSeparator(marker: string, separator: string): string {
|
|
76
|
+
// chevron 标记由调用方画 SVG(kitIcon),这里只给文本分隔符
|
|
77
|
+
return separator === '' ? '/' : separator;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 页面框用的静态面包屑 HTML(编译期直出):label / href 已转义,href 过 safeHref */
|
|
81
|
+
export function breadcrumbsHtml(items: readonly { label: string; href?: string }[], opts: { max?: number; separator?: string; marker?: string; anchorId?: string; locale?: string } = {}): string {
|
|
82
|
+
if (items.length === 0) return '';
|
|
83
|
+
const crumbs: KitCrumb[] = items.map((it, i) => ({ id: `c${i}`, label: it.label, href: it.href ?? '', icon: '', raw: it }));
|
|
84
|
+
const parts = breadcrumbParts(crumbs, opts.max ?? KIT_BREADCRUMBS_DEFAULT_MAX);
|
|
85
|
+
const sep = breadcrumbSeparator(opts.marker ?? 'text', opts.separator ?? '/');
|
|
86
|
+
const lis = parts.map((p, i) => {
|
|
87
|
+
const sepHtml = opts.marker === 'chevron' ? kitIconHtml('chevron-right', 'xs', 'rtl:-scale-x-100') : esc(sep);
|
|
88
|
+
const lead = i === 0 ? '' : `<span aria-hidden="true" class="${SEP_CLASS}">${sepHtml}</span>`;
|
|
89
|
+
if (p.kind === 'ellipsis') {
|
|
90
|
+
const title = p.hidden.map((c) => c.label).join(' / ');
|
|
91
|
+
return `<li class="${ITEM_CLASS}">${lead}<span title="${esc(title)}" class="${ELLIPSIS_STATIC_CLASS}">…</span></li>`;
|
|
92
|
+
}
|
|
93
|
+
const { crumb, current } = p;
|
|
94
|
+
if (current) return `<li class="${ITEM_CLASS}">${lead}<span aria-current="page" class="${CURRENT_CLASS}">${esc(crumb.label)}</span></li>`;
|
|
95
|
+
const href = crumb.href !== '' ? safeHref(crumb.href) : '';
|
|
96
|
+
const inner = href !== ''
|
|
97
|
+
? `<a href="${esc(href)}" class="${LINK_CLASS}">${esc(crumb.label)}</a>`
|
|
98
|
+
: `<span class="min-w-0 truncate">${esc(crumb.label)}</span>`;
|
|
99
|
+
return `<li class="${ITEM_CLASS}">${lead}${inner}</li>`;
|
|
100
|
+
});
|
|
101
|
+
const id = opts.anchorId !== undefined ? ` data-soma-id="${esc(opts.anchorId)}"` : '';
|
|
102
|
+
return `<nav aria-label="${esc(kitText('soma.kit.nav_breadcrumb', undefined, opts.locale ?? ''))}"${id} class="${ROOT_CLASS}"><ol class="${LIST_CLASS}">${lis.join('')}</ol></nav>`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface KitBreadcrumbsHandle {
|
|
106
|
+
update(props: Rec): void;
|
|
107
|
+
destroy(): void;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 挂载(组件本体与 workspace-frame 共用):root 是容器元素,update 整体重画 */
|
|
111
|
+
export function mountKitBreadcrumbs(root: HTMLElement, emit: (name: string, payload?: unknown) => void): KitBreadcrumbsHandle {
|
|
112
|
+
let props: Rec = {};
|
|
113
|
+
let expanded = false;
|
|
114
|
+
let sig = '';
|
|
115
|
+
|
|
116
|
+
const paint = (): void => {
|
|
117
|
+
const crumbs = crumbsOf(props.data);
|
|
118
|
+
const nextSig = JSON.stringify(crumbs.map((c) => c.id + c.label));
|
|
119
|
+
if (nextSig !== sig) { sig = nextSig; expanded = false; }
|
|
120
|
+
const scroll = str(props.overflow) === 'scroll';
|
|
121
|
+
const maxRaw = num(props.max_items);
|
|
122
|
+
const max = scroll ? 0 : (maxRaw === null ? KIT_BREADCRUMBS_DEFAULT_MAX : maxRaw);
|
|
123
|
+
const sep = breadcrumbSeparator(str(props.marker), str(props.separator, '/'));
|
|
124
|
+
root.className = joinClass(scroll ? ROOT_SCROLL_CLASS : ROOT_CLASS, props.class);
|
|
125
|
+
root.setAttribute('aria-label', str(props.label, kitText('soma.kit.nav_breadcrumb', undefined, root)));
|
|
126
|
+
const list = h('ol', scroll ? LIST_SCROLL_CLASS : LIST_CLASS);
|
|
127
|
+
breadcrumbParts(crumbs, max, expanded).forEach((p, i) => {
|
|
128
|
+
const li = h('li', ITEM_CLASS);
|
|
129
|
+
if (i > 0) li.append(h('span', SEP_CLASS, { 'aria-hidden': 'true' }, str(props.marker) === 'chevron' ? kitIcon('chevron-right', 'xs', 'rtl:-scale-x-100') : sep));
|
|
130
|
+
if (p.kind === 'ellipsis') {
|
|
131
|
+
const title = p.hidden.map((c) => c.label).join(' / ');
|
|
132
|
+
const btn = h('button', ELLIPSIS_CLASS, { type: 'button', 'aria-label': kitText('soma.kit.bc_expand', { title }, root), title }, '…');
|
|
133
|
+
btn.addEventListener('click', () => { expanded = true; paint(); (root.querySelectorAll('li')[1]?.querySelector('a, button') as HTMLElement | null)?.focus(); });
|
|
134
|
+
li.append(btn);
|
|
135
|
+
} else {
|
|
136
|
+
const { crumb, current } = p;
|
|
137
|
+
const text: Child[] = [crumb.icon !== '' ? h('span', '', { 'aria-hidden': 'true' }, crumb.icon) : null, crumb.label];
|
|
138
|
+
if (current) {
|
|
139
|
+
li.append(h('span', CURRENT_CLASS, { 'aria-current': 'page' }, ...text));
|
|
140
|
+
} else {
|
|
141
|
+
const href = crumb.href !== '' ? safeHref(crumb.href) : '';
|
|
142
|
+
if (isRec(crumb.raw) && crumb.raw.inert === true) {
|
|
143
|
+
// inert:只是层级文字(如菜单分组名),不可点
|
|
144
|
+
li.append(h('span', 'min-w-0 truncate', {}, ...text));
|
|
145
|
+
} else {
|
|
146
|
+
const el = href !== ''
|
|
147
|
+
? h('a', LINK_CLASS, { href }, ...text)
|
|
148
|
+
: h('button', LINK_CLASS, { type: 'button' }, ...text);
|
|
149
|
+
el.addEventListener('click', () => emit('select', { item: crumb.raw }));
|
|
150
|
+
li.append(el);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
list.append(li);
|
|
155
|
+
});
|
|
156
|
+
root.replaceChildren(list);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
update(next) { props = next; paint(); },
|
|
161
|
+
destroy() { root.replaceChildren(); },
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const HANDLES = new WeakMap<HTMLElement, KitBreadcrumbsHandle>();
|
|
166
|
+
|
|
167
|
+
export function createBreadcrumbs(): SomaComponentDefinition {
|
|
168
|
+
return {
|
|
169
|
+
contract: {
|
|
170
|
+
name: 'Breadcrumbs',
|
|
171
|
+
layer: 'data',
|
|
172
|
+
props: {
|
|
173
|
+
data: { type: 'array', default: [], desc: '路径 [{ id?, label, href?, icon? }],末项为当前页' },
|
|
174
|
+
max_items: { type: 'number', default: KIT_BREADCRUMBS_DEFAULT_MAX, desc: '最多显示的层级数;多于它则折叠中段(小于 3 不折叠)' },
|
|
175
|
+
overflow: { type: 'enum', values: ['ellipsis', 'scroll'], default: 'ellipsis', desc: 'ellipsis 折叠中段;scroll 不折叠、横向滚动' },
|
|
176
|
+
marker: { type: 'enum', values: ['text', 'chevron'], default: 'text', desc: '分隔符样式' },
|
|
177
|
+
separator: { type: 'string', default: '/', desc: 'marker=text 时的分隔符' },
|
|
178
|
+
label: { type: 'string', default: '', desc: '导航区域的可访问名;缺省取内置文案(按页面语言)' },
|
|
179
|
+
class: { type: 'string', default: '', desc: '外部传入的类,追加在根节点类串末尾' },
|
|
180
|
+
},
|
|
181
|
+
emits: {
|
|
182
|
+
select: { payload: { item: { type: 'object' } }, desc: '点前序层级(末项不可点);item 是源数据里的原对象' },
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
templateHtml: `<nav data-soma-id="root" class="${ROOT_CLASS}"></nav>`,
|
|
186
|
+
controllerFactory: createKitController({
|
|
187
|
+
attach(root, api) {
|
|
188
|
+
const handle = mountKitBreadcrumbs(root, (name, payload) => api.emit(name, payload));
|
|
189
|
+
HANDLES.set(root, handle);
|
|
190
|
+
return () => { handle.destroy(); HANDLES.delete(root); };
|
|
191
|
+
},
|
|
192
|
+
render(root, p) {
|
|
193
|
+
HANDLES.get(root)?.update(p);
|
|
194
|
+
},
|
|
195
|
+
}),
|
|
196
|
+
};
|
|
197
|
+
}
|