@tbox.cn/app-agent-ui-kit 0.0.0-stage → 1.0.0-alpha.10
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/LICENSE +21 -0
- package/README.md +69 -2
- package/assets/keyboard-circle.svg +12 -0
- package/assets/more-circle.svg +3 -0
- package/assets/new-chat.svg +3 -0
- package/assets/quick-command-star.svg +3 -0
- package/assets/send-gradient.svg +14 -0
- package/assets/stop@2x.png +0 -0
- package/assets/voice-circle.svg +3 -0
- package/package.json +70 -3
- package/src/ChatPage.tsx +400 -0
- package/src/card/action-list-pending.test.tsx +91 -0
- package/src/card/define-card.test.ts +25 -0
- package/src/card/define-card.ts +47 -0
- package/src/card/primitives.test.tsx +157 -0
- package/src/card/primitives.tsx +717 -0
- package/src/card/use-card-actions.ts +86 -0
- package/src/card/view-card-inline.test.tsx +42 -0
- package/src/card/view-card.test.tsx +79 -0
- package/src/card/view-card.tsx +250 -0
- package/src/chat-config.test.ts +46 -0
- package/src/chat-config.ts +84 -0
- package/src/components/ActionBar/DebugButton.test.tsx +87 -0
- package/src/components/ActionBar/DebugButton.tsx +48 -0
- package/src/components/ActionBar/index.test.tsx +83 -0
- package/src/components/ActionBar/index.tsx +158 -0
- package/src/components/BottomSheet/index.test.tsx +97 -0
- package/src/components/BottomSheet/index.tsx +49 -0
- package/src/components/CardRenderer.tsx +36 -0
- package/src/components/ChatHeader/index.test.tsx +74 -0
- package/src/components/ChatHeader/index.tsx +77 -0
- package/src/components/ChatInput/index.test.tsx +156 -0
- package/src/components/ChatInput/index.tsx +295 -0
- package/src/components/ChatInput/voice/VoicePressField.tsx +43 -0
- package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +51 -0
- package/src/components/ChatInput/voice/machine.ts +27 -0
- package/src/components/ChatInput/voice/useVoicePress.ts +207 -0
- package/src/components/HistoryEntry/index.tsx +42 -0
- package/src/components/MediaList/index.tsx +270 -0
- package/src/components/MessageBubble/index.test.tsx +134 -0
- package/src/components/MessageBubble/index.tsx +208 -0
- package/src/components/PullDownIndicator/index.tsx +31 -0
- package/src/components/QuickCommands/index.test.tsx +101 -0
- package/src/components/QuickCommands/index.tsx +75 -0
- package/src/components/ReasoningBlock/index.tsx +61 -0
- package/src/components/ScrollToBottomButton/index.tsx +12 -0
- package/src/components/ToolCallBlock/detail.test.ts +346 -0
- package/src/components/ToolCallBlock/detail.ts +204 -0
- package/src/components/ToolCallBlock/index.test.tsx +261 -0
- package/src/components/ToolCallBlock/index.tsx +232 -0
- package/src/components/ToolCallBlock/titles.ts +102 -0
- package/src/hooks/use-drag-gesture.test.ts +66 -0
- package/src/hooks/use-drag-gesture.ts +129 -0
- package/src/hooks/use-pull-down-history.ts +241 -0
- package/src/hooks/use-pull-down-wheel-native.test.ts +64 -0
- package/src/hooks/useMediaUpload.ts +242 -0
- package/src/hooks/useTTSAutoPlay.ts +82 -0
- package/src/icons.ts +39 -0
- package/src/index.ts +143 -0
- package/src/overlay/Overlay.tsx +154 -0
- package/src/overlay/focus.ts +53 -0
- package/src/overlay/index.ts +16 -0
- package/src/overlay/inert.ts +27 -0
- package/src/overlay/layers.ts +21 -0
- package/src/overlay/overlay.ssr.test.tsx +27 -0
- package/src/overlay/overlay.test.tsx +209 -0
- package/src/overlay/presence.ts +86 -0
- package/src/overlay/scroll-lock.ts +62 -0
- package/src/overlay/stack.ts +111 -0
- package/src/overlay/use-overlay.ts +80 -0
- package/src/preview-provider.tsx +47 -0
- package/src/shell.contract.json +75 -0
- package/src/shell.test.ts +72 -0
- package/src/shell.ts +60 -0
- package/src/store-context.tsx +92 -0
- package/src/surface.test.tsx +35 -0
- package/src/surface.tsx +44 -0
- package/src/test/setup.ts +7 -0
- package/src/theme/base.css +167 -0
- package/src/theme/contract.json +193 -0
- package/src/theme/motion.css +19 -0
- package/src/theme/theme-contract.test.ts +85 -0
- package/src/theme/theme.ts +17 -0
- package/src/theme/tokens.css +78 -0
- package/src/types.ts +29 -0
- package/src/utils/copyText.ts +11 -0
- package/src/utils/haptics.test.ts +51 -0
- package/src/utils/haptics.ts +44 -0
- package/src/utils/image-preview-host.tsx +95 -0
- package/src/utils/imagePreview.test.ts +32 -0
- package/src/utils/imagePreview.ts +19 -0
- package/src/utils/pull-down-machine.test.ts +188 -0
- package/src/utils/pull-down-machine.ts +157 -0
- package/src/utils/session-error.ts +5 -0
- package/src/utils/toast.test.ts +64 -0
- package/src/utils/toast.ts +108 -0
- package/src/utils/tool-call-display.test.ts +240 -0
- package/src/utils/tool-call-display.ts +146 -0
- package/src/vite-env.d.ts +1 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* createPullDownMachine 全转移表回归(纯逻辑,node 环境):
|
|
3
|
+
* - 实时门控取代 touchStart 锁定(未到顶 move 重锚 → 到顶后从最近锚点测距);
|
|
4
|
+
* - 顶部门槛跨越(pulling→ready→touchEnd shouldLoad)/ 不足回弹 / noMore 封顶 + holdNoMore;
|
|
5
|
+
* - isLoading 全忽略;wheel 累积语义(顶部向上滚累积越阈即发,发后/离顶/向下滚归零);reset 归零。
|
|
6
|
+
*/
|
|
7
|
+
import { describe, it, expect } from 'vitest';
|
|
8
|
+
import {
|
|
9
|
+
createPullDownMachine,
|
|
10
|
+
DAMPING,
|
|
11
|
+
PULL_DOWN_THRESHOLD,
|
|
12
|
+
PULL_TRIGGER_OFFSET,
|
|
13
|
+
WHEEL_DISTANCE_SCALE,
|
|
14
|
+
WHEEL_TRIGGER_ACCUM,
|
|
15
|
+
} from './pull-down-machine';
|
|
16
|
+
|
|
17
|
+
/** 越过触发线所需的手指行程(阈值按内容位移定义,手指行程 = 位移 / 阻尼)。 */
|
|
18
|
+
const FINGER_OVER = PULL_TRIGGER_OFFSET / DAMPING + 10;
|
|
19
|
+
|
|
20
|
+
describe('createPullDownMachine(touch 路径)', () => {
|
|
21
|
+
it('M1 二段手势:未到顶 move×3 重锚 → 到顶后从最近锚点测距(实时门控)', () => {
|
|
22
|
+
const m = createPullDownMachine();
|
|
23
|
+
m.touchStart(300);
|
|
24
|
+
// 未到顶:重锚 + 状态不动(无 preventDefault)
|
|
25
|
+
expect(m.touchMove({ clientY: 250, isAtTop: false, hasMore: true, isLoading: false })).toEqual({ preventDefault: false });
|
|
26
|
+
expect(m.touchMove({ clientY: 200, isAtTop: false, hasMore: true, isLoading: false })).toEqual({ preventDefault: false });
|
|
27
|
+
m.touchMove({ clientY: 100, isAtTop: false, hasMore: true, isLoading: false });
|
|
28
|
+
// 到顶后:从 100 测距(若未重锚会从 300 测出巨值)
|
|
29
|
+
const r = m.touchMove({ clientY: 130, isAtTop: true, hasMore: true, isLoading: false });
|
|
30
|
+
expect(r.preventDefault).toBe(true);
|
|
31
|
+
expect(m.getState().distance).toBe(30 * DAMPING);
|
|
32
|
+
expect(m.getState().status).toBe('pulling');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('M2 门槛跨越:distance > PULL_TRIGGER_OFFSET → ready;touchEnd 触发 shouldLoad 并进入 loading', () => {
|
|
36
|
+
const m = createPullDownMachine();
|
|
37
|
+
m.touchStart(100);
|
|
38
|
+
m.touchMove({ clientY: 100 + FINGER_OVER, isAtTop: true, hasMore: true, isLoading: false });
|
|
39
|
+
expect(m.getState().status).toBe('ready');
|
|
40
|
+
const end = m.touchEnd({ hasMore: true });
|
|
41
|
+
expect(end).toEqual({ shouldLoad: true, holdNoMore: false });
|
|
42
|
+
expect(m.getState()).toEqual({ status: 'loading', distance: PULL_DOWN_THRESHOLD });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('M2b 触发线以下:保持 pulling,松手不加载(位移≠意图)', () => {
|
|
46
|
+
const m = createPullDownMachine();
|
|
47
|
+
m.touchStart(100);
|
|
48
|
+
// 差一点越线:阈值按内容位移定义,故手指行程取 (触发线 - 1) / 阻尼
|
|
49
|
+
m.touchMove({ clientY: 100 + (PULL_TRIGGER_OFFSET - 1) / DAMPING, isAtTop: true, hasMore: true, isLoading: false });
|
|
50
|
+
expect(m.getState().distance).toBeLessThan(PULL_TRIGGER_OFFSET);
|
|
51
|
+
expect(m.getState().status).toBe('pulling');
|
|
52
|
+
expect(m.touchEnd({ hasMore: true })).toEqual({ shouldLoad: false, holdNoMore: false });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('M3 不足阈值 touchEnd:回弹 idle(shouldLoad=false)', () => {
|
|
56
|
+
const m = createPullDownMachine();
|
|
57
|
+
m.touchStart(100);
|
|
58
|
+
m.touchMove({ clientY: 120, isAtTop: true, hasMore: true, isLoading: false });
|
|
59
|
+
expect(m.getState().status).toBe('pulling');
|
|
60
|
+
expect(m.touchEnd({ hasMore: true })).toEqual({ shouldLoad: false, holdNoMore: false });
|
|
61
|
+
expect(m.getState()).toEqual({ status: 'idle', distance: 0 });
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('M4 deltaY<=0(上滑):状态回 idle、distance 归零', () => {
|
|
65
|
+
const m = createPullDownMachine();
|
|
66
|
+
m.touchStart(100);
|
|
67
|
+
m.touchMove({ clientY: 140, isAtTop: true, hasMore: true, isLoading: false });
|
|
68
|
+
expect(m.getState().status).toBe('pulling');
|
|
69
|
+
m.touchMove({ clientY: 90, isAtTop: true, hasMore: true, isLoading: false });
|
|
70
|
+
expect(m.getState()).toEqual({ status: 'idle', distance: 0 });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('M5 hasMore=false:noMore 态 + 距离封顶 + touchEnd holdNoMore', () => {
|
|
74
|
+
const m = createPullDownMachine();
|
|
75
|
+
m.touchStart(100);
|
|
76
|
+
m.touchMove({ clientY: 400, isAtTop: true, hasMore: false, isLoading: false });
|
|
77
|
+
expect(m.getState().status).toBe('noMore');
|
|
78
|
+
expect(m.getState().distance).toBe(PULL_DOWN_THRESHOLD); // 封顶
|
|
79
|
+
expect(m.touchEnd({ hasMore: false })).toEqual({ shouldLoad: false, holdNoMore: true });
|
|
80
|
+
expect(m.getState().status).toBe('noMore'); // 保留(wrapper 定时 reset)
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('M6 isLoading=true:touchMove 全忽略', () => {
|
|
84
|
+
const m = createPullDownMachine();
|
|
85
|
+
m.touchStart(100);
|
|
86
|
+
const r = m.touchMove({ clientY: 400, isAtTop: true, hasMore: true, isLoading: true });
|
|
87
|
+
expect(r.preventDefault).toBe(false);
|
|
88
|
+
expect(m.getState()).toEqual({ status: 'idle', distance: 0 });
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('createPullDownMachine(wheel 路径)', () => {
|
|
93
|
+
it('W1 顶部向上滚累积:分次未达阈不触发,越阈触发且归零', () => {
|
|
94
|
+
const m = createPullDownMachine();
|
|
95
|
+
const step = WHEEL_TRIGGER_ACCUM / 2; // 两步刚好到线(不越),第三步越线
|
|
96
|
+
expect(m.wheel({ deltaY: -step, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
|
|
97
|
+
expect(m.wheel({ deltaY: -step, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
|
|
98
|
+
expect(m.wheel({ deltaY: -10, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: true }); // 越线
|
|
99
|
+
expect(m.wheel({ deltaY: -step, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false }); // 触发后归零重计
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('W1b 单次大 deltaY 不足以触发:滚轮需连续意图(回归「一滚即加载」)', () => {
|
|
103
|
+
const m = createPullDownMachine();
|
|
104
|
+
expect(m.wheel({ deltaY: -(WHEEL_TRIGGER_ACCUM - 1), isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('W2 离顶 / 向下滚:累积清零', () => {
|
|
108
|
+
const m = createPullDownMachine();
|
|
109
|
+
const near = WHEEL_TRIGGER_ACCUM - 10; // 差一点越线
|
|
110
|
+
m.wheel({ deltaY: -near, isAtTop: true, hasMore: true });
|
|
111
|
+
m.wheel({ deltaY: 10, isAtTop: true, hasMore: true }); // 向下滚清零
|
|
112
|
+
expect(m.wheel({ deltaY: -near, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
|
|
113
|
+
m.wheel({ deltaY: -near, isAtTop: false, hasMore: true }); // 离顶清零
|
|
114
|
+
expect(m.wheel({ deltaY: -near, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('W3 wheel 驱动与拖拽同尺度的视觉:累积中 pulling,越阈直接进 loading', () => {
|
|
118
|
+
const m = createPullDownMachine();
|
|
119
|
+
const half = WHEEL_TRIGGER_ACCUM / 2;
|
|
120
|
+
m.wheel({ deltaY: -half, isAtTop: true, hasMore: true });
|
|
121
|
+
expect(m.getState().status).toBe('pulling');
|
|
122
|
+
// 累积按触发线之比换算到拖拽尺度,两条路径的文案切换点因此重合
|
|
123
|
+
expect(m.getState().distance).toBeCloseTo(half * WHEEL_DISTANCE_SCALE);
|
|
124
|
+
|
|
125
|
+
m.wheel({ deltaY: -WHEEL_TRIGGER_ACCUM, isAtTop: true, hasMore: true }); // 越线
|
|
126
|
+
// 没有「松手」这一刻,越阈即发:直接进 loading,不经 ready
|
|
127
|
+
expect(m.getState().status).toBe('loading');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('W4 wheelIdle:停止滚动后清掉累积与视觉,文案不卡在拉动态', () => {
|
|
131
|
+
const m = createPullDownMachine();
|
|
132
|
+
const near = WHEEL_TRIGGER_ACCUM - 10;
|
|
133
|
+
m.wheel({ deltaY: -near, isAtTop: true, hasMore: true });
|
|
134
|
+
expect(m.getState().status).toBe('pulling');
|
|
135
|
+
m.wheelIdle();
|
|
136
|
+
expect(m.getState()).toEqual({ status: 'idle', distance: 0 });
|
|
137
|
+
// 累积也已清零:复位后需重新攒够才触发
|
|
138
|
+
expect(m.wheel({ deltaY: -near, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('W4b 加载中 wheel 全忽略:惯性滚动不得把 loading 打回 pulling(动画弹跳根因)', () => {
|
|
142
|
+
const m = createPullDownMachine();
|
|
143
|
+
m.wheel({ deltaY: -(WHEEL_TRIGGER_ACCUM + 10), isAtTop: true, hasMore: true });
|
|
144
|
+
expect(m.getState().status).toBe('loading');
|
|
145
|
+
const during = m.getState();
|
|
146
|
+
|
|
147
|
+
// 触控板惯性在加载期间继续送 wheel
|
|
148
|
+
expect(m.wheel({ deltaY: -200, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
|
|
149
|
+
expect(m.getState()).toEqual(during);
|
|
150
|
+
// 显式 isLoading 同样短路
|
|
151
|
+
expect(m.wheel({ deltaY: -200, isAtTop: true, hasMore: true, isLoading: true })).toEqual({ shouldLoad: false });
|
|
152
|
+
expect(m.getState()).toEqual(during);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('W4c wheelIdle 不打断加载:loading 期间只清累积,视觉留给加载完成时的 reset', () => {
|
|
156
|
+
const m = createPullDownMachine();
|
|
157
|
+
m.wheel({ deltaY: -(WHEEL_TRIGGER_ACCUM + 10), isAtTop: true, hasMore: true });
|
|
158
|
+
expect(m.getState().status).toBe('loading');
|
|
159
|
+
m.wheelIdle();
|
|
160
|
+
expect(m.getState().status).toBe('loading'); // 文案不该被打回「历史记录」
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('W5 hasMore=false:wheel 同样进 noMore 且距离封顶,不发加载', () => {
|
|
164
|
+
const m = createPullDownMachine();
|
|
165
|
+
expect(m.wheel({ deltaY: -500, isAtTop: true, hasMore: false })).toEqual({ shouldLoad: false });
|
|
166
|
+
expect(m.getState().status).toBe('noMore');
|
|
167
|
+
expect(m.getState().distance).toBe(PULL_DOWN_THRESHOLD);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe('createPullDownMachine(reset)', () => {
|
|
172
|
+
it('R1 reset:status/distance/wheelAccum 全归零(G9 noMore 复活)', () => {
|
|
173
|
+
const m = createPullDownMachine();
|
|
174
|
+
m.touchStart(100);
|
|
175
|
+
m.touchMove({ clientY: 100 + FINGER_OVER, isAtTop: true, hasMore: false, isLoading: false });
|
|
176
|
+
expect(m.getState().status).toBe('noMore');
|
|
177
|
+
m.wheel({ deltaY: -50, isAtTop: true, hasMore: true });
|
|
178
|
+
m.reset();
|
|
179
|
+
expect(m.getState()).toEqual({ status: 'idle', distance: 0 });
|
|
180
|
+
// noMore 复活:reset 后 hasMore=true 的正常下拉可用
|
|
181
|
+
m.touchStart(100);
|
|
182
|
+
m.touchMove({ clientY: 100 + FINGER_OVER, isAtTop: true, hasMore: true, isLoading: false });
|
|
183
|
+
expect(m.getState().status).toBe('ready');
|
|
184
|
+
// wheelAccum 已归零
|
|
185
|
+
expect(m.wheel({ deltaY: -30, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
|
|
186
|
+
expect(m.wheel({ deltaY: -30, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
|
|
187
|
+
});
|
|
188
|
+
});
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 下拉加载历史手势状态机(纯逻辑,零 React 依赖,node 可测)。
|
|
3
|
+
* 核心修正:touchStart 不再锁定 isAtTop(旧实现在未滚动容器上初始 false → 全新手势死区),
|
|
4
|
+
* 门控移到 touchMove 实时判定——未到顶时每次重锚,到顶后从最近锚点测距;
|
|
5
|
+
* wheel 为桌面回退(顶部向上滚累积,越阈即发,发后归零)。
|
|
6
|
+
* 阈值三分:PULL_DOWN_THRESHOLD 只管指示器视觉,PULL_TRIGGER_OFFSET 管触摸触发,
|
|
7
|
+
* WHEEL_TRIGGER_ACCUM 管滚轮触发——共用一个常量会让「看得见」等同于「要加载」。
|
|
8
|
+
* wrapper(use-pull-down-history)职责:状态镜像 / 定时器 / disabled 短路;
|
|
9
|
+
* 业务防抖权威在 store(isLoadingHistory),机器内 isLoading 忽略为双保险。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** 指示器完全露出所需的阻尼后位移——纯视觉基准(PullDownIndicator 的 marginTop 同源)。 */
|
|
13
|
+
export const PULL_DOWN_THRESHOLD = 60;
|
|
14
|
+
/**
|
|
15
|
+
* 触发加载所需的**内容位移**,对齐平台惯例:iOS UIRefreshControl 实际在 60~80pt 触发
|
|
16
|
+
* (控件自身高约 60pt),Android SwipeRefreshLayout 默认 64dp(DEFAULT_CIRCLE_TARGET)。
|
|
17
|
+
* 取 64 落在两者交集;阻尼 0.5 下对应手指行程 128px。
|
|
18
|
+
* 注意这些惯例量的是内容位移而非手指行程——按手指行程设阈值会得到四五倍的错值。
|
|
19
|
+
*/
|
|
20
|
+
export const PULL_TRIGGER_OFFSET = 64;
|
|
21
|
+
/**
|
|
22
|
+
* 桌面滚轮累积触发阈值(单独于触摸):一次滚轮事件 deltaY 常见 100+,
|
|
23
|
+
* 与触摸共用 60 会「一滚即加载」;这里要求连续向上滚约三次。
|
|
24
|
+
*/
|
|
25
|
+
export const WHEEL_TRIGGER_ACCUM = 128;
|
|
26
|
+
/** wheel 累积 → 拖拽尺度的换算比,使两条路径的位移与文案切换点重合。 */
|
|
27
|
+
export const WHEEL_DISTANCE_SCALE = PULL_TRIGGER_OFFSET / WHEEL_TRIGGER_ACCUM;
|
|
28
|
+
/** wheel 停止多久后复位视觉(ms)——触控板惯性滚动的间隔通常远小于此。 */
|
|
29
|
+
export const WHEEL_IDLE_RESET_MS = 220;
|
|
30
|
+
/**
|
|
31
|
+
* 拖拽阻尼:原生下拉在小位移区间接近 1:1 跟手,0.4 会显得「沉」(手指 100px 内容才动 40px)。
|
|
32
|
+
* 0.5 兼顾跟手感与「越拉越沉」的物理暗示。
|
|
33
|
+
*/
|
|
34
|
+
export const DAMPING = 0.5;
|
|
35
|
+
/** noMore 展示保持时长(wrapper 定时后 reset;机器内部无定时器保 node 可测) */
|
|
36
|
+
export const NO_MORE_HOLD_MS = 1500;
|
|
37
|
+
|
|
38
|
+
export type PullStatus = 'idle' | 'pulling' | 'ready' | 'loading' | 'noMore';
|
|
39
|
+
|
|
40
|
+
export interface PullDownState {
|
|
41
|
+
status: PullStatus;
|
|
42
|
+
distance: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function createPullDownMachine() {
|
|
46
|
+
let status: PullStatus = 'idle';
|
|
47
|
+
let distance = 0;
|
|
48
|
+
let startY = 0;
|
|
49
|
+
let wheelAccum = 0;
|
|
50
|
+
|
|
51
|
+
/** 同步置 loading(touchEnd 越阈触发;离开由 wrapper 在 onLoad 完成后 reset()) */
|
|
52
|
+
const enterLoading = () => {
|
|
53
|
+
status = 'loading';
|
|
54
|
+
distance = PULL_DOWN_THRESHOLD;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const resetDistance = () => {
|
|
58
|
+
if (status !== 'idle' && status !== 'noMore') {
|
|
59
|
+
status = 'idle';
|
|
60
|
+
}
|
|
61
|
+
if (status !== 'noMore') distance = 0;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
getState(): PullDownState {
|
|
66
|
+
return { status, distance };
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
touchStart(clientY: number): void {
|
|
70
|
+
startY = clientY;
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
touchMove(i: { clientY: number; isAtTop: boolean; hasMore: boolean; isLoading: boolean }): { preventDefault: boolean } {
|
|
74
|
+
if (i.isLoading) return { preventDefault: false };
|
|
75
|
+
// 未到顶:重锚(原生滚动照常),状态回 idle(noMore 保留——顶部已无更多的展示态)
|
|
76
|
+
if (!i.isAtTop) {
|
|
77
|
+
startY = i.clientY;
|
|
78
|
+
resetDistance();
|
|
79
|
+
return { preventDefault: false };
|
|
80
|
+
}
|
|
81
|
+
const deltaY = i.clientY - startY;
|
|
82
|
+
if (deltaY <= 0) {
|
|
83
|
+
resetDistance();
|
|
84
|
+
return { preventDefault: false };
|
|
85
|
+
}
|
|
86
|
+
if (!i.hasMore) {
|
|
87
|
+
status = 'noMore';
|
|
88
|
+
distance = Math.min(deltaY * DAMPING, PULL_DOWN_THRESHOLD);
|
|
89
|
+
return { preventDefault: true };
|
|
90
|
+
}
|
|
91
|
+
distance = deltaY * DAMPING;
|
|
92
|
+
status = distance > PULL_TRIGGER_OFFSET ? 'ready' : 'pulling';
|
|
93
|
+
return { preventDefault: true };
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
touchEnd(i: { hasMore: boolean }): { shouldLoad: boolean; holdNoMore: boolean } {
|
|
97
|
+
if (status === 'ready' && i.hasMore) {
|
|
98
|
+
enterLoading();
|
|
99
|
+
return { shouldLoad: true, holdNoMore: false };
|
|
100
|
+
}
|
|
101
|
+
if (status === 'noMore') {
|
|
102
|
+
return { shouldLoad: false, holdNoMore: true }; // wrapper 定时后 reset
|
|
103
|
+
}
|
|
104
|
+
status = 'idle';
|
|
105
|
+
distance = 0;
|
|
106
|
+
return { shouldLoad: false, holdNoMore: false };
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 触控板双指滑动 / 滚轮。与拖拽的差别是没有「松手」这一刻,故越阈即发、不经 ready 等释放。
|
|
111
|
+
* 但累积过程同样驱动 status/distance——否则双指滑动全程无反馈、加载突然发生,
|
|
112
|
+
* 与拖拽的观感对不上。distance 按触发线之比换算到与拖拽同一尺度,胶囊位移与文案才同步。
|
|
113
|
+
*/
|
|
114
|
+
wheel(i: { deltaY: number; isAtTop: boolean; hasMore: boolean; isLoading?: boolean }): { shouldLoad: boolean } {
|
|
115
|
+
// 加载中全忽略:触控板惯性会在加载期间继续送 wheel,若放行会把 status 从 loading
|
|
116
|
+
// 改回 pulling、distance 反复增减——表现为动画弹来弹去(touchMove 同守卫)。
|
|
117
|
+
if (i.isLoading || status === 'loading') return { shouldLoad: false };
|
|
118
|
+
// 离顶 / 向下滚:累积与视觉一并复位
|
|
119
|
+
if (!i.isAtTop || i.deltaY >= 0) {
|
|
120
|
+
wheelAccum = 0;
|
|
121
|
+
resetDistance();
|
|
122
|
+
return { shouldLoad: false };
|
|
123
|
+
}
|
|
124
|
+
wheelAccum -= i.deltaY;
|
|
125
|
+
if (!i.hasMore) {
|
|
126
|
+
status = 'noMore';
|
|
127
|
+
distance = Math.min(wheelAccum * WHEEL_DISTANCE_SCALE, PULL_DOWN_THRESHOLD);
|
|
128
|
+
return { shouldLoad: false };
|
|
129
|
+
}
|
|
130
|
+
if (wheelAccum > WHEEL_TRIGGER_ACCUM) {
|
|
131
|
+
wheelAccum = 0;
|
|
132
|
+
enterLoading();
|
|
133
|
+
return { shouldLoad: true };
|
|
134
|
+
}
|
|
135
|
+
distance = wheelAccum * WHEEL_DISTANCE_SCALE;
|
|
136
|
+
status = 'pulling';
|
|
137
|
+
return { shouldLoad: false };
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* wheel 停止后由 wrapper 定时调用:清掉累积与视觉,避免文案卡在拉动态。
|
|
142
|
+
* loading 期间只清累积不动视觉——此刻的 loading 由加载完成时的 reset 收尾,
|
|
143
|
+
* 在这里复位会把「加载中」文案打回「历史记录」。
|
|
144
|
+
*/
|
|
145
|
+
wheelIdle(): void {
|
|
146
|
+
wheelAccum = 0;
|
|
147
|
+
if (status !== 'loading') resetDistance();
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
reset(): void {
|
|
151
|
+
status = 'idle';
|
|
152
|
+
distance = 0;
|
|
153
|
+
startY = 0;
|
|
154
|
+
wheelAccum = 0;
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { toast } from './toast';
|
|
4
|
+
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
vi.useFakeTimers();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
vi.useRealTimers();
|
|
11
|
+
document.body.innerHTML = '';
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
function bubbles(): HTMLElement[] {
|
|
15
|
+
return [...document.querySelectorAll<HTMLElement>('[data-tbox-toast]')];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('toast', () => {
|
|
19
|
+
it('单实例:新提示替换旧提示并重置计时', async () => {
|
|
20
|
+
await toast('第一条');
|
|
21
|
+
await toast('第二条');
|
|
22
|
+
expect(bubbles()).toHaveLength(1);
|
|
23
|
+
expect(bubbles()[0].textContent).toBe('第二条');
|
|
24
|
+
vi.advanceTimersByTime(1999);
|
|
25
|
+
expect(bubbles()).toHaveLength(1);
|
|
26
|
+
vi.advanceTimersByTime(1);
|
|
27
|
+
expect(bubbles()[0].getAttribute('data-state')).toBe('closed');
|
|
28
|
+
vi.advanceTimersByTime(150);
|
|
29
|
+
expect(bubbles()).toHaveLength(0);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('语气决定图标与可访问语义', async () => {
|
|
33
|
+
await toast('成功', 'success');
|
|
34
|
+
expect(bubbles()[0].querySelector('svg')?.getAttribute('class')).toContain('text-success');
|
|
35
|
+
await toast('失败', 'error');
|
|
36
|
+
expect(bubbles()[0].querySelector('svg')?.getAttribute('class')).toContain('text-destructive');
|
|
37
|
+
await toast('普通');
|
|
38
|
+
expect(bubbles()[0].querySelector('svg')).toBeNull();
|
|
39
|
+
const host = document.querySelector('[role="status"]') as HTMLElement;
|
|
40
|
+
expect(host.getAttribute('aria-live')).toBe('polite');
|
|
41
|
+
expect(host.getAttribute('aria-atomic')).toBe('true');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('内容按纯文本写入,不解释 HTML', async () => {
|
|
45
|
+
await toast('<b>加粗</b>');
|
|
46
|
+
expect(bubbles()[0].querySelector('b')).toBeNull();
|
|
47
|
+
expect(bubbles()[0].textContent).toBe('<b>加粗</b>');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('隐藏中又来一条:替换旧节点,不留隐形残骸', async () => {
|
|
51
|
+
await toast('第一条');
|
|
52
|
+
vi.advanceTimersByTime(2000); // 进入 150ms 隐藏窗口
|
|
53
|
+
await toast('第二条'); // 在隐藏窗口内替换
|
|
54
|
+
vi.advanceTimersByTime(400);
|
|
55
|
+
expect(bubbles()).toHaveLength(1);
|
|
56
|
+
expect(bubbles()[0].textContent).toBe('第二条');
|
|
57
|
+
expect(bubbles()[0].getAttribute('data-state')).toBe('open');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('不拦截点击(pointer-events 关闭)', async () => {
|
|
61
|
+
await toast('提示');
|
|
62
|
+
expect(bubbles()[0].className).toContain('pointer-events-none');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/** 提示语气(对齐原模板 TboxCard.toast 的 tone):info 无图标、success/error 带图标 */
|
|
2
|
+
export type ToastTone = 'info' | 'success' | 'error';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 轻提示:纯 DOM 实现,不依赖 React、不依赖任何组件库。
|
|
6
|
+
*
|
|
7
|
+
* 为什么不用组件库:`toast` 的调用方里有 React 树外的命令式通道(SDK chat-store 的 `deps.toast`),
|
|
8
|
+
* 组件库方案要求应用根部挂 Provider 并接线;纯 DOM 版零接线、可静态导入(不再等动态 chunk),
|
|
9
|
+
* 且 Node/SSR 环境直接空返回(不再需要 ssr.noExternal 兜底)。
|
|
10
|
+
*
|
|
11
|
+
* 语义对齐被替换的 antd Toast:单实例(新提示替换旧提示并重置计时)、2000ms、视口居中、不拦截点击、
|
|
12
|
+
* 图标区 36px;额外补 `role="status"` + `aria-live` 让读屏可朗读。
|
|
13
|
+
*
|
|
14
|
+
* @param tone 语气:success → 成功图标,error → 失败图标,info/省略 → 无图标
|
|
15
|
+
*/
|
|
16
|
+
export async function toast(content: string, tone: ToastTone = 'info'): Promise<void> {
|
|
17
|
+
if (typeof document === 'undefined' || !document.body) return;
|
|
18
|
+
clearTimers();
|
|
19
|
+
const host = ensureHost();
|
|
20
|
+
const next = buildToast(content, tone);
|
|
21
|
+
if (bubble?.isConnected) bubble.replaceWith(next);
|
|
22
|
+
else host.append(next);
|
|
23
|
+
bubble = next;
|
|
24
|
+
hideTimer = window.setTimeout(hide, TOAST_DURATION);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const TOAST_DURATION = 2000;
|
|
28
|
+
const FADE_OUT_MS = 150;
|
|
29
|
+
|
|
30
|
+
const HOST_CLASS = 'pointer-events-none fixed inset-0 z-[1300] flex items-center justify-center px-6';
|
|
31
|
+
const BUBBLE_CLASS = [
|
|
32
|
+
'pointer-events-none flex max-w-[min(80vw,320px)] flex-col items-center gap-1.5',
|
|
33
|
+
'rounded-lg bg-[rgba(0,0,0,0.75)] px-4 py-3 text-center text-sm leading-5 text-white',
|
|
34
|
+
'transition-opacity duration-150 data-[state=closed]:opacity-0 motion-reduce:transition-none',
|
|
35
|
+
].join(' ');
|
|
36
|
+
|
|
37
|
+
let host: HTMLDivElement | null = null;
|
|
38
|
+
let bubble: HTMLDivElement | null = null;
|
|
39
|
+
let hideTimer: number | undefined;
|
|
40
|
+
let removeTimer: number | undefined;
|
|
41
|
+
|
|
42
|
+
function ensureHost(): HTMLDivElement {
|
|
43
|
+
if (host?.isConnected) return host;
|
|
44
|
+
const el = document.createElement('div');
|
|
45
|
+
el.className = HOST_CLASS;
|
|
46
|
+
el.setAttribute('role', 'status');
|
|
47
|
+
el.setAttribute('aria-live', 'polite');
|
|
48
|
+
el.setAttribute('aria-atomic', 'true');
|
|
49
|
+
document.body.append(el);
|
|
50
|
+
host = el;
|
|
51
|
+
return el;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function iconNode(tone: Exclude<ToastTone, 'info'>): SVGSVGElement {
|
|
55
|
+
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
56
|
+
svg.setAttribute('viewBox', '0 0 24 24');
|
|
57
|
+
svg.setAttribute('fill', 'none');
|
|
58
|
+
svg.setAttribute('stroke', 'currentColor');
|
|
59
|
+
svg.setAttribute('stroke-width', '2');
|
|
60
|
+
svg.setAttribute('stroke-linecap', 'round');
|
|
61
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
62
|
+
svg.setAttribute('class', `size-9 ${tone === 'success' ? 'text-success' : 'text-destructive'}`);
|
|
63
|
+
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
64
|
+
circle.setAttribute('cx', '12');
|
|
65
|
+
circle.setAttribute('cy', '12');
|
|
66
|
+
circle.setAttribute('r', '10');
|
|
67
|
+
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
68
|
+
path.setAttribute('d', tone === 'success' ? 'm8 12 3 3 5-6' : 'm9 9 6 6M15 9l-6 6');
|
|
69
|
+
svg.append(circle, path);
|
|
70
|
+
return svg;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function buildToast(content: string, tone: ToastTone): HTMLDivElement {
|
|
74
|
+
const el = document.createElement('div');
|
|
75
|
+
el.className = BUBBLE_CLASS;
|
|
76
|
+
el.setAttribute('data-tbox-toast', '');
|
|
77
|
+
el.setAttribute('data-state', 'open');
|
|
78
|
+
if (tone !== 'info') el.append(iconNode(tone));
|
|
79
|
+
const text = document.createElement('span');
|
|
80
|
+
text.className = '[overflow-wrap:anywhere]';
|
|
81
|
+
// textContent 而非 innerHTML:提示内容是业务/模型文本,不做 HTML 解释。
|
|
82
|
+
text.textContent = content;
|
|
83
|
+
el.append(text);
|
|
84
|
+
return el;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function clearTimers(): void {
|
|
88
|
+
if (hideTimer !== undefined) window.clearTimeout(hideTimer);
|
|
89
|
+
if (removeTimer !== undefined) window.clearTimeout(removeTimer);
|
|
90
|
+
hideTimer = undefined;
|
|
91
|
+
removeTimer = undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function hide(): void {
|
|
95
|
+
const node = bubble;
|
|
96
|
+
if (!node) return;
|
|
97
|
+
node.setAttribute('data-state', 'closed');
|
|
98
|
+
// 引用保留到真正移除:这段时间里再来一条提示,`toast()` 会 replaceWith 掉这个节点(
|
|
99
|
+
// 提前置 null 会让它既不被替换也不被移除,留下永不清理的隐形残骸)。
|
|
100
|
+
removeTimer = window.setTimeout(() => {
|
|
101
|
+
node.remove();
|
|
102
|
+
if (bubble === node) bubble = null;
|
|
103
|
+
if (host && !host.childElementCount) {
|
|
104
|
+
host.remove();
|
|
105
|
+
host = null;
|
|
106
|
+
}
|
|
107
|
+
}, FADE_OUT_MS);
|
|
108
|
+
}
|