Skip to content

Commit 2f8a31a

Browse files
committedJun 13, 2024·
feat(extension): inspect dom
1 parent 9b9c0a8 commit 2f8a31a

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed
 

‎packages/applet/src/modules/components/index.vue

+8
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ function scrollToComponent() {
234234
rpc.value.scrollToComponent(activeComponentId.value)
235235
}
236236
237+
function inspectDOM() {
238+
rpc.value.inspectDOM(activeComponentId.value).then(() => {
239+
// @ts-expect-error skip type check
240+
chrome.devtools.inspectedWindow.eval('inspect(window.__VUE_DEVTOOLS_INSPECT_DOM_TARGET__)')
241+
})
242+
}
243+
237244
function getComponentRenderCode() {
238245
rpc.value.getComponentRenderCode(activeComponentId.value).then((data) => {
239246
componentRenderCode.value = data!
@@ -297,6 +304,7 @@ function closeComponentRenderCode() {
297304
<div class="flex items-center gap-2 px-1">
298305
<i v-tooltip.bottom="'Scroll to component'" class="i-material-symbols-light:eye-tracking-outline h-4 w-4 cursor-pointer hover:(op-70)" @click="scrollToComponent" />
299306
<i v-tooltip.bottom="'Show render code'" class="i-material-symbols-light:code h-5 w-5 cursor-pointer hover:(op-70)" @click="getComponentRenderCode" />
307+
<i v-if="isInChromePanel" v-tooltip.bottom="'Inspect DOM'" class="i-material-symbols-light:menu-open h-5 w-5 cursor-pointer hover:(op-70)" @click="inspectDOM" />
300308
<i v-if="activeTreeNodeFilePath && !isInChromePanel" v-tooltip.bottom="'Open in Editor'" class="i-carbon-launch h-4 w-4 cursor-pointer hover:(op-70)" @click="openInEditor" />
301309
</div>
302310
</div>

‎packages/core/src/rpc/global.ts

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ export const functions = {
8484
scrollToComponent(id: string) {
8585
return devtools.ctx.api.scrollToComponent(id)
8686
},
87+
inspectDOM(id: string) {
88+
return devtools.ctx.api.inspectDOM(id)
89+
},
8790
getInspectorNodeActions(id: string) {
8891
return getInspectorNodeActions(id)
8992
},

‎packages/devtools-kit/src/ctx/api.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { HookKeys, Hookable } from 'hookable'
2+
import { target } from '@vue/devtools-shared'
23
import type { CustomInspectorState } from '../types'
34
import { StateEditor } from '../core/component/state/editor'
45
import { cancelInspectComponentHighLighter, inspectComponentHighLighter, scrollToComponent } from '../core/component-highlighter'
56
import { getComponentInstance } from '../core/component/utils'
7+
import { getRootElementsFromComponentInstance } from '../core/component/tree/el'
68
import { openInEditor } from '../core/open-in-editor'
79
import { normalizeRouterInfo } from '../core/router'
810
import { getComponentInspector } from '../core/component-inspector'
@@ -108,6 +110,16 @@ export function createDevToolsApi(hooks: Hookable<DevToolsContextHooks & DevTool
108110
callInspectorUpdatedHook()
109111
}
110112
},
113+
// inspect dom
114+
inspectDOM(instanceId: string) {
115+
const instance = getComponentInstance(activeAppRecord.value, instanceId)
116+
if (instance) {
117+
const [el] = getRootElementsFromComponentInstance(instance)
118+
if (el) {
119+
target.__VUE_DEVTOOLS_INSPECT_DOM_TARGET__ = el
120+
}
121+
}
122+
},
111123
}
112124
}
113125

0 commit comments

Comments
 (0)
Please sign in to comment.