An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Keep multiselect menus anchored after database item refresh https://github.com/siyuan-note/siyuan/issues/19860
Daniele3fc31b5d2b0
diff --git a/app/src/protyle/render/av/openMenuPanel.ts b/app/src/protyle/render/av/openMenuPanel.tsindex 2016f9a1df..9ce1a6ffd9 100644--- a/app/src/protyle/render/av/openMenuPanel.ts+++ b/app/src/protyle/render/av/openMenuPanel.ts@@ -85,6 +85,7 @@ import {clearSelect} from "../../util/clear";import {applyAVColorPalette, getAVCustomColors} from "./color";import {bindContextFilterEvent, getContextFilterHTML} from "./contextFilter";import {setAVCellPanelTarget} from "./panelTarget";+import {setSelectMenuPosition} from "./selectPosition";export const openMenuPanel = (options: {protyle: IProtyle,@@ -316,7 +317,11 @@ export const openMenuPanel = (options: {inputElement.select();inputElement.focus();}- setPosition(menuElement, cellRect.left, cellRect.bottom, cellRect.height, 0, true);+ if (options.type === "select") {+ setSelectMenuPosition(menuElement, lastElement || options.cellElements[options.cellElements.length - 1]);+ } else {+ setPosition(menuElement, cellRect.left, cellRect.bottom, cellRect.height, 0, true);+ }}} else {setPosition(menuElement, tabRect.right - menuElement.clientWidth, tabRect.bottom, tabRect.height, 0, true);diff --git a/app/src/protyle/render/av/select.ts b/app/src/protyle/render/av/select.tsindex 78dde125fe..2dd207e85b 100644--- a/app/src/protyle/render/av/select.ts+++ b/app/src/protyle/render/av/select.ts@@ -13,7 +13,7 @@ import * as dayjs from "dayjs";import {getFieldsByData} from "./view";import {getFieldIdByCellElement} from "./row";import {Constants} from "../../../constants";-import {setPosition} from "../../../util/setPosition";+import {setSelectMenuPosition} from "./selectPosition";import {getAVBatchEditMode, getAVBatchSourceValue} from "./batchValue";import {AV_MANAGE_CUSTOM_COLORS_TYPE,@@ -163,8 +163,7 @@ export const removeCellOption = (protyle: IProtyle, cellElements: HTMLElement[],const menuElement = hasClosestByClassName(target, "b3-menu");target.remove();if (menuElement) {- const cellRect = cellElements[cellElements.length - 1].getBoundingClientRect();- setPosition(menuElement, cellRect.left, cellRect.bottom, cellRect.height, 0, true);+ setSelectMenuPosition(menuElement, cellElements[cellElements.length - 1]);}};@@ -269,8 +268,7 @@ export const setColOption = (protyle: IProtyle, data: IAV, target: HTMLElement,if (selectedElement) {menuElement.querySelector(".b3-menu__items").scrollTop = oldScroll + (menuElement.querySelector(".b3-chips").clientHeight - oldChipsHeight);// chips 增减导致菜单高度变化后重新定位(锁底部,顶部自适应,避免底部溢出视口)- const cellRect = cellElements[cellElements.length - 1].getBoundingClientRect();- setPosition(menuElement, cellRect.left, cellRect.bottom, cellRect.height, 0, true);+ setSelectMenuPosition(menuElement, cellElements[cellElements.length - 1]);}}, keepMenuOpen);if (menu.isOpen) {@@ -398,8 +396,7 @@ ${isMobile() ? "" : '<div class="fn__hr--small"></div>'}`,if (selectedElement) {menuElement.querySelector(".b3-menu__items").scrollTop = oldScroll + (menuElement.querySelector(".b3-chips").clientHeight - oldChipsHeight);// chips 增减导致菜单高度变化后重新定位(锁底部,顶部自适应,避免底部溢出视口)- const cellRect = cellElements[cellElements.length - 1].getBoundingClientRect();- setPosition(menuElement, cellRect.left, cellRect.bottom, cellRect.height, 0, true);+ setSelectMenuPosition(menuElement, cellElements[cellElements.length - 1]);}}, undefined, true);}@@ -506,8 +503,7 @@ ${isMobile() ? "" : '<div class="fn__hr--small"></div>'}`,}menuElement.querySelector(".b3-menu__items").scrollTop = oldScroll;// chips 增减导致菜单高度变化后重新定位(锁底部,顶部自适应,避免底部溢出视口)- const cellRect = cellElements[cellElements.length - 1].getBoundingClientRect();- setPosition(menuElement, cellRect.left, cellRect.bottom, cellRect.height, 0, true);+ setSelectMenuPosition(menuElement, cellElements[cellElements.length - 1]);name = inputElement.value;desc = descElement.value;color = newColor;@@ -744,8 +740,7 @@ export const addColOptionOrCell = (protyle: IProtyle, data: IAV, cellElements: H}menuElement.querySelector(".b3-menu__items").scrollTop = oldScroll + (menuElement.querySelector(".b3-chips").clientHeight - oldChipsHeight);// chips 增减导致菜单高度变化后重新定位(锁底部,顶部自适应,避免底部溢出视口)- const cellRect = cellElements[cellElements.length - 1].getBoundingClientRect();- setPosition(menuElement, cellRect.left, cellRect.bottom, cellRect.height, 0, true);+ setSelectMenuPosition(menuElement, cellElements[cellElements.length - 1]);}};diff --git a/app/src/protyle/render/av/selectPosition.test.ts b/app/src/protyle/render/av/selectPosition.test.tsnew file mode 100644index 0000000000..aa8111468d--- /dev/null+++ b/app/src/protyle/render/av/selectPosition.test.ts@@ -0,0 +1,62 @@+import * as assert from "node:assert/strict";+import {readFileSync} from "node:fs";+import {join} from "node:path";+import {test} from "node:test";+import {runInNewContext} from "node:vm";+import {transpileModule, ModuleKind, ScriptTarget} from "typescript";++test("select menus retain their anchor after a field is replaced and still avoid viewport overflow", () => {+ const viewport = {innerWidth: 1200, innerHeight: 800};+ const positionExports = {} as typeof import("../../../util/setPosition");+ const compile = (file: string) => transpileModule(readFileSync(file, "utf8"), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2020},+ }).outputText;+ runInNewContext(compile(join(__dirname, "../../../util/setPosition.ts")), {+ exports: positionExports,+ require: () => ({getTopBarHeight: () => 30}),+ window: viewport,+ });+ const exports = {} as typeof import("./selectPosition");+ runInNewContext(compile(join(__dirname, "selectPosition.ts")), {+ exports, require: () => positionExports,+ });+ let height = 100;+ const menu = {+ style: {left: "", top: ""},+ dataset: {} as Record<string, string>,+ getBoundingClientRect() {+ const left = parseFloat(this.style.left);+ const top = parseFloat(this.style.top);+ return {left, top, right: left + 200, bottom: top + height, height, width: 200};+ },+ };+ let rect = {left: 900, bottom: 620, height: 30};+ const field = {isConnected: true, getBoundingClientRect: () => rect};+ const position = () => exports.setSelectMenuPosition(menu as unknown as HTMLElement,+ field as unknown as HTMLElement);+ position();+ assert.equal(menu.style.left, "900px");+ assert.equal(menu.style.top, "620px");++ field.isConnected = false;+ rect = {left: 0, bottom: 0, height: 0};+ height = 220;+ position();+ assert.equal(menu.style.left, "900px");+ assert.equal(menu.style.top, "500px");+ assert.ok(parseFloat(menu.style.top) + height <= viewport.innerHeight);+ height = 100;+ position();+ assert.equal(menu.style.top, "620px");++ field.isConnected = true;+ rect = {left: 500, bottom: 300, height: 40};+ position();+ assert.equal(menu.style.left, "500px");+ assert.equal(menu.style.top, "300px");++ field.isConnected = false;+ const otherMenu = {style: {}, dataset: {}};+ exports.setSelectMenuPosition(otherMenu as unknown as HTMLElement, field as unknown as HTMLElement);+ assert.deepEqual(otherMenu.style, {});+});diff --git a/app/src/protyle/render/av/selectPosition.ts b/app/src/protyle/render/av/selectPosition.tsnew file mode 100644index 0000000000..97011cc855--- /dev/null+++ b/app/src/protyle/render/av/selectPosition.ts@@ -0,0 +1,14 @@+import {setPosition} from "../../../util/setPosition";++const menuAnchors = new WeakMap<HTMLElement, DOMRect>();++export const setSelectMenuPosition = (menuElement: HTMLElement, cellElement: HTMLElement) => {+ if (cellElement.isConnected) {+ menuAnchors.set(menuElement, cellElement.getBoundingClientRect());+ }+ // 属性刷新会替换字段元素,继续使用最近的有效锚点,避免菜单跳到窗口左上角。+ const rect = menuAnchors.get(menuElement);+ if (rect) {+ setPosition(menuElement, rect.left, rect.bottom, rect.height, 0, true);+ }+};