11import { setupDevtoolsPlugin } from '@vue/devtools-api'
22import type { App } from 'vue'
33import { 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'
1711
1812export const setupDevtools = ( app : App , clientData : ClientData ) : void => {
1913 setupDevtoolsPlugin (
2014 {
2115 // fix recursive reference
2216 app : app as never ,
23- id : PLUGIN_ID ,
24- label : PLUGIN_LABEL ,
17+ id : DEVTOOLS . PLUGIN_ID ,
18+ label : DEVTOOLS . PLUGIN_LABEL ,
2519 packageName : '@vuepress/client' ,
2620 homepage : 'https://vuepress.vuejs.org' ,
2721 logo : 'https://vuepress.vuejs.org/images/hero.png' ,
28- componentStateTypes : [ PLUGIN_COMPONENT_STATE_TYPE ] ,
22+ componentStateTypes : [ DEVTOOLS . COMPONENT_STATE_TYPE ] ,
2923 } ,
3024 ( api ) => {
3125 const clientDataEntries = Object . entries ( clientData ) as [
@@ -39,7 +33,7 @@ export const setupDevtools = (app: App, clientData: ClientData): void => {
3933 api . on . inspectComponent ( ( payload ) => {
4034 payload . instanceData . state . push (
4135 ...clientDataEntries . map ( ( [ name , item ] ) => ( {
42- type : PLUGIN_COMPONENT_STATE_TYPE ,
36+ type : DEVTOOLS . COMPONENT_STATE_TYPE ,
4337 editable : false ,
4438 key : name ,
4539 value : item . value ,
@@ -49,38 +43,47 @@ export const setupDevtools = (app: App, clientData: ClientData): void => {
4943
5044 // setup custom inspector
5145 api . addInspector ( {
52- id : INSPECTOR_ID ,
53- label : INSPECTOR_LABEL ,
46+ id : DEVTOOLS . INSPECTOR_ID ,
47+ label : DEVTOOLS . INSPECTOR_LABEL ,
5448 icon : 'article' ,
5549 } )
50+
5651 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 ,
6561 } ) ) ,
66- } ,
67- ]
62+ } ) ,
63+ )
6864 } )
65+
6966 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 ) {
7274 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+ } ) ) ,
7979 }
80+ return
8081 }
82+
83+ // root nodes children state
8184 if ( clientDataKeys . includes ( payload . nodeId as ClientDataKey ) ) {
8285 payload . state = {
83- [ INSPECTOR_CLIENT_DATA_LABEL ] : [
86+ [ DEVTOOLS . INSPECTOR_STATE_SECTION_NAME ] : [
8487 {
8588 key : payload . nodeId ,
8689 value : clientData [ payload . nodeId as ClientDataKey ] . value ,
@@ -93,7 +96,7 @@ export const setupDevtools = (app: App, clientData: ClientData): void => {
9396 // refresh the component state and inspector state
9497 watch ( clientDataValues , ( ) => {
9598 api . notifyComponentUpdate ( )
96- api . sendInspectorState ( INSPECTOR_ID )
99+ api . sendInspectorState ( DEVTOOLS . INSPECTOR_ID )
97100 } )
98101 } ,
99102 )
0 commit comments