-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
526 lines (475 loc) · 14.4 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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const { exec } = require('child_process');
const sqlite3 = require('sqlite3').verbose();
const fs = require('fs');
let mainWindow;
let db;
function createWindow() {
mainWindow = new BrowserWindow({
width: 600,
height: 400,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWindow.loadFile('index.html');
}
function initDatabase() {
return new Promise((resolve, reject) => {
const dbPath = path.join(app.getPath('userData'), 'vocabulary.sqlite');
db = new sqlite3.Database(dbPath, (err) => {
if (err) {
console.error('Error opening database:', err);
reject(err);
} else {
db.get("SELECT name FROM sqlite_master WHERE type='table' AND name='vocabulary'", (err, row) => {
if (err) {
console.error('Error checking if table exists:', err);
reject(err);
} else if (!row) {
// Table doesn't exist, create it
db.run(`CREATE TABLE IF NOT EXISTS vocabulary (
id INTEGER PRIMARY KEY AUTOINCREMENT,
word TEXT UNIQUE,
created_at INTEGER,
lookup_count INTEGER DEFAULT 1,
is_favorite INTEGER DEFAULT 0
)`, (err) => {
if (err) {
console.error('Error creating table:', err);
reject(err);
} else {
console.log('Database table created');
resolve();
}
});
} else {
console.log('Database table already exists');
resolve();
}
});
}
});
});
}
app.whenReady().then(() => {
createWindow();
initDatabase().then(() => {
console.log('Database initialized');
}).catch(err => {
console.error('Failed to initialize database:', err);
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
function executeAppleScript(script) {
return new Promise((resolve, reject) => {
exec(`osascript -e '${script}'`, (error, stdout, stderr) => {
if (error) {
console.error(`AppleScript execution error: ${error}`);
reject(error);
} else if (stderr) {
console.error(`AppleScript stderr: ${stderr}`);
reject(new Error(stderr));
} else {
resolve(stdout.trim());
}
});
});
}
function generateAppleScript(word) {
return `
tell application "Arc"
activate
delay 1
tell application "System Events"
keystroke "1" using command down
delay 0.2
repeat with i from 1 to 3
keystroke (i as string) using {control down, shift down}
delay 0.5
keystroke "u" using {control down, shift down}
delay 2
keystroke "r" using command down
delay 2
keystroke "${word}"
key code 36
delay 0.2
end repeat
end tell
end tell
return "Success"
`;
}
async function lookupWordInArc(word) {
const appleScript = generateAppleScript(word);
try {
const result = await executeAppleScript(appleScript);
console.log(`Operation result: ${result}`);
return result;
} catch (error) {
console.error('Error executing AppleScript:', error);
throw error;
}
}
function addOrUpdateWord(word) {
return new Promise((resolve, reject) => {
const now = Math.floor(Date.now() / 1000); // 使用 UNIX 时间戳
db.run(`INSERT INTO vocabulary (word, created_at, lookup_count, is_favorite)
VALUES (?, ?, 1, 0)
ON CONFLICT(word) DO UPDATE SET
lookup_count = lookup_count + 1,
created_at = CASE WHEN created_at IS NULL THEN ? ELSE created_at END`,
[word, now, now], function(err) {
if (err) {
console.error('Error in addOrUpdateWord:', err);
reject(err);
} else {
resolve(this.changes);
}
});
});
}
function getVocabulary(page, itemsPerPage) {
return new Promise((resolve, reject) => {
const offset = (page - 1) * itemsPerPage;
db.all(`SELECT word, created_at, lookup_count, is_favorite FROM vocabulary
ORDER BY created_at DESC
LIMIT ? OFFSET ?`,
[itemsPerPage, offset], (err, rows) => {
if (err) {
reject(err);
} else {
db.get(`SELECT COUNT(*) as total FROM vocabulary`, (err, countResult) => {
if (err) {
reject(err);
} else {
resolve({
words: rows.map(row => ({
...row,
lookup_count: Number(row.lookup_count),
is_favorite: Boolean(row.is_favorite)
})),
total: Number(countResult.total)
});
}
});
}
});
});
}
function removeWord(word) {
return new Promise((resolve, reject) => {
db.run(`DELETE FROM vocabulary WHERE word = ?`, [word], function(err) {
if (err) {
reject(err);
} else {
resolve(this.changes);
}
});
});
}
function searchVocabulary(searchTerm, page, itemsPerPage, sortOption, showFavorites) {
return new Promise((resolve, reject) => {
const offset = (page - 1) * itemsPerPage;
let query, params;
let orderBy;
switch (sortOption) {
case 'created_desc':
orderBy = 'created_at DESC';
break;
case 'created_asc':
orderBy = 'created_at ASC';
break;
case 'alpha_asc':
orderBy = 'word ASC';
break;
case 'alpha_desc':
orderBy = 'word DESC';
break;
case 'favorites':
orderBy = 'created_at DESC';
break;
default:
orderBy = 'created_at DESC';
}
let whereClause = '';
if (searchTerm.trim() !== '') {
whereClause = 'WHERE word LIKE ?';
params = [`%${searchTerm}%`];
} else {
params = [];
}
if (showFavorites) {
whereClause = whereClause ? `${whereClause} AND is_favorite = 1` : 'WHERE is_favorite = 1';
}
query = `SELECT word, created_at, lookup_count, is_favorite FROM vocabulary
${whereClause}
ORDER BY ${orderBy}
LIMIT ? OFFSET ?`;
params.push(itemsPerPage, offset);
db.all(query, params, (err, rows) => {
if (err) {
reject(err);
} else {
let countQuery = `SELECT COUNT(*) as total FROM vocabulary ${whereClause}`;
let countParams = params.slice(0, -2); // Remove LIMIT and OFFSET
db.get(countQuery, countParams, (err, countResult) => {
if (err) {
reject(err);
} else {
resolve({
words: rows.map(row => ({
...row,
lookup_count: Number(row.lookup_count),
is_favorite: Boolean(row.is_favorite),
created_at: Number(row.created_at) * 1000 // 转换为毫秒
})),
total: Number(countResult.total)
});
}
});
}
});
});
}
function getAllWords() {
return new Promise((resolve, reject) => {
db.all(`SELECT word FROM vocabulary ORDER BY word ASC`, (err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows.map(row => row.word));
}
});
});
}
function resetDatabase() {
return new Promise((resolve, reject) => {
const dbPath = path.join(app.getPath('userData'), 'vocabulary.sqlite');
// 关闭现有的数据库连接
if (db) {
db.close((err) => {
if (err) {
console.error('Error closing database:', err);
reject(err);
return;
}
// 删除数据库文件
fs.unlink(dbPath, (err) => {
if (err && err.code !== 'ENOENT') {
console.error('Error deleting database file:', err);
reject(err);
return;
}
// 重新初始化数据库
initDatabase()
.then(() => resolve())
.catch((err) => reject(err));
});
});
} else {
// 如果数据库连接不存在,直接重新初始化
initDatabase()
.then(() => resolve())
.catch((err) => reject(err));
}
});
}
// 添加导出数据库功能
function exportDatabase() {
return new Promise((resolve, reject) => {
const dbPath = path.join(app.getPath('userData'), 'vocabulary.sqlite');
const backupPath = path.join(app.getPath('downloads'), 'vocabulary_backup.sqlite');
fs.copyFile(dbPath, backupPath, (err) => {
if (err) {
reject(err);
} else {
resolve(backupPath);
}
});
});
}
// 添加导入数据库功能
function importDatabase(filePath) {
return new Promise((resolve, reject) => {
const dbPath = path.join(app.getPath('userData'), 'vocabulary.sqlite');
// 关闭现有的数据库连接
if (db) {
db.close((err) => {
if (err) {
reject(err);
return;
}
// 复制导入的文件到应用数据目录
fs.copyFile(filePath, dbPath, (err) => {
if (err) {
reject(err);
} else {
// 重新初始化数据库连接
initDatabase()
.then(() => resolve())
.catch((err) => reject(err));
}
});
});
} else {
// 如果数据库连接不存在,直接复制文件并初始化
fs.copyFile(filePath, dbPath, (err) => {
if (err) {
reject(err);
} else {
initDatabase()
.then(() => resolve())
.catch((err) => reject(err));
}
});
}
});
}
ipcMain.on('lookup-word', async (event, word, openArc, itemsPerPage) => {
try {
// 首先将单词添加到数据库
await addOrUpdateWord(word);
console.log(`Word "${word}" added/updated in the database`);
let result = "Word added to database";
if (openArc) {
// 如果选择打开 ARC,则执行 lookup 操作
result = await lookupWordInArc(word);
console.log(`Lookup result for "${word}": ${result}`);
}
// 获取更新后的词汇列表,使用用户选择的每页数量
const vocabulary = await getVocabulary(1, itemsPerPage);
// 发送结果回渲染进程
event.reply('lookup-result', {
success: true,
message: result,
vocabulary,
openedArc: openArc
});
} catch (error) {
console.error(`Error during lookup of "${word}":`, error);
event.reply('lookup-result', { success: false, message: error.message });
}
});
ipcMain.on('get-vocabulary', async (event, page, itemsPerPage) => {
try {
const result = await getVocabulary(page, itemsPerPage);
event.reply('vocabulary', result);
} catch (error) {
event.reply('vocabulary-error', error.message);
}
});
ipcMain.on('remove-word', async (event, word) => {
try {
await removeWord(word);
event.reply('word-removed', { success: true, word: word });
} catch (error) {
console.error(`Error removing "${word}" from database:`, error);
event.reply('word-removed', { success: false, message: error.message });
}
});
ipcMain.on('search-vocabulary', async (event, searchTerm, page, itemsPerPage, sortOption, showFavorites) => {
try {
const result = await searchVocabulary(searchTerm, page, itemsPerPage, sortOption, showFavorites);
event.reply('search-result', result);
} catch (error) {
console.error('Error searching vocabulary:', error);
event.reply('search-error', error.message);
}
});
ipcMain.on('get-all-words', async (event) => {
try {
const words = await getAllWords();
event.reply('all-words', words);
} catch (error) {
console.error('Error getting all words:', error);
}
});
// 添加切换收藏状态的函数
function toggleFavorite(word) {
return new Promise((resolve, reject) => {
db.run(`UPDATE vocabulary SET is_favorite = 1 - is_favorite WHERE word = ?`,
[word], function(err) {
if (err) {
reject(err);
} else {
resolve(this.changes);
}
});
});
}
// 添加 IPC 处理程序来处理收藏换
ipcMain.on('toggle-favorite', async (event, word) => {
try {
await toggleFavorite(word);
const result = await getVocabulary(1, itemsPerPage); // 假设我们总是返回第一页
event.reply('vocabulary', result);
} catch (error) {
event.reply('toggle-favorite-error', error.message);
}
});
// 添加新的 IPC 监听器来处理重置数据库的请求
ipcMain.on('reset-database', async (event) => {
try {
await resetDatabase();
event.reply('database-reset', { success: true });
} catch (error) {
console.error('Error resetting database:', error);
event.reply('database-reset', { success: false, message: error.message });
}
});
ipcMain.on('add-word', async (event, word) => {
try {
await addOrUpdateWord(word);
console.log(`Word "${word}" added to the database`);
// 获取更新后的词汇列表
const vocabulary = await getVocabulary(1, 10000); // 假设我们想显示所有单词
// 发送结果回渲染进程
event.reply('word-added', {
success: true,
word: word,
vocabulary: vocabulary
});
} catch (error) {
console.error(`Error adding "${word}" to database:`, error);
event.reply('word-added', { success: false, message: error.message });
}
});
// 添加新的 IPC 监听器
ipcMain.on('export-database', async (event) => {
try {
const backupPath = await exportDatabase();
event.reply('database-exported', { success: true, path: backupPath });
} catch (error) {
console.error('Error exporting database:', error);
event.reply('database-exported', { success: false, message: error.message });
}
});
ipcMain.on('import-database', async (event) => {
try {
const result = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'SQLite Database', extensions: ['sqlite'] }]
});
if (!result.canceled && result.filePaths.length > 0) {
await importDatabase(result.filePaths[0]);
event.reply('database-imported', { success: true });
} else {
event.reply('database-imported', { success: false, message: 'Import cancelled' });
}
} catch (error) {
console.error('Error importing database:', error);
event.reply('database-imported', { success: false, message: error.message });
}
});