feat: 使App.vue和VirtualDirectory解耦

This commit is contained in:
539943419 committed 2024-12-24 14:19:08 +08:00
1 parent 6d550505a0
commit 4aba71d451
8 files changed
+162 -66

No files matched your search

@@ -0,0 +1,36 @@
import { Plugin, PluginGroup } from "@/class/PluginCenter";
import { PluginExportType } from "@/types/type";
import { watchEffect } from "vue";
const pluginConfig: Plugin = {
name: "初始化完成之后打开miaoDirectory",
filter: () => {
return false;
},
disable: false,
group: [PluginGroup.immediately],
func: (_, __, hooks) => {
const { getPluginCenter, getRootVDirectory, getViews } = hooks
const views = getViews();
const pc = getPluginCenter();
const root = getRootVDirectory();
setTimeout(() => {
// if (views.length === 0) {
// pc.usePlugin('miaoDirectory', [root])
// }
pc.deregister('miaoDirectory_onInit')
watchEffect(() => {
if(views.length === 0) {
pc.usePlugin('miaoDirectory', [root])
}
})
})
},
priority: -1
};
export default pluginConfig;
export const type = PluginExportType.default;
export const key = "miaoDirectory_onInit";
@@ -0,0 +1,36 @@
import type { Plugin, registerComponentOption } from "@/class/PluginCenter";
export function checkNormalPlugin(obj: any): obj is Plugin {
return (
typeof obj === "object" &&
obj!== null &&
typeof obj.name === "string" &&
(obj.icon === undefined || typeof obj.icon === "object") &&
typeof obj.disable === "boolean" &&
Array.isArray(obj.group) &&
obj.group.every((group: any) => typeof group === "string") &&
typeof obj.priority === "number" &&
typeof obj.filter === "function" &&
typeof obj.func === "function"
);
}
export function checkComponentPlugin(
obj: any
): obj is registerComponentOption {
return (
typeof obj === "object" &&
obj!== null &&
typeof obj.key === "string" &&
typeof obj.name === "string" &&
(obj.icon === undefined || typeof obj.icon === "object") &&
typeof obj.getComponent === "function" &&
((obj.group === undefined) || (Array.isArray(obj.group) && obj.group.every((item: any) => typeof item === "string"))) &&
typeof obj.filter === "function" &&
(obj.disable === undefined || typeof obj.disable === "boolean") &&
(obj.exitConfirm === undefined || typeof obj.exitConfirm === "boolean") &&
(obj.single === undefined || typeof obj.single === "boolean") &&
(obj.priority === undefined || typeof obj.priority === "number")
);
}
@@ -2,24 +2,35 @@ import VirtualDirectory, { VirtualFile } from "@/class/VirtualDirectory";
// import useMiaoFetchApi from "@/hooks/useMiaoFetchApi";
import { PluginExportType, usePluginHooksType } from "@/types/type";
import type { Plugin, registerComponentOption } from "@/class/PluginCenter";
import { checkComponentPlugin, checkNormalPlugin } from "./checkPluginAvailable";
// const miaoFetchApi = useMiaoFetchApi()
export default async function registerExternalPlugin(_VDirectories: VirtualDirectory[], VFiles: VirtualFile[], hooks: usePluginHooksType) {
const targetPluginVFile = VFiles[0] as VirtualFile
// const pluginU8iArr = await miaoFetchApi.getFile(targetPluginVFile).response
// const pluginText = new TextDecoder().decode(pluginU8iArr)
// const plugin = await import(/* @vite-ignore */'')
const plugin = await import(/* @vite-ignore */ targetPluginVFile.url) as {
default: registerComponentOption | Plugin;
type: PluginExportType;
key?: string;
}
// console.log(plugin)
const pc = hooks.getPluginCenter()
if(plugin.type === PluginExportType.default) {
pc.register(`${Math.random()}`, plugin.default as Plugin)
} else {
pc.registerComponent(plugin.default as registerComponentOption)
try {
const targetPluginVFile = VFiles[0] as VirtualFile
const plugin = await import(/* @vite-ignore */ targetPluginVFile.url) as {
default: registerComponentOption | Plugin;
type: PluginExportType;
key?: string;
}
const pc = hooks.getPluginCenter()
if (plugin.type === PluginExportType.default) {
if (checkNormalPlugin(plugin.default) === false) {
throw('插件不合法')
}
pc.register(plugin.key ?? `${Math.random()}`, plugin.default as Plugin)
} else {
if (checkComponentPlugin(plugin.default) === false) {
throw('插件不合法')
}
pc.registerComponent(plugin.default as registerComponentOption)
}
} catch (e) {
console.error(e);
hooks.globalAlertTip('导入外部插件失败', {
type: 'error',
timeout: 3000
})
}
}