Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Improve keyboard shortcut validation https://github.com/siyuan-note/siyuan/issues/18698

Daniel7e6155e0ca26
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
index 40284db00a..b95087244e 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
@@ -7,9 +7,32 @@
"id": "20200924100950-9op5xi1",
"title": "Shortcuts",
"type": "doc",
- "updated": "20260811122156"
+ "updated": "20260813143406"
},
"Children": [
+ {
+ "ID": "20260813143406-k7m2x9q",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260813143406-k7m2x9q",
+ "updated": "20260813143406"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "To avoid interfering with normal text input, a single character alone or a single character combined with "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Shift"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " cannot be set as a shortcut."
+ }
+ ]
+ },
{
"ID": "20201227133532-44ubczo",
"Type": "NodeHeading",
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
index e235090508..2b420fb078 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
@@ -7,9 +7,32 @@
"id": "20200813004551-gm0pbn1",
"title": "快捷键",
"type": "doc",
- "updated": "20260811122156"
+ "updated": "20260813143407"
},
"Children": [
+ {
+ "ID": "20260813143407-p4v8n2c",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260813143407-p4v8n2c",
+ "updated": "20260813143407"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "为避免影响正常文本输入,不能将单个字符或与 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Shift"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " 组合的单个字符设置为快捷键。"
+ }
+ ]
+ },
{
"ID": "20240112150737-e41yt1y",
"Type": "NodeHeading",
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
index 43f319828a..2f17c17508 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
@@ -7,9 +7,32 @@
"id": "20211226122549-jktxego",
"title": "快捷鍵",
"type": "doc",
- "updated": "20260811122156"
+ "updated": "20260813143408"
},
"Children": [
+ {
+ "ID": "20260813143408-r6w3j9h",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260813143408-r6w3j9h",
+ "updated": "20260813143408"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "為避免影響正常文字輸入,不能將單一字元或與 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Shift"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " 組合的單一字元設為快捷鍵。"
+ }
+ ]
+ },
{
"ID": "20211226122652-0t4c1qf",
"Type": "NodeHeading",
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
index 48f01aaca3..e26285dc62 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
@@ -9,9 +9,32 @@
"id": "20240530101000-xsbxokr",
"title": "ショートカット",
"type": "doc",
- "updated": "20260811122156"
+ "updated": "20260813143409"
},
"Children": [
+ {
+ "ID": "20260813143409-t5y8q2m",
+ "Type": "NodeParagraph",
+ "Properties": {
+ "id": "20260813143409-t5y8q2m",
+ "updated": "20260813143409"
+ },
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "通常のテキスト入力への影響を避けるため、単一文字、または "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Shift"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " と組み合わせた単一文字をショートカットに設定することはできません。"
+ }
+ ]
+ },
{
"ID": "20240530101000-9imgmgf",
"Type": "NodeHeading",
diff --git a/app/src/boot/globalEvent/commonHotkey.ts b/app/src/boot/globalEvent/commonHotkey.ts
index 81aa5fb9fb..95889238f5 100644
--- a/app/src/boot/globalEvent/commonHotkey.ts
+++ b/app/src/boot/globalEvent/commonHotkey.ts
@@ -7,6 +7,7 @@ import {ipcRenderer} from "electron";
import type {App} from "../../index";
import {isMac, isNotCtrl, isOnlyMeta} from "../../protyle/util/compatibility";
import {showPopover} from "../../block/popover";
+import {clearDisallowedKeymapItems} from "../../util/hotKeyPolicy";
const matchKeymap = (keymap: Config.IKeys, key1: "general" | "editor", key2?: "general" | "insert" | "heading" | "list" | "table") => {
if (key1 === "general") {
@@ -95,6 +96,21 @@ const hasKeymap = (keymap: Record<string, IKeymapItem>, key1: "general" | "edito
return match;
};
+const clearDisallowedKeymap = () => {
+ let changed = clearDisallowedKeymapItems(window.siyuan.config.keymap.general);
+ Object.values(window.siyuan.config.keymap.editor).forEach((keymap) => {
+ if (clearDisallowedKeymapItems(keymap)) {
+ changed = true;
+ }
+ });
+ Object.values(window.siyuan.config.keymap.plugin || {}).forEach((keymap) => {
+ if (clearDisallowedKeymapItems(keymap, true)) {
+ changed = true;
+ }
+ });
+ return changed;
+};
+
export const correctHotkey = (app: App) => {
if (!["darwin", "ios"].includes(window.siyuan.config.system.os)) {
["fileTree", "outline", "bookmark", "tag", "dailyNote", "inbox", "backlinks",
@@ -121,9 +137,11 @@ export const correctHotkey = (app: App) => {
const hasKeymap4 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.heading, "editor", "heading");
const hasKeymap5 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.list, "editor", "list");
const hasKeymap6 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.table, "editor", "table");
+ const clearedDisallowedKeymap = clearDisallowedKeymap();
if (!window.siyuan.config.readonly &&
(!matchKeymap1 || !matchKeymap2 || !matchKeymap3 || !matchKeymap4 || !matchKeymap5 || !matchKeymap6 ||
- !hasKeymap1 || !hasKeymap2 || !hasKeymap3 || !hasKeymap4 || !hasKeymap5 || !hasKeymap6)) {
+ !hasKeymap1 || !hasKeymap2 || !hasKeymap3 || !hasKeymap4 || !hasKeymap5 || !hasKeymap6 ||
+ clearedDisallowedKeymap)) {
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_CMD, {
cmd: "writeLog",
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts
index adf49da35f..d27e8c84aa 100644
--- a/app/src/boot/globalEvent/keydown.ts
+++ b/app/src/boot/globalEvent/keydown.ts
@@ -53,6 +53,7 @@ import {reloadProtyle} from "../../protyle/util/reload";
import {fullscreen} from "../../protyle/breadcrumb/action";
import {openRecentDocs} from "../../business/openRecentDocs";
import type {App} from "../../index";
+import {clearDisallowedTextInputHotkey} from "../../util/hotKeyPolicy";
import {openBacklink, openGraph, openOutline, toggleDockBar} from "../../layout/dock/util";
import {workspaceMenu} from "../../menus/workspace";
import {resize} from "../../protyle/util/resize";
@@ -1856,11 +1857,14 @@ export const windowKeyDown = (app: App, event: KeyboardEvent) => {
export const sendGlobalShortcut = (app: App) => {
/// #if !BROWSER
- const hotkeys = [window.siyuan.config.keymap.general.toggleWin.custom];
+ const hotkeys = [clearDisallowedTextInputHotkey(window.siyuan.config.keymap.general.toggleWin.custom)];
app.plugins.forEach(plugin => {
plugin.commands.forEach(command => {
- if (command.globalCallback) {
- hotkeys.push(command.customHotkey);
+ if (command.globalCallback && command.customHotkey) {
+ const hotkey = clearDisallowedTextInputHotkey(command.customHotkey);
+ if (hotkey) {
+ hotkeys.push(hotkey);
+ }
}
});
});
diff --git a/app/src/config/tabs/keymapUi.ts b/app/src/config/tabs/keymapUi.ts
index 6825fa2c08..470feefead 100644
--- a/app/src/config/tabs/keymapUi.ts
+++ b/app/src/config/tabs/keymapUi.ts
@@ -12,6 +12,7 @@ import {sendGlobalShortcut, sendUnregisterGlobalShortcut} from "../../boot/globa
import {normalizeSearchText} from "../search/normalize";
import {genButtonRowHtml, genConfigGroup} from "../render/render";
import type {Plugin} from "../../plugin";
+import {isDisallowedTextInputHotkey} from "../../util/hotKeyPolicy";
const keymapToolbarSearchStrings = (): string[] => [
window.siyuan.languages.keymapTip,
window.siyuan.languages.keymapTip2,
@@ -458,10 +459,10 @@ const bindKeymapList = (root: HTMLElement) => {
hasConflict = true;
}
if (
- !hasConflict && (RESERVED_KEYMAPS.includes(keymapStr) || !matchHotKey(keymapStr, event) ||
+ !hasConflict && (isDisallowedTextInputHotkey(keymapStr) || RESERVED_KEYMAPS.includes(keymapStr) ||
+ !matchHotKey(keymapStr, event) ||
(isMac() && keys[0] === "general" && ["goToEditTabNext", "goToEditTabPrev"].includes(keys[1]) && keymapStr.includes("⌘")))
) {
- // TODO 还应该禁止单个数字或字母作为快捷键?
showMessage(`${window.siyuan.languages.invalid} [${adoptKeymapStr}]`, undefined, undefined, "keymapInvalid");
hasConflict = true;
} else {
diff --git a/app/src/plugin/index.ts b/app/src/plugin/index.ts
index 362247b5de..47f95953c4 100644
--- a/app/src/plugin/index.ts
+++ b/app/src/plugin/index.ts
@@ -24,6 +24,31 @@ import {addPluginDock, afterLoadPlugin, loadPlugins} from "./loader";
import {normalizeStoragePath} from "../util/pathName";
import {Kernel} from "./kernel";
import {IAgentCapabilityEffects, registerCapability} from "../layout/dock/agent/frontendCapabilities";
+import {isDisallowedTextInputHotkey, normalizePluginHotkey} from "../util/hotKeyPolicy";
+
+const updatePluginKeymap = (pluginName: string, key: string, hotkey: unknown) => {
+ if (!window.siyuan.config.keymap.plugin) {
+ window.siyuan.config.keymap.plugin = {};
+ }
+ if (!window.siyuan.config.keymap.plugin[pluginName]) {
+ window.siyuan.config.keymap.plugin[pluginName] = {};
+ }
+ const keymapItem = window.siyuan.config.keymap.plugin[pluginName][key];
+ const normalized = normalizePluginHotkey(hotkey, keymapItem?.custom);
+ if (!keymapItem) {
+ window.siyuan.config.keymap.plugin[pluginName][key] = {
+ default: normalized.defaultHotkey,
+ custom: normalized.customHotkey,
+ };
+ } else {
+ keymapItem.default = normalized.defaultHotkey;
+ keymapItem.custom = normalized.customHotkey;
+ }
+ normalized.ignoredHotkeys.forEach((ignoredHotkey) => {
+ console.warn(`Plugin ${pluginName} ignored disallowed hotkey "${ignoredHotkey}" for "${key}".`);
+ });
+ return window.siyuan.config.keymap.plugin[pluginName][key];
+};
export class Plugin {
private app: App;
@@ -99,25 +124,7 @@ export class Plugin {
if (typeof toolbarItem.hotkey !== "string") {
toolbarItem.hotkey = "";
}
- if (!window.siyuan.config.keymap.plugin) {
- window.siyuan.config.keymap.plugin = {};
- }
- if (!window.siyuan.config.keymap.plugin[options.name]) {
- window.siyuan.config.keymap.plugin[options.name] = {
- [toolbarItem.name]: {
- default: toolbarItem.hotkey,
- custom: toolbarItem.hotkey,
- }
- };
- }
- if (!window.siyuan.config.keymap.plugin[options.name][toolbarItem.name]) {
- window.siyuan.config.keymap.plugin[options.name][toolbarItem.name] = {
- default: toolbarItem.hotkey,
- custom: toolbarItem.hotkey,
- };
- } else {
- window.siyuan.config.keymap.plugin[options.name][toolbarItem.name].default = toolbarItem.hotkey;
- }
+ toolbarItem.hotkey = updatePluginKeymap(options.name, toolbarItem.name, toolbarItem.hotkey).default;
});
}
@@ -162,37 +169,15 @@ export class Plugin {
if (typeof command.hotkey !== "string") {
command.hotkey = "";
}
- if (!window.siyuan.config.keymap.plugin) {
- window.siyuan.config.keymap.plugin = {};
- }
- if (!window.siyuan.config.keymap.plugin[this.name]) {
- command.customHotkey = command.hotkey;
- window.siyuan.config.keymap.plugin[this.name] = {
- [command.langKey]: {
- default: command.hotkey,
- custom: command.hotkey,
- }
- };
- } else if (!window.siyuan.config.keymap.plugin[this.name][command.langKey]) {
- command.customHotkey = command.hotkey;
- window.siyuan.config.keymap.plugin[this.name][command.langKey] = {
- default: command.hotkey,
- custom: command.hotkey,
- };
- } else if (window.siyuan.config.keymap.plugin[this.name][command.langKey]) {
- if (typeof window.siyuan.config.keymap.plugin[this.name][command.langKey].custom === "string") {
- command.customHotkey = window.siyuan.config.keymap.plugin[this.name][command.langKey].custom;
- } else {
- command.customHotkey = command.hotkey;
- }
- window.siyuan.config.keymap.plugin[this.name][command.langKey]["default"] = command.hotkey;
- }
+ const keymapItem = updatePluginKeymap(this.name, command.langKey, command.hotkey);
+ command.hotkey = keymapItem.default;
+ command.customHotkey = keymapItem.custom;
if (typeof command.customHotkey !== "string") {
console.error(`${this.name} - commands data is error and has been removed.`);
} else {
this.commands.push(command);
/// #if !BROWSER
- if (command.globalCallback) {
+ if (command.globalCallback && command.customHotkey && !isDisallowedTextInputHotkey(command.customHotkey)) {
ipcRenderer.send(Constants.SIYUAN_CMD, {
cmd: "registerGlobalShortcut",
accelerator: command.customHotkey
@@ -524,24 +509,7 @@ export class Plugin {
}
/// #endif
};
- if (!window.siyuan.config.keymap.plugin) {
- window.siyuan.config.keymap.plugin = {};
- }
- if (!window.siyuan.config.keymap.plugin[this.name]) {
- window.siyuan.config.keymap.plugin[this.name] = {};
- }
- const hotkey = typeof options.config.hotkey === "string" ? options.config.hotkey : "";
- if (!window.siyuan.config.keymap.plugin[this.name][type2]) {
- window.siyuan.config.keymap.plugin[this.name][type2] = {
- default: hotkey,
- custom: hotkey,
- };
- } else {
- if (typeof window.siyuan.config.keymap.plugin[this.name][type2].custom !== "string") {
- window.siyuan.config.keymap.plugin[this.name][type2].custom = hotkey;
- }
- window.siyuan.config.keymap.plugin[this.name][type2]["default"] = hotkey;
- }
+ options.config.hotkey = updatePluginKeymap(this.name, type2, options.config.hotkey).default;
addPluginDock(this);
return this.docks[type2];
}
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index 40ac9c0774..a78ac202f9 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -58,6 +58,7 @@ import {resizeSide} from "../../history/resizeSide";
import {activeBlur, updateMobilePluginToolbar} from "../../mobile/util/keyboardToolbar";
import {FormatPainter} from "./FormatPainter";
import {IFormatPainterSnapshot} from "./formatPainterCore";
+import {clearDisallowedTextInputHotkey} from "../../util/hotKeyPolicy";
const filterPluginToolbar = (toolbar: Array<string | IMenuItem>, lite: boolean) => {
if (!lite) {
@@ -118,6 +119,7 @@ export class Toolbar {
if (window.siyuan.config.keymap.plugin && window.siyuan.config.keymap.plugin[item.name] && window.siyuan.config.keymap.plugin[item.name][toolbarItem.name]) {
toolbarItem.hotkey = window.siyuan.config.keymap.plugin[item.name][toolbarItem.name].custom;
}
+ toolbarItem.hotkey = clearDisallowedTextInputHotkey(toolbarItem.hotkey);
});
options.toolbar = toolbarKeyToMenu(pluginToolbar);
});
@@ -181,6 +183,7 @@ export class Toolbar {
if (window.siyuan.config.keymap.plugin && window.siyuan.config.keymap.plugin[item.name] && window.siyuan.config.keymap.plugin[item.name][toolbarItem.name]) {
toolbarItem.hotkey = window.siyuan.config.keymap.plugin[item.name][toolbarItem.name].custom;
}
+ toolbarItem.hotkey = clearDisallowedTextInputHotkey(toolbarItem.hotkey);
});
protyle.options.toolbar = toolbarKeyToMenu(pluginToolbar);
});
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index 5b786a1184..65707b58ab 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -781,7 +781,7 @@ interface ILayoutJSON extends ILayoutOptions {
interface ICommand {
langKey: string, // 用于区分不同快捷键的 key, 同时作为 i18n 的字段名
langText?: string, // 显示的文本, 指定后不再使用 langKey 对应的 i18n 文本
- hotkey?: string, // 快捷键,默认为空字符串
+ hotkey?: string, // 快捷键,默认为空字符串,不能使用单个字符或 Shift+单个字符
customHotkey?: string,
callback?: () => void // 其余回调存在时将不会触发
globalCallback?: () => void // 焦点不在应用内时执行的回调
@@ -802,7 +802,7 @@ interface IPluginDockTab {
position: TPluginDockPosition,
size: Config.IUILayoutDockPanelSize,
icon: string,
- hotkey?: string,
+ hotkey?: string, // 快捷键,不能使用单个字符或 Shift+单个字符
title: string,
index?: number
show?: boolean
diff --git a/app/src/types/protyle.d.ts b/app/src/types/protyle.d.ts
index 19411ec6d9..14c63b254f 100644
--- a/app/src/types/protyle.d.ts
+++ b/app/src/types/protyle.d.ts
@@ -391,7 +391,7 @@ interface IMenuItem {
lang?: string;
/** svg 图标 */
icon?: string;
- /** 快捷键 */
+ /** 快捷键,不能使用单个字符或 Shift+单个字符 */
hotkey?: string;
/** 提示的位置 */
tipPosition?: string;
diff --git a/app/src/util/hotKeyPolicy.test.ts b/app/src/util/hotKeyPolicy.test.ts
new file mode 100644
index 0000000000..9cb3966ecb
--- /dev/null
+++ b/app/src/util/hotKeyPolicy.test.ts
@@ -0,0 +1,85 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {
+ clearDisallowedKeymapItems,
+ clearDisallowedTextInputHotkey,
+ isDisallowedTextInputHotkey,
+ normalizePluginHotkey,
+} from "./hotKeyPolicy";
+
+describe("text input hotkey policy", () => {
+ it("disallows a character without a non-Shift modifier", () => {
+ ["A", "z", "0", " ", "/", "+", "你", "🙂", "e\u0301"].forEach((hotkey) => {
+ assert.equal(isDisallowedTextInputHotkey(hotkey), true, hotkey);
+ });
+ });
+
+ it("disallows Shift with a character", () => {
+ ["⇧A", "⇧0", "⇧ ", "⇧/", "⇧你", "⇧🙂"].forEach((hotkey) => {
+ assert.equal(isDisallowedTextInputHotkey(hotkey), true, hotkey);
+ });
+ });
+
+ it("allows function and editing keys", () => {
+ ["", "F1", "F10", "Home", "⇧F1", "⇧Home", "←", "⇧←", "⇥", "⇧⇥", "↩", "⇧↩"].forEach((hotkey) => {
+ assert.equal(isDisallowedTextInputHotkey(hotkey), false, hotkey);
+ });
+ });
+
+ it("allows a character with a non-Shift modifier", () => {
+ ["⌘A", "⌃A", "⌥A", "⇧⌘A", "⌥⇧A", "⌃⌥⇧⌘A"].forEach((hotkey) => {
+ assert.equal(isDisallowedTextInputHotkey(hotkey), false, hotkey);
+ });
+ });
+
+ it("clears only disallowed text input hotkeys", () => {
+ assert.equal(clearDisallowedTextInputHotkey("⇧S"), "");
+ assert.equal(clearDisallowedTextInputHotkey("⇧F1"), "⇧F1");
+ assert.equal(clearDisallowedTextInputHotkey("⌘S"), "⌘S");
+ });
+
+ it("clears a disallowed plugin default and preserves a valid custom hotkey", () => {
+ assert.deepEqual(normalizePluginHotkey("⇧S", "⌘K"), {
+ defaultHotkey: "",
+ customHotkey: "⌘K",
+ ignoredHotkeys: ["⇧S"],
+ });
+ });
+
+ it("clears a disallowed plugin custom hotkey without restoring the default", () => {
+ assert.deepEqual(normalizePluginHotkey("⌘K", "S"), {
+ defaultHotkey: "⌘K",
+ customHotkey: "",
+ ignoredHotkeys: ["S"],
+ });
+ });
+
+ it("uses a normalized plugin default when no custom hotkey exists", () => {
+ assert.deepEqual(normalizePluginHotkey("⇧S"), {
+ defaultHotkey: "",
+ customHotkey: "",
+ ignoredHotkeys: ["⇧S"],
+ });
+ });
+
+ it("clears only custom hotkeys from the built-in keymap", () => {
+ const keymap = {
+ invalid: {default: "S", custom: "S"},
+ valid: {default: "⌘K", custom: "⌘K"},
+ };
+ assert.equal(clearDisallowedKeymapItems(keymap), true);
+ assert.deepEqual(keymap, {
+ invalid: {default: "S", custom: ""},
+ valid: {default: "⌘K", custom: "⌘K"},
+ });
+ assert.equal(clearDisallowedKeymapItems(keymap), false);
+ });
+
+ it("clears plugin defaults together with custom hotkeys", () => {
+ const keymap = {
+ invalid: {default: "⇧S", custom: "A"},
+ };
+ assert.equal(clearDisallowedKeymapItems(keymap, true), true);
+ assert.deepEqual(keymap.invalid, {default: "", custom: ""});
+ });
+});
diff --git a/app/src/util/hotKeyPolicy.ts b/app/src/util/hotKeyPolicy.ts
new file mode 100644
index 0000000000..fd1629247c
--- /dev/null
+++ b/app/src/util/hotKeyPolicy.ts
@@ -0,0 +1,59 @@
+const MODIFIER_KEYS = "⌃⌥⇧⌘";
+const NON_CHARACTER_KEYS = new Set(["←", "↑", "→", "↓", "⇥", "⌫", "⌦", "↩"]);
+
+export const isDisallowedTextInputHotkey = (hotkey: string) => {
+ let mainKeyIndex = 0;
+ while (mainKeyIndex < hotkey.length && MODIFIER_KEYS.includes(hotkey[mainKeyIndex])) {
+ mainKeyIndex++;
+ }
+ const modifiers = hotkey.slice(0, mainKeyIndex);
+ if (modifiers.includes("⌃") || modifiers.includes("⌥") || modifiers.includes("⌘")) {
+ return false;
+ }
+ const mainKey = hotkey.slice(mainKeyIndex).normalize();
+ return Array.from(mainKey).length === 1 && !NON_CHARACTER_KEYS.has(mainKey);
+};
+
+export const clearDisallowedTextInputHotkey = (hotkey: string) =>
+ isDisallowedTextInputHotkey(hotkey) ? "" : hotkey;
+
+export const normalizePluginHotkey = (hotkey: unknown, customHotkey?: unknown) => {
+ const ignoredHotkeys = new Set<string>();
+ const sanitizeHotkey = (value: string) => {
+ const sanitized = clearDisallowedTextInputHotkey(value);
+ if (sanitized !== value) {
+ ignoredHotkeys.add(value);
+ }
+ return sanitized;
+ };
+ const defaultHotkey = typeof hotkey === "string" ? sanitizeHotkey(hotkey) : "";
+ return {
+ defaultHotkey,
+ customHotkey: typeof customHotkey === "string" ? sanitizeHotkey(customHotkey) : defaultHotkey,
+ ignoredHotkeys: Array.from(ignoredHotkeys),
+ };
+};
+
+export const clearDisallowedKeymapItems = (
+ keymap: Record<string, {custom?: unknown; default?: unknown}> | undefined,
+ includeDefault = false,
+) => {
+ let changed = false;
+ Object.values(keymap || {}).forEach((item) => {
+ if (typeof item.custom === "string") {
+ const custom = clearDisallowedTextInputHotkey(item.custom);
+ if (custom !== item.custom) {
+ item.custom = custom;
+ changed = true;
+ }
+ }
+ if (includeDefault && typeof item.default === "string") {
+ const defaultHotkey = clearDisallowedTextInputHotkey(item.default);
+ if (defaultHotkey !== item.default) {
+ item.default = defaultHotkey;
+ changed = true;
+ }
+ }
+ });
+ return changed;
+};