Skip to content

Commit f270c25

Browse files
committed
refactor: component order follows dom order, closes #1503
1 parent 94c2a32 commit f270c25

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

packages/api/src/api/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface ComponentTreeNode {
1212
isFragment: boolean
1313
hasChildren: boolean
1414
children: ComponentTreeNode[]
15-
positionTop?: number
15+
indexInParent?: number
1616
consoleId?: string
1717
isRouterView?: boolean
1818
macthedRouteSegment?: string

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,15 @@ function addIframePosition (bounds, win: any) {
116116
}
117117
return bounds
118118
}
119+
120+
export function getRootElementsFromComponentInstance (instance) {
121+
if (instance._isFragment) {
122+
const list = []
123+
const { _fragmentStart, _fragmentEnd } = instance
124+
util().mapNodeRange(_fragmentStart, _fragmentEnd, node => {
125+
list.push(node)
126+
})
127+
return list
128+
}
129+
return [instance.$el]
130+
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AppRecord, BackendContext, DevtoolsApi } from '@vue-devtools/app-backend-api'
22
import { classify } from '@vue-devtools/shared-utils'
33
import { ComponentTreeNode } from '@vue/devtools-api'
4-
import { getInstanceOrVnodeRect } from './el'
4+
import { getRootElementsFromComponentInstance } from './el'
55
import { getInstanceName, getRenderKey, getUniqueId, isBeingDestroyed } from './util'
66

77
export let instanceMap: Map<any, any>
@@ -253,12 +253,13 @@ async function capture (instance, index?: number, list?: any[]): Promise<Compone
253253
ret.hasChildren = !!ret.children.length
254254
}
255255

256-
// record screen position to ensure correct ordering
257-
if ((!list || list.length > 1) && !instance._inactive) {
258-
const rect = getInstanceOrVnodeRect(instance)
259-
ret.positionTop = rect ? rect.top : Infinity
256+
// ensure correct ordering
257+
const rootElements = getRootElementsFromComponentInstance(instance)
258+
if (rootElements.length) {
259+
const firstElement = rootElements[0]
260+
ret.indexInParent = Array.from(firstElement.parentElement.childNodes).indexOf(firstElement)
260261
} else {
261-
ret.positionTop = Infinity
262+
ret.indexInParent = -1
262263
}
263264

264265
// check if instance is available in console

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DevtoolsBackend, BuiltinBackendFeature } from '@vue-devtools/app-backend-api'
22
import { backendInjections, getComponentName } from '@vue-devtools/shared-utils'
33
import { editState, getCustomInstanceDetails, getInstanceDetails } from './components/data'
4-
import { getInstanceOrVnodeRect, findRelatedComponent } from './components/el'
4+
import { getInstanceOrVnodeRect, findRelatedComponent, getRootElementsFromComponentInstance } from './components/el'
55
import { getComponentParents, instanceMap, walkTree } from './components/tree'
66
import { getInstanceName } from './components/util'
77
import { wrapVueForEvents } from './events'
@@ -55,7 +55,7 @@ export const backend: DevtoolsBackend = {
5555
})
5656

5757
api.on.getComponentRootElements(payload => {
58-
payload.rootElements = [payload.componentInstance.$el]
58+
payload.rootElements = getRootElementsFromComponentInstance(payload.componentInstance)
5959
})
6060

6161
api.on.getComponentDevtoolsOptions(payload => {

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isBeingDestroyed, getUniqueComponentId, getInstanceName, getRenderKey,
22
import { ComponentFilter } from './filter'
33
import { BackendContext } from '@vue-devtools/app-backend-api'
44
import { ComponentTreeNode } from '@vue/devtools-api'
5-
import { getInstanceOrVnodeRect } from './el'
5+
import { getRootElementsFromComponentInstance } from './el'
66

77
export class ComponentWalker {
88
ctx: BackendContext
@@ -162,10 +162,13 @@ export class ComponentWalker {
162162
}
163163
}
164164

165-
// record screen position to ensure correct ordering
166-
if ((!list || list.length > 1) && !instance._inactive) {
167-
const rect = getInstanceOrVnodeRect(instance)
168-
treeNode.positionTop = rect ? rect.top : Infinity
165+
// ensure correct ordering
166+
const rootElements = getRootElementsFromComponentInstance(instance)
167+
if (rootElements.length) {
168+
const firstElement = rootElements[0]
169+
treeNode.indexInParent = Array.from(firstElement.parentElement.childNodes).indexOf(firstElement)
170+
} else {
171+
treeNode.indexInParent = -1
169172
}
170173

171174
if (instance.suspense) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,10 @@ export function loadComponent (id: ComponentTreeNode['id']) {
314314

315315
export function sortChildren (children: ComponentTreeNode[]) {
316316
return children.slice().sort((a, b) => {
317-
if (a.positionTop === b.positionTop) {
317+
if (a.indexInParent === b.indexInParent) {
318318
return a.id.localeCompare(b.id)
319319
} else {
320-
return a.positionTop - b.positionTop
320+
return a.indexInParent - b.indexInParent
321321
}
322322
})
323323
}

0 commit comments

Comments
 (0)