Skip to content

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

:bug: Restore document shortcuts when body has focus https://github.com/siyuan-note/siyuan/issues/19406

Daniel4e7110fd9f18
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts
index 090a0593f1..6c23e6db63 100644
--- a/app/src/boot/globalEvent/keydown.ts
+++ b/app/src/boot/globalEvent/keydown.ts
@@ -1302,9 +1302,10 @@ export const windowKeyDown = (app: App, event: KeyboardEvent) => {
}
// 当前焦点范围先处理快捷键,未命中再按固定顺序处理通用操作。
- // 面板通过活动样式记录焦点,点击后按键事件的目标可能是 body。
+ // 按键目标为 body 时,优先使用活动面板,否则回退到活动窗口的当前页签。
const shortcutTarget = event.target === document.body ?
- document.querySelector<HTMLElement>(".layout__tab--active") || document.body : event.target as HTMLElement;
+ document.querySelector<HTMLElement>(".layout__tab--active") || getActiveTab()?.panelElement || document.body :
+ event.target as HTMLElement;
if (!shortcutTarget.closest("input, textarea, .b3-menu, .av__panel, .av__mask")) {
if (shortcutTarget.closest(".protyle") && editKeydown(app, event)) {
return;
diff --git a/app/src/boot/globalEvent/readonlyKeydown.test.ts b/app/src/boot/globalEvent/readonlyKeydown.test.ts
index 5b866dc2ed..eca11cb216 100644
--- a/app/src/boot/globalEvent/readonlyKeydown.test.ts
+++ b/app/src/boot/globalEvent/readonlyKeydown.test.ts
@@ -65,6 +65,7 @@ const globalFixture = (disabled: boolean, hasRange = true, foreignRange = false)
isOnlyMeta: () => false,
matchHotKey: (binding: string, event: {key: string}) => binding === event.key,
execByCommand: ({command}: {command: string}) => calls.push(command),
+ onlyProtyleCommand: ({command}: {command: string}) => calls.push(command),
});
return {edit: (event: any) => edit({}, event), calls};
};
@@ -109,6 +110,52 @@ test("editable undo still executes", () => {
}
});
+test("document readonly shortcut works without a body selection", () => {
+ for (const disabled of [false, true]) {
+ for (const [hasRange, foreignRange] of [[true, false], [false, false], [true, true]]) {
+ const fixture = globalFixture(disabled, hasRange, foreignRange);
+ assert.equal(fixture.edit(keyboardEvent("switchReadonly")), true);
+ assert.deepEqual(fixture.calls, ["switchReadonly"]);
+ }
+ }
+});
+
+test("body shortcuts follow the current document while preserving focused panel priority", () => {
+ const calls: string[] = [];
+ const element = (selector: string) => ({closest: (value: string) => value.split(", ").includes(selector)});
+ const body = element("body");
+ const firstDocument = element(".protyle");
+ const secondDocument = element(".protyle");
+ const filePanel = element(".sy__file");
+ let activeDocument = firstDocument;
+ let activePanel: ReturnType<typeof element> | undefined = undefined;
+ const dispatch = loadFunction("src/boot/globalEvent/keydown.ts", "windowKeyDown", {
+ filterHotkey: () => false,
+ switchDialog: undefined,
+ searchKeydown: () => false,
+ isWindow: () => false,
+ bindMenuKeydown: () => false,
+ bindAVPanelKeydown: () => false,
+ document: {body, querySelector: () => activePanel},
+ getActiveTab: () => ({panelElement: activeDocument}),
+ editKeydown: () => {
+ calls.push(activeDocument === firstDocument ? "first" : "second");
+ return true;
+ },
+ fileTreeKeydown: () => {
+ calls.push("files");
+ return true;
+ },
+ });
+ const event = {...keyboardEvent("switchReadonly"), target: body};
+ dispatch({}, event);
+ activeDocument = secondDocument;
+ dispatch({}, event);
+ activePanel = filePanel;
+ dispatch({}, event);
+ assert.deepEqual(calls, ["first", "second", "files"]);
+});
+
test("native command availability protects readonly content across command panel and shortcuts", () => {
const matches = loadFunction("src/command/nativeCommands.ts", "matchesContext", {});
for (const legacyId of ["move", "addToDatabase"]) {