Skip to content

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

:bug: Size search menus by the available height instead of 50vh

Jeffrey Chen2e4006ee2ac8
diff --git a/app/src/ai/actions.ts b/app/src/ai/actions.ts
index 57739ad842..1d0092bb16 100644
--- a/app/src/ai/actions.ts
+++ b/app/src/ai/actions.ts
@@ -1,4 +1,5 @@
import {toggleMenu} from "../menus/menuToggle";
+import {markMenuInnerScroll} from "../menus/Menu";
import {fetchPost} from "../util/fetch";
import {focusByRange} from "../protyle/util/selectionOffsets";
import {Dialog} from "../dialog";
@@ -267,7 +268,7 @@ const openAIActions = (actions: IAIEditorAction[], elements: HTMLElement[], prot
});
}
});
- menu.element.querySelector(".b3-menu__items").setAttribute("style", "overflow: initial");
+ markMenuInnerScroll(menu.element);
/// #if MOBILE
menu.fullscreen();
/// #else
diff --git a/app/src/assets/scss/component/_menu.scss b/app/src/assets/scss/component/_menu.scss
index c7b5e42563..6b6dc5322a 100644
--- a/app/src/assets/scss/component/_menu.scss
+++ b/app/src/assets/scss/component/_menu.scss
@@ -404,6 +404,29 @@
max-height: var(--b3-menu-items-max-height, none);
}
+ // 由 markMenuInnerScroll() 标记的菜单:搜索框固定,滚动交给内层列表。
+ // items 不再作为滚动容器,让 popup() 按锚点位置写入的可用高度生效(filter 的 50vh 会提前截断它,
+ // 导致窗口还有余量也出现滚动条);放不下时由内层列表滚动。
+ & > &__items--inner-scroll {
+ display: flex;
+ flex-direction: column;
+ overflow: visible;
+ // 全屏 sheet 中 items 由 flex 分配高度,需允许收缩到内容以下
+ min-height: 0;
+
+ > div {
+ display: flex;
+ flex-direction: column;
+ min-height: 0;
+ }
+
+ .b3-menu__filter {
+ flex: 1 1 auto;
+ min-height: 0;
+ max-height: none;
+ }
+ }
+
&__item {
padding: var(--b3-ui-menu-item-padding, 0 8px);
border-radius: var(--b3-border-radius);
diff --git a/app/src/config/tabs/ai/aiProviderUi.ts b/app/src/config/tabs/ai/aiProviderUi.ts
index 0770da2a9a..c6ebf045dd 100644
--- a/app/src/config/tabs/ai/aiProviderUi.ts
+++ b/app/src/config/tabs/ai/aiProviderUi.ts
@@ -1,4 +1,5 @@
import {toggleMenu} from "../../../menus/menuToggle";
+import {markMenuInnerScroll} from "../../../menus/Menu";
import {escapeHtmlTextAndAttr} from "../../../util/escape";
import type {AIModelTestData} from "../../../types/api";
import {bindPasswordIconaToggle, genConfigItemMainHtml} from "../../render/fragments";
@@ -362,7 +363,7 @@ const openAvailableModelMenu = (modelInput: HTMLInputElement, models: string[],
});
const rect = modelInput.getBoundingClientRect();
session.show(() => menu.open({x: rect.left, y: rect.bottom, h: rect.height, w: rect.width, target: modelInput}));
- menu.element.querySelector(".b3-menu__items").setAttribute("style", "overflow: initial");
+ markMenuInnerScroll(menu.element);
},
});
diff --git a/app/src/config/tabs/appearanceTab.ts b/app/src/config/tabs/appearanceTab.ts
index 631e6846c4..c9b868cfa9 100644
--- a/app/src/config/tabs/appearanceTab.ts
+++ b/app/src/config/tabs/appearanceTab.ts
@@ -1,4 +1,5 @@
import {toggleMenu} from "../../menus/menuToggle";
+import {markMenuInnerScroll} from "../../menus/Menu";
/// #if !BROWSER
import * as path from "path";
import {useShell} from "../../util/pathName";
@@ -578,7 +579,7 @@ const mountAppearanceFontFamily = (root: HTMLElement, configKey: FontFamiliesCon
const rect = fontFamiliesElement.getBoundingClientRect();
session.show(() => fontMenu.open({x: rect.left, y: rect.bottom, h: rect.height}));
// 内部列表自行滚动,搜索框保持固定
- fontMenu.element.querySelector(".b3-menu__items").setAttribute("style", "overflow: initial");
+ markMenuInnerScroll(fontMenu.element);
if (!isMobile()) {
fontMenu.element.querySelector<HTMLInputElement>('[data-type="font-search"]').focus();
}
diff --git a/app/src/menus/Menu.ts b/app/src/menus/Menu.ts
index 288661f54d..f1cde65d3d 100644
--- a/app/src/menus/Menu.ts
+++ b/app/src/menus/Menu.ts
@@ -31,6 +31,15 @@ const applyMenuConfig = (menuElement: HTMLElement) => {
/// #endif
};
+// 标记「搜索框固定、内容由内层列表滚动」的菜单。标记写在内容容器上,样式见 component/_menu.scss
+// 的 .b3-menu__items--inner-scroll:容器不再自行滚动,保留 popup() 按锚点位置写入的可用高度,
+// 放不下时由内层列表滚动。返回内容容器,供调用方继续调整内层结构。
+export const markMenuInnerScroll = (menuElement: HTMLElement): HTMLElement => {
+ const itemsElement = menuElement.lastElementChild as HTMLElement;
+ itemsElement.classList.add("b3-menu__items--inner-scroll");
+ return itemsElement;
+};
+
export class Menu {
public element: HTMLElement;
public data: any; // 用于记录当前菜单的数据
@@ -618,7 +627,7 @@ export class Menu {
(this.element.firstElementChild.querySelector(".b3-menu__label") as HTMLElement).innerHTML =
window.siyuan.languages.back;
this.element.lastElementChild.innerHTML = "";
- this.element.lastElementChild.classList.remove("b3-menu__items--menu");
+ this.element.lastElementChild.classList.remove("b3-menu__items--menu", "b3-menu__items--inner-scroll");
this.element.lastElementChild.removeAttribute("style"); // 输入框 focus 后 boxShadow 显示不全
this.element.classList.add("fn__none");
setPopoverMenuOpen(this.element, false);
diff --git a/app/src/menus/menuLayout.dom.test.ts b/app/src/menus/menuLayout.dom.test.ts
index d6c4b39672..92efee3ecc 100644
--- a/app/src/menus/menuLayout.dom.test.ts
+++ b/app/src/menus/menuLayout.dom.test.ts
@@ -122,7 +122,7 @@ const browserCases = async (sources: Record<string, string>, css: string,
document.head.append(style);
Object.assign(window, {siyuan: {languages: {back: "Back"}, zIndex: 0, storage: {}, config: {appearance: {}},
mobile: {size: {portrait: {height1: innerHeight}}}, menus: {}}});
- const {Menu, MenuItem}: typeof import("./Menu") = load("menus/Menu");
+ const {Menu, MenuItem, markMenuInnerScroll}: typeof import("./Menu") = load("menus/Menu");
const {Menu: PluginMenu}: typeof import("../plugin/Menu") = load("plugin/Menu");
const settle = async () => {
for (let i = 0; i < 3; i++) {
@@ -636,6 +636,19 @@ const browserCases = async (sources: Record<string, string>, css: string,
check.equal(hiddenItems.scrollTop, 0);
dispose(multiple);
+ const innerScroll = createMenu();
+ const innerScrollItems = markMenuInnerScroll(innerScroll.element);
+ innerScroll.popup({x: 40, y: 40});
+ await settle();
+ check.ok(innerScrollItems === innerScroll.element.lastElementChild,
+ "markMenuInnerScroll returns the content container");
+ check.ok(innerScrollItems.classList.contains("b3-menu__items--inner-scroll"), "the container is marked for inner scrolling");
+ check.equal(getComputedStyle(innerScrollItems).overflow, "visible", "the container does not scroll by itself");
+ innerScroll.remove();
+ check.equal(innerScrollItems.classList.contains("b3-menu__items--inner-scroll"), false,
+ "closing the menu clears the inner scroll marker");
+ dispose(innerScroll);
+
const {getPdfViewerHTML} = load("asset/pdf/viewerTemplate");
const {SecondaryToolbar} = load("asset/pdf/secondary_toolbar.js");
const createPDFMenu = () => {
diff --git a/app/src/protyle/header/Background.ts b/app/src/protyle/header/Background.ts
index 1b29adcf2d..b208b4ab7e 100644
--- a/app/src/protyle/header/Background.ts
+++ b/app/src/protyle/header/Background.ts
@@ -1,4 +1,5 @@
import {toggleMenu} from "../../menus/menuToggle";
+import {markMenuInnerScroll} from "../../menus/Menu";
import {hasClosestByClassName} from "../util/hasClosest";
import {getRandom} from "../../util/functions";
import {hideElements} from "../ui/hideElements";
@@ -846,8 +847,7 @@ export class Background {
});
}
});
- const itemsElement = menu.element.querySelector(".b3-menu__items");
- itemsElement.setAttribute("style", "overflow: initial");
+ const itemsElement = markMenuInnerScroll(menu.element);
/// #if MOBILE
session.show(() => menu.fullscreen("bottom"));
itemsElement.firstElementChild.setAttribute("style", "padding: 0 8px;height: 100%;");
diff --git a/app/src/protyle/render/av/rollup.ts b/app/src/protyle/render/av/rollup.ts
index 74ad871df5..91318851c4 100644
--- a/app/src/protyle/render/av/rollup.ts
+++ b/app/src/protyle/render/av/rollup.ts
@@ -1,4 +1,5 @@
import {Menu} from "../../../plugin/Menu";
+import {markMenuInnerScroll} from "../../../menus/Menu";
import {hasClosestByClassName} from "../../util/hasClosest";
import {upDownHint} from "../../../util/upDownHint";
import {fetchPost} from "../../../util/fetch";
@@ -188,7 +189,7 @@ export const goSearchRollupCol = (options: {
});
}
});
- menu.element.querySelector(".b3-menu__items").setAttribute("style", "overflow: initial");
+ markMenuInnerScroll(menu.element);
};
export const getRollupHTML = (options: { data?: IAV, cellElements?: HTMLElement[], colData?: IAVColumn }) => {
diff --git a/app/src/protyle/toolbar/fontFamilyMenu.ts b/app/src/protyle/toolbar/fontFamilyMenu.ts
index 3cb17646c0..46d68d5985 100644
--- a/app/src/protyle/toolbar/fontFamilyMenu.ts
+++ b/app/src/protyle/toolbar/fontFamilyMenu.ts
@@ -1,5 +1,6 @@
import {toggleMenu} from "../../menus/menuToggle";
import {Menu} from "../../plugin/Menu";
+import {markMenuInnerScroll} from "../../menus/Menu";
import {observeFontPreview} from "../../util/fontPreview";
import {escapeAttr, escapeHtml} from "../../util/escape";
import {
@@ -323,7 +324,7 @@ export const openFontFamilyMenu = async (target: HTMLElement, options: IFontFami
} else {
const rect = target.getBoundingClientRect();
session.show(() => menu.open({x: rect.left, y: rect.bottom, h: rect.height, w: rect.width, target}));
- menu.element.querySelector(".b3-menu__items").setAttribute("style", "overflow: initial");
+ markMenuInnerScroll(menu.element);
}
};
target.setAttribute("aria-expanded", "true");
diff --git a/app/tests/menuInnerScroll.test.js b/app/tests/menuInnerScroll.test.js
new file mode 100644
index 0000000000..c1befa25b2
--- /dev/null
+++ b/app/tests/menuInnerScroll.test.js
@@ -0,0 +1,132 @@
+const assert = require("node:assert/strict");
+const fs = require("node:fs");
+const os = require("node:os");
+const path = require("node:path");
+
+// 搜索框固定、滚动交给内层列表的菜单(AI 编辑、字体族、背景图、模型选择、汇总)结构一致:
+// .b3-menu__items--inner-scroll(由 Menu.useInnerScroll() 标记,并保留 open() 写入的可用高度)
+// > div > .b3-menu__filter > .b3-list
+
+const browserCases = (entries) => {
+ const listHTML = count => Array.from({length: count}, (_, index) =>
+ `<div class="b3-list-item b3-list-item--narrow">Option ${index + 1}</div>`).join("");
+ const check = require("node:assert/strict");
+ const style = document.createElement("style");
+ document.head.append(style);
+ const menu = document.createElement("div");
+ menu.id = "verifyMenu";
+ menu.className = "b3-menu";
+ document.body.append(menu);
+
+ const build = ({count, maxHeight, mobile = false, internalScroll = true}) => {
+ style.textContent = mobile ? entries.mobile : entries.base;
+ menu.className = mobile ? "b3-menu b3-menu--fullscreen" : "b3-menu";
+ menu.removeAttribute("style");
+ menu.innerHTML = '<div class="b3-menu__title fn__none"></div><div class="b3-menu__items">' +
+ '<div><div class="fn__flex-column b3-menu__filter"><input class="b3-text-field fn__flex-shrink">' +
+ '<div class="fn__hr"></div><div class="b3-list fn__flex-1 b3-list--background">' +
+ listHTML(count) + "</div></div></div></div>";
+ menu.style.left = "120px";
+ menu.style.top = mobile ? "0" : "60px";
+ const items = menu.querySelector(".b3-menu__items");
+ if (internalScroll) {
+ items.classList.add("b3-menu__items--inner-scroll");
+ }
+ if (maxHeight) {
+ items.style.maxHeight = maxHeight;
+ }
+ if (mobile) {
+ menu.querySelector(".b3-menu__items > div").style.cssText = "padding: 0 8px;height: 100%";
+ }
+ return {items, filter: menu.querySelector(".b3-menu__filter"), list: menu.querySelector(".b3-list"),
+ input: menu.querySelector("input"), rect: menu.getBoundingClientRect()};
+ };
+
+ try {
+ // 可用高度与 Menu.setPopupPosition() 一致:锚点下方剩余空间
+ const available = Math.max(0, window.innerHeight - 60 - 20);
+
+ const long = build({count: 40, maxHeight: `${available}px`});
+ check.ok(long.filter.getBoundingClientRect().height > window.innerHeight * .5 + 20,
+ `search menus use the available height instead of 50vh: ${long.filter.getBoundingClientRect().height}`);
+ check.ok(long.rect.bottom <= window.innerHeight + .5, "the menu stays inside the viewport");
+ check.ok(long.list.scrollHeight > long.list.clientHeight + 1, "a long list still scrolls");
+ check.equal(getComputedStyle(long.items).overflow, "visible", "the container does not scroll by itself");
+ const inputTop = long.input.getBoundingClientRect().top;
+ long.list.scrollTop = 200;
+ check.equal(long.input.getBoundingClientRect().top, inputTop,
+ "the search field stays fixed while the list scrolls");
+
+ const short = build({count: 12, maxHeight: `${available}px`});
+ check.ok(short.list.scrollHeight <= short.list.clientHeight + 1,
+ "a short list fits without a scrollbar while the window still has room");
+
+ const plain = build({count: 40, maxHeight: `${available}px`, internalScroll: false});
+ check.ok(Math.abs(parseFloat(getComputedStyle(plain.filter).maxHeight) - window.innerHeight * .5) < 1,
+ `menus that scroll as a whole keep the 50vh filter limit: ${getComputedStyle(plain.filter).maxHeight}`);
+ check.equal(getComputedStyle(plain.items).overflow, "auto");
+ check.equal(plain.items.classList.contains("b3-menu__items--inner-scroll"), false);
+
+ const sheet = build({count: 60, mobile: true});
+ const sheetRect = menu.getBoundingClientRect();
+ const sheetList = sheet.list.getBoundingClientRect();
+ check.ok(Math.abs(sheetRect.height - window.innerHeight) < 2,
+ `the fullscreen sheet fills the window: ${sheetRect.height}`);
+ check.ok(sheet.items.getBoundingClientRect().height <= sheetRect.height + .5,
+ "the sheet items do not overflow the fullscreen menu");
+ check.ok(sheetList.height < sheetRect.height, "the sheet list is bounded by the menu");
+ check.ok(sheet.list.scrollHeight > sheet.list.clientHeight + 1, "the sheet list scrolls inside the menu");
+ check.equal(getComputedStyle(sheet.items).overflow, "visible",
+ "the sheet overrides its own horizontal clipping so only the list scrolls");
+ return "ok";
+ } catch (error) {
+ return `${error.message} (viewport ${window.innerWidth}x${window.innerHeight})`;
+ }
+};
+
+if (process.versions.electron && process.type === "browser") {
+ const {app, BrowserWindow} = require("electron");
+ app.setPath("userData", process.argv[2]);
+ app.disableHardwareAcceleration();
+ app.whenReady().then(async () => {
+ const win = new BrowserWindow({show: false, width: 900, height: 900,
+ webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true, backgroundThrottling: false}});
+ const watchdog = setTimeout(() => app.exit(1), 50000);
+ try {
+ const entries = Object.fromEntries(["base", "mobile"].map(name => [name,
+ require("sass").compile(path.join(__dirname, `../src/assets/scss/${name}.scss`),
+ {logger: require("sass").Logger.silent}).css]));
+ await win.loadURL("about:blank");
+ const result = await win.webContents.executeJavaScript(
+ `(${browserCases.toString()})(${JSON.stringify(entries)})`);
+ if (result !== "ok") {
+ throw new Error(result);
+ }
+ console.log("Verified search menu height, fixed search field, sheet layout and unchanged plain menus");
+ win.destroy();
+ clearTimeout(watchdog);
+ app.exit(0);
+ } catch (error) {
+ console.error(error);
+ win.destroy();
+ app.exit(1);
+ }
+ });
+} else {
+ const {test} = require("node:test");
+ const {execFile} = require("node:child_process");
+ const {promisify} = require("node:util");
+ test("search menus fill the available height and keep the search field visible", {timeout: 60000}, async () => {
+ const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "siyuan-menu-filter-"));
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ const result = await promisify(execFile)(require("electron"), [__filename, temporary],
+ {env, windowsHide: true, timeout: 55000});
+ assert.match(result.stdout, /Verified search menu height/);
+ } finally {
+ assert.equal(path.dirname(path.resolve(temporary)), path.resolve(os.tmpdir()));
+ fs.rmSync(temporary, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});
+ }
+ });
+}