@trimjs/web-app 0.0.1
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 +916 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.iife.js +4 -0
- package/dist/index.iife.js.map +1 -0
- package/dist/index.js +1041 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.cjs +4 -0
- package/dist/index.umd.cjs.map +1 -0
- package/package.json +28 -0
- package/src/index.ts +257 -0
- package/src/logger.ts +65 -0
- package/src/mobile/index.ts +107 -0
- package/src/mobile/types.ts +9 -0
- package/src/types.ts +18 -0
- package/src/utils/platform.ts +17 -0
- package/src/web/index.ts +27 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { logger } from "../logger";
|
|
2
|
+
import { isFnApp } from "../utils/platform";
|
|
3
|
+
|
|
4
|
+
export interface FlutterInAppWebView {
|
|
5
|
+
callHandler: (fn: string, params?: string) => Promise<string>;
|
|
6
|
+
_platformReady?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare global {
|
|
10
|
+
interface Window {
|
|
11
|
+
flutter_inappwebview: FlutterInAppWebView;
|
|
12
|
+
fnAppMessage?: (message: string) => void;
|
|
13
|
+
onAppHeaderGoBack?: () => boolean | number | string; // 随意返回
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const initMobile = () => {
|
|
18
|
+
/**
|
|
19
|
+
* 处理app有回掉的方法消息
|
|
20
|
+
*/
|
|
21
|
+
return new Promise<FlutterInAppWebView>((res) => {
|
|
22
|
+
if (window.flutter_inappwebview?._platformReady) {
|
|
23
|
+
logger.log("flutter_inappwebview is ready");
|
|
24
|
+
res(window.flutter_inappwebview);
|
|
25
|
+
} else {
|
|
26
|
+
window.addEventListener("flutterInAppWebViewPlatformReady", () => {
|
|
27
|
+
logger.log("flutter_inappwebview is ready");
|
|
28
|
+
res(window.flutter_inappwebview);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export interface FnAppMessage<T> {
|
|
35
|
+
reqId: string;
|
|
36
|
+
method: string;
|
|
37
|
+
result: T;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const callHandler = async <T>(
|
|
41
|
+
flutterInAppWebView: FlutterInAppWebView,
|
|
42
|
+
fn: string,
|
|
43
|
+
params?: string
|
|
44
|
+
): Promise<T | null> => {
|
|
45
|
+
logger.log(`callHandler: ${fn}, params: ${params}`);
|
|
46
|
+
const res = await flutterInAppWebView.callHandler(fn, params);
|
|
47
|
+
if (res) {
|
|
48
|
+
try {
|
|
49
|
+
const messageData: FnAppMessage<string> = JSON.parse(res);
|
|
50
|
+
logger.log(`messageData: ${JSON.stringify(messageData)}`);
|
|
51
|
+
const appMessage: T = JSON.parse(messageData.result);
|
|
52
|
+
logger.log(`appMessage: ${JSON.stringify(appMessage)}`);
|
|
53
|
+
return appMessage;
|
|
54
|
+
} catch (error) {
|
|
55
|
+
logger.error(`error: ${error}`);
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// 存储app消息的回调
|
|
63
|
+
const fnAppMessageHandleMap = new Map<
|
|
64
|
+
string,
|
|
65
|
+
(Response: FnAppMessage<any>) => void
|
|
66
|
+
>();
|
|
67
|
+
|
|
68
|
+
function initAppMessageEvent() {
|
|
69
|
+
window.fnAppMessage = (message: string) => {
|
|
70
|
+
logger.log("app-message", message);
|
|
71
|
+
const data: FnAppMessage<any> = JSON.parse(message);
|
|
72
|
+
|
|
73
|
+
const handle = fnAppMessageHandleMap.get(data.reqId);
|
|
74
|
+
if (handle) {
|
|
75
|
+
handle(data);
|
|
76
|
+
fnAppMessageHandleMap.delete(data.reqId);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (isFnApp()) {
|
|
82
|
+
initAppMessageEvent();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function pickFilePromise(
|
|
86
|
+
flutterInAppWebView: FlutterInAppWebView,
|
|
87
|
+
params: {
|
|
88
|
+
title: string;
|
|
89
|
+
multiple: boolean;
|
|
90
|
+
directory: boolean;
|
|
91
|
+
}
|
|
92
|
+
): Promise<string[] | undefined> {
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
flutterInAppWebView
|
|
95
|
+
?.callHandler("openFolder", JSON.stringify(params))
|
|
96
|
+
.then((reqId) => {
|
|
97
|
+
if (reqId) {
|
|
98
|
+
function handle(data: FnAppMessage<string[]>) {
|
|
99
|
+
resolve(data.result);
|
|
100
|
+
}
|
|
101
|
+
fnAppMessageHandleMap.set(reqId, handle);
|
|
102
|
+
} else {
|
|
103
|
+
reject(new Error("openFolder failed"));
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK 配置选项
|
|
3
|
+
*/
|
|
4
|
+
export interface TrimAppOptions {
|
|
5
|
+
/** 是否开启 debug 模式,开启后会输出日志 */
|
|
6
|
+
debug?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface PlatformConfig {
|
|
10
|
+
theme: "dark" | "light";
|
|
11
|
+
language: string; // "zh-CN" | "en-US" ....
|
|
12
|
+
appVersion?: string;
|
|
13
|
+
systemVersion: string;
|
|
14
|
+
format: {
|
|
15
|
+
date?: string; // "YYYY-MM-DD" | "MM-DD-YYYY" | "DD-MM-YYYY" | "YYYY/MM/DD" | "MM/DD/YYYY" | "DD/MM/YYYY"
|
|
16
|
+
time?: string; // "12h" | "24h"
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function fnAppUAMatch(ua: string) {
|
|
2
|
+
const regex = /FNOS\/([\d.]+)|FNAppType\/(\w+)|FNAppVer\/([\d.]+)/g;
|
|
3
|
+
|
|
4
|
+
const matches: any = {};
|
|
5
|
+
let match;
|
|
6
|
+
while ((match = regex.exec(ua)) !== null) {
|
|
7
|
+
if (match[1]) matches.fnOSVersion = match[1];
|
|
8
|
+
if (match[2]) matches.fnAppType = match[2];
|
|
9
|
+
if (match[3]) matches.fnAppVersion = match[3];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return matches;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const isFnApp = () => {
|
|
16
|
+
return fnAppUAMatch(navigator.userAgent).fnAppVersion;
|
|
17
|
+
};
|
package/src/web/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { connectToOS } from "@fn/micro-app-postmate";
|
|
2
|
+
import { OSConfig } from "@fn/micro-app-postmate/src/parent/type";
|
|
3
|
+
import {
|
|
4
|
+
OSMethods,
|
|
5
|
+
ConnectionResult,
|
|
6
|
+
AsyncMethodReturns,
|
|
7
|
+
} from "@fn/micro-app-postmate/src/types";
|
|
8
|
+
import type { PlatformConfig, TrimAppOptions } from "../types";
|
|
9
|
+
|
|
10
|
+
export type OSConnection = AsyncMethodReturns<
|
|
11
|
+
ConnectionResult<
|
|
12
|
+
OSMethods & {
|
|
13
|
+
setExitPageTips: (params?: { title?: string; content?: string }) => void;
|
|
14
|
+
openFileManagerApp: (path: string) => void;
|
|
15
|
+
openFile: (path: string) => void;
|
|
16
|
+
getPlatformConfig: () => Promise<PlatformConfig>;
|
|
17
|
+
close: () => void;
|
|
18
|
+
},
|
|
19
|
+
OSConfig
|
|
20
|
+
>
|
|
21
|
+
>;
|
|
22
|
+
|
|
23
|
+
export const initWeb = (options?: TrimAppOptions) => {
|
|
24
|
+
return connectToOS({
|
|
25
|
+
debug: options?.debug,
|
|
26
|
+
}).promise;
|
|
27
|
+
};
|