1
1
import { setupDevtoolsPlugin } from '@vue/devtools-api'
2
+ import { watch } from 'vue'
2
3
import type { App } from 'vue'
3
4
import type { GlobalComputed } from './setupGlobalComputed'
4
5
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'
6
14
7
15
export const setupDevtools = (
8
16
app : App ,
@@ -12,24 +20,78 @@ export const setupDevtools = (
12
20
{
13
21
// fix recursive reference
14
22
app : app as any ,
15
- id : 'org.vuejs.vuepress' ,
16
- label : 'VuePress' ,
23
+ id : PLUGIN_ID ,
24
+ label : PLUGIN_LABEL ,
17
25
packageName : '@vuepress/client' ,
18
26
homepage : 'https://v2.vuepress.vuejs.org' ,
19
27
logo : 'https://v2.vuepress.vuejs.org/images/hero.png' ,
20
- componentStateTypes : [ COMPONENT_STATE_TYPE ] ,
28
+ componentStateTypes : [ PLUGIN_COMPONENT_STATE_TYPE ] ,
21
29
} ,
22
30
( api ) => {
31
+ const globalComputedEntries = Object . entries ( globalComputed )
32
+ const globalComputedKeys = Object . keys ( globalComputed )
33
+ const globalComputedValues = Object . values ( globalComputed )
34
+
35
+ // setup component state
23
36
api . on . inspectComponent ( ( payload ) => {
24
37
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 ,
28
40
editable : false ,
41
+ key : name ,
29
42
value : item . value ,
30
43
} ) )
31
44
)
32
45
} )
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
+ } )
33
95
}
34
96
)
35
97
}
0 commit comments