fix(sidebar): stop sticky headers at connection boundaries

Closes #9996
This commit is contained in:
t8y2 committed 2026-09-26 02:26:53 +08:00
1 parent cc456c3d4f
commit 6e3709c73b
5 files changed
+46 -48

No files matched your search

@@ -844,16 +844,6 @@ function readExpandedSidebarSchemas(): Array<{ id: string; label: string }> {
return expanded;
}
// The plain (non-virtualized) renderer uses the same container selection as the
// virtual sticky overlay: database containers take precedence, while schema
// containers stick only in trees without a database-level container.
function isPlainStickyContainerNode(index: number): boolean {
// Mirror the virtual branch's sticky overlay: both suppress sticky headers
// while a search filter is active so filtered rows don't pin at the top.
if (isTreeSearchFiltering.value) return false;
return flatTreeIndex.value.stickyContainerIndexByIndex[index] === index;
}
const sidebarLayoutMonitor = createSidebarLayoutMonitor({
readContext: () => ({
flatNodeCount: flatNodes.value.length,
@@ -1058,10 +1048,9 @@ watch(
);
// --- Sticky database header ---
// RecycleScroller positions each row absolutely, so CSS `position: sticky` on
// a database row can't work. Instead we overlay a pinned row from this parent
// component, tracking scroll offset to find the topmost visible database-level
// ancestor. The overlay reuses <TreeItem>, so collapse/expand comes for free.
// Both renderers use the same overlay instead of CSS `position: sticky` on
// individual rows. A shared overlay can be pushed out by the next connection
// boundary, while native sticky rows would cover that non-sticky connection.
const stickyScrollTop = ref(0);
const sidebarScrollMetrics = ref({ scrollTop: 0, scrollLeft: 0, clientHeight: 0, clientWidth: 0, scrollHeight: 0, scrollWidth: 0 });
const isScrollingSidebar = ref(false);
@@ -1080,7 +1069,7 @@ function updateSidebarScrollMetrics() {
return;
}
if (useVirtualTree.value) stickyScrollTop.value = scroller.scrollTop;
stickyScrollTop.value = scroller.scrollTop;
sidebarScrollMetrics.value = {
scrollTop: scroller.scrollTop,
scrollLeft: scroller.scrollLeft,
@@ -1141,28 +1130,30 @@ watch(
{ flush: "post" },
);
const stickyNode = computed<FlatTreeNode | null>(() => {
if (!useVirtualTree.value || isTreeSearchFiltering.value) return null;
const stickyContainerIndex = computed(() => {
if (isTreeSearchFiltering.value) return -1;
const nodes = flatNodes.value;
const len = nodes.length;
if (len === 0) return null;
if (len === 0) return -1;
const topIndex = Math.min(Math.floor(stickyScrollTop.value / SIDEBAR_TREE_ROW_HEIGHT), len - 1);
const containerIndex = flatTreeIndex.value.stickyContainerIndexByIndex[topIndex] ?? -1;
if (containerIndex < 0) return null;
return stickyScrollTop.value > containerIndex * SIDEBAR_TREE_ROW_HEIGHT ? nodes[containerIndex] : null;
if (containerIndex < 0) return -1;
return stickyScrollTop.value > containerIndex * SIDEBAR_TREE_ROW_HEIGHT ? containerIndex : -1;
});
const stickyNode = computed<FlatTreeNode | null>(() => flatNodes.value[stickyContainerIndex.value] ?? null);
const stickyHeaderStyle = computed<CSSProperties>(() => {
const node = stickyNode.value;
if (!node) return {};
const currentIndex = flatTreeIndex.value.flatNodeIndexById.get(node.id) ?? -1;
const currentIndex = stickyContainerIndex.value;
if (currentIndex < 0) return {};
// The next peer index is precomputed with the flat-tree snapshot so scrolling
// never scans the remaining tree. Connection boundaries reset the lookup.
const nextDatabaseIndex = SCHEMA_LEVEL_TYPES.has(node.type) ? flatTreeIndex.value.nextSchemaContainerIndexByIndex[currentIndex] : flatTreeIndex.value.nextDatabaseContainerIndexByIndex[currentIndex];
if (nextDatabaseIndex < 0) return {};
const distanceToNext = nextDatabaseIndex * SIDEBAR_TREE_ROW_HEIGHT - stickyScrollTop.value;
const node = flatNodes.value[currentIndex];
if (!node) return {};
const nextContainerIndex = SCHEMA_LEVEL_TYPES.has(node.type) ? flatTreeIndex.value.nextSchemaContainerIndexByIndex[currentIndex] : flatTreeIndex.value.nextDatabaseContainerIndexByIndex[currentIndex];
const nextBoundaryIndex = flatTreeIndex.value.nextBoundaryIndexByIndex[currentIndex] ?? -1;
const nextCollisionIndex = nextContainerIndex < 0 ? nextBoundaryIndex : nextBoundaryIndex < 0 ? nextContainerIndex : Math.min(nextContainerIndex, nextBoundaryIndex);
if (nextCollisionIndex < 0) return {};
const distanceToNext = nextCollisionIndex * SIDEBAR_TREE_ROW_HEIGHT - stickyScrollTop.value;
if (distanceToNext >= SIDEBAR_TREE_ROW_HEIGHT) return {};
return {
transform: `translateY(${Math.min(0, distanceToNext - SIDEBAR_TREE_ROW_HEIGHT)}px)`,
@@ -1495,7 +1486,7 @@ async function flashSidebarNode(nodeId: string) {
function topOcclusionHeightForSidebarNode(nodeId: string): number {
const sticky = stickyNode.value;
if (!useVirtualTree.value || !sticky || sticky.id === nodeId) return 0;
if (!sticky || sticky.id === nodeId) return 0;
return SIDEBAR_TREE_ROW_HEIGHT;
}
@@ -2712,7 +2703,7 @@ defineExpose({ focusSearch, createNewGroup, collapseAllTreeNodes, locateTabInSid
<div ref="plainTreeScrollerRef" class="sidebar-tree connection-tree-scroller h-full overflow-y-auto" :class="sidebarTreeOverflowClass" :style="sidebarTreeScrollerStyle" @click="clearSidebarSelection" @scroll.passive="onTreeScroll">
<div class="connection-tree-content">
<TreeItem
v-for="(item, index) in flatNodes"
v-for="item in flatNodes"
:key="item.renderKey"
:node="item.node"
:depth="item.depth"
@@ -2721,13 +2712,22 @@ defineExpose({ focusSearch, createNewGroup, collapseAllTreeNodes, locateTabInSid
:pending-rename="pendingRenameNodeId === item.node.id"
:highlighted="highlightedNodeId === item.id"
:comment-label-width="sidebarCommentLabelWidths.get(item.node.id)"
:sticky-header="isPlainStickyContainerNode(index)"
@context-menu="(event, node) => openSidebarContextMenu(event, node, contextMenuSlot.onContextMenu)"
@rename-started="pendingRenameNodeId = null"
@group-created="startRenamingCreatedGroup"
/>
</div>
</div>
<div v-if="stickyNode" class="sticky-database-header pointer-events-auto absolute inset-x-0 top-0 z-[5]" :style="stickyHeaderStyle">
<TreeItem
:node="stickyNode.node"
:depth="stickyNode.depth"
:reorder-disabled="true"
:reference-drag-disabled="true"
:comment-label-width="sidebarCommentLabelWidths.get(stickyNode.node.id)"
@context-menu="(event, node) => openSidebarContextMenu(event, node, contextMenuSlot.onContextMenu)"
/>
</div>
<div
v-if="hasSidebarVerticalOverflow"
ref="sidebarScrollbarTrackRef"
@@ -202,10 +202,6 @@ const props = defineProps<{
pendingRename?: boolean;
highlighted?: boolean;
commentLabelWidth?: number;
/** Plain (non-virtualized) renderer: make database/schema container rows
* stick to the top of the tree scroller while their children scroll under
* them (mirrors the overlay sticky header of the virtual renderer). */
stickyHeader?: boolean;
}>();
const emit = defineEmits<{
@@ -1610,7 +1606,7 @@ function onKeydown(event: KeyboardEvent) {
</LightTooltip>
</div>
<div v-else :class="{ 'sidebar-tree-item--sticky': stickyHeader }" @contextmenu="onTreeItemContextMenu">
<div v-else @contextmenu="onTreeItemContextMenu">
<LightTooltip :text="visibleLabel(node)" :disabled="isTooltipDisabled()" side="right" :side-offset="8" :delay="0" :close-delay="30" :surface="detailTooltip ? 'popover' : 'foreground'">
<div
ref="rowRef"
@@ -1934,18 +1930,6 @@ function onKeydown(event: KeyboardEvent) {
background-color: transparent !important;
}
/* Plain (non-virtualized) renderer: database/schema container rows stick to
the top of the tree scroller while their children scroll under them,
mirroring the overlay sticky header the virtual renderer uses. The row is
min-h-7, so a solid background guarantees no content shows through while
rows slide underneath. */
.sidebar-tree-item--sticky {
position: sticky;
top: 0;
z-index: 2;
background-color: var(--sidebar);
}
.tree-item-connection-tint:hover::before {
background-color: var(--tree-connection-row-hover-bg, var(--tree-connection-row-bg));
}
@@ -55,6 +55,9 @@ describe("createFlatTreeIndex", () => {
expect(index.nextSchemaContainerIndexByIndex[2]).toBe(4);
expect(index.nextSchemaContainerIndexByIndex[4]).toBe(-1);
expect(index.nextSchemaContainerIndexByIndex[9]).toBe(-1);
expect(index.nextBoundaryIndexByIndex[1]).toBe(8);
expect(index.nextBoundaryIndexByIndex[6]).toBe(8);
expect(index.nextBoundaryIndexByIndex[9]).toBe(-1);
});
it("keeps indexes isolated across connections without database containers", () => {
@@ -65,6 +68,7 @@ describe("createFlatTreeIndex", () => {
expect(index.stickyContainerIndexByIndex[2]).toBe(1);
expect(index.stickyContainerIndexByIndex[4]).toBe(-1);
expect(index.nextSchemaContainerIndexByIndex[1]).toBe(-1);
expect(index.nextBoundaryIndexByIndex[1]).toBe(3);
expect(index.flatNodeIndexById.get("table-b")).toBe(4);
});
@@ -42,6 +42,7 @@ export interface FlatTreeIndex {
stickyContainerIndexByIndex: Int32Array;
nextDatabaseContainerIndexByIndex: Int32Array;
nextSchemaContainerIndexByIndex: Int32Array;
nextBoundaryIndexByIndex: Int32Array;
}
interface FlatTreeIndexOptions {
@@ -140,9 +141,11 @@ export function createFlatTreeIndex(nodes: readonly FlatTreeNode[], options: Fla
const stickyContainerIndexByIndex = new Int32Array(nodes.length);
const nextDatabaseContainerIndexByIndex = new Int32Array(nodes.length);
const nextSchemaContainerIndexByIndex = new Int32Array(nodes.length);
const nextBoundaryIndexByIndex = new Int32Array(nodes.length);
stickyContainerIndexByIndex.fill(-1);
nextDatabaseContainerIndexByIndex.fill(-1);
nextSchemaContainerIndexByIndex.fill(-1);
nextBoundaryIndexByIndex.fill(-1);
let databaseContainerIndex = -1;
let schemaContainerIndex = -1;
@@ -170,9 +173,12 @@ export function createFlatTreeIndex(nodes: readonly FlatTreeNode[], options: Fla
let nextDatabaseContainerIndex = -1;
let nextSchemaContainerIndex = -1;
let nextBoundaryIndex = -1;
for (let index = nodes.length - 1; index >= 0; index -= 1) {
const item = nodes[index];
nextBoundaryIndexByIndex[index] = nextBoundaryIndex;
if (options.isBoundary(item.type)) {
nextBoundaryIndex = index;
nextDatabaseContainerIndex = -1;
nextSchemaContainerIndex = -1;
continue;
@@ -193,5 +199,6 @@ export function createFlatTreeIndex(nodes: readonly FlatTreeNode[], options: Fla
stickyContainerIndexByIndex,
nextDatabaseContainerIndexByIndex,
nextSchemaContainerIndexByIndex,
nextBoundaryIndexByIndex,
};
}