forked from luzexi/xls2lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
123 lines (113 loc) · 3.39 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const {
app,
BrowserWindow,
Menu,
nativeTheme,
dialog,
shell
} = require('electron');
const path = require('path');
const fs = require('fs');
const ipc = require('electron').ipcMain;
const {
convertToLua
} = require('./src/xls2lua')
let win;
let logWin;
function createWindow() {
win = new BrowserWindow({
width: 640,
height: 540,
resizable: false,
icon: './resources/Icon.png',
frame: false,
transparent: true,
hasShadow: true,
webPreferences: {
preload: path.join(__dirname, 'src', 'preload.js')
}
})
nativeTheme.themeSource = 'dark'
win.loadFile('./views/home.html')
Menu.setApplicationMenu(null)
}
function getKeyByName(data, name) {
for (let i = 0; i < data.length; i++) {
if (data[i].name == name) {
return i
}
}
return null
}
function writeConfigJson(data) {
const oldData = JSON.parse(fs.readFileSync(path.join(__dirname, '.ava-x2l-config.json')))
const existsKey = getKeyByName(oldData['projects'], data.name)
if (existsKey) {
oldData['projects'][existsKey] = data
} else {
if (oldData['projects'].length >= 5) {
oldData['projects'].shift()
}
oldData['projects'].unshift(data)
}
let newdata = JSON.stringify(oldData)
fs.writeFileSync(path.join(__dirname, '.ava-x2l-config.json'), newdata, 'utf8')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function() {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// ********* 主窗口 ******************
ipc.on('closeMain', () => {
win.close()
})
ipc.on('minimizeMain', () => {
win.minimize()
})
ipc.on('openUrl', (evetn, url) => {
shell.openExternal(url)
})
ipc.on('closeLog', () => {
logWin.close()
})
ipc.on('sendData', async (event, data, key) => {
const oldData = JSON.parse(fs.readFileSync(path.join(__dirname, '.ava-x2l-config.json')))
if (key && getKeyByName(oldData['projects'], data.name) != null && getKeyByName(oldData['projects'], data.name) != key) { //如果存在项目名,且为更改某一项目卡
dialog.showErrorBox('项目名重复', '已经有重复的项目名了')
return
} else if (key) { //不存在项目,为更改某一项目
oldData['projects'][key] = data
let newdata = JSON.stringify(oldData)
fs.writeFileSync(path.join(__dirname, '.ava-x2l-config.json'), newdata, 'utf8')
} else { //不存在项目名,为新建
writeConfigJson(data)
}
logWin = new BrowserWindow({
width: 552,
height: 488,
transparent: true,
parent: win,
modal: true,
resizable: false,
frame: false,
backgroundColor: '#00000000',
hasShadow: true,
webPreferences: {
preload: path.join(__dirname, 'src', 'preLog.js')
}
})
await logWin.loadFile('./views/log.html')
let log = await convertToLua(data['input-folder'], data['output-folder'], data['output-format'])
logWin.webContents.send('showLog', log.error_messages, log.message)
win.webContents.send('backToHome')
})
ipc.on('folderSelect', (event, id) => {
let folderPath = dialog.showOpenDialogSync({
properties: ['openDirectory']
})
if (folderPath) {
win.webContents.send('setFolder', folderPath[0], id)
}
})