Skip to content

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

:bug: Fit the mobile Add to Database sheet to its content https://github.com/siyuan-note/siyuan/issues/19519

Danielcee02f5b2148
diff --git a/app/src/menus/Menu.ts b/app/src/menus/Menu.ts
index 47ddf1e27f..4713f5a548 100644
--- a/app/src/menus/Menu.ts
+++ b/app/src/menus/Menu.ts
@@ -350,7 +350,16 @@ export class Menu {
const mobileSize = window.siyuan.mobile.size;
const orientationSize = mobileSize.isLandscape ? mobileSize.landscape : mobileSize.portrait;
// 使用当前方向记录的完整视口高度,避免软键盘收起期间菜单高度被压缩
- this.element.style.height = Math.max(window.innerHeight, orientationSize?.height1 || 0) * .56 + "px";
+ const maxHeight = Math.max(window.innerHeight, orientationSize?.height1 || 0) * .56;
+ if (this.element.classList.contains("b3-menu--fit")) {
+ // 内容不足时收缩面板,避免列表下方留白;先清空高度,否则 scrollHeight 会被当前高度撑大
+ this.element.style.height = "";
+ const titleHeight = this.element.firstElementChild.getBoundingClientRect().height;
+ const contentHeight = this.element.lastElementChild.scrollHeight;
+ this.element.style.height = Math.min(maxHeight, Math.max(160, titleHeight + contentHeight)) + "px";
+ return;
+ }
+ this.element.style.height = maxHeight + "px";
}
public showSubMenu(subMenuElement: HTMLElement) {
@@ -506,7 +515,7 @@ export class Menu {
this.element.lastElementChild.classList.remove("b3-menu__items--menu");
this.element.lastElementChild.removeAttribute("style"); // 输入框 focus 后 boxShadow 显示不全
this.element.classList.add("fn__none");
- this.element.classList.remove("b3-menu--list", "b3-menu--fullscreen", "b3-menu--sheet");
+ this.element.classList.remove("b3-menu--list", "b3-menu--fullscreen", "b3-menu--sheet", "b3-menu--fit");
this.element.removeAttribute("style"); // zIndex
this.element.removeAttribute("data-name"); // 标识再次点击不消失
this.element.removeAttribute("data-from"); // 标识菜单入口
diff --git a/app/src/menus/menuSheetHeight.test.ts b/app/src/menus/menuSheetHeight.test.ts
new file mode 100644
index 0000000000..8d3def85c5
--- /dev/null
+++ b/app/src/menus/menuSheetHeight.test.ts
@@ -0,0 +1,69 @@
+import {readFileSync} from "node:fs";
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isClassDeclaration, ModuleKind, ScriptTarget, transpileModule} from "typescript";
+import type {Menu} from "./Menu";
+
+const source = createSourceFile("Menu.ts", readFileSync("src/menus/Menu.ts", "utf8"), ScriptTarget.ES2021, true);
+const declaration = source.statements.find(statement => isClassDeclaration(statement) && statement.name?.text === "Menu");
+const compiled = transpileModule(declaration.getText(source), {
+ compilerOptions: {target: ScriptTarget.ES2021, module: ModuleKind.CommonJS},
+}).outputText;
+
+const setup = (options: {fit: boolean, contentHeight: number, viewportHeight: number}) => {
+ const classes = new Set<string>(["b3-menu--sheet"]);
+ if (options.fit) {
+ classes.add("b3-menu--fit");
+ }
+ const title = {getBoundingClientRect: () => ({height: 16})};
+ const items = {scrollHeight: options.contentHeight};
+ const element = {
+ style: {height: "100px"} as CSSStyleDeclaration,
+ firstElementChild: title,
+ lastElementChild: items,
+ classList: {
+ contains: (name: string) => classes.has(name),
+ },
+ };
+ const module = {exports: {} as {Menu: typeof Menu}};
+ runInNewContext(compiled, {
+ exports: module.exports,
+ window: {
+ innerHeight: options.viewportHeight,
+ siyuan: {mobile: {size: {portrait: {height1: options.viewportHeight}}}},
+ },
+ });
+ const menu = Object.create(module.exports.Menu.prototype) as Menu;
+ Object.assign(menu, {
+ element,
+ updateSheetTitle: () => {},
+ });
+ return {menu, element};
+};
+
+describe("mobile menu sheet content fit", () => {
+ it("keeps the standard sheet height when fitting is not requested", () => {
+ const {menu, element} = setup({fit: false, contentHeight: 272, viewportHeight: 2000});
+ menu.resetPosition();
+ assert.equal(element.style.height, "1120px");
+ });
+
+ it("shrinks the sheet to short content", () => {
+ const {menu, element} = setup({fit: true, contentHeight: 272, viewportHeight: 2000});
+ menu.resetPosition();
+ assert.equal(element.style.height, "288px");
+ });
+
+ it("caps fitted content at the standard sheet height", () => {
+ const {menu, element} = setup({fit: true, contentHeight: 2000, viewportHeight: 2000});
+ menu.resetPosition();
+ assert.equal(element.style.height, "1120px");
+ });
+
+ it("keeps a minimum height for empty content", () => {
+ const {menu, element} = setup({fit: true, contentHeight: 0, viewportHeight: 2000});
+ menu.resetPosition();
+ assert.equal(element.style.height, "160px");
+ });
+});
diff --git a/app/src/protyle/render/av/relation.ts b/app/src/protyle/render/av/relation.ts
index c5f4f120a8..2960336f54 100644
--- a/app/src/protyle/render/av/relation.ts
+++ b/app/src/protyle/render/av/relation.ts
@@ -132,6 +132,12 @@ export const openSearchAV = (options: IOpenSearchAVOptions) => {
let searchTimer = 0;
let requestSequence = 0;
let controller: AbortController;
+ // 列表内容变化后重新适配面板高度,避免下方留白
+ const updateSheetHeight = () => {
+ if (menu.element.classList.contains("b3-menu--fit")) {
+ window.siyuan.menus.menu.resetPosition();
+ }
+ };
const loadList = (keyword: string, cb?: () => void) => {
controller?.abort();
controller = new AbortController();
@@ -141,6 +147,7 @@ export const openSearchAV = (options: IOpenSearchAVOptions) => {
return;
}
cb?.();
+ updateSheetHeight();
});
};
const search = () => {
@@ -200,6 +207,8 @@ export const openSearchAV = (options: IOpenSearchAVOptions) => {
}
event.preventDefault();
event.stopPropagation();
+ // 展开或收起视图后重新适配面板高度
+ updateSheetHeight();
break;
} else if (clickTarget.classList.contains("b3-list-item")) {
event.preventDefault();
@@ -222,6 +231,10 @@ export const openSearchAV = (options: IOpenSearchAVOptions) => {
}
});
menu.element.querySelector(".b3-menu__items").setAttribute("style", "overflow: initial");
+ if (isMobile()) {
+ // 移动端底部面板按内容收缩,避免列表下方留白
+ menu.element.classList.add("b3-menu--fit");
+ }
const popoverElement = hasTopClosestByClassName(options.target, "block__popover", true);
menu.element.setAttribute("data-from", popoverElement ? popoverElement.dataset.level + "popover" : "app");
};