@@ -40,11 +40,10 @@ const width = ref(window.innerWidth)
4040const height = ref (window .innerHeight )
4141const nodesRefMap = shallowReactive (new Map <string , HTMLDivElement >())
4242
43- const collapsedNodes = shallowReactive (new Set <string >())
4443const isUpdating = ref (false )
45- const lastActionNodeId = ref <string | null >(null )
46- const childToParentMap = shallowReactive (new Map <string , string >())
4744const isFirstCalculateGraph = ref (true )
45+ const collapsedNodes = shallowReactive (new Set <string >())
46+ const childToParentMap = shallowReactive (new Map <string , string >())
4847
4948const nodes = shallowRef <HierarchyNode <Node >[]>([])
5049const links = shallowRef <Link []>([])
@@ -90,7 +89,7 @@ const createLinkVertical = linkVertical()
9089 .x (d => d [0 ])
9190 .y (d => d [1 ])
9291
93- function calculateGraph() {
92+ function calculateGraph(focusOnFirstRooeNode = true ) {
9493 // Unset the canvas size, and recalculate again after nodes are rendered
9594 width .value = window .innerWidth
9695 height .value = window .innerHeight
@@ -109,7 +108,7 @@ function calculateGraph() {
109108 })
110109 return rootModules .value .map (x => ({
111110 module: x ,
112- expanded: ! collapsedNodes .has (x .id ), // 简化:未折叠即为展开
111+ expanded: ! collapsedNodes .has (x .id ),
113112 hasChildren: false ,
114113 }))
115114 }
@@ -209,7 +208,7 @@ function calculateGraph() {
209208 width .value = (container .value ! .scrollWidth / scale .value + SPACING .margin )
210209 height .value = (container .value ! .scrollHeight / scale .value + SPACING .margin )
211210 const moduleId = rootModules .value ?.[0 ]?.id
212- if (! lastActionNodeId . value && moduleId ) {
211+ if (focusOnFirstRooeNode && moduleId ) {
213212 nextTick (() => {
214213 focusOn (moduleId , false )
215214 })
@@ -249,9 +248,7 @@ function adjustScrollPositionAfterToggle(id: string, beforePosition: { x: number
249248function toggleNode(id : string ) {
250249 if (isUpdating .value )
251250 return
252-
253251 isUpdating .value = true
254- lastActionNodeId .value = id
255252
256253 const node = nodesRefMap .get (id )
257254 let beforePosition: null | { x: number , y: number } = null
@@ -273,15 +270,14 @@ function toggleNode(id: string) {
273270 collapsedNodes .add (id )
274271 }
275272
276- calculateGraph ()
273+ calculateGraph (false )
277274
278275 // Adjust scroll position after layout changes
279276 if (beforePosition ) {
280277 adjustScrollPositionAfterToggle (id , beforePosition )
281278 }
282279
283280 isUpdating .value = false
284- lastActionNodeId .value = null
285281}
286282
287283function expandAll() {
@@ -380,7 +376,7 @@ onMounted(() => {
380376
381377 watch (
382378 () => graphRender .value ,
383- calculateGraph ,
379+ () => calculateGraph () ,
384380 )
385381})
386382 </script >
0 commit comments