An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Restore plugin editor command shortcuts in read-only mode https://github.com/siyuan-note/siyuan/issues/19652
Daniel1ac4d787ac03
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.tsindex 0055692dcd..513d57838f 100644--- a/app/src/boot/globalEvent/keydown.ts+++ b/app/src/boot/globalEvent/keydown.ts@@ -82,6 +82,7 @@ import {cancelDrag} from "./dragover";import {bindAVPanelKeydown} from "../../protyle/render/av/keydown";import {formatPainter} from "../../protyle/toolbar/FormatPainter";import {adjustEditorFontSize, type TEditorFontSizeAction} from "../../util/editorFontSize";+import {areProtylePluginExtensionsEnabled} from "../../protyle/runtimeCapabilities";const EDITOR_FONT_SIZE_COMMANDS: Array<{command: "increaseEditorFontSize" | "decreaseEditorFontSize" | "resetEditorFontSize",@@ -631,6 +632,22 @@ const editKeydown = (app: App, event: KeyboardEvent) => {}return true;}+ // 只读正文的快捷键使用所属编辑器上下文,插件命令仍遵循自身的可用性判断。+ if (protyle.disabled && !isFileFocus && !event.isComposing &&+ (target === document.body || protyle.wysiwyg.element.contains(target)) &&+ areProtylePluginExtensionsEnabled(protyle) &&+ dispatchPluginShortcut(app, event, "editorShortcut", () => {+ const context = captureCommandContext({app, source: "editorShortcut", protyle, range});+ // 无有效正文选区时,不携带上下文采集回退得到的历史选区。+ if (!range) {+ context.range = undefined;+ context.block = undefined;+ context.tableCell = undefined;+ }+ return context;+ })) {+ return true;+ }if (hasClosestByClassName(target, "protyle-title__input")) {return false;}diff --git a/app/src/boot/globalEvent/readonlyKeydown.test.ts b/app/src/boot/globalEvent/readonlyKeydown.test.tsindex 5892564fe5..8ebaa75c65 100644--- a/app/src/boot/globalEvent/readonlyKeydown.test.ts+++ b/app/src/boot/globalEvent/readonlyKeydown.test.ts@@ -51,7 +51,7 @@ 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 body = {localName: "div", closest: (): null => null};const range = {commonAncestorContainer: foreignRange ? {} : body};const protyle = {disabled,@@ -60,6 +60,7 @@ const globalFixture = (disabled: boolean, hasRange = true, foreignRange = false)preview: {element: {classList: {contains: () => true}}},getInstance: () => ({isFullscreen: () => false, setFullscreen: () => calls.push("fullscreen")}),element: {contains: (node: unknown) => node === body || (node as any)?.localName === "div"},+ wysiwyg: {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});@@ -67,7 +68,21 @@ const globalFixture = (disabled: boolean, hasRange = true, foreignRange = false)getAllEditor: () => [{protyle}],getSelection: () => ({rangeCount: hasRange ? 1 : 0, getRangeAt: () => range}),getActiveTab: (): null => null,- document: {querySelector: (): null => null},+ document: {body, querySelector: (): null => null},+ areProtylePluginExtensionsEnabled: () => true,+ captureCommandContext: (context: {range?: unknown}) => ({...context, range: context.range || range}),+ dispatchPluginShortcut: (_app: unknown, event: {key: string, preventDefault: () => void},+ source: string, capture: () => {protyle: unknown, range?: unknown}) => {+ if (event.key !== "pluginShortcut") {+ return false;+ }+ assert.equal(source, "editorShortcut");+ const context = capture();+ contexts.push({protyle: context.protyle, previousRange: context.range});+ calls.push("plugin");+ event.preventDefault();+ return true;+ },window: {siyuan: {config: {keymap: {general: bindings, editor: {general: bindings}}}}},hasClosestByClassName: () => false,isOnlyMeta: () => false,@@ -92,9 +107,49 @@ const globalFixture = (disabled: boolean, hasRange = true, foreignRange = false)};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};+ return {edit: (event: any) => edit({}, event), calls, contexts, protyle, range, body, globals};};+test("readonly plugin shortcuts receive the editor with a valid selection or no selection", () => {+ for (const bodyTarget of [false, true]) {+ for (const [hasRange, foreignRange] of [[true, false], [false, false], [true, true]]) {+ const fixture = globalFixture(true, hasRange, foreignRange);+ const event = keyboardEvent("pluginShortcut");+ if (bodyTarget) {+ event.target = fixture.body;+ }+ assert.equal(fixture.edit(event), true);+ assert.deepEqual(fixture.calls, ["plugin"]);+ assert.equal(event.defaultPrevented, true);+ assert.equal(fixture.contexts[0].protyle, fixture.protyle);+ assert.equal(fixture.contexts[0].previousRange, hasRange && !foreignRange ? fixture.range : undefined);+ }+ }+});++test("readonly plugin fallback excludes controls, composition, editable and restricted editors", () => {+ for (const control of [true, "button", '[role="tab"]', '[role="checkbox"]', "input", "textarea"]) {+ const fixture = globalFixture(true);+ const event = keyboardEvent("pluginShortcut", control);+ if (control === "input" || control === "textarea") {+ Object.assign(event.target, {tagName: control.toUpperCase()});+ }+ assert.equal(fixture.edit(event), false);+ assert.deepEqual(fixture.calls, []);+ }+ const editable = globalFixture(false);+ assert.equal(editable.edit(keyboardEvent("pluginShortcut")), false);+ const composing = globalFixture(true);+ assert.equal(composing.edit({...keyboardEvent("pluginShortcut"), isComposing: true}), false);+ const restricted = globalFixture(true);+ restricted.globals.areProtylePluginExtensionsEnabled = () => false;+ const edit = loadFunction("src/boot/globalEvent/keydown.ts", "editKeydown", {+ ...restricted.globals, documentKeydown: () => false,+ });+ assert.equal(edit({}, keyboardEvent("pluginShortcut")), false);+ assert.deepEqual(restricted.calls, []);+});+test("readonly content mutations are consumed before command execution, even without a valid selection", () => {for (const [hasRange, foreignRange] of [[true, false], [false, false], [true, true]]) {const fixture = globalFixture(true, hasRange, foreignRange);