From c8e3054247d9347fe9a21a3b9391765ec0707190 Mon Sep 17 00:00:00 2001 From: terwer Date: Sat, 4 Mar 2023 20:20:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#27=20=E4=BD=BF=E7=94=A8=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E7=B3=BB=E7=BB=9F=E6=8C=82=E8=BD=BD=E8=8F=9C=E5=8D=95?= =?UTF-8?q?-=E4=BC=98=E5=8C=96=E6=8F=92=E4=BB=B6=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin-system/plugin-system-hook.js | 68 +++++++++++++------ theme.js | 20 ++++-- 2 files changed, 62 insertions(+), 26 deletions(-) diff --git a/public/lib/plugin/plugin-system/plugin-system-hook.js b/public/lib/plugin/plugin-system/plugin-system-hook.js index 35879c28..7ee72b7d 100644 --- a/public/lib/plugin/plugin-system/plugin-system-hook.js +++ b/public/lib/plugin/plugin-system/plugin-system-hook.js @@ -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") @@ -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.") })() diff --git a/theme.js b/theme.js index fb4426c7..d186b211 100644 --- a/theme.js +++ b/theme.js @@ -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主题构建路径目录 * @@ -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) } }