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: Clear modifier state on mindmap key release https://github.com/siyuan-note/siyuan/issues/21047
Daniel80948712c731
diff --git a/app/src/block/popover.test.ts b/app/src/block/popover.test.tsindex 8f1fad0af0..932fdc2eea 100644--- a/app/src/block/popover.test.ts+++ b/app/src/block/popover.test.ts@@ -5,9 +5,10 @@ import {tmpdir} from "node:os";import * as path from "node:path";import {promisify} from "node:util";import test from "node:test";-import {ScriptTarget, transpileModule} from "typescript";+import {createSourceFile, isCallExpression, isClassDeclaration, isConstructorDeclaration,+ isExpressionStatement, isVariableStatement, ScriptTarget, transpileModule} from "typescript";-const browserCases = async (source: string) => {+const browserCases = async (source: string, keyboardSource: string) => {const check = require("node:assert/strict");const requests: ((response: unknown) => void)[] = [];let tooltip = "";@@ -316,10 +317,121 @@ const browserCases = async (source: string) => {check.equal(tooltip, "", "fragment editors continue to hide the list action tooltip");editor.remove();fragment.remove();++ window.siyuan.blockPanels.slice().forEach(panel => panel.destroy());+ window.siyuan.dialogs = [];+ let mac = true;+ const keyboard = new Function("isMac", "showPopover", "Constants", keyboardSource ++ "; return {filterHotkey, windowKeyUp, bindMindmapKeyboard};")(+ () => mac, api.showPopover, {KEYCODELIST: {}});+ keyboard.bindMindmapKeyboard(root);+ const outer = document.createElement("div");+ outer.className = "protyle-wysiwyg";+ document.body.append(outer);+ outer.append(root);+ let outerReleases = 0;+ outer.addEventListener("keyup", () => outerReleases++);+ const nodeEditor = document.createElement("div");+ nodeEditor.className = "mindmap-view__editor";+ nodeEditor.contentEditable = "true";+ // 节点编辑器在冒泡阶段隔离按键释放,容器仍需同步修饰键状态。+ nodeEditor.addEventListener("keyup", event => event.stopPropagation());+ root.append(nodeEditor);+ const keydown = (event: KeyboardEvent) => keyboard.filterHotkey(event, {});+ const keyup = (event: KeyboardEvent) => keyboard.windowKeyUp({}, event);+ window.addEventListener("keydown", keydown);+ window.addEventListener("keyup", keyup);+ for (const platform of ["mac", "windows"]) {+ mac = platform === "mac";+ const key = mac ? "Meta" : "Control";+ const modifier = mac ? {metaKey: true} : {ctrlKey: true};+ for (const target of [root, nodeEditor]) {+ window.siyuan.config.editor.floatWindowMode = 1;+ hover();+ check.equal(window.siyuan.blockPanels.length, 0, `${platform}: hover alone does not open a panel`);+ target.dispatchEvent(new KeyboardEvent("keydown", {key, ...modifier, bubbles: true}));+ check.equal(window.siyuan.ctrlIsPressed, true);+ check.equal(window.siyuan.blockPanels.length, 1, `${platform}: pressing the modifier over a reference opens it`);+ target.dispatchEvent(new KeyboardEvent("keyup", {key, bubbles: true}));+ check.equal(window.siyuan.ctrlIsPressed, false, `${platform}: releasing inside a mindmap clears the modifier`);+ document.body.dispatchEvent(new MouseEvent("mouseover", {bubbles: true}));+ await settle();+ hover();+ await settle();+ check.equal(window.siyuan.blockPanels.length, 0, `${platform}: released modifiers cannot trigger another panel`);+ hover();+ target.dispatchEvent(new KeyboardEvent("keydown", {key, ...modifier, bubbles: true}));+ check.equal(window.siyuan.blockPanels.length, 1, `${platform}: the next modifier press still works`);+ target.dispatchEvent(new KeyboardEvent("keyup", {key, bubbles: true}));+ document.body.dispatchEvent(new MouseEvent("mouseover", {bubbles: true}));+ await settle();+ check.equal(window.siyuan.blockPanels.length, 0);+ }+ }+ for (const key of ["Shift", "Alt"]) {+ const field = key === "Shift" ? "shiftIsPressed" : "altIsPressed";+ nodeEditor.dispatchEvent(new KeyboardEvent("keydown", {+ key, shiftKey: key === "Shift", altKey: key === "Alt", bubbles: true,+ }));+ check.equal(window.siyuan[field], true);+ nodeEditor.dispatchEvent(new KeyboardEvent("keyup", {key, bubbles: true}));+ check.equal(window.siyuan[field], false, `${key} cannot remain pressed after node editing`);+ check.equal(document.body.classList.contains("body--shift-pressed"), false);+ }+ window.siyuan.config.editor.floatWindowMode = 0;+ hover();+ await settle();+ check.equal(window.siyuan.blockPanels.length, 1, "ordinary hover mode still opens a panel");+ window.siyuan.blockPanels[0].destroy();+ window.siyuan.config.editor.floatWindowMode = 2;+ hover();+ await settle();+ check.equal(window.siyuan.blockPanels.length, 0, "disabled mode never opens a panel");+ check.equal(outerReleases, 0, "mindmap key releases do not reach the source editor");+ window.removeEventListener("keydown", keydown);+ window.removeEventListener("keyup", keyup);+ outer.remove();return "Popover interaction cases passed";};-test("mindmap interaction cancels pending popovers and preserves hover preferences and pinned panels", {+const getKeyboardSource = () => {+ const read = (file: string) => createSourceFile(file, readFileSync(path.join(__dirname, file), "utf8"),+ ScriptTarget.Latest, true);+ const variables = (file: string, names: string[]) => {+ const source = read(file);+ const statements = source.statements.filter(statement => isVariableStatement(statement) &&+ statement.declarationList.declarations.some(declaration => names.includes(declaration.name.getText(source))));+ assert.equal(statements.length, names.length);+ return statements.map(statement => statement.getText(source).replace(/^export /, "")).join("\n");+ };+ const controllerSource = read("../protyle/render/listMindmap/index.ts");+ const controller = controllerSource.statements.find(isClassDeclaration);+ const controllerConstructor = controller.members.find(isConstructorDeclaration);+ const releases = controllerConstructor.body.statements.filter(statement => isExpressionStatement(statement) &&+ isCallExpression(statement.expression) &&+ statement.expression.expression.getText(controllerSource) === "this.host.addEventListener" &&+ statement.expression.arguments[0]?.getText(controllerSource) === '"keyup"');+ const viewSource = read("../protyle/render/listMindmap/view.ts");+ const view = viewSource.statements.find(isClassDeclaration);+ const viewConstructor = view.members.find(isConstructorDeclaration);+ const isolation = viewConstructor.body.statements.find(statement =>+ statement.getText(viewSource).includes('"compositionend"') && statement.getText(viewSource).includes(".forEach"));+ assert.ok(isolation);+ return transpileModule([+ variables("../protyle/util/compatibility.ts", ["isOnlyMeta", "isNotCtrl"]),+ variables("../boot/globalEvent/commonHotkey.ts", ["filterHotkey"]),+ variables("../boot/globalEvent/keyup.ts", ["windowKeyUp"]),+ `const bindMindmapKeyboard = host => {+ const options = {host};+ (function () {+ ${releases.map(statement => statement.getText(controllerSource)).join("\n")}+ ${isolation.getText(viewSource)}+ }).call({host, options: {}, listen: (target, type, handler, options) => target.addEventListener(type, handler, options)});+ };`,+ ].join("\n"), {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;+};++test("mindmap interactions release modifiers and preserve hover preferences and pinned panels", {skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,timeout: 20000,}, async () => {@@ -337,7 +449,7 @@ app.whenReady().then(async () => {try {await win.loadURL("data:text/html,<html><body></body></html>");console.log(await win.webContents.executeJavaScript(${JSON.stringify(- `const __name = value => value; (${browserCases.toString()})(${JSON.stringify(source)})`)}));+ `const __name = value => value; (${browserCases.toString()})(${JSON.stringify(source)}, ${JSON.stringify(getKeyboardSource())})`)}));win.destroy();app.exit(0);} catch (error) {diff --git a/app/src/protyle/render/listMindmap/index.ts b/app/src/protyle/render/listMindmap/index.tsindex 7a7e3e0a49..ee3b09a885 100644--- a/app/src/protyle/render/listMindmap/index.ts+++ b/app/src/protyle/render/listMindmap/index.ts@@ -196,6 +196,13 @@ class ListMindmapController {this.host.className = "mindmap-view";syncListMindmapHeight(list, this.host);this.host.contentEditable = "false";+ // 节点编辑器和画布隔离 keyup,捕获阶段同步全局修饰键释放状态。+ this.host.addEventListener("keyup", () => {+ window.siyuan.ctrlIsPressed = false;+ window.siyuan.shiftIsPressed = false;+ window.siyuan.altIsPressed = false;+ document.body.classList.remove("body--shift-pressed");+ }, {capture: true});const renderGutter = (event: PointerEvent) => {if (!owner.options.render.gutter ||!owner.gutter || this.host.classList.contains("fullscreen") ||