From a17868a78a8223e4ea7bf8e28ed7952b75c2815e Mon Sep 17 00:00:00 2001 From: Kevin Luo Date: Mon, 30 Dec 2024 23:54:41 -0500 Subject: [PATCH] fix: switch to relevant app when inspecting component (#748) --- .../applet/src/modules/components/index.vue | 71 ++++++++++--------- packages/core/src/rpc/global.ts | 4 +- .../src/core/component-highlighter/index.ts | 12 +--- .../devtools-kit/src/core/plugin/index.ts | 8 ++- packages/devtools-kit/src/ctx/api.ts | 4 +- packages/playground/multi-app/src/App.vue | 4 +- packages/playground/multi-app/src/App2.vue | 4 +- .../multi-app/src/components/Number.vue | 9 +++ 8 files changed, 67 insertions(+), 49 deletions(-) create mode 100644 packages/playground/multi-app/src/components/Number.vue diff --git a/packages/applet/src/modules/components/index.vue b/packages/applet/src/modules/components/index.vue index 865540304..929245e52 100644 --- a/packages/applet/src/modules/components/index.vue +++ b/packages/applet/src/modules/components/index.vue @@ -215,21 +215,54 @@ onUnmounted(() => { rpc.functions.off(DevToolsMessagingEvents.INSPECTOR_TREE_UPDATED, onInspectorTreeUpdated) }) -function inspectComponentInspector() { +// #region toggle app +const devtoolsState = useDevToolsState() +const appRecords = computed(() => devtoolsState.appRecords.value.map(app => ({ + label: app.name + (app.version ? ` (${app.version})` : ''), + value: app.id, +}))) + +const normalizedAppRecords = computed(() => appRecords.value.map(app => ({ + label: app.label, + id: app.value, +}))) + +const activeAppRecordId = ref(devtoolsState.activeAppRecordId.value) +watchEffect(() => { + activeAppRecordId.value = devtoolsState.activeAppRecordId.value +}) + +async function toggleApp(id: string, options: { inspectingComponent?: boolean } = {}) { + await rpc.value.toggleApp(id, options) + activeComponentId.value = '' + await getComponentsInspectorTree() +} +// #endregion + +async function inspectComponentInspector() { inspectComponentTipVisible.value = true emit('onInspectComponentStart') - rpc.value.inspectComponentInspector().then((_data) => { - const data = JSON.parse(_data! as unknown as string) + + try { + const data = JSON.parse(await rpc.value.inspectComponentInspector()) + + const appId = data.id.split(':')[0] + if (activeAppRecordId.value !== data.appId) { + await toggleApp(appId, { inspectingComponent: true }) + } + activeComponentId.value = data.id - if (!expandedTreeNodes.value.includes(data.id)) + if (!expandedTreeNodes.value.includes(data.id)) { expandedTreeNodes.value.push(data.id) + } expandedTreeNodes.value = [...new Set([...expandedTreeNodes.value, ...getTargetLinkedNodes(treeNodeLinkedList.value, data.id)])] scrollToActiveTreeNode() - }).finally(() => { + } + finally { inspectComponentTipVisible.value = false emit('onInspectComponentEnd') - }) + } } function cancelInspectComponentInspector() { @@ -280,32 +313,6 @@ function closeComponentRenderCode() { componentRenderCode.value = '' componentRenderCodeVisible.value = false } - -// #region toggle app -const devtoolsState = useDevToolsState() -const appRecords = computed(() => devtoolsState.appRecords.value.map(app => ({ - label: app.name + (app.version ? ` (${app.version})` : ''), - value: app.id, -}))) - -const normalizedAppRecords = computed(() => appRecords.value.map(app => ({ - label: app.label, - id: app.value, -}))) - -const activeAppRecordId = ref(devtoolsState.activeAppRecordId.value) -watchEffect(() => { - activeAppRecordId.value = devtoolsState.activeAppRecordId.value -}) - -function toggleApp(id: string) { - rpc.value.toggleApp(id).then(() => { - activeComponentId.value = '' - getComponentsInspectorTree() - }) -} - -// #endregion