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; 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({ const sidebarLayoutMonitor = createSidebarLayoutMonitor({
readContext: () => ({ readContext: () => ({
flatNodeCount: flatNodes.value.length, flatNodeCount: flatNodes.value.length,
@@ -1058,10 +1048,9 @@ watch(
); );
// --- Sticky database header --- // --- Sticky database header ---
// RecycleScroller positions each row absolutely, so CSS `position: sticky` on // Both renderers use the same overlay instead of CSS `position: sticky` on
// a database row can't work. Instead we overlay a pinned row from this parent // individual rows. A shared overlay can be pushed out by the next connection
// component, tracking scroll offset to find the topmost visible database-level // boundary, while native sticky rows would cover that non-sticky connection.
// ancestor. The overlay reuses <TreeItem>, so collapse/expand comes for free.
const stickyScrollTop = ref(0); const stickyScrollTop = ref(0);
const sidebarScrollMetrics = ref({ scrollTop: 0, scrollLeft: 0, clientHeight: 0, clientWidth: 0, scrollHeight: 0, scrollWidth: 0 }); const sidebarScrollMetrics = ref({ scrollTop: 0, scrollLeft: 0, clientHeight: 0, clientWidth: 0, scrollHeight: 0, scrollWidth: 0 });
const isScrollingSidebar = ref(false); const isScrollingSidebar = ref(false);
@@ -1080,7 +1069,7 @@ function updateSidebarScrollMetrics() {
return; return;
} }
if (useVirtualTree.value) stickyScrollTop.value = scroller.scrollTop; stickyScrollTop.value = scroller.scrollTop;
sidebarScrollMetrics.value = { sidebarScrollMetrics.value = {
scrollTop: scroller.scrollTop, scrollTop: scroller.scrollTop,
scrollLeft: scroller.scrollLeft, scrollLeft: scroller.scrollLeft,
@@ -1141,28 +1130,30 @@ watch(
{ flush: "post" }, { flush: "post" },
); );
const stickyNode = computed<FlatTreeNode | null>(() => { const stickyContainerIndex = computed(() => {
if (!useVirtualTree.value || isTreeSearchFiltering.value) return null; if (isTreeSearchFiltering.value) return -1;
const nodes = flatNodes.value; const nodes = flatNodes.value;
const len = nodes.length; 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 topIndex = Math.min(Math.floor(stickyScrollTop.value / SIDEBAR_TREE_ROW_HEIGHT), len - 1);
const containerIndex = flatTreeIndex.value.stickyContainerIndexByIndex[topIndex] ?? -1; const containerIndex = flatTreeIndex.value.stickyContainerIndexByIndex[topIndex] ?? -1;
if (containerIndex < 0) return null; if (containerIndex < 0) return -1;
return stickyScrollTop.value > containerIndex * SIDEBAR_TREE_ROW_HEIGHT ? nodes[containerIndex] : null; 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 stickyHeaderStyle = computed<CSSProperties>(() => {
const node = stickyNode.value; const currentIndex = stickyContainerIndex.value;
if (!node) return {};
const currentIndex = flatTreeIndex.value.flatNodeIndexById.get(node.id) ?? -1;
if (currentIndex < 0) return {}; if (currentIndex < 0) return {};
// The next peer index is precomputed with the flat-tree snapshot so scrolling const node = flatNodes.value[currentIndex];
// never scans the remaining tree. Connection boundaries reset the lookup. if (!node) return {};
const nextDatabaseIndex = SCHEMA_LEVEL_TYPES.has(node.type) ? flatTreeIndex.value.nextSchemaContainerIndexByIndex[currentIndex] : flatTreeIndex.value.nextDatabaseContainerIndexByIndex[currentIndex]; const nextContainerIndex = SCHEMA_LEVEL_TYPES.has(node.type) ? flatTreeIndex.value.nextSchemaContainerIndexByIndex[currentIndex] : flatTreeIndex.value.nextDatabaseContainerIndexByIndex[currentIndex];
if (nextDatabaseIndex < 0) return {}; const nextBoundaryIndex = flatTreeIndex.value.nextBoundaryIndexByIndex[currentIndex] ?? -1;
const distanceToNext = nextDatabaseIndex * SIDEBAR_TREE_ROW_HEIGHT - stickyScrollTop.value; 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 {}; if (distanceToNext >= SIDEBAR_TREE_ROW_HEIGHT) return {};
return { return {
transform: `translateY(${Math.min(0, distanceToNext - SIDEBAR_TREE_ROW_HEIGHT)}px)`, 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 { function topOcclusionHeightForSidebarNode(nodeId: string): number {
const sticky = stickyNode.value; 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; 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 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"> <div class="connection-tree-content">
<TreeItem <TreeItem
v-for="(item, index) in flatNodes" v-for="item in flatNodes"
:key="item.renderKey" :key="item.renderKey"
:node="item.node" :node="item.node"
:depth="item.depth" :depth="item.depth"
@@ -2721,13 +2712,22 @@ defineExpose({ focusSearch, createNewGroup, collapseAllTreeNodes, locateTabInSid
:pending-rename="pendingRenameNodeId === item.node.id" :pending-rename="pendingRenameNodeId === item.node.id"
:highlighted="highlightedNodeId === item.id" :highlighted="highlightedNodeId === item.id"
:comment-label-width="sidebarCommentLabelWidths.get(item.node.id)" :comment-label-width="sidebarCommentLabelWidths.get(item.node.id)"
:sticky-header="isPlainStickyContainerNode(index)"
@context-menu="(event, node) => openSidebarContextMenu(event, node, contextMenuSlot.onContextMenu)" @context-menu="(event, node) => openSidebarContextMenu(event, node, contextMenuSlot.onContextMenu)"
@rename-started="pendingRenameNodeId = null" @rename-started="pendingRenameNodeId = null"
@group-created="startRenamingCreatedGroup" @group-created="startRenamingCreatedGroup"
/> />
</div> </div>
</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 <div
v-if="hasSidebarVerticalOverflow" v-if="hasSidebarVerticalOverflow"
ref="sidebarScrollbarTrackRef" ref="sidebarScrollbarTrackRef"
@@ -202,10 +202,6 @@ const props = defineProps<{
pendingRename?: boolean; pendingRename?: boolean;
highlighted?: boolean; highlighted?: boolean;
commentLabelWidth?: number; 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<{ const emit = defineEmits<{
@@ -1610,7 +1606,7 @@ function onKeydown(event: KeyboardEvent) {
</LightTooltip> </LightTooltip>
</div> </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'"> <LightTooltip :text="visibleLabel(node)" :disabled="isTooltipDisabled()" side="right" :side-offset="8" :delay="0" :close-delay="30" :surface="detailTooltip ? 'popover' : 'foreground'">
<div <div
ref="rowRef" ref="rowRef"
@@ -1934,18 +1930,6 @@ function onKeydown(event: KeyboardEvent) {
background-color: transparent !important; 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 { .tree-item-connection-tint:hover::before {
background-color: var(--tree-connection-row-hover-bg, var(--tree-connection-row-bg)); 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[2]).toBe(4);
expect(index.nextSchemaContainerIndexByIndex[4]).toBe(-1); expect(index.nextSchemaContainerIndexByIndex[4]).toBe(-1);
expect(index.nextSchemaContainerIndexByIndex[9]).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", () => { it("keeps indexes isolated across connections without database containers", () => {
@@ -65,6 +68,7 @@ describe("createFlatTreeIndex", () => {
expect(index.stickyContainerIndexByIndex[2]).toBe(1); expect(index.stickyContainerIndexByIndex[2]).toBe(1);
expect(index.stickyContainerIndexByIndex[4]).toBe(-1); expect(index.stickyContainerIndexByIndex[4]).toBe(-1);
expect(index.nextSchemaContainerIndexByIndex[1]).toBe(-1); expect(index.nextSchemaContainerIndexByIndex[1]).toBe(-1);
expect(index.nextBoundaryIndexByIndex[1]).toBe(3);
expect(index.flatNodeIndexById.get("table-b")).toBe(4); expect(index.flatNodeIndexById.get("table-b")).toBe(4);
}); });
@@ -42,6 +42,7 @@ export interface FlatTreeIndex {
stickyContainerIndexByIndex: Int32Array; stickyContainerIndexByIndex: Int32Array;
nextDatabaseContainerIndexByIndex: Int32Array; nextDatabaseContainerIndexByIndex: Int32Array;
nextSchemaContainerIndexByIndex: Int32Array; nextSchemaContainerIndexByIndex: Int32Array;
nextBoundaryIndexByIndex: Int32Array;
} }
interface FlatTreeIndexOptions { interface FlatTreeIndexOptions {
@@ -140,9 +141,11 @@ export function createFlatTreeIndex(nodes: readonly FlatTreeNode[], options: Fla
const stickyContainerIndexByIndex = new Int32Array(nodes.length); const stickyContainerIndexByIndex = new Int32Array(nodes.length);
const nextDatabaseContainerIndexByIndex = new Int32Array(nodes.length); const nextDatabaseContainerIndexByIndex = new Int32Array(nodes.length);
const nextSchemaContainerIndexByIndex = new Int32Array(nodes.length); const nextSchemaContainerIndexByIndex = new Int32Array(nodes.length);
const nextBoundaryIndexByIndex = new Int32Array(nodes.length);
stickyContainerIndexByIndex.fill(-1); stickyContainerIndexByIndex.fill(-1);
nextDatabaseContainerIndexByIndex.fill(-1); nextDatabaseContainerIndexByIndex.fill(-1);
nextSchemaContainerIndexByIndex.fill(-1); nextSchemaContainerIndexByIndex.fill(-1);
nextBoundaryIndexByIndex.fill(-1);
let databaseContainerIndex = -1; let databaseContainerIndex = -1;
let schemaContainerIndex = -1; let schemaContainerIndex = -1;
@@ -170,9 +173,12 @@ export function createFlatTreeIndex(nodes: readonly FlatTreeNode[], options: Fla
let nextDatabaseContainerIndex = -1; let nextDatabaseContainerIndex = -1;
let nextSchemaContainerIndex = -1; let nextSchemaContainerIndex = -1;
let nextBoundaryIndex = -1;
for (let index = nodes.length - 1; index >= 0; index -= 1) { for (let index = nodes.length - 1; index >= 0; index -= 1) {
const item = nodes[index]; const item = nodes[index];
nextBoundaryIndexByIndex[index] = nextBoundaryIndex;
if (options.isBoundary(item.type)) { if (options.isBoundary(item.type)) {
nextBoundaryIndex = index;
nextDatabaseContainerIndex = -1; nextDatabaseContainerIndex = -1;
nextSchemaContainerIndex = -1; nextSchemaContainerIndex = -1;
continue; continue;
@@ -193,5 +199,6 @@ export function createFlatTreeIndex(nodes: readonly FlatTreeNode[], options: Fla
stickyContainerIndexByIndex, stickyContainerIndexByIndex,
nextDatabaseContainerIndexByIndex, nextDatabaseContainerIndexByIndex,
nextSchemaContainerIndexByIndex, nextSchemaContainerIndexByIndex,
nextBoundaryIndexByIndex,
}; };
} }
+3
View File
@@ -104,6 +104,9 @@ test("sticky containers prefer databases and fall back to schemas", () => {
assert.equal(index.stickyContainerIndexByIndex[3], 1); assert.equal(index.stickyContainerIndexByIndex[3], 1);
assert.equal(index.stickyContainerIndexByIndex[5], 5); assert.equal(index.stickyContainerIndexByIndex[5], 5);
assert.equal(index.stickyContainerIndexByIndex[6], 5); assert.equal(index.stickyContainerIndexByIndex[6], 5);
assert.equal(index.nextBoundaryIndexByIndex[1], 4);
assert.equal(index.nextBoundaryIndexByIndex[2], 4);
assert.equal(index.nextBoundaryIndexByIndex[5], -1);
}); });
test("shouldVirtualizeFlatTree keeps small/medium trees on the plain renderer", () => { test("shouldVirtualizeFlatTree keeps small/medium trees on the plain renderer", () => {