1
1
import { setupDevtoolsPlugin } from '@vue/devtools-api'
2
2
import type { App } from 'vue'
3
3
import { watch } from 'vue'
4
- import type { ClientData } from './types/index.js'
5
-
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_CLIENT_DATA_ID = 'client-data'
13
- const INSPECTOR_CLIENT_DATA_LABEL = 'Client Data'
14
-
15
- type ClientDataKey = keyof ClientData
16
- type ClientDataValue = ClientData [ ClientDataKey ]
4
+ import type { ClientData } from '../types/index.js'
5
+ import * as DEVTOOLS from './constants.js'
6
+ import type {
7
+ ClientDataKey ,
8
+ ClientDataValue ,
9
+ InspectorNodeConfig ,
10
+ } from './types.js'
17
11
18
12
export const setupDevtools = ( app : App , clientData : ClientData ) : void => {
19
13
setupDevtoolsPlugin (
20
14
{
21
15
// fix recursive reference
22
16
app : app as never ,
23
- id : PLUGIN_ID ,
24
- label : PLUGIN_LABEL ,
17
+ id : DEVTOOLS . PLUGIN_ID ,
18
+ label : DEVTOOLS . PLUGIN_LABEL ,
25
19
packageName : '@vuepress/client' ,
26
20
homepage : 'https://vuepress.vuejs.org' ,
27
21
logo : 'https://vuepress.vuejs.org/images/hero.png' ,
28
- componentStateTypes : [ PLUGIN_COMPONENT_STATE_TYPE ] ,
22
+ componentStateTypes : [ DEVTOOLS . COMPONENT_STATE_TYPE ] ,
29
23
} ,
30
24
( api ) => {
31
25
const clientDataEntries = Object . entries ( clientData ) as [
@@ -39,7 +33,7 @@ export const setupDevtools = (app: App, clientData: ClientData): void => {
39
33
api . on . inspectComponent ( ( payload ) => {
40
34
payload . instanceData . state . push (
41
35
...clientDataEntries . map ( ( [ name , item ] ) => ( {
42
- type : PLUGIN_COMPONENT_STATE_TYPE ,
36
+ type : DEVTOOLS . COMPONENT_STATE_TYPE ,
43
37
editable : false ,
44
38
key : name ,
45
39
value : item . value ,
@@ -49,38 +43,47 @@ export const setupDevtools = (app: App, clientData: ClientData): void => {
49
43
50
44
// setup custom inspector
51
45
api . addInspector ( {
52
- id : INSPECTOR_ID ,
53
- label : INSPECTOR_LABEL ,
46
+ id : DEVTOOLS . INSPECTOR_ID ,
47
+ label : DEVTOOLS . INSPECTOR_LABEL ,
54
48
icon : 'article' ,
55
49
} )
50
+
56
51
api . on . getInspectorTree ( ( payload ) => {
57
- if ( payload . inspectorId !== INSPECTOR_ID ) return
58
- payload . rootNodes = [
59
- {
60
- id : INSPECTOR_CLIENT_DATA_ID ,
61
- label : INSPECTOR_CLIENT_DATA_LABEL ,
62
- children : clientDataKeys . map ( ( name ) => ( {
63
- id : name ,
64
- label : name ,
52
+ if ( payload . inspectorId !== DEVTOOLS . INSPECTOR_ID ) return
53
+
54
+ payload . rootNodes = Object . values ( DEVTOOLS . INSPECTOR_NODES ) . map (
55
+ ( node ) => ( {
56
+ id : node . id ,
57
+ label : node . label ,
58
+ children : node . keys . map ( ( key : ClientDataKey ) => ( {
59
+ id : key ,
60
+ label : key ,
65
61
} ) ) ,
66
- } ,
67
- ]
62
+ } ) ,
63
+ )
68
64
} )
65
+
69
66
api . on . getInspectorState ( ( payload ) => {
70
- if ( payload . inspectorId !== INSPECTOR_ID ) return
71
- if ( payload . nodeId === INSPECTOR_CLIENT_DATA_ID ) {
67
+ if ( payload . inspectorId !== DEVTOOLS . INSPECTOR_ID ) return
68
+
69
+ // root nodes state
70
+ const inspectorNode = DEVTOOLS . INSPECTOR_NODES [ payload . nodeId ] as
71
+ | InspectorNodeConfig
72
+ | undefined
73
+ if ( inspectorNode ) {
72
74
payload . state = {
73
- [ INSPECTOR_CLIENT_DATA_LABEL ] : clientDataEntries . map (
74
- ( [ name , item ] ) => ( {
75
- key : name ,
76
- value : item . value ,
77
- } ) ,
78
- ) ,
75
+ [ inspectorNode . label ] : inspectorNode . keys . map ( ( key ) => ( {
76
+ key,
77
+ value : clientData [ key ] . value ,
78
+ } ) ) ,
79
79
}
80
+ return
80
81
}
82
+
83
+ // root nodes children state
81
84
if ( clientDataKeys . includes ( payload . nodeId as ClientDataKey ) ) {
82
85
payload . state = {
83
- [ INSPECTOR_CLIENT_DATA_LABEL ] : [
86
+ [ DEVTOOLS . INSPECTOR_STATE_SECTION_NAME ] : [
84
87
{
85
88
key : payload . nodeId ,
86
89
value : clientData [ payload . nodeId as ClientDataKey ] . value ,
@@ -93,7 +96,7 @@ export const setupDevtools = (app: App, clientData: ClientData): void => {
93
96
// refresh the component state and inspector state
94
97
watch ( clientDataValues , ( ) => {
95
98
api . notifyComponentUpdate ( )
96
- api . sendInspectorState ( INSPECTOR_ID )
99
+ api . sendInspectorState ( DEVTOOLS . INSPECTOR_ID )
97
100
} )
98
101
} ,
99
102
)
0 commit comments