Skip to content

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

:sparkles: Improve database filter option dropdown sizing

Daniela56e66a0fb9b
diff --git a/app/src/assets/scss/business/_av.scss b/app/src/assets/scss/business/_av.scss
index 4827e099e3..8d6f199f5a 100644
--- a/app/src/assets/scss/business/_av.scss
+++ b/app/src/assets/scss/business/_av.scss
@@ -161,6 +161,10 @@
overflow: hidden;
}
+ [data-type="selectDropdown"] .av__select-option .fn__ellipsis {
+ white-space: nowrap;
+ }
+
// 触发器内已选 chip
.av__select-chip {
margin: 1px 2px;
diff --git a/app/src/protyle/render/av/filter.ts b/app/src/protyle/render/av/filter.ts
index 0703888399..1d40617dcb 100644
--- a/app/src/protyle/render/av/filter.ts
+++ b/app/src/protyle/render/av/filter.ts
@@ -11,6 +11,7 @@ import {getFieldsByData} from "./view";
import {Constants} from "../../../constants";
import {countFilterLeaves} from "./filterTree";
import {getAVColorStyle} from "./color";
+import {setFilterSelectPosition} from "./selectPosition";
interface FilterEditorOptions {
action?: IAVFilterOperation["action"];
@@ -736,7 +737,7 @@ const genInlineSelectHTML = (filter: IAVFilter, colData: IAVColumn, path: string
: "";
const chips = options.map(option => {
const selected = selectedValues.some((s: IAVCellSelectValue) => s.content === option.name);
- return `<button type="button" class="av__select-option" data-name="${escapeAttr(option.name)}" data-color="${escapeAttr(option.color)}" data-type="selectOption" data-path="${path}">
+ return `<button type="button" class="av__select-option" title="${escapeAttr(option.name)}" data-name="${escapeAttr(option.name)}" data-color="${escapeAttr(option.color)}" data-type="selectOption" data-path="${path}">
<svg class="av__select-option-check"><use xlink:href="#${selected ? "iconCheck" : "iconUncheck"}"></use></svg>
<span class="b3-chip b3-chip--middle" style="${getAVColorStyle(option)}"><span class="fn__ellipsis">${escapeHtml(option.name)}</span></span>
</button>`;
@@ -1208,23 +1209,8 @@ export const bindInlineFilterEvents = (panelElement: HTMLElement, data: IAV, pro
});
if (dropdown.style.display === "none") {
// 展开时用 fixed 定位到 trigger 下方(避免被 overflow:auto 裁剪)
- const rect = trigger.getBoundingClientRect();
dropdown.style.zIndex = (++window.siyuan.zIndex).toString();
- dropdown.style.left = rect.left + "px";
- dropdown.style.width = Math.max(rect.width, 120) + "px";
- // 先临时显示以测量真实高度,再决定向上还是向下展开
- dropdown.style.visibility = "hidden";
- dropdown.style.display = "block";
- const dropdownHeight = dropdown.offsetHeight;
- dropdown.style.visibility = "";
- const spaceBelow = window.innerHeight - rect.bottom;
- if (spaceBelow < dropdownHeight + 8 && rect.top > dropdownHeight + 8) {
- // 下方不够且上方够:向上展开,紧贴 trigger 上方
- dropdown.style.top = (rect.top - dropdownHeight - 4) + "px";
- } else {
- // 向下展开
- dropdown.style.top = (rect.bottom + 4) + "px";
- }
+ setFilterSelectPosition(dropdown, trigger);
} else {
dropdown.style.display = "none";
}
diff --git a/app/src/protyle/render/av/filterSelectPosition.test.ts b/app/src/protyle/render/av/filterSelectPosition.test.ts
new file mode 100644
index 0000000000..1ab878e503
--- /dev/null
+++ b/app/src/protyle/render/av/filterSelectPosition.test.ts
@@ -0,0 +1,51 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, transpileModule} from "typescript";
+
+const position = (viewport: {innerWidth: number, innerHeight: number}, rect: {
+ left: number, top: number, bottom: number, width: number
+}, labelWidth: number, height: number) => {
+ const methods = {} as typeof import("./selectPosition");
+ runInNewContext(transpileModule(readFileSync("src/protyle/render/av/selectPosition.ts", "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS},
+ }).outputText, {exports: methods, require: () => ({}), window: viewport});
+ const style = {} as CSSStyleDeclaration;
+ const dropdown = {
+ style,
+ querySelectorAll: () => [{scrollWidth: labelWidth}],
+ get offsetHeight() { return Math.min(height, parseFloat(style.maxHeight) || height); },
+ };
+ methods.setFilterSelectPosition(dropdown as unknown as HTMLElement, {
+ getBoundingClientRect: () => rect,
+ } as HTMLElement);
+ return {style, height: dropdown.offsetHeight};
+};
+
+test("long filter options expand independently of a narrow trigger and stay inside the right edge", () => {
+ const {style, height} = position({innerWidth: 1920, innerHeight: 1080},
+ {left: 1740, top: 340, bottom: 368, width: 80}, 400, 800);
+ assert.equal(style.width, "464px");
+ assert.equal(parseFloat(style.left) + parseFloat(style.width), 1912);
+ assert.equal(height, 480);
+ assert.equal(style.top, "372px");
+ assert.equal(style.visibility, "");
+});
+
+test("filter options fit a narrow mobile viewport and open above a low trigger", () => {
+ const {style, height} = position({innerWidth: 320, innerHeight: 640},
+ {left: 250, top: 500, bottom: 528, width: 70}, 900, 800);
+ assert.equal(style.width, "304px");
+ assert.equal(style.left, "8px");
+ assert.equal(height, 480);
+ assert.equal(style.top, "16px");
+});
+
+test("short viewports constrain the list to the larger available side", () => {
+ const {style, height} = position({innerWidth: 800, innerHeight: 300},
+ {left: 20, top: 100, bottom: 128, width: 100}, 30, 800);
+ assert.equal(style.width, "280px");
+ assert.equal(height, 160);
+ assert.equal(parseFloat(style.top) + height, 292);
+});
diff --git a/app/src/protyle/render/av/selectPosition.ts b/app/src/protyle/render/av/selectPosition.ts
index 97011cc855..1474a5a260 100644
--- a/app/src/protyle/render/av/selectPosition.ts
+++ b/app/src/protyle/render/av/selectPosition.ts
@@ -2,6 +2,32 @@ import {setPosition} from "../../../util/setPosition";
const menuAnchors = new WeakMap<HTMLElement, DOMRect>();
+export const setFilterSelectPosition = (dropdown: HTMLElement, trigger: HTMLElement) => {
+ const rect = trigger.getBoundingClientRect();
+ const availableWidth = Math.max(0, window.innerWidth - 16);
+ const minWidth = Math.min(Math.max(rect.width, 280), availableWidth);
+ dropdown.style.boxSizing = "border-box";
+ dropdown.style.minWidth = minWidth + "px";
+ dropdown.style.width = minWidth + "px";
+ dropdown.style.visibility = "hidden";
+ dropdown.style.display = "block";
+ // 按完整选项文本扩宽,并为复选框、标签内边距和滚动条预留空间。
+ let contentWidth = minWidth;
+ dropdown.querySelectorAll<HTMLElement>(".av__select-option .fn__ellipsis").forEach((label) => {
+ contentWidth = Math.max(contentWidth, label.scrollWidth + 64);
+ });
+ const width = Math.min(Math.max(minWidth, Math.min(contentWidth, 560)), availableWidth);
+ dropdown.style.width = width + "px";
+ dropdown.style.left = Math.max(8, Math.min(rect.left, window.innerWidth - width - 8)) + "px";
+ const spaceBelow = Math.max(0, window.innerHeight - rect.bottom - 12);
+ const spaceAbove = Math.max(0, rect.top - 12);
+ dropdown.style.maxHeight = Math.min(480, Math.max(spaceBelow, spaceAbove)) + "px";
+ const openAbove = dropdown.offsetHeight > spaceBelow && spaceAbove > spaceBelow;
+ dropdown.style.maxHeight = Math.min(480, openAbove ? spaceAbove : spaceBelow) + "px";
+ dropdown.style.top = (openAbove ? rect.top - dropdown.offsetHeight - 4 : rect.bottom + 4) + "px";
+ dropdown.style.visibility = "";
+};
+
export const setSelectMenuPosition = (menuElement: HTMLElement, cellElement: HTMLElement) => {
if (cellElement.isConnected) {
menuAnchors.set(menuElement, cellElement.getBoundingClientRect());