Skip to content

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

:bug: Align panel context menus and reopen tag and bookmark menus https://github.com/siyuan-note/siyuan/issues/19269

Danielf1742c6325e4
diff --git a/app/src/layout/dock/Inbox.ts b/app/src/layout/dock/Inbox.ts
index c279a7030f..aac1d0d2d5 100644
--- a/app/src/layout/dock/Inbox.ts
+++ b/app/src/layout/dock/Inbox.ts
@@ -307,9 +307,9 @@ ${data.shorthandContent}
separatorPosition: "top",
});
const button = (event.target as Element).closest("[data-type='more']");
- const rect = !itemElement ? button?.getBoundingClientRect() : undefined;
+ const rect = (itemElement || button)?.getBoundingClientRect();
window.siyuan.menus.menu.popup({
- x: rect ? rect.left : event.clientX,
+ x: !itemElement && rect ? rect.left : event.clientX,
y: rect ? rect.bottom : event.clientY + 16,
h: rect ? rect.height : 0,
});
diff --git a/app/src/layout/dock/Outline.ts b/app/src/layout/dock/Outline.ts
index 74d9938bc4..ee094727b9 100644
--- a/app/src/layout/dock/Outline.ts
+++ b/app/src/layout/dock/Outline.ts
@@ -1402,9 +1402,11 @@ export class Outline extends Model {
}
}).element);
+ const rect = element.getBoundingClientRect();
window.siyuan.menus.menu.popup({
x: event.clientX,
- y: event.clientY
+ y: rect.bottom,
+ h: rect.height,
});
}
diff --git a/app/src/menus/bookmark.ts b/app/src/menus/bookmark.ts
index aa6e0fafe7..b09ab65cc0 100644
--- a/app/src/menus/bookmark.ts
+++ b/app/src/menus/bookmark.ts
@@ -9,7 +9,7 @@ import {MobileBookmarks} from "../mobile/dock/MobileBookmarks";
import {Constants} from "../constants";
export const openBookmarkMenu = (element: HTMLElement, event: MouseEvent, bookmarkObj: Bookmark | MobileBookmarks) => {
- if (!window.siyuan.menus.menu.element.classList.contains("fn__none") &&
+ if (event.type !== "contextmenu" && !window.siyuan.menus.menu.element.classList.contains("fn__none") &&
window.siyuan.menus.menu.element.getAttribute("data-name") === Constants.MENU_BOOKMARK) {
window.siyuan.menus.menu.remove();
return;
@@ -76,5 +76,7 @@ export const openBookmarkMenu = (element: HTMLElement, event: MouseEvent, bookma
}).element);
}
window.siyuan.menus.menu.element.setAttribute("data-name", Constants.MENU_BOOKMARK);
- window.siyuan.menus.menu.popup({x: event.clientX - 11, y: event.clientY + 11, h: 22, w: 12});
+ const button = event.type === "contextmenu" ? null : (event.target as Element).closest(".b3-list-item__action");
+ const rect = (button || element).getBoundingClientRect();
+ window.siyuan.menus.menu.popup({x: button ? rect.left : event.clientX, y: rect.bottom, h: rect.height});
};
diff --git a/app/src/menus/panelMenus.test.ts b/app/src/menus/panelMenus.test.ts
new file mode 100644
index 0000000000..1bbfff555d
--- /dev/null
+++ b/app/src/menus/panelMenus.test.ts
@@ -0,0 +1,74 @@
+import * as assert from "node:assert/strict";
+import test from "node:test";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import {runInNewContext} from "node:vm";
+import * as ts from "typescript";
+
+const loadMenu = (name: "tag" | "bookmark") => {
+ let visible = false;
+ let menuName: string;
+ const positions: {x: number, y: number, h: number}[] = [];
+ const menu = {
+ element: {
+ classList: {contains: () => !visible},
+ getAttribute: () => menuName,
+ setAttribute: (_key: string, value: string) => { menuName = value; },
+ },
+ remove: () => { visible = false; },
+ append: () => {},
+ popup: (position: {x: number, y: number, h: number}) => {
+ visible = true;
+ positions.push({...position});
+ },
+ };
+ const exports: Record<string, (...args: unknown[]) => void> = {};
+ const source = ts.transpileModule(readFileSync(join(__dirname, `${name}.ts`), "utf8"), {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
+ }).outputText;
+ runInNewContext(source, {
+ exports,
+ window: {siyuan: {config: {readonly: false}, menus: {menu}, languages: {}}},
+ require: (path: string) => {
+ if (path === "./Menu") { return {MenuItem: class { element = {}; }}; }
+ if (path.endsWith("/constants")) {
+ return {Constants: {MENU_TAG: "tag", MENU_BOOKMARK: "bookmark"}};
+ }
+ return {};
+ },
+ });
+ return {open: exports[name === "tag" ? "openTagMenu" : "openBookmarkMenu"], positions,
+ isVisible: () => visible};
+};
+
+for (const name of ["tag", "bookmark"] as const) {
+ test(`${name} right clicks reopen at the current row and pointer`, () => {
+ const {open, positions, isVisible} = loadMenu(name);
+ const row = (bottom: number) => ({getAttribute: (): string | null => null,
+ getBoundingClientRect: () => ({left: 0, bottom, height: 28})});
+ const firstRow = row(120);
+ const event = {type: "contextmenu", clientX: 80, clientY: 110};
+ open(firstRow, event, "first");
+ open(firstRow, {...event, clientX: 150, clientY: 115}, "first");
+ open(row(200), {...event, clientX: 210, clientY: 190}, "second");
+ assert.equal(isVisible(), true);
+ assert.deepEqual(positions, [
+ {x: 80, y: 120, h: 28},
+ {x: 150, y: 120, h: 28},
+ {x: 210, y: 200, h: 28},
+ ]);
+ });
+
+ test(`${name} more button anchors to the button and toggles on a second click`, () => {
+ const {open, positions, isVisible} = loadMenu(name);
+ const row = {getAttribute: (): string | null => null};
+ const button = {getBoundingClientRect: () => ({left: 240, bottom: 120, height: 20})};
+ const event = {type: "click", clientX: 250, clientY: 110, target: {closest: () => button}};
+ open(row, event, "first");
+ assert.equal(isVisible(), true);
+ assert.deepEqual(positions, [{x: 240, y: 120, h: 20}]);
+ open(row, event, "first");
+ assert.equal(isVisible(), false);
+ assert.equal(positions.length, 1);
+ });
+}
diff --git a/app/src/menus/tag.ts b/app/src/menus/tag.ts
index 5ba05b7e79..42f60ea290 100644
--- a/app/src/menus/tag.ts
+++ b/app/src/menus/tag.ts
@@ -11,7 +11,7 @@ export const openTagMenu = (element: HTMLElement, event: MouseEvent, labelName:
if (window.siyuan.config.readonly) {
return;
}
- if (!window.siyuan.menus.menu.element.classList.contains("fn__none") &&
+ if (event.type !== "contextmenu" && !window.siyuan.menus.menu.element.classList.contains("fn__none") &&
window.siyuan.menus.menu.element.getAttribute("data-name") === Constants.MENU_TAG) {
window.siyuan.menus.menu.remove();
return;
@@ -41,5 +41,7 @@ export const openTagMenu = (element: HTMLElement, event: MouseEvent, labelName:
}
}).element);
window.siyuan.menus.menu.element.setAttribute("data-name", Constants.MENU_TAG);
- window.siyuan.menus.menu.popup({x: event.clientX - 11, y: event.clientY + 11, h: 22, w: 12});
+ const button = event.type === "contextmenu" ? null : (event.target as Element).closest(".b3-list-item__action");
+ const rect = (button || element).getBoundingClientRect();
+ window.siyuan.menus.menu.popup({x: button ? rect.left : event.clientX, y: rect.bottom, h: rect.height});
};