Skip to content

Commit 5568abe

Browse files
committedJul 20, 2022
feat(client): add devtools custom inspector
1 parent 870d618 commit 5568abe

File tree

1 file changed

+69
-7
lines changed

1 file changed

+69
-7
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { setupDevtoolsPlugin } from '@vue/devtools-api'
2+
import { watch } from 'vue'
23
import type { App } from 'vue'
34
import type { GlobalComputed } from './setupGlobalComputed'
45

5-
const COMPONENT_STATE_TYPE = 'VuePress'
6+
const PLUGIN_ID = 'org.vuejs.vuepress'
7+
const PLUGIN_LABEL = 'VuePress'
8+
const PLUGIN_COMPONENT_STATE_TYPE = PLUGIN_LABEL
9+
10+
const INSPECTOR_ID = PLUGIN_ID
11+
const INSPECTOR_LABEL = PLUGIN_LABEL
12+
const INSPECTOR_GLOBAL_COMPUTED_ID = 'global-computed'
13+
const INSPECTOR_GLOBAL_COMPUTED_LABEL = 'Global Computed'
614

715
export const setupDevtools = (
816
app: App,
@@ -12,24 +20,78 @@ export const setupDevtools = (
1220
{
1321
// fix recursive reference
1422
app: app as any,
15-
id: 'org.vuejs.vuepress',
16-
label: 'VuePress',
23+
id: PLUGIN_ID,
24+
label: PLUGIN_LABEL,
1725
packageName: '@vuepress/client',
1826
homepage: 'https://v2.vuepress.vuejs.org',
1927
logo: 'https://v2.vuepress.vuejs.org/images/hero.png',
20-
componentStateTypes: [COMPONENT_STATE_TYPE],
28+
componentStateTypes: [PLUGIN_COMPONENT_STATE_TYPE],
2129
},
2230
(api) => {
31+
const globalComputedEntries = Object.entries(globalComputed)
32+
const globalComputedKeys = Object.keys(globalComputed)
33+
const globalComputedValues = Object.values(globalComputed)
34+
35+
// setup component state
2336
api.on.inspectComponent((payload) => {
2437
payload.instanceData.state.push(
25-
...Object.entries(globalComputed).map(([name, item]) => ({
26-
type: COMPONENT_STATE_TYPE,
27-
key: name,
38+
...globalComputedEntries.map(([name, item]) => ({
39+
type: PLUGIN_COMPONENT_STATE_TYPE,
2840
editable: false,
41+
key: name,
2942
value: item.value,
3043
}))
3144
)
3245
})
46+
47+
// setup custom inspector
48+
api.addInspector({
49+
id: INSPECTOR_ID,
50+
label: INSPECTOR_LABEL,
51+
icon: 'article',
52+
})
53+
api.on.getInspectorTree((payload) => {
54+
if (payload.inspectorId !== INSPECTOR_ID) return
55+
payload.rootNodes = [
56+
{
57+
id: INSPECTOR_GLOBAL_COMPUTED_ID,
58+
label: INSPECTOR_GLOBAL_COMPUTED_LABEL,
59+
children: globalComputedKeys.map((name) => ({
60+
id: name,
61+
label: name,
62+
})),
63+
},
64+
]
65+
})
66+
api.on.getInspectorState((payload) => {
67+
if (payload.inspectorId !== INSPECTOR_ID) return
68+
if (payload.nodeId === INSPECTOR_GLOBAL_COMPUTED_ID) {
69+
payload.state = {
70+
[INSPECTOR_GLOBAL_COMPUTED_LABEL]: globalComputedEntries.map(
71+
([name, item]) => ({
72+
key: name,
73+
value: item.value,
74+
})
75+
),
76+
}
77+
}
78+
if (globalComputedKeys.includes(payload.nodeId)) {
79+
payload.state = {
80+
[INSPECTOR_GLOBAL_COMPUTED_LABEL]: [
81+
{
82+
key: payload.nodeId,
83+
value: globalComputed[payload.nodeId].value,
84+
},
85+
],
86+
}
87+
}
88+
})
89+
90+
// refresh the component state and inspector state
91+
watch(globalComputedValues, () => {
92+
api.notifyComponentUpdate()
93+
api.sendInspectorState(INSPECTOR_ID)
94+
})
3395
}
3496
)
3597
}

0 commit comments

Comments
 (0)
Please sign in to comment.