Skip to content

Commit

Permalink
feat(extension): inspect dom
Browse files Browse the repository at this point in the history
  • Loading branch information
webfansplz committed Jun 13, 2024
1 parent 9b9c0a8 commit 2f8a31a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/applet/src/modules/components/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ function scrollToComponent() {
rpc.value.scrollToComponent(activeComponentId.value)
}
function inspectDOM() {
rpc.value.inspectDOM(activeComponentId.value).then(() => {
// @ts-expect-error skip type check
chrome.devtools.inspectedWindow.eval('inspect(window.__VUE_DEVTOOLS_INSPECT_DOM_TARGET__)')
})
}
function getComponentRenderCode() {
rpc.value.getComponentRenderCode(activeComponentId.value).then((data) => {
componentRenderCode.value = data!
Expand Down Expand Up @@ -297,6 +304,7 @@ function closeComponentRenderCode() {
<div class="flex items-center gap-2 px-1">
<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" />
<i v-tooltip.bottom="'Show render code'" class="i-material-symbols-light:code h-5 w-5 cursor-pointer hover:(op-70)" @click="getComponentRenderCode" />
<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" />
<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" />
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/rpc/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export const functions = {
scrollToComponent(id: string) {
return devtools.ctx.api.scrollToComponent(id)
},
inspectDOM(id: string) {
return devtools.ctx.api.inspectDOM(id)
},
getInspectorNodeActions(id: string) {
return getInspectorNodeActions(id)
},
Expand Down
12 changes: 12 additions & 0 deletions packages/devtools-kit/src/ctx/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { HookKeys, Hookable } from 'hookable'
import { target } from '@vue/devtools-shared'
import type { CustomInspectorState } from '../types'
import { StateEditor } from '../core/component/state/editor'
import { cancelInspectComponentHighLighter, inspectComponentHighLighter, scrollToComponent } from '../core/component-highlighter'
import { getComponentInstance } from '../core/component/utils'
import { getRootElementsFromComponentInstance } from '../core/component/tree/el'
import { openInEditor } from '../core/open-in-editor'
import { normalizeRouterInfo } from '../core/router'
import { getComponentInspector } from '../core/component-inspector'
Expand Down Expand Up @@ -108,6 +110,16 @@ export function createDevToolsApi(hooks: Hookable<DevToolsContextHooks & DevTool
callInspectorUpdatedHook()
}
},
// inspect dom
inspectDOM(instanceId: string) {
const instance = getComponentInstance(activeAppRecord.value, instanceId)
if (instance) {
const [el] = getRootElementsFromComponentInstance(instance)
if (el) {
target.__VUE_DEVTOOLS_INSPECT_DOM_TARGET__ = el
}
}
},
}
}

Expand Down

0 comments on commit 2f8a31a

Please sign in to comment.