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,542 @@
|
|
|
1
|
+
// 基础组件的浮层小工具(A 组:Select 搜索、TagInput、TimePicker、TreeSelect 共用)。
|
|
2
|
+
//
|
|
3
|
+
// - 列表键盘走 core 的键盘接线(`soma-core/keyboard` createKeyboardWiring),不自写方向键分支;
|
|
4
|
+
// 本文件只把「一个纵向列表 + 游标」包成 handle(event),接线所需的两个数字(翻页步长)取自 foundation 的尺寸单位。
|
|
5
|
+
// - 浮层不进 top layer:面板是组件根内的绝对定位块,显隐靠整串类切换(`hidden` 与布局类不同层)。
|
|
6
|
+
// - 点外部收起:监听 document 的 pointerdown,组件卸载时摘掉。
|
|
7
|
+
|
|
8
|
+
import { createKeyboardWiring, type CollectionItem } from 'soma-core/keyboard';
|
|
9
|
+
import { SOMA_SIZE_UNITS } from 'soma-foundation/theme';
|
|
10
|
+
import { kitDirAt } from '../i18n/kit-text';
|
|
11
|
+
import {
|
|
12
|
+
createPanelPortalWith, KIT_PANEL_PORTAL_CLASS_CANDIDATES, showAtPoint,
|
|
13
|
+
type PanelPortal, type PointPanel, type PointPanelOptions, type PointXY,
|
|
14
|
+
} from './portal-core';
|
|
15
|
+
|
|
16
|
+
import { KIT_DROPDOWN_LIMITS } from 'soma-foundation/kit-limits';
|
|
17
|
+
import { KIT_GRAMMAR_RUNTIME as grammarJson } from '../generated/kit-grammar-runtime';
|
|
18
|
+
|
|
19
|
+
type WiringOptions = Parameters<typeof createKeyboardWiring>[0];
|
|
20
|
+
|
|
21
|
+
// 下拉面板高度与浮层阴影档的真源:类名读 spec/kit-grammar.json#dropdown,行高 / 内边距 / 各档像素读 design 的 kit-limits.json#dropdown
|
|
22
|
+
// (最大高 = 整数行 + 半行;shadow-lg 浮层档;border-border-subtle);clamps 两处按下标一一对应(行数从大到小)
|
|
23
|
+
type DropdownRules = {
|
|
24
|
+
rowClass: string; panelMaxClass: string; panelShadowClass: string; panelBorderClass: string;
|
|
25
|
+
clamps: readonly { cls: string }[];
|
|
26
|
+
};
|
|
27
|
+
const DD = (grammarJson as unknown as { dropdown: DropdownRules }).dropdown;
|
|
28
|
+
|
|
29
|
+
export interface ListNavOptions {
|
|
30
|
+
/** 集合宿主(列表容器;接线需要它判断书写方向) */
|
|
31
|
+
host: () => HTMLElement | null;
|
|
32
|
+
/** 可见序的项;disabled 的项会被跳过 */
|
|
33
|
+
items: () => readonly CollectionItem[];
|
|
34
|
+
getCursor: () => string | null;
|
|
35
|
+
setCursor: (key: string) => void;
|
|
36
|
+
/** Enter(以及 space: activate 时的空格)确认当前项 */
|
|
37
|
+
onActivate: (key: string) => void;
|
|
38
|
+
/** 树:→ 展开 / ← 折叠 */
|
|
39
|
+
onExpand?: (key: string) => void;
|
|
40
|
+
onCollapse?: (key: string) => void;
|
|
41
|
+
/** 默认纵向列表;树用 'tree' */
|
|
42
|
+
layout?: 'list' | 'tree';
|
|
43
|
+
/** 焦点留在文本框里(aria-activedescendant):Home / End / 空格归文本框,不交给接线 */
|
|
44
|
+
textFocus?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ListNav {
|
|
48
|
+
/** 挂在 keydown 上;返回 true = 已处理(调用方 preventDefault) */
|
|
49
|
+
handle(event: KeyboardEvent): boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createListNav(o: ListNavOptions): ListNav {
|
|
53
|
+
const pageStep = SOMA_SIZE_UNITS.collection.keyboard.pageStep;
|
|
54
|
+
const textFocus = o.textFocus === true;
|
|
55
|
+
const wiringOptions: WiringOptions = {
|
|
56
|
+
contract: {
|
|
57
|
+
layout: o.layout ?? 'list',
|
|
58
|
+
wrap: false,
|
|
59
|
+
typeahead: false,
|
|
60
|
+
focus_model: textFocus ? 'activedescendant' : 'roving',
|
|
61
|
+
space: textFocus ? 'none' : 'activate',
|
|
62
|
+
},
|
|
63
|
+
host: o.host,
|
|
64
|
+
items: o.items,
|
|
65
|
+
// 契约关了 typeahead,这个窗口不会被用到;接线要求必传
|
|
66
|
+
typeaheadDebounceMs: 0,
|
|
67
|
+
paging: { listItems: pageStep.listItems, gridRows: pageStep.gridRows },
|
|
68
|
+
getCursor: o.getCursor,
|
|
69
|
+
setCursor: o.setCursor,
|
|
70
|
+
onActivate: o.onActivate,
|
|
71
|
+
};
|
|
72
|
+
if (o.onExpand) wiringOptions.onExpand = o.onExpand;
|
|
73
|
+
if (o.onCollapse) wiringOptions.onCollapse = o.onCollapse;
|
|
74
|
+
const wiring = createKeyboardWiring(wiringOptions);
|
|
75
|
+
return {
|
|
76
|
+
handle(event) {
|
|
77
|
+
if (textFocus && (event.key === 'Home' || event.key === 'End' || event.key === ' ')) return false;
|
|
78
|
+
return wiring.onKeydown(event);
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 点组件之外的地方时收起;返回摘除函数 */
|
|
84
|
+
export function onOutsidePress(scope: HTMLElement, isOpen: () => boolean, close: () => void, alsoInside?: (node: Node) => boolean): () => void {
|
|
85
|
+
const handler = (event: Event): void => {
|
|
86
|
+
if (!isOpen()) return;
|
|
87
|
+
const target = event.target as Node | null;
|
|
88
|
+
// 挂到浮层端口的下拉面板不在 scope 的 DOM 里,但逻辑上属于 scope:点它不算外部
|
|
89
|
+
if (target && (scope.contains(target) || alsoInside?.(target) === true || inDropdownPortalOf(scope, target))) return;
|
|
90
|
+
close();
|
|
91
|
+
};
|
|
92
|
+
document.addEventListener('pointerdown', handler, true);
|
|
93
|
+
return () => document.removeEventListener('pointerdown', handler, true);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function cssEscape(value: string): string {
|
|
97
|
+
return value.replace(/["\\]/g, '\\$&');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 把游标项滚进可视区(jsdom 无此方法时静默) */
|
|
101
|
+
export function scrollIntoViewIfAny(el: Element | null): void {
|
|
102
|
+
const anyEl = el as (Element & { scrollIntoView?: (arg?: unknown) => void }) | null;
|
|
103
|
+
if (anyEl && typeof anyEl.scrollIntoView === 'function') anyEl.scrollIntoView({ block: 'nearest' });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ── 焦点环只在键盘操作时显示(R4)──────────────────────────────
|
|
107
|
+
// 程序化 .focus()(打开浮层落焦)在 Chromium 里可能令 :focus-visible 匹配,鼠标点开也冒出焦点环。
|
|
108
|
+
// 记最近一次交互是不是指针(文档级捕获监听只装一次);是指针则落点标 data-kit-quiet,
|
|
109
|
+
// 类串里的 data-kit-quiet:focus-visible:ring-0 把环压掉;键盘一动由面板的 keydown 摘掉标记。
|
|
110
|
+
let lastInputIsPointer = false;
|
|
111
|
+
let inputTrackerOn = false;
|
|
112
|
+
export function trackInputModality(doc: Document): void {
|
|
113
|
+
if (inputTrackerOn) return;
|
|
114
|
+
inputTrackerOn = true;
|
|
115
|
+
doc.addEventListener('pointerdown', () => { lastInputIsPointer = true; }, true);
|
|
116
|
+
doc.addEventListener('mousedown', () => { lastInputIsPointer = true; }, true);
|
|
117
|
+
doc.addEventListener('keydown', () => { lastInputIsPointer = false; }, true);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 程序化落焦:最近交互是鼠标则压掉焦点环;键盘操作照常显示。落焦不滚动页面 */
|
|
121
|
+
export function focusCellQuiet(node: HTMLElement | null | undefined): void {
|
|
122
|
+
if (!node) return;
|
|
123
|
+
if (lastInputIsPointer) node.setAttribute('data-kit-quiet', '');
|
|
124
|
+
else node.removeAttribute('data-kit-quiet');
|
|
125
|
+
node.focus({ preventScroll: true });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** 默认的整串面板类:显隐只换整串,`hidden` 不与布局类并存 */
|
|
129
|
+
export const PANEL_HIDDEN_CLASS = 'hidden';
|
|
130
|
+
export const PANEL_SHOWN_CLASS =
|
|
131
|
+
`absolute start-0 top-full z-10 mt-1 ${DD.panelMaxClass} w-full overflow-auto rounded-lg ${DD.panelBorderClass} bg-surface-raised py-1 ${DD.panelShadowClass}`;
|
|
132
|
+
|
|
133
|
+
// 选项行高固定(真源 dropdown.rowClass = h-9),面板最大高才能算成「整数行 + 半行」
|
|
134
|
+
const ROW = DD.rowClass;
|
|
135
|
+
export const OPTION_CLASS = {
|
|
136
|
+
idle: `flex ${ROW} cursor-pointer items-center gap-2 px-3 text-sm text-fg transition-colors hover:bg-surface-sunken`,
|
|
137
|
+
active: `flex ${ROW} cursor-pointer items-center gap-2 bg-surface-sunken px-3 text-sm text-fg transition-colors`,
|
|
138
|
+
selected: `flex ${ROW} cursor-pointer items-center gap-2 bg-primary-soft px-3 text-sm font-medium text-primary-on-soft transition-colors`,
|
|
139
|
+
selectedActive: `flex ${ROW} cursor-pointer items-center gap-2 bg-primary-soft px-3 text-sm font-medium text-primary-on-soft transition-colors`,
|
|
140
|
+
disabled: `flex ${ROW} cursor-not-allowed items-center gap-2 px-3 text-sm text-fg-subtle opacity-60`,
|
|
141
|
+
} as const;
|
|
142
|
+
|
|
143
|
+
export const EMPTY_ROW_CLASS = `flex ${ROW} items-center px-3 text-sm text-fg-subtle`;
|
|
144
|
+
|
|
145
|
+
export function optionClass(selected: boolean, active: boolean, disabled: boolean): string {
|
|
146
|
+
if (disabled) return OPTION_CLASS.disabled;
|
|
147
|
+
if (selected) return active ? OPTION_CLASS.selectedActive : OPTION_CLASS.selected;
|
|
148
|
+
return active ? OPTION_CLASS.active : OPTION_CLASS.idle;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 本文件自带的类(供各组件并入候选类清单) */
|
|
152
|
+
export const KIT_POPUP_CLASS_CANDIDATES: readonly string[] = Object.freeze([
|
|
153
|
+
'hidden', 'absolute', 'start-0', 'top-full', 'z-10', 'mt-1', DD.panelMaxClass, DD.rowClass, 'w-full', 'overflow-auto',
|
|
154
|
+
'rounded-md', 'rounded-lg', 'border', 'border-border', 'border-border-subtle', 'bg-surface-raised', 'py-1', DD.panelShadowClass, 'shadow-sm',
|
|
155
|
+
'flex', 'cursor-pointer', 'cursor-not-allowed', 'items-center', 'gap-2', 'px-3', 'py-2', 'text-sm',
|
|
156
|
+
'text-fg', 'text-fg-subtle', 'bg-surface-sunken', 'font-medium', 'text-primary',
|
|
157
|
+
'transition-colors', 'hover:bg-surface-sunken', 'bg-primary-soft', 'text-primary-on-soft', 'opacity-60',
|
|
158
|
+
]);
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 轻量提示气泡(tooltip)的整串类:与 PANEL_* 同属 popup 底座(绝对定位在目标之下、显隐只换整串、同一 z 层),
|
|
162
|
+
* 通用 `tooltip` prop(shared.ts 的 withTooltip)取用;Popover / Popconfirm 的面板另用 PANEL_*。
|
|
163
|
+
*/
|
|
164
|
+
export const POPUP_TIP_HIDDEN_CLASS = PANEL_HIDDEN_CLASS;
|
|
165
|
+
export const POPUP_TIP_BASE =
|
|
166
|
+
'absolute z-10 w-max max-w-xs rounded-md bg-fg px-3 py-1 text-xs font-normal leading-5 text-surface shadow-md pointer-events-none';
|
|
167
|
+
|
|
168
|
+
// ── 贴边自动翻转 ──────────────────────────────────────────────
|
|
169
|
+
//
|
|
170
|
+
// 浮层在视口边缘会出屏。打开(或挂载)时测一次:把面板按偏好位置显示,量面板与锚点矩形,
|
|
171
|
+
// 在预置的闭集位置之间切换——side(top / bottom / start / end)+ align(start / end)——只切整串类,
|
|
172
|
+
// 不写内联 style、不用任意值。顺序:偏好 side 放得下就不动;放不下而对侧放得下则翻到对侧;
|
|
173
|
+
// 上下都放不下再试水平两侧(start / end);仍不行保留偏好。align 同理:偏好对齐越出视口则换到另一端。
|
|
174
|
+
// 没有布局(jsdom:矩形全 0)时恒不翻转。
|
|
175
|
+
|
|
176
|
+
export type PopSide = 'top' | 'bottom' | 'start' | 'end';
|
|
177
|
+
export type PopAlign = 'start' | 'end';
|
|
178
|
+
export interface PopPlace { side: PopSide; align: PopAlign }
|
|
179
|
+
|
|
180
|
+
/** 偏好位置到矩形无关的整串定位类;gap = 与锚点的间距刻度(1 或 2,对应 m?-1 / m?-2) */
|
|
181
|
+
const POP_POS: Readonly<Record<PopSide, Readonly<Record<PopAlign, string>>>> = {
|
|
182
|
+
bottom: { start: 'start-0 top-full', end: 'end-0 top-full' },
|
|
183
|
+
top: { start: 'bottom-full start-0', end: 'bottom-full end-0' },
|
|
184
|
+
start: { start: 'end-full top-0', end: 'bottom-0 end-full' },
|
|
185
|
+
end: { start: 'start-full top-0', end: 'bottom-0 start-full' },
|
|
186
|
+
};
|
|
187
|
+
const POP_GAP: Readonly<Record<PopSide, Readonly<Record<1 | 2, string>>>> = {
|
|
188
|
+
bottom: { 1: 'mt-1', 2: 'mt-2' },
|
|
189
|
+
top: { 1: 'mb-1', 2: 'mb-2' },
|
|
190
|
+
start: { 1: 'me-1', 2: 'me-2' },
|
|
191
|
+
end: { 1: 'ms-1', 2: 'ms-2' },
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/** 位置类串(不含 absolute / z / 外观类,由组件自己拼) */
|
|
195
|
+
export function popPlaceClass(place: PopPlace, gap: 1 | 2 = 1): string {
|
|
196
|
+
return `${POP_POS[place.side][place.align]} ${POP_GAP[place.side][gap]}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** 全部位置类(供类名候选清单) */
|
|
200
|
+
export const KIT_POPUP_PLACE_CLASS_CANDIDATES: readonly string[] = Object.freeze(
|
|
201
|
+
[...new Set((['top', 'bottom', 'start', 'end'] as const).flatMap((s) =>
|
|
202
|
+
(['start', 'end'] as const).flatMap((a) => `${POP_POS[s][a]} ${POP_GAP[s][1]} ${POP_GAP[s][2]}`.split(' '))))],
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const FLIP_OPPOSITE: Readonly<Record<PopSide, PopSide>> = { top: 'bottom', bottom: 'top', start: 'end', end: 'start' };
|
|
206
|
+
|
|
207
|
+
/** 纯函数:给定锚点矩形、面板尺寸、视口,算最终位置;gapPx 为面板与锚点间距像素估值 */
|
|
208
|
+
export function resolvePopPlace(
|
|
209
|
+
anchor: { left: number; right: number; top: number; bottom: number },
|
|
210
|
+
size: { width: number; height: number },
|
|
211
|
+
viewport: { width: number; height: number },
|
|
212
|
+
pref: PopPlace,
|
|
213
|
+
gapPx = 8,
|
|
214
|
+
rtl = false,
|
|
215
|
+
): PopPlace {
|
|
216
|
+
const { width: w, height: h } = size;
|
|
217
|
+
if (w === 0 && h === 0) return pref;
|
|
218
|
+
// start / end 是行内方向:LTR 的 start 在左,RTL 的 start 在右
|
|
219
|
+
const left = anchor.left - gapPx;
|
|
220
|
+
const right = viewport.width - anchor.right - gapPx;
|
|
221
|
+
const room = {
|
|
222
|
+
bottom: viewport.height - anchor.bottom - gapPx,
|
|
223
|
+
top: anchor.top - gapPx,
|
|
224
|
+
end: rtl ? left : right,
|
|
225
|
+
start: rtl ? right : left,
|
|
226
|
+
};
|
|
227
|
+
const need = (s: PopSide): number => (s === 'top' || s === 'bottom' ? h : w);
|
|
228
|
+
const fits = (s: PopSide): boolean => room[s] >= need(s);
|
|
229
|
+
let side = pref.side;
|
|
230
|
+
if (!fits(side)) {
|
|
231
|
+
const opp = FLIP_OPPOSITE[side];
|
|
232
|
+
if (fits(opp)) side = opp;
|
|
233
|
+
else {
|
|
234
|
+
const sides: PopSide[] = side === 'top' || side === 'bottom' ? ['end', 'start'] : ['bottom', 'top'];
|
|
235
|
+
const found = sides.find(fits);
|
|
236
|
+
if (found !== undefined) side = found;
|
|
237
|
+
else if (room[opp] > room[side]) side = opp;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
// 对齐:垂直侧看水平越界,水平侧看垂直越界
|
|
241
|
+
const vertical = side === 'top' || side === 'bottom';
|
|
242
|
+
const fitsAlign = (a: PopAlign): boolean => vertical
|
|
243
|
+
? ((a === 'start') !== rtl ? anchor.left + w <= viewport.width : anchor.right - w >= 0)
|
|
244
|
+
: (a === 'start' ? anchor.top + h <= viewport.height : anchor.bottom - h >= 0);
|
|
245
|
+
let align = pref.align;
|
|
246
|
+
if (!fitsAlign(align) && fitsAlign(align === 'start' ? 'end' : 'start')) align = align === 'start' ? 'end' : 'start';
|
|
247
|
+
return { side, align };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 显示面板并贴边翻转:先按偏好位置落类,量一次,必要时换成翻转后的整串类。
|
|
252
|
+
* classFor 由组件给(位置类 + 自己的外观类);返回最终位置。
|
|
253
|
+
*/
|
|
254
|
+
export function showPopupFitted(
|
|
255
|
+
panel: HTMLElement,
|
|
256
|
+
anchor: HTMLElement,
|
|
257
|
+
pref: PopPlace,
|
|
258
|
+
classFor: (place: PopPlace) => string,
|
|
259
|
+
gapPx = 8,
|
|
260
|
+
): PopPlace {
|
|
261
|
+
panel.className = classFor(pref);
|
|
262
|
+
if (typeof panel.getBoundingClientRect !== 'function') return pref;
|
|
263
|
+
const a = anchor.getBoundingClientRect();
|
|
264
|
+
const b = panel.getBoundingClientRect();
|
|
265
|
+
const vw = document.documentElement.clientWidth || (typeof window !== 'undefined' ? window.innerWidth : 0);
|
|
266
|
+
const vh = document.documentElement.clientHeight || (typeof window !== 'undefined' ? window.innerHeight : 0);
|
|
267
|
+
if (vw === 0 || vh === 0) return pref;
|
|
268
|
+
const place = resolvePopPlace(a, { width: b.width, height: b.height }, { width: vw, height: vh }, pref, gapPx, kitDirAt(anchor) === 'rtl');
|
|
269
|
+
if (place.side !== pref.side || place.align !== pref.align) panel.className = classFor(place);
|
|
270
|
+
return place;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** 气泡的偏好位置:目标下方、左对齐;贴边时由 showPopupFitted 翻转 */
|
|
274
|
+
export const POPUP_TIP_PREF: PopPlace = { side: 'bottom', align: 'start' };
|
|
275
|
+
export const popupTipClass = (place: PopPlace): string => `${POPUP_TIP_BASE} ${popPlaceClass(place, 1)}`;
|
|
276
|
+
export const POPUP_TIP_SHOWN_CLASS = popupTipClass(POPUP_TIP_PREF);
|
|
277
|
+
|
|
278
|
+
// ── 下拉面板的贴边翻转(Select / Combobox / Cascader / TreeSelect / TimePicker / ColorPicker / Mentions / TagInput / Navbar / Menu columns 共用)──
|
|
279
|
+
//
|
|
280
|
+
// 下拉面板宽度跟随触发器(外层 relative 盒内 `w-full`)或自带宽度,这里不动宽度类,只换位置类:
|
|
281
|
+
// 默认在锚点下方;下方放不下、上方放得下则翻到上方;上下都放不下取空间较大的一侧,
|
|
282
|
+
// 并把面板的 max-h 换成闭集里放得下的一档、内部滚动。水平对齐越界时换到另一端。
|
|
283
|
+
// 全程只换整串类,不写内联 style;jsdom(矩形全 0)恒不翻转。
|
|
284
|
+
|
|
285
|
+
/** 下拉面板外观类(不含位置类):与 PANEL_SHOWN_CLASS 同一份外观 */
|
|
286
|
+
export const PANEL_BASE_CLASS =
|
|
287
|
+
`absolute z-10 ${DD.panelMaxClass} w-full overflow-auto rounded-lg ${DD.panelBorderClass} bg-surface-raised py-1 ${DD.panelShadowClass}`;
|
|
288
|
+
|
|
289
|
+
/** 空间不足时面板内部滚动的 max-h 闭集档位(px = 刻度 x 4),从大到小;每档都是「整数行 + 半行」(真源 dropdown.clamps),空间不够也不腰斩一行 */
|
|
290
|
+
export const DROPDOWN_CLAMPS: readonly { cls: string; px: number }[] = Object.freeze(
|
|
291
|
+
DD.clamps.map((c, i) => ({ cls: c.cls, px: KIT_DROPDOWN_LIMITS.clamps[i]!.px })),
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
/** 下拉翻转用到的全部类(位置类、间距类、max-h 档位、滚动) */
|
|
295
|
+
export const KIT_DROPDOWN_CLASS_CANDIDATES: readonly string[] = Object.freeze([
|
|
296
|
+
...new Set([
|
|
297
|
+
...KIT_POPUP_PLACE_CLASS_CANDIDATES,
|
|
298
|
+
...PANEL_BASE_CLASS.split(' '),
|
|
299
|
+
...DROPDOWN_CLAMPS.map((c) => c.cls),
|
|
300
|
+
'overflow-y-auto',
|
|
301
|
+
'pointer-events-auto',
|
|
302
|
+
...KIT_PANEL_PORTAL_CLASS_CANDIDATES,
|
|
303
|
+
]),
|
|
304
|
+
]);
|
|
305
|
+
|
|
306
|
+
/** 纯函数:下拉的最终位置与是否需要内部滚动(clampPx = 可用高度,null = 不需要) */
|
|
307
|
+
export function resolveDropdown(
|
|
308
|
+
anchor: { left: number; right: number; top: number; bottom: number },
|
|
309
|
+
size: { width: number; height: number },
|
|
310
|
+
viewport: { width: number; height: number },
|
|
311
|
+
prefAlign: PopAlign = 'start',
|
|
312
|
+
gapPx = 8,
|
|
313
|
+
rtl = false,
|
|
314
|
+
): { place: PopPlace; clampPx: number | null } {
|
|
315
|
+
if (size.width === 0 && size.height === 0) return { place: { side: 'bottom', align: prefAlign }, clampPx: null };
|
|
316
|
+
const below = viewport.height - anchor.bottom - gapPx;
|
|
317
|
+
const above = anchor.top - gapPx;
|
|
318
|
+
let side: PopSide = 'bottom';
|
|
319
|
+
let clampPx: number | null = null;
|
|
320
|
+
if (size.height > below) {
|
|
321
|
+
if (size.height <= above) side = 'top';
|
|
322
|
+
else {
|
|
323
|
+
side = above > below ? 'top' : 'bottom';
|
|
324
|
+
clampPx = Math.max(side === 'top' ? above : below, 0);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const fitsAlign = (a: PopAlign): boolean => ((a === 'start') !== rtl ? anchor.left + size.width <= viewport.width : anchor.right - size.width >= 0);
|
|
328
|
+
let align = prefAlign;
|
|
329
|
+
const other: PopAlign = prefAlign === 'start' ? 'end' : 'start';
|
|
330
|
+
if (!fitsAlign(align) && fitsAlign(other)) align = other;
|
|
331
|
+
return { place: { side, align }, clampPx };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** 去掉 max-h-* 后按档位加上放得下的一档(内部滚动);没有 overflow 类时补 overflow-y-auto */
|
|
335
|
+
export function clampDropdownClass(cls: string, clampPx: number): string {
|
|
336
|
+
const tokens = cls.split(/\s+/).filter((t) => t !== '' && !t.startsWith('max-h-'));
|
|
337
|
+
const step = DROPDOWN_CLAMPS.find((c) => c.px <= clampPx) ?? DROPDOWN_CLAMPS[DROPDOWN_CLAMPS.length - 1]!;
|
|
338
|
+
tokens.push(step.cls);
|
|
339
|
+
if (!tokens.some((t) => t.startsWith('overflow-'))) tokens.push('overflow-y-auto');
|
|
340
|
+
return tokens.join(' ');
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* 显示下拉并贴边翻转。base = 面板外观类(含 absolute / z / 宽度,不含位置类);
|
|
345
|
+
* 内容必须已画好再调(要量真实高度)。返回最终位置。
|
|
346
|
+
*/
|
|
347
|
+
export function showDropdownFitted(
|
|
348
|
+
panel: HTMLElement,
|
|
349
|
+
anchor: HTMLElement,
|
|
350
|
+
base: string,
|
|
351
|
+
prefAlign: PopAlign = 'start',
|
|
352
|
+
gap: 1 | 2 = 1,
|
|
353
|
+
): PopPlace {
|
|
354
|
+
const classFor = (place: PopPlace): string => `${base} ${popPlaceClass(place, gap)}`;
|
|
355
|
+
const pref: PopPlace = { side: 'bottom', align: prefAlign };
|
|
356
|
+
panel.className = classFor(pref);
|
|
357
|
+
if (typeof panel.getBoundingClientRect !== 'function') return pref;
|
|
358
|
+
const vw = document.documentElement.clientWidth || (typeof window !== 'undefined' ? window.innerWidth : 0);
|
|
359
|
+
const vh = document.documentElement.clientHeight || (typeof window !== 'undefined' ? window.innerHeight : 0);
|
|
360
|
+
if (vw === 0 || vh === 0) return pref;
|
|
361
|
+
const a = anchor.getBoundingClientRect();
|
|
362
|
+
const b = panel.getBoundingClientRect();
|
|
363
|
+
const { place, clampPx } = resolveDropdown(a, { width: b.width, height: b.height }, { width: vw, height: vh }, prefAlign, 8, kitDirAt(anchor) === 'rtl');
|
|
364
|
+
const next = clampPx === null ? classFor(place) : `${clampDropdownClass(base, clampPx)} ${popPlaceClass(place, gap)}`;
|
|
365
|
+
if (panel.className !== next) panel.className = next;
|
|
366
|
+
return place;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// ── 浮层端口挂载(下拉 / 菜单默认走这里,只此一份实现)──
|
|
370
|
+
//
|
|
371
|
+
// 下拉面板默认挂到 core 的浮层挂载点(body 下专用根),不再受触发器祖先的 overflow / transform 裁切。
|
|
372
|
+
// 锚盒 = 触发器(锚点)的视口矩形,面板写 `w-full` 即宽度跟随;贴边翻转 / 内部滚动沿用 showDropdownFitted。
|
|
373
|
+
// 端口不可用(无 DOM、没注入)时退回原位绝对定位。收起或组件卸载时必须经 syncDropdown(panel, false) / releaseDropdown 放回 / 销毁。
|
|
374
|
+
|
|
375
|
+
/** 绑定了贴边翻转的锚定面板挂载(菜单、表格面板用) */
|
|
376
|
+
export function createPanelPortal(owner: Element | null): PanelPortal {
|
|
377
|
+
return createPanelPortalWith(owner, showPopupFitted);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* 右键菜单:把面板挂到浮层根、按落点(视口坐标)翻转 / 夹取定位。翻转逻辑复用本文件的 resolvePopPlace(零尺寸锚点 = 落点),
|
|
382
|
+
* 几何只在 portal-core 的 placeAtPoint 写一次。端口不可用返回 null(调用方走就地绝对定位,不写 CSSOM)。
|
|
383
|
+
*/
|
|
384
|
+
export function showMenuAtPoint(panel: HTMLElement, point: PointXY, opts: Omit<PointPanelOptions, 'resolve' | 'rtl'> & { anchor: Element }): PointPanel | null {
|
|
385
|
+
return showAtPoint(panel, point, { ...opts, rtl: kitDirAt(opts.anchor) === 'rtl', resolve: resolvePopPlace });
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** 浮层端口挂载用到的类(并入使用下拉的组件的候选类清单) */
|
|
389
|
+
export const KIT_DROPDOWN_PORTAL_CLASS_CANDIDATES: readonly string[] = Object.freeze([
|
|
390
|
+
...KIT_PANEL_PORTAL_CLASS_CANDIDATES,
|
|
391
|
+
'pointer-events-auto',
|
|
392
|
+
]);
|
|
393
|
+
|
|
394
|
+
const DROPDOWN_LIVE_CLASS = 'pointer-events-auto';
|
|
395
|
+
|
|
396
|
+
interface DropdownMount { portal: PanelPortal; anchor: HTMLElement; panel: HTMLElement }
|
|
397
|
+
const DROPDOWN_MOUNTS = new WeakMap<HTMLElement, DropdownMount>();
|
|
398
|
+
/** 当前挂在端口里的下拉(点外部判断要把它们算作各自 scope 的内部) */
|
|
399
|
+
const DROPDOWN_LIVE = new Set<DropdownMount>();
|
|
400
|
+
|
|
401
|
+
function inDropdownPortalOf(scope: HTMLElement, node: Node): boolean {
|
|
402
|
+
for (const m of DROPDOWN_LIVE) {
|
|
403
|
+
if (!m.anchor.isConnected) {
|
|
404
|
+
DROPDOWN_LIVE.delete(m);
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
if (scope.contains(m.anchor) && m.portal.contains(node)) return true;
|
|
408
|
+
}
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** scope 内的锚点所挂出的、当前在浮层里的下拉面板 */
|
|
413
|
+
function livePanelsIn(scope: Element): HTMLElement[] {
|
|
414
|
+
const out: HTMLElement[] = [];
|
|
415
|
+
for (const m of DROPDOWN_LIVE) if (scope.contains(m.anchor) && m.portal.shell() !== null) out.push(m.panel);
|
|
416
|
+
return out;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** 节点所在浮层下拉的锚点(不在任何下拉里为 null):整树重画型组件的"点外部"判断把它的锚点当作内部 */
|
|
420
|
+
export function dropdownOwnerOf(node: Node): HTMLElement | null {
|
|
421
|
+
for (const m of DROPDOWN_LIVE) if (m.portal.contains(node)) return m.anchor;
|
|
422
|
+
return null;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* 浮层归属的统一判定(kit 组件编写准则 R3:组件不自己 closest 浮层层猜归属)。
|
|
428
|
+
* - 'inside':节点在某个浮层下拉里,且它的锚点在 scope 内;或点击瞬间节点已被重画摘掉
|
|
429
|
+
* - 'outside':节点在锚点不在 scope 内的浮层下拉里
|
|
430
|
+
* - 'foreign-layer':节点在浮层层里但没有登记属主,且该层不是包住 scope 的祖先层(如编辑器内嵌的未登记面板)
|
|
431
|
+
* - 'none':与浮层无关,按普通 DOM 判断
|
|
432
|
+
*/
|
|
433
|
+
export type OverlayVerdict = 'inside' | 'outside' | 'foreign-layer' | 'none';
|
|
434
|
+
export function overlayVerdict(node: Node, scope: Element): OverlayVerdict {
|
|
435
|
+
if (!node.isConnected) return 'inside';
|
|
436
|
+
const owner = dropdownOwnerOf(node);
|
|
437
|
+
if (owner !== null) return scope.contains(owner) ? 'inside' : 'outside';
|
|
438
|
+
const el = node instanceof Element ? node : node.parentElement;
|
|
439
|
+
const layer = el?.closest('[data-soma-overlay-layer]') ?? null;
|
|
440
|
+
return layer !== null && !layer.contains(scope) ? 'foreign-layer' : 'none';
|
|
441
|
+
}
|
|
442
|
+
/** 先在 scope 里找,找不到再到它挂出去的下拉面板里找(面板打开时不在 scope 的后代中) */
|
|
443
|
+
export function queryWithDropdowns(scope: Element, selector: string): HTMLElement | null {
|
|
444
|
+
const hit = scope.querySelector<HTMLElement>(selector);
|
|
445
|
+
if (hit) return hit;
|
|
446
|
+
for (const panel of livePanelsIn(scope)) {
|
|
447
|
+
const inner = panel.matches(selector) ? panel : panel.querySelector<HTMLElement>(selector);
|
|
448
|
+
if (inner) return inner;
|
|
449
|
+
}
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** 焦点是否落在 scope 内或它挂出去的下拉面板里;是则返回聚焦元素 */
|
|
454
|
+
export function activeWithDropdowns(scope: Element): HTMLElement | null {
|
|
455
|
+
const active = scope.ownerDocument.activeElement as HTMLElement | null;
|
|
456
|
+
if (!active) return null;
|
|
457
|
+
if (scope.contains(active)) return active;
|
|
458
|
+
return livePanelsIn(scope).some((p) => p.contains(active)) ? active : null;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/** 整树重画前调用:销毁 scope 内锚点所挂出的全部下拉面板(旧面板随旧树丢弃,重画后按需重新挂出) */
|
|
462
|
+
export function releaseDropdownsIn(scope: Element): void {
|
|
463
|
+
for (const m of [...DROPDOWN_LIVE]) if (scope.contains(m.anchor)) releaseDropdown(m.panel, false);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** 收起并放回原位(restore)或直接销毁(组件卸载) */
|
|
467
|
+
export function releaseDropdown(panel: HTMLElement, restore = true): void {
|
|
468
|
+
const m = DROPDOWN_MOUNTS.get(panel);
|
|
469
|
+
if (!m) return;
|
|
470
|
+
DROPDOWN_MOUNTS.delete(panel);
|
|
471
|
+
DROPDOWN_LIVE.delete(m);
|
|
472
|
+
if (restore) m.portal.hide(true);
|
|
473
|
+
else m.portal.destroy();
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** 挂到端口并落位;返回实际锚点,端口不可用返回 null(调用方走原位) */
|
|
477
|
+
function mountAnchored(panel: HTMLElement, anchor: HTMLElement | undefined, pref: PopPlace, fit: (p: HTMLElement, box: HTMLElement) => PopPlace): HTMLElement | null {
|
|
478
|
+
const prev = DROPDOWN_MOUNTS.get(panel);
|
|
479
|
+
const at = anchor ?? prev?.anchor ?? panel.parentElement ?? panel;
|
|
480
|
+
const portal = prev?.portal ?? createPanelPortal(at);
|
|
481
|
+
// 端口给的 box 是内部零宽高包裹盒,不能当锚点测视口空间;贴边翻转一律用真实触发器
|
|
482
|
+
const placed = portal.show(panel, at, pref, () => '', { fit: (p) => fit(p, at) });
|
|
483
|
+
if (placed === null) {
|
|
484
|
+
if (prev) DROPDOWN_LIVE.delete(prev);
|
|
485
|
+
DROPDOWN_MOUNTS.delete(panel);
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
if (!prev) {
|
|
489
|
+
const m: DropdownMount = { portal, anchor: at, panel };
|
|
490
|
+
DROPDOWN_MOUNTS.set(panel, m);
|
|
491
|
+
DROPDOWN_LIVE.add(m);
|
|
492
|
+
}
|
|
493
|
+
return at;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* 下拉显隐一步到位:收起 = 放回原位 + 整串 hidden;展开 = 挂到浮层端口并贴边翻转(锚点缺省取面板的父盒,
|
|
498
|
+
* 面板已挂出时取上次的锚点)。端口不可用时退回原位绝对定位。
|
|
499
|
+
*/
|
|
500
|
+
export function syncDropdown(panel: HTMLElement, open: boolean, base: string = PANEL_BASE_CLASS, anchor?: HTMLElement, prefAlign: PopAlign = 'start'): void {
|
|
501
|
+
if (!open) {
|
|
502
|
+
releaseDropdown(panel);
|
|
503
|
+
panel.className = PANEL_HIDDEN_CLASS;
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
const live = `${base} ${DROPDOWN_LIVE_CLASS}`;
|
|
507
|
+
const at = mountAnchored(panel, anchor, { side: 'bottom', align: prefAlign }, (p, box) => showDropdownFitted(p, box, live, prefAlign));
|
|
508
|
+
if (at === null) showDropdownFitted(panel, anchor ?? panel.parentElement ?? panel, base, prefAlign);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* 气泡类面板(Popover):收起 = 放回原位 + hidden;展开 = 挂到浮层端口,按偏好位置贴边翻转(锚点 = 触发钮)。
|
|
513
|
+
* classFor 给位置类 + 外观类。端口不可用时退回原位。
|
|
514
|
+
*/
|
|
515
|
+
export function syncAnchored(
|
|
516
|
+
panel: HTMLElement, open: boolean, anchor: HTMLElement, pref: PopPlace, classFor: (place: PopPlace) => string, gapPx?: number,
|
|
517
|
+
): void {
|
|
518
|
+
if (!open) {
|
|
519
|
+
releaseDropdown(panel);
|
|
520
|
+
panel.className = PANEL_HIDDEN_CLASS;
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
const live = (place: PopPlace): string => `${classFor(place)} ${DROPDOWN_LIVE_CLASS}`;
|
|
524
|
+
const at = mountAnchored(panel, anchor, pref, (p, box) => showPopupFitted(p, box, pref, live, gapPx));
|
|
525
|
+
if (at === null) showPopupFitted(panel, anchor, pref, classFor, gapPx);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const TAB_STOPS = 'a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])';
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* 面板挂在浮层里后,Tab 走出面板会落到 body 末尾而不是触发钮后面。面板里在首 / 尾元素上继续 Tab 时,
|
|
532
|
+
* 先把焦点交回触发钮并返回 true(调用方随后关闭面板、不要 preventDefault),默认的 Tab 就从触发钮继续往下。
|
|
533
|
+
*/
|
|
534
|
+
export function handoffTab(event: KeyboardEvent, panel: HTMLElement, trigger: HTMLElement): boolean {
|
|
535
|
+
if (event.key !== 'Tab') return false;
|
|
536
|
+
const stops = [...panel.querySelectorAll<HTMLElement>(TAB_STOPS)].filter((n) => !n.hasAttribute('hidden'));
|
|
537
|
+
const active = panel.ownerDocument.activeElement;
|
|
538
|
+
const edge = event.shiftKey ? (stops[0] ?? panel) : (stops[stops.length - 1] ?? panel);
|
|
539
|
+
if (active !== edge && !(stops.length === 0 && active === panel)) return false;
|
|
540
|
+
trigger.focus();
|
|
541
|
+
return true;
|
|
542
|
+
}
|