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: Preserve relation dialog anchor after database refresh https://github.com/siyuan-note/siyuan/issues/19904
Daniel1feaf3e0b787
diff --git a/app/src/protyle/render/av/relation.ts b/app/src/protyle/render/av/relation.tsindex 96c44969f0..c16516940e 100644--- a/app/src/protyle/render/av/relation.ts+++ b/app/src/protyle/render/av/relation.ts@@ -561,12 +561,32 @@ export const bindRelationEvent = (options: {listNaturalMaxHeight = naturalHeight;return updateListMaxHeight();};+ let anchorElement = options.cellElements[options.cellElements.length - 1];+ let anchorRect = anchorElement.getBoundingClientRect();+ const anchorRow = anchorElement.closest<HTMLElement>(".av__row, .av__gallery-item, .av__calendar-item");+ const anchorColumnAttribute = anchorElement.dataset.colId ? "data-col-id" : "data-field-id";+ const anchorColumnID = anchorElement.getAttribute(anchorColumnAttribute);+ const anchorRowID = anchorRow?.dataset.id || anchorElement.dataset.rowId;+ const anchorRowClass = ["av__row", "av__gallery-item", "av__calendar-item"].find(name => anchorRow?.classList.contains(name));+ const anchorSelector = anchorRow ?+ `.${anchorRowClass}[data-id="${CSS.escape(anchorRowID)}"] [${anchorColumnAttribute}="${CSS.escape(anchorColumnID)}"]` :+ anchorRowID ? `[data-row-id="${CSS.escape(anchorRowID)}"][${anchorColumnAttribute}="${CSS.escape(anchorColumnID)}"]` :+ `.fn__flex-1[${anchorColumnAttribute}="${CSS.escape(anchorColumnID)}"]`;const positionMenu = (reset = false) => {if (reset) {resetPosition();}- const cellRect = options.cellElements[options.cellElements.length - 1].getBoundingClientRect();- setPosition(options.menuElement, cellRect.left, cellRect.bottom, cellRect.height, 0, true);+ // 数据库刷新会替换单元格,重新查找锚点;条目不可见时沿用最后的有效位置。+ if (!anchorElement?.isConnected) {+ anchorElement = options.blockElement.querySelector<HTMLElement>(anchorSelector);+ }+ if (anchorElement?.isConnected) {+ const rect = anchorElement.getBoundingClientRect();+ if (rect.width > 0 && rect.height > 0) {+ anchorRect = rect;+ }+ }+ setPosition(options.menuElement, anchorRect.left, anchorRect.bottom, anchorRect.height, 0, true);positionInitialized = true;};const resize = () => {diff --git a/app/tests/avRelationTemplate.test.js b/app/tests/avRelationTemplate.test.jsindex 499b18c1c6..f4abad863c 100644--- a/app/tests/avRelationTemplate.test.js+++ b/app/tests/avRelationTemplate.test.js@@ -43,6 +43,33 @@ const runCases = sources => {return dependencies[name];};const {renderCell, genCellValueByElement} = load("./cell");+ const {createPosition} = load("./relationPosition");+ for (const rowClass of ["av__row", "av__gallery-item", "av__calendar-item", ""]) {+ const blockElement = document.createElement("div");+ const attribute = rowClass === "av__gallery-item" ? "data-field-id" : "data-col-id";+ const markup = rowClass ? `<div class="${rowClass}" data-id="row"><div ${attribute}="column"></div></div>` :+ `<div data-row-id="row" ${attribute}="column"></div>`;+ blockElement.innerHTML = markup;+ document.body.replaceChildren(blockElement);+ const cell = blockElement.querySelector(`[${attribute}]`);+ cell.getBoundingClientRect = () => ({left: 200, bottom: 300, width: 100, height: 30});+ const positions = [];+ let resets = 0;+ const position = createPosition({blockElement, cellElements: [cell], menuElement: {}},+ () => resets++, (_menu, left, bottom, height) => positions.push([left, bottom, height]));+ position();+ blockElement.innerHTML = markup;+ cell.getBoundingClientRect = () => ({left: 0, bottom: 0, width: 0, height: 0});+ const replacement = blockElement.querySelector(`[${attribute}]`);+ replacement.getBoundingClientRect = () => ({left: 220, bottom: 320, width: 100, height: 30});+ position(true);+ replacement.getBoundingClientRect = () => ({left: 0, bottom: 0, width: 0, height: 0});+ position(true);+ blockElement.replaceChildren();+ position(true);+ assert.deepEqual(positions, [[200, 300, 30], [220, 320, 30], [220, 320, 30], [220, 320, 30]]);+ assert.equal(resets, 3);+ }const {genAVValueHTML, getAVTemplateInteractiveElement} = load("./attributeValue");for (const isDetached of [true, false]) {for (const renderedContent of [undefined, "", '<b>Formatted</b><a href="https://example.com">Link</a>']) {@@ -140,6 +167,14 @@ const runElectron = async () => {const sources = Object.fromEntries(["cell", "cellValue", "attributeValue", "relationLayout"].map(name => ["./" + name,ts.transpileModule(readFileSync(path.join(__dirname, `../src/protyle/render/av/${name}.ts`), "utf8"),{compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020}}).outputText]));+ const relationSource = readFileSync(path.join(__dirname, "../src/protyle/render/av/relation.ts"), "utf8");+ const positionStart = relationSource.indexOf(" let anchorElement =");+ const positionEnd = relationSource.indexOf(" const resize =", positionStart);+ assert.ok(positionStart > 0 && positionEnd > positionStart);+ sources["./relationPosition"] = ts.transpileModule(+ `export const createPosition = (options, resetPosition, setPosition) => { let positionInitialized = false;+${relationSource.slice(positionStart, positionEnd)} return positionMenu; };`,+ {compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020}}).outputText;await win.loadURL("data:text/html,<html><body></body></html>");await win.webContents.executeJavaScript(`(${runCases.toString()})(${JSON.stringify(sources)})`);console.log("AV relation template cases passed");