Skip to content

Commit 22a1397

Browse files
feat(api): node actions for custom inspector (#1820)
Co-authored-by: Guillaume Chau <[email protected]>
1 parent e9398cd commit 22a1397

File tree

7 files changed

+56
-3
lines changed

7 files changed

+56
-3
lines changed

packages/api/src/api/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ export interface CustomInspectorOptions {
9696
tooltip?: string
9797
action: () => void | Promise<void>
9898
}[]
99+
nodeActions?: {
100+
icon: string
101+
tooltip?: string
102+
action: (nodeId: string) => void | Promise<void>
103+
}[]
99104
}
100105

101106
export interface CustomInspectorNode {

packages/app-backend-core/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,12 +609,12 @@ function connectBridge () {
609609
}
610610
})
611611

612-
ctx.bridge.on(BridgeEvents.TO_BACK_CUSTOM_INSPECTOR_ACTION, async ({ inspectorId, appId, actionIndex }) => {
612+
ctx.bridge.on(BridgeEvents.TO_BACK_CUSTOM_INSPECTOR_ACTION, async ({ inspectorId, appId, actionIndex, actionType, args }) => {
613613
const inspector = await getInspectorWithAppId(inspectorId, appId, ctx)
614614
if (inspector) {
615-
const action = inspector.actions[actionIndex]
615+
const action = inspector[actionType ?? 'actions'][actionIndex]
616616
try {
617-
await action.action()
617+
await action.action(...(args ?? []))
618618
} catch (e) {
619619
if (SharedData.debugInfo) {
620620
console.error(e)

packages/app-backend-core/src/inspector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export async function sendCustomInspectors (ctx: BackendContext) {
5656
icon: a.icon,
5757
tooltip: a.tooltip,
5858
})),
59+
nodeActions: i.nodeActions?.map(a => ({
60+
icon: a.icon,
61+
tooltip: a.tooltip,
62+
})),
5963
})
6064
}
6165
ctx.bridge.send(BridgeEvents.TO_FRONT_CUSTOM_INSPECTOR_LIST, {

packages/app-frontend/src/features/inspector/custom/CustomInspectorSelectedNodePane.vue

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import EmptyPane from '@front/features/layout/EmptyPane.vue'
33
44
import { watch, defineComponent, ref } from '@vue/composition-api'
5+
import { BridgeEvents } from '@vue-devtools/shared-utils'
6+
import { useBridge } from '@front/features/bridge'
57
import { useCurrentInspector } from './composable'
68
import StateInspector from '../StateInspector.vue'
79
@@ -32,11 +34,29 @@ export default defineComponent({
3234
}
3335
})
3436
37+
// Custom actions
38+
const {
39+
bridge,
40+
} = useBridge()
41+
42+
function executeCustomAction (index: number) {
43+
bridge.send(BridgeEvents.TO_BACK_CUSTOM_INSPECTOR_ACTION, {
44+
inspectorId: inspector.value.id,
45+
appId: inspector.value.appId,
46+
actionIndex: index,
47+
actionType: 'nodeActions',
48+
args: [
49+
inspector.value.selectedNodeId,
50+
],
51+
})
52+
}
53+
3554
return {
3655
inspector,
3756
filteredState,
3857
editState,
3958
stateInspector,
59+
executeCustomAction,
4060
}
4161
},
4262
})
@@ -58,6 +78,15 @@ export default defineComponent({
5878
:placeholder="inspector.stateFilterPlaceholder || 'Filter state...'"
5979
class="search flex-1 flat ml-2"
6080
/>
81+
82+
<VueButton
83+
v-for="(action, index) of inspector.nodeActions"
84+
:key="index"
85+
v-tooltip="action.tooltip"
86+
class="icon-button flat"
87+
:icon-left="action.icon"
88+
@click="executeCustomAction(index)"
89+
/>
6190
</div>
6291

6392
<StateInspector

packages/app-frontend/src/features/inspector/custom/composable.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export interface InspectorFromBackend {
1717
icon: string
1818
tooltip?: string
1919
}[]
20+
nodeActions?: {
21+
icon: string
22+
tooltip?: string
23+
}[]
2024
}
2125

2226
export interface Inspector extends InspectorFromBackend {

packages/shared-utils/src/consts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export enum BridgeEvents {
8484
TO_FRONT_CUSTOM_INSPECTOR_STATE = 'f:custom-inspector:state',
8585
TO_BACK_CUSTOM_INSPECTOR_EDIT_STATE = 'b:custom-inspector:edit-state',
8686
TO_BACK_CUSTOM_INSPECTOR_ACTION = 'b:custom-inspector:action',
87+
TO_BACK_CUSTOM_INSPECTOR_NODE_ACTION = 'b:custom-inspector:node-action',
8788
TO_FRONT_CUSTOM_INSPECTOR_SELECT_NODE = 'f:custom-inspector:select-node',
8889

8990
// Custom state

packages/shell-dev-vue3/src/devtools-plugin/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ export default {
197197
},
198198
},
199199
],
200+
nodeActions: [
201+
{
202+
icon: 'help',
203+
tooltip: 'Test custom node action',
204+
action: (arg1) => {
205+
console.log('Node action', arg1)
206+
api.selectInspectorNode('test-inspector', 'child')
207+
},
208+
},
209+
],
200210
})
201211

202212
api.addInspector({

0 commit comments

Comments
 (0)