From 0e3bd7fd4f469aee00341bcea2242eaa46111581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A6=82=E9=9B=A8?= Date: Wed, 18 Oct 2023 17:04:40 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20schedules=20save=20local=20?= =?UTF-8?q?path=20=F0=9F=90=9E=20fix:=20create=20schedule=20the=20form=20n?= =?UTF-8?q?ot=20reset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/App.vue | 6 ++++++ src/background.ts | 22 ++++++++++++++-------- src/composables/variable.ts | 4 ++++ src/electron/index.ts | 1 + src/electron/ipc.ts | 26 ++++++++++++++++++++++++++ src/pages/index.vue | 5 ++--- src/pages/schedule-add.vue | 12 +++++++++++- src/preload.ts | 2 ++ 9 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 src/electron/ipc.ts diff --git a/package.json b/package.json index 48c0fe8..013989a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "schedule", - "version": "0.1.11", + "version": "0.1.12", "private": true, "packageManager": "pnpm@8.6.0", "description": "A Schedule Tools, You can create a scheduled task by using the cron string", diff --git a/src/App.vue b/src/App.vue index 7c2aa3f..9d5f321 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,3 +1,9 @@ + + diff --git a/src/background.ts b/src/background.ts index 6933836..8ebd53c 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1,8 +1,18 @@ // Electron entry file -import { Notification, app, ipcMain } from 'electron' -import { createGlobalShortcut, createTray, createWindow } from './electron' +import path from 'node:path' +import { app } from 'electron' +import { + createGlobalShortcut, + createIPC, + createTray, + createWindow, +} from './electron' const windowMap: WindowMap = new Map() + +app.setAppUserModelId('Schedule Notification') +app.setPath('userData', path.join(app.getPath('userData'), 'data')) + app.whenReady().then(() => { const mainWindow = createWindow() windowMap.set('main', mainWindow) @@ -10,15 +20,11 @@ app.whenReady().then(() => { createGlobalShortcut(mainWindow) process.argv[2] ? mainWindow.loadURL(process.argv[2]) : mainWindow.loadFile('index.html') - ipcMain.handle('notification', (_event, title: string, body: string) => { - new Notification({ - title, - body, - }).show() - }) + createIPC() mainWindow.on('closed', () => { windowMap.delete('main') }) + tray.on('click', () => { mainWindow.isVisible() ? mainWindow.hide() diff --git a/src/composables/variable.ts b/src/composables/variable.ts index 882c880..90a2f8f 100644 --- a/src/composables/variable.ts +++ b/src/composables/variable.ts @@ -2,3 +2,7 @@ export const isDark = useDark() export const toggleDark = useToggle(isDark) export const schedules = ref([]) + +watchArray(schedules, (newValue) => { + window.OS_API.saveSchedule(JSON.stringify(newValue)) +}, { deep: true }) diff --git a/src/electron/index.ts b/src/electron/index.ts index c24a22c..ff9bd42 100644 --- a/src/electron/index.ts +++ b/src/electron/index.ts @@ -1,3 +1,4 @@ export * from './main' export * from './tray' export * from './globalShortcut' +export * from './ipc' diff --git a/src/electron/ipc.ts b/src/electron/ipc.ts new file mode 100644 index 0000000..fceef4b --- /dev/null +++ b/src/electron/ipc.ts @@ -0,0 +1,26 @@ +import fs from 'node:fs' +import path from 'node:path' +import { Notification, app, ipcMain } from 'electron' + +export function createIPC() { + ipcMain.handle('notification', (_event, title: string, body: string) => { + new Notification({ + title, + body, + icon: path.relative(process.cwd(), path.join('dist/256x256.ico')), + }).show() + }) + + ipcMain.handle('read-schedule', () => { + return fs.existsSync(path.join(app.getPath('userData'), 'schedule.json')) + ? JSON.parse(fs.readFileSync(path.join(app.getPath('userData'), 'schedule.json'), 'utf-8')) + : { schedules: [] } + }) + + ipcMain.handle('save-schedule', (event, list: string) => { + fs.writeFileSync( + fs.openSync(path.join(app.getPath('userData'), 'schedule.json'), 'w'), + JSON.stringify({ schedules: list }, null, 4)) + return true + }) +} diff --git a/src/pages/index.vue b/src/pages/index.vue index 16f0574..76003a4 100644 --- a/src/pages/index.vue +++ b/src/pages/index.vue @@ -71,9 +71,8 @@ function handleOpenUpdateSchedule(schedule: Schedule) { function handleSaveUpdateSchedule() { update_form.value && update_form.value.validate((valid: boolean) => { if (valid) { - const index = schedules.value.findIndex((item: Schedule) => { - return item.id === schedule_form.value.id - }) + const index = schedules.value.findIndex((item: Schedule) => item.id === schedule_form.value.id) + // clear old schedule setTimeOut task schedules.value[index].status = false diff --git a/src/pages/schedule-add.vue b/src/pages/schedule-add.vue index db7f247..ee09364 100644 --- a/src/pages/schedule-add.vue +++ b/src/pages/schedule-add.vue @@ -59,6 +59,16 @@ function handleAddSchedule() { schedules.value.push(schedule) ElMessage.success('添加成功') insertFormRef.value && insertFormRef.value.resetFields() + stepActive.value = 0 + cron.value = { + hour: [], + minute: [], + dayOfWeek: [], + dayOfMonth: [], + month: [], + second: [0], + + } } }) } @@ -239,7 +249,7 @@ function handleCloseDrawer() {