feat: 优化

This commit is contained in:
范胜发
2022-03-11 17:22:33 +08:00
parent 9bae8badbd
commit acf83137e1
24 changed files with 453 additions and 193 deletions

View File

@ -1,35 +1,37 @@
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const url = require('url');
const { install: storeInstall } = require('./store/index');
let win = null;
ipcMain.on('size-change', (event, flag) => {
win.setOpacity(0);
const [x, y] = win.getPosition();
if (flag) {
win.setSize(500, 300, true);
win.setPosition(x, y - 300 + 75, true);
win.setBounds({
x,
y: y - 300 + 75,
width: 500,
height: 300,
});
} else {
win.setSize(120, 75, true);
win.setPosition(x, y + 300 - 75, true);
win.setBounds({
x,
y: y + 300 - 75,
width: 120,
height: 75,
});
}
setTimeout(() => {
win.setOpacity(1);
}, 300);
});
ipcMain.on('pos-change', (event, { x, y }) => {
win.setPosition(x, y, true);
});
const { getTodo, setTodo } = require('./store');
ipcMain.handle('get-todo', () => {
console.log(getTodo());
return getTodo();
});
ipcMain.on('set-todo', (e, value) => {
setTodo(value);
});
function createWindow() {
win = new BrowserWindow({
width: 120,
@ -39,13 +41,13 @@ function createWindow() {
frame: false,
transparent: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
preload: path.join(__dirname, 'preload/preload.js'),
},
});
win.loadURL(
url.format({
pathname: path.join(__dirname, './index.html'),
pathname: path.join(__dirname, 'build/index.html'),
protocol: 'file:',
slashes: true,
})
@ -57,5 +59,6 @@ function createWindow() {
}
app.whenReady().then(() => {
storeInstall();
createWindow();
});

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -1,52 +0,0 @@
{
"name": "time-cat",
"version": "0.1.0",
"private": true,
"main": "main.js",
"homepage": ".",
"dependencies": {
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.1.3",
"@testing-library/user-event": "^13.5.0",
"electron-drag": "^2.0.0",
"electron-store": "^8.0.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"electron": "electron .",
"electron:build": "electron-packager ./build TimeCat --platform=win32 --arch=x64 --out=./../out --ar --app-version=0.1.0 --electron-version=17.1.0"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"electron": "^17.1.0",
"electron-builder": "^22.14.13",
"electron-packager": "^15.4.0"
},
"build": {
"appId": "time-cat-v0.1.0"
}
}

View File

@ -4,5 +4,6 @@ contextBridge.exposeInMainWorld('electron', {
ipcRenderer: {
...ipcRenderer,
on: ipcRenderer.on,
invoke: ipcRenderer.invoke,
},
});

View File

@ -1,20 +0,0 @@
const Store = require('electron-store');
const store = new Store();
function initStore() {
if (!store.has('todo')) {
store.set('todo', []);
}
}
initStore();
module.exports = {
getTodo() {
return store.get('todo');
},
setTodo(value) {
console.log(store.path);
return store.set('todo', value);
},
};

29
public/store/index.js Normal file
View File

@ -0,0 +1,29 @@
const { ipcMain } = require('electron');
const { TodoStore } = require('./store');
const store = new TodoStore();
module.exports.install = function install() {
ipcMain.handle('get-todo', (e) => {
return store.getTodo();
});
ipcMain.handle('set-todo', (e, id, value) => {
store.setTodo(id, value);
return store.getTodo();
});
ipcMain.handle('add-todo', (e, value) => {
store.addTodo(value);
return store.getTodo();
});
ipcMain.handle('del-todo', (e, id) => {
store.delTodo(id);
return store.getTodo();
});
ipcMain.handle('clear-todo', (e, id, status) => {
store.setClear(id, status);
return store.getTodo();
});
};

102
public/store/store.js Normal file
View File

@ -0,0 +1,102 @@
const Store = require('electron-store');
const store = new Store();
const { isDef, isDate } = require('../utils/def');
class Todo {
constructor({ id, title, time, isClear, clearTime }) {
this.id = id;
this.title = title;
this.time = isDate(time) ? new Date(time).getTime() : Date.now();
this.isClear = isDef(isClear) ? isClear : false;
this.clearTime = isDate(clearTime) ? new Date(clearTime).getTime() : 0;
}
}
class TodoStore {
constructor() {
this.init();
}
init() {
if (!store.has('todo')) {
store.set('todo', []);
}
}
getTodo() {
return store.get('todo');
}
setTodo(id, value) {
const list = this.getTodo();
const i = list.findIndex((d) => d.id === id);
if (i > -1) {
list[i] = new Todo({
...value,
id,
});
store.set('todo', list);
return true;
} else {
return false;
}
}
addTodo(value) {
const list = this.getTodo();
const last = list[list.length - 1] || {};
const id = last.id ? `${last.id + 1}` : '1';
const newData = new Todo({
id,
...value,
});
list.push(newData);
store.set('todo', list);
return newData;
}
delTodo(id) {
const list = this.getTodo();
const i = list.findIndex((d) => d.id === id);
if (i > -1) {
list.splice(i, 1);
store.set('todo', list);
return true;
} else {
return false;
}
}
clearAll() {
store.set('todo', []);
return true;
}
setClear(id, status) {
const list = this.getTodo();
const i = list.findIndex((d) => d.id === id);
if (i > -1) {
list[i].isClear = isDef(status) ? status : !list[i].isClear;
if (list[i].isClear) {
list[i].clearTime = Date.now();
} else {
list[i].clearTime = 0;
}
store.set('todo', list);
return true;
} else {
return false;
}
}
}
module.exports.TodoStore = TodoStore;

12
public/utils/def.js Normal file
View File

@ -0,0 +1,12 @@
module.exports.isDef = function isDef(v) {
return v !== undefined && v !== null;
};
module.exports.isUnDef = function isUnDef(v) {
return v === undefined || v === null;
};
module.exports.isDate = function isDate(d) {
const D = new Date(d);
return !D.toString().includes('Invalid');
};