Skip to content

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

:bug: Restore read-only cross-block selection with Escape https://github.com/siyuan-note/siyuan/issues/19558

Danield96ac603ca8e
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts
index 2973fc82e4..0055692dcd 100644
--- a/app/src/boot/globalEvent/keydown.ts
+++ b/app/src/boot/globalEvent/keydown.ts
@@ -92,15 +92,23 @@ const EDITOR_FONT_SIZE_COMMANDS: Array<{
{command: "resetEditorFontSize", action: "reset"},
];
-const selectReadonlyBlocksByRange = (range: Range) => {
+const getReadonlyBlockSelectionProtyle = (range: Range) => {
const startElement = hasClosestBlock(range.startContainer);
const endElement = hasClosestBlock(range.endContainer);
if (!startElement || !endElement || startElement === endElement) {
- return false;
+ return;
}
const protyle = getAllEditor().find(item => item.protyle.wysiwyg.element.contains(startElement) &&
item.protyle.wysiwyg.element.contains(endElement))?.protyle;
if (!protyle?.disabled) {
+ return;
+ }
+ return protyle;
+};
+
+const selectReadonlyBlocksByRange = (range: Range) => {
+ const protyle = getReadonlyBlockSelectionProtyle(range);
+ if (!protyle) {
return false;
}
hideElements(["toolbar", "hint", "util", "select"], protyle);
@@ -1311,12 +1319,21 @@ export const windowKeyDown = (app: App, event: KeyboardEvent) => {
return;
}
+ let readonlyBlockSelection: IProtyle;
+ if (event.key === "Escape" && !event.isComposing && !event.repeat) {
+ const selection = getSelection();
+ if (selection.rangeCount > 0) {
+ readonlyBlockSelection = getReadonlyBlockSelectionProtyle(selection.getRangeAt(0));
+ }
+ }
+
// 当前焦点范围先处理快捷键,未命中再按固定顺序处理通用操作。
// 按键目标为 body 时,优先使用活动面板,否则回退到活动窗口的当前页签。
const shortcutTarget = event.target === document.body ?
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")) {
+ // 只读正文无法获得焦点,跨块选区的 Esc 不应按上一次活动面板分派。
+ if (!readonlyBlockSelection && !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 65b6426f6b..5892564fe5 100644
--- a/app/src/boot/globalEvent/readonlyKeydown.test.ts
+++ b/app/src/boot/globalEvent/readonlyKeydown.test.ts
@@ -223,6 +223,90 @@ test("body shortcuts follow the current document while preserving focused panel
assert.deepEqual(calls, ["first", "second", "files"]);
});
+test("readonly cross-block Escape bypasses stale panel shortcut focus", () => {
+ const calls: string[] = [];
+ const body = {tagName: "BODY", closest: (): null => null};
+ const filePanel = {
+ closest: (selector: string) => selector.split(", ").some(value =>
+ [".layout__tab--active", ".sy__file"].includes(value)) ? filePanel : null,
+ };
+ const startContainer = {};
+ const endContainer = {};
+ const startBlock = {};
+ const endBlock = {};
+ const range = {startContainer, endContainer};
+ const protyle = {
+ disabled: true,
+ wysiwyg: {element: {contains: (element: unknown) => element === startBlock || element === endBlock}},
+ };
+ const selection = {rangeCount: 1, getRangeAt: () => range};
+ const globals = {
+ getSelection: () => selection,
+ hasClosestBlock: (element: unknown) => element === startContainer ? startBlock : endBlock,
+ getAllEditor: () => [{protyle}],
+ hideElements: () => calls.push("hide"),
+ selectBlocksByRange: () => calls.push("select"),
+ };
+ const getReadonlyBlockSelectionProtyle = loadFunction("src/boot/globalEvent/keydown.ts",
+ "getReadonlyBlockSelectionProtyle", globals);
+ const selectReadonlyBlocksByRange = loadFunction("src/boot/globalEvent/keydown.ts",
+ "selectReadonlyBlocksByRange", {...globals, getReadonlyBlockSelectionProtyle});
+ const dispatch = loadFunction("src/boot/globalEvent/keydown.ts", "windowKeyDown", {
+ ...globals,
+ getReadonlyBlockSelectionProtyle,
+ selectReadonlyBlocksByRange,
+ filterHotkey: () => false,
+ switchDialog: undefined,
+ searchKeydown: () => false,
+ isWindow: () => false,
+ bindMenuKeydown: () => false,
+ bindAVPanelKeydown: () => false,
+ document: {
+ body,
+ activeElement: null,
+ querySelector: (selector: string) => selector === ".layout__tab--active" ? filePanel : null,
+ },
+ getActiveTab: (): null => null,
+ editKeydown: () => false,
+ fileTreeKeydown: () => {
+ calls.push("file");
+ return true;
+ },
+ panelTreeKeydown: () => false,
+ EDITOR_FONT_SIZE_COMMANDS: [],
+ getKeymapBindings: (): string[] => [],
+ matchHotKey: () => false,
+ isNotCtrl: () => true,
+ hasClosestByClassName: (_element: unknown, className: string) => className === "protyle-content",
+ getAllDocks: (): unknown[] => [],
+ formatPainter: {deactivate: () => false},
+ cancelDrag: (): void => undefined,
+ window: {
+ siyuan: {
+ config: {readonly: false, keymap: {general: new Proxy({}, {get: () => ({})})}},
+ menus: {menu: {element: {classList: {contains: () => true}}}},
+ dialogs: [],
+ blockPanels: [],
+ backStack: [],
+ },
+ },
+ });
+ const event = {
+ ...keyboardEvent("Escape"),
+ target: body,
+ repeat: false,
+ isComposing: false,
+ keyCode: 27,
+ ctrlKey: false,
+ metaKey: false,
+ altKey: false,
+ shiftKey: false,
+ };
+ dispatch({}, event);
+ assert.deepEqual(calls, ["hide", "select"]);
+ assert.equal(event.defaultPrevented, true);
+});
+
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"]) {