-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
76 lines (60 loc) · 1.9 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path')
const fs = require('fs')
const NOTE_PATH = path.join(__dirname, './data/notes.json')
if (!fs.existsSync(NOTE_PATH)) {
fs.mkdirSync(path.join(__dirname, './data'))
fs.writeFileSync(NOTE_PATH, '[]')
}
const getNotesData = () => JSON.parse(fs.readFileSync(NOTE_PATH))
const createWindow = () => {
const win = new BrowserWindow({
width: 900,
height: 600,
frame: false,
transparent: true,
show: false,
resizable: false,
maximizable: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
}
})
win.loadURL('http://localhost:5173/')
// win.loadFile(path.join(__dirname, './dist/index.html'))
win.on('ready-to-show', () => {
win.show()
})
ipcMain.handle('get-notes-data', () => getNotesData())
ipcMain.on('insert-note', (event, data) => {
let notesData = getNotesData()
notesData.unshift(data)
fs.writeFileSync(NOTE_PATH, JSON.stringify(notesData))
})
ipcMain.on('update-note', (event, index, data) => {
let notesData = getNotesData()
notesData[index] = data
fs.writeFileSync(NOTE_PATH, JSON.stringify(notesData))
})
ipcMain.on('delete-note', (event, index) => {
let notesData = getNotesData()
notesData.splice(index, 1)
fs.writeFileSync(NOTE_PATH, JSON.stringify(notesData))
})
ipcMain.on('window-close', () => {
win.close()
})
ipcMain.on('log', (event, text) => {
console.log(text)
})
// win.webContents.openDevTools()
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})