This repository has been archived by the owner on May 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
main.js
119 lines (104 loc) · 2.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
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
const {app, BrowserWindow, shell, Menu, globalShortcut, screen} = require('electron')
const {spawn} = require('child_process')
const path = require('path')
const createWindow = () => {
console.log(screen.getPrimaryDisplay());
// 创建浏览器窗口
const mainWindow = new BrowserWindow({
show: false,
width: screen.getPrimaryDisplay().size.width * 0.8,
height: screen.getPrimaryDisplay().size.height * 0.8,
webPreferences: {
nodeIntegration: true,
nativeWindowOpen: true,
webviewTag: true,
},
frame: process.platform === 'darwin',
titleBarStyle: 'hidden',
})
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})
// 加载index.html文件
mainWindow.loadFile('../public/index.html')
if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.openDevTools({mode: 'bottom'})
} else {
const template = [
{
label: '链滴笔记',
submenu: [
{role: 'about'},
{type: 'separator'},
{role: 'toggledevtools'},
{type: 'separator'},
{role: 'togglefullscreen'},
{role: 'minimize'},
{role: 'close'},
{role: 'quit'},
],
},
]
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
// 当前页面链接使用浏览器打开
mainWindow.webContents.on('will-navigate', (event, url) => {
if (url.endsWith('public/index.html')) {
return
}
event.preventDefault()
shell.openExternal(url)
})
mainWindow.on('blur',() => {
globalShortcut.unregisterAll()
})
mainWindow.on('focus',() => {
globalShortcut.register('CommandOrControl+F', () => {
mainWindow.webContents.send('liandi-find-show')
})
globalShortcut.register('CommandOrControl+S', () => {
mainWindow.webContents.send('liandi-editor-save')
})
})
}
const startKernel = () => {
let fileName = 'kernel.exe'
if (process.platform === 'darwin') {
fileName = 'kernel-darwin'
} else if (process.platform === 'linux') {
fileName = 'kernel-linux'
}
let kernelPath = path.join(path.dirname(app.getAppPath()), fileName)
if (process.env.NODE_ENV === 'development') {
kernelPath = path.join('..', 'kernel', fileName)
}
spawn(kernelPath)
}
app.whenReady().then(() => {
createWindow()
startKernel()
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
app.on('will-quit', () => {
// 注销所有快捷键
globalShortcut.unregisterAll()
})
// 在编辑器内打开链接的处理
app.on('web-contents-created', (webContentsCreatedEvent, contents) => {
if (contents.getType() === 'webview') {
contents.on('new-window', (newWindowEvent, url) => {
newWindowEvent.preventDefault();
shell.openExternal(url);
});
}
});