fix(sidebar): reveal expanded tree items

This commit is contained in:
t8y2 committed 2026-05-18 16:35:09 +08:00
1 parent 28f4aec50d
commit ab87de4054
4 files changed
+83 -1

No files matched your search

@@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref, computed, watch } from "vue";
import { ref, computed, nextTick, watch } from "vue";
import { useI18n } from "vue-i18n";
import { Search, X, ListFilter, Check, FolderPlus } from "lucide-vue-next";
import { useConnectionStore } from "@/stores/connectionStore";
@@ -11,6 +11,7 @@ import {
SIDEBAR_TREE_PRERENDER_COUNT,
SIDEBAR_TREE_SCROLL_BUFFER,
flattenTree,
scrollTopForExpandedTreeNode,
shouldVirtualizeFlatTree,
type FlatTreeNode,
} from "@/composables/useFlatTree";
@@ -32,6 +33,7 @@ const store = useConnectionStore();
const searchQuery = ref("");
const deferredSearchQuery = ref("");
const searchInputRef = ref<HTMLInputElement>();
const treeScrollerRef = ref<InstanceType<typeof RecycleScroller> | null>(null);
const selectedTypes = ref<string[]>([]);
const searchCollapsedIds = ref<Set<string>>(new Set());
let searchTimer: number | undefined;
@@ -139,6 +141,28 @@ function onSearchToggle(node: TreeNode) {
searchCollapsedIds.value = next;
}
async function onNodeToggled(node: TreeNode, wasExpanded: boolean) {
if (wasExpanded || !node.isExpanded) return;
await nextTick();
const expandedIndex = flatNodes.value.findIndex((item) => item.id === node.id);
const insertedRowCount = flattenTree([node]).length - 1;
const scroller = treeScrollerRef.value?.$el as HTMLElement | undefined;
if (!scroller || expandedIndex < 0 || insertedRowCount <= 0) return;
const nextScrollTop = scrollTopForExpandedTreeNode({
expandedIndex,
insertedRowCount,
currentScrollTop: scroller.scrollTop,
viewportHeight: scroller.clientHeight,
});
if (nextScrollTop !== scroller.scrollTop) {
scroller.scrollTop = nextScrollTop;
}
}
function focusSearch(): boolean {
const input = searchInputRef.value;
if (!input) return false;
@@ -225,6 +249,7 @@ defineExpose({ focusSearch });
</div>
<RecycleScroller
v-if="flatNodes.length > 0 && useVirtualTree"
ref="treeScrollerRef"
class="sidebar-tree connection-tree-scroller min-h-0 flex-1 overflow-y-auto overflow-x-auto"
:items="flatNodes"
:item-size="SIDEBAR_TREE_ROW_HEIGHT"
@@ -241,6 +266,7 @@ defineExpose({ focusSearch });
:depth="item.depth"
:drag-disabled="isFiltering"
:pending-rename="pendingRenameGroupId === item.node.id"
@node-toggled="onNodeToggled"
@search-toggle="onSearchToggle"
@rename-started="pendingRenameGroupId = null"
/>
@@ -254,6 +280,7 @@ defineExpose({ focusSearch });
:depth="item.depth"
:drag-disabled="isFiltering"
:pending-rename="pendingRenameGroupId === item.node.id"
@node-toggled="onNodeToggled"
@search-toggle="onSearchToggle"
@rename-started="pendingRenameGroupId = null"
/>