mirror of
https://github.com/rubickCenter/rubick
synced 2025-12-26 04:19:27 +08:00
✨ API 部分迁移完成
This commit is contained in:
@@ -12,9 +12,10 @@ export default () => {
|
||||
};
|
||||
|
||||
const createView = (plugin, window: BrowserWindow) => {
|
||||
const preload = commonConst.dev()
|
||||
? path.resolve(__static, `../feature/public/preload.js`)
|
||||
: path.resolve(plugin.indexPath, `../`, plugin.preload);
|
||||
const preload =
|
||||
commonConst.dev() && plugin.name === "rubick-system-feature"
|
||||
? path.resolve(__static, `../feature/public/preload.js`)
|
||||
: path.resolve(plugin.indexPath.replace("file:", ""), `../`, plugin.preload);
|
||||
|
||||
const ses = session.fromPartition("<" + plugin.name + ">");
|
||||
ses.setPreloads([`${__static}/preload.js`]);
|
||||
@@ -54,6 +55,7 @@ export default () => {
|
||||
const getView = () => view;
|
||||
|
||||
const executeHooks = (hook, data) => {
|
||||
if (!view) return;
|
||||
const evalJs = `if(window.rubick && window.rubick.hooks && typeof window.rubick.hooks.on${hook} === 'function' ) {
|
||||
try {
|
||||
window.rubick.hooks.on${hook}(${data ? JSON.stringify(data) : ""});
|
||||
|
||||
@@ -1,20 +1,30 @@
|
||||
import { BrowserWindow, ipcMain, dialog, webContents } from "electron";
|
||||
import {
|
||||
BrowserWindow,
|
||||
ipcMain,
|
||||
dialog,
|
||||
app,
|
||||
Notification,
|
||||
nativeImage,
|
||||
clipboard,
|
||||
} from "electron";
|
||||
import { runner } from "../browsers";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import { LocalDb } from "@/core";
|
||||
import plist from "plist";
|
||||
|
||||
const runnerInstance = runner();
|
||||
const dbInstance = new LocalDb(app.getPath("userData"));
|
||||
|
||||
function getWorkWebContentsBySender(sender, mainWindow) {
|
||||
const window = BrowserWindow.fromWebContents(sender);
|
||||
console.log(window);
|
||||
|
||||
if (!window) return null;
|
||||
return window.webContents;
|
||||
}
|
||||
dbInstance.init();
|
||||
|
||||
const API: any = {
|
||||
currentPlugin: null,
|
||||
DBKEY: "RUBICK_DB_DEFAULT",
|
||||
openPlugin({ plugin }, window) {
|
||||
runnerInstance.removeView(window);
|
||||
runnerInstance.init(plugin, window);
|
||||
API.currentPlugin = plugin;
|
||||
},
|
||||
removePlugin(e, window) {
|
||||
runnerInstance.removeView(window);
|
||||
@@ -42,6 +52,103 @@ const API: any = {
|
||||
sendSubInputChangeEvent({ data }) {
|
||||
runnerInstance.executeHooks("SubInputChange", data);
|
||||
},
|
||||
removeSubInput(e, window) {
|
||||
window.webContents.executeJavaScript(`window.removeSubInput()`);
|
||||
},
|
||||
setSubInputValue({ data }, window) {
|
||||
window.webContents.executeJavaScript(
|
||||
`window.setSubInputValue(${JSON.stringify({
|
||||
value: data.text,
|
||||
})})`
|
||||
);
|
||||
},
|
||||
getPath({ data }) {
|
||||
return app.getPath(data.name);
|
||||
},
|
||||
showNotification({ data: { body } }) {
|
||||
if (!Notification.isSupported()) return;
|
||||
"string" != typeof body && (body = String(body));
|
||||
const plugin = API.currentPlugin;
|
||||
if (!plugin) return;
|
||||
const notify = new Notification({
|
||||
title: plugin.pluginName,
|
||||
body,
|
||||
icon: plugin.logo,
|
||||
});
|
||||
notify.show();
|
||||
},
|
||||
copyImage: ({ data }) => {
|
||||
const image = nativeImage.createFromDataURL(data.img);
|
||||
clipboard.writeImage(image);
|
||||
},
|
||||
copyText({ data }) {
|
||||
clipboard.writeText(String(data.text));
|
||||
return true;
|
||||
},
|
||||
copyFile: ({ data }) => {
|
||||
if (data.file && fs.existsSync(data.file)) {
|
||||
clipboard.writeBuffer(
|
||||
"NSFilenamesPboardType",
|
||||
Buffer.from(plist.build([data.file]))
|
||||
);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
dbPut({ data }) {
|
||||
return dbInstance.put(API.DBKEY, data.data);
|
||||
},
|
||||
dbGet({ data }) {
|
||||
return dbInstance.get(API.DBKEY, data.id);
|
||||
},
|
||||
dbRemove({ data }) {
|
||||
return dbInstance.remove(API.DBKEY, data.doc);
|
||||
},
|
||||
dbBulkDocs({ data }) {
|
||||
return dbInstance.bulkDocs(API.DBKEY, data.docs);
|
||||
},
|
||||
dbAllDocs({ data }) {
|
||||
return dbInstance.bulkDocs(API.DBKEY, data.key);
|
||||
},
|
||||
getFeatures() {
|
||||
return API.currentPlugin.features;
|
||||
},
|
||||
setFeature({ data }, window) {
|
||||
API.currentPlugin = {
|
||||
...API.currentPlugin,
|
||||
features: (() => {
|
||||
let has = false;
|
||||
API.currentPlugin.features.some((feature) => {
|
||||
has = feature.code === data.feature.code;
|
||||
return has;
|
||||
});
|
||||
if (!has) {
|
||||
return [...API.currentPlugin.features, data.feature];
|
||||
}
|
||||
return API.currentPlugin.features;
|
||||
})(),
|
||||
};
|
||||
window.webContents.executeJavaScript(
|
||||
`window.updatePlugin(${JSON.stringify({
|
||||
currentPlugin: API.currentPlugin,
|
||||
})})`
|
||||
);
|
||||
return true;
|
||||
},
|
||||
removeFeature({ data }, window) {
|
||||
API.currentPlugin = {
|
||||
...API.currentPlugin,
|
||||
features: API.currentPlugin.features.filter(
|
||||
(feature) => feature.code !== data.code
|
||||
),
|
||||
};
|
||||
window.webContents.executeJavaScript(
|
||||
`window.updatePlugin(${JSON.stringify({
|
||||
currentPlugin: API.currentPlugin,
|
||||
})})`
|
||||
);
|
||||
return true;
|
||||
},
|
||||
};
|
||||
|
||||
export default (mainWindow: BrowserWindow) => {
|
||||
|
||||
Reference in New Issue
Block a user