@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 +264 -0
- package/dist/index.d.mts +719 -0
- package/dist/index.d.ts +719 -0
- package/dist/index.js +942 -0
- package/dist/index.mjs +899 -0
- package/package.json +47 -0
- package/src/bridge.ts +157 -0
- package/src/controllers/index.ts +527 -0
- package/src/core.ts +236 -0
- package/src/index.ts +94 -0
- package/src/tweaks.ts +383 -0
- package/src/types.ts +295 -0
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ayden-fc2/riffle-bridge-web",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Riffle Bridge Web SDK - WebView 与 Native 通信桥接库",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
14
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
15
|
+
"typecheck": "tsc --noEmit",
|
|
16
|
+
"lint": "eslint src --ext .ts,.tsx"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react-native",
|
|
20
|
+
"webview",
|
|
21
|
+
"bridge",
|
|
22
|
+
"riffle",
|
|
23
|
+
"sdk"
|
|
24
|
+
],
|
|
25
|
+
"author": "Riffle Team",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"react": ">=17.0.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependenciesMeta": {
|
|
31
|
+
"react": {
|
|
32
|
+
"optional": true
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/react": "^18.2.0",
|
|
37
|
+
"tsup": "^8.0.0",
|
|
38
|
+
"typescript": "^5.0.0"
|
|
39
|
+
},
|
|
40
|
+
"exports": {
|
|
41
|
+
".": {
|
|
42
|
+
"types": "./dist/index.d.ts",
|
|
43
|
+
"import": "./dist/index.mjs",
|
|
44
|
+
"require": "./dist/index.js"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/bridge.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Riffle Bridge 主类
|
|
3
|
+
*
|
|
4
|
+
* 提供完整的 WebView Bridge API
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { BridgeCore, getBridgeCore } from './core';
|
|
8
|
+
import {
|
|
9
|
+
HapticController,
|
|
10
|
+
SensorController,
|
|
11
|
+
DeviceController,
|
|
12
|
+
FileStorageController,
|
|
13
|
+
AudioController,
|
|
14
|
+
CameraController,
|
|
15
|
+
MicrophoneController,
|
|
16
|
+
ConfigController,
|
|
17
|
+
} from './controllers';
|
|
18
|
+
import type { HapticFeedbackType, TiltDirection, ShakeIntensity } from './types';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 工具函数类
|
|
22
|
+
*/
|
|
23
|
+
export class Utils {
|
|
24
|
+
/**
|
|
25
|
+
* 计算向量模长
|
|
26
|
+
*/
|
|
27
|
+
static magnitude(x: number, y: number, z: number): number {
|
|
28
|
+
return Math.sqrt(x * x + y * y + z * z);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 获取倾斜方向
|
|
33
|
+
*/
|
|
34
|
+
static getTiltDirection(x: number, y: number, threshold = 2): TiltDirection {
|
|
35
|
+
if (Math.abs(x) > Math.abs(y)) {
|
|
36
|
+
return x > threshold ? 'right' : x < -threshold ? 'left' : 'center';
|
|
37
|
+
} else {
|
|
38
|
+
return y > threshold ? 'forward' : y < -threshold ? 'backward' : 'center';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 获取摇晃强度
|
|
44
|
+
*/
|
|
45
|
+
static getShakeIntensity(x: number, y: number, z: number): ShakeIntensity {
|
|
46
|
+
const magnitude = this.magnitude(x, y, z);
|
|
47
|
+
if (magnitude > 15) return 'strong';
|
|
48
|
+
if (magnitude > 8) return 'medium';
|
|
49
|
+
if (magnitude > 3) return 'light';
|
|
50
|
+
return 'none';
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* RiffleBridge 主类
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* import { RiffleBridge } from '@riffle/bridge-web';
|
|
60
|
+
*
|
|
61
|
+
* const bridge = new RiffleBridge();
|
|
62
|
+
*
|
|
63
|
+
* // 检查是否可用
|
|
64
|
+
* if (bridge.isAvailable()) {
|
|
65
|
+
* // 震动
|
|
66
|
+
* await bridge.vibrate('success');
|
|
67
|
+
*
|
|
68
|
+
* // 使用传感器
|
|
69
|
+
* await bridge.sensor.start({ types: ['accelerometer'] });
|
|
70
|
+
* bridge.sensor.onAccelerometer((data) => {
|
|
71
|
+
* console.log(data);
|
|
72
|
+
* });
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export class RiffleBridge {
|
|
77
|
+
private core: BridgeCore;
|
|
78
|
+
|
|
79
|
+
/** 震动控制器 */
|
|
80
|
+
public readonly haptic: HapticController;
|
|
81
|
+
/** 传感器控制器 */
|
|
82
|
+
public readonly sensor: SensorController;
|
|
83
|
+
/** 设备信息控制器 */
|
|
84
|
+
public readonly device: DeviceController;
|
|
85
|
+
/** 文件存储控制器 */
|
|
86
|
+
public readonly fileStorage: FileStorageController;
|
|
87
|
+
/** 音频控制器 */
|
|
88
|
+
public readonly audio: AudioController;
|
|
89
|
+
/** 相机控制器 */
|
|
90
|
+
public readonly camera: CameraController;
|
|
91
|
+
/** 麦克风控制器 */
|
|
92
|
+
public readonly microphone: MicrophoneController;
|
|
93
|
+
/** 配置控制器 */
|
|
94
|
+
public readonly config: ConfigController;
|
|
95
|
+
/** 工具函数 */
|
|
96
|
+
public readonly utils = Utils;
|
|
97
|
+
|
|
98
|
+
constructor() {
|
|
99
|
+
this.core = getBridgeCore();
|
|
100
|
+
|
|
101
|
+
// 初始化各控制器
|
|
102
|
+
this.haptic = new HapticController(this.core);
|
|
103
|
+
this.sensor = new SensorController(this.core);
|
|
104
|
+
this.device = new DeviceController(this.core);
|
|
105
|
+
this.fileStorage = new FileStorageController(this.core);
|
|
106
|
+
this.audio = new AudioController(this.core, this.fileStorage);
|
|
107
|
+
this.camera = new CameraController(this.core);
|
|
108
|
+
this.microphone = new MicrophoneController(this.core);
|
|
109
|
+
this.config = new ConfigController(this.core);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 检查 Bridge 是否可用
|
|
114
|
+
*/
|
|
115
|
+
isAvailable(): boolean {
|
|
116
|
+
return this.core.isAvailable();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 等待 Bridge 就绪
|
|
121
|
+
*/
|
|
122
|
+
async waitForReady(timeout?: number): Promise<boolean> {
|
|
123
|
+
return this.core.waitForReady(timeout);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 获取 SDK 版本
|
|
128
|
+
*/
|
|
129
|
+
getVersion(): string {
|
|
130
|
+
return '1.0.0';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 便捷方法:震动
|
|
135
|
+
*/
|
|
136
|
+
async vibrate(type: HapticFeedbackType = 'light'): Promise<void> {
|
|
137
|
+
const method = this.haptic[type as keyof HapticController];
|
|
138
|
+
if (typeof method === 'function') {
|
|
139
|
+
await (method as () => Promise<void>).call(this.haptic);
|
|
140
|
+
} else {
|
|
141
|
+
throw new Error(`Unsupported haptic type: ${type}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 单例实例
|
|
147
|
+
let riffleBridgeInstance: RiffleBridge | null = null;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 获取 RiffleBridge 单例
|
|
151
|
+
*/
|
|
152
|
+
export function getRiffleBridge(): RiffleBridge {
|
|
153
|
+
if (!riffleBridgeInstance) {
|
|
154
|
+
riffleBridgeInstance = new RiffleBridge();
|
|
155
|
+
}
|
|
156
|
+
return riffleBridgeInstance;
|
|
157
|
+
}
|