Skip to content

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

:bug: Improve database view settings submenu positioning https://github.com/siyuan-note/siyuan/issues/13740

Daniel7c1da77762cc
diff --git a/app/src/protyle/render/av/calendar/settings.test.ts b/app/src/protyle/render/av/calendar/settings.test.ts
index 786266b564..4addb0412e 100644
--- a/app/src/protyle/render/av/calendar/settings.test.ts
+++ b/app/src/protyle/render/av/calendar/settings.test.ts
@@ -19,6 +19,7 @@ const setup = () => {
transactions.push({do: perform, undo})},
"../../../../util/escape": {escapeAttr: (value: string) => value, escapeHtml: (value: string) => value},
"../col": {getColNameByType: (type: string) => type},
+ "../viewSettingMenu": {openViewSettingMenu: (menu: {open: () => void}) => menu.open()},
"../../../../plugin/Menu": {Menu: class {
items: Array<{label: string; checked: boolean; click: () => void}> = [];
constructor() { menus.push(this.items); }
diff --git a/app/src/protyle/render/av/calendar/settings.ts b/app/src/protyle/render/av/calendar/settings.ts
index 2df57b4808..d26e820f36 100644
--- a/app/src/protyle/render/av/calendar/settings.ts
+++ b/app/src/protyle/render/av/calendar/settings.ts
@@ -2,6 +2,7 @@ import {transaction} from "../../../wysiwyg/transaction";
import {escapeAttr, escapeHtml} from "../../../../util/escape";
import {getColNameByType} from "../col";
import {Menu} from "../../../../plugin/Menu";
+import {openViewSettingMenu} from "../viewSettingMenu";
export const isCalendarDateColumn = (column: IAVColumn) => ["date", "created", "updated"].includes(column?.type);
@@ -84,8 +85,7 @@ export const bindCalendarSettings = (options: {
checked: view.calendar[item.key].toString() === choice.value,
click: () => update(choice.value),
}));
- const rect = select.getBoundingClientRect();
- menu.open({x: rect.left, y: rect.bottom, h: rect.height});
+ openViewSettingMenu(menu, select);
event.preventDefault();
event.stopPropagation();
});
diff --git a/app/src/protyle/render/av/gallery/util.ts b/app/src/protyle/render/av/gallery/util.ts
index ed5c4f9506..604b487dfa 100644
--- a/app/src/protyle/render/av/gallery/util.ts
+++ b/app/src/protyle/render/av/gallery/util.ts
@@ -1,5 +1,6 @@
import {transaction} from "../../../wysiwyg/transaction";
import {Menu} from "../../../../plugin/Menu";
+import {openViewSettingMenu} from "../viewSettingMenu";
import {hasClosestByClassName} from "../../../util/hasClosest";
import {unicode2Emoji} from "../../../../emoji";
import {getColIconByType} from "../col";
@@ -142,8 +143,7 @@ export const setGalleryCover = (options: {
});
}
});
- const rect = options.target.getBoundingClientRect();
- menu.open({x: rect.left, y: rect.bottom, h: rect.height});
+ openViewSettingMenu(menu, options.target);
};
export const setGallerySize = (options: {
@@ -219,8 +219,7 @@ export const setGallerySize = (options: {
});
}
});
- const rect = options.target.getBoundingClientRect();
- menu.open({x: rect.left, y: rect.bottom, h: rect.height});
+ openViewSettingMenu(menu, options.target);
};
export const setGalleryRatio = (options: {
@@ -297,8 +296,7 @@ export const setGalleryRatio = (options: {
});
}
});
- const rect = options.target.getBoundingClientRect();
- menu.open({x: rect.left, y: rect.bottom, h: rect.height});
+ openViewSettingMenu(menu, options.target);
};
export const openGalleryItemMenu = (options: {
diff --git a/app/src/protyle/render/av/layout.ts b/app/src/protyle/render/av/layout.ts
index 9a00c007e3..334f0131a5 100644
--- a/app/src/protyle/render/av/layout.ts
+++ b/app/src/protyle/render/av/layout.ts
@@ -12,6 +12,7 @@ import {getColIconByType} from "./col";
import {escapeHtml} from "../../../util/escape";
import {CARD_LAYOUT_COMPACT, CARD_LAYOUT_LIST} from "./gallery/cardLayout";
import {Menu} from "../../../plugin/Menu";
+import {openViewSettingMenu} from "./viewSettingMenu";
const getCardLayoutHTML = (view: IAVGallery | IAVKanban) => {
return `<button class="b3-menu__item" data-type="set-card-layout">
@@ -326,8 +327,7 @@ export const bindLayoutEvent = (options: {
}
});
});
- const rect = cardLayoutElement.getBoundingClientRect();
- menu.open({x: rect.left, y: rect.bottom, h: rect.height});
+ openViewSettingMenu(menu, cardLayoutElement);
event.preventDefault();
event.stopPropagation();
});
diff --git a/app/src/protyle/render/av/row.ts b/app/src/protyle/render/av/row.ts
index 45ba7400d7..67b4cf2ae5 100644
--- a/app/src/protyle/render/av/row.ts
+++ b/app/src/protyle/render/av/row.ts
@@ -2,6 +2,7 @@ import {isTableLikeView} from "./viewType";
import {hasClosestBlock, hasClosestByClassName, hasTopClosestByAttribute} from "../../util/hasClosest";
import {focusBlock} from "../../util/selection";
import {Menu} from "../../../plugin/Menu";
+import {openViewSettingMenu} from "./viewSettingMenu";
import {transaction} from "../../wysiwyg/transaction";
import {
cellValueIsEmpty,
@@ -888,12 +889,7 @@ export const setPageSize = (options: {
});
}
});
- const rect = options.target.getBoundingClientRect();
- menu.open({
- x: rect.left,
- y: rect.bottom,
- h: rect.height
- });
+ openViewSettingMenu(menu, options.target);
};
export const deleteRow = (blockElement: HTMLElement, protyle: IProtyle,
diff --git a/app/src/protyle/render/av/viewSettingMenu.test.ts b/app/src/protyle/render/av/viewSettingMenu.test.ts
new file mode 100644
index 0000000000..b6a00bc710
--- /dev/null
+++ b/app/src/protyle/render/av/viewSettingMenu.test.ts
@@ -0,0 +1,50 @@
+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 open = (left: number, right: number, viewport: number, width: number, mobile = false) => {
+ const exports = {};
+ runInNewContext(ts.transpileModule(readFileSync(join(__dirname, "viewSettingMenu.ts"), "utf8"), {
+ compilerOptions: {module: ts.ModuleKind.CommonJS},
+ }).outputText, {exports, window: {innerWidth: viewport}, require: () => ({isMobile: () => mobile})});
+ let position: IPosition;
+ const element = {style: {left: ""}, getBoundingClientRect: () => ({width})};
+ const menu = {element, open: (value: IPosition) => { position = value; }};
+ const target = {getBoundingClientRect: () => ({left: left + 8, right: right - 8, top: 120, bottom: 148, height: 28}),
+ closest: () => ({getBoundingClientRect: () => ({left, right})})};
+ const methods = exports as typeof import("./viewSettingMenu");
+ methods.openViewSettingMenu(menu as unknown as Parameters<typeof methods.openViewSettingMenu>[0], target as unknown as HTMLElement);
+ return {position, element};
+};
+
+test("view setting submenus open beside the parent and align with the selected row", () => {
+ const {position, element} = open(20, 310, 1000, 180);
+ assert.equal(position.x, 298);
+ assert.equal(position.y, 112);
+ assert.equal(position.h, undefined);
+ assert.equal(element.style.left, "298px");
+});
+
+test("view setting submenus flip left and stay close to the setting row", () => {
+ const {position, element} = open(680, 970, 1000, 180);
+ assert.equal(position.x, 512);
+ assert.equal(position.x + 180, 680 + 8 + 4);
+ assert.equal(element.style.left, "512px");
+});
+
+test("view setting submenus keep horizontal bounds when neither side fits", () => {
+ const {position} = open(20, 310, 340, 180);
+ assert.ok(position.x >= 0);
+ assert.ok(position.x + 180 <= 340);
+});
+
+test("mobile view settings retain the standard bottom sheet opening path", () => {
+ const {position, element} = open(20, 310, 340, 180, true);
+ assert.equal(position.x, 28);
+ assert.equal(position.y, 148);
+ assert.equal(position.h, 28);
+ assert.equal(element.style.left, "");
+});
diff --git a/app/src/protyle/render/av/viewSettingMenu.ts b/app/src/protyle/render/av/viewSettingMenu.ts
new file mode 100644
index 0000000000..2d83d43b5a
--- /dev/null
+++ b/app/src/protyle/render/av/viewSettingMenu.ts
@@ -0,0 +1,20 @@
+import type {Menu} from "../../../plugin/Menu";
+import {isMobile} from "../../../util/functions";
+
+// 视图设置的子菜单贴近设置项侧边展开,边框略微重叠以缩短鼠标移动距离。
+export const openViewSettingMenu = (menu: Menu, target: HTMLElement) => {
+ const rect = target.getBoundingClientRect();
+ if (isMobile()) {
+ menu.open({x: rect.left, y: rect.bottom, h: rect.height});
+ return;
+ }
+ const position = {x: rect.right - 4, y: rect.top - 8};
+ menu.open(position);
+ const width = menu.element.getBoundingClientRect().width;
+ if (position.x + width > window.innerWidth) {
+ position.x = rect.left - width + 4;
+ }
+ position.x = Math.max(0, Math.min(position.x, window.innerWidth - width));
+ // 同步保存的坐标,菜单重新定位时继续使用同一展开方向。
+ menu.element.style.left = position.x + "px";
+};