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: Correct database backlink scrolling and remove navigation controls https://github.com/siyuan-note/siyuan/issues/19174
Daniel4ad5cb380c2b
diff --git a/app/src/protyle/render/av/backlink.ts b/app/src/protyle/render/av/backlink.tsindex 3ddf1f55b6..8dd0644580 100644--- a/app/src/protyle/render/av/backlink.ts+++ b/app/src/protyle/render/av/backlink.ts@@ -1,5 +1,5 @@import {setAVLocateRequest} from "./locate";-import {avRender} from "./render";+import {getBacklinkScrollElement} from "./backlinkScroll";export interface IBacklinkAVTarget {blockID: string;@@ -14,8 +14,7 @@ export interface IBacklinkAVTarget {}// 每个反链副本独立定位,避免同 ID 的正文或其他反链数据库接收到定位请求。-export const prepareBacklinkAV = (element: HTMLElement, protyle: IProtyle, targets: IBacklinkAVTarget[],- selections: Map<string, string>, scrollState?: {pending: boolean}) => {+export const prepareBacklinkAV = (element: HTMLElement, targets: IBacklinkAVTarget[], scrollState?: {pending: boolean}) => {const databases = element.matches(".av[data-node-id]") ? [element] :Array.from(element.querySelectorAll<HTMLElement>(".av[data-node-id]"));databases.forEach(database => {@@ -23,59 +22,19 @@ export const prepareBacklinkAV = (element: HTMLElement, protyle: IProtyle, targeif (!target?.matches.length) {return;}- const navigation = document.createElement("div");- navigation.className = "fn__flex fn__flex-center";- navigation.classList.toggle("fn__none", database.classList.contains("fn__none"));- navigation.contentEditable = "false";- const select = document.createElement("select");- select.className = "b3-select fn__flex-1";- select.style.minWidth = "0";- select.setAttribute("aria-label", window.siyuan.languages.ref);- target.matches.forEach((match, index) => {- const option = document.createElement("option");- option.value = index.toString();- option.textContent = `${index + 1}/${target.matches.length} ${match.title || window.siyuan.languages.untitled} - ${match.keyName}`;- select.appendChild(option);- });- const previousIndex = target.matches.findIndex(match => match.valueID === selections.get(target.blockID));- select.selectedIndex = Math.max(0, previousIndex);- const locate = (scroll: boolean) => {- const match = target.matches[select.selectedIndex];- selections.set(target.blockID, match.valueID);- database.querySelectorAll(".def--mark").forEach(item => item.classList.remove("def--mark"));- setAVLocateRequest(database, {- itemID: match.itemID,- keyID: match.keyID,- defIDs: match.defIDs,- select: false,- highlight: true,- persistView: false,- scroll,- });- };- const navigate = () => {- if (!database.isConnected) {- return;- }- locate(true);- database.removeAttribute("data-render");- void avRender(database, protyle);- };- select.addEventListener("change", navigate);- navigation.appendChild(select);- const button = document.createElement("button");- button.type = "button";- button.className = "b3-button b3-button--outline fn__flex-shrink";- button.textContent = window.siyuan.languages.jumpTo;- button.addEventListener("click", navigate);- navigation.appendChild(button);- // 控件位于数据库块外,数据库重新渲染不会移除命中位置列表。- ["click", "mousedown", "keydown", "change"].forEach(type => {- navigation.addEventListener(type, event => event.stopPropagation());- });- database.before(navigation);+ const match = target.matches[0];+ database.querySelectorAll(".def--mark").forEach(item => item.classList.remove("def--mark"));// 首次展开仅滚动到首个数据库命中,刷新和其他副本保留阅读位置。- locate(scrollState?.pending === true);+ setAVLocateRequest(database, {+ itemID: match.itemID,+ keyID: match.keyID,+ defIDs: match.defIDs,+ select: false,+ highlight: true,+ persistView: false,+ scroll: scrollState?.pending === true,+ scrollElement: getBacklinkScrollElement(database),+ });if (scrollState) {scrollState.pending = false;}diff --git a/app/src/protyle/render/av/backlinkScroll.test.ts b/app/src/protyle/render/av/backlinkScroll.test.tsnew file mode 100644index 0000000000..40e295ad1f--- /dev/null+++ b/app/src/protyle/render/av/backlinkScroll.test.ts@@ -0,0 +1,59 @@+import * as assert from "node:assert/strict";+import {describe, it} from "node:test";+import {getBacklinkScrollElement, revealBacklinkReference} from "./backlinkScroll";++const element = (classes: string[], parent?: HTMLElement): HTMLElement => {+ const node = {+ parentElement: parent,+ classList: {contains: (name: string) => classes.includes(name)},+ closest(selector: string) {+ let current = this as unknown as HTMLElement;+ while (current) {+ if (selector.split(",").some(item => current.classList.contains(item.trim().slice(1)))) {+ return current;+ }+ current = current.parentElement;+ }+ return null;+ },+ };+ return node as unknown as HTMLElement;+};++describe("database backlink scrolling", () => {+ it("scrolls the dock list instead of the expanded inner editor", () => {+ const panel = element(["sy__backlink"]);+ const list = element(["backlinkList"], panel);+ const innerEditor = element(["protyle-content"], list);+ assert.equal(getBacklinkScrollElement(element(["av"], innerEditor)), list);+ });++ it("scrolls the owning document for bottom backlinks", () => {+ const ownerEditor = element(["protyle-content"]);+ const panel = element(["sy__backlink", "sy__backlink--bottom"], ownerEditor);+ const list = element(["backlinkList"], panel);+ const innerEditor = element(["protyle-content"], list);+ assert.equal(getBacklinkScrollElement(element(["av"], innerEditor)), ownerEditor);+ });++ it("keeps ordinary database locating on the existing editor scroll path", () => {+ const editor = element(["protyle-content"]);+ assert.equal(getBacklinkScrollElement(element(["av"], editor)), undefined);+ });++ it("reveals a clipped reference inside a long field without scrolling outside the cell", () => {+ const cell = element(["av__cell"]);+ Object.assign(cell, {+ scrollLeft: 0, scrollTop: 0,+ clientWidth: 100, scrollWidth: 1000,+ clientHeight: 30, scrollHeight: 300,+ contains: (node: HTMLElement) => node === cell,+ getBoundingClientRect: () => ({left: 0, right: 100, top: 0, bottom: 30}),+ });+ const reference = element([], cell);+ reference.getBoundingClientRect = () => ({left: 500, right: 550, top: 120, bottom: 140}) as DOMRect;+ revealBacklinkReference(reference, cell);+ assert.equal(cell.scrollLeft, 500);+ assert.equal(cell.scrollTop, 120);+ });+});diff --git a/app/src/protyle/render/av/backlinkScroll.ts b/app/src/protyle/render/av/backlinkScroll.tsnew file mode 100644index 0000000000..a23d3fbf31--- /dev/null+++ b/app/src/protyle/render/av/backlinkScroll.ts@@ -0,0 +1,30 @@+// 反链内嵌编辑器随内容撑开,定位需要滚动面板或所属正文。+export const getBacklinkScrollElement = (element: HTMLElement): HTMLElement | undefined => {+ const backlink = element.closest<HTMLElement>(".sy__backlink");+ if (!backlink) {+ return;+ }+ if (backlink.classList.contains("sy__backlink--bottom")) {+ return backlink.closest<HTMLElement>(".protyle-content") || undefined;+ }+ return element.closest<HTMLElement>(".backlinkList, .backlinkMList") || undefined;+};++// 单行或限高字段会裁剪引用,先移动字段内部视口,再定位外层面板。+export const revealBacklinkReference = (reference: HTMLElement, cell: HTMLElement) => {+ let parent = reference.parentElement;+ while (parent && cell.contains(parent)) {+ const rect = parent.getBoundingClientRect();+ const target = reference.getBoundingClientRect();+ if (parent.scrollWidth > parent.clientWidth && (target.left < rect.left || target.right > rect.right)) {+ parent.scrollLeft += target.left - rect.left;+ }+ if (parent.scrollHeight > parent.clientHeight && (target.top < rect.top || target.bottom > rect.bottom)) {+ parent.scrollTop += target.top - rect.top;+ }+ if (parent === cell) {+ break;+ }+ parent = parent.parentElement;+ }+};diff --git a/app/src/protyle/render/av/locate.ts b/app/src/protyle/render/av/locate.tsindex a46086868f..f73e7ae18d 100644--- a/app/src/protyle/render/av/locate.ts+++ b/app/src/protyle/render/av/locate.ts@@ -8,12 +8,14 @@ import {setAVCellAnchor, setAVItemAnchor} from "./rangeSelect";import {updateAVRowSelect} from "./virtualScroll";import {getAVLocateViewChange} from "./locateView";import {applyAVColorPalette, getAVCustomColors} from "./color";+import {revealBacklinkReference} from "./backlinkScroll";export interface IAVLocateRequest {itemID: string;keyID?: string;defIDs?: string[];scroll?: boolean;+ scrollElement?: HTMLElement;groupID?: string;viewID?: string;select?: boolean;@@ -400,11 +402,18 @@ export const finishAVLocate = (blockElement: HTMLElement, protyle: IProtyle, datconst refs = Array.from(cell.querySelectorAll<HTMLElement>('[data-type~="block-ref"][data-id]')).filter(ref => request.defIDs?.includes(ref.dataset.id));refs.forEach(ref => ref.classList.add("def--mark"));+ if (refs[0] && request.scrollElement) {+ revealBacklinkReference(refs[0], cell);+ }targetElement = refs[0] || cell;}}if (request.scroll !== false) {- if (!request.keyID && data.viewType === "table" && data.target.index === 0 && !data.target.groupID) {+ if (request.scrollElement) {+ const scroller = request.scrollElement;+ const rect = scroller.getBoundingClientRect();+ scroller.scrollTop += targetElement.getBoundingClientRect().top - rect.top - scroller.clientHeight / 2;+ } else if (!request.keyID && data.viewType === "table" && data.target.index === 0 && !data.target.groupID) {const contentRect = protyle.contentElement.getBoundingClientRect();protyle.contentElement.scrollTop += blockElement.getBoundingClientRect().top - contentRect.top;} else {diff --git a/app/src/protyle/wysiwyg/renderBacklink.ts b/app/src/protyle/wysiwyg/renderBacklink.tsindex dd76e95a2b..7b2a66cd14 100644--- a/app/src/protyle/wysiwyg/renderBacklink.ts+++ b/app/src/protyle/wysiwyg/renderBacklink.ts@@ -32,7 +32,6 @@ interface IBacklinkDOMRecord {revision: string,anchor: HTMLElement,targets: IBacklinkAVTarget[],- selections: Map<string, string>,}const backlinkDOMRecords = new WeakMap<IProtyle, Map<string, IBacklinkDOMRecord>>();@@ -65,7 +64,6 @@ const createBacklinkDOMRecord = (item: IBacklinkData, index: number, id: string)revision: item.revision || "",anchor: nodes[0] as HTMLElement,targets: item.attributeViewTargets || [],- selections: new Map<string, string>(),},nodes,};@@ -79,7 +77,7 @@ const renderBacklinkDOMNodes = (protyle: IProtyle, nodes: Node[], record: IBacklimproveBreadcrumbAppearance(item);processRender(item);highlightRender(item);- prepareBacklinkAV(item, protyle, record.targets, record.selections, scrollState);+ prepareBacklinkAV(item, record.targets, scrollState);await avRender(item, protyle);blockRender(protyle, item);}));@@ -118,7 +116,6 @@ export const renderBacklink = (protyle: IProtyle, backlinkData: IBacklinkData[])clearViewFoldDefaults(protyle, id);const created = createBacklinkDOMRecord(item, index, id);if (record) {- created.record.selections = record.selections;created.nodes.forEach(node => element.insertBefore(node, record.anchor));removeBacklinkDOMRecord(record);} else {@@ -227,7 +224,7 @@ export const loadBreadcrumb = (protyle: IProtyle, element: HTMLElement) => {const record = backlinkDOMRecords.get(protyle)?.get(element.parentElement.dataset.backlinkId);getBacklinkDOMNodes(element.parentElement).forEach(node => {if (node instanceof HTMLElement) {- prepareBacklinkAV(node, protyle, record?.targets || [], record?.selections || new Map());+ prepareBacklinkAV(node, record?.targets || []);}});avRender(element.parentElement.parentElement, protyle);