Skip to content

Commit f7b7964

Browse files
committed
feat: expand nodes loaded after event
1 parent 3a28691 commit f7b7964

File tree

10 files changed

+29
-12
lines changed

10 files changed

+29
-12
lines changed

packages/api/src/api/component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface ComponentTreeNode {
1717
isRouterView?: boolean
1818
macthedRouteSegment?: string
1919
tags: InspectorNodeTag[]
20+
autoOpen: boolean
2021
meta?: any
2122
}
2223

packages/api/src/api/hooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export type HookPayloads = {
5656
componentTreeData: ComponentTreeNode[]
5757
maxDepth: number
5858
filter: string
59+
recursively: boolean
5960
}
6061
[Hooks.VISIT_COMPONENT_TREE]: {
6162
app: App

packages/app-backend-api/src/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ export class DevtoolsApi {
8787
})
8888
}
8989

90-
async walkComponentTree (instance: ComponentInstance, maxDepth = -1, filter: string = null) {
90+
async walkComponentTree (instance: ComponentInstance, maxDepth = -1, filter: string = null, recursively = false) {
9191
const payload = await this.callHook(Hooks.WALK_COMPONENT_TREE, {
9292
componentInstance: instance,
9393
componentTreeData: null,
9494
maxDepth,
9595
filter,
96+
recursively,
9697
})
9798
return payload.componentTreeData
9899
}

packages/app-backend-core/src/component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { App, ComponentInstance, EditStatePayload } from '@vue/devtools-api'
66
const MAX_$VM = 10
77
const $vmQueue = []
88

9-
export async function sendComponentTreeData (appRecord: AppRecord, instanceId: string, filter = '', maxDepth: number = null, ctx: BackendContext) {
9+
export async function sendComponentTreeData (appRecord: AppRecord, instanceId: string, filter = '', maxDepth: number = null, ctx: BackendContext, recursively = false) {
1010
if (!instanceId || appRecord !== ctx.currentAppRecord) return
1111

1212
// Flush will send all components in the tree
@@ -30,7 +30,7 @@ export async function sendComponentTreeData (appRecord: AppRecord, instanceId: s
3030
if (maxDepth == null) {
3131
maxDepth = instance === ctx.currentAppRecord.rootInstance ? 2 : 1
3232
}
33-
const data = await appRecord.backend.api.walkComponentTree(instance, maxDepth, filter)
33+
const data = await appRecord.backend.api.walkComponentTree(instance, maxDepth, filter, recursively)
3434
const payload = {
3535
instanceId,
3636
treeData: stringify(data),

packages/app-backend-core/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ function connectBridge () {
359359

360360
// Components
361361

362-
ctx.bridge.on(BridgeEvents.TO_BACK_COMPONENT_TREE, ({ instanceId, filter }) => {
362+
ctx.bridge.on(BridgeEvents.TO_BACK_COMPONENT_TREE, ({ instanceId, filter, recursively }) => {
363363
ctx.currentAppRecord.componentFilter = filter
364-
sendComponentTreeData(ctx.currentAppRecord, instanceId, filter, null, ctx)
364+
sendComponentTreeData(ctx.currentAppRecord, instanceId, filter, null, ctx, recursively)
365365
subscribe(BridgeSubscriptions.COMPONENT_TREE, { instanceId })
366366
})
367367

packages/app-backend-vue2/src/components/tree.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ let api: DevtoolsApi
1313
const consoleBoundInstances = Array(5)
1414

1515
let filter = ''
16+
let recursively = false
1617
const functionalIds = new Map()
1718

1819
// Dedupe instances
1920
// Some instances may be both on a component and on a child abstract/functional component
2021
const captureIds = new Map()
2122

22-
export async function walkTree (instance, pFilter: string, api: DevtoolsApi, ctx: BackendContext): Promise<ComponentTreeNode[]> {
23+
export async function walkTree (instance, pFilter: string, pRecursively: boolean, api: DevtoolsApi, ctx: BackendContext): Promise<ComponentTreeNode[]> {
2324
initCtx(api, ctx)
2425
filter = pFilter
26+
recursively = pRecursively
2527
functionalIds.clear()
2628
captureIds.clear()
2729
const result: ComponentTreeNode[] = flatten(await findQualifiedChildren(instance))
@@ -205,6 +207,7 @@ async function capture (instance, index?: number, list?: any[]): Promise<Compone
205207
hasChildren: !!children.length,
206208
inactive: false,
207209
isFragment: false, // TODO: Check what is it for.
210+
autoOpen: recursively,
208211
}
209212
return api.visitComponentTree(
210213
instance,
@@ -241,6 +244,7 @@ async function capture (instance, index?: number, list?: any[]): Promise<Compone
241244
isFragment: !!instance._isFragment,
242245
children,
243246
hasChildren: !!children.length,
247+
autoOpen: recursively,
244248
tags: [],
245249
meta: {},
246250
}

packages/app-backend-vue2/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const backend = defineBackend({
2727
})
2828

2929
api.on.walkComponentTree(async (payload, ctx) => {
30-
payload.componentTreeData = await walkTree(payload.componentInstance, payload.filter, api, ctx)
30+
payload.componentTreeData = await walkTree(payload.componentInstance, payload.filter, payload.recursively, api, ctx)
3131
})
3232

3333
api.on.walkComponentParents((payload, ctx) => {

packages/app-backend-vue3/src/components/tree.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ export class ComponentWalker {
88
ctx: BackendContext
99
api: DevtoolsApi
1010
maxDepth: number
11+
recursively: boolean
1112
componentFilter: ComponentFilter
1213
// Dedupe instances
1314
// Some instances may be both on a component and on a child abstract/functional component
1415
captureIds: Map<string, undefined>
1516

16-
constructor (maxDepth: number, filter: string, api: DevtoolsApi, ctx: BackendContext) {
17+
constructor (maxDepth: number, filter: string, recursively: boolean, api: DevtoolsApi, ctx: BackendContext) {
1718
this.ctx = ctx
1819
this.api = api
1920
this.maxDepth = maxDepth
21+
this.recursively = recursively
2022
this.componentFilter = new ComponentFilter(filter)
2123
}
2224

@@ -146,6 +148,7 @@ export class ComponentWalker {
146148
children: [],
147149
isFragment: isFragment(instance),
148150
tags: [],
151+
autoOpen: this.recursively,
149152
}
150153

151154
// capture children

packages/app-backend-vue3/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ export const backend = defineBackend({
2626
})
2727

2828
api.on.walkComponentTree(async (payload, ctx) => {
29-
const walker = new ComponentWalker(payload.maxDepth, payload.filter, api, ctx)
29+
const walker = new ComponentWalker(payload.maxDepth, payload.filter, payload.recursively, api, ctx)
3030
payload.componentTreeData = await walker.getComponentTree(payload.componentInstance)
3131
})
3232

3333
api.on.walkComponentParents((payload, ctx) => {
34-
const walker = new ComponentWalker(0, null, api, ctx)
34+
const walker = new ComponentWalker(0, null, false, api, ctx)
3535
payload.parentInstances = walker.getComponentParents(payload.componentInstance)
3636
})
3737

packages/app-frontend/src/features/components/composable/components.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ export function useComponent (instance: Ref<ComponentTreeNode>) {
142142
const isOpen = value === undefined ? !isExpanded.value : value
143143
setComponentOpen(treeNode.id, isOpen)
144144
if (isComponentOpen(treeNode.id)) {
145-
requestComponentTree(treeNode.id)
145+
requestComponentTree(treeNode.id, recursively)
146+
} else {
147+
// stop expanding all treenode
148+
treeNode.autoOpen = false
146149
}
147150
if (recursively) {
148151
treeNode.children.forEach(child => {
@@ -176,6 +179,9 @@ export function useComponent (instance: Ref<ComponentTreeNode>) {
176179
}
177180

178181
onMounted(() => {
182+
if (instance.value.autoOpen) {
183+
toggleExpand(true, true)
184+
}
179185
if (isExpanded.value) {
180186
requestComponentTree(instance.value.id)
181187
}
@@ -270,7 +276,7 @@ export function resetComponents () {
270276

271277
export const requestedComponentTree = new Set()
272278

273-
export async function requestComponentTree (instanceId: ComponentTreeNode['id'] = null) {
279+
export async function requestComponentTree (instanceId: ComponentTreeNode['id'] = null, recursively = false) {
274280
if (!instanceId) {
275281
instanceId = '_root'
276282
}
@@ -285,6 +291,7 @@ export async function requestComponentTree (instanceId: ComponentTreeNode['id']
285291
getBridge().send(BridgeEvents.TO_BACK_COMPONENT_TREE, {
286292
instanceId,
287293
filter: treeFilter.value,
294+
recursively,
288295
})
289296
}
290297

0 commit comments

Comments
 (0)