Skip to content

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

:bug: Mark inner-scroll menus before measuring their height https://github.com/siyuan-note/siyuan/pull/21041

Daniel3889cee8a752
diff --git a/app/src/config/tabs/ai/aiProviderUi.ts b/app/src/config/tabs/ai/aiProviderUi.ts
index c6ebf045dd..9dbff1f5ca 100644
--- a/app/src/config/tabs/ai/aiProviderUi.ts
+++ b/app/src/config/tabs/ai/aiProviderUi.ts
@@ -362,8 +362,8 @@ 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}));
markMenuInnerScroll(menu.element);
+ session.show(() => menu.open({x: rect.left, y: rect.bottom, h: rect.height, w: rect.width, target: modelInput}));
},
});
diff --git a/app/src/config/tabs/appearanceTab.test.ts b/app/src/config/tabs/appearanceTab.test.ts
index 0deebd20b8..062a27806f 100644
--- a/app/src/config/tabs/appearanceTab.test.ts
+++ b/app/src/config/tabs/appearanceTab.test.ts
@@ -102,6 +102,7 @@ const createPendingFontMenu = (key: string) => {
const attributes = new Map<string, string>();
const state = {opened: 0, focused: 0, items: 0};
let visible = false;
+ let innerScroll = false;
let resolveFonts: (fonts: unknown[]) => void;
const input = {style: {removeProperty() {}}, isConnected: true, closest: (): null => null, setAttribute() {},
addEventListener: (name: string, callback: () => Promise<void>) => listeners.set(name, callback),
@@ -112,19 +113,23 @@ const createPendingFontMenu = (key: string) => {
contains: (target: unknown) => target === element,
getAttribute: (name: string) => attributes.get(name),
setAttribute: (name: string, value: string) => attributes.set(name, value),
- querySelector: (selector: string) => selector === ".b3-menu__items" ? {setAttribute() {}} :
- {focus: () => state.focused++},
+ querySelector: () => ({focus: () => state.focused++}),
};
const menu = {element, data: undefined as Element, removeCB: undefined as (() => void),
remove() {
visible = false;
+ innerScroll = false;
this.data = undefined;
const callback = this.removeCB;
this.removeCB = undefined;
callback?.();
attributes.clear();
},
- addItem: () => state.items++, popup: () => { visible = true; state.opened++; },
+ addItem: () => state.items++, popup: () => {
+ assert.equal(innerScroll, true, "enable inner scrolling before measuring the font menu");
+ visible = true;
+ state.opened++;
+ },
};
const window = {siyuan: {menus: {menu}, languages: {default: "Default"},
config: {appearance: {globalFontFamilies: [] as unknown[]},
@@ -143,6 +148,10 @@ const createPendingFontMenu = (key: string) => {
});
const appearance = load("src/config/tabs/appearanceTab.ts", {
"../../menus/menuToggle": loadMenuToggle({window}),
+ "../../menus/Menu": {markMenuInnerScroll: (target: unknown) => {
+ assert.equal(target, element);
+ innerScroll = true;
+ }},
"../../plugin/Menu": pluginMenu,
"../../util/availableFont": {loadAvailableFonts: () => new Promise(resolve => {
resolveFonts = fonts => resolve({customFontSupported: false, customFonts: [], fontItems: fonts});
diff --git a/app/src/config/tabs/appearanceTab.ts b/app/src/config/tabs/appearanceTab.ts
index c9b868cfa9..b65c7daabe 100644
--- a/app/src/config/tabs/appearanceTab.ts
+++ b/app/src/config/tabs/appearanceTab.ts
@@ -577,9 +577,9 @@ const mountAppearanceFontFamily = (root: HTMLElement, configKey: FontFamiliesCon
}
});
const rect = fontFamiliesElement.getBoundingClientRect();
- session.show(() => fontMenu.open({x: rect.left, y: rect.bottom, h: rect.height}));
// 内部列表自行滚动,搜索框保持固定
markMenuInnerScroll(fontMenu.element);
+ session.show(() => fontMenu.open({x: rect.left, y: rect.bottom, h: rect.height}));
if (!isMobile()) {
fontMenu.element.querySelector<HTMLInputElement>('[data-type="font-search"]').focus();
}
diff --git a/app/src/menus/menuLayout.dom.test.ts b/app/src/menus/menuLayout.dom.test.ts
index 92efee3ecc..9d7877adc7 100644
--- a/app/src/menus/menuLayout.dom.test.ts
+++ b/app/src/menus/menuLayout.dom.test.ts
@@ -637,6 +637,17 @@ const browserCases = async (sources: Record<string, string>, css: string,
dispose(multiple);
const innerScroll = createMenu();
+ const openModelPicker = new Function("toggleMenu", "Menu", "markMenuInnerScroll", "escapeHTML", "upDownHint",
+ sources.modelPicker + "\nreturn openAvailableModelMenu;")(
+ ({build}: {build: (menu: unknown, session: {show: (show: () => void) => void}) => void}) =>
+ build(undefined, {show: show => show()}), PluginMenu, markMenuInnerScroll, (value: string) => value, () => {});
+ const modelInput = document.createElement("input");
+ modelInput.style.cssText = "position:fixed;left:40px;top:40px;height:28px";
+ document.body.append(modelInput);
+ openModelPicker(modelInput, Array.from({length: 60}, (_, index) => `Model ${index}`));
+ check.ok(innerScroll.element.querySelector(".b3-menu__filter").getBoundingClientRect().height > innerHeight * .5 + 20,
+ "the model picker must enable inner scrolling before popup measures its height");
+ fitsViewport(innerScroll.element);
const innerScrollItems = markMenuInnerScroll(innerScroll.element);
innerScroll.popup({x: 40, y: 40});
await settle();
@@ -648,6 +659,7 @@ const browserCases = async (sources: Record<string, string>, css: string,
check.equal(innerScrollItems.classList.contains("b3-menu__items--inner-scroll"), false,
"closing the menu clears the inner scroll marker");
dispose(innerScroll);
+ modelInput.remove();
const {getPdfViewerHTML} = load("asset/pdf/viewerTemplate");
const {SecondaryToolbar} = load("asset/pdf/secondary_toolbar.js");
@@ -867,6 +879,11 @@ test("menus adapt to layout changes and optional whole-menu mouse scrolling", {
transpileModule(readFileSync(path.join(__dirname, "..", name.endsWith(".js") ? name : name + ".ts"), "utf8"), {
compilerOptions: {target: ScriptTarget.ES2021, module: ModuleKind.CommonJS},
}).outputText]));
+ const providerSource = readFileSync(path.join(__dirname, "../config/tabs/ai/aiProviderUi.ts"), "utf8");
+ sources.modelPicker = transpileModule(providerSource.slice(providerSource.indexOf("const openAvailableModelMenu ="),
+ providerSource.indexOf("const showTestResult =")), {
+ compilerOptions: {target: ScriptTarget.ES2021, module: ModuleKind.CommonJS},
+ }).outputText;
const css = ["component/_menu.scss", "component/_list.scss", "component/_text-field.scss", "util/_function.scss",
"business/_av.scss", "business/_av-calendar.scss", "business/_ai_agent.scss", "pdf/_pdf.scss",
"protyle/_mindmap-view.scss"].map(name =>
diff --git a/app/src/protyle/toolbar/fontFamilyMenu.ts b/app/src/protyle/toolbar/fontFamilyMenu.ts
index 46d68d5985..d584187d2c 100644
--- a/app/src/protyle/toolbar/fontFamilyMenu.ts
+++ b/app/src/protyle/toolbar/fontFamilyMenu.ts
@@ -323,8 +323,8 @@ export const openFontFamilyMenu = async (target: HTMLElement, options: IFontFami
session.show(() => menu.fullscreen("bottom"));
} else {
const rect = target.getBoundingClientRect();
- session.show(() => menu.open({x: rect.left, y: rect.bottom, h: rect.height, w: rect.width, target}));
markMenuInnerScroll(menu.element);
+ session.show(() => menu.open({x: rect.left, y: rect.bottom, h: rect.height, w: rect.width, target}));
}
};
target.setAttribute("aria-expanded", "true");
diff --git a/app/tests/menuInnerScroll.test.js b/app/tests/menuInnerScroll.test.js
index c1befa25b2..6b0081643b 100644
--- a/app/tests/menuInnerScroll.test.js
+++ b/app/tests/menuInnerScroll.test.js
@@ -4,7 +4,7 @@ const os = require("node:os");
const path = require("node:path");
// 搜索框固定、滚动交给内层列表的菜单(AI 编辑、字体族、背景图、模型选择、汇总)结构一致:
-// .b3-menu__items--inner-scroll(由 Menu.useInnerScroll() 标记,并保留 open() 写入的可用高度)
+// .b3-menu__items--inner-scroll(由 markMenuInnerScroll() 标记,并保留 open() 写入的可用高度)
// > div > .b3-menu__filter > .b3-list
const browserCases = (entries) => {