ref: 项目基础开发

This commit is contained in:
muwoo
2021-06-08 10:06:40 +08:00
parent d9c0f5ed7b
commit 02d9c30bc9
13 changed files with 483 additions and 132 deletions

View File

@@ -1,9 +1,16 @@
import {app} from 'electron';
import {getlocalDataFile, saveData, getData} from './common/utils';
import path from "path";
const appPath = path.join(getlocalDataFile());
const dbPath = path.join(appPath, './db.json');
export default {
getPath(arg) {
return app.getPath(arg.name);
},
hideMainWindow(arg, mainWindow) {
console.log(111, mainWindow)
mainWindow.hide();
},
showMainWindow(arg, mainWindow) {
@@ -13,6 +20,87 @@ export default {
return arg
},
setExpendHeight({height}, mainWindow) {
console.log(height);
mainWindow.setSize(788, height);
},
db: {
put({data}) {
data._rev = '';
let dbData = getData(dbPath) || [];
let target = [];
console.log(data, dbData);
dbData.some((d, i) => {
if (d._id === data._id) {
target = [d, i]
return true;
}
return false;
});
// 更新
if (target[0]) {
dbData[target[1]] = data;
} else {
dbData.push(data);
}
saveData(dbPath, dbData);
return {
id: data.id,
ok: true,
rev: '',
}
},
get({key}) {
const dbData = getData(dbPath) || [];
return dbData.find(d => d._id === key) || {};
},
remove({key}) {
key = typeof key === 'object' ? key.id : key;
let dbData = getData(dbPath);
let find = false;
dbData.some((d, i) => {
if (d._id === key) {
dbData.splice(i, 1);
find = true;
return true;
}
return false;
});
if (find) {
saveData(dbPath, dbData);
return {
id: key,
ok: true,
rev: '',
}
} else {
return {
id: key,
ok: false,
rev: '',
}
}
},
bulkDocs({docs}) {
const dbData = getData(dbPath);
dbData.forEach((d, i) => {
const result = docs.find(data => data._id === d._id);
if (result) {
dbData[i] = result;
}
});
saveData(dbPath, dbData);
return docs.map(d => ({
id: d.id,
success: true,
rev: '',
}))
},
allDocs({key}) {
const dbData = getData(dbPath);
const result = dbData.filter(d => d._id === key);
return result;
}
}
}

View File

@@ -1,4 +1,5 @@
import {globalShortcut, ipcMain} from 'electron';
import {globalShortcut, ipcMain, BrowserWindow} from 'electron';
import {stringify} from 'query-string';
import Api from './api';
export default function init(mainWindow) {
@@ -15,9 +16,41 @@ export default function init(mainWindow) {
});
ipcMain.on('msg-trigger', async (event, arg) => {
const data = Api[arg.type](arg, mainWindow);
const window = arg.winId ? BrowserWindow.fromId(arg.winId) : mainWindow
const operators = arg.type.split('.');
let fn = Api;
operators.forEach((op) => {
fn = fn[op];
});
const data = fn(arg, window);
event.sender.send(`msg-back-${arg.type}`, data);
});
ipcMain.on('new-window', (event, arg) => {
const opts = {
...arg,
searchType: 'subWindow',
}
const winURL = process.env.NODE_ENV === 'development'
? `http://localhost:9080/#/plugin?${stringify(opts)}`
: `file://${__dirname}/index.html/#/plugin?${stringify(opts)}`
const win = new BrowserWindow({
height: 600,
useContentSize: true,
width: 788,
titleBarStyle: 'hiddenInset',
title: '拉比克',
webPreferences: {
webSecurity: false,
enableRemoteModule: true,
backgroundThrottling: false,
webviewTag: true,
nodeIntegration: true // 在网页中集成Node
}
});
win.loadURL(winURL)
win.once('ready-to-show', () => win.show());
})
}

21
src/main/common/utils.js Normal file
View File

@@ -0,0 +1,21 @@
import fs from "fs";
export const getlocalDataFile = () => {
let localDataFile = process.env.HOME;
if (!localDataFile) {
localDataFile = process.env.LOCALAPPDATA;
}
return localDataFile;
};
export function saveData(path, value) {
fs.writeFileSync(path, JSON.stringify(value));
}
export function getData(path, defaultValue) {
try {
return JSON.parse(fs.readFileSync(path, 'utf8'));
} catch (e) {
return defaultValue || undefined;
}
}

View File

@@ -1,6 +1,5 @@
import { app, BrowserWindow } from 'electron'
import { app, BrowserWindow, protocol } from 'electron'
import '../renderer/store'
import eventTracker from '../common/event-tracker';
import init from './common';
import createTray from './tray';
/**
@@ -27,6 +26,7 @@ function createWindow () {
frame: false,
title: '拉比克',
webPreferences: {
webSecurity: false,
enableRemoteModule: true,
backgroundThrottling: false,
webviewTag: true,
@@ -39,8 +39,15 @@ function createWindow () {
mainWindow.on('closed', () => {
mainWindow = null
});
protocol.interceptFileProtocol('file', (req, callback) => {
const url = req.url.substr(8);
callback(decodeURI(url));
}, (error) => {
if (error) {
console.error('Failed to register protocol');
}
});
init(mainWindow);
eventTracker();
}
app.on('ready', () => {