@ayden-fc2/riffle-bridge-web 1.0.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 ADDED
@@ -0,0 +1,264 @@
1
+ # @ayden-fc2/riffle-bridge-web
2
+
3
+ Riffle Bridge Web SDK - WebView 与 React Native 通信桥接库
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @ayden-fc2/riffle-bridge-web
9
+ # 或
10
+ pnpm add @ayden-fc2/riffle-bridge-web
11
+ ```
12
+
13
+ ## 快速开始
14
+
15
+ ### 基础使用
16
+
17
+ ```typescript
18
+ import { RiffleBridge } from '@ayden-fc2/riffle-bridge-web';
19
+
20
+ const bridge = new RiffleBridge();
21
+
22
+ // 直接使用(如果不在 RN WebView 中会抛出错误)
23
+ await bridge.vibrate('success');
24
+
25
+ const deviceInfo = await bridge.device.getDeviceInfo();
26
+ console.log(deviceInfo);
27
+ ```
28
+
29
+ ### Tweaks 配置系统
30
+
31
+ ```tsx
32
+ import React, { useMemo } from 'react';
33
+ import { createTweaks } from '@ayden-fc2/riffle-bridge-web';
34
+
35
+ // 定义配置
36
+ const tweaksConfig = {
37
+ color: { name: '颜色', type: 'color' as const, value: '#667eea' },
38
+ size: { name: '大小', type: 'number' as const, value: 150, min: 50, max: 300 },
39
+ enabled: { name: '启用', type: 'boolean' as const, value: true },
40
+ };
41
+
42
+ function App() {
43
+ // 初始化(必须传入 React)
44
+ const tweaks = useMemo(() => createTweaks(tweaksConfig, { React }), []);
45
+
46
+ // 响应式获取值
47
+ const color = tweaks.color.useState();
48
+ const size = tweaks.size.useState();
49
+ const enabled = tweaks.enabled.useState();
50
+
51
+ if (!enabled) return null;
52
+
53
+ return (
54
+ <div style={{ backgroundColor: color, width: size, height: size }}>
55
+ Hello Tweaks!
56
+ </div>
57
+ );
58
+ }
59
+ ```
60
+
61
+ ### 错误处理
62
+
63
+ 如果不在 React Native WebView 环境中运行,SDK 会抛出明确的错误:
64
+
65
+ ```typescript
66
+ try {
67
+ await bridge.vibrate('success');
68
+ } catch (error) {
69
+ // [RiffleBridge] Not available. This SDK requires React Native WebView environment.
70
+ console.error(error.message);
71
+ }
72
+ ```
73
+
74
+ ## API 文档
75
+
76
+ ### RiffleBridge
77
+
78
+ 主类,提供所有 Native 功能访问。
79
+
80
+ ```typescript
81
+ const bridge = new RiffleBridge();
82
+
83
+ // 检测
84
+ bridge.isAvailable() // 是否在 RN WebView 中
85
+ bridge.waitForReady(timeout) // 等待 Bridge 就绪
86
+ bridge.getVersion() // SDK 版本
87
+
88
+ // 便捷方法
89
+ bridge.vibrate(type) // 震动
90
+ ```
91
+
92
+ ### 震动模块 (bridge.haptic)
93
+
94
+ ```typescript
95
+ await bridge.haptic.light();
96
+ await bridge.haptic.medium();
97
+ await bridge.haptic.heavy();
98
+ await bridge.haptic.success();
99
+ await bridge.haptic.warning();
100
+ await bridge.haptic.error();
101
+ await bridge.haptic.selection();
102
+
103
+ // 序列震动
104
+ await bridge.haptic.sequence(['light', 'medium', 'heavy'], 200);
105
+
106
+ // 根据强度震动
107
+ await bridge.haptic.intensity(0.5); // 0-1
108
+ ```
109
+
110
+ ### 传感器模块 (bridge.sensor)
111
+
112
+ ```typescript
113
+ // 启动
114
+ await bridge.sensor.start({
115
+ types: ['accelerometer', 'gyroscope'],
116
+ interval: 100
117
+ });
118
+
119
+ // 监听
120
+ bridge.sensor.onAccelerometer((data) => {
121
+ const { x, y, z } = data[data.length - 1];
122
+ console.log('加速度:', x, y, z);
123
+ });
124
+
125
+ bridge.sensor.onGyroscope((data) => { /* ... */ });
126
+ bridge.sensor.onMagnetometer((data) => { /* ... */ });
127
+ bridge.sensor.onBarometer((data) => { /* ... */ });
128
+
129
+ // 停止
130
+ await bridge.sensor.stop();
131
+ ```
132
+
133
+ ### 设备信息模块 (bridge.device)
134
+
135
+ ```typescript
136
+ const device = await bridge.device.getDeviceInfo();
137
+ const battery = await bridge.device.getBatteryInfo();
138
+ const network = await bridge.device.getNetworkInfo();
139
+ const system = await bridge.device.getSystemInfo();
140
+ const app = await bridge.device.getAppInfo();
141
+ const screen = await bridge.device.getScreenInfo();
142
+ const all = await bridge.device.getAllInfo();
143
+ ```
144
+
145
+ ### 文件存储模块 (bridge.fileStorage)
146
+
147
+ ```typescript
148
+ // 初始化
149
+ await bridge.fileStorage.init();
150
+
151
+ // 拍照/相册
152
+ const photo = await bridge.fileStorage.takePhoto();
153
+ const picked = await bridge.fileStorage.pickFromGallery();
154
+
155
+ // 文件操作
156
+ const files = await bridge.fileStorage.listFiles('images');
157
+ await bridge.fileStorage.deleteFile(filename, subfolder);
158
+
159
+ // URL 映射
160
+ await bridge.fileStorage.addCustomFileMapping('local://avatar', uri);
161
+ const cached = await bridge.fileStorage.getLocalFileByUrl('local://avatar');
162
+
163
+ // 智能下载(自动缓存)
164
+ const result = await bridge.fileStorage.downloadWithCache(url);
165
+
166
+ // 读取为 Base64
167
+ const base64 = await bridge.fileStorage.readAsBase64(uriOrKey);
168
+ ```
169
+
170
+ ### 音频模块 (bridge.audio)
171
+
172
+ ```typescript
173
+ // 播放(智能 URI 解析)
174
+ await bridge.audio.playOnline({ uri: 'https://example.com/audio.mp3' });
175
+ await bridge.audio.playOnline({ uri: 'local://recording/memo' });
176
+
177
+ // 控制
178
+ await bridge.audio.pause();
179
+ await bridge.audio.resume();
180
+ await bridge.audio.stop();
181
+ await bridge.audio.setVolume(0.5);
182
+ await bridge.audio.setRate(1.5);
183
+ ```
184
+
185
+ ### 相机模块 (bridge.camera)
186
+
187
+ ```typescript
188
+ // 打开相机
189
+ await bridge.camera.open({ facing: 'back' });
190
+
191
+ // 设置透明背景
192
+ document.body.style.background = 'transparent';
193
+
194
+ // 拍照
195
+ const photo = await bridge.camera.takePhoto();
196
+
197
+ // 切换
198
+ await bridge.camera.toggleFacing();
199
+ await bridge.camera.toggleFlash();
200
+
201
+ // 滤镜
202
+ await bridge.camera.setFilter('grayscale');
203
+
204
+ // 关闭
205
+ await bridge.camera.close();
206
+ ```
207
+
208
+ ### 麦克风模块 (bridge.microphone)
209
+
210
+ ```typescript
211
+ // 请求权限
212
+ const perm = await bridge.microphone.requestPermission();
213
+
214
+ // 录音
215
+ await bridge.microphone.start({ enableMonitoring: true });
216
+
217
+ // 监听音量
218
+ bridge.microphone.onVolumeData((data) => {
219
+ console.log('音量:', data[0].normalizedVolume);
220
+ });
221
+
222
+ // 停止
223
+ const result = await bridge.microphone.stop();
224
+ console.log('URI:', result.uri);
225
+ ```
226
+
227
+ ### 工具函数 (bridge.utils)
228
+
229
+ ```typescript
230
+ // 向量模长
231
+ const mag = bridge.utils.magnitude(x, y, z);
232
+
233
+ // 倾斜方向
234
+ const tilt = bridge.utils.getTiltDirection(x, y);
235
+ // => 'forward' | 'backward' | 'left' | 'right' | 'center'
236
+
237
+ // 摇晃强度
238
+ const shake = bridge.utils.getShakeIntensity(x, y, z);
239
+ // => 'none' | 'light' | 'medium' | 'strong'
240
+ ```
241
+
242
+ ## 类型定义
243
+
244
+ 完整的 TypeScript 类型支持,无需额外安装 @types 包。
245
+
246
+ ```typescript
247
+ import type {
248
+ HapticFeedbackType,
249
+ SensorData,
250
+ DeviceInfo,
251
+ TweakConfig,
252
+ // ... 更多类型
253
+ } from '@ayden-fc2/riffle-bridge-web';
254
+ ```
255
+
256
+ ## 与 Native 端配合
257
+
258
+ 此包需要配合 React Native 端的 `RiffleBridge` 和 `GamePlayer` 组件使用。
259
+
260
+ Native 端需要在 WebView 中注入精简版通信脚本,详见项目文档。
261
+
262
+ ## License
263
+
264
+ MIT