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: Prevent block popovers from collapsing near the window center https://github.com/siyuan-note/siyuan/issues/19921
Danielef71cbf565d0
diff --git a/app/src/block/Panel.ts b/app/src/block/Panel.tsindex ef178daded..2db7763c0e 100644--- a/app/src/block/Panel.ts+++ b/app/src/block/Panel.ts@@ -26,6 +26,7 @@ import {planBlockPanelRemoval} from "./panelRemoval";import {getBlockPanelLoadPlan} from "./panelLoad";+import {positionBlockPanel} from "./panelPosition";const BLOCK_PANEL_EDITOR_MIN_HEIGHT = 155;@@ -610,24 +611,11 @@ export class BlockPanel {} else {targetRect = this.targetElement.getBoundingClientRect();}- // 下部位置大的话就置于下部 https://ld246.com/article/1690333302147- if (window.innerHeight - targetRect.bottom - 4 > targetRect.top + 12) {- this.element.style.maxHeight = Math.floor(window.innerHeight - targetRect.bottom - 12) + "px";- }- // 靠边不宜拖拽 https://github.com/siyuan-note/siyuan/issues/2937- setPosition(this.element, targetRect.left, targetRect.bottom + 4, targetRect.height + 12, 8);+ positionBlockPanel(this.element, targetRect);} else if (typeof this.x === "number" && typeof this.y === "number") {setPosition(this.element, this.x, this.y);this.element.style.maxHeight = Math.floor(window.innerHeight - Math.max(this.y, topBarHeight) - 12) + "px";}- const elementRect = this.element.getBoundingClientRect();- if (this.targetElement && !this.targetElement.classList.contains("protyle-wysiwyg__embed")) {- if (elementRect.top < targetRect.top) {- this.element.style.maxHeight = Math.floor(targetRect.top - elementRect.top - 8) + "px";- } else {- this.element.style.maxHeight = Math.floor(window.innerHeight - elementRect.top - 8) + "px";- }- }this.element.classList.add("block__popover--open");this.element.style.zIndex = (++window.siyuan.zIndex).toString();} : undefined);diff --git a/app/src/block/panelPosition.test.ts b/app/src/block/panelPosition.test.tsnew file mode 100644index 0000000000..808a105b25--- /dev/null+++ b/app/src/block/panelPosition.test.ts@@ -0,0 +1,173 @@+import * as assert from "node:assert/strict";+import {execFile} from "node:child_process";+import {mkdtempSync, readFileSync, rmSync, writeFileSync} from "node:fs";+import {tmpdir} from "node:os";+import * as path from "node:path";+import {promisify} from "node:util";+import test from "node:test";+import {compile} from "sass";+import {ScriptTarget, transpileModule} from "typescript";++const browserCases = (source: string, css: string) => {+ const check = require("node:assert/strict");+ const position = new Function(source + "; return positionBlockPanel;")() as+ typeof import("./panelPosition").positionBlockPanel;+ const style = document.createElement("style");+ style.textContent = ":root { --b3-theme-surface-lighter: #ddd; }" + css;+ document.head.append(style);+ document.body.style.margin = "0";+ const toolbar = document.createElement("div");+ toolbar.id = "toolbar";+ toolbar.style.height = "32px";+ document.body.append(toolbar);++ const anchor = (top: number, left = 100) => new DOMRect(left, top, 160, 30);+ const createPanel = (height: number) => {+ document.querySelector(".block__popover")?.remove();+ const element = document.createElement("div");+ element.className = "block__popover";+ element.innerHTML = '<div class="block__icons"></div><div class="block__content"><div></div></div>';+ (element.lastElementChild.firstElementChild as HTMLElement).style.cssText =+ `height:${height - 44}px; flex-shrink:0;`;+ document.body.append(element);+ return element;+ };+ const assertAbove = (element: HTMLElement, target: DOMRect) => {+ const rect = element.getBoundingClientRect();+ check.ok(rect.top >= toolbar.clientHeight, "popover clears the top bar");+ check.ok(Math.abs(rect.bottom - (target.top - 8)) < 1, "popover stays adjacent to the anchor above");+ check.ok(rect.height > 350, "long content uses the available space instead of the minimum height");+ };++ // 中下部的长浮窗在上下均放不下时,应完整利用上方空间。+ let target = anchor(500);+ let element = createPanel(600);+ position(element, target);+ assertAbove(element, target);+ check.ok(element.getBoundingClientRect().height >= 459);++ // 长度与加载时机不应导致同一位置的浮窗被压缩。+ element = createPanel(1600);+ position(element, target);+ assertAbove(element, target);+ position(element, target);+ assertAbove(element, target);++ target = anchor(180);+ element = createPanel(1000);+ position(element, target);+ check.equal(element.getBoundingClientRect().top, target.bottom + 4);+ check.ok(element.getBoundingClientRect().bottom <= window.innerHeight - 8);+ check.ok(element.getBoundingClientRect().height > 600);++ // 内容较少时保留向下展开,后续内容可在可用范围内增长。+ target = anchor(500);+ element = createPanel(250);+ position(element, target);+ check.equal(element.getBoundingClientRect().top, target.bottom + 4);+ check.equal(element.getBoundingClientRect().height, 250);+ (element.lastElementChild.firstElementChild as HTMLElement).style.height = "1000px";+ check.ok(element.getBoundingClientRect().bottom <= window.innerHeight - 8);++ // 上方短浮窗应保持与锚点相邻,并限制后续内容向下侵入锚点。+ target = anchor(760);+ element = createPanel(250);+ position(element, target);+ check.equal(element.getBoundingClientRect().bottom, target.top - 8);+ check.equal(element.getBoundingClientRect().height, 250);+ (element.lastElementChild.firstElementChild as HTMLElement).style.height = "1000px";+ check.ok(element.getBoundingClientRect().bottom <= target.top - 8);++ // 窗口右侧和分数像素坐标仍保留拖拽空间。+ target = anchor(500.5, window.innerWidth - 20);+ element = createPanel(600);+ position(element, target);+ assertAbove(element, target);+ check.ok(element.getBoundingClientRect().right <= window.innerWidth - 8);++ // 主题采用不同盒模型、边框及内边距时,仍按浮窗外框计算可用空间。+ for (const boxSizing of ["content-box", "border-box"]) {+ element = createPanel(600);+ element.style.boxSizing = boxSizing;+ element.style.borderWidth = "3px";+ element.style.padding = "4px";+ position(element, target);+ assertAbove(element, target);+ }++ // 移动端插件共用浮窗组件,布局使用较宽的浮窗且无桌面工具栏。+ toolbar.remove();+ const sidebar = document.createElement("div");+ sidebar.id = "sidebar";+ document.body.append(sidebar);+ target = anchor(500);+ element = createPanel(600);+ element.style.width = "80vw";+ position(element, target);+ assertAbove(element, target);+ check.equal(element.getBoundingClientRect().top, 0);++ // 可用空间较小时,最小高度也必须服从窗口边界。+ toolbar.style.height = "600px";+ sidebar.remove();+ document.body.append(toolbar);+ target = anchor(730);+ element = createPanel(600);+ position(element, target);+ check.ok(element.getBoundingClientRect().top >= 600);+ check.ok(element.getBoundingClientRect().bottom <= window.innerHeight - 8);+ check.equal(element.getBoundingClientRect().top, target.bottom + 4);+ return "Block panel positioning cases passed";+};++test("block popovers use available space without collapsing or covering their anchors", {+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,+ timeout: 20000,+}, async () => {+ const source = ["../layout/getTopBarHeight.ts", "../util/setPosition.ts", "panelPosition.ts"].map(file =>+ transpileModule(readFileSync(path.join(__dirname, file), "utf8")+ .replace(/^import [\s\S]*?;\r?\n/gm, "").replace(/^export /gm, ""),+ {compilerOptions: {target: ScriptTarget.ES2021}}).outputText).join("\n");+ const css = compile(path.join(__dirname, "../assets/scss/business/_block.scss")).css;+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-panel-position-test-"));+ const script = path.join(temporary, "run.cjs");+ writeFileSync(script, `const {app, BrowserWindow} = require("electron");+app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});+app.commandLine.appendSwitch("disable-gpu");+app.whenReady().then(async () => {+ for (const width of [1200, 400]) {+ const win = new BrowserWindow({show: false, width, height: 900, useContentSize: true,+ webPreferences: {nodeIntegration: true, contextIsolation: false}});+ try {+ await win.loadURL("data:text/html,<html><body></body></html>");+ const result = await win.webContents.executeJavaScript(${JSON.stringify(+ `(() => { const __name = value => value; try {+ return (${browserCases.toString()})(${JSON.stringify(source)}, ${JSON.stringify(css)});+ } catch (error) { return error.stack; } })()`)});+ if (result !== "Block panel positioning cases passed") {+ throw new Error(result);+ }+ console.log(result);+ win.destroy();+ } catch (error) {+ console.error(error);+ win.destroy();+ app.exit(1);+ return;+ }+ }+ app.exit(0);+});`, "utf8");+ const env = {...process.env};+ delete env.ELECTRON_RUN_AS_NODE;+ try {+ const executable = require("electron") as unknown as string;+ const result = await promisify(execFile)(executable, [script], {env, timeout: 15000, windowsHide: true});+ assert.match(result.stdout, /Block panel positioning cases passed/);+ } finally {+ const resolvedTemporary = path.resolve(temporary);+ assert.equal(path.dirname(resolvedTemporary), path.resolve(tmpdir()));+ assert.ok(path.basename(resolvedTemporary).startsWith("siyuan-panel-position-test-"));+ rmSync(resolvedTemporary, {recursive: true, force: true});+ }+});diff --git a/app/src/block/panelPosition.ts b/app/src/block/panelPosition.tsnew file mode 100644index 0000000000..537042bb4a--- /dev/null+++ b/app/src/block/panelPosition.ts@@ -0,0 +1,24 @@+import {getTopBarHeight} from "../layout/getTopBarHeight";+import {setPosition} from "../util/setPosition";++export const positionBlockPanel = (element: HTMLElement, targetRect: DOMRect) => {+ // 根据完整内容高度选择展开方向,空间不足时使用较大的一侧。+ element.style.maxHeight = "";+ element.style.minHeight = "";+ const height = element.getBoundingClientRect().height;+ const above = Math.max(0, targetRect.top - getTopBarHeight() - 8);+ const below = Math.max(0, window.innerHeight - targetRect.bottom - 12);+ const openAbove = height > below && above > below;+ const availableHeight = Math.floor(openAbove ? Math.min(height, above) : below);+ const style = getComputedStyle(element);+ const extraHeight = style.boxSizing === "border-box" ? 0 :+ parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth) ++ parseFloat(style.paddingTop) + parseFloat(style.paddingBottom);+ const maxHeight = Math.max(0, availableHeight - extraHeight);+ element.style.minHeight = Math.min(parseFloat(style.minHeight) || 0, maxHeight) + "px";+ element.style.maxHeight = maxHeight + "px";++ // 先约束高度再定位,上方浮窗的底边紧邻锚点,并保留边缘拖拽空间。+ const top = openAbove ? targetRect.top - element.getBoundingClientRect().height - 8 : targetRect.bottom + 4;+ setPosition(element, targetRect.left, top, 0, 8);+};