Skip to content

Commit 00fe7bf

Browse files
committed
#9032
1 parent fe4fe1a commit 00fe7bf

File tree

5 files changed

+102
-74
lines changed

5 files changed

+102
-74
lines changed

app/electron/main.js

+58-24
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ try {
4949
app.exit();
5050
}
5151

52+
const hotKey2Electron = (key) => {
53+
console.log(key)
54+
if (!key) {
55+
return key;
56+
}
57+
let electronKey = "";
58+
if (key.indexOf("⌘") > -1) {
59+
electronKey += "CommandOrControl+";
60+
}
61+
if (key.indexOf("⇧") > -1) {
62+
electronKey += "Shift+";
63+
}
64+
if (key.indexOf("⌥") > -1) {
65+
electronKey += "Alt+";
66+
}
67+
return electronKey + key.substr(key.length - 1);
68+
};
69+
5270
const exitApp = (port, errorWindowId) => {
5371
let tray;
5472
let mainWindow;
@@ -756,36 +774,52 @@ app.whenReady().then(() => {
756774
});
757775
ipcMain.on("siyuan-hotkey", (event, data) => {
758776
globalShortcut.unregisterAll();
759-
if (!data.hotkey) {
777+
if (!data.hotkeys || data.hotkeys.length === 0) {
760778
return;
761779
}
762-
globalShortcut.register(data.hotkey, () => {
763-
workspaces.forEach(item => {
764-
const mainWindow = item.browserWindow;
765-
if (mainWindow.isMinimized()) {
766-
mainWindow.restore();
767-
mainWindow.show(); // 按 `Alt+M` 后隐藏窗口,再次按 `Alt+M` 显示窗口后会卡住不能编辑 https://github.com/siyuan-note/siyuan/issues/8456
768-
} else {
769-
if (mainWindow.isVisible()) {
770-
if (1 === workspaces.length) { // 改进 `Alt+M` 激活窗口 https://github.com/siyuan-note/siyuan/issues/7258
771-
if (!mainWindow.isFocused()) {
772-
mainWindow.show();
780+
data.hotkeys.forEach((item, index) => {
781+
const shortcut = hotKey2Electron(item);
782+
if (!shortcut) {
783+
return;
784+
}
785+
if (index === 0) {
786+
globalShortcut.register(shortcut, () => {
787+
workspaces.forEach(workspaceItem => {
788+
const mainWindow = workspaceItem.browserWindow;
789+
if (mainWindow.isMinimized()) {
790+
mainWindow.restore();
791+
mainWindow.show(); // 按 `Alt+M` 后隐藏窗口,再次按 `Alt+M` 显示窗口后会卡住不能编辑 https://github.com/siyuan-note/siyuan/issues/8456
792+
} else {
793+
if (mainWindow.isVisible()) {
794+
if (1 === workspaces.length) { // 改进 `Alt+M` 激活窗口 https://github.com/siyuan-note/siyuan/issues/7258
795+
if (!mainWindow.isFocused()) {
796+
mainWindow.show();
797+
} else {
798+
hideWindow(mainWindow);
799+
}
800+
} else {
801+
hideWindow(mainWindow);
802+
}
773803
} else {
774-
hideWindow(mainWindow);
804+
mainWindow.show();
775805
}
776-
} else {
777-
hideWindow(mainWindow);
778806
}
779-
} else {
780-
mainWindow.show();
781-
}
782-
}
783807

784-
if ("win32" === process.platform || "linux" === process.platform) {
785-
resetTrayMenu(item.tray, data.languages, mainWindow);
786-
}
787-
});
788-
});
808+
if ("win32" === process.platform || "linux" === process.platform) {
809+
resetTrayMenu(workspaceItem.tray, data.languages, mainWindow);
810+
}
811+
});
812+
});
813+
} else {
814+
globalShortcut.register(data.hotkey, () => {
815+
BrowserWindow.getAllWindows().forEach(itemB => {
816+
itemB.webContents.send("siyuan-hotkey", {
817+
hotkey: item
818+
});
819+
});
820+
});
821+
}
822+
})
789823
});
790824
ipcMain.on("siyuan-send_windows", (event, data) => {
791825
BrowserWindow.getAllWindows().forEach(item => {

app/src/boot/globalEvent/keydown.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ import {getDisplayName, getNotebookName, getTopPaths, movePathTo, moveToPath} fr
2525
import {openFileById} from "../../editor/util";
2626
import {getAllDocks, getAllModels, getAllTabs} from "../../layout/getAll";
2727
import {openGlobalSearch} from "../../search/util";
28-
import {getColIndex} from "../../protyle/util/table";
2928
import {focusBlock, focusByRange} from "../../protyle/util/selection";
3029
import {initFileMenu, initNavigationMenu} from "../../menus/navigation";
3130
import {bindMenuKeydown} from "../../menus/Menu";
32-
import {showMessage} from "../../dialog/message";
3331
import {Dialog} from "../../dialog";
3432
import {unicode2Emoji} from "../../emoji";
3533
import {deleteFiles} from "../../editor/deleteFile";
@@ -43,6 +41,8 @@ import {hintMoveBlock} from "../../protyle/hint/extend";
4341
import {Backlink} from "../../layout/dock/Backlink";
4442
/// #if !BROWSER
4543
import {setZoom} from "../../layout/topBar";
44+
import {getCurrentWindow} from "@electron/remote";
45+
import {ipcRenderer} from "electron";
4646
/// #endif
4747
import {openHistory} from "../../history/history";
4848
import {openCard, openCardByData} from "../../card/openCard";
@@ -57,7 +57,6 @@ import {commandPanel} from "../../plugin/commandPanel";
5757
import {toggleDockBar} from "../../layout/dock/util";
5858
import {workspaceMenu} from "../../menus/workspace";
5959

60-
6160
const switchDialogEvent = (app: App, event: MouseEvent, switchDialog: Dialog) => {
6261
event.preventDefault();
6362
let target = event.target as HTMLElement;
@@ -816,7 +815,7 @@ const panelTreeKeydown = (app: App, event: KeyboardEvent) => {
816815
return false;
817816
};
818817

819-
export const windowKeyDown = (app: App, event: KeyboardEvent, switchDialog:Dialog) => {
818+
export const windowKeyDown = (app: App, event: KeyboardEvent, switchDialog: Dialog) => {
820819

821820
if (document.querySelector(".av__mask") || document.getElementById("errorLog") || event.isComposing) {
822821
return;
@@ -1302,3 +1301,21 @@ export const windowKeyDown = (app: App, event: KeyboardEvent, switchDialog:Dialo
13021301
return true;
13031302
}
13041303
};
1304+
1305+
export const sendGlobalShortcut = (app: App) => {
1306+
/// #if !BROWSER
1307+
const hotkeys = [window.siyuan.config.keymap.general.toggleWin.custom];
1308+
app.plugins.forEach(plugin => {
1309+
plugin.commands.forEach(command => {
1310+
if (command.globalCallback) {
1311+
hotkeys.push(command.customHotkey)
1312+
}
1313+
})
1314+
})
1315+
ipcRenderer.send(Constants.SIYUAN_HOTKEY, {
1316+
languages: window.siyuan.languages["_trayMenu"],
1317+
id: getCurrentWindow().id,
1318+
hotkeys
1319+
});
1320+
/// #endif
1321+
};

app/src/boot/onGetConfig.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {exportLayout, JSONToLayout, resetLayout, resizeTopbar, resizeTabs} from "../layout/util";
2-
import {hotKey2Electron, setStorageVal} from "../protyle/util/compatibility";
2+
import {setStorageVal} from "../protyle/util/compatibility";
33
/// #if !BROWSER
44
import {dialog, getCurrentWindow} from "@electron/remote";
55
import {ipcRenderer, OpenDialogReturnValue, webFrame} from "electron";
@@ -27,6 +27,7 @@ import {openChangelog} from "./openChangelog";
2727
import {getIdFromSYProtocol, isSYProtocol} from "../util/pathName";
2828
import {App} from "../index";
2929
import {initWindowEvent} from "./globalEvent/event";
30+
import {sendGlobalShortcut} from "./globalEvent/keydown";
3031

3132
const matchKeymap = (keymap: Record<string, IKeymapItem>, key1: "general" | "editor", key2?: "general" | "insert" | "heading" | "list" | "table") => {
3233
if (key1 === "general") {
@@ -105,13 +106,7 @@ export const onGetConfig = (isStart: boolean, app: App) => {
105106
fetchPost("/api/setting/setKeymap", {
106107
data: window.siyuan.config.keymap
107108
}, () => {
108-
/// #if !BROWSER
109-
ipcRenderer.send(Constants.SIYUAN_HOTKEY, {
110-
languages: window.siyuan.languages["_trayMenu"],
111-
id: getCurrentWindow().id,
112-
hotkey: hotKey2Electron(window.siyuan.config.keymap.general.toggleWin.custom)
113-
});
114-
/// #endif
109+
sendGlobalShortcut(app);
115110
});
116111
}
117112
/// #if !BROWSER
@@ -121,11 +116,7 @@ export const onGetConfig = (isStart: boolean, app: App) => {
121116
id: getCurrentWindow().id,
122117
port: location.port
123118
});
124-
ipcRenderer.send(Constants.SIYUAN_HOTKEY, {
125-
languages: window.siyuan.languages["_trayMenu"],
126-
id: getCurrentWindow().id,
127-
hotkey: hotKey2Electron(window.siyuan.config.keymap.general.toggleWin.custom)
128-
});
119+
sendGlobalShortcut(app);
129120
webFrame.setZoomFactor(window.siyuan.storage[Constants.LOCAL_ZOOM]);
130121
/// #endif
131122
if (!window.siyuan.config.uiLayout || (window.siyuan.config.uiLayout && !window.siyuan.config.uiLayout.left)) {
@@ -314,6 +305,21 @@ export const initWindow = (app: App) => {
314305
ipcRenderer.on(Constants.SIYUAN_EXPORT_CLOSE, () => {
315306
window.siyuan.printWin.destroy();
316307
});
308+
ipcRenderer.on(Constants.SIYUAN_HOTKEY, (e, data) => {
309+
let matchCommand = false;
310+
app.plugins.find(item => {
311+
item.commands.find(command => {
312+
if (command.globalCallback && data === command.customHotkey) {
313+
matchCommand = true;
314+
command.globalCallback();
315+
return true;
316+
}
317+
});
318+
if (matchCommand) {
319+
return true;
320+
}
321+
});
322+
});
317323
ipcRenderer.on(Constants.SIYUAN_EXPORT_PDF, (e, ipcData) => {
318324
dialog.showOpenDialog({
319325
title: window.siyuan.languages.export + " PDF",

app/src/config/keymap.ts

+4-19
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import {hotKey2Electron, isCtrl, isMac, updateHotkeyTip} from "../protyle/util/compatibility";
1+
import {isCtrl, isMac, updateHotkeyTip} from "../protyle/util/compatibility";
22
import {Constants} from "../constants";
33
import {showMessage} from "../dialog/message";
44
import {fetchPost} from "../util/fetch";
55
import {exportLayout} from "../layout/util";
6-
/// #if !BROWSER
7-
import {getCurrentWindow} from "@electron/remote";
8-
import {ipcRenderer} from "electron";
9-
/// #endif
106
import {confirmDialog} from "../dialog/confirmDialog";
117
import {App} from "../index";
8+
import {sendGlobalShortcut} from "../boot/globalEvent/keydown";
129

1310
export const keymap = {
1411
element: undefined as Element,
@@ -192,13 +189,7 @@ export const keymap = {
192189
fetchPost("/api/setting/setKeymap", {
193190
data
194191
}, () => {
195-
/// #if !BROWSER
196-
ipcRenderer.send(Constants.SIYUAN_HOTKEY, {
197-
languages: window.siyuan.languages["_trayMenu"],
198-
id: getCurrentWindow().id,
199-
hotkey: hotKey2Electron(window.siyuan.config.keymap.general.toggleWin.custom)
200-
});
201-
/// #endif
192+
sendGlobalShortcut(app);
202193
});
203194
},
204195
search(value: string, keymapString: string) {
@@ -298,13 +289,7 @@ export const keymap = {
298289
data: Constants.SIYUAN_KEYMAP,
299290
}, () => {
300291
window.location.reload();
301-
/// #if !BROWSER
302-
ipcRenderer.send(Constants.SIYUAN_HOTKEY, {
303-
languages: window.siyuan.languages["_trayMenu"],
304-
id: getCurrentWindow().id,
305-
hotkey: hotKey2Electron(window.siyuan.config.keymap.general.toggleWin.custom)
306-
});
307-
/// #endif
292+
sendGlobalShortcut(app);
308293
});
309294
});
310295
});

app/src/protyle/util/compatibility.ts

-14
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,6 @@ export const updateHotkeyTip = (hotkey: string) => {
129129
return keys.join("+");
130130
};
131131

132-
export const hotKey2Electron = (key: string) => {
133-
let electronKey = "";
134-
if (key.indexOf("⌘") > -1) {
135-
electronKey += "CommandOrControl+";
136-
}
137-
if (key.indexOf("⇧") > -1) {
138-
electronKey += "Shift+";
139-
}
140-
if (key.indexOf("⌥") > -1) {
141-
electronKey += "Alt+";
142-
}
143-
return electronKey + key.substr(key.length - 1);
144-
};
145-
146132
export const getLocalStorage = (cb: () => void) => {
147133
fetchPost("/api/storage/getLocalStorage", undefined, (response) => {
148134
window.siyuan.storage = response.data;

0 commit comments

Comments
 (0)