mirror of
https://github.com/rubickCenter/rubick
synced 2025-06-13 06:54:05 +08:00
修改部分不规范的命名,优化部分可简化的代码
This commit is contained in:
parent
960b16bb4c
commit
60b1138dd8
@ -1,15 +1,16 @@
|
||||
const { BrowserWindow, protocol } = require("electron")
|
||||
const { BrowserWindow, protocol } = require("electron");
|
||||
module.exports = () => {
|
||||
let win
|
||||
let win;
|
||||
|
||||
let init = (opts) => {
|
||||
createWindow(opts)
|
||||
}
|
||||
createWindow(opts);
|
||||
};
|
||||
|
||||
let createWindow = (opts) => {
|
||||
const winURL = process.env.NODE_ENV === 'development'
|
||||
? `http://localhost:9080`
|
||||
: `file://${__dirname}/index.html`
|
||||
const winURL =
|
||||
process.env.NODE_ENV === "development"
|
||||
? `http://localhost:9080`
|
||||
: `file://${__dirname}/index.html`;
|
||||
|
||||
win = new BrowserWindow({
|
||||
height: 60,
|
||||
@ -17,7 +18,7 @@ module.exports = () => {
|
||||
resizable: true,
|
||||
width: 800,
|
||||
frame: false,
|
||||
title: '拉比克',
|
||||
title: "拉比克",
|
||||
show: false,
|
||||
skipTaskbar: true,
|
||||
webPreferences: {
|
||||
@ -26,31 +27,35 @@ module.exports = () => {
|
||||
backgroundThrottling: false,
|
||||
contextIsolation: false,
|
||||
webviewTag: true,
|
||||
nodeIntegration: true // 在网页中集成Node
|
||||
nodeIntegration: true, // 在网页中集成Node
|
||||
},
|
||||
});
|
||||
|
||||
win.loadURL(winURL);
|
||||
|
||||
protocol.interceptFileProtocol(
|
||||
"image",
|
||||
(req, callback) => {
|
||||
const url = req.url.substr(8);
|
||||
callback(decodeURI(url));
|
||||
},
|
||||
(error) => {
|
||||
if (error) {
|
||||
console.error("Failed to register protocol");
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
win.loadURL(winURL)
|
||||
|
||||
protocol.interceptFileProtocol('image', (req, callback) => {
|
||||
const url = req.url.substr(8)
|
||||
callback(decodeURI(url))
|
||||
}, (error) => {
|
||||
if (error) {
|
||||
console.error('Failed to register protocol')
|
||||
}
|
||||
})
|
||||
|
||||
win.once('ready-to-show', () => win.show())
|
||||
win.once("ready-to-show", () => win.show());
|
||||
win.on("closed", () => {
|
||||
win = undefined
|
||||
})
|
||||
}
|
||||
win = undefined;
|
||||
});
|
||||
};
|
||||
|
||||
let getWindow = () => win
|
||||
let getWindow = () => win;
|
||||
|
||||
return {
|
||||
init: init,
|
||||
getWindow: getWindow,
|
||||
}
|
||||
}
|
||||
init,
|
||||
getWindow,
|
||||
};
|
||||
};
|
||||
|
@ -1,13 +1,13 @@
|
||||
const { BrowserWindow, nativeImage } = require("electron")
|
||||
const { BrowserWindow, nativeImage } = require("electron");
|
||||
|
||||
module.exports = () => {
|
||||
let win
|
||||
let win;
|
||||
|
||||
let init = (x, y) => {
|
||||
if (win === null || win === undefined) {
|
||||
createWindow()
|
||||
createWindow();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let createWindow = () => {
|
||||
win = new BrowserWindow({
|
||||
@ -25,18 +25,18 @@ module.exports = () => {
|
||||
contextIsolation: false,
|
||||
devTools: false,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
win.loadURL(`file://${__static}/plugins/picker/index.html`)
|
||||
win.loadURL(`file://${__static}/plugins/picker/index.html`);
|
||||
win.on("closed", () => {
|
||||
win = undefined
|
||||
})
|
||||
}
|
||||
win = undefined;
|
||||
});
|
||||
};
|
||||
|
||||
let getWindow = () => win
|
||||
let getWindow = () => win;
|
||||
|
||||
return {
|
||||
init: init,
|
||||
getWindow: getWindow,
|
||||
}
|
||||
}
|
||||
init,
|
||||
getWindow,
|
||||
};
|
||||
};
|
||||
|
@ -1,22 +1,23 @@
|
||||
const { BrowserWindow } = require("electron")
|
||||
const { BrowserWindow } = require("electron");
|
||||
|
||||
module.exports = () => {
|
||||
let win
|
||||
let win;
|
||||
|
||||
let init = (opts) => {
|
||||
createWindow(opts)
|
||||
}
|
||||
createWindow(opts);
|
||||
};
|
||||
|
||||
let createWindow = (opts) => {
|
||||
const winURL = process.env.NODE_ENV === 'development'
|
||||
? `http://localhost:9080/#/plugin`
|
||||
: `${__dirname}/index.html`
|
||||
const winURL =
|
||||
process.env.NODE_ENV === "development"
|
||||
? `http://localhost:9080/#/plugin`
|
||||
: `${__dirname}/index.html`;
|
||||
win = new BrowserWindow({
|
||||
height: 600,
|
||||
useContentSize: true,
|
||||
width: 800,
|
||||
titleBarStyle: 'hiddenInset',
|
||||
title: '拉比克',
|
||||
titleBarStyle: "hiddenInset",
|
||||
title: "拉比克",
|
||||
show: false,
|
||||
webPreferences: {
|
||||
webSecurity: false,
|
||||
@ -24,26 +25,30 @@ module.exports = () => {
|
||||
backgroundThrottling: false,
|
||||
contextIsolation: false,
|
||||
webviewTag: true,
|
||||
nodeIntegration: true // 在网页中集成Node
|
||||
}
|
||||
})
|
||||
process.env.NODE_ENV === 'development' ? win.loadURL(winURL) : win.loadFile(winURL, {
|
||||
hash: `#/plugin`,
|
||||
})
|
||||
nodeIntegration: true, // 在网页中集成Node
|
||||
},
|
||||
});
|
||||
process.env.NODE_ENV === "development"
|
||||
? win.loadURL(winURL)
|
||||
: win.loadFile(winURL, {
|
||||
hash: `#/plugin`,
|
||||
});
|
||||
|
||||
win.webContents.executeJavaScript(`window.setPluginInfo(${opts})`).then(() => {
|
||||
win.show()
|
||||
})
|
||||
win.webContents
|
||||
.executeJavaScript(`window.setPluginInfo(${opts})`)
|
||||
.then(() => {
|
||||
win.show();
|
||||
});
|
||||
|
||||
win.on("closed", () => {
|
||||
win = undefined
|
||||
})
|
||||
}
|
||||
win = undefined;
|
||||
});
|
||||
};
|
||||
|
||||
let getWindow = () => win
|
||||
let getWindow = () => win;
|
||||
|
||||
return {
|
||||
init: init,
|
||||
getWindow: getWindow,
|
||||
}
|
||||
}
|
||||
init,
|
||||
getWindow,
|
||||
};
|
||||
};
|
||||
|
@ -6,14 +6,14 @@ module.exports = () => {
|
||||
let init = (mainWindow) => {
|
||||
if (win === null || win === undefined) {
|
||||
createWindow();
|
||||
ipcMain.on('superPanel-hidden', () => {
|
||||
ipcMain.on("superPanel-hidden", () => {
|
||||
win.hide();
|
||||
});
|
||||
ipcMain.on('superPanel-setSize', (e, height) => {
|
||||
ipcMain.on("superPanel-setSize", (e, height) => {
|
||||
win.setSize(250, height);
|
||||
});
|
||||
ipcMain.on('superPanel-openPlugin', (e, args) => {
|
||||
mainWindow.webContents.send('superPanel-openPlugin', args);
|
||||
ipcMain.on("superPanel-openPlugin", (e, args) => {
|
||||
mainWindow.webContents.send("superPanel-openPlugin", args);
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -39,7 +39,7 @@ module.exports = () => {
|
||||
win = undefined;
|
||||
});
|
||||
// 打包后,失焦隐藏
|
||||
win.on('blur', () => {
|
||||
win.on("blur", () => {
|
||||
win.hide();
|
||||
});
|
||||
};
|
||||
@ -47,7 +47,7 @@ module.exports = () => {
|
||||
let getWindow = () => win;
|
||||
|
||||
return {
|
||||
init: init,
|
||||
getWindow: getWindow,
|
||||
init,
|
||||
getWindow,
|
||||
};
|
||||
};
|
||||
|
@ -1,109 +1,73 @@
|
||||
import path from "path"
|
||||
import fs from 'fs'
|
||||
import { getlocalDataFile } from "./utils"
|
||||
import os from 'os'
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import { getLocalDataFile } from "./utils";
|
||||
import os from "os";
|
||||
|
||||
const configPath = path.join(getlocalDataFile(), './rubick-config.json')
|
||||
const configPath = path.join(getLocalDataFile(), "./rubick-config.json");
|
||||
|
||||
const defaultConfigForAnyPlatform = {
|
||||
version: 3,
|
||||
perf: {
|
||||
shortCut: {
|
||||
showAndHidden: "Option+R",
|
||||
separate: "Ctrl+D",
|
||||
quit: "Shift+Escape",
|
||||
},
|
||||
common: {
|
||||
start: true,
|
||||
space: true,
|
||||
},
|
||||
local: {
|
||||
search: true,
|
||||
},
|
||||
},
|
||||
superPanel: {
|
||||
baiduAPI: {
|
||||
key: "",
|
||||
appid: "",
|
||||
},
|
||||
mouseDownTime: 500,
|
||||
},
|
||||
global: [],
|
||||
};
|
||||
|
||||
let defaultConfig = {
|
||||
Darwin: {
|
||||
version: 3,
|
||||
perf: {
|
||||
shortCut: {
|
||||
showAndHidden: 'Option+R',
|
||||
separate: 'Ctrl+D',
|
||||
quit: 'Shift+Escape'
|
||||
},
|
||||
common: {
|
||||
start: true,
|
||||
space: true,
|
||||
},
|
||||
local: {
|
||||
search: true,
|
||||
}
|
||||
},
|
||||
superPanel: {
|
||||
baiduAPI: {
|
||||
key: '',
|
||||
appid: '',
|
||||
},
|
||||
mouseDownTime: 500
|
||||
},
|
||||
global: []
|
||||
...defaultConfigForAnyPlatform,
|
||||
},
|
||||
Windows_NT: {
|
||||
version: 3,
|
||||
perf: {
|
||||
shortCut: {
|
||||
showAndHidden: 'Option+R',
|
||||
separate: 'Ctrl+D',
|
||||
quit: 'Shift+Escape'
|
||||
},
|
||||
common: {
|
||||
start: true,
|
||||
space: true,
|
||||
},
|
||||
local: {
|
||||
search: true,
|
||||
}
|
||||
},
|
||||
superPanel: {
|
||||
baiduAPI: {
|
||||
key: '',
|
||||
appid: '',
|
||||
},
|
||||
mouseDownTime: 500
|
||||
},
|
||||
global: []
|
||||
...defaultConfigForAnyPlatform,
|
||||
},
|
||||
Linux: {
|
||||
version: 3,
|
||||
perf: {
|
||||
shortCut: {
|
||||
showAndHidden: 'Option+R',
|
||||
separate: 'Ctrl+D',
|
||||
quit: 'Shift+Escape'
|
||||
},
|
||||
common: {
|
||||
start: true,
|
||||
space: true,
|
||||
},
|
||||
local: {
|
||||
search: true,
|
||||
}
|
||||
},
|
||||
superPanel: {
|
||||
baiduAPI: {
|
||||
key: '',
|
||||
appid: '',
|
||||
},
|
||||
mouseDownTime: 500
|
||||
},
|
||||
global: []
|
||||
}
|
||||
}
|
||||
...defaultConfigForAnyPlatform,
|
||||
},
|
||||
};
|
||||
global.opConfig = {
|
||||
config: null,
|
||||
get() {
|
||||
const platform = os.type();
|
||||
try {
|
||||
if (!opConfig.config) {
|
||||
opConfig.config = JSON.parse(fs.readFileSync(configPath) || JSON.stringify(defaultConfig[platform]))
|
||||
opConfig.config = JSON.parse(
|
||||
fs.readFileSync(configPath) || JSON.stringify(defaultConfig[platform])
|
||||
);
|
||||
}
|
||||
// 重置
|
||||
if (!opConfig.config.version || opConfig.config.version < defaultConfig[platform].version) {
|
||||
opConfig.config = defaultConfig[platform]
|
||||
fs.writeFileSync(configPath, JSON.stringify(opConfig.config))
|
||||
if (
|
||||
!opConfig.config.version ||
|
||||
opConfig.config.version < defaultConfig[platform].version
|
||||
) {
|
||||
opConfig.config = defaultConfig[platform];
|
||||
fs.writeFileSync(configPath, JSON.stringify(opConfig.config));
|
||||
}
|
||||
return opConfig.config
|
||||
return opConfig.config;
|
||||
} catch (e) {
|
||||
opConfig.config = defaultConfig[platform]
|
||||
return opConfig.config
|
||||
opConfig.config = defaultConfig[platform];
|
||||
return opConfig.config;
|
||||
}
|
||||
},
|
||||
set(key, value) {
|
||||
opConfig.config[key] = value;
|
||||
fs.writeFileSync(configPath, JSON.stringify(opConfig.config))
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(configPath, JSON.stringify(opConfig.config));
|
||||
},
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
import fs from "fs";
|
||||
|
||||
export const getlocalDataFile = () => {
|
||||
export const getLocalDataFile = () => {
|
||||
let localDataFile = process.env.HOME;
|
||||
if (!localDataFile) {
|
||||
localDataFile = process.env.LOCALAPPDATA;
|
||||
@ -14,13 +14,13 @@ export function saveData(path, value) {
|
||||
|
||||
export function getData(path, defaultValue) {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(path, 'utf8'));
|
||||
return JSON.parse(fs.readFileSync(path, "utf8"));
|
||||
} catch (e) {
|
||||
return defaultValue || undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function throttle (func, wait, options) {
|
||||
export function throttle(func, wait, options) {
|
||||
let context, args, result;
|
||||
let timeout = null;
|
||||
let previous = 0;
|
||||
@ -54,21 +54,19 @@ export function throttle (func, wait, options) {
|
||||
}
|
||||
|
||||
export const commonConst = {
|
||||
linux: function () {
|
||||
return process.platform === 'linux'
|
||||
linux: function() {
|
||||
return process.platform === "linux";
|
||||
},
|
||||
macOS () {
|
||||
return process.platform === 'darwin';
|
||||
macOS() {
|
||||
return process.platform === "darwin";
|
||||
},
|
||||
windows () {
|
||||
return process.platform === 'win32'
|
||||
windows() {
|
||||
return process.platform === "win32";
|
||||
},
|
||||
production: function () {
|
||||
return process.env.NODE_ENV !== 'development';
|
||||
production: function() {
|
||||
return process.env.NODE_ENV !== "development";
|
||||
},
|
||||
dev: function () {
|
||||
return process.env.NODE_ENV === 'development';
|
||||
dev: function() {
|
||||
return process.env.NODE_ENV === "development";
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
@ -2,7 +2,7 @@
|
||||
<div @mousedown="drag">
|
||||
<a-layout id="components-layout">
|
||||
<div v-if="!searchType" class="rubick-select">
|
||||
<div class="tag-container" v-if="selected">
|
||||
<div v-if="selected" class="tag-container">
|
||||
<a-tag
|
||||
:key="selected.key"
|
||||
@close="closeTag"
|
||||
@ -59,7 +59,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</a-input>
|
||||
<div class="options" v-show="showOptions">
|
||||
<div v-show="showOptions" class="options">
|
||||
<a-list item-layout="horizontal" :data-source="options">
|
||||
<a-list-item
|
||||
@click="() => item.click($router)"
|
||||
@ -81,7 +81,7 @@
|
||||
</a-list>
|
||||
</div>
|
||||
</div>
|
||||
<div class="rubick-select-subMenu" v-else>
|
||||
<div v-else class="rubick-select-subMenu">
|
||||
<div>
|
||||
<img
|
||||
class="icon-tool-sub"
|
||||
|
@ -1,13 +1,21 @@
|
||||
import { WINDOW_MAX_HEIGHT, WINDOW_MIN_HEIGHT, PRE_ITEM_HEIGHT, SYSTEM_PLUGINS } from './constans';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import Store from 'electron-store';
|
||||
import downloadFile from 'download';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import { getlocalDataFile } from '../../../main/common/utils';
|
||||
import shell from 'shelljs';
|
||||
import {
|
||||
WINDOW_MAX_HEIGHT,
|
||||
WINDOW_MIN_HEIGHT,
|
||||
PRE_ITEM_HEIGHT,
|
||||
SYSTEM_PLUGINS,
|
||||
} from "./constans";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import Store from "electron-store";
|
||||
import downloadFile from "download";
|
||||
import { ipcRenderer } from "electron";
|
||||
import { getLocalDataFile } from "../../../main/common/utils";
|
||||
import shell from "shelljs";
|
||||
|
||||
const getApp = process.platform === 'win32' ? require('./win-app').getApp : require('./darwin-app').getApp;
|
||||
const getApp =
|
||||
process.platform === "win32"
|
||||
? require("./win-app").getApp
|
||||
: require("./darwin-app").getApp;
|
||||
|
||||
const store = new Store();
|
||||
|
||||
@ -17,14 +25,16 @@ const fileLists = getApp.fileLists;
|
||||
function getWindowHeight(searchList) {
|
||||
if (!searchList) return WINDOW_MAX_HEIGHT;
|
||||
if (!searchList.length) return WINDOW_MIN_HEIGHT;
|
||||
return searchList.length * PRE_ITEM_HEIGHT + WINDOW_MIN_HEIGHT + 5 > WINDOW_MAX_HEIGHT
|
||||
return searchList.length * PRE_ITEM_HEIGHT + WINDOW_MIN_HEIGHT + 5 >
|
||||
WINDOW_MAX_HEIGHT
|
||||
? WINDOW_MAX_HEIGHT
|
||||
: searchList.length * PRE_ITEM_HEIGHT + WINDOW_MIN_HEIGHT + 5;
|
||||
}
|
||||
|
||||
function searchKeyValues(lists, value) {
|
||||
return lists.filter((item) => {
|
||||
if (typeof item === 'string') return item.toLowerCase().indexOf(value.toLowerCase()) >= 0;
|
||||
if (typeof item === "string")
|
||||
return item.toLowerCase().indexOf(value.toLowerCase()) >= 0;
|
||||
return item.type.toLowerCase().indexOf(value.toLowerCase()) >= 0;
|
||||
});
|
||||
}
|
||||
@ -41,7 +51,7 @@ function existOrNot(path) {
|
||||
});
|
||||
}
|
||||
|
||||
const appPath = getlocalDataFile();
|
||||
const appPath = getLocalDataFile();
|
||||
|
||||
async function downloadZip(downloadRepoUrl, name) {
|
||||
try {
|
||||
@ -50,7 +60,7 @@ async function downloadZip(downloadRepoUrl, name) {
|
||||
const temp_dest = `${plugin_path}/${name}`;
|
||||
// 下载模板
|
||||
if (await existOrNot(temp_dest)) {
|
||||
shell.rm('-rf', temp_dest);
|
||||
shell.rm("-rf", temp_dest);
|
||||
}
|
||||
|
||||
await downloadFile(downloadRepoUrl, plugin_path, { extract: true });
|
||||
@ -63,7 +73,7 @@ async function downloadZip(downloadRepoUrl, name) {
|
||||
|
||||
const sysFile = {
|
||||
savePlugins(plugins) {
|
||||
ipcRenderer.send('optionPlugin', {
|
||||
ipcRenderer.send("optionPlugin", {
|
||||
plugins: plugins.filter((plugin) => {
|
||||
let hasOption = false;
|
||||
plugin.features.forEach((fe) => {
|
||||
@ -74,20 +84,20 @@ const sysFile = {
|
||||
});
|
||||
});
|
||||
return hasOption;
|
||||
})
|
||||
}),
|
||||
});
|
||||
store.set('user-plugins', plugins);
|
||||
store.set("user-plugins", plugins);
|
||||
},
|
||||
getUserPlugins() {
|
||||
try {
|
||||
return store.get('user-plugins');
|
||||
return store.get("user-plugins");
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
},
|
||||
removeAllPlugins() {
|
||||
store.delete('user-plugins');
|
||||
}
|
||||
store.delete("user-plugins");
|
||||
},
|
||||
};
|
||||
|
||||
function mergePlugins(plugins) {
|
||||
@ -97,10 +107,10 @@ function mergePlugins(plugins) {
|
||||
return {
|
||||
...plugin,
|
||||
status: true,
|
||||
sourceFile: '',
|
||||
type: 'system'
|
||||
sourceFile: "",
|
||||
type: "system",
|
||||
};
|
||||
})
|
||||
}),
|
||||
];
|
||||
|
||||
const target = [];
|
||||
@ -108,7 +118,7 @@ function mergePlugins(plugins) {
|
||||
result.forEach((item, i) => {
|
||||
let targetIndex = -1;
|
||||
target.forEach((tg, j) => {
|
||||
if (tg.tag === item.tag && tg.type === 'system') {
|
||||
if (tg.tag === item.tag && tg.type === "system") {
|
||||
targetIndex = j;
|
||||
}
|
||||
});
|
||||
@ -117,7 +127,7 @@ function mergePlugins(plugins) {
|
||||
}
|
||||
});
|
||||
ipcRenderer &&
|
||||
ipcRenderer.send('optionPlugin', {
|
||||
ipcRenderer.send("optionPlugin", {
|
||||
plugins: target.filter((plugin) => {
|
||||
let hasOption = false;
|
||||
plugin.features.forEach((fe) => {
|
||||
@ -128,25 +138,25 @@ function mergePlugins(plugins) {
|
||||
});
|
||||
});
|
||||
return hasOption;
|
||||
})
|
||||
}),
|
||||
});
|
||||
ipcRenderer &&
|
||||
ipcRenderer.send('pluginInit', {
|
||||
plugins: target
|
||||
});
|
||||
ipcRenderer.send("pluginInit", {
|
||||
plugins: target,
|
||||
});
|
||||
return target;
|
||||
}
|
||||
|
||||
function find(p, target = 'plugin.json') {
|
||||
function find(p, target = "plugin.json") {
|
||||
try {
|
||||
let result;
|
||||
const fileList = fs.readdirSync(p);
|
||||
for (let i = 0; i < fileList.length; i++) {
|
||||
let thisPath = p + '/' + fileList[i];
|
||||
let thisPath = p + "/" + fileList[i];
|
||||
const data = fs.statSync(thisPath);
|
||||
|
||||
if (data.isFile() && fileList[i] === target) {
|
||||
result = path.join(thisPath, '../');
|
||||
result = path.join(thisPath, "../");
|
||||
return result;
|
||||
}
|
||||
if (data.isDirectory()) {
|
||||
@ -172,4 +182,13 @@ function debounce(fn, delay) {
|
||||
};
|
||||
}
|
||||
|
||||
export { getWindowHeight, searchKeyValues, sysFile, mergePlugins, find, downloadZip, fileLists, debounce };
|
||||
export {
|
||||
getWindowHeight,
|
||||
searchKeyValues,
|
||||
sysFile,
|
||||
mergePlugins,
|
||||
find,
|
||||
downloadZip,
|
||||
fileLists,
|
||||
debounce,
|
||||
};
|
||||
|
@ -1,45 +1,56 @@
|
||||
const {getData, getlocalDataFile, saveData} = require("./utils");
|
||||
const axios = require('axios');
|
||||
const { getData, getLocalDataFile, saveData } = require("./utils");
|
||||
const axios = require("axios");
|
||||
const marked = require("marked");
|
||||
const rendererMD = new marked.Renderer();
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const path = require("path");
|
||||
const os = require("os");
|
||||
|
||||
const appPath = path.join(getlocalDataFile());
|
||||
const dbPath = path.join(appPath, './db.json');
|
||||
const appPath = path.join(getLocalDataFile());
|
||||
const dbPath = path.join(appPath, "./db.json");
|
||||
|
||||
let filePath = '';
|
||||
let filePath = "";
|
||||
function getQueryVariable(variable) {
|
||||
var query = window.location.search.substring(1);
|
||||
var vars = query.split("&");
|
||||
for (var i=0;i<vars.length;i++) {
|
||||
for (var i = 0; i < vars.length; i++) {
|
||||
var pair = vars[i].split("=");
|
||||
if(pair[0] == variable){return pair[1];}
|
||||
if (pair[0] == variable) {
|
||||
return pair[1];
|
||||
}
|
||||
}
|
||||
return(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (location.href.indexOf('targetFile') > -1) {
|
||||
filePath = decodeURIComponent(getQueryVariable('targetFile'));
|
||||
if (location.href.indexOf("targetFile") > -1) {
|
||||
filePath = decodeURIComponent(getQueryVariable("targetFile"));
|
||||
} else {
|
||||
filePath = process.platform === 'win32' ? location.pathname.replace('/', '') : location.pathname.replace('file://', '');
|
||||
filePath =
|
||||
process.platform === "win32"
|
||||
? location.pathname.replace("/", "")
|
||||
: location.pathname.replace("file://", "");
|
||||
}
|
||||
const {ipcRenderer, nativeImage, clipboard, remote, shell} = require('electron');
|
||||
const {
|
||||
ipcRenderer,
|
||||
nativeImage,
|
||||
clipboard,
|
||||
remote,
|
||||
shell,
|
||||
} = require("electron");
|
||||
|
||||
const currentWindow = remote.getCurrentWindow();
|
||||
const winId = currentWindow.id;
|
||||
const BrowserWindow = remote.BrowserWindow;
|
||||
|
||||
function convertImgToBase64(url, callback, outputFormat){
|
||||
var canvas = document.createElement('CANVAS'),
|
||||
ctx = canvas.getContext('2d'),
|
||||
img = new Image;
|
||||
img.crossOrigin = 'Anonymous';
|
||||
img.onload = function(){
|
||||
function convertImgToBase64(url, callback, outputFormat) {
|
||||
var canvas = document.createElement("CANVAS"),
|
||||
ctx = canvas.getContext("2d"),
|
||||
img = new Image();
|
||||
img.crossOrigin = "Anonymous";
|
||||
img.onload = function() {
|
||||
canvas.height = img.height;
|
||||
canvas.width = img.width;
|
||||
ctx.drawImage(img,0,0);
|
||||
var dataURL = canvas.toDataURL(outputFormat || 'image/png');
|
||||
ctx.drawImage(img, 0, 0);
|
||||
var dataURL = canvas.toDataURL(outputFormat || "image/png");
|
||||
callback.call(this, dataURL);
|
||||
canvas = null;
|
||||
};
|
||||
@ -49,58 +60,67 @@ function convertImgToBase64(url, callback, outputFormat){
|
||||
window.rubick = {
|
||||
// 事件
|
||||
onPluginEnter(cb) {
|
||||
ipcRenderer.on('onPluginEnter', (e, message) => {
|
||||
ipcRenderer.on("onPluginEnter", (e, message) => {
|
||||
const feature = message.detail;
|
||||
cb({...feature, type: message.cmd.type ? message.cmd.type : 'text', payload: message.payload})
|
||||
})
|
||||
cb({
|
||||
...feature,
|
||||
type: message.cmd.type ? message.cmd.type : "text",
|
||||
payload: message.payload,
|
||||
});
|
||||
});
|
||||
},
|
||||
onPluginReady(cb) {
|
||||
ipcRenderer.once('onPluginReady', (e, message) => {
|
||||
const feature = message.detail
|
||||
cb({...feature, type: message.cmd.type ? message.cmd.type : 'text', payload: message.payload})
|
||||
})
|
||||
ipcRenderer.once("onPluginReady", (e, message) => {
|
||||
const feature = message.detail;
|
||||
cb({
|
||||
...feature,
|
||||
type: message.cmd.type ? message.cmd.type : "text",
|
||||
payload: message.payload,
|
||||
});
|
||||
});
|
||||
},
|
||||
onPluginOut(cb) {
|
||||
ipcRenderer.once('onPluginOut', (e, message) => {
|
||||
const feature = JSON.parse(message.detail)
|
||||
cb({...feature, type: 'text'})
|
||||
})
|
||||
ipcRenderer.once("onPluginOut", (e, message) => {
|
||||
const feature = JSON.parse(message.detail);
|
||||
cb({ ...feature, type: "text" });
|
||||
});
|
||||
},
|
||||
|
||||
// 窗口交互
|
||||
hideMainWindow() {
|
||||
ipcRenderer.send('msg-trigger', {
|
||||
type: 'hideMainWindow',
|
||||
ipcRenderer.send("msg-trigger", {
|
||||
type: "hideMainWindow",
|
||||
});
|
||||
},
|
||||
showMainWindow() {
|
||||
ipcRenderer.send('msg-trigger', {
|
||||
type: 'showMainWindow',
|
||||
ipcRenderer.send("msg-trigger", {
|
||||
type: "showMainWindow",
|
||||
});
|
||||
},
|
||||
setExpendHeight(height) {
|
||||
ipcRenderer.send('msg-trigger', {
|
||||
type: 'setExpendHeight',
|
||||
ipcRenderer.send("msg-trigger", {
|
||||
type: "setExpendHeight",
|
||||
height,
|
||||
winId
|
||||
winId,
|
||||
});
|
||||
},
|
||||
setSubInput(onChange, placeHolder, isFocus) {
|
||||
ipcRenderer.sendToHost('setSubInput', {
|
||||
placeHolder, isFocus
|
||||
ipcRenderer.sendToHost("setSubInput", {
|
||||
placeHolder,
|
||||
isFocus,
|
||||
});
|
||||
ipcRenderer.on(`msg-back-setSubInput`, (e, result) => {
|
||||
onChange({text: result});
|
||||
onChange({ text: result });
|
||||
});
|
||||
},
|
||||
|
||||
removeSubInput() {
|
||||
ipcRenderer.sendToHost('removeSubInput');
|
||||
ipcRenderer.sendToHost("removeSubInput");
|
||||
},
|
||||
|
||||
setSubInputValue(text) {
|
||||
ipcRenderer.sendToHost('setSubInputValue', {
|
||||
text
|
||||
ipcRenderer.sendToHost("setSubInputValue", {
|
||||
text,
|
||||
});
|
||||
},
|
||||
|
||||
@ -109,41 +129,41 @@ window.rubick = {
|
||||
},
|
||||
|
||||
showNotification(body, clickFeatureCode) {
|
||||
const myNotification = new Notification('Rubick 通知', {
|
||||
body
|
||||
const myNotification = new Notification("Rubick 通知", {
|
||||
body,
|
||||
});
|
||||
return myNotification;
|
||||
// todo 实现 clickFeatureCode
|
||||
},
|
||||
showOpenDialog(options) {
|
||||
ipcRenderer.send('msg-trigger', {
|
||||
type: 'showOpenDialog',
|
||||
options: {...options},
|
||||
ipcRenderer.send("msg-trigger", {
|
||||
type: "showOpenDialog",
|
||||
options: { ...options },
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
ipcRenderer.once(`msg-back-showOpenDialog`, (e, result) => {
|
||||
result ? resolve(result) : reject();
|
||||
});
|
||||
})
|
||||
});
|
||||
},
|
||||
|
||||
copyImage(img) {
|
||||
convertImgToBase64(img,function(base64Image) {
|
||||
const image = nativeImage.createFromDataURL(base64Image)
|
||||
clipboard.writeImage(image)
|
||||
})
|
||||
convertImgToBase64(img, function(base64Image) {
|
||||
const image = nativeImage.createFromDataURL(base64Image);
|
||||
clipboard.writeImage(image);
|
||||
});
|
||||
},
|
||||
copyText(text) {
|
||||
clipboard.writeText(text);
|
||||
},
|
||||
db: {
|
||||
put(data) {
|
||||
data._rev = '';
|
||||
data._rev = "";
|
||||
let dbData = getData(dbPath) || [];
|
||||
let target = [];
|
||||
dbData.some((d, i) => {
|
||||
if (d._id === data._id) {
|
||||
target = [d, i]
|
||||
target = [d, i];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -159,15 +179,15 @@ window.rubick = {
|
||||
return {
|
||||
id: data._id,
|
||||
ok: true,
|
||||
rev: '',
|
||||
}
|
||||
rev: "",
|
||||
};
|
||||
},
|
||||
get(key) {
|
||||
const dbData = getData(dbPath) || [];
|
||||
return dbData.find(d => d._id === key) || '';
|
||||
return dbData.find((d) => d._id === key) || "";
|
||||
},
|
||||
remove(key) {
|
||||
key = typeof key === 'object' ? key._id : key;
|
||||
key = typeof key === "object" ? key._id : key;
|
||||
let dbData = getData(dbPath);
|
||||
let find = false;
|
||||
dbData.some((d, i) => {
|
||||
@ -183,52 +203,52 @@ window.rubick = {
|
||||
return {
|
||||
id: key,
|
||||
ok: true,
|
||||
rev: '',
|
||||
}
|
||||
rev: "",
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
id: key,
|
||||
ok: false,
|
||||
rev: '',
|
||||
}
|
||||
rev: "",
|
||||
};
|
||||
}
|
||||
},
|
||||
bulkDocs(docs) {
|
||||
const dbData = getData(dbPath);
|
||||
dbData.forEach((d, i) => {
|
||||
const result = docs.find(data => data._id === d._id);
|
||||
const result = docs.find((data) => data._id === d._id);
|
||||
if (result) {
|
||||
dbData[i] = result;
|
||||
}
|
||||
});
|
||||
saveData(dbPath, dbData);
|
||||
return docs.map(d => ({
|
||||
return docs.map((d) => ({
|
||||
id: d._id,
|
||||
success: true,
|
||||
rev: '',
|
||||
}))
|
||||
rev: "",
|
||||
}));
|
||||
},
|
||||
allDocs(key) {
|
||||
const dbData = getData(dbPath);
|
||||
if (!key) {
|
||||
return dbData;
|
||||
}
|
||||
if (typeof key === 'string') {
|
||||
return dbData.filter(d => d._id.indexOf(key) >= 0);
|
||||
if (typeof key === "string") {
|
||||
return dbData.filter((d) => d._id.indexOf(key) >= 0);
|
||||
}
|
||||
if (Array.isArray(key)) {
|
||||
return dbData.filter(d => key.indexOf(d._id) >= 0);
|
||||
return dbData.filter((d) => key.indexOf(d._id) >= 0);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
},
|
||||
},
|
||||
isDarkColors() {
|
||||
return false;
|
||||
},
|
||||
features: [],
|
||||
getFeatures() {
|
||||
ipcRenderer.sendToHost('getFeatures');
|
||||
return new Promise(resolve => {
|
||||
ipcRenderer.sendToHost("getFeatures");
|
||||
return new Promise((resolve) => {
|
||||
ipcRenderer.on(`msg-back-getFeatures`, (e, result) => {
|
||||
rubick.features = result;
|
||||
resolve(result);
|
||||
@ -236,12 +256,12 @@ window.rubick = {
|
||||
});
|
||||
},
|
||||
setFeature(feature) {
|
||||
ipcRenderer.sendToHost('setFeature', {feature});
|
||||
ipcRenderer.sendToHost("setFeature", { feature });
|
||||
},
|
||||
|
||||
removeFeature(code) {
|
||||
ipcRenderer.sendToHost('removeFeature', {code});
|
||||
return !!rubick.features.filter(fe => fe.code === code).length;
|
||||
ipcRenderer.sendToHost("removeFeature", { code });
|
||||
return !!rubick.features.filter((fe) => fe.code === code).length;
|
||||
},
|
||||
// 系统
|
||||
shellOpenExternal(url) {
|
||||
@ -249,19 +269,19 @@ window.rubick = {
|
||||
},
|
||||
|
||||
isMacOs() {
|
||||
return os.type() === 'Darwin';
|
||||
return os.type() === "Darwin";
|
||||
},
|
||||
|
||||
isWindows() {
|
||||
return os.type() === 'Windows_NT';
|
||||
return os.type() === "Windows_NT";
|
||||
},
|
||||
|
||||
isLinux() {
|
||||
return os.type() === 'Linux';
|
||||
return os.type() === "Linux";
|
||||
},
|
||||
|
||||
shellOpenPath(path) {
|
||||
shell.openPath(path)
|
||||
shell.openPath(path);
|
||||
},
|
||||
|
||||
request(config = {}) {
|
||||
@ -270,10 +290,13 @@ window.rubick = {
|
||||
withCredentials: true,
|
||||
...config,
|
||||
});
|
||||
}
|
||||
}
|
||||
const preloadPath = getQueryVariable('preloadPath') || './preload.js';
|
||||
},
|
||||
};
|
||||
const preloadPath = getQueryVariable("preloadPath") || "./preload.js";
|
||||
|
||||
require(path.join(filePath, '../', preloadPath));
|
||||
window.exports && ipcRenderer.sendToHost('templateConfig', {config: JSON.parse(JSON.stringify(window.exports))});
|
||||
require(path.join(filePath, "../", preloadPath));
|
||||
window.exports &&
|
||||
ipcRenderer.sendToHost("templateConfig", {
|
||||
config: JSON.parse(JSON.stringify(window.exports)),
|
||||
});
|
||||
window.ipcRenderer = ipcRenderer;
|
||||
|
@ -1,6 +1,6 @@
|
||||
const fs = require("fs");
|
||||
|
||||
const getlocalDataFile = () => {
|
||||
const getLocalDataFile = () => {
|
||||
let localDataFile = process.env.HOME;
|
||||
if (!localDataFile) {
|
||||
localDataFile = process.env.LOCALAPPDATA;
|
||||
@ -14,37 +14,39 @@ function saveData(path, value) {
|
||||
|
||||
function getData(path, defaultValue) {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(path, 'utf8'));
|
||||
return JSON.parse(fs.readFileSync(path, "utf8"));
|
||||
} catch (e) {
|
||||
return defaultValue || undefined;
|
||||
}
|
||||
}
|
||||
|
||||
const isArray = Array.isArray ||
|
||||
function(object){ return object instanceof Array }
|
||||
const isArray =
|
||||
Array.isArray ||
|
||||
function(object) {
|
||||
return object instanceof Array;
|
||||
};
|
||||
|
||||
function isPlainObject(obj) {
|
||||
return isObject(obj) && Object.getPrototypeOf(obj) == Object.prototype
|
||||
return isObject(obj) && Object.getPrototypeOf(obj) == Object.prototype;
|
||||
}
|
||||
|
||||
function isObject(obj) { return typeof obj == "object" }
|
||||
|
||||
function isObject(obj) {
|
||||
return typeof obj == "object";
|
||||
}
|
||||
|
||||
function extend(target, source, deep) {
|
||||
for (let key in source)
|
||||
if (deep && (isPlainObject(source[key]) || isArray(source[key]))) {
|
||||
if (isPlainObject(source[key]) && !isPlainObject(target[key]))
|
||||
target[key] = {}
|
||||
if (isArray(source[key]) && !isArray(target[key]))
|
||||
target[key] = []
|
||||
extend(target[key], source[key], deep)
|
||||
}
|
||||
else if (source[key] !== undefined) target[key] = source[key]
|
||||
target[key] = {};
|
||||
if (isArray(source[key]) && !isArray(target[key])) target[key] = [];
|
||||
extend(target[key], source[key], deep);
|
||||
} else if (source[key] !== undefined) target[key] = source[key];
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getlocalDataFile,
|
||||
getLocalDataFile,
|
||||
saveData,
|
||||
getData,
|
||||
extend
|
||||
}
|
||||
extend,
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user