An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:recycle: Separate document shortcuts from content editing https://github.com/siyuan-note/siyuan/issues/19530
Daniela8bd17689f11
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.tsindex 9b4067c5ae..2973fc82e4 100644--- a/app/src/boot/globalEvent/keydown.ts+++ b/app/src/boot/globalEvent/keydown.ts@@ -206,13 +206,140 @@ const dialogArrow = (app: App, element: HTMLElement, event: KeyboardEvent) => {}};+// 文档操作只依赖所属编辑器,可选选区仅用于正文中的引用定位和搜索。+const documentKeydown = (app: App, event: KeyboardEvent, protyle: IProtyle, range: Range, isFileFocus: boolean) => {+ if (!isFileFocus && matchHotKey(window.siyuan.config.keymap.general.replace, event)) {+ execByCommand({+ command: "replace",+ app,+ protyle,+ previousRange: range+ });+ event.preventDefault();+ return true;+ }+ if (!isFileFocus && matchHotKey(window.siyuan.config.keymap.general.search, event)) {+ execByCommand({+ command: "search",+ app,+ protyle,+ previousRange: range+ });+ event.preventDefault();+ return true;+ }+ if (!isFileFocus && matchHotKey(window.siyuan.config.keymap.editor.general.spaceRepetition, event) &&+ !window.siyuan.config.readonly && !isEncryptedBox(protyle.notebookId)) {+ fetchPost("/api/riff/getTreeRiffDueCards", {rootID: protyle.block.rootID}, (response) => {+ openCardByData(app, response.data, "doc", protyle.block.rootID, protyle.title?.editElement.textContent || window.siyuan.languages.untitled);+ });+ event.preventDefault();+ return true;+ }+ if (matchHotKey(window.siyuan.config.keymap.editor.general.exitFocus, event)) {+ event.preventDefault();+ zoomOut({protyle, id: protyle.block.rootID, focusId: protyle.block.id});+ return true;+ }+ if (matchHotKey(window.siyuan.config.keymap.editor.general.switchReadonly, event)) {+ event.preventDefault();+ onlyProtyleCommand({+ protyle,+ command: "switchReadonly",+ previousRange: range,+ });+ return true;+ }+ if (matchHotKey(window.siyuan.config.keymap.editor.general.switchAdjust, event)) {+ event.preventDefault();+ onlyProtyleCommand({+ protyle,+ command: "switchAdjust",+ previousRange: range,+ });+ return true;+ }++ if (matchHotKey(window.siyuan.config.keymap.editor.general.backlinks, event)) {+ event.preventDefault();+ if (range) {+ const refElement = hasClosestByAttribute(range.startContainer, "data-type", "block-ref");+ if (refElement) {+ openBacklink({+ app: protyle.app,+ blockId: refElement.dataset.id,+ notebookId: protyle.notebookId,+ });+ return true;+ }+ }+ openBacklink({+ app: protyle.app,+ blockId: protyle.block.id,+ rootId: protyle.block.rootID,+ notebookId: protyle.notebookId,+ useBlockId: protyle.block.showAll,+ title: protyle.title ? (protyle.title.editElement.textContent || window.siyuan.languages.untitled) : null,+ });+ return true;+ }+ if (matchHotKey(window.siyuan.config.keymap.editor.general.graphView, event)) {+ event.preventDefault();+ if (range) {+ const refElement = hasClosestByAttribute(range.startContainer, "data-type", "block-ref");+ if (refElement) {+ openGraph({+ app: protyle.app,+ blockId: refElement.dataset.id,+ notebookId: protyle.notebookId,+ });+ return true;+ }+ }+ openGraph({+ app: protyle.app,+ blockId: protyle.block.id,+ rootId: protyle.block.rootID,+ notebookId: protyle.notebookId,+ useBlockId: protyle.block.showAll,+ title: protyle.title ? (protyle.title.editElement.textContent || window.siyuan.languages.untitled) : null,+ });+ return true;+ }+ if (matchHotKey(window.siyuan.config.keymap.editor.general.outline, event)) {+ event.preventDefault();+ openOutline({+ app,+ rootId: protyle.block.rootID,+ notebookId: protyle.notebookId,+ title: protyle.options.render.title ? (protyle.title.editElement.textContent || window.siyuan.languages.untitled) : "",+ isPreview: !protyle.preview.element.classList.contains("fn__none")+ });+ return true;+ }+ if (matchHotKey(window.siyuan.config.keymap.editor.general.refresh, event)) {+ reloadProtyle(protyle, true);+ event.preventDefault();+ return true;+ }+ if (matchHotKey(window.siyuan.config.keymap.editor.general.fullscreen, event)) {+ const editor = protyle.getInstance();+ editor.setFullscreen(!editor.isFullscreen());+ event.preventDefault();+ return true;+ }+ if (!event.repeat && !protyle.options.backlinkData &&+ matchHotKey(window.siyuan.config.keymap.editor.general.editMode, event)) {+ toggleEditMode(protyle);+ saveLayout();+ event.preventDefault();+ return true;+ }+ return false;+};+const editKeydown = (app: App, event: KeyboardEvent) => {const eventTarget = event.target as HTMLElement;- // 页签标题允许切换当前文档只读状态,其它按键不使用正文中可能残留的选区执行编辑命令。- if (eventTarget.closest(".tabs-header") &&- !matchHotKey(window.siyuan.config.keymap.editor.general.switchReadonly, event)) {- return false;- }if (hasClosestByClassName(eventTarget, "sy__backlink--bottom", true) &&!hasClosestByClassName(eventTarget, "protyle", true)) {return false;@@ -326,29 +453,17 @@ const editKeydown = (app: App, event: KeyboardEvent) => {event.preventDefault();return true;}- // 焦点移动到不可编辑容器后,浏览器可能没有正文选区,或仍保留其它编辑器的选区。- if (!range || !protyle.element.contains(range.commonAncestorContainer)) {+ // 控件焦点不携带正文残留选区,文档命令仍使用事件所属编辑器。+ const isEditorControl = !!eventTarget.closest('.protyle-action, button, [role="tab"], [role="checkbox"]');+ if (isEditorControl || !range || !protyle.element.contains(range.commonAncestorContainer)) {range = undefined;}- if (!isFileFocus && matchHotKey(window.siyuan.config.keymap.general.replace, event)) {- execByCommand({- command: "replace",- app,- protyle,- previousRange: range- });- event.preventDefault();+ if (documentKeydown(app, event, protyle, range, isFileFocus)) {return true;}- if (!isFileFocus && matchHotKey(window.siyuan.config.keymap.general.search, event)) {- execByCommand({- command: "search",- app,- protyle,- previousRange: range- });- event.preventDefault();- return true;+ // 正文命令需要正文上下文,控件未处理的快捷键继续交给全局处理器。+ if (isEditorControl) {+ return false;}if (!isFileFocus && range && matchHotKey(window.siyuan.config.keymap.editor.general.quickMakeCard, event) && !window.siyuan.config.readonly && !isEncryptedBox(protyle.notebookId)) {if (protyle.title?.editElement.contains(range.startContainer)) {@@ -379,14 +494,6 @@ const editKeydown = (app: App, event: KeyboardEvent) => {event.preventDefault();return true;}- if (!isFileFocus && matchHotKey(window.siyuan.config.keymap.editor.general.spaceRepetition, event) &&- !window.siyuan.config.readonly && !isEncryptedBox(protyle.notebookId)) {- fetchPost("/api/riff/getTreeRiffDueCards", {rootID: protyle.block.rootID}, (response) => {- openCardByData(app, response.data, "doc", protyle.block.rootID, protyle.title?.editElement.textContent || window.siyuan.languages.untitled);- });- event.preventDefault();- return true;- }if (!isFileFocus && matchHotKey(window.siyuan.config.keymap.general.move, event)) {execByCommand({command: "move",@@ -434,93 +541,12 @@ const editKeydown = (app: App, event: KeyboardEvent) => {event.preventDefault();return;}- if (matchHotKey(window.siyuan.config.keymap.editor.general.exitFocus, event)) {- event.preventDefault();- zoomOut({protyle, id: protyle.block.rootID, focusId: protyle.block.id});- return true;- }if (range && matchHotKey(window.siyuan.config.keymap.editor.general.focusBreadcrumb, event)) {if (protyle.breadcrumb?.focus(range)) {event.preventDefault();return true;}}- if (matchHotKey(window.siyuan.config.keymap.editor.general.switchReadonly, event)) {- event.preventDefault();- onlyProtyleCommand({- protyle,- command: "switchReadonly",- previousRange: range,- });- return true;- }- if (matchHotKey(window.siyuan.config.keymap.editor.general.switchAdjust, event)) {- event.preventDefault();- onlyProtyleCommand({- protyle,- command: "switchAdjust",- previousRange: range,- });- return true;- }-- if (matchHotKey(window.siyuan.config.keymap.editor.general.backlinks, event)) {- event.preventDefault();- if (range) {- const refElement = hasClosestByAttribute(range.startContainer, "data-type", "block-ref");- if (refElement) {- openBacklink({- app: protyle.app,- blockId: refElement.dataset.id,- notebookId: protyle.notebookId,- });- return true;- }- }- openBacklink({- app: protyle.app,- blockId: protyle.block.id,- rootId: protyle.block.rootID,- notebookId: protyle.notebookId,- useBlockId: protyle.block.showAll,- title: protyle.title ? (protyle.title.editElement.textContent || window.siyuan.languages.untitled) : null,- });- return true;- }- if (matchHotKey(window.siyuan.config.keymap.editor.general.graphView, event)) {- event.preventDefault();- if (range) {- const refElement = hasClosestByAttribute(range.startContainer, "data-type", "block-ref");- if (refElement) {- openGraph({- app: protyle.app,- blockId: refElement.dataset.id,- notebookId: protyle.notebookId,- });- return true;- }- }- openGraph({- app: protyle.app,- blockId: protyle.block.id,- rootId: protyle.block.rootID,- notebookId: protyle.notebookId,- useBlockId: protyle.block.showAll,- title: protyle.title ? (protyle.title.editElement.textContent || window.siyuan.languages.untitled) : null,- });- return true;- }- if (matchHotKey(window.siyuan.config.keymap.editor.general.outline, event)) {- event.preventDefault();- openOutline({- app,- rootId: protyle.block.rootID,- notebookId: protyle.notebookId,- title: protyle.options.render.title ? (protyle.title.editElement.textContent || window.siyuan.languages.untitled) : "",- isPreview: !protyle.preview.element.classList.contains("fn__none")- });- return true;- }/// #if !MOBILEif (matchHotKey(window.siyuan.config.keymap.editor.general.copyRichText, event)) {if (!range || !hasClosestBlock(range.startContainer)) {@@ -567,24 +593,6 @@ const editKeydown = (app: App, event: KeyboardEvent) => {event.preventDefault();return true;}- if (matchHotKey(window.siyuan.config.keymap.editor.general.refresh, event)) {- reloadProtyle(protyle, true);- event.preventDefault();- return true;- }- if (matchHotKey(window.siyuan.config.keymap.editor.general.fullscreen, event)) {- const editor = protyle.getInstance();- editor.setFullscreen(!editor.isFullscreen());- event.preventDefault();- return true;- }- if (!event.repeat && !protyle.options.backlinkData &&- matchHotKey(window.siyuan.config.keymap.editor.general.editMode, event)) {- toggleEditMode(protyle);- saveLayout();- event.preventDefault();- return true;- }if (range && !isFileFocus && matchHotKey(window.siyuan.config.keymap.editor.general.copyBlockRef, event)) {event.preventDefault();event.stopPropagation();diff --git a/app/src/boot/globalEvent/readonlyKeydown.test.ts b/app/src/boot/globalEvent/readonlyKeydown.test.tsindex 79e12064a0..65b6426f6b 100644--- a/app/src/boot/globalEvent/readonlyKeydown.test.ts+++ b/app/src/boot/globalEvent/readonlyKeydown.test.ts@@ -17,9 +17,13 @@ const loadFunction = (path: string, name: string, globals: Record<string, unknowreturn exports.subject;};-const keyboardEvent = (key: string, header = false) => ({+const keyboardEvent = (key: string, control: boolean | string = false) => ({key,- target: {localName: "div", closest: () => header ? {} : null},+ target: {+ localName: "div",+ closest: (selector: string) => selector.split(", ").some(value =>+ control === true ? [".tabs-header", ".protyle-action"].includes(value) : control === value) ? {} : null,+ },defaultPrevented: false,propagationStopped: false,preventDefault() { this.defaultPrevented = true; },@@ -46,16 +50,20 @@ test("readonly body and tab header preserve keyboard defaults and bubbling", asyconst globalFixture = (disabled: boolean, hasRange = true, foreignRange = false) => {const calls: string[] = [];+ const contexts: Array<{protyle: unknown, previousRange?: unknown}> = [];const body = {};const range = {commonAncestorContainer: foreignRange ? {} : body};const protyle = {disabled,- options: {},+ options: {render: {}},+ block: {id: "doc", rootID: "doc"},+ preview: {element: {classList: {contains: () => true}}},+ getInstance: () => ({isFullscreen: () => false, setFullscreen: () => calls.push("fullscreen")}),element: {contains: (node: unknown) => node === body || (node as any)?.localName === "div"},undo: {undo: () => calls.push("undo"), redo: () => calls.push("redo")},};const bindings = new Proxy({}, {get: (_target, key) => key});- const edit = loadFunction("src/boot/globalEvent/keydown.ts", "editKeydown", {+ const globals = {getAllEditor: () => [{protyle}],getSelection: () => ({rangeCount: hasRange ? 1 : 0, getRangeAt: () => range}),getActiveTab: (): null => null,@@ -64,10 +72,27 @@ const globalFixture = (disabled: boolean, hasRange = true, foreignRange = false)hasClosestByClassName: () => 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};+ execByCommand: (context: {command: string, protyle: unknown, previousRange?: unknown}) => {+ calls.push(context.command);+ contexts.push(context);+ },+ onlyProtyleCommand: (context: {command: string, protyle: unknown, previousRange?: unknown}) => {+ calls.push(context.command);+ contexts.push(context);+ },+ isEncryptedBox: () => false,+ fetchPost: () => calls.push("spaceRepetition"),+ zoomOut: () => calls.push("exitFocus"),+ openBacklink: () => calls.push("backlinks"),+ openGraph: () => calls.push("graphView"),+ openOutline: () => calls.push("outline"),+ reloadProtyle: () => calls.push("refresh"),+ toggleEditMode: () => calls.push("editMode"),+ saveLayout: (): void => undefined,+ };+ const documentKeydown = loadFunction("src/boot/globalEvent/keydown.ts", "documentKeydown", globals);+ const edit = loadFunction("src/boot/globalEvent/keydown.ts", "editKeydown", {...globals, documentKeydown});+ return {edit: (event: any) => edit({}, event), calls, contexts, protyle, range};};test("readonly content mutations are consumed before command execution, even without a valid selection", () => {@@ -120,18 +145,48 @@ test("document readonly shortcut works without a body selection", () => {}});-test("tab headers can toggle document readonly without using a body selection", () => {+test("editor controls dispatch document commands without carrying stale body selections", () => {for (const disabled of [false, true]) {for (const [hasRange, foreignRange] of [[true, false], [false, false], [true, true]]) {- const fixture = globalFixture(disabled, hasRange, foreignRange);- const event = keyboardEvent("switchReadonly", true);- assert.equal(fixture.edit(event), true);- assert.equal(event.defaultPrevented, true);- assert.deepEqual(fixture.calls, ["switchReadonly"]);+ for (const command of ["switchReadonly", "switchAdjust", "search", "replace", "spaceRepetition",+ "exitFocus", "backlinks", "graphView", "outline", "refresh", "fullscreen", "editMode"]) {+ const fixture = globalFixture(disabled, hasRange, foreignRange);+ const event = keyboardEvent(command, true);+ assert.equal(fixture.edit(event), true, command);+ assert.equal(event.defaultPrevented, true, command);+ assert.deepEqual(fixture.calls, [command]);+ for (const context of fixture.contexts) {+ assert.equal(context.protyle, fixture.protyle);+ assert.equal(context.previousRange, undefined);+ }+ }+ }+ }+});++test("editor controls cannot execute body commands even with a retained selection", () => {+ for (const disabled of [false, true]) {+ for (const control of [true, "button", '[role="tab"]', '[role="checkbox"]']) {+ const fixture = globalFixture(disabled);+ for (const command of ["undo", "redo", "duplicate", "duplicateCompletely", "quickMakeCard", "move",+ "addToDatabase", "copyRichText", "copyPlainText", "copyBlockRef", "focusBreadcrumb", "customShortcut"]) {+ fixture.edit(keyboardEvent(command, control));+ }+ assert.deepEqual(fixture.calls, []);+ assert.equal(fixture.edit(keyboardEvent("switchAdjust", control)), true);+ assert.deepEqual(fixture.calls, ["switchAdjust"]);}}});+test("document search retains the selection only for body focus in the same editor", () => {+ for (const foreignRange of [false, true]) {+ const fixture = globalFixture(false, true, foreignRange);+ assert.equal(fixture.edit(keyboardEvent("search")), true);+ assert.equal(fixture.contexts[0].previousRange, foreignRange ? undefined : fixture.range);+ }+});+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)});