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 resized database cell inputs aligned within the viewport https://github.com/siyuan-note/siyuan/issues/20440
Daniela3504c8d7b8c
diff --git a/app/src/dialog/anchoredInputPosition.ts b/app/src/dialog/anchoredInputPosition.tsindex 1916ce7b80..e7d9e0c12c 100644--- a/app/src/dialog/anchoredInputPosition.ts+++ b/app/src/dialog/anchoredInputPosition.ts@@ -4,15 +4,15 @@ export const bindAnchoredInputPosition = (inputElement: HTMLElement, getAnchor:let frame = 0;let disposed = false;let observedAnchor: HTMLElement;- // 记录最近一次写入的尺寸;内联尺寸与之不同说明用户拖拽调整过大小,此后仅更新位置+ // 分别记录用户设置的尺寸和当前显示尺寸,空间恢复后还原用户设置。let appliedWidth = "";let appliedHeight = "";- let keepSize = false;+ let preferredSize: {width: number, height: number};const applySize = (width: number, height: number) => {- appliedWidth = `${width}px`;- appliedHeight = `${height}px`;- inputElement.style.width = appliedWidth;- inputElement.style.height = appliedHeight;+ inputElement.style.width = `${width}px`;+ inputElement.style.height = `${height}px`;+ appliedWidth = inputElement.style.width;+ appliedHeight = inputElement.style.height;};const update = () => {frame = 0;@@ -32,28 +32,34 @@ export const bindAnchoredInputPosition = (inputElement: HTMLElement, getAnchor:resizeObserver?.observe(anchorElement);observedAnchor = anchorElement;}- if (!keepSize && appliedWidth &&+ if (appliedWidth &&(inputElement.style.width !== appliedWidth || inputElement.style.height !== appliedHeight)) {- keepSize = true;+ // 仅更新用户实际拖动的轴,避免将另一轴临时受限的尺寸记作用户设置。+ preferredSize = {+ width: inputElement.style.width !== appliedWidth ? parseFloat(inputElement.style.width) :+ preferredSize?.width ?? parseFloat(appliedWidth),+ height: inputElement.style.height !== appliedHeight ? parseFloat(inputElement.style.height) :+ preferredSize?.height ?? parseFloat(appliedHeight),+ };}const cellRect = anchorElement.getBoundingClientRect();const maskRect = maskElement.getBoundingClientRect();const contentRect = contentElement?.getBoundingClientRect();+ const viewport = window.visualViewport;+ const viewportLeft = viewport?.offsetLeft || 0;+ const viewportTop = viewport?.offsetTop || 0;+ const viewportWidth = viewport?.width ?? window.innerWidth;+ const viewportHeight = viewport?.height ?? window.innerHeight;if (fitContent) {- const viewport = window.visualViewport;- const viewportLeft = viewport?.offsetLeft || 0;- const viewportTop = viewport?.offsetTop || 0;- const viewportWidth = viewport?.width || window.innerWidth;- const viewportHeight = viewport?.height || window.innerHeight;- let width = parseFloat(inputElement.style.width);- let height = parseFloat(inputElement.style.height);- if (!keepSize) {- width = Math.min(Math.max(cellRect.width, 240), Math.max(0, viewportWidth - 16));+ const width = Math.min(preferredSize?.width ?? Math.max(cellRect.width, 240), Math.max(0, viewportWidth - 16));+ let height = preferredSize?.height;+ if (!preferredSize) {inputElement.style.width = `${width}px`;inputElement.style.height = "auto";- height = Math.min(Math.max(cellRect.height, inputElement.scrollHeight), Math.max(0, viewportHeight - 16));- applySize(width, height);+ height = Math.max(cellRect.height, inputElement.scrollHeight);}+ height = Math.min(height, Math.max(0, viewportHeight - 16));+ applySize(width, height);const left = Math.max(viewportLeft + 8, Math.min(cellRect.left, viewportLeft + viewportWidth - width - 8));const top = Math.max(viewportTop + 8, Math.min(cellRect.top, viewportTop + viewportHeight - height - 8));inputElement.style.left = `${left - maskRect.left}px`;@@ -61,19 +67,20 @@ export const bindAnchoredInputPosition = (inputElement: HTMLElement, getAnchor:inputElement.style.visibility = "";return;}- const width = keepSize ? parseFloat(inputElement.style.width) :- contentRect ? Math.min(Math.max(cellRect.width, 25), contentRect.width) : Math.max(cellRect.width, 25);- const left = contentRect && (cellRect.left < contentRect.left || cellRect.left + width > contentRect.right) ?- contentRect.left : cellRect.left;- const height = contentRect ? Math.max(0, Math.min(cellRect.bottom, contentRect.bottom) - cellRect.top) :- cellRect.height;+ const visibleLeft = Math.max(viewportLeft, contentRect?.left ?? viewportLeft);+ const visibleTop = Math.max(viewportTop, contentRect?.top ?? viewportTop);+ const visibleRight = Math.min(viewportLeft + viewportWidth, contentRect?.right ?? viewportLeft + viewportWidth);+ const visibleBottom = Math.min(viewportTop + viewportHeight, contentRect?.bottom ?? viewportTop + viewportHeight);+ const left = Math.max(cellRect.left, visibleLeft);+ const top = Math.max(cellRect.top, visibleTop);+ const width = Math.min(preferredSize?.width ?? Math.max(cellRect.width, 25), Math.max(0, visibleRight - left));+ const height = Math.max(0, Math.min(preferredSize?.height ?? cellRect.bottom - top, visibleBottom - top));// 输入框位于独立遮罩中,使用当前单元格坐标并扣除遮罩原点,保持滚动和视口变化后的对齐。- inputElement.style.top = `${cellRect.top - maskRect.top}px`;+ inputElement.style.top = `${top - maskRect.top}px`;inputElement.style.left = `${left - maskRect.left}px`;- if (!keepSize) {- applySize(width, height);- }- inputElement.style.visibility = height > 0 ? "" : "hidden";+ applySize(width, height);+ inputElement.style.visibility = width > 0 && height > 0 && cellRect.right > visibleLeft &&+ cellRect.bottom > visibleTop ? "" : "hidden";};const schedule = () => {if (!disposed && !frame) {@@ -82,6 +89,7 @@ export const bindAnchoredInputPosition = (inputElement: HTMLElement, getAnchor:};const viewport = window.visualViewport;const resizeObserver = typeof ResizeObserver === "undefined" ? undefined : new ResizeObserver(schedule);+ resizeObserver?.observe(inputElement);if (contentElement) {resizeObserver?.observe(contentElement);}diff --git a/app/src/protyle/render/av/cellInputPosition.test.ts b/app/src/protyle/render/av/cellInputPosition.test.tsindex 7f1ae9691c..90b7e08760 100644--- a/app/src/protyle/render/av/cellInputPosition.test.ts+++ b/app/src/protyle/render/av/cellInputPosition.test.ts@@ -292,3 +292,136 @@ test("anchored input keeps a manually resized size while it follows the anchor",assert.equal(fit.input.style.top, "500px");fit.dispose();});++test("resized cell input stays aligned while constrained and restores its requested dimensions", () => {+ const f = setup();+ f.flush();+ assert.ok(f.observers[0].targets.includes(f.input));+ f.input.style.width = "300px";+ f.input.style.height = "120px";+ f.observers[0].notify();+ f.flush();+ f.content.rect = rect(16, 90, 250, 450);+ f.observers[0].notify();+ f.flush();+ assert.equal(f.input.style.left, "40px");+ assert.equal(f.input.style.top, "500px");+ assert.equal(f.input.style.width, "226px");+ assert.equal(f.input.style.height, "40px");+ // 自身尺寸变化产生的后续通知不能覆盖用户设置。+ f.observers[0].notify();+ f.flush();+ f.content.rect = rect(16, 90, 328, 710);+ f.observers[0].notify();+ f.flush();+ assert.equal(f.input.style.width, "300px");+ assert.equal(f.input.style.height, "120px");+ assert.equal(f.input.value, "draft");+ f.dispose();+});++test("resizing one constrained dimension preserves the requested size on the other axis", () => {+ const f = setup();+ f.input.style.width = "300px";+ f.input.style.height = "120px";+ f.content.rect = rect(16, 90, 250, 450);+ f.observers[0].notify();+ f.flush();+ f.input.style.width = "180px";+ f.observers[0].notify();+ f.flush();+ f.content.rect = rect(16, 90, 328, 710);+ f.observers[0].notify();+ f.flush();+ assert.equal(f.input.style.width, "180px");+ assert.equal(f.input.style.height, "120px");+ f.content.rect = rect(16, 90, 160, 450);+ f.observers[0].notify();+ f.flush();+ f.input.style.height = "30px";+ f.observers[0].notify();+ f.flush();+ f.content.rect = rect(16, 90, 328, 710);+ f.observers[0].notify();+ f.flush();+ assert.equal(f.input.style.width, "180px");+ assert.equal(f.input.style.height, "30px");+ f.dispose();+});++test("cell input respects the mobile visual viewport even when the layout viewport stays large", () => {+ const f = setup();+ f.input.style.width = "300px";+ f.input.style.height = "120px";+ f.anchor.rect = rect(40, 280, 220, 36);+ Object.assign(f.window.visualViewport, {offsetLeft: 20, offsetTop: 100, width: 240, height: 220});+ f.mask.rect = rect(8, 120, 360, 800);+ f.window.visualViewport.dispatchEvent(new Event("resize"));+ f.flush();+ assert.equal(f.input.style.left, "32px");+ assert.equal(f.input.style.top, "160px");+ assert.equal(f.input.style.width, "220px");+ assert.equal(f.input.style.height, "40px");+ Object.assign(f.window.visualViewport, {offsetLeft: 0, offsetTop: 0, width: 360, height: 800});+ f.window.visualViewport.dispatchEvent(new Event("resize"));+ f.flush();+ assert.equal(f.input.style.width, "300px");+ assert.equal(f.input.style.height, "120px");+ f.dispose();+});++test("resized input hides with an offscreen cell and restores its size when the cell returns", () => {+ const f = setup();+ f.input.style.width = "300px";+ f.input.style.height = "120px";+ for (const position of [rect(40, 40, 220, 36), rect(40, 810, 220, 36),+ rect(-220, 500, 220, 36), rect(350, 500, 220, 36)]) {+ f.anchor.rect = position;+ f.window.dispatchEvent(new Event("scroll"));+ f.flush();+ assert.equal(f.input.style.visibility, "hidden");+ }+ f.anchor.rect = rect(40, 500, 220, 36);+ f.window.dispatchEvent(new Event("scroll"));+ f.flush();+ assert.equal(f.input.style.width, "300px");+ assert.equal(f.input.style.height, "120px");+ assert.equal(f.input.style.visibility, "");+ f.dispose();+});++test("a partially visible cell keeps its input within the editor without changing automatic sizing", () => {+ const f = setup();+ f.anchor.rect = rect(240, 70, 220, 36);+ f.window.dispatchEvent(new Event("scroll"));+ f.flush();+ assert.equal(f.input.style.left, "240px");+ assert.equal(f.input.style.top, "90px");+ assert.equal(f.input.style.width, "104px");+ assert.equal(f.input.style.height, "16px");+ f.anchor.rect = rect(40, 500, 240, 40);+ f.window.dispatchEvent(new Event("scroll"));+ f.flush();+ assert.equal(f.input.style.width, "240px");+ assert.equal(f.input.style.height, "40px");+ f.dispose();+});++test("floating inputs also restore manually requested dimensions after a viewport constraint", () => {+ const f = setup(true);+ f.input.style.width = "300px";+ f.input.style.height = "200px";+ Object.assign(f.window.visualViewport, {width: 180, height: 150});+ f.window.visualViewport.dispatchEvent(new Event("resize"));+ f.flush();+ assert.equal(f.input.style.width, "164px");+ assert.equal(f.input.style.height, "134px");+ assert.equal(f.input.style.left, "8px");+ assert.equal(f.input.style.top, "8px");+ Object.assign(f.window.visualViewport, {width: 360, height: 800});+ f.window.visualViewport.dispatchEvent(new Event("resize"));+ f.flush();+ assert.equal(f.input.style.width, "300px");+ assert.equal(f.input.style.height, "200px");+ f.dispose();+});diff --git a/app/tests/cellInputPosition.test.js b/app/tests/cellInputPosition.test.jsnew file mode 100644index 0000000000..f575791f93--- /dev/null+++ b/app/tests/cellInputPosition.test.js@@ -0,0 +1,118 @@+const assert = require("node:assert/strict");+const fs = require("node:fs");+const os = require("node:os");+const path = require("node:path");++const runCases = async (source, css) => {+ const assert = require("node:assert/strict");+ const exports = {};+ new Function("exports", source)(exports);+ const style = document.createElement("style");+ style.textContent = css;+ document.head.append(style);+ const content = document.createElement("div");+ content.style.cssText = "position:absolute;left:20px;top:30px;width:600px;height:480px;overflow:auto";+ const cell = document.createElement("div");+ cell.style.cssText = "position:absolute;left:40px;top:50px;width:220.333px;height:36.333px";+ content.append(cell);+ document.body.append(content);+ const mask = document.createElement("div");+ mask.className = "av__mask";+ const input = document.createElement("textarea");+ input.className = "b3-text-field";+ input.style.position = "absolute";+ input.value = "draft\n".repeat(40);+ mask.append(input);+ document.body.append(mask);+ const settle = async () => {+ for (let i = 0; i < 4; i++) {+ await new Promise(resolve => requestAnimationFrame(resolve));+ }+ };+ const dispose = exports.bindAnchoredInputPosition(input, () => cell, content);+ const closeTo = (actual, expected) => assert.ok(Math.abs(actual - expected) < 0.1, `${actual} != ${expected}`);+ const aligned = () => {+ const inputRect = input.getBoundingClientRect();+ const cellRect = cell.getBoundingClientRect();+ closeTo(inputRect.left, cellRect.left);+ closeTo(inputRect.top, cellRect.top);+ };+ await settle();+ // 浏览器会规范化小数尺寸;自动更新不能因此被误判为手动拖拽。+ cell.style.width = "230.667px";+ await settle();+ closeTo(input.getBoundingClientRect().width, cell.getBoundingClientRect().width);+ input.style.width = "420px";+ input.style.height = "240px";+ await settle();+ aligned();+ // 模拟缩小编辑区,仅由真实 ResizeObserver 驱动定位。+ content.style.width = "260px";+ content.style.height = "130px";+ await settle();+ aligned();+ closeTo(input.getBoundingClientRect().width, 220);+ closeTo(input.getBoundingClientRect().height, 80);+ closeTo(input.getBoundingClientRect().right, content.getBoundingClientRect().right);+ closeTo(input.getBoundingClientRect().bottom, content.getBoundingClientRect().bottom);+ input.style.width = "190px";+ await settle();+ content.style.width = "600px";+ content.style.height = "480px";+ await settle();+ closeTo(input.getBoundingClientRect().width, 190);+ closeTo(input.getBoundingClientRect().height, 240);+ aligned();+ // 监听内联尺寸写入,确保自身尺寸通知收敛,且不会持续触发定位更新。+ let mutations = 0;+ const observer = new MutationObserver(records => {mutations += records.length;});+ observer.observe(input, {attributes: true, attributeFilter: ["style"]});+ await settle();+ assert.equal(mutations, 0);+ observer.disconnect();+ assert.equal(input.value, "draft\n".repeat(40));+ dispose();+ mask.remove();+ content.remove();+ style.remove();+};++if (process.versions.electron && process.type === "browser") {+ const {app, BrowserWindow} = require("electron");+ app.setPath("userData", process.argv[2]);+ app.whenReady().then(async () => {+ const win = new BrowserWindow({show: false, width: 800, height: 600,+ webPreferences: {nodeIntegration: true, contextIsolation: false, backgroundThrottling: false, offscreen: true}});+ let code = 0;+ try {+ await win.loadURL("data:text/html,<html><body></body></html>");+ const ts = require("typescript");+ const source = ts.transpileModule(fs.readFileSync(path.join(__dirname, "../src/dialog/anchoredInputPosition.ts"), "utf8"), {+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},+ }).outputText;+ const css = require("sass").compile(path.join(__dirname, "../src/assets/scss/component/_text-field.scss")).css;+ await win.webContents.executeJavaScript(`(${runCases.toString()})(${JSON.stringify(source)}, ${JSON.stringify(css)})`);+ } catch (error) {+ console.error(error);+ code = 1;+ } finally {+ win.destroy();+ app.exit(code);+ }+ });+} else {+ const {test} = require("node:test");+ const {execFile} = require("node:child_process");+ const {promisify} = require("node:util");+ test("cell input layout follows resizing and restores manual dimensions without observer loops", async () => {+ const profile = fs.mkdtempSync(path.join(os.tmpdir(), "siyuan-cell-input-position-"));+ const env = {...process.env};+ delete env.ELECTRON_RUN_AS_NODE;+ try {+ await promisify(execFile)(require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 30000});+ } finally {+ assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));+ fs.rmSync(profile, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});+ }+ });+}