Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Preserve database view tab scroll position https://github.com/siyuan-note/siyuan/issues/19703

Daniel902028d96899
diff --git a/app/src/protyle/render/av/calendar/render.ts b/app/src/protyle/render/av/calendar/render.ts
index 9a5e984215..727db38275 100644
--- a/app/src/protyle/render/av/calendar/render.ts
+++ b/app/src/protyle/render/av/calendar/render.ts
@@ -10,6 +10,7 @@ import {finishAVLocate} from "../locate";
import {createAttributeViewItem} from "../newItemTemplate";
import {openDatabaseRowByData} from "../openDatabaseRow";
import {avRender, genTabHeaderHTML, updateSearch} from "../render";
+import {replaceAVContainer} from "../container";
import {renderAVRichTextElements} from "../richText";
import {deleteRow} from "../row";
import {bindAvSearch} from "../search";
@@ -228,7 +229,7 @@ export const renderCalendar = async (blockElement: HTMLElement, protyle: IProtyl
}
}
blockElement.removeAttribute(Constants.ATTRIBUTE_V_SCROLL);
- blockElement.firstElementChild.outerHTML = `<div class="av__container fn__block">
+ replaceAVContainer(blockElement, `<div class="av__container fn__block">
${genTabHeaderHTML(data, !!query || isSearching, editable, blockElement, editable && !!dateColumn)}
<div class="av__calendar" contenteditable="false">
<div class="av__calendar-toolbar">
@@ -247,7 +248,7 @@ export const renderCalendar = async (blockElement: HTMLElement, protyle: IProtyl
</div>
</div>
<div class="av__cursor" contenteditable="true">${Constants.ZWSP}</div>
- </div>`;
+ </div>`);
blockElement.dataset.render = "true";
setAVData(blockElement, data);
const root = blockElement.querySelector<HTMLElement>(".av__calendar");
diff --git a/app/src/protyle/render/av/container.ts b/app/src/protyle/render/av/container.ts
new file mode 100644
index 0000000000..d37b91a323
--- /dev/null
+++ b/app/src/protyle/render/av/container.ts
@@ -0,0 +1,33 @@
+// 重建数据库容器时保留标签栏位置,仅在首次渲染或切换视图时滚动到目标标签。
+export const replaceAVContainer = (blockElement: HTMLElement, html: string) => {
+ const selector = ":scope > .av__container > .av__header > .av__views > .layout-tab-bar";
+ const previousTabBar = blockElement.querySelector<HTMLElement>(selector);
+ const scrollLeft = previousTabBar?.scrollLeft || 0;
+ const previousViewID = previousTabBar?.querySelector<HTMLElement>(".item--focus")?.dataset.id;
+
+ blockElement.firstElementChild.outerHTML = html;
+
+ const tabBar = blockElement.querySelector<HTMLElement>(selector);
+ if (!tabBar) {
+ return;
+ }
+ tabBar.scrollLeft = scrollLeft;
+ const focusedTab = tabBar.querySelector<HTMLElement>(".item--focus");
+ if (!focusedTab || focusedTab.dataset.id === previousViewID || tabBar.clientWidth === 0) {
+ return;
+ }
+
+ const tabBarRect = tabBar.getBoundingClientRect();
+ const left = tabBarRect.left + tabBar.clientLeft;
+ const right = left + tabBar.clientWidth;
+ const focusedRect = focusedTab.getBoundingClientRect();
+ // 标签宽于可视区域且覆盖两侧时保持位置,其余情况仅滚动到最近的边缘。
+ if (focusedRect.left < left && focusedRect.right > right) {
+ return;
+ }
+ if (focusedRect.left < left) {
+ tabBar.scrollLeft += Math.max(focusedRect.left - left, focusedRect.right - right);
+ } else if (focusedRect.right > right) {
+ tabBar.scrollLeft += Math.min(focusedRect.left - left, focusedRect.right - right);
+ }
+};
diff --git a/app/src/protyle/render/av/gallery/render.ts b/app/src/protyle/render/av/gallery/render.ts
index 2af2606a6a..9b169388c2 100644
--- a/app/src/protyle/render/av/gallery/render.ts
+++ b/app/src/protyle/render/av/gallery/render.ts
@@ -26,6 +26,7 @@ import {getCardStyle} from "./style";
import {setGroupFoldedStates} from "../groupFold";
import {getPublishAVView} from "../publishState";
import {renderAVRichTextElements} from "../richText";
+import {replaceAVContainer} from "../container";
interface IIds {
groupId: string,
@@ -99,13 +100,13 @@ const renderGroupGallery = (options: ITableOptions) => {
}
});
if (options.renderAll) {
- options.blockElement.firstElementChild.outerHTML = `<div class="av__container fn__block">
+ replaceAVContainer(options.blockElement, `<div class="av__container fn__block">
${genTabHeaderHTML(options.data, isSearching || !!query, !options.protyle.disabled, options.blockElement)}
<div>
${avBodyHTML}
</div>
<div class="av__cursor" contenteditable="true">${Constants.ZWSP}</div>
-</div>`;
+</div>`);
} else {
options.blockElement.querySelector(".av__header").nextElementSibling.innerHTML = avBodyHTML;
}
@@ -177,10 +178,6 @@ export const afterRenderGallery = (options: ITableOptions) => {
}
}
}
- const focusViewElement = options.blockElement.querySelector(".layout-tab-bar .item--focus") as HTMLElement;
- if (focusViewElement) {
- options.blockElement.querySelector(".layout-tab-bar").scrollLeft = focusViewElement.offsetLeft - 30;
- }
if (options.cb) {
options.cb(options.data);
}
@@ -341,7 +338,7 @@ export const renderGallery = async (options: {
}
const bodyHTML = getGalleryHTML(view, options.blockElement, virtualData.all);
if (options.renderAll) {
- options.blockElement.firstElementChild.outerHTML = `<div class="av__container fn__block">
+ replaceAVContainer(options.blockElement, `<div class="av__container fn__block">
${genTabHeaderHTML(data, resetData.isSearching || !!resetData.query, !options.protyle.disabled, options.blockElement)}
<div>
<div class="av__body" data-group-id="" data-page-size="${view.pageSize}"${virtualData.all?.locate ? ' data-av-locate-window="true"' : ""}>
@@ -349,7 +346,7 @@ export const renderGallery = async (options: {
</div>
</div>
<div class="av__cursor" contenteditable="true">${Constants.ZWSP}</div>
-</div>`;
+</div>`);
} else {
const bodyElement = options.blockElement.querySelector(".av__body") as HTMLElement;
bodyElement.innerHTML = bodyHTML;
diff --git a/app/src/protyle/render/av/kanban/render.ts b/app/src/protyle/render/av/kanban/render.ts
index 79eb49c149..cd428fe1eb 100644
--- a/app/src/protyle/render/av/kanban/render.ts
+++ b/app/src/protyle/render/av/kanban/render.ts
@@ -6,6 +6,7 @@ import {getPageSize} from "../groups";
import {fetchSyncPost} from "../../../../util/fetch";
import {Constants} from "../../../../constants";
import {avRender, genTabHeaderHTML} from "../render";
+import {replaceAVContainer} from "../container";
import {afterRenderGallery, renderGallery} from "../gallery/render";
import {escapeAttr, escapeHtml} from "../../../../util/escape";
import {getRowHTML} from "../row";
@@ -238,14 +239,14 @@ export const renderKanban = async (options: {
}
});
if (options.renderAll) {
- options.blockElement.firstElementChild.outerHTML = `<div class="av__container fn__block">
+ replaceAVContainer(options.blockElement, `<div class="av__container fn__block">
${genTabHeaderHTML(data, resetData.isSearching || !!resetData.query,
!options.protyle.disabled && !queryEmbedElement, options.blockElement, !queryEmbedElement)}
<div class="av__kanban${isSelectGroup ? " av__kanban--bg" : ""}" data-group-options="${escapeAttr(JSON.stringify(groupOptions))}" style="${getCardStyle(view)}">
${bodyHTML}
</div>
<div class="av__cursor" contenteditable="true">${Constants.ZWSP}</div>
-</div>`;
+</div>`);
} else {
const kanbanElement = options.blockElement.querySelector(".av__kanban");
kanbanElement.innerHTML = bodyHTML;
diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.ts
index a6a7e698cf..a51c792979 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -63,6 +63,7 @@ import {getAVHeaderEditingState} from "./headerEditing";
import {getAVColorStyle} from "./color";
import {getContextFilterKeyID} from "./contextFilterState";
import {isAVCellPanelForBlock} from "./panelTarget";
+import {replaceAVContainer} from "./container";
interface IIds {
groupId: string,
@@ -342,13 +343,13 @@ const renderGroupTable = (options: ITableOptions) => {
}
});
if (options.renderAll) {
- options.blockElement.firstElementChild.outerHTML = `<div class="av__container">
+ replaceAVContainer(options.blockElement, `<div class="av__container">
${genTabHeaderHTML(options.data, isSearching || !!query, !options.protyle.disabled, options.blockElement)}
<div class="av__scroll">
${avBodyHTML}
</div>
<div class="av__cursor" contenteditable="true">${Constants.ZWSP}</div>
-</div>`;
+</div>`);
} else {
options.blockElement.firstElementChild.querySelector(".av__scroll").innerHTML = avBodyHTML;
}
@@ -529,10 +530,6 @@ const afterRenderTable = (options: ITableOptions) => {
}
}
}
- const focusViewElement = options.blockElement.querySelector(".layout-tab-bar .item--focus") as HTMLElement;
- if (focusViewElement) {
- options.blockElement.querySelector(".layout-tab-bar").scrollLeft = focusViewElement.offsetLeft - 30;
- }
if (options.cb) {
options.cb(options.data);
}
@@ -747,13 +744,13 @@ export const avRender = async (element: Element, protyle: IProtyle, cb?: (data:
${getTableHTMLs(view, e, resetData.virtualData.all)}
</div>`;
if (renderAll) {
- e.firstElementChild.outerHTML = `<div class="av__container">
+ replaceAVContainer(e, `<div class="av__container">
${genTabHeaderHTML(data, resetData.isSearching || !!resetData.query, !protyle.disabled, e)}
<div class="av__scroll">
${avBodyHTML}
</div>
<div class="av__cursor" contenteditable="true">${Constants.ZWSP}</div>
-</div>`;
+</div>`);
} else {
e.firstElementChild.querySelector(".av__scroll").innerHTML = avBodyHTML;
}