1
- import { ref , computed , watch , Ref } from '@vue/composition-api'
1
+ import { ref , computed , watch , Ref , onMounted } from '@vue/composition-api'
2
2
import { ComponentTreeNode , EditStatePayload , InspectedComponentData } from '@vue/devtools-api'
3
3
import Vue from 'vue'
4
4
import groupBy from 'lodash/groupBy'
@@ -26,7 +26,6 @@ export const selectedComponentPendingId = ref<ComponentTreeNode['id']>(null)
26
26
let lastSelectedApp : AppRecord = null
27
27
export const lastSelectedComponentId : Record < AppRecord [ 'id' ] , ComponentTreeNode [ 'id' ] > = { }
28
28
export const expandedMap = ref < Record < ComponentTreeNode [ 'id' ] , boolean > > ( { } )
29
- export const resetComponentsQueued = ref ( false )
30
29
31
30
export function useComponentRequests ( ) {
32
31
const router = useRouter ( )
@@ -134,17 +133,13 @@ export function useComponent (instance: Ref<ComponentTreeNode>) {
134
133
const { selectComponent, requestComponentTree } = useComponentRequests ( )
135
134
const { subscribe } = useBridge ( )
136
135
137
- function checkIsExpanded ( id ) {
138
- return ! ! expandedMap . value [ id ]
139
- }
140
-
141
- const isExpanded = computed ( ( ) => checkIsExpanded ( instance . value . id ) )
136
+ const isExpanded = computed ( ( ) => isComponentOpen ( instance . value . id ) )
142
137
const isExpandedUndefined = computed ( ( ) => expandedMap . value [ instance . value . id ] == null )
143
138
144
- function toggleExpand ( load = true ) {
139
+ function toggleExpand ( ) {
145
140
if ( ! instance . value . hasChildren ) return
146
141
setComponentOpen ( instance . value . id , ! isExpanded . value )
147
- if ( load ) {
142
+ if ( isComponentOpen ( instance . value . id ) ) {
148
143
requestComponentTree ( instance . value . id )
149
144
}
150
145
}
@@ -173,14 +168,16 @@ export function useComponent (instance: Ref<ComponentTreeNode>) {
173
168
} )
174
169
}
175
170
176
- if ( isExpanded . value ) {
177
- requestComponentTree ( instance . value . id )
178
- }
171
+ onMounted ( ( ) => {
172
+ if ( isExpanded . value ) {
173
+ requestComponentTree ( instance . value . id )
174
+ }
175
+ } )
179
176
180
177
return {
181
178
isExpanded,
182
179
isExpandedUndefined,
183
- checkIsExpanded ,
180
+ isComponentOpen ,
184
181
toggleExpand,
185
182
isSelected,
186
183
select,
@@ -192,6 +189,10 @@ export function setComponentOpen (id: ComponentTreeNode['id'], isOpen: boolean)
192
189
Vue . set ( expandedMap . value , id , isOpen )
193
190
}
194
191
192
+ export function isComponentOpen ( id ) {
193
+ return ! ! expandedMap . value [ id ]
194
+ }
195
+
195
196
export function useSelectedComponent ( ) {
196
197
const data = computed ( ( ) => selectedComponentData . value )
197
198
const state = computed ( ( ) => selectedComponentData . value
@@ -255,7 +256,6 @@ export function useSelectedComponent () {
255
256
}
256
257
257
258
export function resetComponents ( ) {
258
- resetComponentsQueued . value = false
259
259
rootInstances . value = [ ]
260
260
componentsMap . value = { }
261
261
componentsParent = { }
@@ -273,9 +273,6 @@ export async function requestComponentTree (instanceId: ComponentTreeNode['id']
273
273
}
274
274
requestedComponentTree . add ( instanceId )
275
275
276
- if ( instanceId === '_root' ) {
277
- resetComponentsQueued . value = true
278
- }
279
276
await waitForAppSelect ( )
280
277
281
278
getBridge ( ) . send ( BridgeEvents . TO_BACK_COMPONENT_TREE , {
@@ -284,35 +281,45 @@ export async function requestComponentTree (instanceId: ComponentTreeNode['id']
284
281
} )
285
282
}
286
283
287
- export function restoreChildrenFromComponentsMap ( data : ComponentTreeNode ) {
288
- const instance = componentsMap . value [ data . id ]
289
- if ( instance && data . hasChildren ) {
290
- if ( ! data . children . length && instance . children . length ) {
291
- data . children = instance . children
292
- } else {
293
- for ( const child of data . children ) {
294
- restoreChildrenFromComponentsMap ( child )
295
- }
296
- }
284
+ export function ensureComponentsMapData ( data : ComponentTreeNode ) {
285
+ let component = componentsMap . value [ data . id ]
286
+ if ( ! component ) {
287
+ component = addToComponentsMap ( data )
288
+ } else {
289
+ component = updateComponentsMapData ( data )
290
+ }
291
+ return component
292
+ }
293
+
294
+ function ensureComponentsMapChildren ( id : string , children : ComponentTreeNode [ ] ) {
295
+ const result = children . map ( child => ensureComponentsMapData ( child ) )
296
+ for ( const child of children ) {
297
+ componentsParent [ child . id ] = id
297
298
}
299
+ return result
298
300
}
299
301
300
- export function updateComponentsMapData ( data : ComponentTreeNode ) {
302
+ function updateComponentsMapData ( data : ComponentTreeNode ) {
301
303
const component = componentsMap . value [ data . id ]
302
304
for ( const key in data ) {
303
- Vue . set ( component , key , data [ key ] )
305
+ if ( key === 'children' ) {
306
+ if ( ! data . hasChildren || data . children . length ) {
307
+ const children = ensureComponentsMapChildren ( component . id , data . children )
308
+ Vue . set ( component , key , children )
309
+ }
310
+ } else {
311
+ Vue . set ( component , key , data [ key ] )
312
+ }
304
313
}
305
314
return component
306
315
}
307
316
308
- export function addToComponentsMap ( instance : ComponentTreeNode ) {
309
- componentsMap . value [ instance . id ] = instance
310
- if ( instance . children ) {
311
- instance . children . forEach ( c => {
312
- componentsParent [ c . id ] = instance . id
313
- addToComponentsMap ( c )
314
- } )
317
+ function addToComponentsMap ( data : ComponentTreeNode ) {
318
+ if ( ! data . hasChildren || data . children . length ) {
319
+ data . children = ensureComponentsMapChildren ( data . id , data . children )
315
320
}
321
+ componentsMap . value [ data . id ] = data
322
+ return data
316
323
}
317
324
318
325
export async function loadComponent ( id : ComponentTreeNode [ 'id' ] ) {
0 commit comments