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 database cell inputs aligned with their cells https://github.com/siyuan-note/siyuan/issues/20071
Daniel0d492f0aa982
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.tsindex 79ec7ca443..4404ff0ade 100644--- a/app/src/protyle/render/av/cell.ts+++ b/app/src/protyle/render/av/cell.ts@@ -60,6 +60,7 @@ import {openAVRichTextEditor} from "./richTextEditor";import {getAVData, getAVPrimaryCell} from "./virtualScroll";import {AV_CELL_EDITOR_CLOSE_EVENT} from "./cellEditor";import {getAVBlockIconHTML, renderAVBlockIcon} from "./blockIcon";+import {bindAVCellInputPosition} from "./cellInputPosition";export {cellValueIsEmpty} from "./cellValue";@@ -631,7 +632,6 @@ export const popTextCell = (protyle: IProtyle, cellElements: HTMLElement[], type}cellRect = cellElements[0].getBoundingClientRect();let html = "";- let height = cellRect.height;const cssStyle = getComputedStyle(cellElements[0]);const storedCellValue = getStoredCellValueByElement(cellElements[0]);const hasRenderedTemplate = cellElements[0].matches(".av__celltext--template") ||@@ -657,19 +657,11 @@ export const popTextCell = (protyle: IProtyle, cellElements: HTMLElement[], typereturn;}}- const inputTop = options?.positionByMenu ? cellRect.bottom : cellRect.top;- let style = `font-family:${cssStyle.fontFamily};font-size:${cssStyle.fontSize};line-height:${cssStyle.lineHeight};padding:${cssStyle.padding};position:absolute;top: ${inputTop}px;`;- if (contentElement && !options?.positionByMenu) {- const contentRect = contentElement.getBoundingClientRect();- if (cellRect.bottom > contentRect.bottom) {- height = contentRect.bottom - cellRect.top;- }- const width = Math.min(Math.max(cellRect.width, 25), contentRect.width);- style = `style='height: ${height}px;width:${width}px;left: ${(cellRect.left < contentRect.left || cellRect.left + width > contentRect.right) ? contentRect.left : cellRect.left}px;${style}'`;- } else {- const width = options?.positionByMenu ? Math.max(cellRect.width, 200) : Math.max(cellRect.width, 25);- style = `style='height: ${height}px;width:${width}px;left: ${cellRect.left}px;${style}'`;+ let style = `font-family:${cssStyle.fontFamily};font-size:${cssStyle.fontSize};line-height:${cssStyle.lineHeight};padding:${cssStyle.padding};position:absolute;`;+ if (options?.positionByMenu) {+ style += `height:${cellRect.height}px;width:${Math.max(cellRect.width, 200)}px;left:${cellRect.left}px;top:${cellRect.bottom}px;`;}+ style = `style='${style}'`;if (["text", "email", "phone", "block", "template"].includes(type)) {html = `<textarea ${style} spellcheck="false" class="b3-text-field"></textarea>`;@@ -780,6 +772,24 @@ export const popTextCell = (protyle: IProtyle, cellElements: HTMLElement[], typeif (inputElement) {if (options?.positionByMenu) {setPosition(inputElement, cellRect.left, cellRect.bottom, cellRect.height);+ } else {+ const anchorRow = cellElements[0].closest<HTMLElement>(".av__row[data-id], .av__gallery-item[data-id]");+ const groupID = cellElements[0].closest<HTMLElement>(".av__body[data-group-id]")?.dataset.groupId;+ const fieldAttr = isTableLikeView(viewType) ? "data-col-id" : "data-field-id";+ const fieldID = cellElements[0].getAttribute(fieldAttr);+ const anchorSelector = anchorRow && fieldID ?+ `${groupID ? `.av__body[data-group-id="${escapeAttr(groupID)}"] ` : ""}` ++ `.${anchorRow.classList.contains("av__row") ? "av__row" : "av__gallery-item"}[data-id="${anchorRow.dataset.id}"] .av__cell[${fieldAttr}="${fieldID}"]` : "";+ bindAVCellInputPosition(inputElement, () => {+ if (!cellElements[0].isConnected && anchorSelector && blockElement.isConnected) {+ // 行重绘后按原分组、行和字段重新绑定,输入内容继续保留在同一个编辑框中。+ const anchor = blockElement.querySelector<HTMLElement>(anchorSelector);+ if (anchor) {+ cellElements[0] = anchor;+ }+ }+ return cellElements[0];+ }, contentElement || undefined);}if (["text", "email", "phone", "block", "template"].includes(type)) {const storedContent = type === "template" ? undefined :diff --git a/app/src/protyle/render/av/cellInputPosition.test.ts b/app/src/protyle/render/av/cellInputPosition.test.tsnew file mode 100644index 0000000000..25ba42dd9a--- /dev/null+++ b/app/src/protyle/render/av/cellInputPosition.test.ts@@ -0,0 +1,235 @@+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 compiled = transpileModule(readFileSync("src/protyle/render/av/cellInputPosition.ts", "utf8"), {+ compilerOptions: {module: ModuleKind.CommonJS},+}).outputText;++const rect = (left: number, top: number, width: number, height: number) => ({+ left, top, width, height, right: left + width, bottom: top + height,+});++const setup = () => {+ const frames = new Map<number, FrameRequestCallback>();+ let nextFrame = 0;+ class TestWindow extends EventTarget {+ visualViewport = new EventTarget();+ requestAnimationFrame(callback: FrameRequestCallback) {+ frames.set(++nextFrame, callback);+ return nextFrame;+ }+ cancelAnimationFrame(id: number) {+ frames.delete(id);+ }+ }+ const window = new TestWindow();+ const observers: TestObserver[] = [];+ class TestObserver {+ connected = true;+ targets: unknown[] = [];+ constructor(private callback: () => void) {+ observers.push(this);+ }+ observe(target: unknown) {+ this.targets.push(target);+ }+ unobserve(target: unknown) {+ this.targets = this.targets.filter(item => item !== target);+ }+ disconnect() {+ this.connected = false;+ }+ notify() {+ if (this.connected) {+ this.callback();+ }+ }+ }+ const mask = {isConnected: true, rect: rect(0, 0, 360, 800), getBoundingClientRect() {return this.rect;}};+ let anchor = {isConnected: true, rect: rect(40, 500, 220, 36), getBoundingClientRect() {return this.rect;}};+ const content = {rect: rect(16, 90, 328, 710), getBoundingClientRect() {return this.rect;}};+ const input = {parentElement: mask, style: {} as Record<string, string>, value: "draft"};+ const exports: {bindAVCellInputPosition?: (input: unknown, anchor: () => unknown, content?: unknown) => () => void} = {};+ runInNewContext(compiled, {+ exports, window, document: {body: {}}, MutationObserver: TestObserver, ResizeObserver: TestObserver,+ });+ const dispose = exports.bindAVCellInputPosition(input, () => anchor, content);+ const flush = () => {+ const pending = Array.from(frames.values());+ frames.clear();+ pending.forEach(callback => callback(0));+ };+ return {window, observers, mask, content, input, frames, dispose, flush,+ get anchor() {return anchor;}, set anchor(value) {anchor = value;}};+};++test("cell input follows native keyboard opening and closing without replacing the draft", () => {+ const fixture = setup();+ assert.equal(fixture.input.style.top, "500px");+ // 聚焦后页面上移,原生键盘改变布局视口。+ fixture.anchor.rect = rect(40, 280, 220, 36);+ fixture.content.rect = rect(16, 90, 328, 330);+ fixture.window.dispatchEvent(new Event("resize"));+ fixture.window.dispatchEvent(new Event("scroll"));+ assert.equal(fixture.frames.size, 1);+ fixture.flush();+ assert.equal(fixture.input.style.top, "280px");+ fixture.anchor.rect = rect(40, 500, 220, 36);+ fixture.content.rect = rect(16, 90, 328, 710);+ fixture.window.dispatchEvent(new Event("resize"));+ fixture.flush();+ assert.equal(fixture.input.style.top, "500px");+ assert.equal(fixture.input.value, "draft");+ fixture.dispose();+});++test("visual viewport changes keep the input aligned when the fixed mask origin moves", () => {+ const fixture = setup();+ fixture.flush();+ fixture.anchor.rect = rect(40, 300, 220, 36);+ fixture.mask.rect = rect(8, 120, 360, 400);+ fixture.window.visualViewport.dispatchEvent(new Event("resize"));+ fixture.flush();+ assert.equal(fixture.input.style.top, "180px");+ assert.equal(fixture.input.style.left, "32px");+ fixture.mask.rect = rect(8, 150, 360, 400);+ fixture.window.visualViewport.dispatchEvent(new Event("scroll"));+ fixture.flush();+ assert.equal(fixture.input.style.top, "150px");+ fixture.dispose();+});++test("desktop scrolling and narrow editor resizing preserve input width and bottom clipping", () => {+ const fixture = setup();+ fixture.flush();+ fixture.anchor.rect = rect(-60, 380, 400, 36);+ fixture.content.rect = rect(16, 90, 160, 310);+ fixture.window.dispatchEvent(new Event("scroll"));+ fixture.observers[0].notify();+ fixture.flush();+ assert.equal(fixture.input.style.left, "16px");+ assert.equal(fixture.input.style.width, "160px");+ assert.equal(fixture.input.style.height, "20px");+ fixture.anchor.rect = rect(40, 420, 220, 36);+ fixture.window.dispatchEvent(new Event("scroll"));+ fixture.flush();+ assert.equal(fixture.input.style.height, "0px");+ assert.equal(fixture.input.style.visibility, "hidden");+ fixture.dispose();+});++test("sticky header DOM changes update the anchor even when its dimensions stay the same", () => {+ const fixture = setup();+ fixture.flush();+ fixture.anchor.rect = rect(40, 460, 220, 36);+ fixture.observers[1].notify();+ fixture.flush();+ assert.equal(fixture.input.style.top, "460px");+ fixture.dispose();+});++test("detached anchors hide the input until its current cell is rebound", () => {+ const fixture = setup();+ fixture.flush();+ fixture.anchor.isConnected = false;+ const previousAnchor = fixture.anchor;+ fixture.observers[1].notify();+ fixture.flush();+ assert.equal(fixture.input.style.visibility, "hidden");+ fixture.anchor = {isConnected: true, rect: rect(40, 480, 220, 36), getBoundingClientRect() {return this.rect;}};+ fixture.observers[1].notify();+ fixture.flush();+ assert.equal(fixture.input.style.top, "480px");+ assert.equal(fixture.input.style.visibility, "");+ assert.equal(fixture.input.value, "draft");+ assert.ok(!fixture.observers[0].targets.includes(previousAnchor));+ assert.ok(fixture.observers[0].targets.includes(fixture.anchor));+ fixture.dispose();+});++test("removing the mask cancels pending positioning and releases observers and event listeners", () => {+ const fixture = setup();+ fixture.mask.isConnected = false;+ fixture.observers[1].notify();+ assert.equal(fixture.frames.size, 0);+ assert.ok(fixture.observers.every(observer => !observer.connected));+ fixture.window.dispatchEvent(new Event("scroll"));+ fixture.window.dispatchEvent(new Event("resize"));+ fixture.window.visualViewport.dispatchEvent(new Event("scroll"));+ fixture.window.visualViewport.dispatchEvent(new Event("resize"));+ assert.equal(fixture.frames.size, 0);+ fixture.dispose();+});++const cellSource = readFileSync("src/protyle/render/av/cell.ts", "utf8");+const popCompiled = transpileModule(cellSource.slice(cellSource.indexOf("export const popTextCell ="),+ cellSource.indexOf("const updateCellValueByInput =")), {+ compilerOptions: {module: ModuleKind.CommonJS},+}).outputText;++const openInput = (positionByMenu = false) => {+ let getAnchor: () => unknown;+ let menuPositioned = false;+ let inserted = false;+ let selector = "";+ const input = Object.assign(new EventTarget(), {+ value: "", dataset: {}, select: () => {}, focus: () => {+ assert.ok(positionByMenu ? menuPositioned : getAnchor, "positioning must be bound before focus");+ },+ });+ const mask = Object.assign(new EventTarget(), {querySelector: () => input});+ const cell = {+ isConnected: true,+ dataset: {},+ getBoundingClientRect: () => rect(40, 500, 220, 36),+ matches: () => false,+ querySelector: (name: string) => name === ".av__celltext" ? {textContent: "draft"} : null,+ getAttribute: () => "column-id",+ closest: (name: string) => name.includes("av__body") ? {dataset: {groupId: "group-id"}} :+ {dataset: {id: "row-id"}, classList: {contains: () => true}},+ };+ const replacement = {...cell};+ const cells = [cell];+ const block = {+ isConnected: true,+ dataset: {nodeId: "block-id"},+ getAttribute: () => "table",+ querySelector: (value: string) => {+ selector = value;+ return replacement;+ },+ };+ const exports: {popTextCell?: (protyle: unknown, cells: unknown[], type: string, options: unknown) => void} = {};+ runInNewContext(popCompiled, {+ exports, isTableLikeView: () => true, hasClosestBlock: () => block, hasClosestByClassName: (): null => null,+ getComputedStyle: () => ({}), getStoredCellValueByElement: (): undefined => undefined,+ escapeAttr: (value: string) => value,+ AV_CELL_EDITOR_CLOSE_EVENT: "close", callMobileAppShowKeyboard: () => {},+ window: {siyuan: {menus: {menu: {remove: () => {}}}, zIndex: 0}},+ document: {querySelector: () => inserted ? mask : null, body: {insertAdjacentHTML: () => {inserted = true;}}},+ bindAVCellInputPosition: (_input: unknown, getter: () => unknown) => {getAnchor = getter;},+ setPosition: () => {menuPositioned = true;},+ });+ exports.popTextCell({}, cells, "block", {scrollIntoView: false, positionByMenu});+ return {cell, cells, replacement, input, getAnchor, menuPositioned, getSelector: () => selector};+};++test("opening a primary cell binds positioning before focus and retains its group identity after redraw", () => {+ const fixture = openInput();+ assert.equal(fixture.input.value, "draft");+ fixture.cell.isConnected = false;+ assert.equal(fixture.getAnchor(), fixture.replacement);+ assert.equal(fixture.cells[0], fixture.replacement);+ assert.equal(fixture.getSelector(),+ '.av__body[data-group-id="group-id"] .av__row[data-id="row-id"] .av__cell[data-col-id="column-id"]');+ assert.equal(fixture.input.value, "draft");+});++test("batch inputs opened below a menu keep menu positioning instead of cell tracking", () => {+ const fixture = openInput(true);+ assert.equal(fixture.menuPositioned, true);+ assert.equal(fixture.getAnchor, undefined);+});diff --git a/app/src/protyle/render/av/cellInputPosition.ts b/app/src/protyle/render/av/cellInputPosition.tsnew file mode 100644index 0000000000..0d954e34d4--- /dev/null+++ b/app/src/protyle/render/av/cellInputPosition.ts@@ -0,0 +1,82 @@+export const bindAVCellInputPosition = (inputElement: HTMLElement, getAnchor: () => HTMLElement,+ contentElement?: HTMLElement) => {+ const maskElement = inputElement.parentElement;+ let frame = 0;+ let disposed = false;+ let observedAnchor: HTMLElement;+ const update = () => {+ frame = 0;+ const anchorElement = getAnchor();+ if (!maskElement.isConnected) {+ dispose();+ return;+ }+ if (!anchorElement?.isConnected) {+ inputElement.style.visibility = "hidden";+ return;+ }+ if (observedAnchor !== anchorElement) {+ if (observedAnchor) {+ resizeObserver?.unobserve(observedAnchor);+ }+ resizeObserver?.observe(anchorElement);+ observedAnchor = anchorElement;+ }+ const cellRect = anchorElement.getBoundingClientRect();+ const maskRect = maskElement.getBoundingClientRect();+ const contentRect = contentElement?.getBoundingClientRect();+ const 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;+ // 输入框位于独立遮罩中,使用当前单元格坐标并扣除遮罩原点,保持滚动和视口变化后的对齐。+ inputElement.style.top = `${cellRect.top - maskRect.top}px`;+ inputElement.style.left = `${left - maskRect.left}px`;+ inputElement.style.width = `${width}px`;+ inputElement.style.height = `${height}px`;+ inputElement.style.visibility = height > 0 ? "" : "hidden";+ };+ const schedule = () => {+ if (!disposed && !frame) {+ frame = window.requestAnimationFrame(update);+ }+ };+ const viewport = window.visualViewport;+ const resizeObserver = typeof ResizeObserver === "undefined" ? undefined : new ResizeObserver(schedule);+ if (contentElement) {+ resizeObserver?.observe(contentElement);+ }+ // 固定表头占位和行重绘也会改变单元格位置;遮罩移除时统一释放所有监听。+ const ownerObserver = new MutationObserver(() => {+ if (!maskElement.isConnected) {+ dispose();+ } else {+ schedule();+ }+ });+ const dispose = () => {+ if (disposed) {+ return;+ }+ disposed = true;+ window.cancelAnimationFrame(frame);+ frame = 0;+ resizeObserver?.disconnect();+ ownerObserver.disconnect();+ window.removeEventListener("scroll", schedule, true);+ window.removeEventListener("resize", schedule);+ viewport?.removeEventListener("resize", schedule);+ viewport?.removeEventListener("scroll", schedule);+ };+ window.addEventListener("scroll", schedule, {capture: true, passive: true});+ window.addEventListener("resize", schedule);+ viewport?.addEventListener("resize", schedule);+ viewport?.addEventListener("scroll", schedule);+ ownerObserver.observe(document.body, {childList: true, subtree: true, attributes: true, attributeFilter: ["class"]});+ update();+ // 焦点及键盘桥接调用结束后再次测量,避免沿用聚焦前的布局位置。+ schedule();+ return dispose;+};