mirror of
https://github.com/rubickCenter/rubick
synced 2025-12-22 18:22:53 +08:00
♻️ 代码重构
This commit is contained in:
@@ -1,21 +1,13 @@
|
||||
import { reactive, toRefs, toRaw } from "vue";
|
||||
import { reactive, toRefs, ref } from "vue";
|
||||
import { nativeImage, remote, ipcRenderer } from "electron";
|
||||
import { appSearch, PluginHandler } from "@/core";
|
||||
import path from "path";
|
||||
import throttle from "lodash.throttle";
|
||||
import commonConst from "@/common/utils/commonConst";
|
||||
import searchManager from "./search";
|
||||
import optionsManager from "./options";
|
||||
|
||||
const appPath = remote.app.getPath("cache");
|
||||
|
||||
function searchKeyValues(lists, value) {
|
||||
return lists.filter((item) => {
|
||||
if (typeof item === "string") {
|
||||
return item.toLowerCase().indexOf(value.toLowerCase()) >= 0;
|
||||
}
|
||||
return item.type.toLowerCase().indexOf(value.toLowerCase()) >= 0;
|
||||
});
|
||||
}
|
||||
|
||||
const createPluginManager = (): any => {
|
||||
const baseDir = path.join(appPath, "./rubick-plugins");
|
||||
const pluginInstance = new PluginHandler({
|
||||
@@ -25,100 +17,47 @@ const createPluginManager = (): any => {
|
||||
const state: any = reactive({
|
||||
appList: [],
|
||||
plugins: [],
|
||||
options: [],
|
||||
searchValue: "",
|
||||
localPlugins: [],
|
||||
currentPlugin: {},
|
||||
});
|
||||
|
||||
const appList = ref([]);
|
||||
|
||||
const initPlugins = async () => {
|
||||
state.appList = await appSearch(nativeImage);
|
||||
appList.value = await appSearch(nativeImage);
|
||||
};
|
||||
|
||||
const addPlugin = (plugin: any) => {
|
||||
state.plugins.unshift(plugin);
|
||||
};
|
||||
|
||||
const removePlugin = (plugin: any) => {
|
||||
// todo
|
||||
};
|
||||
|
||||
const onSearch = throttle((e) => {
|
||||
const value = e.target.value;
|
||||
state.searchValue = value;
|
||||
if (!value) return;
|
||||
state.localPlugins = remote.getGlobal("LOCAL_PLUGINS").getLocalPlugins();
|
||||
let options: any = [];
|
||||
// todo 先搜索 plugin
|
||||
state.localPlugins.forEach((plugin) => {
|
||||
const feature = plugin.features;
|
||||
feature.forEach((fe) => {
|
||||
const cmds = searchKeyValues(fe.cmds, value);
|
||||
options = [
|
||||
...options,
|
||||
...cmds.map((cmd) => ({
|
||||
name: cmd,
|
||||
value: "plugin",
|
||||
icon: plugin.logo,
|
||||
desc: fe.explain,
|
||||
type: plugin.pluginType,
|
||||
click: () => {
|
||||
const pluginPath = path.resolve(
|
||||
pluginInstance.baseDir,
|
||||
"node_modules",
|
||||
plugin.name
|
||||
);
|
||||
openPlugin({
|
||||
...toRaw(plugin),
|
||||
indexPath: `file://${path.join(
|
||||
pluginPath,
|
||||
"./",
|
||||
plugin.main
|
||||
)}`,
|
||||
cmd,
|
||||
feature: fe,
|
||||
});
|
||||
const openPlugin = (plugin) => {
|
||||
if (plugin.pluginType === "ui") {
|
||||
state.currentPlugin = plugin;
|
||||
ipcRenderer.sendSync("msg-trigger", {
|
||||
type: "openPlugin",
|
||||
plugin: JSON.parse(
|
||||
JSON.stringify({
|
||||
...plugin,
|
||||
ext: {
|
||||
code: plugin.feature.code,
|
||||
type: plugin.cmd.type || "text",
|
||||
payload: null,
|
||||
},
|
||||
})),
|
||||
];
|
||||
})
|
||||
),
|
||||
});
|
||||
});
|
||||
// todo 再搜索 app
|
||||
const descMap = new Map();
|
||||
options = [
|
||||
...options,
|
||||
...state.appList
|
||||
.filter((plugin) => {
|
||||
if (!descMap.get(plugin)) {
|
||||
descMap.set(plugin, true);
|
||||
let has = false;
|
||||
plugin.keyWords.some((keyWord) => {
|
||||
if (
|
||||
keyWord
|
||||
.toLocaleUpperCase()
|
||||
.indexOf(value.toLocaleUpperCase()) >= 0
|
||||
) {
|
||||
has = keyWord;
|
||||
plugin.name = keyWord;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return has;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.map((plugin) => {
|
||||
plugin.click = () => {
|
||||
openPlugin(plugin);
|
||||
};
|
||||
return plugin;
|
||||
}),
|
||||
];
|
||||
state.options = options;
|
||||
}, 500);
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
// document.getElementById("search").value = "";
|
||||
// state.searchValue = "";
|
||||
}
|
||||
};
|
||||
|
||||
const { searchValue, onSearch } = searchManager();
|
||||
const { options } = optionsManager({
|
||||
searchValue,
|
||||
baseDir,
|
||||
appList,
|
||||
openPlugin,
|
||||
});
|
||||
// plugin operation
|
||||
const getPluginInfo = async ({ pluginName, pluginPath }) => {
|
||||
const pluginInfo = await pluginInstance.getAdapterInfo(pluginName, pluginPath);
|
||||
return {
|
||||
@@ -129,20 +68,18 @@ const createPluginManager = (): any => {
|
||||
};
|
||||
};
|
||||
|
||||
const openPlugin = (plugin) => {
|
||||
if (plugin.pluginType === "ui") {
|
||||
state.currentPlugin = plugin;
|
||||
ipcRenderer.sendSync("msg-trigger", {
|
||||
type: "openPlugin",
|
||||
plugin: JSON.parse(JSON.stringify(plugin)),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const changeSelect = (select) => {
|
||||
state.currentPlugin = select;
|
||||
};
|
||||
|
||||
const addPlugin = (plugin: any) => {
|
||||
state.plugins.unshift(plugin);
|
||||
};
|
||||
|
||||
const removePlugin = (plugin: any) => {
|
||||
// todo
|
||||
};
|
||||
|
||||
return {
|
||||
...toRefs(state),
|
||||
initPlugins,
|
||||
@@ -152,6 +89,8 @@ const createPluginManager = (): any => {
|
||||
getPluginInfo,
|
||||
openPlugin,
|
||||
changeSelect,
|
||||
options,
|
||||
searchValue,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user