-
Notifications
You must be signed in to change notification settings - Fork 39
/
utils.ts
73 lines (65 loc) · 1.92 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import CSInterface from "../lib/csinterface";
import Vulcan, { VulcanMessage } from "../lib/vulcan";
import { ns } from "../../shared/shared";
export const csi = new CSInterface();
export const vulcan = new Vulcan();
export const openLinkInBrowser = (url: string) => {
if (window.cep) {
csi.openURLInDefaultBrowser(url);
} else {
location.href = url;
}
};
export const evalES = (script: string, isGlobal = false): Promise<string> => {
return new Promise(function (resolve, reject) {
const pre = isGlobal
? ""
: `var host = typeof $ !== 'undefined' ? $ : window; host["${ns}"].`;
const fullString = pre + script;
csi.evalScript(
"try{" + fullString + "}catch(e){alert(e);}",
(res: string) => {
resolve(res);
}
);
});
};
export const evalFile = (file: string) => {
return evalES(
"typeof $ !== 'undefined' ? $.evalFile(\"" +
file +
'") : fl.runScript(FLfile.platformPathToURI("' +
file +
'"));',
true
);
};
export const getAppBackgroundColor = () => {
const { green, blue, red } =
csi.hostEnvironment.appSkinInfo.panelBackgroundColor.color;
return `#${red.toString(16)}${green.toString(16)}${blue.toString(16)}`;
};
declare type IVulcanMessageObject = {
event: string;
callbackID?: string;
data?: string | null;
payload?: object;
};
export const vulcanSend = (id: string, msgObj: IVulcanMessageObject) => {
const msg = new VulcanMessage(VulcanMessage.TYPE_PREFIX + id, null, null);
const msgStr = JSON.stringify(msgObj);
msg.setPayload(msgStr);
vulcan.dispatchMessage(msg);
};
export const vulcanListen = (id: string, callback: Function) => {
vulcan.addMessageListener(
VulcanMessage.TYPE_PREFIX + id,
(res: any) => {
var msgStr = vulcan.getPayload(res);
const msgObj = JSON.parse(msgStr);
callback(msgObj);
},
null
);
};
export const posix = (str: string) => str.replace(/\\/g, "/");