@astryx-vue/core 0.16.5 → 0.17.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/dist/AvatarGroup/index.d.ts +1 -1
- package/dist/Banner/index.d.ts +1 -1
- package/dist/Button/index.d.ts +1 -1
- package/dist/ButtonGroup/index.d.ts +1 -1
- package/dist/Calendar/index.d.ts +1 -1
- package/dist/Card/index.d.ts +1 -1
- package/dist/Carousel/index.d.ts +1 -1
- package/dist/Chat/index.d.ts +1 -1
- package/dist/Chat/index.js +1 -1
- package/dist/CheckboxInput/index.d.ts +1 -1
- package/dist/CheckboxList/index.d.ts +1 -1
- package/dist/ClickableCard/index.d.ts +1 -1
- package/dist/Code/index.js +1 -1
- package/dist/Collapsible/index.d.ts +1 -1
- package/dist/CommandPalette/index.d.ts +1 -1
- package/dist/CommandPalette/index.js +1 -1
- package/dist/{CommandPalette-mNR6dV0B.js → CommandPalette-CECjNvGB.js} +1 -1
- package/dist/ComplexSelector/index.d.ts +1 -1
- package/dist/ComplexSelector/index.js +1 -1
- package/dist/{ComplexSelector-CR9XbpTa.js → ComplexSelector-Dmaag1wm.js} +5 -1
- package/dist/ContextMenu/index.d.ts +1 -1
- package/dist/DateInput/index.d.ts +1 -1
- package/dist/DateInput/index.js +1 -1
- package/dist/DateRangeInput/index.d.ts +1 -1
- package/dist/DateRangeInput/index.js +1 -1
- package/dist/DateTimeInput/index.d.ts +1 -1
- package/dist/DateTimeInput/index.js +1 -1
- package/dist/{DateTimeInput-ChGin5zS.js → DateTimeInput-Cfkuec_h.js} +1 -1
- package/dist/Dialog/index.d.ts +1 -1
- package/dist/Drawer/index.d.ts +1 -1
- package/dist/Field/index.d.ts +1 -1
- package/dist/FileInput/index.d.ts +1 -1
- package/dist/Heading/index.d.ts +1 -1
- package/dist/HoverCard/index.d.ts +1 -1
- package/dist/Indicator/index.d.ts +2 -2
- package/dist/InfoTip/index.d.ts +1 -1
- package/dist/InputGroup/index.d.ts +1 -1
- package/dist/InternationalizationProvider/index.d.ts +1 -1
- package/dist/InternationalizationProvider/index.js +1 -1
- package/dist/{InternationalizationProvider-7rA65NO7.js → InternationalizationProvider-Dr5GE6f0.js} +1 -1
- package/dist/Link/index.d.ts +1 -1
- package/dist/List/index.d.ts +1 -1
- package/dist/Markdown/index.js +1 -1
- package/dist/{Markdown-BNVd70jg.js → Markdown-DnnWjJGX.js} +1 -1
- package/dist/MoreMenu/index.d.ts +1 -1
- package/dist/MultiSelector/index.d.ts +1 -1
- package/dist/MultiSelector/index.js +1 -1
- package/dist/{MultiSelector-CXX1Hsty.js → MultiSelector-BK6nZweN.js} +26 -14
- package/dist/NavItem/index.d.ts +1 -1
- package/dist/NumberInput/index.d.ts +1 -1
- package/dist/OverflowList/index.d.ts +1 -1
- package/dist/Pagination/index.d.ts +1 -1
- package/dist/Pagination/index.js +1 -1
- package/dist/{Pagination-BvZFvwso.js → Pagination-DFQNX_Mb.js} +1 -1
- package/dist/Popover/index.d.ts +1 -1
- package/dist/PowerSearch/index.d.ts +1 -1
- package/dist/PowerSearch/index.js +1 -1
- package/dist/{PowerSearch-BiupstfT.js → PowerSearch-BAOqpQag.js} +10 -10
- package/dist/ProgressBar/index.d.ts +1 -1
- package/dist/Radio/index.d.ts +1 -1
- package/dist/RadioList/index.d.ts +1 -1
- package/dist/Resizable/index.d.ts +1 -1
- package/dist/ScrollableArea/index.d.ts +1 -1
- package/dist/SegmentedControl/index.d.ts +1 -1
- package/dist/SelectableCard/index.d.ts +1 -1
- package/dist/Selector/index.d.ts +1 -1
- package/dist/Selector/index.js +1 -1
- package/dist/{Selector-DiSLNzl8.js → Selector-Bo7qkt1P.js} +157 -62
- package/dist/SideNav/index.d.ts +1 -1
- package/dist/Stepper/index.d.ts +1 -1
- package/dist/Switch/index.d.ts +1 -1
- package/dist/TabList/index.d.ts +1 -1
- package/dist/Table/index.d.ts +1 -1
- package/dist/Table/index.js +1 -1
- package/dist/{Table-BD8MugZo.js → Table-CRKljXg4.js} +6 -6
- package/dist/Text/index.d.ts +1 -1
- package/dist/TextArea/index.d.ts +1 -1
- package/dist/TextInput/index.d.ts +1 -1
- package/dist/Theme/index.d.ts +3 -3
- package/dist/Theme/index.js +1 -1
- package/dist/{Theme-8bDq2nud.js → Theme-iqi6AvU_.js} +363 -39
- package/dist/TimeInput/index.d.ts +1 -1
- package/dist/Timestamp/index.d.ts +1 -1
- package/dist/ToggleButton/index.d.ts +1 -1
- package/dist/Token/index.d.ts +1 -1
- package/dist/Tokenizer/index.d.ts +1 -1
- package/dist/Tokenizer/index.js +1 -1
- package/dist/{Tokenizer-mu0Sak58.js → Tokenizer-CsFiYUJ_.js} +171 -103
- package/dist/Toolbar/index.d.ts +1 -1
- package/dist/Tooltip/index.d.ts +1 -1
- package/dist/TopNav/index.d.ts +1 -1
- package/dist/Typeahead/index.d.ts +2 -1
- package/dist/Typeahead/index.js +2 -2
- package/dist/{Typeahead-Db8Tu27Z.js → Typeahead--kEp21ut.js} +159 -71
- package/dist/{useKeyboardHighlight-CGCf1O57.js → assertOptions-CK_gJ0d3.js} +12 -1
- package/dist/composables/index.d.ts +3 -3
- package/dist/composables/index.js +5 -5
- package/dist/composables-BKHcPgN0.js +5730 -0
- package/dist/{index-CE74Qsbn.d.ts → index-85RFPeV0.d.ts} +10 -10
- package/dist/{index-BFZxzCYu.d.ts → index-9tZJgo0R.d.ts} +2 -2
- package/dist/{index-B-Jla56c.d.ts → index-B0iRA00v.d.ts} +3 -3
- package/dist/{index-LHvLg1Pv.d.ts → index-B95VIgRl.d.ts} +84 -84
- package/dist/{index-DMN_JVEt.d.ts → index-BL4mH8aK.d.ts} +1 -1
- package/dist/{index-Bfm6mF-r.d.ts → index-BfoazhEu.d.ts} +1 -1
- package/dist/{index-CQ1bsxLC.d.ts → index-C1qJlC65.d.ts} +1 -1
- package/dist/{index-ygmRS6Np.d.ts → index-C1zwvxPH.d.ts} +2 -2
- package/dist/{index-gmY3A_Iw.d.ts → index-C93nwgTW.d.ts} +1 -1
- package/dist/{index-BvWBeoa8.d.ts → index-CEjvYAUZ.d.ts} +1 -1
- package/dist/{index-B0yaHi__.d.ts → index-CH-xSiJz.d.ts} +12 -5
- package/dist/{index-sXu15K4j.d.ts → index-CO9GW-ik.d.ts} +1 -1
- package/dist/{index-Xc3q9f84.d.ts → index-COgrQZIL.d.ts} +3 -3
- package/dist/{index-Bgk1Rj2A.d.ts → index-CTjkJP4x.d.ts} +1 -1
- package/dist/{index-BQ3cm7mP.d.ts → index-CbA8rR_b.d.ts} +1 -1
- package/dist/{index-B0R5CLPF.d.ts → index-CsYDrQ9R.d.ts} +1 -1
- package/dist/{index-BWzEt0gT.d.ts → index-CuW4AQ7M.d.ts} +2 -2
- package/dist/{index-BPf_jmME.d.ts → index-Cy1rEWC-.d.ts} +2 -2
- package/dist/{index-D_xBhUV5.d.ts → index-CyMStZq4.d.ts} +10 -10
- package/dist/{index-C2cH8OLC.d.ts → index-D31bgBoy.d.ts} +1 -1
- package/dist/index-DGXCnzOL.d.ts +69 -0
- package/dist/{index-DBwEz4M9.d.ts → index-DK0Wdo9N.d.ts} +13 -13
- package/dist/{index-SWw-WMTO.d.ts → index-DYabo8TI.d.ts} +2 -2
- package/dist/{index-CJdOt5Cc.d.ts → index-DofC9Rll.d.ts} +2 -2
- package/dist/{index-Byan7Dad.d.ts → index-GmoeIuhl.d.ts} +1 -1
- package/dist/{index-unAZVFGz.d.ts → index-Qdbssopn.d.ts} +2 -2
- package/dist/{index-DE1genab.d.ts → index-QnlWyUxN.d.ts} +2 -2
- package/dist/{index-C5e2rZML.d.ts → index-dWlCpnua.d.ts} +2 -2
- package/dist/{index-BPHqOCfp.d.ts → index-lmd9LZKB.d.ts} +4 -4
- package/dist/{index-CtYlR9Cu.d.ts → index-rdwI-mcm.d.ts} +1 -1
- package/dist/{index-BXDqyk2_.d.ts → index-tuNsmJxw.d.ts} +169 -2
- package/dist/{index-BEZ4ezJk.d.ts → index-wY6dKz6c.d.ts} +2 -2
- package/dist/{index-C89cIh4E.d.ts → index-waTkqxTz.d.ts} +1 -1
- package/dist/index.d.ts +39 -38
- package/dist/index.js +26 -26
- package/dist/{loadCatalog-DFyDz_4e.d.ts → loadCatalog-BJwcyX8i.d.ts} +2 -0
- package/dist/{loadCatalog-C3MSknfx.js → loadCatalog-C1kms5sO.js} +5 -1
- package/dist/locale/index.d.ts +2 -2
- package/dist/locale/index.js +3 -3
- package/dist/{locale-OCGw4bi6.js → locale-CHhsd9Mq.js} +1 -1
- package/dist/{typeahead-RkZw0Ktn.js → typeahead-BgfLP7vR.js} +2 -0
- package/dist/{index-DbFIQDoa.d.ts → types-C1ydeFiu.d.ts} +43 -65
- package/dist/{types-uWxXh1Jm.d.ts → types-fRasDCe4.d.ts} +47 -6
- package/dist/{useAsyncSource-Gk0VtE7_.js → useAsyncSource-7GTcK8ep.js} +31 -1
- package/dist/{useAsyncSource-aGzrvRkI.d.ts → useAsyncSource-DbG2FSuI.d.ts} +19 -2
- package/dist/{useCombobox-D374_pT8.js → useCombobox-DIR9yoEu.js} +150 -3
- package/dist/{useTypeaheadAnnouncement-CSikDlpK.js → useTypeaheadMenuConfig--4f7jR6G.js} +264 -20
- package/package.json +1 -1
- package/styles/motion.css +10 -0
- package/styles/selector.css +10 -0
- package/dist/composables-CCsHfpCS.js +0 -1256
- package/dist/{Chat-PEkZTrQE.js → Chat-Wq7e98zS.js} +1 -1
- package/dist/{CodeBlock-9Lfec8C8.js → CodeBlock-CLVXVlB7.js} +1 -1
- package/dist/{DateInput-kXAc_r5m.js → DateInput-BitZfGEY.js} +1 -1
- package/dist/{DateRangeInput-DW7IzFQM.js → DateRangeInput-kxXBsUTU.js} +1 -1
- package/dist/{index-BqhgeAT4.d.ts → index-D8lv2BiD.d.ts} +1 -1
|
@@ -1,1256 +0,0 @@
|
|
|
1
|
-
import { f as pushLayer } from "./interactionModality-B5dR7nnb.js";
|
|
2
|
-
import { t as useMediaQuery } from "./useMediaQuery-BzXGT1Aw.js";
|
|
3
|
-
import { o as useComponentMessages } from "./context-B8IEFCDR.js";
|
|
4
|
-
import { t as _sfc_main$1 } from "./Kbd-CbD993ig.js";
|
|
5
|
-
import { t as keyboardHintMessages } from "./keyboard-hint-KWx0-7kD.js";
|
|
6
|
-
import { r as makeSnapPaging, s as snapPages } from "./snapPagingValue-B5hfph1H.js";
|
|
7
|
-
import { t as useTouchTriggerInternal } from "./useTouchTriggerInternal-Bs7obOrb.js";
|
|
8
|
-
import { Fragment, computed, createBlock, createElementBlock, createElementVNode, defineComponent, getCurrentInstance, h, normalizeStyle, onBeforeUnmount, onMounted, onScopeDispose, openBlock, ref, renderList, toDisplayString, toValue, unref, vShow, watch, withDirectives } from "vue";
|
|
9
|
-
import "../styles/indicator-focus-ring.css";
|
|
10
|
-
import "../styles/keyboard-hint.css";
|
|
11
|
-
//#region src/composables/useContainerReveal.ts
|
|
12
|
-
function useContainerReveal(options = {}) {
|
|
13
|
-
function containerProps(reveal = {}) {
|
|
14
|
-
if (toValue(options.isEnabled) === false) return {};
|
|
15
|
-
return {
|
|
16
|
-
class: {
|
|
17
|
-
"astryx-reveal": true,
|
|
18
|
-
"astryx-reveal--inactive": reveal.forceState === "inactive",
|
|
19
|
-
"astryx-reveal--active": reveal.forceState === "active"
|
|
20
|
-
},
|
|
21
|
-
style: reveal.hoverDelay ? { "--_hover-delay": `${reveal.hoverDelay}ms` } : void 0
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
function contentProps(reveal = {}) {
|
|
25
|
-
if (toValue(options.isEnabled) === false) return {};
|
|
26
|
-
const inverted = reveal.isRevealInverted === true;
|
|
27
|
-
const preserved = reveal.isLayoutPreserved === true;
|
|
28
|
-
return { class: {
|
|
29
|
-
"astryx-reveal__content": !inverted,
|
|
30
|
-
"astryx-reveal__content--preserve": !inverted && preserved,
|
|
31
|
-
"astryx-reveal__conceal": inverted,
|
|
32
|
-
"astryx-reveal__conceal--preserve": inverted && preserved,
|
|
33
|
-
"astryx-reveal__shown": reveal.forceVisibility === "shown",
|
|
34
|
-
"astryx-reveal__hidden": reveal.forceVisibility === "hidden" && !preserved,
|
|
35
|
-
"astryx-reveal__hidden--preserve": reveal.forceVisibility === "hidden" && preserved
|
|
36
|
-
} };
|
|
37
|
-
}
|
|
38
|
-
return {
|
|
39
|
-
containerProps,
|
|
40
|
-
contentProps
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
//#endregion
|
|
44
|
-
//#region src/composables/useControllableState.ts
|
|
45
|
-
/**
|
|
46
|
-
* 受控或自己管的 v-model。
|
|
47
|
-
*
|
|
48
|
-
* Astryx 没有同名 hook。各组件原来各自判断「外面传了值就听外面的,没传就自己记」,
|
|
49
|
-
* 这里收成一个函数,方便以后的 v-model 共用。
|
|
50
|
-
*/
|
|
51
|
-
function useControllableState(options) {
|
|
52
|
-
const { modelValue, onChange } = options;
|
|
53
|
-
const controlled = modelValue.value !== void 0;
|
|
54
|
-
const local = ref(toValue(options.defaultValue));
|
|
55
|
-
const isControlled = computed(() => controlled);
|
|
56
|
-
const value = computed(() => controlled ? modelValue.value : local.value);
|
|
57
|
-
function setValue(next) {
|
|
58
|
-
if (Object.is(value.value, next)) return;
|
|
59
|
-
if (controlled) modelValue.value = next;
|
|
60
|
-
else local.value = next;
|
|
61
|
-
onChange?.(next);
|
|
62
|
-
}
|
|
63
|
-
return {
|
|
64
|
-
isControlled,
|
|
65
|
-
setValue,
|
|
66
|
-
value
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
//#endregion
|
|
70
|
-
//#region src/composables/internal/entryAnimation.ts
|
|
71
|
-
/**
|
|
72
|
-
* 预设对应的共享动画类。时长、缓动和「减少动态效果」的处理都在 `styles/motion.css` 里。
|
|
73
|
-
* 类名里的 above 指元素从下方升上去,below 指从上方落下来。
|
|
74
|
-
*/
|
|
75
|
-
const ENTRY_ANIMATION_CLASSES = {
|
|
76
|
-
slideDown: "astryx-motion-slide-in-below",
|
|
77
|
-
slideUp: "astryx-motion-slide-in-above",
|
|
78
|
-
fadeIn: "astryx-motion-fade-in",
|
|
79
|
-
scaleIn: "astryx-motion-scale-in"
|
|
80
|
-
};
|
|
81
|
-
let hasPaintedPage = false;
|
|
82
|
-
let isTrackingPaint = false;
|
|
83
|
-
function trackFirstPaint() {
|
|
84
|
-
if (isTrackingPaint || typeof window === "undefined" || typeof requestAnimationFrame !== "function") return;
|
|
85
|
-
isTrackingPaint = true;
|
|
86
|
-
requestAnimationFrame(() => {
|
|
87
|
-
requestAnimationFrame(() => {
|
|
88
|
-
hasPaintedPage = true;
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
/** 首屏是否已经画过。服务端没有 window,永远返回 false。 */
|
|
93
|
-
function isAfterFirstPaint() {
|
|
94
|
-
trackFirstPaint();
|
|
95
|
-
return hasPaintedPage;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* 对应原版 Astryx 的 `hooks/useEntryAnimation`。
|
|
99
|
-
*
|
|
100
|
-
* 元素挂载时播一次入场动画,但只对首屏画完之后才插进来的元素生效:
|
|
101
|
-
* 首屏就渲染出来的元素不播放,免得整页一起动。
|
|
102
|
-
*
|
|
103
|
-
* 用法是把返回的 `entryClass` 绑到 `class` 上。动画时长和缓动取自主题的
|
|
104
|
-
* motion token,「减少动态效果」打开时动画会直接落到结束状态。
|
|
105
|
-
*/
|
|
106
|
-
function useEntryAnimation(options = {}) {
|
|
107
|
-
const preset = options.preset ?? "slideDown";
|
|
108
|
-
if (isHydrating()) return { entryClass: null };
|
|
109
|
-
return { entryClass: isAfterFirstPaint() ? ENTRY_ANIMATION_CLASSES[preset] : null };
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* 当前组件是否在接管服务端渲染的 DOM。Vue 接管时先把已有的 DOM 节点交给组件的 vnode,
|
|
113
|
-
* 再运行 setup;普通挂载时这里还是 null。不在组件里调用时返回 false。
|
|
114
|
-
*/
|
|
115
|
-
function isHydrating() {
|
|
116
|
-
return getCurrentInstance()?.vnode.el != null;
|
|
117
|
-
}
|
|
118
|
-
trackFirstPaint();
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/composables/useGridFocus.ts
|
|
121
|
-
/** 网格里一格可以接受的元素:按钮,或者显式可聚焦的元素。 */
|
|
122
|
-
const DEFAULT_CELL_SELECTOR = "button:not([disabled]), [tabindex]:not([tabindex=\"-1\"])";
|
|
123
|
-
/**
|
|
124
|
-
* 对应原版 Astryx 的 `hooks/useGridFocus`。
|
|
125
|
-
*
|
|
126
|
-
* 按 WAI-ARIA 的 grid 模式管二维网格的键盘导航:方向键一格一格走,
|
|
127
|
-
* Home/End 走到行首行尾,Ctrl+Home/Ctrl+End 走到整张网格的开头和结尾,
|
|
128
|
-
* Page Up/Down 交给调用方(日历里就是翻月)。走到边界时可以把事件交出去,
|
|
129
|
-
* 让别的网格接住焦点,例如翻到上个月继续用方向键。
|
|
130
|
-
*
|
|
131
|
-
* 它靠读 DOM 里格子的顺序算行列,所以格子要不要能聚焦、要不要聚焦格子里的某个按钮,
|
|
132
|
-
* 都是通过 `isCellFocusable` 和 `getFocusTarget` 两个回调交给调用方决定。
|
|
133
|
-
*/
|
|
134
|
-
function useGridFocus(options) {
|
|
135
|
-
const gridRef = ref(null);
|
|
136
|
-
const cellSelector = options.cellSelector ?? DEFAULT_CELL_SELECTOR;
|
|
137
|
-
function isRoving() {
|
|
138
|
-
return toValue(options.hasRovingTabIndex ?? false);
|
|
139
|
-
}
|
|
140
|
-
function getCells() {
|
|
141
|
-
const grid = gridRef.value;
|
|
142
|
-
if (grid == null) return [];
|
|
143
|
-
return [...grid.querySelectorAll(cellSelector)];
|
|
144
|
-
}
|
|
145
|
-
/** 能接焦点的格子,按 DOM 顺序。 */
|
|
146
|
-
function getFocusableCells() {
|
|
147
|
-
return getCells().filter((cell) => options.isCellFocusable?.(cell) ?? true);
|
|
148
|
-
}
|
|
149
|
-
function resolveTarget(cell) {
|
|
150
|
-
return options.getFocusTarget?.(cell) ?? cell;
|
|
151
|
-
}
|
|
152
|
-
function indexOfTarget(target) {
|
|
153
|
-
if (target == null) return -1;
|
|
154
|
-
return getCells().findIndex((cell) => cell === target || cell.contains(target));
|
|
155
|
-
}
|
|
156
|
-
function activeIndex() {
|
|
157
|
-
const grid = gridRef.value;
|
|
158
|
-
if (grid == null) return -1;
|
|
159
|
-
const focused = grid.ownerDocument.activeElement;
|
|
160
|
-
if (focused == null || !grid.contains(focused)) return -1;
|
|
161
|
-
return indexOfTarget(focused);
|
|
162
|
-
}
|
|
163
|
-
/** 让当前格子成为唯一的 Tab 落脚点,其余格子退出 Tab 顺序。 */
|
|
164
|
-
function syncRovingTabIndex(index) {
|
|
165
|
-
if (!isRoving()) return;
|
|
166
|
-
getCells().forEach((cell, cellIndex) => {
|
|
167
|
-
const target = resolveTarget(cell);
|
|
168
|
-
if (target != null) target.tabIndex = cellIndex === index ? 0 : -1;
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
function focusTargetOf(cell) {
|
|
172
|
-
if (cell == null) return false;
|
|
173
|
-
if (!(options.isCellFocusable?.(cell) ?? true)) return false;
|
|
174
|
-
const target = resolveTarget(cell);
|
|
175
|
-
if (target == null) return false;
|
|
176
|
-
target.focus({ preventScroll: true });
|
|
177
|
-
syncRovingTabIndex(getCells().indexOf(cell));
|
|
178
|
-
return true;
|
|
179
|
-
}
|
|
180
|
-
function columns() {
|
|
181
|
-
return Math.max(1, toValue(options.columns));
|
|
182
|
-
}
|
|
183
|
-
function columnOf(index) {
|
|
184
|
-
return index < 0 ? 0 : index % columns();
|
|
185
|
-
}
|
|
186
|
-
/** 走到边界外面时,把当前列和步长交给调用方,让另一张网格接住焦点。 */
|
|
187
|
-
function navigateOut(current, offset, isAfter) {
|
|
188
|
-
if (current < 0) return;
|
|
189
|
-
const column = columnOf(current);
|
|
190
|
-
if (isAfter) options.onNavigateAfter?.(column, offset);
|
|
191
|
-
else options.onNavigateBefore?.(column, offset);
|
|
192
|
-
}
|
|
193
|
-
function moveTo(index, offset) {
|
|
194
|
-
const cells = getCells();
|
|
195
|
-
const target = cells[index];
|
|
196
|
-
if (target == null) {
|
|
197
|
-
navigateOut(activeIndex(), offset, index >= cells.length);
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
if (!(options.isCellFocusable?.(target) ?? true)) return;
|
|
201
|
-
focusTargetOf(target);
|
|
202
|
-
}
|
|
203
|
-
function handleKeyDown(event) {
|
|
204
|
-
if (event.defaultPrevented) return;
|
|
205
|
-
if (gridRef.value == null) return;
|
|
206
|
-
const index = activeIndex();
|
|
207
|
-
if (index < 0) return;
|
|
208
|
-
const cols = columns();
|
|
209
|
-
const last = getCells().length - 1;
|
|
210
|
-
const rowStart = index - columnOf(index);
|
|
211
|
-
switch (event.key) {
|
|
212
|
-
case "ArrowRight":
|
|
213
|
-
event.preventDefault();
|
|
214
|
-
moveTo(index + 1, 1);
|
|
215
|
-
break;
|
|
216
|
-
case "ArrowLeft":
|
|
217
|
-
event.preventDefault();
|
|
218
|
-
moveTo(index - 1, 1);
|
|
219
|
-
break;
|
|
220
|
-
case "ArrowDown":
|
|
221
|
-
event.preventDefault();
|
|
222
|
-
moveTo(index + cols, cols);
|
|
223
|
-
break;
|
|
224
|
-
case "ArrowUp":
|
|
225
|
-
event.preventDefault();
|
|
226
|
-
moveTo(index - cols, cols);
|
|
227
|
-
break;
|
|
228
|
-
case "Home":
|
|
229
|
-
event.preventDefault();
|
|
230
|
-
moveTo(event.ctrlKey || event.metaKey ? 0 : rowStart, 1);
|
|
231
|
-
break;
|
|
232
|
-
case "End":
|
|
233
|
-
event.preventDefault();
|
|
234
|
-
moveTo(event.ctrlKey || event.metaKey ? last : rowStart + cols - 1, 1);
|
|
235
|
-
break;
|
|
236
|
-
case "PageUp":
|
|
237
|
-
if (options.onPageUp == null) break;
|
|
238
|
-
event.preventDefault();
|
|
239
|
-
options.onPageUp();
|
|
240
|
-
break;
|
|
241
|
-
case "PageDown":
|
|
242
|
-
if (options.onPageDown == null) break;
|
|
243
|
-
event.preventDefault();
|
|
244
|
-
options.onPageDown();
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
function handleFocus(event) {
|
|
248
|
-
if (!isRoving()) return;
|
|
249
|
-
const target = event.target;
|
|
250
|
-
if (!(target instanceof Element)) return;
|
|
251
|
-
const index = indexOfTarget(target);
|
|
252
|
-
if (index < 0) return;
|
|
253
|
-
syncRovingTabIndex(index);
|
|
254
|
-
}
|
|
255
|
-
function focusCell(index) {
|
|
256
|
-
const cells = getCells();
|
|
257
|
-
if (cells.length === 0) return;
|
|
258
|
-
focusTargetOf(cells[Math.min(Math.max(index, 0), cells.length - 1)]);
|
|
259
|
-
}
|
|
260
|
-
function focusFirst() {
|
|
261
|
-
focusTargetOf(getFocusableCells()[0]);
|
|
262
|
-
}
|
|
263
|
-
function focusLast() {
|
|
264
|
-
const cells = getFocusableCells();
|
|
265
|
-
focusTargetOf(cells[cells.length - 1]);
|
|
266
|
-
}
|
|
267
|
-
/** 首次渲染后补上 Tab 落脚点;已经有一个 `tabindex="0"` 的格子就尊重它。 */
|
|
268
|
-
function seedRovingTabIndex() {
|
|
269
|
-
if (!isRoving()) return;
|
|
270
|
-
const cells = getCells();
|
|
271
|
-
const seeded = cells.findIndex((cell) => {
|
|
272
|
-
const target = resolveTarget(cell);
|
|
273
|
-
return target != null && target.tabIndex === 0 && (options.isCellFocusable?.(cell) ?? true);
|
|
274
|
-
});
|
|
275
|
-
if (seeded >= 0) {
|
|
276
|
-
syncRovingTabIndex(seeded);
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
const first = cells.findIndex((cell) => options.isCellFocusable?.(cell) ?? true);
|
|
280
|
-
if (first >= 0) syncRovingTabIndex(first);
|
|
281
|
-
}
|
|
282
|
-
onMounted(seedRovingTabIndex);
|
|
283
|
-
watch(gridRef, seedRovingTabIndex);
|
|
284
|
-
watch([() => toValue(options.columns), () => toValue(options.hasRovingTabIndex ?? false)], seedRovingTabIndex);
|
|
285
|
-
onScopeDispose(() => {
|
|
286
|
-
gridRef.value = null;
|
|
287
|
-
});
|
|
288
|
-
return {
|
|
289
|
-
gridRef,
|
|
290
|
-
handleKeyDown,
|
|
291
|
-
handleFocus,
|
|
292
|
-
focusCell,
|
|
293
|
-
focusFirst,
|
|
294
|
-
focusLast
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
//#endregion
|
|
298
|
-
//#region src/composables/useGridRovingFocus.ts
|
|
299
|
-
/**
|
|
300
|
-
* 网格式导航(roving tabindex):用方向键在二维网格中移动焦点。
|
|
301
|
-
* 供 Calendar、Pagination(如果需要)等组件复用。
|
|
302
|
-
*/
|
|
303
|
-
function useGridRovingFocus(options) {
|
|
304
|
-
const { rows, columns, activeIndex, isEnabled = ref(true), isDisabled, onChange } = options;
|
|
305
|
-
const row = ref(0);
|
|
306
|
-
const column = ref(0);
|
|
307
|
-
watch(activeIndex, (index) => {
|
|
308
|
-
const cols = columns.value;
|
|
309
|
-
if (cols > 0) {
|
|
310
|
-
row.value = Math.floor(index / cols);
|
|
311
|
-
column.value = index % cols;
|
|
312
|
-
}
|
|
313
|
-
}, { immediate: true });
|
|
314
|
-
function indexFromRowCol(r, c) {
|
|
315
|
-
return r * columns.value + c;
|
|
316
|
-
}
|
|
317
|
-
function moveTo(targetRow, targetCol) {
|
|
318
|
-
if (!isEnabled.value) return;
|
|
319
|
-
const r = Math.max(0, Math.min(targetRow, rows.value - 1));
|
|
320
|
-
const c = Math.max(0, Math.min(targetCol, columns.value - 1));
|
|
321
|
-
if (isDisabled?.(r, c)) return;
|
|
322
|
-
const index = indexFromRowCol(r, c);
|
|
323
|
-
onChange(index);
|
|
324
|
-
}
|
|
325
|
-
function moveUp() {
|
|
326
|
-
moveTo(row.value - 1, column.value);
|
|
327
|
-
}
|
|
328
|
-
function moveDown() {
|
|
329
|
-
moveTo(row.value + 1, column.value);
|
|
330
|
-
}
|
|
331
|
-
function moveLeft() {
|
|
332
|
-
moveTo(row.value, column.value - 1);
|
|
333
|
-
}
|
|
334
|
-
function moveRight() {
|
|
335
|
-
moveTo(row.value, column.value + 1);
|
|
336
|
-
}
|
|
337
|
-
function moveToFirstInRow() {
|
|
338
|
-
moveTo(row.value, 0);
|
|
339
|
-
}
|
|
340
|
-
function moveToLastInRow() {
|
|
341
|
-
moveTo(row.value, columns.value - 1);
|
|
342
|
-
}
|
|
343
|
-
function moveToFirstInColumn() {
|
|
344
|
-
moveTo(0, column.value);
|
|
345
|
-
}
|
|
346
|
-
function moveToLastInColumn() {
|
|
347
|
-
moveTo(rows.value - 1, column.value);
|
|
348
|
-
}
|
|
349
|
-
function onKeyDown(event) {
|
|
350
|
-
if (!isEnabled.value) return;
|
|
351
|
-
switch (event.key) {
|
|
352
|
-
case "ArrowUp":
|
|
353
|
-
event.preventDefault();
|
|
354
|
-
moveUp();
|
|
355
|
-
break;
|
|
356
|
-
case "ArrowDown":
|
|
357
|
-
event.preventDefault();
|
|
358
|
-
moveDown();
|
|
359
|
-
break;
|
|
360
|
-
case "ArrowLeft":
|
|
361
|
-
event.preventDefault();
|
|
362
|
-
moveLeft();
|
|
363
|
-
break;
|
|
364
|
-
case "ArrowRight":
|
|
365
|
-
event.preventDefault();
|
|
366
|
-
moveRight();
|
|
367
|
-
break;
|
|
368
|
-
case "Home":
|
|
369
|
-
event.preventDefault();
|
|
370
|
-
if (event.ctrlKey) moveTo(0, 0);
|
|
371
|
-
else moveToFirstInRow();
|
|
372
|
-
break;
|
|
373
|
-
case "End":
|
|
374
|
-
event.preventDefault();
|
|
375
|
-
if (event.ctrlKey) moveTo(rows.value - 1, columns.value - 1);
|
|
376
|
-
else moveToLastInRow();
|
|
377
|
-
break;
|
|
378
|
-
case "PageUp":
|
|
379
|
-
event.preventDefault();
|
|
380
|
-
moveToFirstInColumn();
|
|
381
|
-
break;
|
|
382
|
-
case "PageDown":
|
|
383
|
-
event.preventDefault();
|
|
384
|
-
moveToLastInColumn();
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return {
|
|
388
|
-
row,
|
|
389
|
-
column,
|
|
390
|
-
onKeyDown,
|
|
391
|
-
moveTo
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
//#endregion
|
|
395
|
-
//#region src/composables/useImageMode.ts
|
|
396
|
-
/**
|
|
397
|
-
* APCA 感知亮度。先把 sRGB 按 2.4 线性化,再用接近 BT.709 的系数合成,
|
|
398
|
-
* 最后做 0.56 次方,让 0.5 接近视觉上的中灰。饱和色比简单的伽马亮度更准。
|
|
399
|
-
*/
|
|
400
|
-
function perceptualLightness(r, g, b) {
|
|
401
|
-
const linear = (channel) => (channel / 255) ** 2.4;
|
|
402
|
-
return (.2126729 * linear(r) + .7151522 * linear(g) + .072175 * linear(b)) ** .56;
|
|
403
|
-
}
|
|
404
|
-
/** 一组采样像素的平均亮度,再和阈值比出深浅。 */
|
|
405
|
-
function imageModeFromPixels(pixels, threshold = .5) {
|
|
406
|
-
let red = 0;
|
|
407
|
-
let green = 0;
|
|
408
|
-
let blue = 0;
|
|
409
|
-
let count = 0;
|
|
410
|
-
for (let index = 0; index < pixels.length; index += 4) {
|
|
411
|
-
red += pixels[index] ?? 0;
|
|
412
|
-
green += pixels[index + 1] ?? 0;
|
|
413
|
-
blue += pixels[index + 2] ?? 0;
|
|
414
|
-
count += 1;
|
|
415
|
-
}
|
|
416
|
-
const safeCount = count || 1;
|
|
417
|
-
return perceptualLightness(red / safeCount, green / safeCount, blue / safeCount) > threshold ? "light" : "dark";
|
|
418
|
-
}
|
|
419
|
-
/**
|
|
420
|
-
* 按图片亮度判断该用深色还是浅色前景。采样走 OffscreenCanvas,不往页面上画。
|
|
421
|
-
* 跨域或网络失败时返回 `fallback`。
|
|
422
|
-
*/
|
|
423
|
-
function useImageMode(src, options = {}) {
|
|
424
|
-
const mode = ref(toValue(options).fallback ?? null);
|
|
425
|
-
let request = 0;
|
|
426
|
-
async function detect(url) {
|
|
427
|
-
const current = ++request;
|
|
428
|
-
const settings = toValue(options);
|
|
429
|
-
const threshold = settings.threshold ?? .5;
|
|
430
|
-
const fallback = settings.fallback ?? null;
|
|
431
|
-
mode.value = fallback;
|
|
432
|
-
try {
|
|
433
|
-
const response = await fetch(url);
|
|
434
|
-
if (!response.ok) throw new Error(`image ${response.status}`);
|
|
435
|
-
const bitmap = await createImageBitmap(await response.blob());
|
|
436
|
-
if (current !== request) return;
|
|
437
|
-
const region = settings.region;
|
|
438
|
-
const sx = region ? Math.round(region.x * bitmap.width) : 0;
|
|
439
|
-
const sy = region ? Math.round(region.y * bitmap.height) : 0;
|
|
440
|
-
const sw = region ? Math.max(1, Math.round(region.width * bitmap.width)) : bitmap.width;
|
|
441
|
-
const sh = region ? Math.max(1, Math.round(region.height * bitmap.height)) : bitmap.height;
|
|
442
|
-
const sample = 10;
|
|
443
|
-
const context = new OffscreenCanvas(sample, sample).getContext("2d");
|
|
444
|
-
if (!context) return;
|
|
445
|
-
context.drawImage(bitmap, sx, sy, sw, sh, 0, 0, sample, sample);
|
|
446
|
-
const pixels = context.getImageData(0, 0, sample, sample).data;
|
|
447
|
-
if (current !== request) return;
|
|
448
|
-
mode.value = imageModeFromPixels(pixels, threshold);
|
|
449
|
-
} catch {
|
|
450
|
-
if (current === request) mode.value = fallback;
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
const sampleKey = computed(() => {
|
|
454
|
-
const { region, threshold } = toValue(options);
|
|
455
|
-
return [
|
|
456
|
-
toValue(src),
|
|
457
|
-
region?.x,
|
|
458
|
-
region?.y,
|
|
459
|
-
region?.width,
|
|
460
|
-
region?.height,
|
|
461
|
-
threshold
|
|
462
|
-
].join("|");
|
|
463
|
-
});
|
|
464
|
-
watch(sampleKey, () => {
|
|
465
|
-
const url = toValue(src);
|
|
466
|
-
if (!url) {
|
|
467
|
-
request += 1;
|
|
468
|
-
mode.value = toValue(options).fallback ?? null;
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
detect(url);
|
|
472
|
-
}, { immediate: true });
|
|
473
|
-
onScopeDispose(() => {
|
|
474
|
-
request += 1;
|
|
475
|
-
});
|
|
476
|
-
return mode;
|
|
477
|
-
}
|
|
478
|
-
//#endregion
|
|
479
|
-
//#region src/composables/useIndicatorFocusRing.ts
|
|
480
|
-
/** 读不到 `:focus-visible` 时宁可按可见处理,不能把焦点环弄丢(WCAG 2.4.7)。 */
|
|
481
|
-
function isFocusVisible(target) {
|
|
482
|
-
try {
|
|
483
|
-
return target.matches(":focus-visible");
|
|
484
|
-
} catch {
|
|
485
|
-
return true;
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* 对应原版 Astryx 的 `hooks/useIndicatorFocusRing`。
|
|
490
|
-
*
|
|
491
|
-
* 给「真输入框藏起来、画面上的图形代替它」的控件画标准焦点环:复选框、
|
|
492
|
-
* 单选框都把真正的 input 设成透明,焦点环只能画在旁边那个图形上。
|
|
493
|
-
*
|
|
494
|
-
* 环画在指示器自己的元素上,只有这个元素自己的圆角能决定环的形状;
|
|
495
|
-
* 而且只在 `:focus-visible` 时画,鼠标点一下不会亮。统一放在这里管,
|
|
496
|
-
* 主题换掉指示器也照样有可见焦点,不会因为漏了一个属性就整块没法用键盘。
|
|
497
|
-
*/
|
|
498
|
-
function useIndicatorFocusRing(options) {
|
|
499
|
-
function indicator() {
|
|
500
|
-
const child = options.containerRef.value?.firstElementChild;
|
|
501
|
-
return child instanceof HTMLElement ? child : null;
|
|
502
|
-
}
|
|
503
|
-
/** 标记属性 `data-astryx-focus-ring` 的画法是 `styles/indicator-focus-ring.css`。 */
|
|
504
|
-
function clear() {
|
|
505
|
-
const element = indicator();
|
|
506
|
-
if (element != null) delete element.dataset.astryxFocusRing;
|
|
507
|
-
}
|
|
508
|
-
function onFocus(event) {
|
|
509
|
-
if (toValue(options.isDisabled ?? false)) {
|
|
510
|
-
clear();
|
|
511
|
-
return;
|
|
512
|
-
}
|
|
513
|
-
const target = event.target;
|
|
514
|
-
if (!(target instanceof Element) || !isFocusVisible(target)) {
|
|
515
|
-
clear();
|
|
516
|
-
return;
|
|
517
|
-
}
|
|
518
|
-
const element = indicator();
|
|
519
|
-
if (element != null) element.dataset.astryxFocusRing = "";
|
|
520
|
-
}
|
|
521
|
-
function onBlur() {
|
|
522
|
-
clear();
|
|
523
|
-
}
|
|
524
|
-
return { focusProps: {
|
|
525
|
-
onFocus,
|
|
526
|
-
onBlur
|
|
527
|
-
} };
|
|
528
|
-
}
|
|
529
|
-
//#endregion
|
|
530
|
-
//#region src/composables/KeyboardHint.vue
|
|
531
|
-
const _hoisted_1 = { class: "astryx-keyboard-hint__keys" };
|
|
532
|
-
const _hoisted_2 = { class: "astryx-keyboard-hint__label" };
|
|
533
|
-
const _sfc_main = /*@__PURE__*/ defineComponent({
|
|
534
|
-
__name: "KeyboardHint",
|
|
535
|
-
props: {
|
|
536
|
-
visible: { type: Boolean },
|
|
537
|
-
anchor: {},
|
|
538
|
-
orientation: {}
|
|
539
|
-
},
|
|
540
|
-
setup(__props) {
|
|
541
|
-
const props = __props;
|
|
542
|
-
const messages = useComponentMessages({ keyboardHint: keyboardHintMessages });
|
|
543
|
-
const hint = ref(null);
|
|
544
|
-
const position = ref({
|
|
545
|
-
top: 0,
|
|
546
|
-
left: 0
|
|
547
|
-
});
|
|
548
|
-
const keys = computed(() => {
|
|
549
|
-
if (props.orientation === "vertical") return ["up", "down"];
|
|
550
|
-
if (props.orientation === "both") return [
|
|
551
|
-
"left",
|
|
552
|
-
"right",
|
|
553
|
-
"up",
|
|
554
|
-
"down"
|
|
555
|
-
];
|
|
556
|
-
return ["left", "right"];
|
|
557
|
-
});
|
|
558
|
-
const style = computed(() => ({
|
|
559
|
-
top: `${position.value.top}px`,
|
|
560
|
-
left: `${position.value.left}px`
|
|
561
|
-
}));
|
|
562
|
-
function place() {
|
|
563
|
-
const anchor = props.anchor;
|
|
564
|
-
const element = hint.value;
|
|
565
|
-
if (!anchor || !element) return;
|
|
566
|
-
const rect = anchor.getBoundingClientRect();
|
|
567
|
-
const hintRect = element.getBoundingClientRect();
|
|
568
|
-
const top = Math.max(8, rect.top - hintRect.height - 8);
|
|
569
|
-
const left = Math.min(window.innerWidth - hintRect.width - 8, Math.max(8, rect.left + rect.width / 2 - hintRect.width / 2));
|
|
570
|
-
position.value = {
|
|
571
|
-
top,
|
|
572
|
-
left
|
|
573
|
-
};
|
|
574
|
-
}
|
|
575
|
-
watch(() => [props.visible, props.anchor], () => {
|
|
576
|
-
if (!props.visible) return;
|
|
577
|
-
requestAnimationFrame(() => place());
|
|
578
|
-
});
|
|
579
|
-
function onScroll() {
|
|
580
|
-
if (props.visible) place();
|
|
581
|
-
}
|
|
582
|
-
onMounted(() => {
|
|
583
|
-
window.addEventListener("scroll", onScroll, true);
|
|
584
|
-
window.addEventListener("resize", onScroll);
|
|
585
|
-
});
|
|
586
|
-
onBeforeUnmount(() => {
|
|
587
|
-
window.removeEventListener("scroll", onScroll, true);
|
|
588
|
-
window.removeEventListener("resize", onScroll);
|
|
589
|
-
});
|
|
590
|
-
return (_ctx, _cache) => {
|
|
591
|
-
return withDirectives((openBlock(), createElementBlock("div", {
|
|
592
|
-
ref_key: "hint",
|
|
593
|
-
ref: hint,
|
|
594
|
-
class: "astryx-keyboard-hint",
|
|
595
|
-
"aria-hidden": "true",
|
|
596
|
-
style: normalizeStyle(style.value)
|
|
597
|
-
}, [createElementVNode("span", _hoisted_1, [(openBlock(true), createElementBlock(Fragment, null, renderList(keys.value, (key) => {
|
|
598
|
-
return openBlock(), createBlock(_sfc_main$1, {
|
|
599
|
-
key,
|
|
600
|
-
keys: key
|
|
601
|
-
}, null, 8, ["keys"]);
|
|
602
|
-
}), 128))]), createElementVNode("span", _hoisted_2, toDisplayString(unref(messages).keyboardHint.navigate), 1)], 4)), [[vShow, __props.visible]]);
|
|
603
|
-
};
|
|
604
|
-
}
|
|
605
|
-
});
|
|
606
|
-
//#endregion
|
|
607
|
-
//#region src/composables/useKeyboardHint.ts
|
|
608
|
-
const ARROW_KEYS = /* @__PURE__ */ new Set([
|
|
609
|
-
"ArrowLeft",
|
|
610
|
-
"ArrowRight",
|
|
611
|
-
"ArrowUp",
|
|
612
|
-
"ArrowDown"
|
|
613
|
-
]);
|
|
614
|
-
/**
|
|
615
|
-
* 一组方向键控件第一次被键盘点进来时,在当前项旁边短暂提示「用方向键移动」。
|
|
616
|
-
* 提示显示期间登记在共用的关闭栈里,Escape 只关它,焦点不动。
|
|
617
|
-
* 按过方向键、Escape、焦点离开(或设了时限且超时)之后不再出现。
|
|
618
|
-
*/
|
|
619
|
-
function useKeyboardHint(options = {}) {
|
|
620
|
-
const visible = ref(false);
|
|
621
|
-
const anchor = ref(null);
|
|
622
|
-
const orientation = computed(() => options.orientation ?? "horizontal");
|
|
623
|
-
let dismissed = false;
|
|
624
|
-
let timer;
|
|
625
|
-
function hide() {
|
|
626
|
-
visible.value = false;
|
|
627
|
-
if (timer != null) {
|
|
628
|
-
clearTimeout(timer);
|
|
629
|
-
timer = void 0;
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
function show(target) {
|
|
633
|
-
if (dismissed || options.isEnabled === false) return;
|
|
634
|
-
anchor.value = target;
|
|
635
|
-
visible.value = true;
|
|
636
|
-
if (timer != null) clearTimeout(timer);
|
|
637
|
-
timer = void 0;
|
|
638
|
-
if (options.dismissAfterMs != null) timer = setTimeout(() => {
|
|
639
|
-
dismissed = true;
|
|
640
|
-
hide();
|
|
641
|
-
}, options.dismissAfterMs);
|
|
642
|
-
}
|
|
643
|
-
const token = {};
|
|
644
|
-
let unregister = null;
|
|
645
|
-
function release() {
|
|
646
|
-
unregister?.();
|
|
647
|
-
unregister = null;
|
|
648
|
-
}
|
|
649
|
-
watch(visible, (shown) => {
|
|
650
|
-
release();
|
|
651
|
-
if (!shown) return;
|
|
652
|
-
unregister = pushLayer({
|
|
653
|
-
token,
|
|
654
|
-
getTrigger: () => anchor.value,
|
|
655
|
-
dismissOnPointerOutside: false,
|
|
656
|
-
dismissOnEscape: "close",
|
|
657
|
-
onDismiss() {
|
|
658
|
-
dismissed = true;
|
|
659
|
-
hide();
|
|
660
|
-
}
|
|
661
|
-
});
|
|
662
|
-
}, { flush: "post" });
|
|
663
|
-
function onFocus(event) {
|
|
664
|
-
if (dismissed || options.isEnabled === false) return;
|
|
665
|
-
const target = event.target;
|
|
666
|
-
const current = event.currentTarget;
|
|
667
|
-
if (!(target instanceof HTMLElement) || !(current instanceof HTMLElement)) return;
|
|
668
|
-
if (event.relatedTarget instanceof Node && current.contains(event.relatedTarget)) {
|
|
669
|
-
if (visible.value) anchor.value = target;
|
|
670
|
-
return;
|
|
671
|
-
}
|
|
672
|
-
if (!target.matches(":focus-visible")) return;
|
|
673
|
-
show(target);
|
|
674
|
-
}
|
|
675
|
-
function onBlur(event) {
|
|
676
|
-
const current = event.currentTarget;
|
|
677
|
-
if (!(current instanceof HTMLElement)) return;
|
|
678
|
-
if (event.relatedTarget instanceof Node && current.contains(event.relatedTarget)) return;
|
|
679
|
-
dismissed = true;
|
|
680
|
-
hide();
|
|
681
|
-
}
|
|
682
|
-
function onKeyDown(event) {
|
|
683
|
-
if (!ARROW_KEYS.has(event.key)) return;
|
|
684
|
-
dismissed = true;
|
|
685
|
-
hide();
|
|
686
|
-
}
|
|
687
|
-
onBeforeUnmount(() => {
|
|
688
|
-
release();
|
|
689
|
-
if (timer != null) clearTimeout(timer);
|
|
690
|
-
});
|
|
691
|
-
return {
|
|
692
|
-
visible,
|
|
693
|
-
anchor,
|
|
694
|
-
orientation,
|
|
695
|
-
onFocus,
|
|
696
|
-
onBlur,
|
|
697
|
-
onKeyDown,
|
|
698
|
-
Hint: defineComponent({
|
|
699
|
-
name: "AstryxKeyboardHint",
|
|
700
|
-
setup() {
|
|
701
|
-
return () => h(_sfc_main, {
|
|
702
|
-
visible: visible.value,
|
|
703
|
-
anchor: anchor.value,
|
|
704
|
-
orientation: toValue(orientation)
|
|
705
|
-
});
|
|
706
|
-
}
|
|
707
|
-
})
|
|
708
|
-
};
|
|
709
|
-
}
|
|
710
|
-
//#endregion
|
|
711
|
-
//#region src/composables/internal/snapPagingMotion.ts
|
|
712
|
-
const browserClock = {
|
|
713
|
-
now: () => performance.now(),
|
|
714
|
-
frame: (callback) => requestAnimationFrame(() => callback()),
|
|
715
|
-
cancel: (handle) => cancelAnimationFrame(handle)
|
|
716
|
-
};
|
|
717
|
-
/** 只看最近这么久的位置算速度。 */
|
|
718
|
-
const VELOCITY_WINDOW_MS = 100;
|
|
719
|
-
/** 最后一次移动之后停了这么久,就当手指已经停住,速度为 0。 */
|
|
720
|
-
const STILL_AFTER_MS = 50;
|
|
721
|
-
/** 记录拖动中的位置,算出松手时的速度(像素每秒,朝末端为正)。 */
|
|
722
|
-
function createVelocitySampler() {
|
|
723
|
-
let samples = [];
|
|
724
|
-
return {
|
|
725
|
-
add(time, position) {
|
|
726
|
-
samples.push({
|
|
727
|
-
time,
|
|
728
|
-
position
|
|
729
|
-
});
|
|
730
|
-
samples = samples.filter((sample) => time - sample.time <= VELOCITY_WINDOW_MS);
|
|
731
|
-
},
|
|
732
|
-
reset() {
|
|
733
|
-
samples = [];
|
|
734
|
-
},
|
|
735
|
-
velocity(time) {
|
|
736
|
-
const last = samples[samples.length - 1];
|
|
737
|
-
const first = samples[0];
|
|
738
|
-
if (last == null || first == null || last === first || time - last.time > STILL_AFTER_MS) return 0;
|
|
739
|
-
const elapsed = last.time - first.time;
|
|
740
|
-
return elapsed > 0 ? (last.position - first.position) / elapsed * 1e3 : 0;
|
|
741
|
-
},
|
|
742
|
-
lastTime() {
|
|
743
|
-
return samples[samples.length - 1]?.time ?? null;
|
|
744
|
-
}
|
|
745
|
-
};
|
|
746
|
-
}
|
|
747
|
-
/** 下标列表里离 `to` 最近的那一个。 */
|
|
748
|
-
function nearestIndex(stops, to) {
|
|
749
|
-
let best = 0;
|
|
750
|
-
for (let i = 1; i < stops.length; i++) if (Math.abs(stops[i] - to) < Math.abs(stops[best] - to)) best = i;
|
|
751
|
-
return best;
|
|
752
|
-
}
|
|
753
|
-
/**
|
|
754
|
-
* 从第 `anchor` 个停点算起的「一屏一页」:每一页落在离上一页一屏远最近的停点上,
|
|
755
|
-
* 并且至少往前走一个停点。
|
|
756
|
-
*/
|
|
757
|
-
function viewPages(stops, anchor, width) {
|
|
758
|
-
const pages = [stops[anchor]];
|
|
759
|
-
for (let i = anchor; i < stops.length - 1;) {
|
|
760
|
-
i = Math.max(nearestIndex(stops, stops[i] + width), i + 1);
|
|
761
|
-
pages.push(stops[i]);
|
|
762
|
-
}
|
|
763
|
-
for (let i = anchor; i > 0;) {
|
|
764
|
-
i = Math.min(nearestIndex(stops, stops[i] - width), i - 1);
|
|
765
|
-
pages.unshift(stops[i]);
|
|
766
|
-
}
|
|
767
|
-
return pages;
|
|
768
|
-
}
|
|
769
|
-
/** 松手后按这个时长(秒)把速度投出去,再找离投点最近的一页。 */
|
|
770
|
-
const PROJECTION_SECONDS = .1;
|
|
771
|
-
/**
|
|
772
|
-
* 松手时停在哪一页:轻扫至少翻到运动方向上的下一页,慢慢松手就停在投点最近的一页。
|
|
773
|
-
* 不论多快,离起手那一页(`anchor`)最多一页。返回页的下标。
|
|
774
|
-
*/
|
|
775
|
-
function pickPage(pages, at, velocity, anchor) {
|
|
776
|
-
let index = nearestIndex(pages, at + velocity * PROJECTION_SECONDS);
|
|
777
|
-
if (velocity >= 300) {
|
|
778
|
-
const ahead = pages.findIndex((page) => page > at + .5);
|
|
779
|
-
if (ahead !== -1) index = Math.max(index, ahead);
|
|
780
|
-
} else if (velocity <= -300) {
|
|
781
|
-
let behind = -1;
|
|
782
|
-
for (let i = pages.length - 1; i >= 0; i--) if (pages[i] < at - .5) {
|
|
783
|
-
behind = i;
|
|
784
|
-
break;
|
|
785
|
-
}
|
|
786
|
-
if (behind !== -1) index = Math.min(index, behind);
|
|
787
|
-
}
|
|
788
|
-
return Math.min(Math.max(index, anchor - 1, 0), anchor + 1, pages.length - 1);
|
|
789
|
-
}
|
|
790
|
-
/** 弹簧的固有角频率:大约 0.4 秒走完一页,接近 UIKit 翻页的手感。 */
|
|
791
|
-
const SPRING_OMEGA = 2 * Math.PI / .4;
|
|
792
|
-
/** 轻扫很快时弹簧可以收得更紧,但不超过这个角频率。 */
|
|
793
|
-
const MAX_OMEGA = 60;
|
|
794
|
-
const REST_DISTANCE = .5;
|
|
795
|
-
const REST_VELOCITY = 20;
|
|
796
|
-
/**
|
|
797
|
-
* 用临界阻尼弹簧把位置从 `from` 带到 `to`,带着初速度 `velocity`(像素每秒)。
|
|
798
|
-
* 每一帧调用 `onFrame`,停稳后最后一次给出 `to` 并调用 `onRest`。
|
|
799
|
-
*/
|
|
800
|
-
function springTo(clock, from, to, velocity, onFrame, onRest) {
|
|
801
|
-
const start = clock.now();
|
|
802
|
-
const offset0 = from - to;
|
|
803
|
-
const toward = offset0 === 0 ? 0 : -velocity * Math.sign(offset0);
|
|
804
|
-
const omega = Math.max(SPRING_OMEGA, Math.min(MAX_OMEGA, toward / Math.max(Math.abs(offset0), 1e-6)));
|
|
805
|
-
let handle = 0;
|
|
806
|
-
let stopped = false;
|
|
807
|
-
function state(time) {
|
|
808
|
-
const t = Math.max(0, time - start) / 1e3;
|
|
809
|
-
const decay = Math.exp(-omega * t);
|
|
810
|
-
const b = velocity + omega * offset0;
|
|
811
|
-
return {
|
|
812
|
-
offset: (offset0 + b * t) * decay,
|
|
813
|
-
velocity: (b - omega * (offset0 + b * t)) * decay
|
|
814
|
-
};
|
|
815
|
-
}
|
|
816
|
-
function tick() {
|
|
817
|
-
if (stopped) return;
|
|
818
|
-
const now = state(clock.now());
|
|
819
|
-
if (Math.abs(now.offset) < REST_DISTANCE && Math.abs(now.velocity) < REST_VELOCITY) {
|
|
820
|
-
stopped = true;
|
|
821
|
-
onFrame(to);
|
|
822
|
-
onRest();
|
|
823
|
-
return;
|
|
824
|
-
}
|
|
825
|
-
onFrame(to + now.offset);
|
|
826
|
-
handle = clock.frame(tick);
|
|
827
|
-
}
|
|
828
|
-
handle = clock.frame(tick);
|
|
829
|
-
return {
|
|
830
|
-
target: to,
|
|
831
|
-
velocity: () => stopped ? 0 : state(clock.now()).velocity,
|
|
832
|
-
stop() {
|
|
833
|
-
stopped = true;
|
|
834
|
-
clock.cancel(handle);
|
|
835
|
-
}
|
|
836
|
-
};
|
|
837
|
-
}
|
|
838
|
-
//#endregion
|
|
839
|
-
//#region src/composables/internal/snapPagingSkip.ts
|
|
840
|
-
/**
|
|
841
|
-
* 原生吸附要跳过末端那一页吞掉的项(见 `snapPages`),这样滚轮、方向键和
|
|
842
|
-
* 普通滚动也停在和轻扫同样的页上。被跳过的项交还时恢复自己原来的行内值。
|
|
843
|
-
* 停点随尺寸和子项变化,最多一帧算一次;分页按住这一行时,等交还时再写。
|
|
844
|
-
*/
|
|
845
|
-
function followSkippedStops(element, track) {
|
|
846
|
-
const skipped = /* @__PURE__ */ new Map();
|
|
847
|
-
let frame = null;
|
|
848
|
-
let isDue = false;
|
|
849
|
-
function restore(item, own) {
|
|
850
|
-
if (own.value === "") item.style.removeProperty("scroll-snap-align");
|
|
851
|
-
else item.style.setProperty("scroll-snap-align", own.value, own.priority);
|
|
852
|
-
}
|
|
853
|
-
function skip(items) {
|
|
854
|
-
for (const [item, own] of skipped) if (!items.includes(item)) {
|
|
855
|
-
skipped.delete(item);
|
|
856
|
-
restore(item, own);
|
|
857
|
-
}
|
|
858
|
-
for (const item of items) if (item instanceof HTMLElement && !skipped.has(item)) {
|
|
859
|
-
skipped.set(item, {
|
|
860
|
-
value: item.style.getPropertyValue("scroll-snap-align"),
|
|
861
|
-
priority: item.style.getPropertyPriority("scroll-snap-align")
|
|
862
|
-
});
|
|
863
|
-
item.style.setProperty("scroll-snap-align", "none");
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
function apply() {
|
|
867
|
-
isDue = false;
|
|
868
|
-
skip(track.pages().skipped);
|
|
869
|
-
}
|
|
870
|
-
function schedule() {
|
|
871
|
-
if (track.isHeld()) {
|
|
872
|
-
isDue = true;
|
|
873
|
-
return;
|
|
874
|
-
}
|
|
875
|
-
if (frame != null || typeof requestAnimationFrame !== "function") return;
|
|
876
|
-
frame = requestAnimationFrame(() => {
|
|
877
|
-
frame = null;
|
|
878
|
-
if (track.isHeld()) isDue = true;
|
|
879
|
-
else apply();
|
|
880
|
-
});
|
|
881
|
-
}
|
|
882
|
-
const resize = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(schedule);
|
|
883
|
-
function observeItems() {
|
|
884
|
-
if (resize == null) return;
|
|
885
|
-
resize.disconnect();
|
|
886
|
-
resize.observe(element);
|
|
887
|
-
for (const item of Array.from(element.children)) resize.observe(item);
|
|
888
|
-
}
|
|
889
|
-
const mutations = typeof MutationObserver === "undefined" ? null : new MutationObserver(() => {
|
|
890
|
-
observeItems();
|
|
891
|
-
schedule();
|
|
892
|
-
});
|
|
893
|
-
mutations?.observe(element, { childList: true });
|
|
894
|
-
observeItems();
|
|
895
|
-
schedule();
|
|
896
|
-
return {
|
|
897
|
-
takeDue: () => isDue ? apply : void 0,
|
|
898
|
-
stop() {
|
|
899
|
-
mutations?.disconnect();
|
|
900
|
-
resize?.disconnect();
|
|
901
|
-
if (frame != null) cancelAnimationFrame(frame);
|
|
902
|
-
frame = null;
|
|
903
|
-
skip([]);
|
|
904
|
-
}
|
|
905
|
-
};
|
|
906
|
-
}
|
|
907
|
-
//#endregion
|
|
908
|
-
//#region src/composables/internal/snapPagingTrack.ts
|
|
909
|
-
function createSnapTrack(element, onHeld) {
|
|
910
|
-
let held = false;
|
|
911
|
-
let heldAt = 0;
|
|
912
|
-
let heldTravel = 0;
|
|
913
|
-
let sign = 1;
|
|
914
|
-
let ownSnapType = "";
|
|
915
|
-
let ownSnapPriority = "";
|
|
916
|
-
const isRtl = () => getComputedStyle(element).direction === "rtl";
|
|
917
|
-
const nativeTravel = () => Math.max(0, element.scrollWidth - element.clientWidth);
|
|
918
|
-
const position = () => held ? heldAt : Math.abs(element.scrollLeft);
|
|
919
|
-
const travel = () => held ? heldTravel : nativeTravel();
|
|
920
|
-
function write(at) {
|
|
921
|
-
element.scrollTo({
|
|
922
|
-
left: sign * at,
|
|
923
|
-
behavior: "instant"
|
|
924
|
-
});
|
|
925
|
-
}
|
|
926
|
-
function report() {
|
|
927
|
-
onHeld?.({
|
|
928
|
-
offset: heldAt,
|
|
929
|
-
travel: heldTravel
|
|
930
|
-
});
|
|
931
|
-
}
|
|
932
|
-
return {
|
|
933
|
-
isHeld: () => held,
|
|
934
|
-
position,
|
|
935
|
-
travel,
|
|
936
|
-
pages: () => snapPages(element, position(), travel()),
|
|
937
|
-
viewWidth() {
|
|
938
|
-
const style = getComputedStyle(element);
|
|
939
|
-
const padding = (Number.parseFloat(style.scrollPaddingLeft) || 0) + (Number.parseFloat(style.scrollPaddingRight) || 0);
|
|
940
|
-
return Math.max(1, element.clientWidth - padding);
|
|
941
|
-
},
|
|
942
|
-
takeOver() {
|
|
943
|
-
if (held) return;
|
|
944
|
-
sign = isRtl() ? -1 : 1;
|
|
945
|
-
heldAt = Math.abs(element.scrollLeft);
|
|
946
|
-
heldTravel = nativeTravel();
|
|
947
|
-
ownSnapType = element.style.getPropertyValue("scroll-snap-type");
|
|
948
|
-
ownSnapPriority = element.style.getPropertyPriority("scroll-snap-type");
|
|
949
|
-
element.style.setProperty("scroll-snap-type", "none");
|
|
950
|
-
held = true;
|
|
951
|
-
report();
|
|
952
|
-
},
|
|
953
|
-
place(at) {
|
|
954
|
-
if (!held) return;
|
|
955
|
-
heldAt = Math.min(Math.max(at, 0), heldTravel);
|
|
956
|
-
write(heldAt);
|
|
957
|
-
report();
|
|
958
|
-
},
|
|
959
|
-
handBack(beforeSnap) {
|
|
960
|
-
if (!held) return;
|
|
961
|
-
held = false;
|
|
962
|
-
beforeSnap?.();
|
|
963
|
-
if (ownSnapType === "") element.style.removeProperty("scroll-snap-type");
|
|
964
|
-
else element.style.setProperty("scroll-snap-type", ownSnapType, ownSnapPriority);
|
|
965
|
-
write(heldAt);
|
|
966
|
-
if (onHeld != null) {
|
|
967
|
-
onHeld({
|
|
968
|
-
offset: heldAt,
|
|
969
|
-
travel: heldTravel
|
|
970
|
-
});
|
|
971
|
-
onHeld(null);
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
};
|
|
975
|
-
}
|
|
976
|
-
//#endregion
|
|
977
|
-
//#region src/composables/internal/snapPagingAttach.ts
|
|
978
|
-
/** 手指移动超过这么多像素才算拖动;先往竖直方向走的交给页面滚动。 */
|
|
979
|
-
const DRAG_SLOP = 6;
|
|
980
|
-
function prefersReducedMotion() {
|
|
981
|
-
return typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
982
|
-
}
|
|
983
|
-
/**
|
|
984
|
-
* 把吸附分页挂到滚动行上。手指、触控笔和触控板横扫会接管这一行,停稳后交还;
|
|
985
|
-
* 鼠标拖动和鼠标滚轮仍然是原生滚动。
|
|
986
|
-
*/
|
|
987
|
-
function attachPaging(element, options, onHeld, clock = browserClock) {
|
|
988
|
-
const track = createSnapTrack(element, onHeld);
|
|
989
|
-
const skip = followSkippedStops(element, track);
|
|
990
|
-
const sampler = createVelocitySampler();
|
|
991
|
-
let pages = [0];
|
|
992
|
-
let anchor = 0;
|
|
993
|
-
let sign = 1;
|
|
994
|
-
let motion = null;
|
|
995
|
-
let drag = null;
|
|
996
|
-
let wheelTimer = null;
|
|
997
|
-
function hold() {
|
|
998
|
-
motion?.stop();
|
|
999
|
-
motion = null;
|
|
1000
|
-
track.takeOver();
|
|
1001
|
-
sign = getComputedStyle(element).direction === "rtl" ? -1 : 1;
|
|
1002
|
-
const at = track.position();
|
|
1003
|
-
const { stops } = track.pages();
|
|
1004
|
-
pages = (options().pages ?? "view") === "item" ? stops : viewPages(stops, nearestIndex(stops, at), track.viewWidth());
|
|
1005
|
-
anchor = nearestIndex(pages, at);
|
|
1006
|
-
}
|
|
1007
|
-
function finish() {
|
|
1008
|
-
if (drag?.isActive || wheelTimer != null) return;
|
|
1009
|
-
track.handBack(skip.takeDue());
|
|
1010
|
-
}
|
|
1011
|
-
function settleTo(target, velocity) {
|
|
1012
|
-
motion?.stop();
|
|
1013
|
-
motion = null;
|
|
1014
|
-
if (prefersReducedMotion()) {
|
|
1015
|
-
track.place(target);
|
|
1016
|
-
finish();
|
|
1017
|
-
return;
|
|
1018
|
-
}
|
|
1019
|
-
motion = springTo(clock, track.position(), target, velocity, (at) => track.place(at), () => {
|
|
1020
|
-
motion = null;
|
|
1021
|
-
finish();
|
|
1022
|
-
});
|
|
1023
|
-
}
|
|
1024
|
-
function release(velocity) {
|
|
1025
|
-
const at = track.position();
|
|
1026
|
-
settleTo(pages[pickPage(pages, at, velocity, anchor)] ?? at, velocity);
|
|
1027
|
-
}
|
|
1028
|
-
function onPointerDown(event) {
|
|
1029
|
-
if (event.pointerType === "mouse" || !event.isPrimary) return;
|
|
1030
|
-
const caught = motion != null;
|
|
1031
|
-
if (caught) hold();
|
|
1032
|
-
drag = {
|
|
1033
|
-
id: event.pointerId,
|
|
1034
|
-
x: event.clientX,
|
|
1035
|
-
y: event.clientY,
|
|
1036
|
-
from: track.position(),
|
|
1037
|
-
isActive: caught
|
|
1038
|
-
};
|
|
1039
|
-
sampler.reset();
|
|
1040
|
-
if (caught) sampler.add(clock.now(), track.position());
|
|
1041
|
-
}
|
|
1042
|
-
function onPointerMove(event) {
|
|
1043
|
-
if (drag == null || event.pointerId !== drag.id) return;
|
|
1044
|
-
if (!drag.isActive) {
|
|
1045
|
-
const dx = event.clientX - drag.x;
|
|
1046
|
-
const dy = event.clientY - drag.y;
|
|
1047
|
-
if (Math.abs(dx) < DRAG_SLOP && Math.abs(dy) < DRAG_SLOP) return;
|
|
1048
|
-
if (Math.abs(dy) > Math.abs(dx) || track.travel() <= 1) {
|
|
1049
|
-
drag = null;
|
|
1050
|
-
return;
|
|
1051
|
-
}
|
|
1052
|
-
hold();
|
|
1053
|
-
drag.isActive = true;
|
|
1054
|
-
drag.x = event.clientX;
|
|
1055
|
-
drag.from = track.position();
|
|
1056
|
-
try {
|
|
1057
|
-
element.setPointerCapture(event.pointerId);
|
|
1058
|
-
} catch {}
|
|
1059
|
-
}
|
|
1060
|
-
event.preventDefault();
|
|
1061
|
-
track.place(drag.from - sign * (event.clientX - drag.x));
|
|
1062
|
-
sampler.add(clock.now(), track.position());
|
|
1063
|
-
}
|
|
1064
|
-
function endDrag(event, velocity) {
|
|
1065
|
-
if (drag == null || event.pointerId !== drag.id) return;
|
|
1066
|
-
const wasActive = drag.isActive;
|
|
1067
|
-
drag = null;
|
|
1068
|
-
if (wasActive) release(velocity(clock.now()));
|
|
1069
|
-
}
|
|
1070
|
-
const onPointerUp = (event) => endDrag(event, (now) => sampler.velocity(now));
|
|
1071
|
-
const onPointerCancel = (event) => endDrag(event, () => 0);
|
|
1072
|
-
function onWheel(event) {
|
|
1073
|
-
if (event.defaultPrevented || event.deltaMode !== 0 || event.deltaX === 0 || Math.abs(event.deltaX) <= Math.abs(event.deltaY)) return;
|
|
1074
|
-
if (drag?.isActive) {
|
|
1075
|
-
event.preventDefault();
|
|
1076
|
-
return;
|
|
1077
|
-
}
|
|
1078
|
-
if (wheelTimer == null) {
|
|
1079
|
-
if (!track.isHeld() && track.travel() <= 1) return;
|
|
1080
|
-
hold();
|
|
1081
|
-
sampler.reset();
|
|
1082
|
-
} else clearTimeout(wheelTimer);
|
|
1083
|
-
event.preventDefault();
|
|
1084
|
-
const low = pages[anchor - 1] ?? pages[0] ?? 0;
|
|
1085
|
-
const high = pages[anchor + 1] ?? pages[pages.length - 1] ?? 0;
|
|
1086
|
-
track.place(Math.min(Math.max(track.position() + sign * event.deltaX, low), high));
|
|
1087
|
-
sampler.add(clock.now(), track.position());
|
|
1088
|
-
wheelTimer = setTimeout(() => {
|
|
1089
|
-
wheelTimer = null;
|
|
1090
|
-
release(sampler.velocity(sampler.lastTime() ?? clock.now()));
|
|
1091
|
-
}, 120);
|
|
1092
|
-
}
|
|
1093
|
-
function move(target) {
|
|
1094
|
-
if (drag?.isActive) return;
|
|
1095
|
-
const velocity = motion?.velocity() ?? 0;
|
|
1096
|
-
if (wheelTimer != null) {
|
|
1097
|
-
clearTimeout(wheelTimer);
|
|
1098
|
-
wheelTimer = null;
|
|
1099
|
-
}
|
|
1100
|
-
hold();
|
|
1101
|
-
const end = track.travel();
|
|
1102
|
-
settleTo(Math.min(Math.max(target(track.position(), end), 0), end), velocity);
|
|
1103
|
-
}
|
|
1104
|
-
const ownTouchAction = element.style.getPropertyValue("touch-action");
|
|
1105
|
-
element.style.setProperty("touch-action", "pan-y pinch-zoom");
|
|
1106
|
-
element.addEventListener("pointerdown", onPointerDown);
|
|
1107
|
-
element.addEventListener("pointermove", onPointerMove);
|
|
1108
|
-
element.addEventListener("pointerup", onPointerUp);
|
|
1109
|
-
element.addEventListener("pointercancel", onPointerCancel);
|
|
1110
|
-
element.addEventListener("wheel", onWheel, { passive: false });
|
|
1111
|
-
return {
|
|
1112
|
-
detach() {
|
|
1113
|
-
element.removeEventListener("pointerdown", onPointerDown);
|
|
1114
|
-
element.removeEventListener("pointermove", onPointerMove);
|
|
1115
|
-
element.removeEventListener("pointerup", onPointerUp);
|
|
1116
|
-
element.removeEventListener("pointercancel", onPointerCancel);
|
|
1117
|
-
element.removeEventListener("wheel", onWheel);
|
|
1118
|
-
if (ownTouchAction === "") element.style.removeProperty("touch-action");
|
|
1119
|
-
else element.style.setProperty("touch-action", ownTouchAction);
|
|
1120
|
-
if (wheelTimer != null) clearTimeout(wheelTimer);
|
|
1121
|
-
wheelTimer = null;
|
|
1122
|
-
motion?.stop();
|
|
1123
|
-
motion = null;
|
|
1124
|
-
drag = null;
|
|
1125
|
-
track.handBack();
|
|
1126
|
-
skip.stop();
|
|
1127
|
-
},
|
|
1128
|
-
move,
|
|
1129
|
-
moveHeld(target) {
|
|
1130
|
-
if (!track.isHeld()) return false;
|
|
1131
|
-
move(target);
|
|
1132
|
-
return true;
|
|
1133
|
-
}
|
|
1134
|
-
};
|
|
1135
|
-
}
|
|
1136
|
-
//#endregion
|
|
1137
|
-
//#region src/composables/useSnapPaging.ts
|
|
1138
|
-
/**
|
|
1139
|
-
* 给 Carousel 的吸附停点加上 iOS 式翻页:`<Carousel :has-snap="paging">`,
|
|
1140
|
-
* 其中 `const paging = useSnapPaging()`。手指或触控板横扫时,一次轻扫翻一屏,
|
|
1141
|
-
* 传 `{ pages: 'item' }` 时翻一项,停在某一项的起始边。鼠标滚轮和鼠标拖动保持原生滚动。
|
|
1142
|
-
*
|
|
1143
|
-
* `options` 可以是 ref 或 getter,新的 `pages` 从下一次轻扫开始生效。
|
|
1144
|
-
* Carousel 一直用它第一次挂上的那个值。
|
|
1145
|
-
*/
|
|
1146
|
-
function useSnapPaging(options = {}) {
|
|
1147
|
-
return makeSnapPaging((element, onHeld) => attachPaging(element, () => toValue(options), onHeld));
|
|
1148
|
-
}
|
|
1149
|
-
//#endregion
|
|
1150
|
-
//#region src/composables/useStreamingText.ts
|
|
1151
|
-
const CHARS_PER_TICK = {
|
|
1152
|
-
natural: 10,
|
|
1153
|
-
fast: 4,
|
|
1154
|
-
instant: Number.POSITIVE_INFINITY
|
|
1155
|
-
};
|
|
1156
|
-
const FALLBACK_DURATION_MS = 95;
|
|
1157
|
-
function parseCssDuration(value) {
|
|
1158
|
-
const milliseconds = /^([\d.]+)ms$/.exec(value.trim());
|
|
1159
|
-
if (milliseconds?.[1] != null) return Number(milliseconds[1]);
|
|
1160
|
-
const seconds = /^([\d.]+)s$/.exec(value.trim());
|
|
1161
|
-
if (seconds?.[1] != null) return Number(seconds[1]) * 1e3;
|
|
1162
|
-
return null;
|
|
1163
|
-
}
|
|
1164
|
-
function motionDuration() {
|
|
1165
|
-
if (typeof document === "undefined") return FALLBACK_DURATION_MS;
|
|
1166
|
-
return parseCssDuration(getComputedStyle(document.documentElement).getPropertyValue("--astryx-duration-fast-min")) ?? FALLBACK_DURATION_MS;
|
|
1167
|
-
}
|
|
1168
|
-
function tickInterval(speed) {
|
|
1169
|
-
if (speed === "instant") return 0;
|
|
1170
|
-
const duration = motionDuration();
|
|
1171
|
-
const tick = speed === "fast" ? duration / 20 : duration / 10;
|
|
1172
|
-
return Math.max(4, Math.round(tick));
|
|
1173
|
-
}
|
|
1174
|
-
const graphemeSegmenter = typeof Intl.Segmenter === "function" ? new Intl.Segmenter(void 0, { granularity: "grapheme" }) : null;
|
|
1175
|
-
function isLowSurrogate(codeUnit) {
|
|
1176
|
-
return codeUnit >= 56320 && codeUnit <= 57343;
|
|
1177
|
-
}
|
|
1178
|
-
/**
|
|
1179
|
-
* Move `offset` back to the start of the grapheme it lands inside, so a slice
|
|
1180
|
-
* never cuts a surrogate pair, a flag, or a ZWJ sequence in half.
|
|
1181
|
-
*/
|
|
1182
|
-
function snapToGraphemeBoundary(text, offset) {
|
|
1183
|
-
if (offset <= 0 || offset >= text.length) return offset;
|
|
1184
|
-
if (graphemeSegmenter != null) return graphemeSegmenter.segment(text).containing(offset)?.index ?? offset;
|
|
1185
|
-
return isLowSurrogate(text.charCodeAt(offset)) ? offset - 1 : offset;
|
|
1186
|
-
}
|
|
1187
|
-
/**
|
|
1188
|
-
* Smooth a burst of streamed text into a steady reveal.
|
|
1189
|
-
* While `isStreaming` is false, or the user prefers reduced motion, the full
|
|
1190
|
-
* source is returned. The reveal steps by UTF-16 units and then snaps back to
|
|
1191
|
-
* a grapheme boundary.
|
|
1192
|
-
*/
|
|
1193
|
-
function useStreamingText(targetText, isStreaming = false, options) {
|
|
1194
|
-
const displayedLen = ref(0);
|
|
1195
|
-
const prefersReducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
|
|
1196
|
-
const target = computed(() => toValue(targetText));
|
|
1197
|
-
const streaming = computed(() => toValue(isStreaming));
|
|
1198
|
-
const speed = computed(() => toValue(options)?.speed ?? "natural");
|
|
1199
|
-
const instant = computed(() => !streaming.value || speed.value === "instant" || prefersReducedMotion.value);
|
|
1200
|
-
watch(target, (next, previous) => {
|
|
1201
|
-
const revealed = (previous ?? "").slice(0, displayedLen.value);
|
|
1202
|
-
if (next.length === 0 || revealed.length > 0 && !next.startsWith(revealed)) displayedLen.value = 0;
|
|
1203
|
-
});
|
|
1204
|
-
watch(streaming, (next) => {
|
|
1205
|
-
if (!next) displayedLen.value = target.value.length;
|
|
1206
|
-
});
|
|
1207
|
-
watch(instant, (isInstant, _, onCleanup) => {
|
|
1208
|
-
if (isInstant) return;
|
|
1209
|
-
let frame = 0;
|
|
1210
|
-
let lastTick = 0;
|
|
1211
|
-
const tick = (now) => {
|
|
1212
|
-
const interval = tickInterval(speed.value);
|
|
1213
|
-
if (now - lastTick >= interval) {
|
|
1214
|
-
lastTick = now;
|
|
1215
|
-
const goal = target.value.length;
|
|
1216
|
-
if (displayedLen.value < goal) {
|
|
1217
|
-
const step = CHARS_PER_TICK[speed.value];
|
|
1218
|
-
displayedLen.value = Math.min(displayedLen.value + step, goal);
|
|
1219
|
-
}
|
|
1220
|
-
}
|
|
1221
|
-
frame = requestAnimationFrame(tick);
|
|
1222
|
-
};
|
|
1223
|
-
frame = requestAnimationFrame(tick);
|
|
1224
|
-
onCleanup(() => cancelAnimationFrame(frame));
|
|
1225
|
-
}, { immediate: true });
|
|
1226
|
-
return computed(() => {
|
|
1227
|
-
const text = target.value;
|
|
1228
|
-
if (instant.value) return text;
|
|
1229
|
-
return text.slice(0, snapToGraphemeBoundary(text, displayedLen.value));
|
|
1230
|
-
});
|
|
1231
|
-
}
|
|
1232
|
-
//#endregion
|
|
1233
|
-
//#region src/composables/useTouchTrigger.ts
|
|
1234
|
-
/**
|
|
1235
|
-
* 悬停类浮层(Tooltip、HoverCard)共用的触屏行为。
|
|
1236
|
-
*
|
|
1237
|
-
* 触屏上表达不了「悬停」:点一下会合成 `mouseenter`,不处理的悬停浮层要么
|
|
1238
|
-
* 每次点都弹出来、又没有东西能关掉它,要么把用户点控件的这一下吃掉。
|
|
1239
|
-
*
|
|
1240
|
-
* 一次触屏按压要等它成了轻点才算数。手指落在触发器上可能是开始滚动、缩放或长按,
|
|
1241
|
-
* 只有浏览器在手指挪动或抬起后才知道是哪个,它的 click 就是结论。所以从触发器上开始的
|
|
1242
|
-
* 滚动不会打开提示,停着不动的手指也不会。点开的浮层在外面轻点抬起时关,
|
|
1243
|
-
* 或在外面开始的滚动被浏览器接走时关,和 iOS 关闭弹出框的时机一样。
|
|
1244
|
-
*/
|
|
1245
|
-
function useTouchTrigger(options) {
|
|
1246
|
-
const touch = useTouchTriggerInternal(options);
|
|
1247
|
-
return {
|
|
1248
|
-
isTouchPointer: touch.isTouchPointer,
|
|
1249
|
-
isTouchInteraction: touch.isTouchInteraction,
|
|
1250
|
-
handlePointerEnter: touch.handlePointerEnter,
|
|
1251
|
-
handlePointerDown: touch.handlePointerDown,
|
|
1252
|
-
clearTapOpen: touch.clearTapOpen
|
|
1253
|
-
};
|
|
1254
|
-
}
|
|
1255
|
-
//#endregion
|
|
1256
|
-
export { useKeyboardHint as a, perceptualLightness as c, useGridFocus as d, useEntryAnimation as f, useSnapPaging as i, useImageMode as l, useContainerReveal as m, snapToGraphemeBoundary as n, useIndicatorFocusRing as o, useControllableState as p, useStreamingText as r, imageModeFromPixels as s, useTouchTrigger as t, useGridRovingFocus as u };
|