Skip to content

Commit 550bcbd

Browse files
authored
feat: component highlighter (#683)
1 parent 0daefe4 commit 550bcbd

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

packages/applet/src/components/tree/TreeViewer.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const props = withDefaults(defineProps<{
1616
depth: 0,
1717
withTag: false,
1818
})
19+
const emit = defineEmits(['hover', 'leave'])
1920
const selectedNodeId = defineModel()
2021
const { expanded, toggleExpanded } = useToggleExpanded()
2122
const { select: _select } = useSelect()
@@ -43,6 +44,8 @@ function select(id: string) {
4344
:class="{ 'bg-primary-600! active': selectedNodeId === item.id }"
4445
@click="select(item.id)"
4546
@dblclick="toggleExpanded(item.id)"
47+
@mouseover="() => emit('hover', item.id)"
48+
@mouseleave="() => emit('leave')"
4649
>
4750
<ToggleExpanded
4851
v-if="item?.children?.length"
@@ -86,7 +89,9 @@ function select(id: string) {
8689
<div
8790
v-if="item?.children?.length && expanded.includes(item.id)"
8891
>
89-
<ComponentTreeViewer v-model="selectedNodeId" :data="item?.children" :depth="depth + 1" :with-tag="withTag" />
92+
<ComponentTreeViewer
93+
v-model="selectedNodeId" :data="item?.children" :depth="depth + 1" :with-tag="withTag" @hover="(id) => emit('hover', id)" @leave="emit('leave')"
94+
/>
9095
</div>
9196
</div>
9297
</template>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { rpc } from '@vue/devtools-core'
2+
import { useDebounceFn } from '@vueuse/core'
3+
4+
const THROTTLE_TIME = 200
5+
6+
export function useComponentHighlight() {
7+
const highlight = useDebounceFn((id: string) => {
8+
return rpc.value.highlighComponent(id)
9+
}, THROTTLE_TIME)
10+
11+
const unhighlight = useDebounceFn(() => {
12+
return rpc.value.unhighlight()
13+
}, THROTTLE_TIME)
14+
15+
return {
16+
highlight,
17+
unhighlight,
18+
}
19+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { computed, onUnmounted, ref, watch, watchEffect } from 'vue'
1515
import SelectiveList from '~/components/basic/SelectiveList.vue'
1616
import RootStateViewer from '~/components/state/RootStateViewer.vue'
1717
import ComponentTree from '~/components/tree/TreeViewer.vue'
18+
import { useComponentHighlight } from '~/composables/component-highlight'
1819
import { createSelectedContext } from '~/composables/select'
1920
import { createExpandedContext } from '~/composables/toggle-expanded'
2021
import { filterInspectorState } from '~/utils'
@@ -34,6 +35,7 @@ const componentTreeLoaded = ref(false)
3435
const inspectComponentTipVisible = ref(false)
3536
const componentRenderCode = ref('')
3637
const componentRenderCodeVisible = ref(false)
38+
const highlighter = useComponentHighlight()
3739
3840
// tree
3941
function dfs(node: { id: string, children?: { id: string }[] }, path: string[] = [], linkedList: string[][] = []) {
@@ -321,7 +323,7 @@ function toggleApp(id: string) {
321323
</button>
322324
</div>
323325
<div ref="componentTreeContainer" class="no-scrollbar flex-1 select-none overflow-scroll">
324-
<ComponentTree v-model="activeComponentId" :data="tree" :with-tag="true" />
326+
<ComponentTree v-model="activeComponentId" :data="tree" :with-tag="true" @hover="highlighter.highlight" @leave="highlighter.unhighlight" />
325327
</div>
326328
</div>
327329
</Pane>

packages/core/src/rpc/global.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,11 @@ export const functions = {
155155
getInspectorInfo(id: string) {
156156
return getInspectorInfo(id)
157157
},
158+
highlighComponent(uid: string) {
159+
return devtools.ctx.hooks.callHook(DevToolsContextHookKeys.COMPONENT_HIGHLIGHT, { uid })
160+
},
158161
unhighlight() {
159-
devtools.ctx.hooks.callHook(DevToolsContextHookKeys.COMPONENT_UNHIGHLIGHT)
162+
return devtools.ctx.hooks.callHook(DevToolsContextHookKeys.COMPONENT_UNHIGHLIGHT)
160163
},
161164
updateDevToolsClientDetected(params: Record<string, boolean>) {
162165
updateDevToolsClientDetected(params)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ export function toggleComponentHighLighter(options: ComponentHighLighterOptions)
145145

146146
export function highlight(instance: VueAppInstance) {
147147
const bounds = getComponentBoundingRect(instance)
148+
if (!bounds.width && !bounds.height)
149+
return
148150
const name = getInstanceName(instance)
149151
const container = getContainerElement()
150152
container ? update({ bounds, name }) : create({ bounds, name })

0 commit comments

Comments
 (0)