Skip to content

Commit 32da1bf

Browse files
committed
fix: build plugin
1 parent ddc64c1 commit 32da1bf

4 files changed

Lines changed: 37 additions & 10 deletions

File tree

apps/utools/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ pnpm utools:package
2727

2828
- `plugin.json`:uTools 插件的清单文件。
2929
- `preload.js`:在 uTools 渲染进程和 Web 前端之间建立通信的脚本,用于处理插件唤起事件。
30+
- `package.json`:将此目录标记为 CommonJS 模块以兼容 uTools。
3031
- `dist/`:由 Vite 构建输出的静态资源目录。
3132
- `release/`:运行打包命令后生成的插件安装包。

apps/utools/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

apps/utools/preload.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,51 @@
22
if (typeof window === `undefined`)
33
return
44

5+
// 标识当前环境为 uTools
56
window.__MD_UTOOLS__ = true
67

7-
const enter = (action) => {
8+
/**
9+
* 安全调用 uTools API 方法
10+
* @param {string} method - 方法名
11+
* @param {...any} args - 方法参数
12+
*/
13+
const safeCall = (method, ...args) => {
814
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+
}
1218
}
1319
catch (error) {
14-
console.warn(`[md][utools] enter failed`, error)
20+
console.warn(`[md][utools] ${method} failed:`, error)
1521
}
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)
1633

17-
window.postMessage({ type: `utools:enter`, payload: action })
34+
// 通知前端应用
35+
window.postMessage({ type: `utools:enter`, payload: action }, `*`)
1836
}
1937

38+
/**
39+
* 插件退出回调
40+
*/
2041
const leave = () => {
21-
window.postMessage({ type: `utools:leave` })
42+
window.postMessage({ type: `utools:leave` }, `*`)
2243
}
2344

24-
window.utools?.onPluginEnter(enter)
25-
window.utools?.onPluginOut(leave)
45+
// 注册生命周期回调
46+
safeCall(`onPluginEnter`, enter)
47+
safeCall(`onPluginOut`, leave)
2648

49+
// 导出插件配置
2750
window.exports = {
2851
'wechat-md': {
2952
mode: `none`,

scripts/package-utools.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async function main() {
7070

7171
await cp(distDir, path.join(packageRoot, `dist`), { recursive: true })
7272

73-
for (const file of [`plugin.json`, `preload.js`, `logo.png`, `README.md`]) {
73+
for (const file of [`plugin.json`, `preload.js`, `logo.png`, `README.md`, `package.json`]) {
7474
const source = path.join(utoolsDir, file)
7575
await ensureFileExists(source, `apps/utools/${file}`)
7676
await cp(source, path.join(packageRoot, file), { recursive: true })

0 commit comments

Comments
 (0)