|
2 | 2 | if (typeof window === `undefined`) |
3 | 3 | return |
4 | 4 |
|
| 5 | + // 标识当前环境为 uTools |
5 | 6 | window.__MD_UTOOLS__ = true |
6 | 7 |
|
7 | | - const enter = (action) => { |
| 8 | + /** |
| 9 | + * 安全调用 uTools API 方法 |
| 10 | + * @param {string} method - 方法名 |
| 11 | + * @param {...any} args - 方法参数 |
| 12 | + */ |
| 13 | + const safeCall = (method, ...args) => { |
8 | 14 | try { |
9 | | - window.utools?.hideMainWindowWhenBlur(false) |
10 | | - window.utools?.showMainWindow() |
11 | | - window.utools?.setExpendHeight(680) |
| 15 | + if (typeof window.utools?.[method] === `function`) { |
| 16 | + window.utools[method](...args) |
| 17 | + } |
12 | 18 | } |
13 | 19 | catch (error) { |
14 | | - console.warn(`[md][utools] enter failed`, error) |
| 20 | + console.warn(`[md][utools] ${method} failed:`, error) |
15 | 21 | } |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * 插件进入回调 |
| 26 | + * @param {object} action - 插件动作参数 |
| 27 | + */ |
| 28 | + const enter = (action) => { |
| 29 | + // 配置 uTools 窗口行为 |
| 30 | + safeCall(`hideMainWindowWhenBlur`, false) |
| 31 | + safeCall(`showMainWindow`) |
| 32 | + safeCall(`setExpendHeight`, 680) |
16 | 33 |
|
17 | | - window.postMessage({ type: `utools:enter`, payload: action }) |
| 34 | + // 通知前端应用 |
| 35 | + window.postMessage({ type: `utools:enter`, payload: action }, `*`) |
18 | 36 | } |
19 | 37 |
|
| 38 | + /** |
| 39 | + * 插件退出回调 |
| 40 | + */ |
20 | 41 | const leave = () => { |
21 | | - window.postMessage({ type: `utools:leave` }) |
| 42 | + window.postMessage({ type: `utools:leave` }, `*`) |
22 | 43 | } |
23 | 44 |
|
24 | | - window.utools?.onPluginEnter(enter) |
25 | | - window.utools?.onPluginOut(leave) |
| 45 | + // 注册生命周期回调 |
| 46 | + safeCall(`onPluginEnter`, enter) |
| 47 | + safeCall(`onPluginOut`, leave) |
26 | 48 |
|
| 49 | + // 导出插件配置 |
27 | 50 | window.exports = { |
28 | 51 | 'wechat-md': { |
29 | 52 | mode: `none`, |
|
0 commit comments