-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathpreload.js
More file actions
59 lines (52 loc) · 1.21 KB
/
preload.js
File metadata and controls
59 lines (52 loc) · 1.21 KB
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
(() => {
if (typeof window === `undefined`)
return
// 标识当前环境为 uTools
window.__MD_UTOOLS__ = true
/**
* 安全调用 uTools API 方法
* @param {string} method - 方法名
* @param {...any} args - 方法参数
*/
const safeCall = (method, ...args) => {
try {
if (typeof window.utools?.[method] === `function`) {
window.utools[method](...args)
}
}
catch (error) {
console.warn(`[md][utools] ${method} failed:`, error)
}
}
/**
* 插件进入回调
* @param {object} action - 插件动作参数
*/
const enter = (action) => {
// 配置 uTools 窗口行为
safeCall(`hideMainWindowWhenBlur`, false)
safeCall(`showMainWindow`)
safeCall(`setExpendHeight`, 680)
// 通知前端应用
window.postMessage({ type: `utools:enter`, payload: action }, `*`)
}
/**
* 插件退出回调
*/
const leave = () => {
window.postMessage({ type: `utools:leave` }, `*`)
}
// 注册生命周期回调
safeCall(`onPluginEnter`, enter)
safeCall(`onPluginOut`, leave)
// 导出插件配置
window.exports = {
'wechat-md': {
mode: `none`,
args: {
enter,
leave,
},
},
}
})()