-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
main.js
1349 lines (1280 loc) · 52.6 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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SiYuan - Refactor your thinking
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const {
net,
app,
BrowserWindow,
shell,
Menu,
MenuItem,
screen,
ipcMain,
globalShortcut,
Tray,
dialog,
systemPreferences,
powerMonitor
} = require("electron");
const path = require("path");
const fs = require("fs");
const gNet = require("net");
const remote = require("@electron/remote/main");
process.noAsar = true;
const appDir = path.dirname(app.getAppPath());
const isDevEnv = process.env.NODE_ENV === "development";
const appVer = app.getVersion();
const confDir = path.join(app.getPath("home"), ".config", "siyuan");
const windowStatePath = path.join(confDir, "windowState.json");
let bootWindow;
let firstOpen = false;
let workspaces = []; // workspaceDir, id, browserWindow, tray
let kernelPort = 6806;
let resetWindowStateOnRestart = false;
remote.initialize();
if (!app.requestSingleInstanceLock()) {
app.quit();
return;
}
try {
firstOpen = !fs.existsSync(path.join(confDir, "workspace.json"));
if (!fs.existsSync(confDir)) {
fs.mkdirSync(confDir, {mode: 0o755, recursive: true});
}
} catch (e) {
console.error(e);
require("electron").dialog.showErrorBox("创建配置目录失败 Failed to create config directory", "思源需要在用户家目录下创建配置文件夹(~/.config/siyuan),请确保该路径具有写入权限。\n\nSiYuan needs to create a configuration folder (~/.config/siyuan) in the user's home directory. Please make sure that the path has write permissions.");
app.exit();
}
const windowNavigate = (currentWindow) => {
currentWindow.webContents.on("will-navigate", (event) => {
const url = event.url;
if (url.startsWith(localServer)) {
return;
}
event.preventDefault();
shell.openExternal(url);
});
};
const setProxy = (proxyURL, webContents) => {
if (proxyURL.startsWith("://")) {
console.log("network proxy [system]");
return webContents.session.setProxy({mode: "system"});
}
console.log("network proxy [" + proxyURL + "]");
return webContents.session.setProxy({proxyRules: proxyURL});
};
const hotKey2Electron = (key) => {
if (!key) {
return key;
}
let electronKey = "";
if (key.indexOf("⌘") > -1) {
electronKey += "CommandOrControl+";
}
if (key.indexOf("⇧") > -1) {
electronKey += "Shift+";
}
if (key.indexOf("⌥") > -1) {
electronKey += "Alt+";
}
return electronKey + key.replace("⌘", "").replace("⇧", "").replace("⌥", "");
};
const exitApp = (port, errorWindowId) => {
let tray;
let mainWindow;
// 关闭端口相同的所有非主窗口
BrowserWindow.getAllWindows().forEach((item) => {
try {
const currentURL = new URL(item.getURL());
if (port.toString() === currentURL.port.toString()) {
const hasMain = workspaces.find((workspaceItem) => {
if (workspaceItem.browserWindow.id === item.id) {
mainWindow = item;
return true;
}
});
if (!hasMain) {
item.destroy();
}
}
} catch (e) {
// load file is not a url
}
});
workspaces.find((item, index) => {
if (mainWindow && mainWindow.id === item.browserWindow.id) {
if (workspaces.length > 1) {
item.browserWindow.destroy();
}
workspaces.splice(index, 1);
tray = item.tray;
return true;
}
});
if (tray && ("win32" === process.platform || "linux" === process.platform)) {
tray.destroy();
}
if (workspaces.length === 0 && mainWindow) {
try {
if (resetWindowStateOnRestart) {
fs.writeFileSync(windowStatePath, "{}");
} else {
const bounds = mainWindow.getBounds();
fs.writeFileSync(windowStatePath, JSON.stringify({
isMaximized: mainWindow.isMaximized(),
fullscreen: mainWindow.isFullScreen(),
isDevToolsOpened: mainWindow.webContents.isDevToolsOpened(),
x: bounds.x,
y: bounds.y,
width: bounds.width,
height: bounds.height,
}));
}
} catch (e) {
writeLog(e);
}
if (errorWindowId) {
BrowserWindow.getAllWindows().forEach((item) => {
if (errorWindowId !== item.id) {
item.destroy();
}
});
} else {
app.exit();
}
globalShortcut.unregisterAll();
writeLog("exited ui");
}
};
const localServer = "http://127.0.0.1";
const getServer = (port = kernelPort) => {
return localServer + ":" + port;
};
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
};
const showErrorWindow = (title, content) => {
let errorHTMLPath = path.join(appDir, "app", "electron", "error.html");
if (isDevEnv) {
errorHTMLPath = path.join(appDir, "electron", "error.html");
}
const errWindow = new BrowserWindow({
width: Math.floor(screen.getPrimaryDisplay().size.width * 0.5),
height: Math.floor(screen.getPrimaryDisplay().workAreaSize.height * 0.8),
frame: false,
icon: path.join(appDir, "stage", "icon-large.png"),
webPreferences: {
nodeIntegration: true, webviewTag: true, webSecurity: false, contextIsolation: false,
},
});
errWindow.loadFile(errorHTMLPath, {
query: {
home: app.getPath("home"),
v: appVer,
title: title,
content: content,
icon: path.join(appDir, "stage", "icon-large.png"),
},
});
errWindow.show();
return errWindow.id;
};
const writeLog = (out) => {
console.log(out);
const logFile = path.join(confDir, "app.log");
let log = "";
const maxLogLines = 1024;
try {
if (fs.existsSync(logFile)) {
log = fs.readFileSync(logFile).toString();
let lines = log.split("\n");
if (maxLogLines < lines.length) {
log = lines.slice(maxLogLines / 2, maxLogLines).join("\n") + "\n";
}
}
out = out.toString();
out = new Date().toISOString().replace(/T/, " ").replace(/\..+/, "") + " " + out;
log += out + "\n";
fs.writeFileSync(logFile, log);
} catch (e) {
console.error(e);
}
};
let openAsHidden = false;
const isOpenAsHidden = function () {
return 1 === workspaces.length && openAsHidden;
};
const initMainWindow = () => {
// 恢复主窗体状态
let oldWindowState = {};
try {
oldWindowState = JSON.parse(fs.readFileSync(windowStatePath, "utf8"));
} catch (e) {
fs.writeFileSync(windowStatePath, "{}");
}
let defaultWidth;
let defaultHeight;
let workArea;
try {
defaultWidth = Math.floor(screen.getPrimaryDisplay().size.width * 0.8);
defaultHeight = Math.floor(screen.getPrimaryDisplay().workAreaSize.height * 0.8);
workArea = screen.getPrimaryDisplay().workArea;
} catch (e) {
console.error(e);
}
const windowState = Object.assign({}, {
isMaximized: false,
fullscreen: false,
isDevToolsOpened: false,
x: 0,
y: 0,
width: defaultWidth,
height: defaultHeight,
}, oldWindowState);
writeLog("windowStat [x=" + windowState.x + ", y=" + windowState.y + ", width=" + windowState.width + ", height=" + windowState.height + "], default [width=" + defaultWidth + ", height=" + defaultHeight + "], workArea [width=" + workArea.width + ", height=" + workArea.height + "]");
let resetToCenter = false;
let x = windowState.x;
let y = windowState.y;
if (workArea) {
// 窗口大于 workArea 时缩小会隐藏到左下角,这里使用最小值重置
if (windowState.width > workArea.width || windowState.height > workArea.height) { // 重启后窗口大小恢复默认问题 https://github.com/siyuan-note/siyuan/issues/7755
windowState.width = Math.min(defaultWidth, workArea.width);
windowState.height = Math.min(defaultHeight, workArea.height);
}
if (x >= workArea.width * 0.8 || y >= workArea.height * 0.8) {
resetToCenter = true;
}
}
if (x < 0 || y < 0) {
resetToCenter = true;
}
if (windowState.width < 493) {
windowState.width = 493;
}
if (windowState.height < 376) {
windowState.height = 376;
}
// 创建主窗体
const currentWindow = new BrowserWindow({
show: false,
width: windowState.width,
height: windowState.height,
minWidth: 493,
minHeight: 376,
fullscreenable: true,
fullscreen: windowState.fullscreen,
trafficLightPosition: {x: 8, y: 8},
webPreferences: {
nodeIntegration: true,
webviewTag: true,
webSecurity: false,
contextIsolation: false,
autoplayPolicy: "user-gesture-required" // 桌面端禁止自动播放多媒体 https://github.com/siyuan-note/siyuan/issues/7587
},
frame: "darwin" === process.platform,
titleBarStyle: "hidden",
icon: path.join(appDir, "stage", "icon-large.png"),
});
remote.enable(currentWindow.webContents);
resetToCenter ? currentWindow.center() : currentWindow.setPosition(x, y);
currentWindow.webContents.userAgent = "SiYuan/" + appVer + " https://b3log.org/siyuan Electron " + currentWindow.webContents.userAgent;
// set proxy
net.fetch(getServer() + "/api/system/getNetwork", {method: "POST"}).then((response) => {
return response.json();
}).then((response) => {
setProxy(`${response.data.proxy.scheme}://${response.data.proxy.host}:${response.data.proxy.port}`, currentWindow.webContents).then(() => {
// 加载主界面
currentWindow.loadURL(getServer() + "/stage/build/app/index.html?v=" + new Date().getTime());
});
});
currentWindow.webContents.session.setSpellCheckerLanguages(["en-US"]);
// 发起互联网服务请求时绕过安全策略 https://github.com/siyuan-note/siyuan/issues/5516
currentWindow.webContents.session.webRequest.onBeforeSendHeaders((details, cb) => {
if (-1 < details.url.indexOf("bili")) {
// B 站不移除 Referer https://github.com/siyuan-note/siyuan/issues/94
cb({requestHeaders: details.requestHeaders});
return;
}
for (let key in details.requestHeaders) {
if ("referer" === key.toLowerCase()) {
delete details.requestHeaders[key];
}
}
cb({requestHeaders: details.requestHeaders});
});
currentWindow.webContents.session.webRequest.onHeadersReceived((details, cb) => {
for (let key in details.responseHeaders) {
if ("x-frame-options" === key.toLowerCase()) {
delete details.responseHeaders[key];
} else if ("content-security-policy" === key.toLowerCase()) {
delete details.responseHeaders[key];
} else if ("access-control-allow-origin" === key.toLowerCase()) {
delete details.responseHeaders[key];
}
}
cb({responseHeaders: details.responseHeaders});
});
currentWindow.webContents.on("did-finish-load", () => {
let siyuanOpenURL;
if ("win32" === process.platform || "linux" === process.platform) {
siyuanOpenURL = process.argv.find((arg) => arg.startsWith("siyuan://"));
}
if (siyuanOpenURL) {
if (currentWindow.isMinimized()) {
currentWindow.restore();
}
currentWindow.show();
setTimeout(() => { // 等待界面js执行完毕
writeLog(siyuanOpenURL);
currentWindow.webContents.send("siyuan-open-url", siyuanOpenURL);
}, 2000);
}
});
if (windowState.isDevToolsOpened) {
currentWindow.webContents.openDevTools({mode: "bottom"});
}
// 主界面事件监听
currentWindow.once("ready-to-show", () => {
if (isOpenAsHidden()) {
currentWindow.minimize();
} else {
currentWindow.show();
if (windowState.isMaximized) {
currentWindow.maximize();
} else {
currentWindow.unmaximize();
}
}
if (bootWindow && !bootWindow.isDestroyed()) {
bootWindow.destroy();
}
});
// 菜单
const productName = "SiYuan";
const template = [{
label: productName, submenu: [{
label: `About ${productName}`, role: "about",
}, {type: "separator"}, {role: "services"}, {type: "separator"}, {
label: `Hide ${productName}`, role: "hide",
}, {role: "hideOthers"}, {role: "unhide"}, {type: "separator"}, {
label: `Quit ${productName}`, role: "quit",
},],
}, {
role: "editMenu", submenu: [{role: "cut"}, {role: "copy"}, {role: "paste"}, {
role: "pasteAndMatchStyle", accelerator: "CmdOrCtrl+Shift+C"
}, {role: "selectAll"},],
}, {
role: "windowMenu",
submenu: [{role: "minimize"}, {role: "zoom"}, {role: "togglefullscreen"}, {type: "separator"}, {role: "toggledevtools"}, {type: "separator"}, {role: "front"},],
},];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
// 当前页面链接使用浏览器打开
windowNavigate(currentWindow);
currentWindow.on("close", (event) => {
if (currentWindow && !currentWindow.isDestroyed()) {
currentWindow.webContents.send("siyuan-save-close", false);
}
event.preventDefault();
});
workspaces.push({
browserWindow: currentWindow,
});
};
const showWindow = (wnd) => {
if (!wnd || wnd.isDestroyed()) {
return;
}
if (wnd.isMinimized()) {
wnd.restore();
}
wnd.show();
};
const initKernel = (workspace, port, lang) => {
return new Promise(async (resolve) => {
bootWindow = new BrowserWindow({
show: false,
width: Math.floor(screen.getPrimaryDisplay().size.width / 2),
height: Math.floor(screen.getPrimaryDisplay().workAreaSize.height / 2),
frame: false,
backgroundColor: "#1e1e1e",
resizable: false,
icon: path.join(appDir, "stage", "icon-large.png"),
});
let bootIndex = path.join(appDir, "app", "electron", "boot.html");
if (isDevEnv) {
bootIndex = path.join(appDir, "electron", "boot.html");
}
bootWindow.loadFile(bootIndex, {query: {v: appVer}});
if (openAsHidden) {
bootWindow.minimize();
} else {
bootWindow.show();
}
const kernelName = "win32" === process.platform ? "SiYuan-Kernel.exe" : "SiYuan-Kernel";
const kernelPath = path.join(appDir, "kernel", kernelName);
if (!fs.existsSync(kernelPath)) {
showErrorWindow("⚠️ 内核程序丢失 Kernel program is missing", `<div>内核程序丢失,请重新安装思源,并将思源内核程序加入杀毒软件信任列表。</div><div>The kernel program is not found, please reinstall SiYuan and add SiYuan Kernel prgram into the trust list of your antivirus software.</div><div><i>${kernelPath}</i></div>`);
bootWindow.destroy();
resolve(false);
return;
}
if (!isDevEnv || workspaces.length > 0) {
if (port && "" !== port) {
kernelPort = port;
} else {
const getAvailablePort = () => {
// https://gist.github.com/mikeal/1840641
return new Promise((portResolve, portReject) => {
const server = gNet.createServer();
server.on("error", error => {
writeLog(error);
kernelPort = "";
portReject();
});
server.listen(0, () => {
kernelPort = server.address().port;
server.close(() => portResolve(kernelPort));
});
});
};
await getAvailablePort();
}
}
writeLog("got kernel port [" + kernelPort + "]");
if (!kernelPort) {
bootWindow.destroy();
resolve(false);
return;
}
const cmds = ["--port", kernelPort, "--wd", appDir];
if (isDevEnv && workspaces.length === 0) {
cmds.push("--mode", "dev");
}
if (workspace && "" !== workspace) {
cmds.push("--workspace", workspace);
}
if (port && "" !== port) {
cmds.push("--port", port);
}
if (lang && "" !== lang) {
cmds.push("--lang", lang);
}
let cmd = `ui version [${appVer}], booting kernel [${kernelPath} ${cmds.join(" ")}]`;
writeLog(cmd);
if (!isDevEnv || workspaces.length > 0) {
const cp = require("child_process");
const kernelProcess = cp.spawn(kernelPath, cmds, {
detached: false, // 桌面端内核进程不再以游离模式拉起 https://github.com/siyuan-note/siyuan/issues/6336
stdio: "ignore",
},);
const currentKernelPort = kernelPort;
writeLog("booted kernel process [pid=" + kernelProcess.pid + ", port=" + kernelPort + "]");
kernelProcess.on("close", (code) => {
writeLog(`kernel [pid=${kernelProcess.pid}, port=${currentKernelPort}] exited with code [${code}]`);
if (0 !== code) {
let errorWindowId;
switch (code) {
case 20:
errorWindowId = showErrorWindow("⚠️ 数据库被锁定 The database is locked", "<div>数据库文件正在被其他进程占用,请检查是否同时存在多个内核进程(SiYuan Kernel)服务相同的工作空间。</div><div>The database file is being occupied by other processes, please check whether there are multiple kernel processes (SiYuan Kernel) serving the same workspace at the same time.</div>");
break;
case 21:
errorWindowId = showErrorWindow("⚠️ 监听端口 " + currentKernelPort + " 失败 Failed to listen to port " + currentKernelPort, "<div>监听 " + currentKernelPort + " 端口失败,请确保程序拥有网络权限并不受防火墙和杀毒软件阻止。</div><div>Failed to listen to port " + currentKernelPort + ", please make sure the program has network permissions and is not blocked by firewalls and antivirus software.</div>");
break;
case 24: // 工作空间已被锁定,尝试切换到第一个打开的工作空间
if (workspaces && 0 < workspaces.length) {
showWindow(workspaces[0].browserWindow);
}
errorWindowId = showErrorWindow("⚠️ 工作空间已被锁定 The workspace is locked", "<div>该工作空间正在被使用,请尝试在任务管理器中结束 SiYuan-Kernel 进程或者重启操作系统后再启动思源。</div><div>The workspace is being used, please try to end the SiYuan-Kernel process in the task manager or restart the operating system and then start SiYuan.</div>");
break;
case 25:
errorWindowId = showErrorWindow("⚠️ 初始化工作空间失败 Failed to create workspace directory", "<div>初始化工作空间失败。</div><div>Failed to init workspace.</div>");
break;
case 26:
errorWindowId = showErrorWindow("🚒 已成功避免潜在的数据损坏<br>Successfully avoid potential data corruption", "<div>工作空间下的文件正在被第三方软件(比如同步盘 iCloud/OneDrive/Dropbox/Google Drive/坚果云/百度网盘/腾讯微云等)扫描读取占用,继续使用会导致数据损坏,思源内核已经安全退出。<br><br>请将工作空间移动到其他路径后再打开,停止同步盘同步工作空间。如果以上步骤无法解决问题,请参考<a href=\"https://ld246.com/article/1684586140917\" target=\"_blank\">这里</a>或者<a href=\"https://ld246.com/article/1649901726096\" target=\"_blank\">发帖</a>寻求帮助。</div><hr><div>The files in the workspace are being scanned and read by third-party software (such as sync disk iCloud/OneDrive/Dropbox/Google Drive/Nutstore/Baidu Netdisk/Tencent Weiyun, etc.), continuing to use it will cause data corruption, and the SiYuan kernel is already safe shutdown.<br><br>Move the workspace to another path and open it again, stop the sync disk to sync the workspace. If the above steps do not resolve the issue, please look for help or report bugs <a href=\"https://liuyun.io/article/1686530886208\" target=\"_blank\">here</a>.</div>");
break;
case 0:
break;
default:
errorWindowId = showErrorWindow("⚠️ 内核因未知原因退出 The kernel exited for unknown reasons", `<div>思源内核因未知原因退出 [code=${code}],请尝试重启操作系统后再启动思源。如果该问题依然发生,请检查杀毒软件是否阻止思源内核启动。</div><div>SiYuan Kernel exited for unknown reasons [code=${code}], please try to reboot your operating system and then start SiYuan again. If occurs this problem still, please check your anti-virus software whether kill the SiYuan Kernel.</div>`);
break;
}
exitApp(currentKernelPort, errorWindowId);
bootWindow.destroy();
resolve(false);
}
});
}
let apiData;
let count = 0;
writeLog("checking kernel version");
for (; ;) {
try {
const apiResult = await net.fetch(getServer() + "/api/system/version");
apiData = await apiResult.json();
bootWindow.loadURL(getServer() + "/appearance/boot/index.html");
break;
} catch (e) {
writeLog("get kernel version failed: " + e.message);
if (14 < ++count) {
writeLog("get kernel ver failed");
showErrorWindow("⚠️ 获取内核服务端口失败 Failed to get kernel serve port", "<div>获取内核服务端口失败,请确保程序拥有网络权限并不受防火墙和杀毒软件阻止。</div><div>Failed to get kernel serve port, please make sure the program has network permissions and is not blocked by firewalls and antivirus software.</div>");
bootWindow.destroy();
resolve(false);
return;
}
await sleep(200);
}
}
if (0 === apiData.code) {
writeLog("got kernel version [" + apiData.data + "]");
if (!isDevEnv && apiData.data !== appVer) {
writeLog(`kernel [${apiData.data}] is running, shutdown it now and then start kernel [${appVer}]`);
net.fetch(getServer() + "/api/system/exit", {method: "POST"});
bootWindow.destroy();
resolve(false);
} else {
let progressing = false;
while (!progressing) {
try {
const progressResult = await net.fetch(getServer() + "/api/system/bootProgress");
const progressData = await progressResult.json();
if (progressData.data.progress >= 100) {
resolve(true);
progressing = true;
} else {
await sleep(100);
}
} catch (e) {
writeLog("get boot progress failed: " + e.message);
net.fetch(getServer() + "/api/system/exit", {method: "POST"});
bootWindow.destroy();
resolve(false);
progressing = true;
}
}
}
} else {
writeLog(`get kernel version failed: ${apiData.code}, ${apiData.msg}`);
resolve(false);
}
});
};
app.setAsDefaultProtocolClient("siyuan");
app.setPath("userData", app.getPath("userData") + "-Electron"); // `~/.config` 下 Electron 相关文件夹名称改为 `SiYuan-Electron` https://github.com/siyuan-note/siyuan/issues/3349
app.commandLine.appendSwitch("disable-web-security");
app.commandLine.appendSwitch("auto-detect", "false");
app.commandLine.appendSwitch("no-proxy-server");
app.commandLine.appendSwitch("enable-features", "PlatformHEVCDecoderSupport");
// Support set Chromium command line arguments on the desktop https://github.com/siyuan-note/siyuan/issues/9696
writeLog("app is packaged [" + app.isPackaged + "], command line args [" + process.argv.join(", ") + "]");
let argStart = 1;
if (!app.isPackaged) {
argStart = 2;
}
for (let i = argStart; i < process.argv.length; i++) {
let arg = process.argv[i];
if (arg.startsWith("--workspace=") || arg.startsWith("--openAsHidden") || arg.startsWith("--port=") || arg.startsWith("siyuan://")) {
// 跳过内置参数
if (arg.startsWith("--openAsHidden")) {
openAsHidden = true;
writeLog("open as hidden");
}
continue;
}
app.commandLine.appendSwitch(arg);
writeLog("command line switch [" + arg + "]");
}
app.whenReady().then(() => {
const resetTrayMenu = (tray, lang, mainWindow) => {
const trayMenuTemplate = [{
label: mainWindow.isVisible() ? lang.hideWindow : lang.showWindow, click: () => {
showHideWindow(tray, lang, mainWindow);
},
}, {
label: lang.officialWebsite, click: () => {
shell.openExternal("https://b3log.org/siyuan/");
},
}, {
label: lang.openSource, click: () => {
shell.openExternal("https://github.com/siyuan-note/siyuan");
},
}, {
label: lang.resetWindow, type: "checkbox", click: v => {
resetWindowStateOnRestart = v.checked;
mainWindow.webContents.send("siyuan-save-close", true);
},
}, {
label: lang.quit, click: () => {
mainWindow.webContents.send("siyuan-save-close", true);
},
},];
if ("win32" === process.platform) {
// Windows 端支持窗口置顶 https://github.com/siyuan-note/siyuan/issues/6860
trayMenuTemplate.splice(1, 0, {
label: mainWindow.isAlwaysOnTop() ? lang.cancelWindowTop : lang.setWindowTop, click: () => {
if (!mainWindow.isAlwaysOnTop()) {
mainWindow.setAlwaysOnTop(true);
} else {
mainWindow.setAlwaysOnTop(false);
}
resetTrayMenu(tray, lang, mainWindow);
},
});
}
const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
tray.setContextMenu(contextMenu);
};
const hideWindow = (wnd) => {
// 通过 `Alt+M` 最小化后焦点回到先前的窗口 https://github.com/siyuan-note/siyuan/issues/7275
wnd.minimize();
wnd.hide();
};
const showHideWindow = (tray, lang, mainWindow) => {
if (!mainWindow.isVisible()) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.show();
} else {
hideWindow(mainWindow);
}
resetTrayMenu(tray, lang, mainWindow);
};
const getWindowByContentId = (id) => {
return BrowserWindow.getAllWindows().find((win) => win.webContents.id === id);
};
ipcMain.on("siyuan-context-menu", (event, langs) => {
const template = [new MenuItem({
role: "undo", label: langs.undo
}), new MenuItem({
role: "redo", label: langs.redo
}), {type: "separator"}, new MenuItem({
role: "copy", label: langs.copy
}), new MenuItem({
role: "cut", label: langs.cut
}), new MenuItem({
role: "delete", label: langs.delete
}), new MenuItem({
role: "paste", label: langs.paste
}), new MenuItem({
role: "pasteAndMatchStyle", label: langs.pasteAsPlainText
}), new MenuItem({
role: "selectAll", label: langs.selectAll
})];
const menu = Menu.buildFromTemplate(template);
menu.popup({window: BrowserWindow.fromWebContents(event.sender)});
});
ipcMain.on("siyuan-open-folder", (event, filePath) => {
shell.showItemInFolder(filePath);
});
ipcMain.on("siyuan-first-quit", () => {
app.exit();
});
ipcMain.handle("siyuan-get", (event, data) => {
if (data.cmd === "showOpenDialog") {
return dialog.showOpenDialog(data);
}
if (data.cmd === "getContentsId") {
return event.sender.id;
}
if (data.cmd === "setProxy") {
return setProxy(data.proxyURL, event.sender);
}
if (data.cmd === "showSaveDialog") {
return dialog.showSaveDialog(data);
}
if (data.cmd === "isFullScreen") {
const wnd = getWindowByContentId(event.sender.id);
if (!wnd) {
return false;
}
return wnd.isFullScreen();
}
if (data.cmd === "isMaximized") {
const wnd = getWindowByContentId(event.sender.id);
if (!wnd) {
return false;
}
return wnd.isMaximized();
}
if (data.cmd === "getMicrophone") {
return systemPreferences.getMediaAccessStatus("microphone");
}
if (data.cmd === "askMicrophone") {
return systemPreferences.askForMediaAccess("microphone");
}
if (data.cmd === "printToPDF") {
return getWindowByContentId(data.webContentsId).webContents.printToPDF(data.pdfOptions);
}
if (data.cmd === "siyuan-open-file") {
let hasMatch = false;
BrowserWindow.getAllWindows().find(item => {
if (item.webContents.id === event.sender.id) {
return;
}
const ids = decodeURIComponent(new URL(item.webContents.getURL()).hash.substring(1)).split("\u200b");
const options = JSON.parse(data.options);
if (ids.includes(options.rootID) || ids.includes(options.assetPath)) {
item.focus();
item.webContents.send("siyuan-open-file", options);
hasMatch = true;
return true;
}
});
return hasMatch;
}
});
const initEventId = [];
ipcMain.on("siyuan-event", (event) => {
if (initEventId.includes(event.sender.id)) {
return;
}
initEventId.push(event.sender.id);
const currentWindow = getWindowByContentId(event.sender.id);
if (!currentWindow) {
return;
}
currentWindow.on("focus", () => {
event.sender.send("siyuan-event", "focus");
});
currentWindow.on("blur", () => {
event.sender.send("siyuan-event", "blur");
});
if ("darwin" !== process.platform) {
currentWindow.on("maximize", () => {
event.sender.send("siyuan-event", "maximize");
});
currentWindow.on("unmaximize", () => {
event.sender.send("siyuan-event", "unmaximize");
});
}
currentWindow.on("enter-full-screen", () => {
event.sender.send("siyuan-event", "enter-full-screen");
});
currentWindow.on("leave-full-screen", () => {
event.sender.send("siyuan-event", "leave-full-screen");
});
});
ipcMain.on("siyuan-cmd", (event, data) => {
let cmd = data;
let webContentsId = event.sender.id;
if (typeof data !== "string") {
cmd = data.cmd;
if (data.webContentsId) {
webContentsId = data.webContentsId;
}
}
const currentWindow = getWindowByContentId(webContentsId);
switch (cmd) {
case "openDevTools":
event.sender.openDevTools({mode: "bottom"});
break;
case "unregisterGlobalShortcut":
if (data.accelerator) {
globalShortcut.unregister(hotKey2Electron(data.accelerator));
}
break;
case "show":
if (!currentWindow) {
return;
}
showWindow(currentWindow);
break;
case "hide":
if (!currentWindow) {
return;
}
currentWindow.hide();
break;
case "minimize":
if (!currentWindow) {
return;
}
currentWindow.minimize();
break;
case "maximize":
if (!currentWindow) {
return;
}
currentWindow.maximize();
break;
case "restore":
if (!currentWindow) {
return;
}
if (currentWindow.isFullScreen()) {
currentWindow.setFullScreen(false);
} else {
currentWindow.unmaximize();
}
break;
case "focus":
if (!currentWindow) {
return;
}
currentWindow.focus();
break;
case "setAlwaysOnTopFalse":
if (!currentWindow) {
return;
}
currentWindow.setAlwaysOnTop(false);
break;
case "setAlwaysOnTopTrue":
if (!currentWindow) {
return;
}
currentWindow.setAlwaysOnTop(true);
break;
case "clearCache":
event.sender.session.clearCache();
break;
case "redo":
event.sender.redo();
break;
case "undo":
event.sender.undo();
break;
case "destroy":
if (!currentWindow) {
return;
}
currentWindow.destroy();
break;
case "writeLog":
writeLog(data.msg);
break;
case "closeButtonBehavior":
if (!currentWindow) {
return;
}
if (currentWindow.isFullScreen()) {
currentWindow.once("leave-full-screen", () => {
currentWindow.hide();
});
currentWindow.setFullScreen(false);
} else {
currentWindow.hide();
}
break;
}
});
ipcMain.on("siyuan-config-tray", (event, data) => {
workspaces.find(item => {
if (item.browserWindow.webContents.id === event.sender.id) {
hideWindow(item.browserWindow);
if ("win32" === process.platform || "linux" === process.platform) {
resetTrayMenu(item.tray, data.languages, item.browserWindow);
}
return true;
}
});
});
ipcMain.on("siyuan-export-pdf", (event, data) => {
dialog.showOpenDialog({
title: data.title, properties: ["createDirectory", "openDirectory"],
}).then((result) => {
if (result.canceled) {
event.sender.destroy();
return;
}
data.filePaths = result.filePaths;
data.webContentsId = event.sender.id;
getWindowByContentId(data.parentWindowId).send("siyuan-export-pdf", data);
});
});
ipcMain.on("siyuan-export-newwindow", (event, data) => {
// The PDF/Word export preview window automatically adjusts according to the size of the main window https://github.com/siyuan-note/siyuan/issues/10554
const wndBounds = getWindowByContentId(event.sender.id).getBounds();
const wndScreen = screen.getDisplayNearestPoint({x: wndBounds.x, y: wndBounds.y});
const printWin = new BrowserWindow({
show: true,
width: Math.floor(wndScreen.size.width * 0.8),
height: Math.floor(wndScreen.size.height * 0.8),
resizable: true,
frame: "darwin" === process.platform,
icon: path.join(appDir, "stage", "icon-large.png"),
titleBarStyle: "hidden",
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
webviewTag: true,
webSecurity: false,
autoplayPolicy: "user-gesture-required" // 桌面端禁止自动播放多媒体 https://github.com/siyuan-note/siyuan/issues/7587
},
});
printWin.center();
printWin.webContents.userAgent = "SiYuan/" + appVer + " https://b3log.org/siyuan Electron " + printWin.webContents.userAgent;
printWin.loadURL(data);
windowNavigate(printWin);
});
ipcMain.on("siyuan-quit", (event, port) => {
exitApp(port);
});
ipcMain.on("siyuan-open-window", (event, data) => {
const mainWindow = BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0];
const mainBounds = mainWindow.getBounds();
const mainScreen = screen.getDisplayNearestPoint({x: mainBounds.x, y: mainBounds.y});
const win = new BrowserWindow({
show: true,
trafficLightPosition: {x: 8, y: 13},
width: Math.floor(data.width || mainScreen.size.width * 0.7),
height: Math.floor(data.height || mainScreen.size.height * 0.9),
minWidth: 493,
minHeight: 376,
fullscreenable: true,
frame: "darwin" === process.platform,
icon: path.join(appDir, "stage", "icon-large.png"),
titleBarStyle: "hidden",
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
webviewTag: true,
webSecurity: false,