Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/utools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ pnpm utools:package

- `plugin.json`:uTools 插件的清单文件。
- `preload.js`:在 uTools 渲染进程和 Web 前端之间建立通信的脚本,用于处理插件唤起事件。
- `package.json`:将此目录标记为 CommonJS 模块以兼容 uTools。
- `dist/`:由 Vite 构建输出的静态资源目录。
- `release/`:运行打包命令后生成的插件安装包。
3 changes: 3 additions & 0 deletions apps/utools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
41 changes: 32 additions & 9 deletions apps/utools/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,51 @@
if (typeof window === `undefined`)
return

// 标识当前环境为 uTools
window.__MD_UTOOLS__ = true

const enter = (action) => {
/**
* 安全调用 uTools API 方法
* @param {string} method - 方法名
* @param {...any} args - 方法参数
*/
const safeCall = (method, ...args) => {
try {
window.utools?.hideMainWindowWhenBlur(false)
window.utools?.showMainWindow()
window.utools?.setExpendHeight(680)
if (typeof window.utools?.[method] === `function`) {
window.utools[method](...args)
}
}
catch (error) {
console.warn(`[md][utools] enter failed`, 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 })
// 通知前端应用
window.postMessage({ type: `utools:enter`, payload: action }, `*`)
}

/**
* 插件退出回调
*/
const leave = () => {
window.postMessage({ type: `utools:leave` })
window.postMessage({ type: `utools:leave` }, `*`)
}

window.utools?.onPluginEnter(enter)
window.utools?.onPluginOut(leave)
// 注册生命周期回调
safeCall(`onPluginEnter`, enter)
safeCall(`onPluginOut`, leave)

// 导出插件配置
window.exports = {
'wechat-md': {
mode: `none`,
Expand Down
2 changes: 1 addition & 1 deletion scripts/package-utools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function main() {

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

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