Skip to content

Commit

Permalink
feat: #27 使用插件系统挂载菜单-优化插件系统集成
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 4, 2023
1 parent f19a133 commit c8e3054
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 26 deletions.
68 changes: 46 additions & 22 deletions public/lib/plugin/plugin-system/plugin-system-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
// 警告1⚠️:请勿在非思源笔记Electron环境调用此文件中的任何方法
// 警告2⚠️:此文件请勿引用其他任何需要编译的类库

const log = (...p) => {
console.log(`[zhi-hook] plugin-system-hook`, ...p)
}

const warn = (...p) => {
console.warn(`[zhi-hook] theme`, ...p)
}

const getCrossPlatformAppDataFolder = () => {
const path = window.require("path")

Expand All @@ -45,30 +53,46 @@ const getCrossPlatformAppDataFolder = () => {
}

const initPluginSystem = async () => {
const path = window.require("path")
try {
const data = window
.require("fs")
.readFileSync(
path.join(getCrossPlatformAppDataFolder(), ".siyuan", "plugin.js")
)
const script = data.toString("utf8")
console.log("local plugin system found, loading...")
eval(script)
} catch (e) {
console.log("local plugin system not found, load online")
return fetch(
"https://gitee.com/zuoez02/siyuan-plugin-system/raw/main/main.js",
{ cache: "no-cache" }
const path = window.require("path")
try {
const data = window
.require("fs")
.readFileSync(
path.join(getCrossPlatformAppDataFolder(), ".siyuan", "plugin.js")
)
.then((res) => res.text())
.then((sc) => {
window.siyuanPluginScript = sc
eval(sc)
})
}
const script = data.toString("utf8")
log("local plugin system found, loading...")
eval(script)
} catch (e) {
log("local plugin system not found, load online")
return fetch(
"https://gitee.com/zuoez02/siyuan-plugin-system/raw/main/main.js",
{ cache: "no-cache" }
)
.then((res) => res.text())
.then((sc) => {
window.siyuanPluginScript = sc
eval(sc)
})
}
}

const syncZhiPlugins = async () => {
log("Start syncing zhi plugin ", "xxx")
}

;(async () => {
await initPluginSystem()
if (!window.pluginSystem) {
log("Undetected plugin system,initiating plugin system...")
await initPluginSystem()
} else {
warn(
"Plugin system already loaded by snapshots, ignore initiation!Loaded plugin system version is ",
window.pluginSystemVersion
)
}

log("Plugin system initiation finished,syncing zhi theme plugins...")
await syncZhiPlugins()
log("Syncing zhi theme plugins finished.")
})()
20 changes: 16 additions & 4 deletions theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
// 警告1⚠️:思源笔记启动会自动加载此文件,请勿调用此文件中的任何方法
// 警告2⚠️:此文件请勿引用其他任何需要编译的类库

const log = (...p) => {
console.log(`[zhi-hook] theme`, ...p)
}

const warn = (...p) => {
console.warn(`[zhi-hook] theme`, ...p)
}

const error = (...p) => {
console.error(`[zhi-hook] theme`, ...p)
}

/**
* 获取zhi主题构建路径目录
*
Expand Down Expand Up @@ -62,14 +74,14 @@ const safeImport = async (libpath) => {

try {
if (!fs.existsSync(realpath)) {
console.warn("依赖库不存在,请排查。依赖库路径=>", realpath)
warn("Dependency not found, path is=>", libpath)
return
}
console.log("将要从以下位置引入依赖=>", libpath)
await import(libpath)
log("Loaded dependency=>", libpath)
} catch (e) {
console.error("依赖库加载失败,请排查。依赖库路径=>", realpath)
console.error(e)
error("Failed to load dependency!Dependency path=>", libpath)
error(e)
}
}

Expand Down

0 comments on commit c8e3054

Please sign in to comment.