Skip to content

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

:sparkles: Support pinning the database view toolbar while scrolling https://github.com/siyuan-note/siyuan/issues/18385

Daniel6492fdc82a07
diff --git a/app/src/assets/scss/business/_av.scss b/app/src/assets/scss/business/_av.scss
index 5d26786cf4..bc9ac9b569 100644
--- a/app/src/assets/scss/business/_av.scss
+++ b/app/src/assets/scss/business/_av.scss
@@ -85,6 +85,13 @@
box-shadow: 0 -1px inset var(--b3-theme-background-light);
font-size: 87.5%;
+ &--fixed {
+ background-color: var(--b3-parent-background);
+ box-sizing: border-box;
+ position: fixed;
+ z-index: 4;
+ }
+
&--show .block__icon {
opacity: 1;
}
diff --git a/app/src/protyle/render/av/gallery/render.ts b/app/src/protyle/render/av/gallery/render.ts
index 5016ecd8be..2a9e981975 100644
--- a/app/src/protyle/render/av/gallery/render.ts
+++ b/app/src/protyle/render/av/gallery/render.ts
@@ -8,7 +8,7 @@ import {processRender} from "../../../util/processCode";
import {getPageSize} from "../groups";
import {renderKanban} from "../kanban/render";
import {getBodyVirtualData, initVirtualScroll} from "../virtualScroll";
-import {getRowHTML, updateHeader} from "../row";
+import {getRowHTML, stickyRow, updateHeader} from "../row";
import {beginAVRender, finishAVLocate, getAVLocateParams, isCurrentAVRender, prepareAVLocate} from "../locate";
import {getCardStyle} from "./style";
import {setGroupFoldedStates} from "../groupFold";
@@ -168,6 +168,9 @@ export const afterRenderGallery = (options: ITableOptions) => {
finishAVLocate(options.blockElement, options.protyle, options.data);
return;
}
+ setTimeout(() => {
+ stickyRow(options.blockElement, options.protyle.contentElement, "top");
+ }, Constants.TIMEOUT_LOAD);
bindAvSearch({
blockElement: options.blockElement,
query: options.resetData.query,
diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.ts
index 2fea50ba4d..76ea6a4955 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -309,7 +309,8 @@ const afterRenderTable = (options: ITableOptions) => {
if (headerTransformElement) {
headerTransformElement.style.transform = options.resetData.headerTransform.transform;
}
- } else if (editRect && !options.protyle.options.action.includes(Constants.CB_GET_HISTORY)) {
+ }
+ if (editRect && !options.protyle.options.action.includes(Constants.CB_GET_HISTORY)) {
// 需等待渲染完,否则 getBoundingClientRect 错误 https://github.com/siyuan-note/siyuan/issues/13787
setTimeout(() => {
stickyRow(options.blockElement, options.protyle.contentElement, "top");
diff --git a/app/src/protyle/render/av/row.ts b/app/src/protyle/render/av/row.ts
index deaa2f26e3..364402c4f8 100644
--- a/app/src/protyle/render/av/row.ts
+++ b/app/src/protyle/render/av/row.ts
@@ -459,6 +459,14 @@ const addFixedRow = (item: HTMLElement, fixedClass: string, placeholderClass: st
item.insertAdjacentElement("afterend", placeholder);
};
+const addFixedViews = (item: HTMLElement, height: number) => {
+ item.classList.add("av__views--fixed");
+ const placeholder = document.createElement("div");
+ placeholder.className = "av__views-placeholder";
+ placeholder.style.height = height + "px";
+ item.insertAdjacentElement("afterend", placeholder);
+};
+
const removeFixedRow = (item: HTMLElement, fixedClass: string, placeholderClass: string) => {
if (!item.classList.contains(fixedClass)) {
return;
@@ -486,11 +494,12 @@ const syncFixedRowPos = (item: HTMLElement, bodyRect: DOMRect, scrollLeft: numbe
};
export const stickyRow = (blockElement: HTMLElement, scrollElement: HTMLElement, status: "top" | "bottom" | "all") => {
- if (blockElement.dataset.avType !== "table") {
- return;
- }
const skipFixed = hasTopClosestByAttribute(blockElement, "fold", "1");
if (skipFixed) {
+ const viewsElement = blockElement.querySelector(".av__views") as HTMLElement;
+ if (viewsElement) {
+ removeFixedRow(viewsElement, "av__views--fixed", "av__views-placeholder");
+ }
blockElement.querySelectorAll(".av__row--header--fixed").forEach((item: HTMLElement) => {
removeFixedRow(item, "av__row--header--fixed", "av__row--header-placeholder");
});
@@ -499,8 +508,9 @@ export const stickyRow = (blockElement: HTMLElement, scrollElement: HTMLElement,
});
return;
}
+ const isTable = blockElement.dataset.avType === "table";
const scrollEl = blockElement.querySelector(".av__scroll") as HTMLElement;
- if (scrollEl) {
+ if (isTable && scrollEl) {
bindHeaderScrollSync(blockElement, scrollEl);
}
@@ -510,6 +520,39 @@ export const stickyRow = (blockElement: HTMLElement, scrollElement: HTMLElement,
const scrollLeft = scrollEl ? scrollEl.scrollLeft : 0;
const doTop = status === "top" || status === "all";
const doBottom = status === "bottom" || status === "all";
+ const stickyTop = Math.round(elementRect.top);
+ const viewsElement = blockElement.querySelector(".av__views") as HTMLElement;
+ let viewsTask: {
+ element: HTMLElement;
+ height: number;
+ left: number;
+ top: number;
+ width: number;
+ shouldFix: boolean;
+ } | undefined;
+ let headerStickyTop = stickyTop;
+ if (doTop && viewsElement) {
+ const placeholderElement = viewsElement.classList.contains("av__views--fixed") &&
+ viewsElement.nextElementSibling?.classList.contains("av__views-placeholder")
+ ? viewsElement.nextElementSibling as HTMLElement
+ : viewsElement;
+ const viewsRect = placeholderElement.getBoundingClientRect();
+ const blockRect = blockElement.getBoundingClientRect();
+ const height = viewsElement.offsetHeight;
+ const shouldFix = height > 0 && viewsRect.top < stickyTop && blockRect.bottom > stickyTop;
+ const top = blockRect.bottom < stickyTop + height ? Math.round(blockRect.bottom - height) : stickyTop;
+ viewsTask = {
+ element: viewsElement,
+ height,
+ left: Math.round(viewsRect.left),
+ top,
+ width: Math.round(viewsRect.width),
+ shouldFix,
+ };
+ if (shouldFix) {
+ headerStickyTop = top + height;
+ }
+ }
// 第一遍:纯读取,收集每个 header/footer 的几何与判定结果
const headerTasks: Array<{
@@ -528,24 +571,22 @@ export const stickyRow = (blockElement: HTMLElement, scrollElement: HTMLElement,
scrollLeft: number;
scrollEl: HTMLElement;
}> = [];
- if (doTop) {
+ if (doTop && isTable) {
blockElement.querySelectorAll(".av__row--header").forEach((item: HTMLElement) => {
const body = item.parentElement as HTMLElement;
const bodyRect = body.getBoundingClientRect();
- const offset = Math.round(bodyRect.top - elementRect.top + scrollTop);
const headerH = item.offsetHeight;
- const bodyH = body.offsetHeight;
headerTasks.push({
item,
bodyRect,
headerH,
- shouldFix: scrollTop > offset && scrollTop < offset + bodyH,
+ shouldFix: bodyRect.top < headerStickyTop && bodyRect.bottom > headerStickyTop,
scrollLeft,
scrollEl,
});
});
}
- if (doBottom) {
+ if (doBottom && isTable) {
blockElement.querySelectorAll(".av__row--footer").forEach((item: HTMLElement) => {
if (!item.querySelector(".av__calc--ashow")) {
return;
@@ -569,8 +610,19 @@ export const stickyRow = (blockElement: HTMLElement, scrollElement: HTMLElement,
}
// 第二遍:纯写入,此时不再读取布局,仅触发一次重排
- const stickyTop = Math.round(elementRect.top);
const stickyBottom = Math.round(window.innerHeight - elementRect.bottom);
+ if (viewsTask) {
+ if (viewsTask.shouldFix) {
+ if (!viewsTask.element.classList.contains("av__views--fixed")) {
+ addFixedViews(viewsTask.element, viewsTask.height);
+ }
+ viewsTask.element.style.left = viewsTask.left + "px";
+ viewsTask.element.style.top = viewsTask.top + "px";
+ viewsTask.element.style.width = viewsTask.width + "px";
+ } else {
+ removeFixedRow(viewsTask.element, "av__views--fixed", "av__views-placeholder");
+ }
+ }
headerTasks.forEach((task) => {
const {item, bodyRect, headerH, shouldFix} = task;
if (shouldFix) {
@@ -578,9 +630,9 @@ export const stickyRow = (blockElement: HTMLElement, scrollElement: HTMLElement,
addFixedRow(item, "av__row--header--fixed", "av__row--header-placeholder", headerH, Math.round(bodyRect.width));
}
syncFixedRowPos(item, bodyRect, task.scrollLeft, task.scrollEl);
- item.style.top = bodyRect.bottom < stickyTop + headerH
+ item.style.top = bodyRect.bottom < headerStickyTop + headerH
? Math.round(bodyRect.bottom - headerH) + "px"
- : stickyTop + "px";
+ : headerStickyTop + "px";
} else {
removeFixedRow(item, "av__row--header--fixed", "av__row--header-placeholder");
}
diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.ts
index d9864905f0..adc76bc79c 100644
--- a/app/src/protyle/util/onGet.ts
+++ b/app/src/protyle/util/onGet.ts
@@ -484,9 +484,7 @@ export const enableProtyle = (protyle: IProtyle) => {
item.setAttribute("draggable", "true");
});
protyle.wysiwyg.element.querySelectorAll(".av").forEach((item: HTMLElement) => {
- if (item.querySelector(".av__scroll")) {
- stickyRow(item, protyle.contentElement, "all");
- }
+ stickyRow(item, protyle.contentElement, "all");
});
if (protyle.breadcrumb) {
const readonlyButton = protyle.breadcrumb.element.parentElement.querySelector('[data-type="readonly"]');
diff --git a/app/src/protyle/util/resize.ts b/app/src/protyle/util/resize.ts
index ecd19d0703..eedd62c06e 100644
--- a/app/src/protyle/util/resize.ts
+++ b/app/src/protyle/util/resize.ts
@@ -56,13 +56,9 @@ export const resize = (protyle: IProtyle) => {
if (protyle.scroll && protyle.scroll.element.parentElement.getAttribute("style")) {
protyle.scroll.element.parentElement.setAttribute("style", `--b3-dynamicscroll-width:${Math.min(protyle.contentElement.clientHeight - 49, 200)}px`);
}
- if (!protyle.disabled) {
- protyle.wysiwyg.element.querySelectorAll(".av").forEach((item: HTMLElement) => {
- if (item.querySelector(".av__scroll")) {
- stickyRow(item, protyle.contentElement, "all");
- }
- });
- }
+ protyle.wysiwyg.element.querySelectorAll(".av").forEach((item: HTMLElement) => {
+ stickyRow(item, protyle.contentElement, "all");
+ });
if (abs.width > MIN_ABS || isNaN(abs.width)) {
if (typeof window.echarts !== "undefined") {
protyle.wysiwyg.element.querySelectorAll('[data-subtype="echarts"], [data-subtype="mindmap"]').forEach((chartItem: HTMLElement) => {