Skip to content

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

:bug: Restore block menus after undoing mind map conversions https://github.com/siyuan-note/siyuan/issues/18831

Vanessa4960cde1f3db
diff --git a/app/src/protyle/gutter/click.test.ts b/app/src/protyle/gutter/click.test.ts
new file mode 100644
index 0000000000..8fb5ab7b18
--- /dev/null
+++ b/app/src/protyle/gutter/click.test.ts
@@ -0,0 +1,80 @@
+import * as assert from "node:assert/strict";
+import test from "node:test";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import {createSourceFile, forEachChild, isCallExpression, ScriptTarget, transpileModule} from "typescript";
+
+const source = createSourceFile("index.ts", readFileSync(join(__dirname, "index.ts"), "utf8"), ScriptTarget.Latest, true);
+const listeners = new Map<string, string>();
+const collect = (node: import("typescript").Node) => {
+ if (isCallExpression(node) && node.expression.getText(source) === "this.element.addEventListener") {
+ const type = node.arguments[0].getText(source).slice(1, -1);
+ if (type === "click" || type === "contextmenu") {
+ listeners.set(type, node.arguments[1].getText(source));
+ }
+ }
+ forEachChild(node, collect);
+};
+collect(source);
+assert.equal(listeners.size, 2);
+
+const fixture = (type: string, mobile: boolean, cached: Record<string, boolean>) => {
+ const calls: string[] = [];
+ const noop = () => {};
+ const button = {
+ dataset: {type: "NodeList"},
+ classList: {contains: () => false},
+ getAttribute: (key: string) => key === "data-node-id" ? "list-id" : "NodeList",
+ getBoundingClientRect: () => ({left: 10, bottom: 30}),
+ };
+ const menu = {
+ element: {setAttribute: noop},
+ fullscreen: () => calls.push("fullscreen"),
+ popup: () => calls.push("popup"),
+ };
+ const dependencies = {
+ window: {siyuan: {...cached, menus: {menu}}},
+ hasClosestByTag: () => button,
+ hideTooltip: noop,
+ clearSelect: noop,
+ isOnlyMeta: (event: MouseEvent) => (event.metaKey || event.ctrlKey) && !event.altKey && !event.shiftKey,
+ isEncryptedBox: () => false,
+ zoomOut: () => calls.push("zoom"),
+ openAttr: () => calls.push("attributes"),
+ hasTopClosestByClassName: () => false,
+ restoreGutterRange: noop,
+ };
+ const callback = listeners.get(type).replace(/\/\/\/ #if MOBILE\n([\s\S]*?)\/\/\/ #else\n([\s\S]*?)\/\/\/ #endif/g,
+ (_match, mobileSource: string, desktopSource: string) => mobile ? mobileSource : desktopSource);
+ const js = transpileModule(`const listener = ${callback};`, {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+ const gutter = {renderMenu: () => calls.push("menu"), getNodeElement: () => button};
+ const handler = new Function("protyle", ...Object.keys(dependencies), `${js}\nreturn listener;`)
+ .call(gutter, {wysiwyg: {element: {}}, toolbar: {range: {}}, options: {}}, ...Object.values(dependencies));
+ return (modifiers: Record<string, boolean> = {}) => {
+ handler({target: button, preventDefault: noop, stopPropagation: noop,
+ ctrlKey: false, metaKey: false, altKey: false, shiftKey: false, ...modifiers});
+ return calls;
+ };
+};
+
+test("block menus open after mind map undo leaves cached modifier flags set", () => {
+ for (const mobile of [false, true]) {
+ for (const type of ["click", "contextmenu"]) {
+ for (const key of ["ctrlIsPressed", "shiftIsPressed", "altIsPressed"]) {
+ assert.deepEqual(fixture(type, mobile, {[key]: true})(), ["menu", mobile ? "fullscreen" : "popup"],
+ `${type}, mobile=${mobile}, stale ${key}`);
+ }
+ }
+ }
+});
+
+test("block icon clicks still honor modifiers carried by the mouse event", () => {
+ for (const mobile of [false, true]) {
+ assert.deepEqual(fixture("click", mobile, {})({metaKey: true}), ["zoom"]);
+ assert.deepEqual(fixture("click", mobile, {})({ctrlKey: true}), ["zoom"]);
+ assert.deepEqual(fixture("click", mobile, {})({shiftKey: true}), ["attributes"]);
+ for (const modifier of ["ctrlKey", "metaKey", "shiftKey", "altKey"]) {
+ assert.deepEqual(fixture("contextmenu", mobile, {})({[modifier]: true}), []);
+ }
+ }
+});
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 8c3d967a21..c5bd405970 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -592,7 +592,7 @@ export class Gutter {
} else if (event.shiftKey && !protyle.disabled && !isEncryptedBox(protyle.notebookId)) {
// 不使用 window.siyuan.shiftIsPressed ,否则窗口未激活时按 Shift 点击块标无法打开属性面板 https://github.com/siyuan-note/siyuan/issues/15075
openAttr(this.getNodeElement(protyle, buttonElement), "bookmark", protyle);
- } else if (!window.siyuan.ctrlIsPressed && !window.siyuan.altIsPressed && !window.siyuan.shiftIsPressed) {
+ } else if (!event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
this.renderMenu(protyle, buttonElement);
// https://ld246.com/article/1648433751993
if (!protyle.toolbar.range &&
@@ -615,7 +615,7 @@ export class Gutter {
if (!buttonElement || buttonElement.getAttribute("data-type") === "fold") {
return;
}
- if (!window.siyuan.ctrlIsPressed && !window.siyuan.altIsPressed && !window.siyuan.shiftIsPressed) {
+ if (!event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
hideTooltip();
clearSelect(["cell", "img"], protyle.wysiwyg.element);
const gutterRect = buttonElement.getBoundingClientRect();