diff --git a/app/electron/main.js b/app/electron/main.js index c907be67c..c1a665db1 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -49,6 +49,24 @@ try { app.exit(); } +const hotKey2Electron = (key) => { + console.log(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.substr(key.length - 1); +}; + const exitApp = (port, errorWindowId) => { let tray; let mainWindow; @@ -756,36 +774,52 @@ app.whenReady().then(() => { }); ipcMain.on("siyuan-hotkey", (event, data) => { globalShortcut.unregisterAll(); - if (!data.hotkey) { + if (!data.hotkeys || data.hotkeys.length === 0) { return; } - globalShortcut.register(data.hotkey, () => { - workspaces.forEach(item => { - const mainWindow = item.browserWindow; - if (mainWindow.isMinimized()) { - mainWindow.restore(); - mainWindow.show(); // 按 `Alt+M` 后隐藏窗口,再次按 `Alt+M` 显示窗口后会卡住不能编辑 https://github.com/siyuan-note/siyuan/issues/8456 - } else { - if (mainWindow.isVisible()) { - if (1 === workspaces.length) { // 改进 `Alt+M` 激活窗口 https://github.com/siyuan-note/siyuan/issues/7258 - if (!mainWindow.isFocused()) { - mainWindow.show(); - } else { - hideWindow(mainWindow); - } + data.hotkeys.forEach((item, index) => { + const shortcut = hotKey2Electron(item); + if (!shortcut) { + return; + } + if (index === 0) { + globalShortcut.register(shortcut, () => { + workspaces.forEach(workspaceItem => { + const mainWindow = workspaceItem.browserWindow; + if (mainWindow.isMinimized()) { + mainWindow.restore(); + mainWindow.show(); // 按 `Alt+M` 后隐藏窗口,再次按 `Alt+M` 显示窗口后会卡住不能编辑 https://github.com/siyuan-note/siyuan/issues/8456 } else { - hideWindow(mainWindow); + if (mainWindow.isVisible()) { + if (1 === workspaces.length) { // 改进 `Alt+M` 激活窗口 https://github.com/siyuan-note/siyuan/issues/7258 + if (!mainWindow.isFocused()) { + mainWindow.show(); + } else { + hideWindow(mainWindow); + } + } else { + hideWindow(mainWindow); + } + } else { + mainWindow.show(); + } } - } else { - mainWindow.show(); - } - } - if ("win32" === process.platform || "linux" === process.platform) { - resetTrayMenu(item.tray, data.languages, mainWindow); - } - }); - }); + if ("win32" === process.platform || "linux" === process.platform) { + resetTrayMenu(workspaceItem.tray, data.languages, mainWindow); + } + }); + }); + } else { + globalShortcut.register(data.hotkey, () => { + BrowserWindow.getAllWindows().forEach(itemB => { + itemB.webContents.send("siyuan-hotkey", { + hotkey: item + }); + }); + }); + } + }) }); ipcMain.on("siyuan-send_windows", (event, data) => { BrowserWindow.getAllWindows().forEach(item => { diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts index a3203e88f..5ba800dc0 100644 --- a/app/src/boot/globalEvent/keydown.ts +++ b/app/src/boot/globalEvent/keydown.ts @@ -25,11 +25,9 @@ import {getDisplayName, getNotebookName, getTopPaths, movePathTo, moveToPath} fr import {openFileById} from "../../editor/util"; import {getAllDocks, getAllModels, getAllTabs} from "../../layout/getAll"; import {openGlobalSearch} from "../../search/util"; -import {getColIndex} from "../../protyle/util/table"; import {focusBlock, focusByRange} from "../../protyle/util/selection"; import {initFileMenu, initNavigationMenu} from "../../menus/navigation"; import {bindMenuKeydown} from "../../menus/Menu"; -import {showMessage} from "../../dialog/message"; import {Dialog} from "../../dialog"; import {unicode2Emoji} from "../../emoji"; import {deleteFiles} from "../../editor/deleteFile"; @@ -43,6 +41,8 @@ import {hintMoveBlock} from "../../protyle/hint/extend"; import {Backlink} from "../../layout/dock/Backlink"; /// #if !BROWSER import {setZoom} from "../../layout/topBar"; +import {getCurrentWindow} from "@electron/remote"; +import {ipcRenderer} from "electron"; /// #endif import {openHistory} from "../../history/history"; import {openCard, openCardByData} from "../../card/openCard"; @@ -57,7 +57,6 @@ import {commandPanel} from "../../plugin/commandPanel"; import {toggleDockBar} from "../../layout/dock/util"; import {workspaceMenu} from "../../menus/workspace"; - const switchDialogEvent = (app: App, event: MouseEvent, switchDialog: Dialog) => { event.preventDefault(); let target = event.target as HTMLElement; @@ -816,7 +815,7 @@ const panelTreeKeydown = (app: App, event: KeyboardEvent) => { return false; }; -export const windowKeyDown = (app: App, event: KeyboardEvent, switchDialog:Dialog) => { +export const windowKeyDown = (app: App, event: KeyboardEvent, switchDialog: Dialog) => { if (document.querySelector(".av__mask") || document.getElementById("errorLog") || event.isComposing) { return; @@ -1302,3 +1301,21 @@ export const windowKeyDown = (app: App, event: KeyboardEvent, switchDialog:Dialo return true; } }; + +export const sendGlobalShortcut = (app: App) => { + /// #if !BROWSER + const hotkeys = [window.siyuan.config.keymap.general.toggleWin.custom]; + app.plugins.forEach(plugin => { + plugin.commands.forEach(command => { + if (command.globalCallback) { + hotkeys.push(command.customHotkey) + } + }) + }) + ipcRenderer.send(Constants.SIYUAN_HOTKEY, { + languages: window.siyuan.languages["_trayMenu"], + id: getCurrentWindow().id, + hotkeys + }); + /// #endif +}; diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts index 0063fb70e..b1c80a0fd 100644 --- a/app/src/boot/onGetConfig.ts +++ b/app/src/boot/onGetConfig.ts @@ -1,5 +1,5 @@ import {exportLayout, JSONToLayout, resetLayout, resizeTopbar, resizeTabs} from "../layout/util"; -import {hotKey2Electron, setStorageVal} from "../protyle/util/compatibility"; +import {setStorageVal} from "../protyle/util/compatibility"; /// #if !BROWSER import {dialog, getCurrentWindow} from "@electron/remote"; import {ipcRenderer, OpenDialogReturnValue, webFrame} from "electron"; @@ -27,6 +27,7 @@ import {openChangelog} from "./openChangelog"; import {getIdFromSYProtocol, isSYProtocol} from "../util/pathName"; import {App} from "../index"; import {initWindowEvent} from "./globalEvent/event"; +import {sendGlobalShortcut} from "./globalEvent/keydown"; const matchKeymap = (keymap: Record, key1: "general" | "editor", key2?: "general" | "insert" | "heading" | "list" | "table") => { if (key1 === "general") { @@ -105,13 +106,7 @@ export const onGetConfig = (isStart: boolean, app: App) => { fetchPost("/api/setting/setKeymap", { data: window.siyuan.config.keymap }, () => { - /// #if !BROWSER - ipcRenderer.send(Constants.SIYUAN_HOTKEY, { - languages: window.siyuan.languages["_trayMenu"], - id: getCurrentWindow().id, - hotkey: hotKey2Electron(window.siyuan.config.keymap.general.toggleWin.custom) - }); - /// #endif + sendGlobalShortcut(app); }); } /// #if !BROWSER @@ -121,11 +116,7 @@ export const onGetConfig = (isStart: boolean, app: App) => { id: getCurrentWindow().id, port: location.port }); - ipcRenderer.send(Constants.SIYUAN_HOTKEY, { - languages: window.siyuan.languages["_trayMenu"], - id: getCurrentWindow().id, - hotkey: hotKey2Electron(window.siyuan.config.keymap.general.toggleWin.custom) - }); + sendGlobalShortcut(app); webFrame.setZoomFactor(window.siyuan.storage[Constants.LOCAL_ZOOM]); /// #endif if (!window.siyuan.config.uiLayout || (window.siyuan.config.uiLayout && !window.siyuan.config.uiLayout.left)) { @@ -314,6 +305,21 @@ export const initWindow = (app: App) => { ipcRenderer.on(Constants.SIYUAN_EXPORT_CLOSE, () => { window.siyuan.printWin.destroy(); }); + ipcRenderer.on(Constants.SIYUAN_HOTKEY, (e, data) => { + let matchCommand = false; + app.plugins.find(item => { + item.commands.find(command => { + if (command.globalCallback && data === command.customHotkey) { + matchCommand = true; + command.globalCallback(); + return true; + } + }); + if (matchCommand) { + return true; + } + }); + }); ipcRenderer.on(Constants.SIYUAN_EXPORT_PDF, (e, ipcData) => { dialog.showOpenDialog({ title: window.siyuan.languages.export + " PDF", diff --git a/app/src/config/keymap.ts b/app/src/config/keymap.ts index 29e2c8ec0..ecff063f8 100644 --- a/app/src/config/keymap.ts +++ b/app/src/config/keymap.ts @@ -1,14 +1,11 @@ -import {hotKey2Electron, isCtrl, isMac, updateHotkeyTip} from "../protyle/util/compatibility"; +import {isCtrl, isMac, updateHotkeyTip} from "../protyle/util/compatibility"; import {Constants} from "../constants"; import {showMessage} from "../dialog/message"; import {fetchPost} from "../util/fetch"; import {exportLayout} from "../layout/util"; -/// #if !BROWSER -import {getCurrentWindow} from "@electron/remote"; -import {ipcRenderer} from "electron"; -/// #endif import {confirmDialog} from "../dialog/confirmDialog"; import {App} from "../index"; +import {sendGlobalShortcut} from "../boot/globalEvent/keydown"; export const keymap = { element: undefined as Element, @@ -192,13 +189,7 @@ export const keymap = { fetchPost("/api/setting/setKeymap", { data }, () => { - /// #if !BROWSER - ipcRenderer.send(Constants.SIYUAN_HOTKEY, { - languages: window.siyuan.languages["_trayMenu"], - id: getCurrentWindow().id, - hotkey: hotKey2Electron(window.siyuan.config.keymap.general.toggleWin.custom) - }); - /// #endif + sendGlobalShortcut(app); }); }, search(value: string, keymapString: string) { @@ -298,13 +289,7 @@ export const keymap = { data: Constants.SIYUAN_KEYMAP, }, () => { window.location.reload(); - /// #if !BROWSER - ipcRenderer.send(Constants.SIYUAN_HOTKEY, { - languages: window.siyuan.languages["_trayMenu"], - id: getCurrentWindow().id, - hotkey: hotKey2Electron(window.siyuan.config.keymap.general.toggleWin.custom) - }); - /// #endif + sendGlobalShortcut(app); }); }); }); diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts index 5ff3c40c2..3a37bcc7b 100644 --- a/app/src/protyle/util/compatibility.ts +++ b/app/src/protyle/util/compatibility.ts @@ -129,20 +129,6 @@ export const updateHotkeyTip = (hotkey: string) => { return keys.join("+"); }; -export const hotKey2Electron = (key: string) => { - let electronKey = ""; - if (key.indexOf("⌘") > -1) { - electronKey += "CommandOrControl+"; - } - if (key.indexOf("⇧") > -1) { - electronKey += "Shift+"; - } - if (key.indexOf("⌥") > -1) { - electronKey += "Alt+"; - } - return electronKey + key.substr(key.length - 1); -}; - export const getLocalStorage = (cb: () => void) => { fetchPost("/api/storage/getLocalStorage", undefined, (response) => { window.siyuan.storage = response.data;