Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: component highlighter #683

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/applet/src/components/tree/TreeViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const props = withDefaults(defineProps<{
depth: 0,
withTag: false,
})
const emit = defineEmits(['hover', 'leave'])
const selectedNodeId = defineModel()
const { expanded, toggleExpanded } = useToggleExpanded()
const { select: _select } = useSelect()
Expand Down Expand Up @@ -43,6 +44,8 @@ function select(id: string) {
:class="{ 'bg-primary-600! active': selectedNodeId === item.id }"
@click="select(item.id)"
@dblclick="toggleExpanded(item.id)"
@mouseover="() => emit('hover', item.id)"
@mouseleave="() => emit('leave')"
>
<ToggleExpanded
v-if="item?.children?.length"
Expand Down Expand Up @@ -86,7 +89,9 @@ function select(id: string) {
<div
v-if="item?.children?.length && expanded.includes(item.id)"
>
<ComponentTreeViewer v-model="selectedNodeId" :data="item?.children" :depth="depth + 1" :with-tag="withTag" />
<ComponentTreeViewer
v-model="selectedNodeId" :data="item?.children" :depth="depth + 1" :with-tag="withTag" @hover="(id) => emit('hover', id)" @leave="emit('leave')"
/>
</div>
</div>
</template>
19 changes: 19 additions & 0 deletions packages/applet/src/composables/component-highlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { rpc } from '@vue/devtools-core'
import { useDebounceFn } from '@vueuse/core'

const THROTTLE_TIME = 200

export function useComponentHighlight() {
const highlight = useDebounceFn((id: string) => {
return rpc.value.highlighComponent(id)
}, THROTTLE_TIME)

const unhighlight = useDebounceFn(() => {
return rpc.value.unhighlight()
}, THROTTLE_TIME)

return {
highlight,
unhighlight,
}
}
4 changes: 3 additions & 1 deletion packages/applet/src/modules/components/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { computed, onUnmounted, ref, watch, watchEffect } from 'vue'
import SelectiveList from '~/components/basic/SelectiveList.vue'
import RootStateViewer from '~/components/state/RootStateViewer.vue'
import ComponentTree from '~/components/tree/TreeViewer.vue'
import { useComponentHighlight } from '~/composables/component-highlight'
import { createSelectedContext } from '~/composables/select'
import { createExpandedContext } from '~/composables/toggle-expanded'
import { filterInspectorState } from '~/utils'
Expand All @@ -34,6 +35,7 @@ const componentTreeLoaded = ref(false)
const inspectComponentTipVisible = ref(false)
const componentRenderCode = ref('')
const componentRenderCodeVisible = ref(false)
const highlighter = useComponentHighlight()

// tree
function dfs(node: { id: string, children?: { id: string }[] }, path: string[] = [], linkedList: string[][] = []) {
Expand Down Expand Up @@ -321,7 +323,7 @@ function toggleApp(id: string) {
</button>
</div>
<div ref="componentTreeContainer" class="no-scrollbar flex-1 select-none overflow-scroll">
<ComponentTree v-model="activeComponentId" :data="tree" :with-tag="true" />
<ComponentTree v-model="activeComponentId" :data="tree" :with-tag="true" @hover="highlighter.highlight" @leave="highlighter.unhighlight" />
</div>
</div>
</Pane>
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/rpc/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,11 @@ export const functions = {
getInspectorInfo(id: string) {
return getInspectorInfo(id)
},
highlighComponent(uid: string) {
return devtools.ctx.hooks.callHook(DevToolsContextHookKeys.COMPONENT_HIGHLIGHT, { uid })
},
unhighlight() {
devtools.ctx.hooks.callHook(DevToolsContextHookKeys.COMPONENT_UNHIGHLIGHT)
return devtools.ctx.hooks.callHook(DevToolsContextHookKeys.COMPONENT_UNHIGHLIGHT)
},
updateDevToolsClientDetected(params: Record<string, boolean>) {
updateDevToolsClientDetected(params)
Expand Down
2 changes: 2 additions & 0 deletions packages/devtools-kit/src/core/component-highlighter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ export function toggleComponentHighLighter(options: ComponentHighLighterOptions)

export function highlight(instance: VueAppInstance) {
const bounds = getComponentBoundingRect(instance)
if (!bounds.width && !bounds.height)
return
const name = getInstanceName(instance)
const container = getContainerElement()
container ? update({ bounds, name }) : create({ bounds, name })
Expand Down