-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.js
939 lines (841 loc) · 31.1 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
const {app, globalShortcut, BrowserWindow, Menu, ipcMain, shell, dialog, net, Notification} = require('electron')
const {exec} = require('child_process')
const fs = require('fs')
const os = require('os')
const url = require("url")
const path = require("path")
const {shakeDom, log, shakeDomFocus, dateFormatter, unicodeBase64Encode, unicodeBase64Decode} = require('./js/Utility')
const {
DEFAULT_BASE_URL,
IS_REQUEST_LOCAL,
IS_IN_DEVELOP,
CONFIG_FILE_PATH,
CONFIG_FILE_NAME,
DEFAULT_CONFIG,
CONFIG_DICT_MAP_FILE_NAME,
SYNC_MAX_WORD_COUNT
} = require('./js/Global')
const plist = require("plist")
const wubiApi = require("./js/wubiApi")
let mainWindow // 主窗口
let fileList = [] // 文件目录列表,用于移动词条
function createMainWindow() {
let width = IS_IN_DEVELOP ? 1800 : 1250
let height = 800
mainWindow = new BrowserWindow({
width,
height,
icon: __dirname + '/assets/appIcon/appicon.ico', // windows icon
// icon: __dirname + '/assets/appIcon/linux.png', // linux icon
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
if (IS_IN_DEVELOP) {
mainWindow.webContents.openDevTools() // 打开调试窗口
}
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, './view/index/index.html'),
protocol: "file:",
slashes: true
})
)
mainWindow.on('closed', function () {
mainWindow = null
if (configWindow) configWindow.close()
if (toolWindow) toolWindow.close()
})
mainWindow.on('show', ()=> {
console.log('main window:showed')
mainWindow.send('MainWindow:onWindowShowed') // 向 vue 发送窗口显示的事件
})
// 保存词库到文件
ipcMain.on('saveFile', (event, filename, yamlString) => {
fs.writeFile(path.join(getRimeConfigDir(), filename), yamlString, {encoding: "utf8"}, err => {
if (!err) {
console.log('saveFileSuccess')
try {
applyRime() // 部署
} catch (err) {
console.log('获取程序目录失败')
}
mainWindow.webContents.send('saveFileSuccess')
}
})
})
// 监听 window 的文件载入请求
ipcMain.on('loadInitDictFile', event => {
let config = readConfigFile()
readFileFromConfigDir(config.initFileName)
})
// 监听载入主文件内容的请求
ipcMain.on('loadDictFile', (event, filename) => {
readFileFromConfigDir(filename)
})
// 监听载入次文件内容的请求
ipcMain.on('MainWindow:LoadSecondDict', (event, filename) => {
let filePath = path.join(getRimeConfigDir(), filename)
fs.readFile(filePath, {encoding: 'utf-8'}, (err, res) => {
if (err) {
console.log(err)
} else {
mainWindow.webContents.send('setTargetDict', filename, filePath, res)
}
})
})
// 监听载入主文件内容的请求
ipcMain.on('loadMainDict', event => {
let config = readConfigFile()
let mainDictFileName = config.mainDictFileName || DEFAULT_CONFIG.mainDictFileName
fs.readFile(path.join(getRimeConfigDir(), mainDictFileName), {encoding: 'utf-8'}, (err, res) => {
if (err) {
console.log(err)
} else {
mainWindow.webContents.send('setMainDict', path.join(getRimeConfigDir(), mainDictFileName), res)
}
})
})
// 外部打开当前码表文件
ipcMain.on('openFileOutside', (event, filename) => {
shell.openPath(path.join(getRimeConfigDir(), filename)).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
})
ipcMain.on('GetFileList', event => {
mainWindow.send('FileList', fileList)
})
// config 相关,载入配置文件内容
ipcMain.on('MainWindow:RequestConfigFile', event => {
let config = readConfigFile() // 没有配置文件时,返回 false
if (config) { // 如果有配置文件
mainWindow.send('MainWindow:ResponseConfigFile', config) // 向窗口发送 config 内容
}
})
// 保存配置文件内容
ipcMain.on('saveConfigFileFromMainWindow', (event, configString) => {
writeConfigFile(configString, mainWindow)
})
// 响应所有请求 dictMap 的请求
ipcMain.on('getDictMap', event => {
let dictMapFilePath = path.join(getAppConfigDir(), CONFIG_DICT_MAP_FILE_NAME)
let dictMapFileContent = readFileFromDisk(dictMapFilePath)
if (dictMapFileContent) {
if (mainWindow) mainWindow.send('setDictMap', dictMapFileContent, CONFIG_DICT_MAP_FILE_NAME, dictMapFilePath)
if (toolWindow) toolWindow.send('setDictMap', dictMapFileContent, CONFIG_DICT_MAP_FILE_NAME, dictMapFilePath)
} else {
// 如果没有设置码表字典文件,使用默认配置目录中的码表文件作为字典文件
let rimeWubiDefaultDictFilePath = path.join(getRimeConfigDir(), 'wubi86_jidian.dict.yaml')
let originalDictFileContent = readFileFromDisk(rimeWubiDefaultDictFilePath)
if (originalDictFileContent) {
if (mainWindow) mainWindow.send('setDictMap', originalDictFileContent, CONFIG_DICT_MAP_FILE_NAME, dictMapFilePath)
if (toolWindow) toolWindow.send('setDictMap', originalDictFileContent, CONFIG_DICT_MAP_FILE_NAME, dictMapFilePath)
}
}
})
// 保存选中词条到 plist 文件
ipcMain.on('MainWindow:ExportSelectionToPlistFile', (event, wordsSelected) => {
let wordsProcessed = wordsSelected.map(item => {
return {
phrase: item.word,
shortcut: item.code
}
})
let plistContentString = plist.build(wordsProcessed)
let exportFilePath = path.join(os.homedir(), 'Desktop', 'wubi-jidian86-export.plist')
fs.writeFile(
exportFilePath,
plistContentString,
{encoding: 'utf-8'},
err => {
if (err) {
console.log(err)
} else {
// notification
if (Notification.isSupported()) {
new Notification({
title: '已成功导出文件',
subtitle: `文件路径:${exportFilePath}`, // macOS
body: `文件路径:${exportFilePath}`
}).show()
}
}
})
})
// 获取线上词库:增量同步本地词库
ipcMain.on('MainWindow:sync.get:INCREASE', (event, {fileName, userInfo}) => {
getOnlineDictContent(fileName, userInfo)
.then(res => {
if (res.data && res.data.content) {
res.data.content = Buffer.from(res.data.content, "base64").toString()
}
mainWindow.send('MainWindow:sync.get:INCREASE:SUCCESS', res)
})
.catch(err => {
console.log(err)
})
})
// 获取线上词库:覆盖本地词库
ipcMain.on('MainWindow:sync.get:OVERWRITE', (event, {fileName, userInfo}) => {
getOnlineDictContent(fileName, userInfo)
.then(res => {
if (res.data && res.data.content) {
res.data.content = Buffer.from(res.data.content, "base64").toString()
}
mainWindow.send('MainWindow:sync.get:OVERWRITE:SUCCESS', res)
})
.catch(err => {
console.log(err)
})
})
function getOnlineDictContent(dictName, userInfo) {
let config = readConfigFile() // 没有配置文件时,返回 false
return wubiApi.pullDictFileContent(userInfo,{
title: dictName,
}, config.baseURL)
}
// 保存至线上词库,如果存在覆盖它
ipcMain.on('MainWindow:sync.save', (event, {fileName, fileContentYaml, wordCount, userInfo}) => {
console.log('MainWindow:sync.save', fileName)
if (fileContentYaml.length < SYNC_MAX_WORD_COUNT) { // 限制整个文件的大小
let finalContent = Buffer.from(fileContentYaml).toString('base64')
console.log('content size original: ', fileContentYaml.length)
console.log('content size escaped: ', (escape(fileContentYaml)).length)
console.log('content size unicodeEncode: ', finalContent.length)
let config = readConfigFile() // 没有配置文件时,返回 false
console.log('config: ', config)
wubiApi
.pushDictFileContent(
userInfo,
{
title: fileName,
content: finalContent, // 为了避免一些标点干扰出现的问题,直接全部转义,
contentSize: fileContentYaml.length,
wordCount: wordCount,
}, config.baseURL)
.then(res => {
mainWindow.send('MainWindow:sync.save:SUCCESS', res.data)
})
.catch(err => {
mainWindow.send('MainWindow:sync.save:FAIL', '上传失败')
console.log(err)
})
} else {
mainWindow.send('MainWindow:sync.save:FAIL', '同步内容超过 20000 字')
}
})
// 载入文件内容
ipcMain.on('MainWindow:LoadFile', (event, fileName) => {
readFileFromConfigDir(fileName, mainWindow)
})
// 载入文件内容
ipcMain.on('MainWindow:ApplyRime', event => {
applyRime()
})
}
let toolWindow
function showToolWindow() {
let width = IS_IN_DEVELOP ? 1400 : 1000
let height = IS_IN_DEVELOP ? 600 : 600
toolWindow = new BrowserWindow({
width,
height,
icon: __dirname + '/assets/appIcon/appicon.ico', // windows icon
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
if (IS_IN_DEVELOP) {
toolWindow.webContents.openDevTools() // 打开调试窗口
}
toolWindow.loadURL(
url.format({
pathname: path.join(__dirname, 'view/tool/tool.html'),
protocol: "file:",
slashes: true
})
)
toolWindow.on('closed', function () {
let listeners = [
'ToolWindow:RequestConfigFile',
'ToolWindow:chooseDictFile',
'ToolWindow:SaveFile',
'ToolWindow:loadFileContent',
'ToolWindow:openFileOutside',
'ToolWindow:GetFileList',
'ToolWindow:LoadTargetDict'
]
listeners.forEach(item => {
ipcMain.removeAllListeners(item)
})
toolWindow = null
if (mainWindow) mainWindow.show()
})
// 保存选中词条到 plist 文件
ipcMain.on('ToolWindow:ExportSelectionToPlistFile', (event, wordsSelected) => {
let wordsProcessed = wordsSelected.map(item => {
return {
phrase: item.word,
shortcut: item.code
}
})
let plistContentString = plist.build(wordsProcessed)
let exportFilePath = path.join(os.homedir(), 'Desktop', 'wubi-jidian86-export.plist')
fs.writeFile(
exportFilePath,
plistContentString,
{encoding: 'utf-8'},
err => {
if (err) {
console.log(err)
} else {
// notification
if (Notification.isSupported()) {
new Notification({
title: '已成功导出文件',
subtitle: `文件路径:${exportFilePath}`, // macOS
body: `文件路径:${exportFilePath}`
}).show()
}
}
})
})
// 选取码表文件目录
ipcMain.on('ToolWindow:chooseDictFile', event => {
let dictFilePath = dialog.showOpenDialogSync(toolWindow, {
filters: [
{name: 'Text', extensions: ['text', 'txt', 'yaml']},
],
properties: ['openFile'] // 选择文件
})
console.log(dictFilePath)
if (dictFilePath) {
readFileFromDiskAndResponse(dictFilePath[0], toolWindow)
}
})
// 监听载入主文件内容的请求
ipcMain.on('ToolWindow:loadMainDict', event => {
let mainDictFileName = 'wubi86_jidian.dict.yaml'
fs.readFile(path.join(getRimeConfigDir(), mainDictFileName), {encoding: 'utf-8'}, (err, res) => {
if (err) {
console.log(err)
} else {
toolWindow.webContents.send('ToolWindow:setMainDict', path.join(getRimeConfigDir(), mainDictFileName), res)
}
})
})
// 保存词库到文件
ipcMain.on('ToolWindow:SaveFile', (event, filePath, fileConentString) => {
fs.writeFile(filePath, fileConentString, {encoding: "utf8"}, err => {
if (!err) {
console.log('saveFileSuccess')
// applyRime() // 部署
toolWindow.webContents.send('saveFileSuccess')
}
})
})
// 监听 window 的文件载入请求
ipcMain.on('ToolWindow:loadFileContent', (event, filePath) => {
readFileFromDiskAndResponse(filePath, toolWindow)
})
// 外部打开当前码表文件
ipcMain.on('ToolWindow:openFileOutside', (event, filename) => {
shell.openPath(path.join(getRimeConfigDir(), filename)).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
})
ipcMain.on('ToolWindow:GetFileList', event => {
toolWindow.send('ToolWindow:FileList', fileList)
})
// 监听载入次文件内容的请求
ipcMain.on('ToolWindow:LoadTargetDict', (event, filename) => {
let filePath = path.join(getRimeConfigDir(), filename)
fs.readFile(filePath, {encoding: 'utf-8'}, (err, res) => {
if (err) {
console.log(err)
} else {
toolWindow.webContents.send('ToolWindow:SetTargetDict', filename, filePath, res)
}
})
})
// config 相关
ipcMain.on('ToolWindow:RequestConfigFile', event => {
let config = readConfigFile() // 没有配置文件时,返回 false
if (config) { // 如果有配置文件
if (toolWindow) { // 如果有配置文件
toolWindow.send('ToolWindow:ResponseConfigFile', config) // 向窗口发送 config 内容
}
}
})
}
// 读取文件 从硬盘
function readFileFromDisk(filePath) {
try {
return fs.readFileSync(filePath, {encoding: 'utf-8'})
} catch (e) {
return false
}
}
// 读取文件并回馈给指定窗口
function readFileFromDiskAndResponse(filePath, responseWindow) {
let fileName = path.basename(filePath) // 获取文件名
let fileContent = readFileFromDisk(filePath)
if (fileContent) {
responseWindow.send('showFileContent', fileName, filePath, fileContent)
} else {
console.log('读取文件错误')
}
}
let configWindow
function createConfigWindow() {
let width = IS_IN_DEVELOP ? 1400 : 800
let height = IS_IN_DEVELOP ? 600 : 600
// TODO:打开配置窗口的时候,先创建配置文件夹,供后面保存配置文件和字典文件使用
// 判断 config 文件夹是否存在
let configDir = getAppConfigDir()
console.log(configDir)
if (!fs.existsSync(configDir)) {
console.log('create config dir', configDir)
fs.mkdirSync(configDir) // 创建目录
}
configWindow = new BrowserWindow({
width,
height,
icon: __dirname + '/assets/appIcon/appicon.ico', // windows icon
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
if (IS_IN_DEVELOP) {
configWindow.webContents.openDevTools() // 打开调试窗口
}
configWindow.loadURL(
url.format({
pathname: path.join(__dirname, 'view/config/config.html'),
protocol: "file:",
slashes: true
})
)
configWindow.on('closed', function () {
let listeners = [
'requestFileList',
'ConfigWindow:RequestSaveConfig',
'ConfigWindow:ChooseRimeHomeDir',
'ConfigWindow:SetDictMapFile',
]
listeners.forEach(item => {
ipcMain.removeAllListeners(item)
})
configWindow = null
if (toolWindow) toolWindow.show()
if (mainWindow) mainWindow.show()
})
// 处理登录请求
ipcMain.on('ConfigWindow:Login', (event, userInfo) => {
let requestData = {
email: userInfo.email,
password: userInfo.password,
}
let config = readConfigFile()
// 1. 新建 net.request 请求
let baseURL = config.baseURL || DEFAULT_BASE_URL // 当配置文件中没有值时,使用默认值
const request = net.request({
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
url: IS_REQUEST_LOCAL ?
'http://localhost:3000/user/login' :
`${baseURL}/user/login`
})
// 2. 通过 request.write() 方法,发送的 post 请求数据需要先进行序列化,变成纯文本的形式
request.write(JSON.stringify(requestData))
// 3. 处理返回结果
request.on('response', response => {
response.on('data', res => {
console.log(res.toString())
// res 是 Buffer 数据
// 通过 toString() 可以转为 String
// 详见: https://blog.csdn.net/KimBing/article/details/124299412
let data = JSON.parse(res.toString())
configWindow.send('ConfigWindow:ResponseLogin', data)
})
response.on('end', () => {
})
})
// 4. 记得关闭请求
request.end()
})
// 载入文件列表
ipcMain.on('requestFileList', event => {
configWindow.send('responseFileList', fileList)
})
// config 相关
ipcMain.on('ConfigWindow:RequestConfigFile', event => {
let config = readConfigFile() // 没有配置文件时,返回 false
if (config) { // 如果有配置文件
if (configWindow) { // 如果有配置文件
configWindow.send('ConfigWindow:ResponseConfigFile', config) // 向窗口发送 config 内容
}
}
})
// 保存配置文件内容
ipcMain.on('ConfigWindow:RequestSaveConfig', (event, configString) => {
writeConfigFile(configString)
})
// 选取配置文件目录
ipcMain.on('ConfigWindow:ChooseRimeHomeDir', event => {
let rimeHomeDir = dialog.showOpenDialogSync(configWindow, {
properties: ['openDirectory'] // 选择文件夹
})
if (rimeHomeDir) {
configWindow.send('ConfigWindow:ChosenRimeHomeDir', rimeHomeDir)
}
})
// 选取输入法程序目录
ipcMain.on('ConfigWindow:ChooseRimeExecDir', event => {
let rimeExecDir = dialog.showOpenDialogSync(configWindow, {
properties: ['openDirectory'] // 选择文件夹
})
if (rimeExecDir) {
configWindow.send('ConfigWindow:ChosenRimeExecDir', rimeExecDir)
}
})
// 选取编码字典文件
ipcMain.on('ConfigWindow:SetDictMapFile', event => {
// 获取文件码表文件路径,返回值为路径数组
let dictMapPathArray = dialog.showOpenDialogSync(configWindow, {
defaultPath: getRimeConfigDir(), // 默认为 Rime 配置文件目录
filters: [
{name: '码表文件', extensions: ['text', 'txt', 'yaml']},
],
properties: ['openFile'] // 选择文件夹
})
if (dictMapPathArray && dictMapPathArray.length > 0) {
let filePath = dictMapPathArray[0]
let fileName = path.basename(filePath) // 获取文件名
let fileContent = readFileFromDisk(filePath)
if (fileContent) {
configWindow.send('ConfigWindow:ShowDictMapContent', fileName, filePath, fileContent)
} else {
log('读取码表字典文件错误')
}
}
})
// 保存 DictMap 文件
ipcMain.on('ConfigWindow:SaveDictMapFile', (event, fileContentString) => {
let configPath = getAppConfigDir()
console.log(configPath)
fs.writeFile(
path.join(configPath, CONFIG_DICT_MAP_FILE_NAME),
fileContentString,
{encoding: 'utf-8'},
err => {
if (err) {
console.log(err)
} else {
configWindow.send('ConfigWindow:SaveDictMapSuccess')
}
})
})
}
// config 文件保存在 用户文件夹下 / CONFIG_FILE_PATH/CONFIG_FILE_NAME 文件中
function writeConfigFile(contentString) {
let configPath = getAppConfigDir()
fs.writeFile(
path.join(configPath, CONFIG_FILE_NAME),
contentString, {encoding: 'utf-8'},
err => {
if (err) {
console.log(err)
} else {
// 配置保存成功后,向主窗口发送配置文件内容
if (toolWindow) toolWindow.send('ToolWindow:ResponseConfigFile', JSON.parse(contentString)) // 向窗口发送 config 内容
if (mainWindow) mainWindow.send('MainWindow:ResponseConfigFile', JSON.parse(contentString)) // 向窗口发送 config 内容
}
})
}
function readConfigFile() {
let configPath = path.join(os.homedir(), CONFIG_FILE_PATH)
try { // 捕获读取文件时的错误,如果有配置文件 返回其内容,如果没有,返回 false
let result = fs.readFileSync(path.join(configPath, CONFIG_FILE_NAME), {encoding: 'utf-8'})
return JSON.parse(result)
} catch (err) {
return DEFAULT_CONFIG
}
}
app.on('ready', () => {
createMainWindow()
getDictFileList() // 读取目录中的所有码表文件
createMenu() // 创建菜单
// Register a 'CommandOrControl+i' shortcut listener.
const ret = globalShortcut.register('CommandOrControl+Shift+Alt+I', () => {
console.log('ctrl + shift + alt + i is pressed')
mainWindow.show()
})
// FOR YG777
// Register a shortcut listener.
// const retF3 = globalShortcut.register('F3', () => {
// console.log('key F3 is pressed')
// mainWindow.show()
// })
// // Register a shortcut listener.
const retF9 = globalShortcut.register('F9', () => {
console.log('key F9 is pressed')
mainWindow.show()
})
if (!ret) {
console.log('registration failed')
}
// Check whether a shortcut is registered.
console.log(globalShortcut.isRegistered('CommandOrControl+Shift+Alt+I'))
})
app.on('will-quit', () => {
// Unregister a shortcut.
globalShortcut.unregister('CommandOrControl+Shift+Alt+I')
// Unregister all shortcuts.
globalShortcut.unregisterAll()
})
app.on('window-all-closed', function () {
// if (process.platform !== 'darwin') app.quit()
app.quit()
})
app.on('activate', function () {
if (mainWindow === null) {
createMainWindow()
}
})
// 读取文件 从配置文件目录
function readFileFromConfigDir(fileName, responseWindow) {
let rimeHomeDir = getRimeConfigDir()
let filePath = path.join(rimeHomeDir, fileName)
fs.readFile(filePath, {encoding: 'utf-8'}, (err, res) => {
if (err) {
console.log(err)
} else {
if (responseWindow) {
responseWindow.send('showFileContent', fileName, filePath, res)
} else {
mainWindow.webContents.send('showFileContent', fileName, filePath, res)
}
}
})
}
// 匹配文件名,返回对应文件的名字
function getLabelNameFromFileName(fileName) {
let map = [
{name: 'iOS仓', path: 'wubi86_jidian_user_hamster.dict.yaml'},
{name: '❤ 用户词库', path: 'wubi86_jidian_user.dict.yaml'},
{name: '分类词库', path: 'wubi86_jidian_extra.dict.yaml'},
{name: '极点主表', path: 'wubi86_jidian.dict.yaml'},
{name: 'pīnyīn 词库', path: 'pinyin_simp.dict.yaml'},
{name: '英文', path: 'wubi86_jidian_english.dict.yaml'},
{name: '扩展-行政区域', path: 'wubi86_jidian_extra_district.dict.yaml'},
// 测试词库
{name: '测试 - 主表 ⛳', path: 'test_main.dict.yaml'},
{name: '测试 - 分组 ⛳', path: 'test_group.dict.yaml'},
{name: '测试 - 普通 ⛳', path: 'test.dict.yaml'},
]
let matchedPath = map.filter(item => item.path === fileName)
// 返回匹配的名字,或者返回原文件名
return matchedPath.length > 0 ? matchedPath[0].name : fileName.substring(0, fileName.indexOf('.dict.yaml'))
}
// 创建 menu
function createMenu() {
let menuStructure = [
{
label: '配置',
submenu: [
{
label: '配置',
click() {
createConfigWindow()
}
},
{
label: '刷新', // 刷新页面
click() {
refreshWindows()
}
},
{
label: '打开调试窗口',
click(menuItem, targetWindow) {
targetWindow.openDevTools()
}
},
{
label: '关闭调试窗口',
click(menuItem, targetWindow) {
targetWindow.closeDevTools()
}
},
]
},
{
label: '编辑',
role: 'editMenu'
},
{
label: '文件夹',
submenu: [
{
label: '打开 Rime 配置文件夹', click() {
shell.openPath(getRimeConfigDir())
}
},
{
label: '打开 Rime 程序文件夹', click() {
shell.openPath(getRimeExecDir())
}
},
{
label: '打开工具配置文件夹', click() {
let configDir = path.join(os.homedir(), CONFIG_FILE_PATH)
shell.openPath(configDir)
}
},
]
},
{
label: '码表处理工具',
submenu: [
{
label: '码表处理工具',
click() {
showToolWindow()
}
},
]
},
{
label: '关于',
submenu: [
{label: '最小化', role: 'minimize'},
{label: '关于', role: 'about'},
{type: 'separator'},
{label: '退出', role: 'quit'},
]
},
]
if (IS_IN_DEVELOP) {
/* menuStructure.push(
)*/
}
let menu = Menu.buildFromTemplate(menuStructure)
Menu.setApplicationMenu(menu)
}
// 刷新所有窗口内容
function refreshWindows() {
if (mainWindow) mainWindow.reload()
if (configWindow) configWindow.reload()
if (toolWindow) toolWindow.reload()
}
// 读取配置目录中的所有码表文件
function getDictFileList() {
let rimeFolderPath = getRimeConfigDir()
fs.readdir(rimeFolderPath, (err, filePaths) => {
if (err) {
console.log(err)
} else {
let filesMenu = []
// 筛选 .yaml 文件
let yamlFileList = filePaths.filter(item => item.indexOf('.dict.yaml') > 0)
// 匹配获取上面提前定义的文件名
fileList = yamlFileList.map(item => {
return {
name: getLabelNameFromFileName(item),
path: item
}
})
// 排序路径
fileList.sort((a, b) => a.name > b.name ? 1 : -1)
}
})
}
// 部署 Rime
function applyRime() {
let rimeBinDir = getRimeExecDir()
console.log(path.join(rimeBinDir, 'WeaselDeployer.exe'))
switch (os.platform()) {
case 'darwin':
// macOS
exec(`"${rimeBinDir}/Squirrel" --reload`, error => {
console.log(error)
})
break
case 'win32':
// windows
let execFilePath = path.join(rimeBinDir, 'WeaselDeployer.exe')
exec(`"${execFilePath}" /deploy`, err => {
if (err) {
console.log(err)
}
})
}
}
// 根据系统返回 rime 配置路径
function getRimeConfigDir() {
let userHome = os.homedir()
let config = readConfigFile()
if (!config.rimeHomeDir) { // 没有设置配置文件目录时
switch (os.platform()) {
case 'aix':
break
case 'darwin':
return path.join(userHome + '/Library/Rime') // macOS
case 'freebsd':
break
case 'linux':
return path.join(userHome + '/.config/ibus/rime/')
case 'openbsd':
break
case 'sunos':
break
case 'win32':
return path.join(userHome + '/AppData/Roaming/Rime') // windows
}
} else {
return config.rimeHomeDir
}
}
function getAppConfigDir() {
return path.join(os.homedir(), CONFIG_FILE_PATH)
}
// 返回 Rime 可执行文件夹
function getRimeExecDir() {
switch (os.platform()) {
case 'aix':
break
case 'darwin':
// macOS
return path.join('/Library/Input Methods/Squirrel.app', 'Contents/MacOS')
case 'freebsd':
break
case 'linux':
break
case 'openbsd':
break
case 'sunos':
break
case 'win32':
// windows
let configContent = readConfigFile()
if (configContent.rimeExecDir) { // 如果存在已配置的程序目录,使用它
return configContent.rimeExecDir
} else {
const PATH_RIME_BIN_WIN = 'C:/Program Files (x86)/Rime'
let execDirEntries = fs.readdirSync(PATH_RIME_BIN_WIN, {withFileTypes: true})
execDirEntries.sort((a,b) => a.name > b.name?1:-1)
let rimeDirEntries = execDirEntries.filter(item => item.name.includes('weasel')) // 过滤带 weasel 字符的文件夹
return path.join(PATH_RIME_BIN_WIN, rimeDirEntries[rimeDirEntries.length - 1].name)
}
}
}