Skip to content

Commit a17868a

Browse files
authored
fix: switch to relevant app when inspecting component (#748)
1 parent 12e5b97 commit a17868a

File tree

8 files changed

+67
-49
lines changed

8 files changed

+67
-49
lines changed

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

+39-32
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,54 @@ onUnmounted(() => {
215215
rpc.functions.off(DevToolsMessagingEvents.INSPECTOR_TREE_UPDATED, onInspectorTreeUpdated)
216216
})
217217
218-
function inspectComponentInspector() {
218+
// #region toggle app
219+
const devtoolsState = useDevToolsState()
220+
const appRecords = computed(() => devtoolsState.appRecords.value.map(app => ({
221+
label: app.name + (app.version ? ` (${app.version})` : ''),
222+
value: app.id,
223+
})))
224+
225+
const normalizedAppRecords = computed(() => appRecords.value.map(app => ({
226+
label: app.label,
227+
id: app.value,
228+
})))
229+
230+
const activeAppRecordId = ref(devtoolsState.activeAppRecordId.value)
231+
watchEffect(() => {
232+
activeAppRecordId.value = devtoolsState.activeAppRecordId.value
233+
})
234+
235+
async function toggleApp(id: string, options: { inspectingComponent?: boolean } = {}) {
236+
await rpc.value.toggleApp(id, options)
237+
activeComponentId.value = ''
238+
await getComponentsInspectorTree()
239+
}
240+
// #endregion
241+
242+
async function inspectComponentInspector() {
219243
inspectComponentTipVisible.value = true
220244
emit('onInspectComponentStart')
221-
rpc.value.inspectComponentInspector().then((_data) => {
222-
const data = JSON.parse(_data! as unknown as string)
245+
246+
try {
247+
const data = JSON.parse(await rpc.value.inspectComponentInspector())
248+
249+
const appId = data.id.split(':')[0]
250+
if (activeAppRecordId.value !== data.appId) {
251+
await toggleApp(appId, { inspectingComponent: true })
252+
}
253+
223254
activeComponentId.value = data.id
224-
if (!expandedTreeNodes.value.includes(data.id))
255+
if (!expandedTreeNodes.value.includes(data.id)) {
225256
expandedTreeNodes.value.push(data.id)
257+
}
226258
227259
expandedTreeNodes.value = [...new Set([...expandedTreeNodes.value, ...getTargetLinkedNodes(treeNodeLinkedList.value, data.id)])]
228260
scrollToActiveTreeNode()
229-
}).finally(() => {
261+
}
262+
finally {
230263
inspectComponentTipVisible.value = false
231264
emit('onInspectComponentEnd')
232-
})
265+
}
233266
}
234267
235268
function cancelInspectComponentInspector() {
@@ -280,32 +313,6 @@ function closeComponentRenderCode() {
280313
componentRenderCode.value = ''
281314
componentRenderCodeVisible.value = false
282315
}
283-
284-
// #region toggle app
285-
const devtoolsState = useDevToolsState()
286-
const appRecords = computed(() => devtoolsState.appRecords.value.map(app => ({
287-
label: app.name + (app.version ? ` (${app.version})` : ''),
288-
value: app.id,
289-
})))
290-
291-
const normalizedAppRecords = computed(() => appRecords.value.map(app => ({
292-
label: app.label,
293-
id: app.value,
294-
})))
295-
296-
const activeAppRecordId = ref(devtoolsState.activeAppRecordId.value)
297-
watchEffect(() => {
298-
activeAppRecordId.value = devtoolsState.activeAppRecordId.value
299-
})
300-
301-
function toggleApp(id: string) {
302-
rpc.value.toggleApp(id).then(() => {
303-
activeComponentId.value = ''
304-
getComponentsInspectorTree()
305-
})
306-
}
307-
308-
// #endregion
309316
</script>
310317

311318
<template>

packages/core/src/rpc/global.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ export const functions = {
122122
if (inspector)
123123
await inspector.enable()
124124
},
125-
async toggleApp(id: string) {
126-
return devtools.ctx.api.toggleApp(id)
125+
async toggleApp(id: string, options?: { inspectingComponent?: boolean }) {
126+
return devtools.ctx.api.toggleApp(id, options)
127127
},
128128
updatePluginSettings(pluginId: string, key: string, value: string) {
129129
return devtools.ctx.api.updatePluginSettings(pluginId, key, value)

packages/devtools-kit/src/core/component-highlighter/index.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ComponentHighLighterOptions, ScrollToComponentOptions } from './ty
33
import { activeAppRecord } from '../../ctx'
44
import { getComponentBoundingRect } from '../component/state/bounding-rect'
55
import { getRootElementsFromComponentInstance } from '../component/tree/el'
6-
import { getComponentId, getComponentInstance, getInstanceName } from '../component/utils'
6+
import { getComponentInstance, getInstanceName, getUniqueComponentId } from '../component/utils'
77

88
export type * from './types'
99

@@ -180,14 +180,8 @@ function selectComponentFn(e: MouseEvent, cb) {
180180
e.preventDefault()
181181
e.stopPropagation()
182182
if (inspectInstance) {
183-
const app = activeAppRecord.value?.app as unknown as VueAppInstance
184-
getComponentId({
185-
app,
186-
uid: app.uid,
187-
instance: inspectInstance,
188-
}).then((id) => {
189-
cb(id)
190-
})
183+
const uniqueComponentId = getUniqueComponentId(inspectInstance)
184+
cb(uniqueComponentId)
191185
}
192186
}
193187

packages/devtools-kit/src/core/plugin/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ export function removeRegisteredPluginApp(app: App) {
4040
target.__VUE_DEVTOOLS_KIT__REGISTERED_PLUGIN_APPS__.delete(app)
4141
}
4242

43-
export function registerDevToolsPlugin(app: App) {
44-
if (target.__VUE_DEVTOOLS_KIT__REGISTERED_PLUGIN_APPS__.has(app) || devtoolsState.highPerfModeEnabled)
43+
export function registerDevToolsPlugin(app: App, options?: { inspectingComponent?: boolean }) {
44+
if (target.__VUE_DEVTOOLS_KIT__REGISTERED_PLUGIN_APPS__.has(app)) {
4545
return
46+
}
47+
if (devtoolsState.highPerfModeEnabled && !options?.inspectingComponent) {
48+
return
49+
}
4650

4751
target.__VUE_DEVTOOLS_KIT__REGISTERED_PLUGIN_APPS__.add(app)
4852

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ export function createDevToolsApi(hooks: Hookable<DevToolsContextHooks & DevTool
102102
// get vue inspector
103103
getVueInspector: getComponentInspector,
104104
// toggle app
105-
toggleApp(id: string) {
105+
toggleApp(id: string, options?: { inspectingComponent?: boolean }) {
106106
const appRecord = devtoolsAppRecords.value.find(record => record.id === id)
107107

108108
if (appRecord) {
109109
setActiveAppRecordId(id)
110110
setActiveAppRecord(appRecord)
111111
normalizeRouterInfo(appRecord, activeAppRecord)
112112
callInspectorUpdatedHook()
113-
registerDevToolsPlugin(appRecord.app)
113+
registerDevToolsPlugin(appRecord.app, options)
114114
}
115115
},
116116
// inspect dom

packages/playground/multi-app/src/App.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<script setup lang="ts">
2+
import Number from './components/Number.vue'
3+
24
const count = ref(0)
35
</script>
46

57
<template>
68
<div class="m-auto mt-3 h-30 w-60 flex flex-col items-center justify-center rounded bg-[#363636]">
79
App
8-
<div>Count: {{ count }}</div>
10+
<div>Count: <Number :num="count" /></div>
911
<button @click="count++">
1012
++
1113
</button>

packages/playground/multi-app/src/App2.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<script setup lang="ts">
2+
import Number from './components/Number.vue'
3+
24
const count = ref(0)
35
</script>
46

57
<template>
68
<div class="m-auto mt-3 h-30 w-60 flex flex-col items-center justify-center rounded bg-[#363636]">
79
App2
8-
<div>Count: {{ count }}</div>
10+
<div>Count: <Number :num="count" /></div>
911
<button @click="count++">
1012
++
1113
</button>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
defineProps<{ num: number }>()
3+
</script>
4+
5+
<template>
6+
<span>
7+
{{ num }}
8+
</span>
9+
</template>

0 commit comments

Comments
 (0)